blob: e3889b2276af66c570f5bbeca71c67a75d671c03 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000028#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000029#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000030#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Jim Ingham151c0322015-09-15 21:13:50 +000034#include "lldb/Expression/UserExpression.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000035#include "Plugins/ExpressionParser/Clang/ClangASTSource.h"
36#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
37#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.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"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000045#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ObjectFile.h"
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000047#include "lldb/Symbol/Function.h"
48#include "lldb/Symbol/Symbol.h"
Jim Ingham151c0322015-09-15 21:13:50 +000049#include "lldb/Target/Language.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000050#include "lldb/Target/LanguageRuntime.h"
51#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000053#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000054#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000055#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000056#include "lldb/Target/Thread.h"
57#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
59using namespace lldb;
60using namespace lldb_private;
61
Jim Ingham4bddaeb2012-02-16 06:50:00 +000062ConstString &
63Target::GetStaticBroadcasterClass ()
64{
65 static ConstString class_name ("lldb.target");
66 return class_name;
67}
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069//----------------------------------------------------------------------
70// Target constructor
71//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000072Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000073 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000074 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000075 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000076 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000077 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000078 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000079 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000080 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000081 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 m_breakpoint_list (false),
83 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000084 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000085 m_process_sp (),
86 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000088 m_scratch_ast_context_ap (),
89 m_scratch_ast_source_ap (),
90 m_ast_importer_ap (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000091 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000092 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000093 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000094 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000095 m_suppress_stop_hooks (false),
96 m_is_dummy_target(is_dummy_target)
97
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000099 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
100 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
101 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +0000102 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +0000103 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000104
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000105 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000106
Greg Clayton5160ce52013-03-27 23:08:40 +0000107 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000109 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000110 if (m_arch.IsValid())
111 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000112 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 +0000113 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
Jim Ingham893c9322014-11-22 01:42:44 +0000116void
117Target::PrimeFromDummyTarget(Target *target)
118{
119 if (!target)
120 return;
121
122 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000123
124 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
125 {
126 if (breakpoint_sp->IsInternal())
127 continue;
128
129 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
130 AddBreakpoint (new_bp, false);
131 }
Jim Ingham893c9322014-11-22 01:42:44 +0000132}
133
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134//----------------------------------------------------------------------
135// Destructor
136//----------------------------------------------------------------------
137Target::~Target()
138{
Greg Clayton5160ce52013-03-27 23:08:40 +0000139 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000141 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 DeleteCurrentProcess ();
143}
144
145void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000146Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147{
Greg Clayton89411422010-10-08 00:21:05 +0000148// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000149 if (description_level != lldb::eDescriptionLevelBrief)
150 {
151 s->Indent();
152 s->PutCString("Target\n");
153 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000154 m_images.Dump(s);
155 m_breakpoint_list.Dump(s);
156 m_internal_breakpoint_list.Dump(s);
157 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000158 }
159 else
160 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000161 Module *exe_module = GetExecutableModulePointer();
162 if (exe_module)
163 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000164 else
165 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000166 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
169void
Greg Clayton90ba8112012-12-05 00:16:59 +0000170Target::CleanupProcess ()
171{
172 // Do any cleanup of the target we need to do between process instances.
173 // NB It is better to do this before destroying the process in case the
174 // clean up needs some help from the process.
175 m_breakpoint_list.ClearAllBreakpointSites();
176 m_internal_breakpoint_list.ClearAllBreakpointSites();
177 // Disable watchpoints just on the debugger side.
178 Mutex::Locker locker;
179 this->GetWatchpointList().GetListMutex(locker);
180 DisableAllWatchpoints(false);
181 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000182 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000183}
184
185void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186Target::DeleteCurrentProcess ()
187{
188 if (m_process_sp.get())
189 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000190 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 if (m_process_sp->IsAlive())
Jason Molendaede31932015-04-17 05:01:58 +0000192 m_process_sp->Destroy(false);
Jim Inghamd0a3e122011-02-16 17:54:55 +0000193
194 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
Greg Clayton90ba8112012-12-05 00:16:59 +0000196 CleanupProcess ();
197
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 m_process_sp.reset();
199 }
200}
201
202const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000203Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204{
205 DeleteCurrentProcess ();
Zachary Turner7529df92015-09-01 20:02:29 +0000206 m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 return m_process_sp;
208}
209
210const lldb::ProcessSP &
211Target::GetProcessSP () const
212{
213 return m_process_sp;
214}
215
Greg Clayton3418c852011-08-10 02:10:13 +0000216void
217Target::Destroy()
218{
219 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000220 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000221 DeleteCurrentProcess ();
222 m_platform_sp.reset();
223 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000224 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000225 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000226 const bool notify = false;
227 m_breakpoint_list.RemoveAll(notify);
228 m_internal_breakpoint_list.RemoveAll(notify);
229 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000230 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000231 m_search_filter_sp.reset();
232 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000233 m_stop_hooks.clear();
234 m_stop_hook_next_id = 0;
235 m_suppress_stop_hooks = false;
236}
237
238
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239BreakpointList &
240Target::GetBreakpointList(bool internal)
241{
242 if (internal)
243 return m_internal_breakpoint_list;
244 else
245 return m_breakpoint_list;
246}
247
248const BreakpointList &
249Target::GetBreakpointList(bool internal) const
250{
251 if (internal)
252 return m_internal_breakpoint_list;
253 else
254 return m_breakpoint_list;
255}
256
257BreakpointSP
258Target::GetBreakpointByID (break_id_t break_id)
259{
260 BreakpointSP bp_sp;
261
262 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
263 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
264 else
265 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
266
267 return bp_sp;
268}
269
270BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000271Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000272 const FileSpecList *source_file_spec_list,
273 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000274 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000275 bool hardware,
276 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277{
Jim Ingham87df91b2011-09-23 00:54:11 +0000278 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000279 if (move_to_nearest_code == eLazyBoolCalculate)
280 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
281 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex, !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000282 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000283}
284
285
286BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000287Target::CreateBreakpoint (const FileSpecList *containingModules,
288 const FileSpec &file,
289 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000290 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000291 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000292 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000293 bool hardware,
294 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000295{
Greg Clayton1f746072012-08-29 21:13:06 +0000296 if (check_inlines == eLazyBoolCalculate)
297 {
298 const InlineStrategy inline_strategy = GetInlineStrategy();
299 switch (inline_strategy)
300 {
301 case eInlineBreakpointsNever:
302 check_inlines = eLazyBoolNo;
303 break;
304
305 case eInlineBreakpointsHeaders:
306 if (file.IsSourceImplementationFile())
307 check_inlines = eLazyBoolNo;
308 else
309 check_inlines = eLazyBoolYes;
310 break;
311
312 case eInlineBreakpointsAlways:
313 check_inlines = eLazyBoolYes;
314 break;
315 }
316 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000317 SearchFilterSP filter_sp;
318 if (check_inlines == eLazyBoolNo)
319 {
320 // Not checking for inlines, we are looking only for matching compile units
321 FileSpecList compile_unit_list;
322 compile_unit_list.Append (file);
323 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
324 }
325 else
326 {
327 filter_sp = GetSearchFilterForModuleList (containingModules);
328 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000329 if (skip_prologue == eLazyBoolCalculate)
330 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000331 if (move_to_nearest_code == eLazyBoolCalculate)
332 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000333
Greg Clayton1f746072012-08-29 21:13:06 +0000334 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
335 file,
336 line_no,
337 check_inlines,
Ilia K055ad9b2015-05-18 13:41:01 +0000338 skip_prologue,
339 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000340 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341}
342
343
344BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000345Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000348
349 // Check for any reason we want to move this breakpoint to other address.
350 addr = GetBreakableLoadAddress(addr);
351
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000352 // Attempt to resolve our load address if possible, though it is ok if
353 // it doesn't resolve to section/offset.
354
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000355 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000356 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000357 if (!so_addr.IsValid())
358 {
359 // The address didn't resolve, so just set this as an absolute address
360 so_addr.SetOffset (addr);
361 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000362 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 return bp_sp;
364}
365
366BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000367Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000369 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000371 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372}
373
374BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000375Target::CreateBreakpoint (const FileSpecList *containingModules,
376 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000377 const char *func_name,
378 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000379 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000380 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000381 bool internal,
382 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000384 BreakpointSP bp_sp;
385 if (func_name)
386 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000387 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000388
389 if (skip_prologue == eLazyBoolCalculate)
390 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000391 if (language == lldb::eLanguageTypeUnknown)
392 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000393
Greg Claytond16e1e52011-07-12 17:06:17 +0000394 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
395 func_name,
396 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000397 language,
Greg Claytond16e1e52011-07-12 17:06:17 +0000398 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000399 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000400 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000401 }
402 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
Jim Inghamfab10e82012-03-06 00:37:27 +0000405lldb::BreakpointSP
406Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000407 const FileSpecList *containingSourceFiles,
408 const std::vector<std::string> &func_names,
409 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000410 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000411 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000412 bool internal,
413 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000414{
415 BreakpointSP bp_sp;
416 size_t num_names = func_names.size();
417 if (num_names > 0)
418 {
419 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000420
421 if (skip_prologue == eLazyBoolCalculate)
422 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000423 if (language == lldb::eLanguageTypeUnknown)
424 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000425
426 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000427 func_names,
428 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000429 language,
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 Inghamfab10e82012-03-06 00:37:27 +0000432 }
433 return bp_sp;
434}
435
Jim Ingham133e0fb2012-03-03 02:05:11 +0000436BreakpointSP
437Target::CreateBreakpoint (const FileSpecList *containingModules,
438 const FileSpecList *containingSourceFiles,
439 const char *func_names[],
440 size_t num_names,
441 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000442 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000443 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000444 bool internal,
445 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000446{
447 BreakpointSP bp_sp;
448 if (num_names > 0)
449 {
450 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
451
Greg Clayton03da4cc2013-04-19 21:31:16 +0000452 if (skip_prologue == eLazyBoolCalculate)
453 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000454 if (language == lldb::eLanguageTypeUnknown)
455 language = GetLanguage();
456
Greg Clayton03da4cc2013-04-19 21:31:16 +0000457
458 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000459 func_names,
460 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000461 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000462 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000463 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000464 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000465 }
466 return bp_sp;
467}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468
469SearchFilterSP
470Target::GetSearchFilterForModule (const FileSpec *containingModule)
471{
472 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473 if (containingModule != NULL)
474 {
475 // TODO: We should look into sharing module based search filters
476 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000477 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478 }
479 else
480 {
481 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000482 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 filter_sp = m_search_filter_sp;
484 }
485 return filter_sp;
486}
487
Jim Ingham969795f2011-09-21 01:17:13 +0000488SearchFilterSP
489Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
490{
491 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000492 if (containingModules && containingModules->GetSize() != 0)
493 {
494 // TODO: We should look into sharing module based search filters
495 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000496 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000497 }
498 else
499 {
500 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000501 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000502 filter_sp = m_search_filter_sp;
503 }
504 return filter_sp;
505}
506
Jim Ingham87df91b2011-09-23 00:54:11 +0000507SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000508Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
509 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000510{
511 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
512 return GetSearchFilterForModuleList(containingModules);
513
514 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000515 if (containingModules == NULL)
516 {
517 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
518 // but that will take a little reworking.
519
Greg Claytone1cd1be2012-01-29 20:56:30 +0000520 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000521 }
522 else
523 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000524 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000525 }
526 return filter_sp;
527}
528
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000529BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000530Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000531 const FileSpecList *containingSourceFiles,
532 RegularExpression &func_regex,
533 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000534 bool internal,
535 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536{
Jim Ingham87df91b2011-09-23 00:54:11 +0000537 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000538 bool skip =
539 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
540 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000541 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
542 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000543 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544
Jim Ingham1460e4b2014-01-10 23:46:59 +0000545 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546}
547
Jim Ingham219ba192012-03-05 04:47:34 +0000548lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000549Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal, Args *additional_args, Error *error)
Jim Ingham219ba192012-03-05 04:47:34 +0000550{
Jim Inghama72b31c2015-04-22 19:42:18 +0000551 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
552 if (exc_bkpt_sp && additional_args)
553 {
554 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
555 if (precondition_sp && additional_args)
556 {
557 if (error)
558 *error = precondition_sp->ConfigurePrecondition(*additional_args);
559 else
560 precondition_sp->ConfigurePrecondition(*additional_args);
561 }
562 }
563 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000564}
Jim Inghama72b31c2015-04-22 19:42:18 +0000565
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000567Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568{
569 BreakpointSP bp_sp;
570 if (filter_sp && resolver_sp)
571 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000572 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000574 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000576 return bp_sp;
577}
578
579void
580Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
581{
582 if (!bp_sp)
583 return;
584 if (internal)
585 m_internal_breakpoint_list.Add (bp_sp, false);
586 else
587 m_breakpoint_list.Add (bp_sp, true);
588
589 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
590 if (log)
591 {
592 StreamString s;
593 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
594 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
595 }
596
597 bp_sp->ResolveBreakpoint();
598
599 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000600 {
601 m_last_created_breakpoint = bp_sp;
602 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603}
604
Johnny Chen86364b42011-09-20 23:28:55 +0000605bool
606Target::ProcessIsValid()
607{
608 return (m_process_sp && m_process_sp->IsAlive());
609}
610
Johnny Chenb90827e2012-06-04 23:19:54 +0000611static bool
612CheckIfWatchpointsExhausted(Target *target, Error &error)
613{
614 uint32_t num_supported_hardware_watchpoints;
615 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
616 if (rc.Success())
617 {
618 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
619 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
620 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
621 num_supported_hardware_watchpoints);
622 }
623 return false;
624}
625
Johnny Chen01a67862011-10-14 00:42:25 +0000626// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000627// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000628WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000629Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000630{
Greg Clayton5160ce52013-03-27 23:08:40 +0000631 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000632 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000633 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000634 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000635
Johnny Chen01a67862011-10-14 00:42:25 +0000636 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000637 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000638 {
639 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000640 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000641 }
Jim Inghamc6462312013-06-18 21:52:48 +0000642
Johnny Chen45e541f2011-09-14 22:20:15 +0000643 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000644 {
645 if (size == 0)
646 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
647 else
Daniel Malead01b2952012-11-29 21:49:15 +0000648 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000649 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000650 }
Jim Inghamc6462312013-06-18 21:52:48 +0000651
652 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
653 {
654 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
655 }
Johnny Chen7313a642011-09-13 01:15:36 +0000656
Johnny Chen01a67862011-10-14 00:42:25 +0000657 // Currently we only support one watchpoint per address, with total number
658 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000659
660 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000661 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 +0000662 Mutex::Locker locker;
663 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000664 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000665 if (matched_sp)
666 {
Johnny Chen0c406372011-09-14 20:23:45 +0000667 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000668 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000669 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
670 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000671 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000672 if (size == old_size && kind == old_type)
673 {
Johnny Chen01a67862011-10-14 00:42:25 +0000674 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000675 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000676 }
677 else
678 {
Johnny Chen01a67862011-10-14 00:42:25 +0000679 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000680 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
681 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000682 }
Johnny Chen3c532582011-09-13 23:29:31 +0000683 }
684
Jason Molenda727e3922012-12-05 23:07:34 +0000685 if (!wp_sp)
686 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000687 wp_sp.reset(new Watchpoint(*this, addr, size, type));
688 wp_sp->SetWatchpointType(kind, notify);
689 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000690 }
Johnny Chen0c406372011-09-14 20:23:45 +0000691
Jim Ingham1b5792e2012-12-18 02:03:49 +0000692 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000693 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000694 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
695 __FUNCTION__,
696 error.Success() ? "succeeded" : "failed",
697 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000698
Jason Molenda727e3922012-12-05 23:07:34 +0000699 if (error.Fail())
700 {
Johnny Chen41b77262012-03-26 22:00:10 +0000701 // Enabling the watchpoint on the device side failed.
702 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000703 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000704 // See if we could provide more helpful error message.
705 if (!CheckIfWatchpointsExhausted(this, error))
706 {
707 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000708 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000709 }
Johnny Chen01a67862011-10-14 00:42:25 +0000710 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000711 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000712 else
Johnny Chen01a67862011-10-14 00:42:25 +0000713 m_last_created_watchpoint = wp_sp;
714 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000715}
716
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717void
718Target::RemoveAllBreakpoints (bool internal_also)
719{
Greg Clayton5160ce52013-03-27 23:08:40 +0000720 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 if (log)
722 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
723
Greg Clayton9fed0d82010-07-23 23:33:17 +0000724 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000726 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000727
728 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729}
730
731void
732Target::DisableAllBreakpoints (bool internal_also)
733{
Greg Clayton5160ce52013-03-27 23:08:40 +0000734 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735 if (log)
736 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
737
738 m_breakpoint_list.SetEnabledAll (false);
739 if (internal_also)
740 m_internal_breakpoint_list.SetEnabledAll (false);
741}
742
743void
744Target::EnableAllBreakpoints (bool internal_also)
745{
Greg Clayton5160ce52013-03-27 23:08:40 +0000746 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 if (log)
748 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
749
750 m_breakpoint_list.SetEnabledAll (true);
751 if (internal_also)
752 m_internal_breakpoint_list.SetEnabledAll (true);
753}
754
755bool
756Target::RemoveBreakpointByID (break_id_t break_id)
757{
Greg Clayton5160ce52013-03-27 23:08:40 +0000758 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 if (log)
760 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
761
762 if (DisableBreakpointByID (break_id))
763 {
764 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000765 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000767 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000768 if (m_last_created_breakpoint)
769 {
770 if (m_last_created_breakpoint->GetID() == break_id)
771 m_last_created_breakpoint.reset();
772 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000773 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000774 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775 return true;
776 }
777 return false;
778}
779
780bool
781Target::DisableBreakpointByID (break_id_t break_id)
782{
Greg Clayton5160ce52013-03-27 23:08:40 +0000783 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784 if (log)
785 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
786
787 BreakpointSP bp_sp;
788
789 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
790 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
791 else
792 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
793 if (bp_sp)
794 {
795 bp_sp->SetEnabled (false);
796 return true;
797 }
798 return false;
799}
800
801bool
802Target::EnableBreakpointByID (break_id_t break_id)
803{
Greg Clayton5160ce52013-03-27 23:08:40 +0000804 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 if (log)
806 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
807 __FUNCTION__,
808 break_id,
809 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
810
811 BreakpointSP bp_sp;
812
813 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
814 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
815 else
816 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
817
818 if (bp_sp)
819 {
820 bp_sp->SetEnabled (true);
821 return true;
822 }
823 return false;
824}
825
Johnny Chenedf50372011-09-23 21:21:43 +0000826// The flag 'end_to_end', default to true, signifies that the operation is
827// performed end to end, for both the debugger and the debuggee.
828
Johnny Chen01a67862011-10-14 00:42:25 +0000829// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
830// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000831bool
Johnny Chen01a67862011-10-14 00:42:25 +0000832Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000833{
Greg Clayton5160ce52013-03-27 23:08:40 +0000834 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000835 if (log)
836 log->Printf ("Target::%s\n", __FUNCTION__);
837
Johnny Chenedf50372011-09-23 21:21:43 +0000838 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000839 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000840 return true;
841 }
842
843 // Otherwise, it's an end to end operation.
844
Johnny Chen86364b42011-09-20 23:28:55 +0000845 if (!ProcessIsValid())
846 return false;
847
Johnny Chen01a67862011-10-14 00:42:25 +0000848 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000849 for (size_t i = 0; i < num_watchpoints; ++i)
850 {
Johnny Chen01a67862011-10-14 00:42:25 +0000851 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
852 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000853 return false;
854
Johnny Chen01a67862011-10-14 00:42:25 +0000855 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000856 if (rc.Fail())
857 return false;
858 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000859 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000860 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000861 return true; // Success!
862}
863
Johnny Chen01a67862011-10-14 00:42:25 +0000864// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
865// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000866bool
Johnny Chen01a67862011-10-14 00:42:25 +0000867Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000868{
Greg Clayton5160ce52013-03-27 23:08:40 +0000869 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000870 if (log)
871 log->Printf ("Target::%s\n", __FUNCTION__);
872
Johnny Chenedf50372011-09-23 21:21:43 +0000873 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000874 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000875 return true;
876 }
877
878 // Otherwise, it's an end to end operation.
879
Johnny Chen86364b42011-09-20 23:28:55 +0000880 if (!ProcessIsValid())
881 return false;
882
Johnny Chen01a67862011-10-14 00:42:25 +0000883 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000884 for (size_t i = 0; i < num_watchpoints; ++i)
885 {
Johnny Chen01a67862011-10-14 00:42:25 +0000886 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
887 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000888 return false;
889
Johnny Chen01a67862011-10-14 00:42:25 +0000890 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000891 if (rc.Fail())
892 return false;
893 }
Johnny Chen86364b42011-09-20 23:28:55 +0000894 return true; // Success!
895}
896
Johnny Chen01a67862011-10-14 00:42:25 +0000897// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
898// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000899bool
Johnny Chen01a67862011-10-14 00:42:25 +0000900Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000901{
Greg Clayton5160ce52013-03-27 23:08:40 +0000902 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000903 if (log)
904 log->Printf ("Target::%s\n", __FUNCTION__);
905
Johnny Chenedf50372011-09-23 21:21:43 +0000906 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000907 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000908 return true;
909 }
910
911 // Otherwise, it's an end to end operation.
912
Johnny Chen86364b42011-09-20 23:28:55 +0000913 if (!ProcessIsValid())
914 return false;
915
Johnny Chen01a67862011-10-14 00:42:25 +0000916 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000917 for (size_t i = 0; i < num_watchpoints; ++i)
918 {
Johnny Chen01a67862011-10-14 00:42:25 +0000919 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
920 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000921 return false;
922
Johnny Chen01a67862011-10-14 00:42:25 +0000923 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000924 if (rc.Fail())
925 return false;
926 }
Johnny Chen86364b42011-09-20 23:28:55 +0000927 return true; // Success!
928}
929
Johnny Chena4d6bc92012-02-25 06:44:30 +0000930// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
931bool
932Target::ClearAllWatchpointHitCounts ()
933{
Greg Clayton5160ce52013-03-27 23:08:40 +0000934 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000935 if (log)
936 log->Printf ("Target::%s\n", __FUNCTION__);
937
938 size_t num_watchpoints = m_watchpoint_list.GetSize();
939 for (size_t i = 0; i < num_watchpoints; ++i)
940 {
941 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
942 if (!wp_sp)
943 return false;
944
945 wp_sp->ResetHitCount();
946 }
947 return true; // Success!
948}
949
Enrico Granata5e3fe042015-02-11 00:37:54 +0000950// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
951bool
952Target::ClearAllWatchpointHistoricValues ()
953{
954 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
955 if (log)
956 log->Printf ("Target::%s\n", __FUNCTION__);
957
958 size_t num_watchpoints = m_watchpoint_list.GetSize();
959 for (size_t i = 0; i < num_watchpoints; ++i)
960 {
961 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
962 if (!wp_sp)
963 return false;
964
965 wp_sp->ResetHistoricValues();
966 }
967 return true; // Success!
968}
969
Johnny Chen01a67862011-10-14 00:42:25 +0000970// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000971// during these operations.
972bool
Johnny Chen01a67862011-10-14 00:42:25 +0000973Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000974{
Greg Clayton5160ce52013-03-27 23:08:40 +0000975 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000976 if (log)
977 log->Printf ("Target::%s\n", __FUNCTION__);
978
979 if (!ProcessIsValid())
980 return false;
981
Johnny Chen01a67862011-10-14 00:42:25 +0000982 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000983 for (size_t i = 0; i < num_watchpoints; ++i)
984 {
Johnny Chen01a67862011-10-14 00:42:25 +0000985 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
986 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000987 return false;
988
Johnny Chen01a67862011-10-14 00:42:25 +0000989 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000990 }
991 return true; // Success!
992}
993
Johnny Chen01a67862011-10-14 00:42:25 +0000994// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000995bool
Johnny Chen01a67862011-10-14 00:42:25 +0000996Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000997{
Greg Clayton5160ce52013-03-27 23:08:40 +0000998 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000999 if (log)
1000 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1001
1002 if (!ProcessIsValid())
1003 return false;
1004
Johnny Chen01a67862011-10-14 00:42:25 +00001005 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1006 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001007 {
Johnny Chen01a67862011-10-14 00:42:25 +00001008 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001009 if (rc.Success())
1010 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001011
Johnny Chenf04ee932011-09-22 18:04:58 +00001012 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001013 }
1014 return false;
1015}
1016
Johnny Chen01a67862011-10-14 00:42:25 +00001017// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001018bool
Johnny Chen01a67862011-10-14 00:42:25 +00001019Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001020{
Greg Clayton5160ce52013-03-27 23:08:40 +00001021 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001022 if (log)
1023 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1024
1025 if (!ProcessIsValid())
1026 return false;
1027
Johnny Chen01a67862011-10-14 00:42:25 +00001028 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1029 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001030 {
Johnny Chen01a67862011-10-14 00:42:25 +00001031 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001032 if (rc.Success())
1033 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001034
Johnny Chenf04ee932011-09-22 18:04:58 +00001035 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001036 }
1037 return false;
1038}
1039
Johnny Chen01a67862011-10-14 00:42:25 +00001040// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001041bool
Johnny Chen01a67862011-10-14 00:42:25 +00001042Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001043{
Greg Clayton5160ce52013-03-27 23:08:40 +00001044 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001045 if (log)
1046 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1047
Jim Inghamb0b45132013-07-02 02:09:46 +00001048 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1049 if (watch_to_remove_sp == m_last_created_watchpoint)
1050 m_last_created_watchpoint.reset();
1051
Johnny Chen01a67862011-10-14 00:42:25 +00001052 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001053 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001054 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001055 return true;
1056 }
1057 return false;
1058}
1059
Johnny Chen01a67862011-10-14 00:42:25 +00001060// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001061bool
Johnny Chen01a67862011-10-14 00:42:25 +00001062Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001063{
Greg Clayton5160ce52013-03-27 23:08:40 +00001064 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001065 if (log)
1066 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1067
1068 if (!ProcessIsValid())
1069 return false;
1070
Johnny Chen01a67862011-10-14 00:42:25 +00001071 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1072 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001073 {
Johnny Chen01a67862011-10-14 00:42:25 +00001074 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001075 return true;
1076 }
1077 return false;
1078}
1079
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001080ModuleSP
1081Target::GetExecutableModule ()
1082{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001083 // search for the first executable in the module list
1084 for (size_t i = 0; i < m_images.GetSize(); ++i)
1085 {
1086 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1087 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1088 if (obj == nullptr)
1089 continue;
1090 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1091 return module_sp;
1092 }
1093 // as fall back return the first module loaded
1094 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001095}
1096
1097Module*
1098Target::GetExecutableModulePointer ()
1099{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001100 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101}
1102
Enrico Granata17598482012-11-08 02:22:02 +00001103static void
1104LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1105{
1106 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001107 StreamString feedback_stream;
1108 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001109 {
Enrico Granata97303392013-05-21 00:00:30 +00001110 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001111 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001112 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1113 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001114 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001115 if (feedback_stream.GetSize())
1116 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1117 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001118}
1119
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120void
Greg Claytonb35db632013-11-09 00:03:31 +00001121Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122{
Greg Claytonb35db632013-11-09 00:03:31 +00001123 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001124 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001125 m_images.Clear();
1126 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001127 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001128 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001129}
1130
1131void
Greg Claytonb35db632013-11-09 00:03:31 +00001132Target::DidExec ()
1133{
1134 // When a process exec's we need to know about it so we can do some cleanup.
1135 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1136 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1137}
1138
1139void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001140Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1141{
1142 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001143 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144
1145 if (executable_sp.get())
1146 {
1147 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001148 "Target::SetExecutableModule (executable = '%s')",
1149 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001151 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152
Jim Ingham5aee1622010-08-09 23:31:02 +00001153 // 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 +00001154 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001155 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001156 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001157 if (log)
1158 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1159 }
1160
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001162 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001164 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165 {
1166 executable_objfile->GetDependentModules(dependent_files);
1167 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1168 {
Greg Claytonded470d2011-03-19 01:12:21 +00001169 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1170 FileSpec platform_dependent_file_spec;
1171 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001172 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001173 else
1174 platform_dependent_file_spec = dependent_file_spec;
1175
Greg Claytonb9a01b32012-02-26 05:51:37 +00001176 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1177 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001178 if (image_module_sp.get())
1179 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 ObjectFile *objfile = image_module_sp->GetObjectFile();
1181 if (objfile)
1182 objfile->GetDependentModules(dependent_files);
1183 }
1184 }
1185 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186 }
1187}
1188
1189
Jim Ingham5aee1622010-08-09 23:31:02 +00001190bool
1191Target::SetArchitecture (const ArchSpec &arch_spec)
1192{
Greg Clayton5160ce52013-03-27 23:08:40 +00001193 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001194 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001195 {
Greg Clayton70512312012-05-08 01:45:38 +00001196 // If we haven't got a valid arch spec, or the architectures are
1197 // compatible, so just update the architecture. Architectures can be
1198 // equal, yet the triple OS and vendor might change, so we need to do
1199 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001200 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001201 if (log)
1202 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 +00001203 return true;
1204 }
1205 else
1206 {
1207 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001208 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001209 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 +00001210 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001211 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001212
Greg Claytonb35db632013-11-09 00:03:31 +00001213 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001214 // Need to do something about unsetting breakpoints.
1215
1216 if (executable_sp)
1217 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001218 if (log)
1219 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 +00001220 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1221 Error error = ModuleList::GetSharedModule (module_spec,
1222 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001223 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001224 NULL,
1225 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001226
1227 if (!error.Fail() && executable_sp)
1228 {
1229 SetExecutableModule (executable_sp, true);
1230 return true;
1231 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001232 }
1233 }
Greg Clayton70512312012-05-08 01:45:38 +00001234 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001235}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001237bool
1238Target::MergeArchitecture (const ArchSpec &arch_spec)
1239{
1240 if (arch_spec.IsValid())
1241 {
1242 if (m_arch.IsCompatibleMatch(arch_spec))
1243 {
1244 // The current target arch is compatible with "arch_spec", see if we
1245 // can improve our current architecture using bits from "arch_spec"
1246
1247 // Merge bits from arch_spec into "merged_arch" and set our architecture
1248 ArchSpec merged_arch (m_arch);
1249 merged_arch.MergeFrom (arch_spec);
1250 return SetArchitecture(merged_arch);
1251 }
1252 else
1253 {
1254 // The new architecture is different, we just need to replace it
1255 return SetArchitecture(arch_spec);
1256 }
1257 }
1258 return false;
1259}
1260
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261void
Enrico Granataefe637d2012-11-08 19:16:03 +00001262Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001263{
1264}
1265
1266void
Enrico Granataefe637d2012-11-08 19:16:03 +00001267Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001268{
1269 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001270 if (m_valid)
1271 {
1272 ModuleList my_module_list;
1273 my_module_list.Append(module_sp);
1274 LoadScriptingResourceForModule(module_sp, this);
1275 ModulesDidLoad (my_module_list);
1276 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277}
1278
1279void
Enrico Granataefe637d2012-11-08 19:16:03 +00001280Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001281{
1282 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001283 if (m_valid)
1284 {
1285 ModuleList my_module_list;
1286 my_module_list.Append(module_sp);
1287 ModulesDidUnload (my_module_list, false);
1288 }
Enrico Granata17598482012-11-08 02:22:02 +00001289}
1290
1291void
Enrico Granataefe637d2012-11-08 19:16:03 +00001292Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293{
Jim Inghame716ae02011-08-03 01:00:06 +00001294 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001295 if (m_valid)
1296 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297}
1298
1299void
1300Target::ModulesDidLoad (ModuleList &module_list)
1301{
Greg Clayton23f8c952014-03-24 23:10:19 +00001302 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001303 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001304 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001305 if (m_process_sp)
1306 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001307 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001308 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001309 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001310 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311}
1312
1313void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001314Target::SymbolsDidLoad (ModuleList &module_list)
1315{
Greg Clayton23f8c952014-03-24 23:10:19 +00001316 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001317 {
Jim Ingham31caf982013-06-04 23:01:35 +00001318 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001319 {
Jim Ingham31caf982013-06-04 23:01:35 +00001320 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1321 if (runtime)
1322 {
1323 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1324 objc_runtime->SymbolsDidLoad(module_list);
1325 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001326 }
Jim Ingham31caf982013-06-04 23:01:35 +00001327
Greg Clayton095eeaa2013-11-05 23:28:00 +00001328 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001329 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001330 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001331}
1332
1333void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001334Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001335{
Greg Clayton23f8c952014-03-24 23:10:19 +00001336 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001337 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001338 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001339 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001340 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001341 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342}
1343
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001344bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001345Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001346{
Greg Clayton67cc0632012-08-22 17:17:09 +00001347 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001348 {
1349 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001350 ModuleSpec module_spec (module_file_spec);
1351 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001352
1353 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1354 // black list.
1355 if (num_modules > 0)
1356 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001357 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001358 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001359 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001360 return false;
1361 }
1362 return true;
1363 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001364 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001365 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001366}
1367
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001368bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001369Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001370{
Greg Clayton67cc0632012-08-22 17:17:09 +00001371 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001372 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001373 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001374 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001375 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001376 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001377}
1378
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379size_t
Greg Claytondb598232011-01-07 01:57:07 +00001380Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1381{
Greg Claytone72dfb32012-02-24 01:59:29 +00001382 SectionSP section_sp (addr.GetSection());
1383 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001384 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001385 // 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 +00001386 if (section_sp->IsEncrypted())
1387 {
Greg Clayton57f06302012-05-25 17:05:55 +00001388 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001389 return 0;
1390 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001391 ModuleSP module_sp (section_sp->GetModule());
1392 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001393 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001394 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1395 if (objfile)
1396 {
1397 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1398 addr.GetOffset(),
1399 dst,
1400 dst_len);
1401 if (bytes_read > 0)
1402 return bytes_read;
1403 else
1404 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1405 }
Greg Claytondb598232011-01-07 01:57:07 +00001406 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001407 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001408 }
1409 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001410 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001411 }
1412 else
Greg Claytondb598232011-01-07 01:57:07 +00001413 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001414
Greg Claytondb598232011-01-07 01:57:07 +00001415 return 0;
1416}
1417
1418size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001419Target::ReadMemory (const Address& addr,
1420 bool prefer_file_cache,
1421 void *dst,
1422 size_t dst_len,
1423 Error &error,
1424 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001425{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001427
Enrico Granata9128ee22011-09-06 19:20:51 +00001428 // if we end up reading this from process memory, we will fill this
1429 // with the actual load address
1430 if (load_addr_ptr)
1431 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1432
Greg Claytondb598232011-01-07 01:57:07 +00001433 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001434
1435 addr_t load_addr = LLDB_INVALID_ADDRESS;
1436 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001437 Address resolved_addr;
1438 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001439 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001440 SectionLoadList &section_load_list = GetSectionLoadList();
1441 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001442 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001443 // No sections are loaded, so we must assume we are not running
1444 // yet and anything we are given is a file address.
1445 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1446 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001447 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001448 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001449 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001450 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001451 // we have manually loaded some sections with "target modules load ..."
1452 // or because we have have a live process that has sections loaded
1453 // through the dynamic loader
1454 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001455 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001456 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001457 }
Greg Clayton357132e2011-03-26 19:14:58 +00001458 if (!resolved_addr.IsValid())
1459 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001460
Greg Claytonc749eb82011-07-11 05:12:02 +00001461
Greg Claytondb598232011-01-07 01:57:07 +00001462 if (prefer_file_cache)
1463 {
1464 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1465 if (bytes_read > 0)
1466 return bytes_read;
1467 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001468
Johnny Chen86364b42011-09-20 23:28:55 +00001469 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001470 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001471 if (load_addr == LLDB_INVALID_ADDRESS)
1472 load_addr = resolved_addr.GetLoadAddress (this);
1473
Greg Claytondda4f7b2010-06-30 23:03:03 +00001474 if (load_addr == LLDB_INVALID_ADDRESS)
1475 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001476 ModuleSP addr_module_sp (resolved_addr.GetModule());
1477 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001478 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001479 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001480 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001481 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001482 else
Daniel Malead01b2952012-11-29 21:49:15 +00001483 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001484 }
1485 else
1486 {
Greg Claytondb598232011-01-07 01:57:07 +00001487 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001488 if (bytes_read != dst_len)
1489 {
1490 if (error.Success())
1491 {
1492 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001493 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001494 else
Daniel Malead01b2952012-11-29 21:49:15 +00001495 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 +00001496 }
1497 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001498 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001499 {
1500 if (load_addr_ptr)
1501 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001502 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001503 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001504 // If the address is not section offset we have an address that
1505 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001506 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001507 // more we can do. If it is section offset, we might be able to
1508 // read cached memory from the object file.
1509 if (!resolved_addr.IsSectionOffset())
1510 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001511 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001513
Greg Claytonc749eb82011-07-11 05:12:02 +00001514 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001515 {
Greg Claytondb598232011-01-07 01:57:07 +00001516 // If we didn't already try and read from the object file cache, then
1517 // try it after failing to read from the process.
1518 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001519 }
1520 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521}
1522
Greg Claytond16e1e52011-07-12 17:06:17 +00001523size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001524Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1525{
1526 char buf[256];
1527 out_str.clear();
1528 addr_t curr_addr = addr.GetLoadAddress(this);
1529 Address address(addr);
1530 while (1)
1531 {
1532 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1533 if (length == 0)
1534 break;
1535 out_str.append(buf, length);
1536 // If we got "length - 1" bytes, we didn't get the whole C string, we
1537 // need to read some more characters
1538 if (length == sizeof(buf) - 1)
1539 curr_addr += length;
1540 else
1541 break;
1542 address = Address(curr_addr);
1543 }
1544 return out_str.size();
1545}
1546
1547
1548size_t
1549Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1550{
1551 size_t total_cstr_len = 0;
1552 if (dst && dst_max_len)
1553 {
1554 result_error.Clear();
1555 // NULL out everything just to be safe
1556 memset (dst, 0, dst_max_len);
1557 Error error;
1558 addr_t curr_addr = addr.GetLoadAddress(this);
1559 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001560
1561 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1562 // think this really needs to be tied to the memory cache subsystem's
1563 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001564 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001565
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001566 size_t bytes_left = dst_max_len - 1;
1567 char *curr_dst = dst;
1568
1569 while (bytes_left > 0)
1570 {
1571 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1572 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1573 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1574
1575 if (bytes_read == 0)
1576 {
1577 result_error = error;
1578 dst[total_cstr_len] = '\0';
1579 break;
1580 }
1581 const size_t len = strlen(curr_dst);
1582
1583 total_cstr_len += len;
1584
1585 if (len < bytes_to_read)
1586 break;
1587
1588 curr_dst += bytes_read;
1589 curr_addr += bytes_read;
1590 bytes_left -= bytes_read;
1591 address = Address(curr_addr);
1592 }
1593 }
1594 else
1595 {
1596 if (dst == NULL)
1597 result_error.SetErrorString("invalid arguments");
1598 else
1599 result_error.Clear();
1600 }
1601 return total_cstr_len;
1602}
1603
1604size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001605Target::ReadScalarIntegerFromMemory (const Address& addr,
1606 bool prefer_file_cache,
1607 uint32_t byte_size,
1608 bool is_signed,
1609 Scalar &scalar,
1610 Error &error)
1611{
1612 uint64_t uval;
1613
1614 if (byte_size <= sizeof(uval))
1615 {
1616 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1617 if (bytes_read == byte_size)
1618 {
1619 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001620 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001621 if (byte_size <= 4)
1622 scalar = data.GetMaxU32 (&offset, byte_size);
1623 else
1624 scalar = data.GetMaxU64 (&offset, byte_size);
1625
1626 if (is_signed)
1627 scalar.SignExtend(byte_size * 8);
1628 return bytes_read;
1629 }
1630 }
1631 else
1632 {
1633 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1634 }
1635 return 0;
1636}
1637
1638uint64_t
1639Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1640 bool prefer_file_cache,
1641 size_t integer_byte_size,
1642 uint64_t fail_value,
1643 Error &error)
1644{
1645 Scalar scalar;
1646 if (ReadScalarIntegerFromMemory (addr,
1647 prefer_file_cache,
1648 integer_byte_size,
1649 false,
1650 scalar,
1651 error))
1652 return scalar.ULongLong(fail_value);
1653 return fail_value;
1654}
1655
1656bool
1657Target::ReadPointerFromMemory (const Address& addr,
1658 bool prefer_file_cache,
1659 Error &error,
1660 Address &pointer_addr)
1661{
1662 Scalar scalar;
1663 if (ReadScalarIntegerFromMemory (addr,
1664 prefer_file_cache,
1665 m_arch.GetAddressByteSize(),
1666 false,
1667 scalar,
1668 error))
1669 {
1670 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1671 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1672 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001673 SectionLoadList &section_load_list = GetSectionLoadList();
1674 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001675 {
1676 // No sections are loaded, so we must assume we are not running
1677 // yet and anything we are given is a file address.
1678 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1679 }
1680 else
1681 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001682 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001683 // we have manually loaded some sections with "target modules load ..."
1684 // or because we have have a live process that has sections loaded
1685 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001686 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001687 }
1688 // We weren't able to resolve the pointer value, so just return
1689 // an address with no section
1690 if (!pointer_addr.IsValid())
1691 pointer_addr.SetOffset (pointer_vm_addr);
1692 return true;
1693
1694 }
1695 }
1696 return false;
1697}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698
1699ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001700Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702 ModuleSP module_sp;
1703
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704 Error error;
1705
Jim Ingham4a94c912012-05-17 18:38:42 +00001706 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1707 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001708
Jim Ingham4a94c912012-05-17 18:38:42 +00001709 if (module_spec.GetUUID().IsValid())
1710 module_sp = m_images.FindFirstModule(module_spec);
1711
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001712 if (!module_sp)
1713 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001714 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1715 bool did_create_module = false;
1716
1717 // If there are image search path entries, try to use them first to acquire a suitable image.
1718 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001719 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001720 ModuleSpec transformed_spec (module_spec);
1721 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1722 {
1723 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1724 error = ModuleList::GetSharedModule (transformed_spec,
1725 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001726 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001727 &old_module_sp,
1728 &did_create_module);
1729 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001730 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001731
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001732 if (!module_sp)
1733 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001734 // If we have a UUID, we can check our global shared module list in case
1735 // we already have it. If we don't have a valid UUID, then we can't since
1736 // the path in "module_spec" will be a platform path, and we will need to
1737 // let the platform find that file. For example, we could be asking for
1738 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1739 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1740 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1741 // cache.
1742 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001743 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001744 // We have a UUID, it is OK to check the global module list...
1745 error = ModuleList::GetSharedModule (module_spec,
1746 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001747 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001748 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001749 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001750 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001751
1752 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001753 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001754 // The platform is responsible for finding and caching an appropriate
1755 // module in the shared module cache.
1756 if (m_platform_sp)
1757 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001758 error = m_platform_sp->GetSharedModule (module_spec,
1759 m_process_sp.get(),
1760 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001761 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001762 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001763 &did_create_module);
1764 }
1765 else
1766 {
1767 error.SetErrorString("no platform is currently set");
1768 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001769 }
1770 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771
Jim Ingham4a94c912012-05-17 18:38:42 +00001772 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1773 // module in the list already, and if there was, let's remove it.
1774 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001776 ObjectFile *objfile = module_sp->GetObjectFile();
1777 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001778 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001779 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001780 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001781 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1782 case ObjectFile::eTypeExecutable: /// A normal executable
1783 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1784 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1785 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1786 break;
1787 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1788 if (error_ptr)
1789 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1790 return ModuleSP();
1791 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1792 if (error_ptr)
1793 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1794 return ModuleSP();
1795 default:
1796 if (error_ptr)
1797 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1798 return ModuleSP();
1799 }
1800 // GetSharedModule is not guaranteed to find the old shared module, for instance
1801 // in the common case where you pass in the UUID, it is only going to find the one
1802 // module matching the UUID. In fact, it has no good way to know what the "old module"
1803 // relevant to this target is, since there might be many copies of a module with this file spec
1804 // in various running debug sessions, but only one of them will belong to this target.
1805 // So let's remove the UUID from the module list, and look in the target's module list.
1806 // Only do this if there is SOMETHING else in the module spec...
1807 if (!old_module_sp)
1808 {
1809 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001810 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001811 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1812 module_spec_copy.GetUUID().Clear();
1813
1814 ModuleList found_modules;
1815 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1816 if (num_found == 1)
1817 {
1818 old_module_sp = found_modules.GetModuleAtIndex(0);
1819 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001820 }
1821 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001822
1823 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1824 {
1825 m_images.ReplaceModule(old_module_sp, module_sp);
1826 Module *old_module_ptr = old_module_sp.get();
1827 old_module_sp.reset();
1828 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1829 }
1830 else
1831 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001832 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001833 else
1834 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001835 }
1836 }
1837 if (error_ptr)
1838 *error_ptr = error;
1839 return module_sp;
1840}
1841
1842
Greg Claytond9e416c2012-02-18 05:35:26 +00001843TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844Target::CalculateTarget ()
1845{
Greg Claytond9e416c2012-02-18 05:35:26 +00001846 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847}
1848
Greg Claytond9e416c2012-02-18 05:35:26 +00001849ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001850Target::CalculateProcess ()
1851{
Greg Claytond9e416c2012-02-18 05:35:26 +00001852 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853}
1854
Greg Claytond9e416c2012-02-18 05:35:26 +00001855ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001856Target::CalculateThread ()
1857{
Greg Claytond9e416c2012-02-18 05:35:26 +00001858 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001859}
1860
Jason Molendab57e4a12013-11-04 09:33:30 +00001861StackFrameSP
1862Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001863{
Jason Molendab57e4a12013-11-04 09:33:30 +00001864 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001865}
1866
1867void
Greg Clayton0603aa92010-10-04 01:05:56 +00001868Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869{
Greg Claytonc14ee322011-09-22 04:58:26 +00001870 exe_ctx.Clear();
1871 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001872}
1873
1874PathMappingList &
1875Target::GetImageSearchPathList ()
1876{
1877 return m_image_search_paths;
1878}
1879
1880void
1881Target::ImageSearchPathsChanged
1882(
1883 const PathMappingList &path_list,
1884 void *baton
1885)
1886{
1887 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001888 ModuleSP exe_module_sp (target->GetExecutableModule());
1889 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001890 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001891}
1892
Jim Ingham151c0322015-09-15 21:13:50 +00001893TypeSystem *
1894Target::GetScratchTypeSystemForLanguage (lldb::LanguageType language, bool create_on_demand)
1895{
1896 if (Language::LanguageIsC(language)
1897 || Language::LanguageIsObjC(language)
1898 || Language::LanguageIsCPlusPlus(language)
Tamas Berghammer6b144352015-09-21 10:08:49 +00001899 || language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Jim Ingham151c0322015-09-15 21:13:50 +00001900 || language == eLanguageTypeUnknown)
1901 return GetScratchClangASTContext(create_on_demand);
1902 else
1903 return NULL;
1904}
1905
1906UserExpression *
1907Target::GetUserExpressionForLanguage(const char *expr,
1908 const char *expr_prefix,
1909 lldb::LanguageType language,
1910 Expression::ResultType desired_type,
1911 Error &error)
1912{
1913 TypeSystem *type_system = GetScratchTypeSystemForLanguage (language);
1914 UserExpression *user_expr = nullptr;
1915
1916 if (!type_system)
1917 {
1918 error.SetErrorStringWithFormat("Could not find type system for language: %s", Language::GetNameForLanguageType(language));
1919 return nullptr;
1920 }
1921
1922 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type);
1923 if (!user_expr)
1924 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1925
1926 return user_expr;
1927}
1928
1929FunctionCaller *
1930Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
1931 const CompilerType &return_type,
1932 const Address& function_address,
1933 const ValueList &arg_value_list,
1934 const char *name,
1935 Error &error)
1936{
1937 TypeSystem *type_system = GetScratchTypeSystemForLanguage (language);
1938 FunctionCaller *persistent_fn = nullptr;
1939
1940 if (!type_system)
1941 {
1942 error.SetErrorStringWithFormat("Could not find type system for language: %s", Language::GetNameForLanguageType(language));
1943 return persistent_fn;
1944 }
1945
1946 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
1947 if (!persistent_fn)
1948 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1949
1950 return persistent_fn;
1951}
1952
1953UtilityFunction *
1954Target::GetUtilityFunctionForLanguage (const char *text,
1955 lldb::LanguageType language,
1956 const char *name,
1957 Error &error)
1958{
1959 TypeSystem *type_system = GetScratchTypeSystemForLanguage (language);
1960 UtilityFunction *utility_fn = nullptr;
1961
1962 if (!type_system)
1963 {
1964 error.SetErrorStringWithFormat("Could not find type system for language: %s", Language::GetNameForLanguageType(language));
1965 return utility_fn;
1966 }
1967
1968 utility_fn = type_system->GetUtilityFunction (text, name);
1969 if (!utility_fn)
1970 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1971
1972 return utility_fn;
1973}
1974
1975
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001977Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001978{
Greg Clayton73da2442011-08-03 01:23:55 +00001979 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001980 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001981 {
Jim Ingham151c0322015-09-15 21:13:50 +00001982 m_scratch_ast_context_ap.reset (new ClangASTContextForExpressions(*this));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001983 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001984 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001985 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001986 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1987 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988 return m_scratch_ast_context_ap.get();
1989}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001990
Sean Callanan686b2312011-11-16 18:20:47 +00001991ClangASTImporter *
1992Target::GetClangASTImporter()
1993{
1994 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1995
1996 if (!ast_importer)
1997 {
1998 ast_importer = new ClangASTImporter();
1999 m_ast_importer_ap.reset(ast_importer);
2000 }
2001
2002 return ast_importer;
2003}
2004
Greg Clayton99d0faf2010-11-18 23:32:35 +00002005void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002006Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002007{
Greg Clayton6920b522012-08-22 18:39:03 +00002008 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002009}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002010
Greg Clayton99d0faf2010-11-18 23:32:35 +00002011void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002012Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002013{
Greg Clayton6920b522012-08-22 18:39:03 +00002014 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002015}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002016
Greg Claytonc859e2d2012-02-13 23:10:39 +00002017FileSpecList
2018Target::GetDefaultExecutableSearchPaths ()
2019{
Greg Clayton67cc0632012-08-22 17:17:09 +00002020 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2021 if (properties_sp)
2022 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002023 return FileSpecList();
2024}
2025
Michael Sartaina7499c92013-07-01 19:45:50 +00002026FileSpecList
2027Target::GetDefaultDebugFileSearchPaths ()
2028{
2029 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2030 if (properties_sp)
2031 return properties_sp->GetDebugFileSearchPaths();
2032 return FileSpecList();
2033}
2034
Sean Callanan85054342015-04-03 15:39:47 +00002035FileSpecList
2036Target::GetDefaultClangModuleSearchPaths ()
2037{
2038 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2039 if (properties_sp)
2040 return properties_sp->GetClangModuleSearchPaths();
2041 return FileSpecList();
2042}
2043
Caroline Ticedaccaa92010-09-20 20:44:43 +00002044ArchSpec
2045Target::GetDefaultArchitecture ()
2046{
Greg Clayton67cc0632012-08-22 17:17:09 +00002047 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2048 if (properties_sp)
2049 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002050 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002051}
2052
2053void
Greg Clayton67cc0632012-08-22 17:17:09 +00002054Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002055{
Greg Clayton67cc0632012-08-22 17:17:09 +00002056 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2057 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002058 {
2059 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 +00002060 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002061 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002062}
2063
Greg Clayton0603aa92010-10-04 01:05:56 +00002064Target *
2065Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2066{
2067 // The target can either exist in the "process" of ExecutionContext, or in
2068 // the "target_sp" member of SymbolContext. This accessor helper function
2069 // will get the target from one of these locations.
2070
2071 Target *target = NULL;
2072 if (sc_ptr != NULL)
2073 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00002074 if (target == NULL && exe_ctx_ptr)
2075 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002076 return target;
2077}
2078
Jim Ingham1624a2d2014-05-05 02:26:40 +00002079ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002080Target::EvaluateExpression
2081(
2082 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00002083 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00002084 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00002085 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002086)
2087{
Enrico Granata97fca502012-09-18 17:43:16 +00002088 result_valobj_sp.reset();
2089
Jim Ingham8646d3c2014-05-05 02:47:44 +00002090 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002091
Greg Claytond1767f02011-12-08 02:13:16 +00002092 if (expr_cstr == NULL || expr_cstr[0] == '\0')
2093 return execution_results;
2094
Jim Ingham6026ca32011-05-12 02:06:14 +00002095 // We shouldn't run stop hooks in expressions.
2096 // Be sure to reset this if you return anywhere within this function.
2097 bool old_suppress_value = m_suppress_stop_hooks;
2098 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002099
2100 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002101
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002102 if (frame)
2103 {
2104 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002105 }
2106 else if (m_process_sp)
2107 {
2108 m_process_sp->CalculateExecutionContext(exe_ctx);
2109 }
2110 else
2111 {
2112 CalculateExecutionContext(exe_ctx);
2113 }
2114
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002115 // Make sure we aren't just trying to see the value of a persistent
2116 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002117 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002118 // Only check for persistent variables the expression starts with a '$'
2119 if (expr_cstr[0] == '$')
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002120 persistent_var_sp = GetScratchTypeSystemForLanguage(eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002121
2122 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002123 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002124 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002125 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002126 }
2127 else
2128 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002129 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002130 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002131 execution_results = UserExpression::Evaluate (exe_ctx,
2132 options,
2133 expr_cstr,
2134 prefix,
2135 result_valobj_sp,
2136 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002137 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002138
2139 m_suppress_stop_hooks = old_suppress_value;
2140
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002141 return execution_results;
2142}
2143
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002144lldb::ExpressionVariableSP
2145Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002146{
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002147 if (ClangASTContext *ast_context = GetScratchClangASTContext(false))
2148 {
2149 if (PersistentExpressionState *persistent_state = ast_context->GetPersistentExpressionState())
2150 {
2151 return persistent_state->GetVariable(name);
2152 }
2153 }
2154
2155 return ExpressionVariableSP();
Zachary Turner32abc6e2015-03-03 19:23:09 +00002156}
2157
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002158lldb::addr_t
2159Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2160{
2161 addr_t code_addr = load_addr;
2162 switch (m_arch.GetMachine())
2163 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002164 case llvm::Triple::mips:
2165 case llvm::Triple::mipsel:
2166 case llvm::Triple::mips64:
2167 case llvm::Triple::mips64el:
2168 switch (addr_class)
2169 {
2170 case eAddressClassData:
2171 case eAddressClassDebug:
2172 return LLDB_INVALID_ADDRESS;
2173
2174 case eAddressClassUnknown:
2175 case eAddressClassInvalid:
2176 case eAddressClassCode:
2177 case eAddressClassCodeAlternateISA:
2178 case eAddressClassRuntime:
2179 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2180 code_addr |= 1ull;
2181 break;
2182 }
2183 break;
2184
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002185 case llvm::Triple::arm:
2186 case llvm::Triple::thumb:
2187 switch (addr_class)
2188 {
2189 case eAddressClassData:
2190 case eAddressClassDebug:
2191 return LLDB_INVALID_ADDRESS;
2192
2193 case eAddressClassUnknown:
2194 case eAddressClassInvalid:
2195 case eAddressClassCode:
2196 case eAddressClassCodeAlternateISA:
2197 case eAddressClassRuntime:
2198 // Check if bit zero it no set?
2199 if ((code_addr & 1ull) == 0)
2200 {
2201 // Bit zero isn't set, check if the address is a multiple of 2?
2202 if (code_addr & 2ull)
2203 {
2204 // The address is a multiple of 2 so it must be thumb, set bit zero
2205 code_addr |= 1ull;
2206 }
2207 else if (addr_class == eAddressClassCodeAlternateISA)
2208 {
2209 // We checked the address and the address claims to be the alternate ISA
2210 // which means thumb, so set bit zero.
2211 code_addr |= 1ull;
2212 }
2213 }
2214 break;
2215 }
2216 break;
2217
2218 default:
2219 break;
2220 }
2221 return code_addr;
2222}
2223
2224lldb::addr_t
2225Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2226{
2227 addr_t opcode_addr = load_addr;
2228 switch (m_arch.GetMachine())
2229 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002230 case llvm::Triple::mips:
2231 case llvm::Triple::mipsel:
2232 case llvm::Triple::mips64:
2233 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002234 case llvm::Triple::arm:
2235 case llvm::Triple::thumb:
2236 switch (addr_class)
2237 {
2238 case eAddressClassData:
2239 case eAddressClassDebug:
2240 return LLDB_INVALID_ADDRESS;
2241
2242 case eAddressClassInvalid:
2243 case eAddressClassUnknown:
2244 case eAddressClassCode:
2245 case eAddressClassCodeAlternateISA:
2246 case eAddressClassRuntime:
2247 opcode_addr &= ~(1ull);
2248 break;
2249 }
2250 break;
2251
2252 default:
2253 break;
2254 }
2255 return opcode_addr;
2256}
2257
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002258lldb::addr_t
2259Target::GetBreakableLoadAddress (lldb::addr_t addr)
2260{
2261 addr_t breakable_addr = addr;
2262 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2263
2264 switch (m_arch.GetMachine())
2265 {
2266 default:
2267 break;
2268 case llvm::Triple::mips:
2269 case llvm::Triple::mipsel:
2270 case llvm::Triple::mips64:
2271 case llvm::Triple::mips64el:
2272 {
2273 addr_t function_start = 0;
2274 addr_t current_offset = 0;
2275 uint32_t loop_count = 0;
2276 Address resolved_addr;
2277 uint32_t arch_flags = m_arch.GetFlags ();
2278 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2279 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2280 SectionLoadList &section_load_list = GetSectionLoadList();
2281
2282 if (section_load_list.IsEmpty())
2283 // No sections are loaded, so we must assume we are not running yet
2284 // and need to operate only on file address.
2285 m_images.ResolveFileAddress (addr, resolved_addr);
2286 else
2287 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2288
2289 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2290 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2291 if (temp_addr_module_sp)
2292 {
2293 SymbolContext sc;
2294 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002295 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002296 if (sc.function)
2297 {
2298 function_start = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(this);
2299 if (function_start == LLDB_INVALID_ADDRESS)
2300 function_start = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
2301 }
2302 else if (sc.symbol)
2303 {
2304 Address sym_addr = sc.symbol->GetAddress();
2305 function_start = sym_addr.GetFileAddress();
2306 }
2307 current_offset = addr - function_start;
2308 }
2309
2310 // If breakpoint address is start of function then we dont have to do anything.
2311 if (current_offset == 0)
2312 return breakable_addr;
2313 else
2314 loop_count = current_offset / 2;
2315
2316 if (loop_count > 3)
2317 {
2318 // Scan previous 6 bytes
2319 if (IsMips16 | IsMicromips)
2320 loop_count = 3;
2321 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2322 else
2323 loop_count = 2;
2324 }
2325
2326 // Create Disassembler Instance
2327 lldb::DisassemblerSP disasm_sp (Disassembler::FindPlugin(m_arch, NULL, NULL));
2328
2329 ExecutionContext exe_ctx;
2330 CalculateExecutionContext(exe_ctx);
2331 InstructionList instruction_list;
2332 InstructionSP prev_insn;
2333 bool prefer_file_cache = true; // Read from file
2334 uint32_t inst_to_choose = 0;
2335
2336 for (uint32_t i = 1; i <= loop_count; i++)
2337 {
2338 // Adjust the address to read from.
2339 resolved_addr.Slide (-2);
2340 AddressRange range(resolved_addr, i*2);
2341 uint32_t insn_size = 0;
2342
Greg Clayton6071e6f2015-08-26 22:57:51 +00002343 disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002344
2345 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2346 if (num_insns)
2347 {
2348 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2349 insn_size = prev_insn->GetOpcode().GetByteSize();
2350 if (i == 1 && insn_size == 2)
2351 {
2352 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2353 instruction_list.Append(prev_insn);
2354 inst_to_choose = 1;
2355 }
2356 else if (i == 2)
2357 {
2358 // Here we may get one 4-byte instruction or two 2-byte instructions.
2359 if (num_insns == 2)
2360 {
2361 // Looks like there are two 2-byte instructions above our breakpoint target address.
2362 // Now the upper 2-byte instruction is either a valid 2-byte instruction or could be a part of it's upper 4-byte instruction.
2363 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2364 // and whatever i=1 iteration has found out is true.
2365 inst_to_choose = 1;
2366 break;
2367 }
2368 else if (insn_size == 4)
2369 {
2370 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2371 // Lets try scanning upper 2 bytes to verify this.
2372 instruction_list.Append(prev_insn);
2373 inst_to_choose = 2;
2374 }
2375 }
2376 else if (i == 3)
2377 {
2378 if (insn_size == 4)
2379 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2380 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2381 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2382 inst_to_choose = 0;
2383 else
2384 // This is straight-forward
2385 inst_to_choose = 2;
2386 break;
2387 }
2388 }
2389 else
2390 {
2391 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2392 if (i > 1)
2393 {
2394 inst_to_choose = i - 1;
2395 break;
2396 }
2397 }
2398 }
2399
2400 // Check if we are able to find any valid instruction.
2401 if (inst_to_choose)
2402 {
2403 if (inst_to_choose > instruction_list.GetSize())
2404 inst_to_choose--;
2405 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2406
2407 if (prev_insn->HasDelaySlot())
2408 {
2409 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2410 // Adjust the breakable address
2411 breakable_addr = addr - shift_size;
2412 if (log)
2413 log->Printf ("Target::%s Breakpoint at 0x%8.8" PRIx64 " is adjusted to 0x%8.8" PRIx64 " due to delay slot\n", __FUNCTION__, addr, breakable_addr);
2414 }
2415 }
2416 break;
2417 }
2418 }
2419 return breakable_addr;
2420}
2421
Greg Clayton9585fbf2013-03-19 00:20:55 +00002422SourceManager &
2423Target::GetSourceManager ()
2424{
2425 if (m_source_manager_ap.get() == NULL)
2426 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2427 return *m_source_manager_ap;
2428}
2429
Sean Callanan9998acd2014-12-05 01:21:59 +00002430ClangModulesDeclVendor *
2431Target::GetClangModulesDeclVendor ()
2432{
2433 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2434
2435 {
2436 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2437
2438 if (!m_clang_modules_decl_vendor_ap)
2439 {
2440 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2441 }
2442 }
2443
2444 return m_clang_modules_decl_vendor_ap.get();
2445}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002446
Greg Clayton44d93782014-01-27 23:43:24 +00002447Target::StopHookSP
2448Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002449{
2450 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002451 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2452 m_stop_hooks[new_uid] = stop_hook_sp;
2453 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002454}
2455
2456bool
2457Target::RemoveStopHookByID (lldb::user_id_t user_id)
2458{
2459 size_t num_removed;
2460 num_removed = m_stop_hooks.erase (user_id);
2461 if (num_removed == 0)
2462 return false;
2463 else
2464 return true;
2465}
2466
2467void
2468Target::RemoveAllStopHooks ()
2469{
2470 m_stop_hooks.clear();
2471}
2472
2473Target::StopHookSP
2474Target::GetStopHookByID (lldb::user_id_t user_id)
2475{
2476 StopHookSP found_hook;
2477
2478 StopHookCollection::iterator specified_hook_iter;
2479 specified_hook_iter = m_stop_hooks.find (user_id);
2480 if (specified_hook_iter != m_stop_hooks.end())
2481 found_hook = (*specified_hook_iter).second;
2482 return found_hook;
2483}
2484
2485bool
2486Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2487{
2488 StopHookCollection::iterator specified_hook_iter;
2489 specified_hook_iter = m_stop_hooks.find (user_id);
2490 if (specified_hook_iter == m_stop_hooks.end())
2491 return false;
2492
2493 (*specified_hook_iter).second->SetIsActive (active_state);
2494 return true;
2495}
2496
2497void
2498Target::SetAllStopHooksActiveState (bool active_state)
2499{
2500 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2501 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2502 {
2503 (*pos).second->SetIsActive (active_state);
2504 }
2505}
2506
2507void
2508Target::RunStopHooks ()
2509{
Jim Ingham6026ca32011-05-12 02:06:14 +00002510 if (m_suppress_stop_hooks)
2511 return;
2512
Jim Ingham9575d842011-03-11 03:53:59 +00002513 if (!m_process_sp)
2514 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002515
2516 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2517 // since in that case we do not want to run the stop-hooks
2518 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2519 return;
2520
Jim Ingham9575d842011-03-11 03:53:59 +00002521 if (m_stop_hooks.empty())
2522 return;
2523
2524 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2525
2526 // If there aren't any active stop hooks, don't bother either:
2527 bool any_active_hooks = false;
2528 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2529 {
2530 if ((*pos).second->IsActive())
2531 {
2532 any_active_hooks = true;
2533 break;
2534 }
2535 }
2536 if (!any_active_hooks)
2537 return;
2538
2539 CommandReturnObject result;
2540
2541 std::vector<ExecutionContext> exc_ctx_with_reasons;
2542 std::vector<SymbolContext> sym_ctx_with_reasons;
2543
2544 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2545 size_t num_threads = cur_threadlist.GetSize();
2546 for (size_t i = 0; i < num_threads; i++)
2547 {
2548 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2549 if (cur_thread_sp->ThreadStoppedForAReason())
2550 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002551 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002552 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2553 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2554 }
2555 }
2556
2557 // If no threads stopped for a reason, don't run the stop-hooks.
2558 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2559 if (num_exe_ctx == 0)
2560 return;
2561
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002562 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2563 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002564
2565 bool keep_going = true;
2566 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002567 bool print_hook_header;
2568 bool print_thread_header;
2569
2570 if (num_exe_ctx == 1)
2571 print_thread_header = false;
2572 else
2573 print_thread_header = true;
2574
2575 if (m_stop_hooks.size() == 1)
2576 print_hook_header = false;
2577 else
2578 print_hook_header = true;
2579
Jim Ingham9575d842011-03-11 03:53:59 +00002580 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2581 {
2582 // result.Clear();
2583 StopHookSP cur_hook_sp = (*pos).second;
2584 if (!cur_hook_sp->IsActive())
2585 continue;
2586
2587 bool any_thread_matched = false;
2588 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2589 {
2590 if ((cur_hook_sp->GetSpecifier () == NULL
2591 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2592 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002593 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002594 {
2595 if (!hooks_ran)
2596 {
Jim Ingham9575d842011-03-11 03:53:59 +00002597 hooks_ran = true;
2598 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002599 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002600 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002601 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2602 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2603 NULL);
2604 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002605 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002606 else
Daniel Malead01b2952012-11-29 21:49:15 +00002607 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002608 any_thread_matched = true;
2609 }
2610
Jim Ingham381e25b2011-03-22 01:47:27 +00002611 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002612 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002613
2614 CommandInterpreterRunOptions options;
2615 options.SetStopOnContinue (true);
2616 options.SetStopOnError (true);
2617 options.SetEchoCommands (false);
2618 options.SetPrintResults (true);
2619 options.SetAddToHistory (false);
2620
2621 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2622 &exc_ctx_with_reasons[i],
2623 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002624 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002625
2626 // If the command started the target going again, we should bag out of
2627 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002628 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2629 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002630 {
Daniel Malead01b2952012-11-29 21:49:15 +00002631 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002632 keep_going = false;
2633 }
2634 }
2635 }
2636 }
Jason Molenda879cf772011-09-23 00:42:55 +00002637
Caroline Tice969ed3d2011-05-02 20:41:46 +00002638 result.GetImmediateOutputStream()->Flush();
2639 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002640}
2641
Greg Claytonfbb76342013-11-20 21:07:01 +00002642const TargetPropertiesSP &
2643Target::GetGlobalProperties()
2644{
2645 static TargetPropertiesSP g_settings_sp;
2646 if (!g_settings_sp)
2647 {
2648 g_settings_sp.reset (new TargetProperties (NULL));
2649 }
2650 return g_settings_sp;
2651}
2652
2653Error
2654Target::Install (ProcessLaunchInfo *launch_info)
2655{
2656 Error error;
2657 PlatformSP platform_sp (GetPlatform());
2658 if (platform_sp)
2659 {
2660 if (platform_sp->IsRemote())
2661 {
2662 if (platform_sp->IsConnected())
2663 {
2664 // Install all files that have an install path, and always install the
2665 // main executable when connected to a remote platform
2666 const ModuleList& modules = GetImages();
2667 const size_t num_images = modules.GetSize();
2668 for (size_t idx = 0; idx < num_images; ++idx)
2669 {
2670 const bool is_main_executable = idx == 0;
2671 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2672 if (module_sp)
2673 {
2674 FileSpec local_file (module_sp->GetFileSpec());
2675 if (local_file)
2676 {
2677 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2678 if (!remote_file)
2679 {
2680 if (is_main_executable) // TODO: add setting for always installing main executable???
2681 {
2682 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002683 remote_file = platform_sp->GetRemoteWorkingDirectory();
2684 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002685 }
2686 }
2687 if (remote_file)
2688 {
2689 error = platform_sp->Install(local_file, remote_file);
2690 if (error.Success())
2691 {
2692 module_sp->SetPlatformFileSpec(remote_file);
2693 if (is_main_executable)
2694 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002695 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002696 if (launch_info)
2697 launch_info->SetExecutableFile(remote_file, false);
2698 }
2699 }
2700 else
2701 break;
2702 }
2703 }
2704 }
2705 }
2706 }
2707 }
2708 }
2709 return error;
2710}
Greg Clayton7b242382011-07-08 00:48:09 +00002711
Greg Claytond5944cd2013-12-06 01:12:00 +00002712bool
2713Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2714{
2715 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2716}
2717
2718bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002719Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2720{
2721 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2722}
2723
2724bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002725Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2726{
2727 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2728 if (old_section_load_addr != new_section_load_addr)
2729 {
2730 uint32_t stop_id = 0;
2731 ProcessSP process_sp(GetProcessSP());
2732 if (process_sp)
2733 stop_id = process_sp->GetStopID();
2734 else
2735 stop_id = m_section_load_history.GetLastStopID();
2736 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2737 return true; // Return true if the section load address was changed...
2738 }
2739 return false; // Return false to indicate nothing changed
2740
2741}
2742
Greg Clayton8012cad2014-11-17 19:39:20 +00002743size_t
2744Target::UnloadModuleSections (const ModuleList &module_list)
2745{
2746 size_t section_unload_count = 0;
2747 size_t num_modules = module_list.GetSize();
2748 for (size_t i=0; i<num_modules; ++i)
2749 {
2750 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2751 }
2752 return section_unload_count;
2753}
2754
2755size_t
2756Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2757{
2758 uint32_t stop_id = 0;
2759 ProcessSP process_sp(GetProcessSP());
2760 if (process_sp)
2761 stop_id = process_sp->GetStopID();
2762 else
2763 stop_id = m_section_load_history.GetLastStopID();
2764 SectionList *sections = module_sp->GetSectionList();
2765 size_t section_unload_count = 0;
2766 if (sections)
2767 {
2768 const uint32_t num_sections = sections->GetNumSections(0);
2769 for (uint32_t i = 0; i < num_sections; ++i)
2770 {
2771 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2772 }
2773 }
2774 return section_unload_count;
2775}
2776
Greg Claytond5944cd2013-12-06 01:12:00 +00002777bool
2778Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2779{
2780 uint32_t stop_id = 0;
2781 ProcessSP process_sp(GetProcessSP());
2782 if (process_sp)
2783 stop_id = process_sp->GetStopID();
2784 else
2785 stop_id = m_section_load_history.GetLastStopID();
2786 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2787}
2788
2789bool
2790Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2791{
2792 uint32_t stop_id = 0;
2793 ProcessSP process_sp(GetProcessSP());
2794 if (process_sp)
2795 stop_id = process_sp->GetStopID();
2796 else
2797 stop_id = m_section_load_history.GetLastStopID();
2798 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2799}
2800
2801void
2802Target::ClearAllLoadedSections ()
2803{
2804 m_section_load_history.Clear();
2805}
2806
Greg Claytonb09c5382013-12-13 17:20:18 +00002807
2808Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002809Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002810{
2811 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002812 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2813
2814 if (log)
2815 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2816
Greg Claytonb09c5382013-12-13 17:20:18 +00002817 StateType state = eStateInvalid;
2818
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002819 // Scope to temporarily get the process state in case someone has manually
2820 // remotely connected already to a process and we can skip the platform
2821 // launching.
2822 {
2823 ProcessSP process_sp (GetProcessSP());
2824
2825 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002826 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002827 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002828 if (log)
2829 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2830 }
2831 else
2832 {
2833 if (log)
2834 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2835 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002836 }
2837
Greg Claytonb09c5382013-12-13 17:20:18 +00002838 launch_info.GetFlags().Set (eLaunchFlagDebug);
2839
2840 // Get the value of synchronous execution here. If you wait till after you have started to
2841 // run, then you could have hit a breakpoint, whose command might switch the value, and
2842 // then you'll pick up that incorrect value.
2843 Debugger &debugger = GetDebugger();
2844 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2845
2846 PlatformSP platform_sp (GetPlatform());
2847
2848 // Finalize the file actions, and if none were given, default to opening
2849 // up a pseudo terminal
2850 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002851 if (log)
2852 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2853 __FUNCTION__,
2854 platform_sp ? "true" : "false",
2855 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2856 default_to_use_pty ? "true" : "false");
2857
Greg Claytonb09c5382013-12-13 17:20:18 +00002858 launch_info.FinalizeFileActions (this, default_to_use_pty);
2859
2860 if (state == eStateConnected)
2861 {
2862 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2863 {
2864 error.SetErrorString("can't launch in tty when launching through a remote connection");
2865 return error;
2866 }
2867 }
2868
2869 if (!launch_info.GetArchitecture().IsValid())
2870 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002871
2872 // 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 +00002873 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2874 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002875 if (log)
2876 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2877
Greg Clayton5df78fa2015-05-23 03:54:53 +00002878 // Get a weak pointer to the previous process if we have one
2879 ProcessWP process_wp;
2880 if (m_process_sp)
2881 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00002882 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2883 debugger,
2884 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002885 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00002886
2887 // Cleanup the old process since someone might still have a strong
2888 // reference to this process and we would like to allow it to cleanup
2889 // as much as it can without the object being destroyed. We try to
2890 // lock the shared pointer and if that works, then someone else still
2891 // has a strong reference to the process.
2892
2893 ProcessSP old_process_sp(process_wp.lock());
2894 if (old_process_sp)
2895 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00002896 }
2897 else
2898 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002899 if (log)
2900 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2901
Greg Claytonb09c5382013-12-13 17:20:18 +00002902 if (state == eStateConnected)
2903 {
2904 assert(m_process_sp);
2905 }
2906 else
2907 {
Todd Fiala015d8182014-07-22 23:41:36 +00002908 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002909 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002910 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002911 }
Todd Fiala015d8182014-07-22 23:41:36 +00002912
2913 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002914 if (m_process_sp)
2915 error = m_process_sp->Launch (launch_info);
2916 }
2917
2918 if (!m_process_sp)
2919 {
2920 if (error.Success())
2921 error.SetErrorString("failed to launch or debug process");
2922 return error;
2923 }
2924
2925 if (error.Success())
2926 {
Ilia K064e69f2015-03-23 21:16:25 +00002927 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00002928 {
Greg Clayton44d93782014-01-27 23:43:24 +00002929 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00002930 if (!hijack_listener_sp)
2931 {
2932 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
2933 launch_info.SetHijackListener(hijack_listener_sp);
2934 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
2935 }
Todd Fialaac33cc92014-10-09 01:02:08 +00002936
Ilia K38810f42015-05-20 10:15:47 +00002937 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002938
2939 if (state == eStateStopped)
2940 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002941 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00002942 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002943 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00002944 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002945 error = m_process_sp->PrivateResume();
2946 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00002947 {
Ilia K064e69f2015-03-23 21:16:25 +00002948 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
2949 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2950 if (!StateIsStoppedState(state, must_be_alive))
2951 {
2952 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
2953 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002954 }
2955 }
Ilia K064e69f2015-03-23 21:16:25 +00002956 else
2957 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002958 m_process_sp->RestoreProcessEvents();
2959 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00002960 }
2961 if (!error.Success())
2962 {
Ilia K064e69f2015-03-23 21:16:25 +00002963 Error error2;
2964 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
2965 error = error2;
2966 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002967 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002968 }
Greg Clayton40286e02014-04-30 20:29:09 +00002969 else if (state == eStateExited)
2970 {
Zachary Turner10687b02014-10-20 17:46:43 +00002971 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002972 const int exit_status = m_process_sp->GetExitStatus();
2973 const char *exit_desc = m_process_sp->GetExitDescription();
2974#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'."
2975 if (exit_desc && exit_desc[0])
2976 {
2977 if (with_shell)
2978 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2979 else
2980 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2981 }
2982 else
2983 {
2984 if (with_shell)
2985 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2986 else
2987 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2988 }
2989 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002990 else
2991 {
2992 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2993 }
2994 }
Greg Clayton44d93782014-01-27 23:43:24 +00002995 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002996 }
2997 else
2998 {
Greg Clayton44d93782014-01-27 23:43:24 +00002999 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003000 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003001 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003002 }
3003 return error;
3004}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003005
3006Error
3007Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3008{
3009 auto state = eStateInvalid;
3010 auto process_sp = GetProcessSP ();
3011 if (process_sp)
3012 {
3013 state = process_sp->GetState ();
3014 if (process_sp->IsAlive () && state != eStateConnected)
3015 {
3016 if (state == eStateAttaching)
3017 return Error ("process attach is in progress");
3018 return Error ("a process is already being debugged");
3019 }
3020 }
3021
3022 ListenerSP hijack_listener_sp (new Listener ("lldb.Target.Attach.attach.hijack"));
3023 attach_info.SetHijackListener (hijack_listener_sp);
3024
3025 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3026
3027 // If no process info was specified, then use the target executable
3028 // name as the process to attach to by default
3029 if (!attach_info.ProcessInfoSpecified ())
3030 {
3031 if (old_exec_module_sp)
3032 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3033
3034 if (!attach_info.ProcessInfoSpecified ())
3035 {
3036 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3037 }
3038 }
3039
3040 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
3041
3042 Error error;
3043 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3044 {
3045 SetPlatform (platform_sp);
3046 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3047 }
3048 else
3049 {
3050 if (state != eStateConnected)
3051 {
3052 const char *plugin_name = attach_info.GetProcessPluginName ();
3053 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3054 if (process_sp == nullptr)
3055 {
3056 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3057 return error;
3058 }
3059 }
3060 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
3061 error = process_sp->Attach (attach_info);
3062 }
3063
3064 if (error.Success () && process_sp)
3065 {
3066 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
3067 process_sp->RestoreProcessEvents ();
3068
3069 if (state != eStateStopped)
3070 {
3071 const char *exit_desc = process_sp->GetExitDescription ();
3072 if (exit_desc)
3073 error.SetErrorStringWithFormat ("attach failed: %s", exit_desc);
3074 else
3075 error.SetErrorString ("attach failed: process did not stop (no such process or permission problem?)");
Jason Molendaede31932015-04-17 05:01:58 +00003076 process_sp->Destroy (false);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003077 }
3078 }
3079 return error;
3080}
3081
Jim Ingham9575d842011-03-11 03:53:59 +00003082//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003083// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003084//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003085Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3086 UserID (uid),
3087 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003088 m_commands (),
3089 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003090 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003091 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003092{
3093}
3094
3095Target::StopHook::StopHook (const StopHook &rhs) :
3096 UserID (rhs.GetID()),
3097 m_target_sp (rhs.m_target_sp),
3098 m_commands (rhs.m_commands),
3099 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003100 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003101 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003102{
3103 if (rhs.m_thread_spec_ap.get() != NULL)
3104 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3105}
3106
3107
3108Target::StopHook::~StopHook ()
3109{
3110}
3111
3112void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003113Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3114{
3115 m_specifier_sp.reset(specifier);
3116}
3117
3118void
Jim Ingham9575d842011-03-11 03:53:59 +00003119Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3120{
3121 m_thread_spec_ap.reset (specifier);
3122}
3123
3124
3125void
3126Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3127{
3128 int indent_level = s->GetIndentLevel();
3129
3130 s->SetIndentLevel(indent_level + 2);
3131
Daniel Malead01b2952012-11-29 21:49:15 +00003132 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003133 if (m_active)
3134 s->Indent ("State: enabled\n");
3135 else
3136 s->Indent ("State: disabled\n");
3137
3138 if (m_specifier_sp)
3139 {
3140 s->Indent();
3141 s->PutCString ("Specifier:\n");
3142 s->SetIndentLevel (indent_level + 4);
3143 m_specifier_sp->GetDescription (s, level);
3144 s->SetIndentLevel (indent_level + 2);
3145 }
3146
3147 if (m_thread_spec_ap.get() != NULL)
3148 {
3149 StreamString tmp;
3150 s->Indent("Thread:\n");
3151 m_thread_spec_ap->GetDescription (&tmp, level);
3152 s->SetIndentLevel (indent_level + 4);
3153 s->Indent (tmp.GetData());
3154 s->PutCString ("\n");
3155 s->SetIndentLevel (indent_level + 2);
3156 }
3157
3158 s->Indent ("Commands: \n");
3159 s->SetIndentLevel (indent_level + 4);
3160 uint32_t num_commands = m_commands.GetSize();
3161 for (uint32_t i = 0; i < num_commands; i++)
3162 {
3163 s->Indent(m_commands.GetStringAtIndex(i));
3164 s->PutCString ("\n");
3165 }
3166 s->SetIndentLevel (indent_level);
3167}
3168
Greg Clayton67cc0632012-08-22 17:17:09 +00003169//--------------------------------------------------------------
3170// class TargetProperties
3171//--------------------------------------------------------------
3172
3173OptionEnumValueElement
3174lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003175{
Greg Clayton67cc0632012-08-22 17:17:09 +00003176 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3177 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3178 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
3179 { 0, NULL, NULL }
3180};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003181
Greg Clayton1f746072012-08-29 21:13:06 +00003182static OptionEnumValueElement
3183g_inline_breakpoint_enums[] =
3184{
3185 { 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."},
3186 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3187 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
3188 { 0, NULL, NULL }
3189};
3190
Jim Ingham0f063ba2013-03-02 00:26:47 +00003191typedef enum x86DisassemblyFlavor
3192{
3193 eX86DisFlavorDefault,
3194 eX86DisFlavorIntel,
3195 eX86DisFlavorATT
3196} x86DisassemblyFlavor;
3197
3198static OptionEnumValueElement
3199g_x86_dis_flavor_value_types[] =
3200{
3201 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3202 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3203 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
3204 { 0, NULL, NULL }
3205};
3206
Enrico Granata397ddd52013-05-21 20:13:34 +00003207static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003208g_hex_immediate_style_values[] =
3209{
3210 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3211 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
3212 { 0, NULL, NULL }
3213};
3214
3215static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003216g_load_script_from_sym_file_values[] =
3217{
3218 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3219 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3220 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
3221 { 0, NULL, NULL }
3222};
3223
Greg Claytonfd814c52013-08-13 01:42:25 +00003224
3225static OptionEnumValueElement
3226g_memory_module_load_level_values[] =
3227{
Greg Clayton86eac942013-08-13 21:32:34 +00003228 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003229 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3230 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
3231 { 0, NULL, NULL }
3232};
3233
Greg Clayton67cc0632012-08-22 17:17:09 +00003234static PropertyDefinition
3235g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003236{
Greg Clayton67cc0632012-08-22 17:17:09 +00003237 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
Ilia K055ad9b2015-05-18 13:41:01 +00003238 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Move breakpoints to nearest code." },
Dawn Perchike0c96ac2015-07-30 22:10:08 +00003239 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , NULL, NULL, "The language to use when interpreting expressions entered in commands." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003240 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "Path to a file containing expressions to be prepended to all expressions." },
Enrico Granata9aa7e8e2015-01-09 00:47:24 +00003241 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eDynamicDontRunTarget , NULL, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003242 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
3243 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
Jim Ingham0ca778e2015-09-15 18:03:00 +00003244 { "source-map" , OptionValue::eTypePathMap , false, 0 , NULL, NULL, "Source path remappings are used to track the change of location between a source file when built, and "
Greg Clayton67cc0632012-08-22 17:17:09 +00003245 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3246 "some part (starting at the root) of the path to the file when it was built, "
3247 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3248 "Each element of the array is checked in order and the first one that results in a match wins." },
3249 { "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 +00003250 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating debug symbol files." },
Sean Callanan85054342015-04-03 15:39:47 +00003251 { "clang-module-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating modules for Clang." },
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003252 { "auto-import-clang-modules" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Automatically load Clang modules referred to by the program." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003253 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
3254 { "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 +00003255 { "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 +00003256 { "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 +00003257 { "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." },
3258 { "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 +00003259 { "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." },
3260 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
3261 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
3262 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
3263 { "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 +00003264 { "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 +00003265 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
3266 { "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 +00003267 { "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 +00003268 "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. "
Jim Ingham63733852015-06-16 21:39:56 +00003269 "Usually this is limited to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
Greg Clayton1f746072012-08-29 21:13:06 +00003270 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003271 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3272 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3273 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003274 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003275 // 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.
3276 { "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 +00003277 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
3278 { "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 +00003279 { "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 +00003280 { "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 +00003281 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
3282 "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. "
3283 "This setting helps users control how much information gets loaded when loading modules from memory."
3284 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3285 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3286 "'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 +00003287 { "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 +00003288 { "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." },
Enrico Granata560558e2015-02-11 02:35:39 +00003289 { "display-runtime-support-values" , OptionValue::eTypeBoolean , false, false, NULL, NULL, "If true, LLDB will show variables that are meant to support the operation of a language's runtime support." },
Ewan Crawford78baa192015-05-13 09:18:18 +00003290 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, NULL, NULL, "Disable lock-step debugging, instead control threads independently." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003291 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
3292};
Enrico Granata560558e2015-02-11 02:35:39 +00003293
Greg Clayton67cc0632012-08-22 17:17:09 +00003294enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003295{
Greg Clayton67cc0632012-08-22 17:17:09 +00003296 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003297 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003298 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003299 ePropertyExprPrefix,
3300 ePropertyPreferDynamic,
3301 ePropertyEnableSynthetic,
3302 ePropertySkipPrologue,
3303 ePropertySourceMap,
3304 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003305 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003306 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003307 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003308 ePropertyMaxChildrenCount,
3309 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003310 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003311 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003312 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003313 ePropertyRunArgs,
3314 ePropertyEnvVars,
3315 ePropertyInheritEnv,
3316 ePropertyInputPath,
3317 ePropertyOutputPath,
3318 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003319 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003320 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003321 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003322 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003323 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003324 ePropertyUseHexImmediates,
3325 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003326 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003327 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003328 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003329 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003330 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003331 ePropertyDisplayRuntimeSupportValues,
3332 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003333};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003334
Caroline Ticedaccaa92010-09-20 20:44:43 +00003335
Greg Clayton67cc0632012-08-22 17:17:09 +00003336class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003337{
Greg Clayton67cc0632012-08-22 17:17:09 +00003338public:
3339 TargetOptionValueProperties (const ConstString &name) :
3340 OptionValueProperties (name),
3341 m_target (NULL),
3342 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003343 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003344 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003345
Greg Clayton67cc0632012-08-22 17:17:09 +00003346 // This constructor is used when creating TargetOptionValueProperties when it
3347 // is part of a new lldb_private::Target instance. It will copy all current
3348 // global property values as needed
3349 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3350 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3351 m_target (target),
3352 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003353 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003354 }
3355
3356 virtual const Property *
3357 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3358 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003359 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003360 // try and grab the setting from the current target if there is one. Else we just
3361 // use the one from this instance.
3362 if (idx == ePropertyEnvVars)
3363 GetHostEnvironmentIfNeeded ();
3364
3365 if (exe_ctx)
3366 {
3367 Target *target = exe_ctx->GetTargetPtr();
3368 if (target)
3369 {
3370 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3371 if (this != target_properties)
3372 return target_properties->ProtectedGetPropertyAtIndex (idx);
3373 }
3374 }
3375 return ProtectedGetPropertyAtIndex (idx);
3376 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003377
3378 lldb::TargetSP
3379 GetTargetSP ()
3380 {
3381 return m_target->shared_from_this();
3382 }
3383
Greg Clayton67cc0632012-08-22 17:17:09 +00003384protected:
3385
3386 void
3387 GetHostEnvironmentIfNeeded () const
3388 {
3389 if (!m_got_host_env)
3390 {
3391 if (m_target)
3392 {
3393 m_got_host_env = true;
3394 const uint32_t idx = ePropertyInheritEnv;
3395 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3396 {
3397 PlatformSP platform_sp (m_target->GetPlatform());
3398 if (platform_sp)
3399 {
3400 StringList env;
3401 if (platform_sp->GetEnvironment(env))
3402 {
3403 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3404 if (env_dict)
3405 {
3406 const bool can_replace = false;
3407 const size_t envc = env.GetSize();
3408 for (size_t idx=0; idx<envc; idx++)
3409 {
3410 const char *env_entry = env.GetStringAtIndex (idx);
3411 if (env_entry)
3412 {
3413 const char *equal_pos = ::strchr(env_entry, '=');
3414 ConstString key;
3415 // It is ok to have environment variables with no values
3416 const char *value = NULL;
3417 if (equal_pos)
3418 {
3419 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3420 if (equal_pos[1])
3421 value = equal_pos + 1;
3422 }
3423 else
3424 {
3425 key.SetCString(env_entry);
3426 }
3427 // Don't allow existing keys to be replaced with ones we get from the platform environment
3428 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3429 }
3430 }
3431 }
3432 }
3433 }
3434 }
3435 }
3436 }
3437 }
3438 Target *m_target;
3439 mutable bool m_got_host_env;
3440};
3441
Greg Claytonfbb76342013-11-20 21:07:01 +00003442//----------------------------------------------------------------------
3443// TargetProperties
3444//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003445TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003446 Properties (),
3447 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003448{
3449 if (target)
3450 {
3451 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003452
3453 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3454 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3455 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3456 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3457 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3458 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3459 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3460 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3461 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3462 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3463
3464 // Update m_launch_info once it was created
3465 Arg0ValueChangedCallback(this, NULL);
3466 RunArgsValueChangedCallback(this, NULL);
3467 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3468 InputPathValueChangedCallback(this, NULL);
3469 OutputPathValueChangedCallback(this, NULL);
3470 ErrorPathValueChangedCallback(this, NULL);
3471 DetachOnErrorValueChangedCallback(this, NULL);
3472 DisableASLRValueChangedCallback(this, NULL);
3473 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003474 }
3475 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003476 {
3477 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3478 m_collection_sp->Initialize(g_properties);
3479 m_collection_sp->AppendProperty(ConstString("process"),
3480 ConstString("Settings specify to processes."),
3481 true,
3482 Process::GetGlobalProperties()->GetValueProperties());
3483 }
Ilia K8f37ca52015-02-13 14:31:06 +00003484
Caroline Ticedaccaa92010-09-20 20:44:43 +00003485}
3486
Greg Clayton67cc0632012-08-22 17:17:09 +00003487TargetProperties::~TargetProperties ()
3488{
3489}
3490ArchSpec
3491TargetProperties::GetDefaultArchitecture () const
3492{
3493 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3494 if (value)
3495 return value->GetCurrentValue();
3496 return ArchSpec();
3497}
3498
3499void
3500TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3501{
3502 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3503 if (value)
3504 return value->SetCurrentValue(arch, true);
3505}
3506
Ilia K055ad9b2015-05-18 13:41:01 +00003507bool
3508TargetProperties::GetMoveToNearestCode() const
3509{
3510 const uint32_t idx = ePropertyMoveToNearestCode;
3511 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3512}
3513
Greg Clayton67cc0632012-08-22 17:17:09 +00003514lldb::DynamicValueType
3515TargetProperties::GetPreferDynamicValue() const
3516{
3517 const uint32_t idx = ePropertyPreferDynamic;
3518 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3519}
3520
3521bool
Greg Clayton15484402015-05-15 18:40:24 +00003522TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3523{
3524 const uint32_t idx = ePropertyPreferDynamic;
3525 return m_collection_sp->SetPropertyAtIndexAsEnumeration(NULL, idx, d);
3526}
3527
3528
3529bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003530TargetProperties::GetDisableASLR () const
3531{
3532 const uint32_t idx = ePropertyDisableASLR;
3533 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3534}
3535
3536void
3537TargetProperties::SetDisableASLR (bool b)
3538{
3539 const uint32_t idx = ePropertyDisableASLR;
3540 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3541}
3542
3543bool
Jim Ingham106d0282014-06-25 02:32:56 +00003544TargetProperties::GetDetachOnError () const
3545{
3546 const uint32_t idx = ePropertyDetachOnError;
3547 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3548}
3549
3550void
3551TargetProperties::SetDetachOnError (bool b)
3552{
3553 const uint32_t idx = ePropertyDetachOnError;
3554 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3555}
3556
3557bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003558TargetProperties::GetDisableSTDIO () const
3559{
3560 const uint32_t idx = ePropertyDisableSTDIO;
3561 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3562}
3563
3564void
3565TargetProperties::SetDisableSTDIO (bool b)
3566{
3567 const uint32_t idx = ePropertyDisableSTDIO;
3568 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3569}
3570
Jim Ingham0f063ba2013-03-02 00:26:47 +00003571const char *
3572TargetProperties::GetDisassemblyFlavor () const
3573{
3574 const uint32_t idx = ePropertyDisassemblyFlavor;
3575 const char *return_value;
3576
3577 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3578 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3579 return return_value;
3580}
3581
Greg Clayton1f746072012-08-29 21:13:06 +00003582InlineStrategy
3583TargetProperties::GetInlineStrategy () const
3584{
3585 const uint32_t idx = ePropertyInlineStrategy;
3586 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3587}
3588
Greg Clayton45392552012-10-17 22:57:12 +00003589const char *
3590TargetProperties::GetArg0 () const
3591{
3592 const uint32_t idx = ePropertyArg0;
3593 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3594}
3595
3596void
3597TargetProperties::SetArg0 (const char *arg)
3598{
3599 const uint32_t idx = ePropertyArg0;
3600 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003601 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003602}
3603
Greg Clayton67cc0632012-08-22 17:17:09 +00003604bool
3605TargetProperties::GetRunArguments (Args &args) const
3606{
3607 const uint32_t idx = ePropertyRunArgs;
3608 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3609}
3610
3611void
3612TargetProperties::SetRunArguments (const Args &args)
3613{
3614 const uint32_t idx = ePropertyRunArgs;
3615 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003616 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003617}
3618
3619size_t
3620TargetProperties::GetEnvironmentAsArgs (Args &env) const
3621{
3622 const uint32_t idx = ePropertyEnvVars;
3623 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3624}
3625
Ilia K8f37ca52015-02-13 14:31:06 +00003626void
3627TargetProperties::SetEnvironmentFromArgs (const Args &env)
3628{
3629 const uint32_t idx = ePropertyEnvVars;
3630 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003631 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003632}
3633
Greg Clayton67cc0632012-08-22 17:17:09 +00003634bool
3635TargetProperties::GetSkipPrologue() const
3636{
3637 const uint32_t idx = ePropertySkipPrologue;
3638 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3639}
3640
3641PathMappingList &
3642TargetProperties::GetSourcePathMap () const
3643{
3644 const uint32_t idx = ePropertySourceMap;
3645 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3646 assert(option_value);
3647 return option_value->GetCurrentValue();
3648}
3649
3650FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003651TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003652{
3653 const uint32_t idx = ePropertyExecutableSearchPaths;
3654 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3655 assert(option_value);
3656 return option_value->GetCurrentValue();
3657}
3658
Michael Sartaina7499c92013-07-01 19:45:50 +00003659FileSpecList &
3660TargetProperties::GetDebugFileSearchPaths ()
3661{
3662 const uint32_t idx = ePropertyDebugFileSearchPaths;
3663 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3664 assert(option_value);
3665 return option_value->GetCurrentValue();
3666}
3667
Sean Callanan85054342015-04-03 15:39:47 +00003668FileSpecList &
3669TargetProperties::GetClangModuleSearchPaths ()
3670{
3671 const uint32_t idx = ePropertyClangModuleSearchPaths;
3672 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3673 assert(option_value);
3674 return option_value->GetCurrentValue();
3675}
3676
Greg Clayton67cc0632012-08-22 17:17:09 +00003677bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003678TargetProperties::GetEnableAutoImportClangModules() const
3679{
3680 const uint32_t idx = ePropertyAutoImportClangModules;
3681 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3682}
3683
3684bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003685TargetProperties::GetEnableSyntheticValue () const
3686{
3687 const uint32_t idx = ePropertyEnableSynthetic;
3688 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3689}
3690
3691uint32_t
3692TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3693{
3694 const uint32_t idx = ePropertyMaxChildrenCount;
3695 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3696}
3697
3698uint32_t
3699TargetProperties::GetMaximumSizeOfStringSummary() const
3700{
3701 const uint32_t idx = ePropertyMaxSummaryLength;
3702 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3703}
3704
Enrico Granatad325bf92013-06-04 22:54:16 +00003705uint32_t
3706TargetProperties::GetMaximumMemReadSize () const
3707{
3708 const uint32_t idx = ePropertyMaxMemReadSize;
3709 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3710}
3711
Greg Clayton67cc0632012-08-22 17:17:09 +00003712FileSpec
3713TargetProperties::GetStandardInputPath () const
3714{
3715 const uint32_t idx = ePropertyInputPath;
3716 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3717}
3718
3719void
3720TargetProperties::SetStandardInputPath (const char *p)
3721{
3722 const uint32_t idx = ePropertyInputPath;
3723 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3724}
3725
3726FileSpec
3727TargetProperties::GetStandardOutputPath () const
3728{
3729 const uint32_t idx = ePropertyOutputPath;
3730 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3731}
3732
3733void
3734TargetProperties::SetStandardOutputPath (const char *p)
3735{
3736 const uint32_t idx = ePropertyOutputPath;
3737 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3738}
3739
3740FileSpec
3741TargetProperties::GetStandardErrorPath () const
3742{
3743 const uint32_t idx = ePropertyErrorPath;
3744 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3745}
3746
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003747LanguageType
3748TargetProperties::GetLanguage () const
3749{
3750 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage (NULL, ePropertyLanguage);
3751 if (value)
3752 return value->GetCurrentValue();
3753 return LanguageType();
3754}
3755
Greg Clayton6920b522012-08-22 18:39:03 +00003756const char *
3757TargetProperties::GetExpressionPrefixContentsAsCString ()
3758{
3759 const uint32_t idx = ePropertyExprPrefix;
3760 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3761 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003762 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003763 const bool null_terminate = true;
3764 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003765 if (data_sp)
3766 return (const char *) data_sp->GetBytes();
3767 }
Greg Clayton6920b522012-08-22 18:39:03 +00003768 return NULL;
3769}
3770
Greg Clayton67cc0632012-08-22 17:17:09 +00003771void
3772TargetProperties::SetStandardErrorPath (const char *p)
3773{
3774 const uint32_t idx = ePropertyErrorPath;
3775 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3776}
3777
3778bool
3779TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3780{
3781 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3782 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3783}
3784
Jim Ingham17d023f2013-03-13 17:58:04 +00003785bool
Daniel Malead79ae052013-08-07 21:54:09 +00003786TargetProperties::GetUseHexImmediates () const
3787{
3788 const uint32_t idx = ePropertyUseHexImmediates;
3789 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3790}
3791
3792bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003793TargetProperties::GetUseFastStepping () const
3794{
3795 const uint32_t idx = ePropertyUseFastStepping;
3796 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3797}
3798
Greg Claytonfb6621e2013-12-06 21:59:52 +00003799bool
3800TargetProperties::GetDisplayExpressionsInCrashlogs () const
3801{
3802 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3803 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3804}
3805
Enrico Granata397ddd52013-05-21 20:13:34 +00003806LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003807TargetProperties::GetLoadScriptFromSymbolFile () const
3808{
3809 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003810 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003811}
3812
Daniel Malead79ae052013-08-07 21:54:09 +00003813Disassembler::HexImmediateStyle
3814TargetProperties::GetHexImmediateStyle () const
3815{
3816 const uint32_t idx = ePropertyHexImmediateStyle;
3817 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3818}
3819
Greg Claytonfd814c52013-08-13 01:42:25 +00003820MemoryModuleLoadLevel
3821TargetProperties::GetMemoryModuleLoadLevel() const
3822{
3823 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3824 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3825}
3826
Jason Molendaa4bea722014-02-14 05:06:49 +00003827bool
3828TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3829{
3830 const uint32_t idx = ePropertyTrapHandlerNames;
3831 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3832}
Greg Claytonfd814c52013-08-13 01:42:25 +00003833
Jason Molendaa4bea722014-02-14 05:06:49 +00003834void
3835TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3836{
3837 const uint32_t idx = ePropertyTrapHandlerNames;
3838 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3839}
Greg Clayton67cc0632012-08-22 17:17:09 +00003840
Enrico Granata560558e2015-02-11 02:35:39 +00003841bool
3842TargetProperties::GetDisplayRuntimeSupportValues () const
3843{
3844 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3845 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3846}
3847
3848void
3849TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3850{
3851 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3852 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3853}
3854
Ewan Crawford78baa192015-05-13 09:18:18 +00003855bool
3856TargetProperties::GetNonStopModeEnabled () const
3857{
3858 const uint32_t idx = ePropertyNonStopModeEnabled;
3859 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3860}
3861
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00003862void
3863TargetProperties::SetNonStopModeEnabled (bool b)
3864{
3865 const uint32_t idx = ePropertyNonStopModeEnabled;
3866 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3867}
3868
Ilia K8f37ca52015-02-13 14:31:06 +00003869const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003870TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003871{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003872 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003873 return m_launch_info;
3874}
3875
3876void
3877TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3878{
3879 m_launch_info = launch_info;
3880 SetArg0(launch_info.GetArg0());
3881 SetRunArguments(launch_info.GetArguments());
3882 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3883 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3884 if (input_file_action)
3885 {
3886 const char *input_path = input_file_action->GetPath();
3887 if (input_path)
3888 SetStandardInputPath(input_path);
3889 }
3890 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3891 if (output_file_action)
3892 {
3893 const char *output_path = output_file_action->GetPath();
3894 if (output_path)
3895 SetStandardOutputPath(output_path);
3896 }
3897 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3898 if (error_file_action)
3899 {
3900 const char *error_path = error_file_action->GetPath();
3901 if (error_path)
3902 SetStandardErrorPath(error_path);
3903 }
3904 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3905 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3906 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3907}
3908
3909void
3910TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3911{
3912 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3913 this_->m_launch_info.SetArg0(this_->GetArg0());
3914}
3915
3916void
3917TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3918{
3919 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3920 Args args;
3921 if (this_->GetRunArguments(args))
3922 this_->m_launch_info.GetArguments() = args;
3923}
3924
3925void
3926TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3927{
3928 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3929 Args args;
3930 if (this_->GetEnvironmentAsArgs(args))
3931 this_->m_launch_info.GetEnvironmentEntries() = args;
3932}
3933
3934void
3935TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3936{
3937 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003938 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00003939}
3940
3941void
3942TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3943{
3944 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003945 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00003946}
3947
3948void
3949TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3950{
3951 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003952 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00003953}
3954
3955void
3956TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
3957{
3958 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3959 if (this_->GetDetachOnError())
3960 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3961 else
3962 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3963}
3964
3965void
3966TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
3967{
3968 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3969 if (this_->GetDisableASLR())
3970 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3971 else
3972 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3973}
3974
3975void
3976TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
3977{
3978 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3979 if (this_->GetDisableSTDIO())
3980 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
3981 else
3982 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
3983}
Ilia Keb2c19a2015-03-10 21:59:55 +00003984
3985//----------------------------------------------------------------------
3986// Target::TargetEventData
3987//----------------------------------------------------------------------
3988
3989Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
3990 EventData (),
3991 m_target_sp (target_sp),
3992 m_module_list ()
3993{
3994}
3995
3996Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
3997 EventData (),
3998 m_target_sp (target_sp),
3999 m_module_list (module_list)
4000{
4001}
4002
4003Target::TargetEventData::~TargetEventData()
4004{
4005}
4006
4007const ConstString &
4008Target::TargetEventData::GetFlavorString ()
4009{
4010 static ConstString g_flavor ("Target::TargetEventData");
4011 return g_flavor;
4012}
4013
4014void
4015Target::TargetEventData::Dump (Stream *s) const
4016{
4017}
4018
4019const Target::TargetEventData *
4020Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4021{
4022 if (event_ptr)
4023 {
4024 const EventData *event_data = event_ptr->GetData();
4025 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4026 return static_cast <const TargetEventData *> (event_ptr->GetData());
4027 }
4028 return NULL;
4029}
4030
4031TargetSP
4032Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4033{
4034 TargetSP target_sp;
4035 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4036 if (event_data)
4037 target_sp = event_data->m_target_sp;
4038 return target_sp;
4039}
4040
4041ModuleList
4042Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4043{
4044 ModuleList module_list;
4045 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4046 if (event_data)
4047 module_list = event_data->m_module_list;
4048 return module_list;
4049}