blob: 03a58454f0100fd982e0b70651ae37c59395696b [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000030#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000031#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000032#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000034#include "lldb/Core/Timer.h"
35#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000036#include "lldb/Expression/ClangASTSource.h"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000037#include "lldb/Expression/ClangPersistentVariables.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000038#include "lldb/Expression/ClangUserExpression.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000039#include "lldb/Expression/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000040#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000042#include "lldb/Interpreter/CommandInterpreter.h"
43#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000044#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000045#include "lldb/Interpreter/OptionValues.h"
46#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000047#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Symbol/ObjectFile.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000049#include "lldb/Target/LanguageRuntime.h"
50#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000052#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000053#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000054#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000055#include "lldb/Target/Thread.h"
56#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
58using namespace lldb;
59using namespace lldb_private;
60
Jim Ingham4bddaeb2012-02-16 06:50:00 +000061ConstString &
62Target::GetStaticBroadcasterClass ()
63{
64 static ConstString class_name ("lldb.target");
65 return class_name;
66}
67
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068//----------------------------------------------------------------------
69// Target constructor
70//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000071Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000072 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000073 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000074 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000075 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000076 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000077 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000078 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000079 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000080 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 m_breakpoint_list (false),
82 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000083 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000084 m_process_sp (),
85 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000087 m_scratch_ast_context_ap (),
88 m_scratch_ast_source_ap (),
89 m_ast_importer_ap (),
Zachary Turner32abc6e2015-03-03 19:23:09 +000090 m_persistent_variables (new ClangPersistentVariables),
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 ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000206 m_process_sp = Process::FindPlugin(*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);
Zachary Turner32abc6e2015-03-03 19:23:09 +0000233 m_persistent_variables->Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000234 m_stop_hooks.clear();
235 m_stop_hook_next_id = 0;
236 m_suppress_stop_hooks = false;
237}
238
239
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240BreakpointList &
241Target::GetBreakpointList(bool internal)
242{
243 if (internal)
244 return m_internal_breakpoint_list;
245 else
246 return m_breakpoint_list;
247}
248
249const BreakpointList &
250Target::GetBreakpointList(bool internal) const
251{
252 if (internal)
253 return m_internal_breakpoint_list;
254 else
255 return m_breakpoint_list;
256}
257
258BreakpointSP
259Target::GetBreakpointByID (break_id_t break_id)
260{
261 BreakpointSP bp_sp;
262
263 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
264 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
265 else
266 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
267
268 return bp_sp;
269}
270
271BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000272Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000273 const FileSpecList *source_file_spec_list,
274 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000275 bool internal,
276 bool hardware)
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));
279 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000280 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000281}
282
283
284BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000285Target::CreateBreakpoint (const FileSpecList *containingModules,
286 const FileSpec &file,
287 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000288 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000289 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000290 bool internal,
291 bool hardware)
Jim Ingham969795f2011-09-21 01:17:13 +0000292{
Greg Clayton1f746072012-08-29 21:13:06 +0000293 if (check_inlines == eLazyBoolCalculate)
294 {
295 const InlineStrategy inline_strategy = GetInlineStrategy();
296 switch (inline_strategy)
297 {
298 case eInlineBreakpointsNever:
299 check_inlines = eLazyBoolNo;
300 break;
301
302 case eInlineBreakpointsHeaders:
303 if (file.IsSourceImplementationFile())
304 check_inlines = eLazyBoolNo;
305 else
306 check_inlines = eLazyBoolYes;
307 break;
308
309 case eInlineBreakpointsAlways:
310 check_inlines = eLazyBoolYes;
311 break;
312 }
313 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000314 SearchFilterSP filter_sp;
315 if (check_inlines == eLazyBoolNo)
316 {
317 // Not checking for inlines, we are looking only for matching compile units
318 FileSpecList compile_unit_list;
319 compile_unit_list.Append (file);
320 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
321 }
322 else
323 {
324 filter_sp = GetSearchFilterForModuleList (containingModules);
325 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000326 if (skip_prologue == eLazyBoolCalculate)
327 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
328
Greg Clayton1f746072012-08-29 21:13:06 +0000329 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
330 file,
331 line_no,
332 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000333 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000334 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335}
336
337
338BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000339Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341 Address so_addr;
342 // Attempt to resolve our load address if possible, though it is ok if
343 // it doesn't resolve to section/offset.
344
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000345 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000346 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000347 if (!so_addr.IsValid())
348 {
349 // The address didn't resolve, so just set this as an absolute address
350 so_addr.SetOffset (addr);
351 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000352 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000353 return bp_sp;
354}
355
356BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000357Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000359 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000361 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362}
363
364BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000365Target::CreateBreakpoint (const FileSpecList *containingModules,
366 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000367 const char *func_name,
368 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000369 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000370 bool internal,
371 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000373 BreakpointSP bp_sp;
374 if (func_name)
375 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000376 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000377
378 if (skip_prologue == eLazyBoolCalculate)
379 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
380
Greg Claytond16e1e52011-07-12 17:06:17 +0000381 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
382 func_name,
383 func_name_type_mask,
384 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000385 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000386 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000387 }
388 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389}
390
Jim Inghamfab10e82012-03-06 00:37:27 +0000391lldb::BreakpointSP
392Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000393 const FileSpecList *containingSourceFiles,
394 const std::vector<std::string> &func_names,
395 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000396 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000397 bool internal,
398 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000399{
400 BreakpointSP bp_sp;
401 size_t num_names = func_names.size();
402 if (num_names > 0)
403 {
404 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000405
406 if (skip_prologue == eLazyBoolCalculate)
407 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
408
409 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000410 func_names,
411 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000412 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000413 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000414 }
415 return bp_sp;
416}
417
Jim Ingham133e0fb2012-03-03 02:05:11 +0000418BreakpointSP
419Target::CreateBreakpoint (const FileSpecList *containingModules,
420 const FileSpecList *containingSourceFiles,
421 const char *func_names[],
422 size_t num_names,
423 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000424 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000425 bool internal,
426 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000427{
428 BreakpointSP bp_sp;
429 if (num_names > 0)
430 {
431 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
432
Greg Clayton03da4cc2013-04-19 21:31:16 +0000433 if (skip_prologue == eLazyBoolCalculate)
434 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
435
436 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000437 func_names,
438 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000439 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000440 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000441 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000442 }
443 return bp_sp;
444}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445
446SearchFilterSP
447Target::GetSearchFilterForModule (const FileSpec *containingModule)
448{
449 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 if (containingModule != NULL)
451 {
452 // TODO: We should look into sharing module based search filters
453 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000454 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 }
456 else
457 {
458 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000459 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 filter_sp = m_search_filter_sp;
461 }
462 return filter_sp;
463}
464
Jim Ingham969795f2011-09-21 01:17:13 +0000465SearchFilterSP
466Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
467{
468 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000469 if (containingModules && containingModules->GetSize() != 0)
470 {
471 // TODO: We should look into sharing module based search filters
472 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000473 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000474 }
475 else
476 {
477 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000478 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000479 filter_sp = m_search_filter_sp;
480 }
481 return filter_sp;
482}
483
Jim Ingham87df91b2011-09-23 00:54:11 +0000484SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000485Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
486 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000487{
488 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
489 return GetSearchFilterForModuleList(containingModules);
490
491 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000492 if (containingModules == NULL)
493 {
494 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
495 // but that will take a little reworking.
496
Greg Claytone1cd1be2012-01-29 20:56:30 +0000497 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000498 }
499 else
500 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000501 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000502 }
503 return filter_sp;
504}
505
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000506BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000507Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000508 const FileSpecList *containingSourceFiles,
509 RegularExpression &func_regex,
510 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000511 bool internal,
512 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513{
Jim Ingham87df91b2011-09-23 00:54:11 +0000514 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000515 bool skip =
516 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
517 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000518 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
519 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000520 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521
Jim Ingham1460e4b2014-01-10 23:46:59 +0000522 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000523}
524
Jim Ingham219ba192012-03-05 04:47:34 +0000525lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000526Target::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 +0000527{
Jim Inghama72b31c2015-04-22 19:42:18 +0000528 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
529 if (exc_bkpt_sp && additional_args)
530 {
531 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
532 if (precondition_sp && additional_args)
533 {
534 if (error)
535 *error = precondition_sp->ConfigurePrecondition(*additional_args);
536 else
537 precondition_sp->ConfigurePrecondition(*additional_args);
538 }
539 }
540 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000541}
Jim Inghama72b31c2015-04-22 19:42:18 +0000542
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000543BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000544Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545{
546 BreakpointSP bp_sp;
547 if (filter_sp && resolver_sp)
548 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000549 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000551 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000553 return bp_sp;
554}
555
556void
557Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
558{
559 if (!bp_sp)
560 return;
561 if (internal)
562 m_internal_breakpoint_list.Add (bp_sp, false);
563 else
564 m_breakpoint_list.Add (bp_sp, true);
565
566 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
567 if (log)
568 {
569 StreamString s;
570 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
571 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
572 }
573
574 bp_sp->ResolveBreakpoint();
575
576 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000577 {
578 m_last_created_breakpoint = bp_sp;
579 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580}
581
Johnny Chen86364b42011-09-20 23:28:55 +0000582bool
583Target::ProcessIsValid()
584{
585 return (m_process_sp && m_process_sp->IsAlive());
586}
587
Johnny Chenb90827e2012-06-04 23:19:54 +0000588static bool
589CheckIfWatchpointsExhausted(Target *target, Error &error)
590{
591 uint32_t num_supported_hardware_watchpoints;
592 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
593 if (rc.Success())
594 {
595 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
596 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
597 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
598 num_supported_hardware_watchpoints);
599 }
600 return false;
601}
602
Johnny Chen01a67862011-10-14 00:42:25 +0000603// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000604// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000605WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000606Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000607{
Greg Clayton5160ce52013-03-27 23:08:40 +0000608 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000609 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000610 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000611 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000612
Johnny Chen01a67862011-10-14 00:42:25 +0000613 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000614 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000615 {
616 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000617 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000618 }
Jim Inghamc6462312013-06-18 21:52:48 +0000619
Johnny Chen45e541f2011-09-14 22:20:15 +0000620 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000621 {
622 if (size == 0)
623 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
624 else
Daniel Malead01b2952012-11-29 21:49:15 +0000625 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000626 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000627 }
Jim Inghamc6462312013-06-18 21:52:48 +0000628
629 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
630 {
631 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
632 }
Johnny Chen7313a642011-09-13 01:15:36 +0000633
Johnny Chen01a67862011-10-14 00:42:25 +0000634 // Currently we only support one watchpoint per address, with total number
635 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000636
637 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000638 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 +0000639 Mutex::Locker locker;
640 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000641 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000642 if (matched_sp)
643 {
Johnny Chen0c406372011-09-14 20:23:45 +0000644 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000645 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000646 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
647 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000648 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000649 if (size == old_size && kind == old_type)
650 {
Johnny Chen01a67862011-10-14 00:42:25 +0000651 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000652 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000653 }
654 else
655 {
Johnny Chen01a67862011-10-14 00:42:25 +0000656 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000657 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
658 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000659 }
Johnny Chen3c532582011-09-13 23:29:31 +0000660 }
661
Jason Molenda727e3922012-12-05 23:07:34 +0000662 if (!wp_sp)
663 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000664 wp_sp.reset(new Watchpoint(*this, addr, size, type));
665 wp_sp->SetWatchpointType(kind, notify);
666 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000667 }
Johnny Chen0c406372011-09-14 20:23:45 +0000668
Jim Ingham1b5792e2012-12-18 02:03:49 +0000669 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000670 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000671 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
672 __FUNCTION__,
673 error.Success() ? "succeeded" : "failed",
674 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000675
Jason Molenda727e3922012-12-05 23:07:34 +0000676 if (error.Fail())
677 {
Johnny Chen41b77262012-03-26 22:00:10 +0000678 // Enabling the watchpoint on the device side failed.
679 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000680 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000681 // See if we could provide more helpful error message.
682 if (!CheckIfWatchpointsExhausted(this, error))
683 {
684 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000685 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000686 }
Johnny Chen01a67862011-10-14 00:42:25 +0000687 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000688 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000689 else
Johnny Chen01a67862011-10-14 00:42:25 +0000690 m_last_created_watchpoint = wp_sp;
691 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000692}
693
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694void
695Target::RemoveAllBreakpoints (bool internal_also)
696{
Greg Clayton5160ce52013-03-27 23:08:40 +0000697 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 if (log)
699 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
700
Greg Clayton9fed0d82010-07-23 23:33:17 +0000701 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000703 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000704
705 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706}
707
708void
709Target::DisableAllBreakpoints (bool internal_also)
710{
Greg Clayton5160ce52013-03-27 23:08:40 +0000711 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 if (log)
713 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
714
715 m_breakpoint_list.SetEnabledAll (false);
716 if (internal_also)
717 m_internal_breakpoint_list.SetEnabledAll (false);
718}
719
720void
721Target::EnableAllBreakpoints (bool internal_also)
722{
Greg Clayton5160ce52013-03-27 23:08:40 +0000723 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 if (log)
725 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
726
727 m_breakpoint_list.SetEnabledAll (true);
728 if (internal_also)
729 m_internal_breakpoint_list.SetEnabledAll (true);
730}
731
732bool
733Target::RemoveBreakpointByID (break_id_t break_id)
734{
Greg Clayton5160ce52013-03-27 23:08:40 +0000735 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736 if (log)
737 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
738
739 if (DisableBreakpointByID (break_id))
740 {
741 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000742 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000744 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000745 if (m_last_created_breakpoint)
746 {
747 if (m_last_created_breakpoint->GetID() == break_id)
748 m_last_created_breakpoint.reset();
749 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000750 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000751 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000752 return true;
753 }
754 return false;
755}
756
757bool
758Target::DisableBreakpointByID (break_id_t break_id)
759{
Greg Clayton5160ce52013-03-27 23:08:40 +0000760 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 if (log)
762 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
763
764 BreakpointSP bp_sp;
765
766 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
767 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
768 else
769 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
770 if (bp_sp)
771 {
772 bp_sp->SetEnabled (false);
773 return true;
774 }
775 return false;
776}
777
778bool
779Target::EnableBreakpointByID (break_id_t break_id)
780{
Greg Clayton5160ce52013-03-27 23:08:40 +0000781 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782 if (log)
783 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
784 __FUNCTION__,
785 break_id,
786 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
787
788 BreakpointSP bp_sp;
789
790 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
791 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
792 else
793 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
794
795 if (bp_sp)
796 {
797 bp_sp->SetEnabled (true);
798 return true;
799 }
800 return false;
801}
802
Johnny Chenedf50372011-09-23 21:21:43 +0000803// The flag 'end_to_end', default to true, signifies that the operation is
804// performed end to end, for both the debugger and the debuggee.
805
Johnny Chen01a67862011-10-14 00:42:25 +0000806// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
807// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000808bool
Johnny Chen01a67862011-10-14 00:42:25 +0000809Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000810{
Greg Clayton5160ce52013-03-27 23:08:40 +0000811 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000812 if (log)
813 log->Printf ("Target::%s\n", __FUNCTION__);
814
Johnny Chenedf50372011-09-23 21:21:43 +0000815 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000816 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000817 return true;
818 }
819
820 // Otherwise, it's an end to end operation.
821
Johnny Chen86364b42011-09-20 23:28:55 +0000822 if (!ProcessIsValid())
823 return false;
824
Johnny Chen01a67862011-10-14 00:42:25 +0000825 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000826 for (size_t i = 0; i < num_watchpoints; ++i)
827 {
Johnny Chen01a67862011-10-14 00:42:25 +0000828 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
829 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000830 return false;
831
Johnny Chen01a67862011-10-14 00:42:25 +0000832 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000833 if (rc.Fail())
834 return false;
835 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000836 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000837 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000838 return true; // Success!
839}
840
Johnny Chen01a67862011-10-14 00:42:25 +0000841// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
842// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000843bool
Johnny Chen01a67862011-10-14 00:42:25 +0000844Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000845{
Greg Clayton5160ce52013-03-27 23:08:40 +0000846 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000847 if (log)
848 log->Printf ("Target::%s\n", __FUNCTION__);
849
Johnny Chenedf50372011-09-23 21:21:43 +0000850 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000851 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000852 return true;
853 }
854
855 // Otherwise, it's an end to end operation.
856
Johnny Chen86364b42011-09-20 23:28:55 +0000857 if (!ProcessIsValid())
858 return false;
859
Johnny Chen01a67862011-10-14 00:42:25 +0000860 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000861 for (size_t i = 0; i < num_watchpoints; ++i)
862 {
Johnny Chen01a67862011-10-14 00:42:25 +0000863 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
864 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000865 return false;
866
Johnny Chen01a67862011-10-14 00:42:25 +0000867 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000868 if (rc.Fail())
869 return false;
870 }
Johnny Chen86364b42011-09-20 23:28:55 +0000871 return true; // Success!
872}
873
Johnny Chen01a67862011-10-14 00:42:25 +0000874// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
875// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000876bool
Johnny Chen01a67862011-10-14 00:42:25 +0000877Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000878{
Greg Clayton5160ce52013-03-27 23:08:40 +0000879 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000880 if (log)
881 log->Printf ("Target::%s\n", __FUNCTION__);
882
Johnny Chenedf50372011-09-23 21:21:43 +0000883 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000884 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000885 return true;
886 }
887
888 // Otherwise, it's an end to end operation.
889
Johnny Chen86364b42011-09-20 23:28:55 +0000890 if (!ProcessIsValid())
891 return false;
892
Johnny Chen01a67862011-10-14 00:42:25 +0000893 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000894 for (size_t i = 0; i < num_watchpoints; ++i)
895 {
Johnny Chen01a67862011-10-14 00:42:25 +0000896 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
897 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000898 return false;
899
Johnny Chen01a67862011-10-14 00:42:25 +0000900 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000901 if (rc.Fail())
902 return false;
903 }
Johnny Chen86364b42011-09-20 23:28:55 +0000904 return true; // Success!
905}
906
Johnny Chena4d6bc92012-02-25 06:44:30 +0000907// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
908bool
909Target::ClearAllWatchpointHitCounts ()
910{
Greg Clayton5160ce52013-03-27 23:08:40 +0000911 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000912 if (log)
913 log->Printf ("Target::%s\n", __FUNCTION__);
914
915 size_t num_watchpoints = m_watchpoint_list.GetSize();
916 for (size_t i = 0; i < num_watchpoints; ++i)
917 {
918 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
919 if (!wp_sp)
920 return false;
921
922 wp_sp->ResetHitCount();
923 }
924 return true; // Success!
925}
926
Enrico Granata5e3fe042015-02-11 00:37:54 +0000927// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
928bool
929Target::ClearAllWatchpointHistoricValues ()
930{
931 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
932 if (log)
933 log->Printf ("Target::%s\n", __FUNCTION__);
934
935 size_t num_watchpoints = m_watchpoint_list.GetSize();
936 for (size_t i = 0; i < num_watchpoints; ++i)
937 {
938 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
939 if (!wp_sp)
940 return false;
941
942 wp_sp->ResetHistoricValues();
943 }
944 return true; // Success!
945}
946
Johnny Chen01a67862011-10-14 00:42:25 +0000947// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000948// during these operations.
949bool
Johnny Chen01a67862011-10-14 00:42:25 +0000950Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000951{
Greg Clayton5160ce52013-03-27 23:08:40 +0000952 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000953 if (log)
954 log->Printf ("Target::%s\n", __FUNCTION__);
955
956 if (!ProcessIsValid())
957 return false;
958
Johnny Chen01a67862011-10-14 00:42:25 +0000959 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000960 for (size_t i = 0; i < num_watchpoints; ++i)
961 {
Johnny Chen01a67862011-10-14 00:42:25 +0000962 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
963 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000964 return false;
965
Johnny Chen01a67862011-10-14 00:42:25 +0000966 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000967 }
968 return true; // Success!
969}
970
Johnny Chen01a67862011-10-14 00:42:25 +0000971// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000972bool
Johnny Chen01a67862011-10-14 00:42:25 +0000973Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000974{
Greg Clayton5160ce52013-03-27 23:08:40 +0000975 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000976 if (log)
977 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
978
979 if (!ProcessIsValid())
980 return false;
981
Johnny Chen01a67862011-10-14 00:42:25 +0000982 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
983 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000984 {
Johnny Chen01a67862011-10-14 00:42:25 +0000985 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000986 if (rc.Success())
987 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000988
Johnny Chenf04ee932011-09-22 18:04:58 +0000989 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000990 }
991 return false;
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::EnableWatchpointByID (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->EnableWatchpoint(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::RemoveWatchpointByID (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
Jim Inghamb0b45132013-07-02 02:09:46 +00001025 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1026 if (watch_to_remove_sp == m_last_created_watchpoint)
1027 m_last_created_watchpoint.reset();
1028
Johnny Chen01a67862011-10-14 00:42:25 +00001029 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001030 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001031 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001032 return true;
1033 }
1034 return false;
1035}
1036
Johnny Chen01a67862011-10-14 00:42:25 +00001037// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001038bool
Johnny Chen01a67862011-10-14 00:42:25 +00001039Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001040{
Greg Clayton5160ce52013-03-27 23:08:40 +00001041 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001042 if (log)
1043 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1044
1045 if (!ProcessIsValid())
1046 return false;
1047
Johnny Chen01a67862011-10-14 00:42:25 +00001048 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1049 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001050 {
Johnny Chen01a67862011-10-14 00:42:25 +00001051 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001052 return true;
1053 }
1054 return false;
1055}
1056
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057ModuleSP
1058Target::GetExecutableModule ()
1059{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001060 // search for the first executable in the module list
1061 for (size_t i = 0; i < m_images.GetSize(); ++i)
1062 {
1063 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1064 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1065 if (obj == nullptr)
1066 continue;
1067 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1068 return module_sp;
1069 }
1070 // as fall back return the first module loaded
1071 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001072}
1073
1074Module*
1075Target::GetExecutableModulePointer ()
1076{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001077 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078}
1079
Enrico Granata17598482012-11-08 02:22:02 +00001080static void
1081LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1082{
1083 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001084 StreamString feedback_stream;
1085 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001086 {
Enrico Granata97303392013-05-21 00:00:30 +00001087 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001088 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001089 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1090 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001091 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001092 if (feedback_stream.GetSize())
1093 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1094 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001095}
1096
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001097void
Greg Claytonb35db632013-11-09 00:03:31 +00001098Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099{
Greg Claytonb35db632013-11-09 00:03:31 +00001100 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001101 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102 m_images.Clear();
1103 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001104 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001105 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001106}
1107
1108void
Greg Claytonb35db632013-11-09 00:03:31 +00001109Target::DidExec ()
1110{
1111 // When a process exec's we need to know about it so we can do some cleanup.
1112 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1113 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1114}
1115
1116void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001117Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1118{
1119 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001120 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121
1122 if (executable_sp.get())
1123 {
1124 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001125 "Target::SetExecutableModule (executable = '%s')",
1126 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001128 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129
Jim Ingham5aee1622010-08-09 23:31:02 +00001130 // 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 +00001131 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001132 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001133 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001134 if (log)
1135 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1136 }
1137
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001138 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001139 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001141 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142 {
1143 executable_objfile->GetDependentModules(dependent_files);
1144 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1145 {
Greg Claytonded470d2011-03-19 01:12:21 +00001146 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1147 FileSpec platform_dependent_file_spec;
1148 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001149 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001150 else
1151 platform_dependent_file_spec = dependent_file_spec;
1152
Greg Claytonb9a01b32012-02-26 05:51:37 +00001153 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1154 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 if (image_module_sp.get())
1156 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 ObjectFile *objfile = image_module_sp->GetObjectFile();
1158 if (objfile)
1159 objfile->GetDependentModules(dependent_files);
1160 }
1161 }
1162 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163 }
1164}
1165
1166
Jim Ingham5aee1622010-08-09 23:31:02 +00001167bool
1168Target::SetArchitecture (const ArchSpec &arch_spec)
1169{
Greg Clayton5160ce52013-03-27 23:08:40 +00001170 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001171 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001172 {
Greg Clayton70512312012-05-08 01:45:38 +00001173 // If we haven't got a valid arch spec, or the architectures are
1174 // compatible, so just update the architecture. Architectures can be
1175 // equal, yet the triple OS and vendor might change, so we need to do
1176 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001177 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001178 if (log)
1179 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 +00001180 return true;
1181 }
1182 else
1183 {
1184 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001185 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001186 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 +00001187 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001188 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001189
Greg Claytonb35db632013-11-09 00:03:31 +00001190 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001191 // Need to do something about unsetting breakpoints.
1192
1193 if (executable_sp)
1194 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001195 if (log)
1196 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 +00001197 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1198 Error error = ModuleList::GetSharedModule (module_spec,
1199 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001200 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001201 NULL,
1202 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001203
1204 if (!error.Fail() && executable_sp)
1205 {
1206 SetExecutableModule (executable_sp, true);
1207 return true;
1208 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001209 }
1210 }
Greg Clayton70512312012-05-08 01:45:38 +00001211 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001212}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001214bool
1215Target::MergeArchitecture (const ArchSpec &arch_spec)
1216{
1217 if (arch_spec.IsValid())
1218 {
1219 if (m_arch.IsCompatibleMatch(arch_spec))
1220 {
1221 // The current target arch is compatible with "arch_spec", see if we
1222 // can improve our current architecture using bits from "arch_spec"
1223
1224 // Merge bits from arch_spec into "merged_arch" and set our architecture
1225 ArchSpec merged_arch (m_arch);
1226 merged_arch.MergeFrom (arch_spec);
1227 return SetArchitecture(merged_arch);
1228 }
1229 else
1230 {
1231 // The new architecture is different, we just need to replace it
1232 return SetArchitecture(arch_spec);
1233 }
1234 }
1235 return false;
1236}
1237
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238void
Enrico Granataefe637d2012-11-08 19:16:03 +00001239Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001240{
1241}
1242
1243void
Enrico Granataefe637d2012-11-08 19:16:03 +00001244Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245{
1246 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001247 if (m_valid)
1248 {
1249 ModuleList my_module_list;
1250 my_module_list.Append(module_sp);
1251 LoadScriptingResourceForModule(module_sp, this);
1252 ModulesDidLoad (my_module_list);
1253 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254}
1255
1256void
Enrico Granataefe637d2012-11-08 19:16:03 +00001257Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001258{
1259 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001260 if (m_valid)
1261 {
1262 ModuleList my_module_list;
1263 my_module_list.Append(module_sp);
1264 ModulesDidUnload (my_module_list, false);
1265 }
Enrico Granata17598482012-11-08 02:22:02 +00001266}
1267
1268void
Enrico Granataefe637d2012-11-08 19:16:03 +00001269Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270{
Jim Inghame716ae02011-08-03 01:00:06 +00001271 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001272 if (m_valid)
1273 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274}
1275
1276void
1277Target::ModulesDidLoad (ModuleList &module_list)
1278{
Greg Clayton23f8c952014-03-24 23:10:19 +00001279 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001280 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001281 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001282 if (m_process_sp)
1283 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001284 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001285 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001286 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001287 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001288}
1289
1290void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001291Target::SymbolsDidLoad (ModuleList &module_list)
1292{
Greg Clayton23f8c952014-03-24 23:10:19 +00001293 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001294 {
Jim Ingham31caf982013-06-04 23:01:35 +00001295 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001296 {
Jim Ingham31caf982013-06-04 23:01:35 +00001297 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1298 if (runtime)
1299 {
1300 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1301 objc_runtime->SymbolsDidLoad(module_list);
1302 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001303 }
Jim Ingham31caf982013-06-04 23:01:35 +00001304
Greg Clayton095eeaa2013-11-05 23:28:00 +00001305 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001306 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001307 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001308}
1309
1310void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001311Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001312{
Greg Clayton23f8c952014-03-24 23:10:19 +00001313 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001314 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001315 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001316 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001317 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001318 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319}
1320
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001321bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001322Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001323{
Greg Clayton67cc0632012-08-22 17:17:09 +00001324 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001325 {
1326 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001327 ModuleSpec module_spec (module_file_spec);
1328 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001329
1330 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1331 // black list.
1332 if (num_modules > 0)
1333 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001334 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001335 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001336 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001337 return false;
1338 }
1339 return true;
1340 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001341 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001342 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001343}
1344
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001345bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001346Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001347{
Greg Clayton67cc0632012-08-22 17:17:09 +00001348 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001349 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001350 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001351 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001352 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001353 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001354}
1355
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001356size_t
Greg Claytondb598232011-01-07 01:57:07 +00001357Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1358{
Greg Claytone72dfb32012-02-24 01:59:29 +00001359 SectionSP section_sp (addr.GetSection());
1360 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001361 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001362 // 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 +00001363 if (section_sp->IsEncrypted())
1364 {
Greg Clayton57f06302012-05-25 17:05:55 +00001365 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001366 return 0;
1367 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001368 ModuleSP module_sp (section_sp->GetModule());
1369 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001370 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001371 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1372 if (objfile)
1373 {
1374 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1375 addr.GetOffset(),
1376 dst,
1377 dst_len);
1378 if (bytes_read > 0)
1379 return bytes_read;
1380 else
1381 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1382 }
Greg Claytondb598232011-01-07 01:57:07 +00001383 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001384 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001385 }
1386 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001387 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001388 }
1389 else
Greg Claytondb598232011-01-07 01:57:07 +00001390 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001391
Greg Claytondb598232011-01-07 01:57:07 +00001392 return 0;
1393}
1394
1395size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001396Target::ReadMemory (const Address& addr,
1397 bool prefer_file_cache,
1398 void *dst,
1399 size_t dst_len,
1400 Error &error,
1401 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001403 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001404
Enrico Granata9128ee22011-09-06 19:20:51 +00001405 // if we end up reading this from process memory, we will fill this
1406 // with the actual load address
1407 if (load_addr_ptr)
1408 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1409
Greg Claytondb598232011-01-07 01:57:07 +00001410 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001411
1412 addr_t load_addr = LLDB_INVALID_ADDRESS;
1413 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001414 Address resolved_addr;
1415 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001416 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001417 SectionLoadList &section_load_list = GetSectionLoadList();
1418 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001419 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001420 // No sections are loaded, so we must assume we are not running
1421 // yet and anything we are given is a file address.
1422 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1423 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001424 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001425 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001426 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001427 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001428 // we have manually loaded some sections with "target modules load ..."
1429 // or because we have have a live process that has sections loaded
1430 // through the dynamic loader
1431 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001432 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001433 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001434 }
Greg Clayton357132e2011-03-26 19:14:58 +00001435 if (!resolved_addr.IsValid())
1436 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001437
Greg Claytonc749eb82011-07-11 05:12:02 +00001438
Greg Claytondb598232011-01-07 01:57:07 +00001439 if (prefer_file_cache)
1440 {
1441 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1442 if (bytes_read > 0)
1443 return bytes_read;
1444 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001445
Johnny Chen86364b42011-09-20 23:28:55 +00001446 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001447 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001448 if (load_addr == LLDB_INVALID_ADDRESS)
1449 load_addr = resolved_addr.GetLoadAddress (this);
1450
Greg Claytondda4f7b2010-06-30 23:03:03 +00001451 if (load_addr == LLDB_INVALID_ADDRESS)
1452 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001453 ModuleSP addr_module_sp (resolved_addr.GetModule());
1454 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001455 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001456 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001457 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001458 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001459 else
Daniel Malead01b2952012-11-29 21:49:15 +00001460 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001461 }
1462 else
1463 {
Greg Claytondb598232011-01-07 01:57:07 +00001464 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001465 if (bytes_read != dst_len)
1466 {
1467 if (error.Success())
1468 {
1469 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001470 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 else
Daniel Malead01b2952012-11-29 21:49:15 +00001472 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 +00001473 }
1474 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001475 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001476 {
1477 if (load_addr_ptr)
1478 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001479 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001480 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001481 // If the address is not section offset we have an address that
1482 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001483 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001484 // more we can do. If it is section offset, we might be able to
1485 // read cached memory from the object file.
1486 if (!resolved_addr.IsSectionOffset())
1487 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001488 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001490
Greg Claytonc749eb82011-07-11 05:12:02 +00001491 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001492 {
Greg Claytondb598232011-01-07 01:57:07 +00001493 // If we didn't already try and read from the object file cache, then
1494 // try it after failing to read from the process.
1495 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001496 }
1497 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001498}
1499
Greg Claytond16e1e52011-07-12 17:06:17 +00001500size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001501Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1502{
1503 char buf[256];
1504 out_str.clear();
1505 addr_t curr_addr = addr.GetLoadAddress(this);
1506 Address address(addr);
1507 while (1)
1508 {
1509 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1510 if (length == 0)
1511 break;
1512 out_str.append(buf, length);
1513 // If we got "length - 1" bytes, we didn't get the whole C string, we
1514 // need to read some more characters
1515 if (length == sizeof(buf) - 1)
1516 curr_addr += length;
1517 else
1518 break;
1519 address = Address(curr_addr);
1520 }
1521 return out_str.size();
1522}
1523
1524
1525size_t
1526Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1527{
1528 size_t total_cstr_len = 0;
1529 if (dst && dst_max_len)
1530 {
1531 result_error.Clear();
1532 // NULL out everything just to be safe
1533 memset (dst, 0, dst_max_len);
1534 Error error;
1535 addr_t curr_addr = addr.GetLoadAddress(this);
1536 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001537
1538 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1539 // think this really needs to be tied to the memory cache subsystem's
1540 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001541 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001542
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001543 size_t bytes_left = dst_max_len - 1;
1544 char *curr_dst = dst;
1545
1546 while (bytes_left > 0)
1547 {
1548 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1549 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1550 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1551
1552 if (bytes_read == 0)
1553 {
1554 result_error = error;
1555 dst[total_cstr_len] = '\0';
1556 break;
1557 }
1558 const size_t len = strlen(curr_dst);
1559
1560 total_cstr_len += len;
1561
1562 if (len < bytes_to_read)
1563 break;
1564
1565 curr_dst += bytes_read;
1566 curr_addr += bytes_read;
1567 bytes_left -= bytes_read;
1568 address = Address(curr_addr);
1569 }
1570 }
1571 else
1572 {
1573 if (dst == NULL)
1574 result_error.SetErrorString("invalid arguments");
1575 else
1576 result_error.Clear();
1577 }
1578 return total_cstr_len;
1579}
1580
1581size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001582Target::ReadScalarIntegerFromMemory (const Address& addr,
1583 bool prefer_file_cache,
1584 uint32_t byte_size,
1585 bool is_signed,
1586 Scalar &scalar,
1587 Error &error)
1588{
1589 uint64_t uval;
1590
1591 if (byte_size <= sizeof(uval))
1592 {
1593 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1594 if (bytes_read == byte_size)
1595 {
1596 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001597 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001598 if (byte_size <= 4)
1599 scalar = data.GetMaxU32 (&offset, byte_size);
1600 else
1601 scalar = data.GetMaxU64 (&offset, byte_size);
1602
1603 if (is_signed)
1604 scalar.SignExtend(byte_size * 8);
1605 return bytes_read;
1606 }
1607 }
1608 else
1609 {
1610 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1611 }
1612 return 0;
1613}
1614
1615uint64_t
1616Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1617 bool prefer_file_cache,
1618 size_t integer_byte_size,
1619 uint64_t fail_value,
1620 Error &error)
1621{
1622 Scalar scalar;
1623 if (ReadScalarIntegerFromMemory (addr,
1624 prefer_file_cache,
1625 integer_byte_size,
1626 false,
1627 scalar,
1628 error))
1629 return scalar.ULongLong(fail_value);
1630 return fail_value;
1631}
1632
1633bool
1634Target::ReadPointerFromMemory (const Address& addr,
1635 bool prefer_file_cache,
1636 Error &error,
1637 Address &pointer_addr)
1638{
1639 Scalar scalar;
1640 if (ReadScalarIntegerFromMemory (addr,
1641 prefer_file_cache,
1642 m_arch.GetAddressByteSize(),
1643 false,
1644 scalar,
1645 error))
1646 {
1647 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1648 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1649 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001650 SectionLoadList &section_load_list = GetSectionLoadList();
1651 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001652 {
1653 // No sections are loaded, so we must assume we are not running
1654 // yet and anything we are given is a file address.
1655 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1656 }
1657 else
1658 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001659 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001660 // we have manually loaded some sections with "target modules load ..."
1661 // or because we have have a live process that has sections loaded
1662 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001663 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001664 }
1665 // We weren't able to resolve the pointer value, so just return
1666 // an address with no section
1667 if (!pointer_addr.IsValid())
1668 pointer_addr.SetOffset (pointer_vm_addr);
1669 return true;
1670
1671 }
1672 }
1673 return false;
1674}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001675
1676ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001677Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001678{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001679 ModuleSP module_sp;
1680
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001681 Error error;
1682
Jim Ingham4a94c912012-05-17 18:38:42 +00001683 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1684 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001685
Jim Ingham4a94c912012-05-17 18:38:42 +00001686 if (module_spec.GetUUID().IsValid())
1687 module_sp = m_images.FindFirstModule(module_spec);
1688
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001689 if (!module_sp)
1690 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001691 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1692 bool did_create_module = false;
1693
1694 // If there are image search path entries, try to use them first to acquire a suitable image.
1695 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001696 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001697 ModuleSpec transformed_spec (module_spec);
1698 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1699 {
1700 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1701 error = ModuleList::GetSharedModule (transformed_spec,
1702 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001703 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001704 &old_module_sp,
1705 &did_create_module);
1706 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001707 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001708
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001709 if (!module_sp)
1710 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001711 // If we have a UUID, we can check our global shared module list in case
1712 // we already have it. If we don't have a valid UUID, then we can't since
1713 // the path in "module_spec" will be a platform path, and we will need to
1714 // let the platform find that file. For example, we could be asking for
1715 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1716 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1717 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1718 // cache.
1719 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001720 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001721 // We have a UUID, it is OK to check the global module list...
1722 error = ModuleList::GetSharedModule (module_spec,
1723 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001724 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001725 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001726 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001727 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001728
1729 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001730 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001731 // The platform is responsible for finding and caching an appropriate
1732 // module in the shared module cache.
1733 if (m_platform_sp)
1734 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001735 error = m_platform_sp->GetSharedModule (module_spec,
1736 m_process_sp.get(),
1737 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001738 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001739 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001740 &did_create_module);
1741 }
1742 else
1743 {
1744 error.SetErrorString("no platform is currently set");
1745 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001746 }
1747 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001748
Jim Ingham4a94c912012-05-17 18:38:42 +00001749 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1750 // module in the list already, and if there was, let's remove it.
1751 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001753 ObjectFile *objfile = module_sp->GetObjectFile();
1754 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001755 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001756 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001757 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001758 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1759 case ObjectFile::eTypeExecutable: /// A normal executable
1760 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1761 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1762 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1763 break;
1764 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1765 if (error_ptr)
1766 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1767 return ModuleSP();
1768 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1769 if (error_ptr)
1770 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1771 return ModuleSP();
1772 default:
1773 if (error_ptr)
1774 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1775 return ModuleSP();
1776 }
1777 // GetSharedModule is not guaranteed to find the old shared module, for instance
1778 // in the common case where you pass in the UUID, it is only going to find the one
1779 // module matching the UUID. In fact, it has no good way to know what the "old module"
1780 // relevant to this target is, since there might be many copies of a module with this file spec
1781 // in various running debug sessions, but only one of them will belong to this target.
1782 // So let's remove the UUID from the module list, and look in the target's module list.
1783 // Only do this if there is SOMETHING else in the module spec...
1784 if (!old_module_sp)
1785 {
1786 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001787 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001788 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1789 module_spec_copy.GetUUID().Clear();
1790
1791 ModuleList found_modules;
1792 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1793 if (num_found == 1)
1794 {
1795 old_module_sp = found_modules.GetModuleAtIndex(0);
1796 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001797 }
1798 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001799
1800 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1801 {
1802 m_images.ReplaceModule(old_module_sp, module_sp);
1803 Module *old_module_ptr = old_module_sp.get();
1804 old_module_sp.reset();
1805 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1806 }
1807 else
1808 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001809 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001810 else
1811 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001812 }
1813 }
1814 if (error_ptr)
1815 *error_ptr = error;
1816 return module_sp;
1817}
1818
1819
Greg Claytond9e416c2012-02-18 05:35:26 +00001820TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001821Target::CalculateTarget ()
1822{
Greg Claytond9e416c2012-02-18 05:35:26 +00001823 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001824}
1825
Greg Claytond9e416c2012-02-18 05:35:26 +00001826ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827Target::CalculateProcess ()
1828{
Greg Claytond9e416c2012-02-18 05:35:26 +00001829 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001830}
1831
Greg Claytond9e416c2012-02-18 05:35:26 +00001832ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001833Target::CalculateThread ()
1834{
Greg Claytond9e416c2012-02-18 05:35:26 +00001835 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001836}
1837
Jason Molendab57e4a12013-11-04 09:33:30 +00001838StackFrameSP
1839Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001840{
Jason Molendab57e4a12013-11-04 09:33:30 +00001841 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001842}
1843
1844void
Greg Clayton0603aa92010-10-04 01:05:56 +00001845Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001846{
Greg Claytonc14ee322011-09-22 04:58:26 +00001847 exe_ctx.Clear();
1848 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001849}
1850
1851PathMappingList &
1852Target::GetImageSearchPathList ()
1853{
1854 return m_image_search_paths;
1855}
1856
1857void
1858Target::ImageSearchPathsChanged
1859(
1860 const PathMappingList &path_list,
1861 void *baton
1862)
1863{
1864 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001865 ModuleSP exe_module_sp (target->GetExecutableModule());
1866 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001867 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001868}
1869
1870ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001871Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001872{
Greg Clayton73da2442011-08-03 01:23:55 +00001873 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001874 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001875 {
Greg Clayton73da2442011-08-03 01:23:55 +00001876 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001877 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001878 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001879 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001880 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1881 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001882 return m_scratch_ast_context_ap.get();
1883}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001884
Sean Callanan686b2312011-11-16 18:20:47 +00001885ClangASTImporter *
1886Target::GetClangASTImporter()
1887{
1888 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1889
1890 if (!ast_importer)
1891 {
1892 ast_importer = new ClangASTImporter();
1893 m_ast_importer_ap.reset(ast_importer);
1894 }
1895
1896 return ast_importer;
1897}
1898
Greg Clayton99d0faf2010-11-18 23:32:35 +00001899void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001900Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001901{
Greg Clayton6920b522012-08-22 18:39:03 +00001902 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001903}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001904
Greg Clayton99d0faf2010-11-18 23:32:35 +00001905void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001906Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001907{
Greg Clayton6920b522012-08-22 18:39:03 +00001908 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001909}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001910
Greg Claytonc859e2d2012-02-13 23:10:39 +00001911FileSpecList
1912Target::GetDefaultExecutableSearchPaths ()
1913{
Greg Clayton67cc0632012-08-22 17:17:09 +00001914 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1915 if (properties_sp)
1916 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001917 return FileSpecList();
1918}
1919
Michael Sartaina7499c92013-07-01 19:45:50 +00001920FileSpecList
1921Target::GetDefaultDebugFileSearchPaths ()
1922{
1923 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1924 if (properties_sp)
1925 return properties_sp->GetDebugFileSearchPaths();
1926 return FileSpecList();
1927}
1928
Sean Callanan85054342015-04-03 15:39:47 +00001929FileSpecList
1930Target::GetDefaultClangModuleSearchPaths ()
1931{
1932 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1933 if (properties_sp)
1934 return properties_sp->GetClangModuleSearchPaths();
1935 return FileSpecList();
1936}
1937
Caroline Ticedaccaa92010-09-20 20:44:43 +00001938ArchSpec
1939Target::GetDefaultArchitecture ()
1940{
Greg Clayton67cc0632012-08-22 17:17:09 +00001941 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1942 if (properties_sp)
1943 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001944 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001945}
1946
1947void
Greg Clayton67cc0632012-08-22 17:17:09 +00001948Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001949{
Greg Clayton67cc0632012-08-22 17:17:09 +00001950 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1951 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001952 {
1953 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 +00001954 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001955 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001956}
1957
Greg Clayton0603aa92010-10-04 01:05:56 +00001958Target *
1959Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1960{
1961 // The target can either exist in the "process" of ExecutionContext, or in
1962 // the "target_sp" member of SymbolContext. This accessor helper function
1963 // will get the target from one of these locations.
1964
1965 Target *target = NULL;
1966 if (sc_ptr != NULL)
1967 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001968 if (target == NULL && exe_ctx_ptr)
1969 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001970 return target;
1971}
1972
Jim Ingham1624a2d2014-05-05 02:26:40 +00001973ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001974Target::EvaluateExpression
1975(
1976 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001977 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001978 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001979 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001980)
1981{
Enrico Granata97fca502012-09-18 17:43:16 +00001982 result_valobj_sp.reset();
1983
Jim Ingham8646d3c2014-05-05 02:47:44 +00001984 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001985
Greg Claytond1767f02011-12-08 02:13:16 +00001986 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1987 return execution_results;
1988
Jim Ingham6026ca32011-05-12 02:06:14 +00001989 // We shouldn't run stop hooks in expressions.
1990 // Be sure to reset this if you return anywhere within this function.
1991 bool old_suppress_value = m_suppress_stop_hooks;
1992 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001993
1994 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001995
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001996 if (frame)
1997 {
1998 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001999 }
2000 else if (m_process_sp)
2001 {
2002 m_process_sp->CalculateExecutionContext(exe_ctx);
2003 }
2004 else
2005 {
2006 CalculateExecutionContext(exe_ctx);
2007 }
2008
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002009 // Make sure we aren't just trying to see the value of a persistent
2010 // variable (something like "$0")
2011 lldb::ClangExpressionVariableSP persistent_var_sp;
2012 // Only check for persistent variables the expression starts with a '$'
2013 if (expr_cstr[0] == '$')
Zachary Turner32abc6e2015-03-03 19:23:09 +00002014 persistent_var_sp = m_persistent_variables->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002015
2016 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002017 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002018 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002019 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002020 }
2021 else
2022 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002023 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002024 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002025 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002026 options,
2027 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002028 prefix,
2029 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002030 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002031 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002032
2033 m_suppress_stop_hooks = old_suppress_value;
2034
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002035 return execution_results;
2036}
2037
Zachary Turner32abc6e2015-03-03 19:23:09 +00002038ClangPersistentVariables &
2039Target::GetPersistentVariables()
2040{
2041 return *m_persistent_variables;
2042}
2043
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002044lldb::addr_t
2045Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2046{
2047 addr_t code_addr = load_addr;
2048 switch (m_arch.GetMachine())
2049 {
2050 case llvm::Triple::arm:
2051 case llvm::Triple::thumb:
2052 switch (addr_class)
2053 {
2054 case eAddressClassData:
2055 case eAddressClassDebug:
2056 return LLDB_INVALID_ADDRESS;
2057
2058 case eAddressClassUnknown:
2059 case eAddressClassInvalid:
2060 case eAddressClassCode:
2061 case eAddressClassCodeAlternateISA:
2062 case eAddressClassRuntime:
2063 // Check if bit zero it no set?
2064 if ((code_addr & 1ull) == 0)
2065 {
2066 // Bit zero isn't set, check if the address is a multiple of 2?
2067 if (code_addr & 2ull)
2068 {
2069 // The address is a multiple of 2 so it must be thumb, set bit zero
2070 code_addr |= 1ull;
2071 }
2072 else if (addr_class == eAddressClassCodeAlternateISA)
2073 {
2074 // We checked the address and the address claims to be the alternate ISA
2075 // which means thumb, so set bit zero.
2076 code_addr |= 1ull;
2077 }
2078 }
2079 break;
2080 }
2081 break;
2082
2083 default:
2084 break;
2085 }
2086 return code_addr;
2087}
2088
2089lldb::addr_t
2090Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2091{
2092 addr_t opcode_addr = load_addr;
2093 switch (m_arch.GetMachine())
2094 {
2095 case llvm::Triple::arm:
2096 case llvm::Triple::thumb:
2097 switch (addr_class)
2098 {
2099 case eAddressClassData:
2100 case eAddressClassDebug:
2101 return LLDB_INVALID_ADDRESS;
2102
2103 case eAddressClassInvalid:
2104 case eAddressClassUnknown:
2105 case eAddressClassCode:
2106 case eAddressClassCodeAlternateISA:
2107 case eAddressClassRuntime:
2108 opcode_addr &= ~(1ull);
2109 break;
2110 }
2111 break;
2112
2113 default:
2114 break;
2115 }
2116 return opcode_addr;
2117}
2118
Greg Clayton9585fbf2013-03-19 00:20:55 +00002119SourceManager &
2120Target::GetSourceManager ()
2121{
2122 if (m_source_manager_ap.get() == NULL)
2123 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2124 return *m_source_manager_ap;
2125}
2126
Sean Callanan9998acd2014-12-05 01:21:59 +00002127ClangModulesDeclVendor *
2128Target::GetClangModulesDeclVendor ()
2129{
2130 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2131
2132 {
2133 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2134
2135 if (!m_clang_modules_decl_vendor_ap)
2136 {
2137 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2138 }
2139 }
2140
2141 return m_clang_modules_decl_vendor_ap.get();
2142}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002143
Greg Clayton44d93782014-01-27 23:43:24 +00002144Target::StopHookSP
2145Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002146{
2147 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002148 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2149 m_stop_hooks[new_uid] = stop_hook_sp;
2150 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002151}
2152
2153bool
2154Target::RemoveStopHookByID (lldb::user_id_t user_id)
2155{
2156 size_t num_removed;
2157 num_removed = m_stop_hooks.erase (user_id);
2158 if (num_removed == 0)
2159 return false;
2160 else
2161 return true;
2162}
2163
2164void
2165Target::RemoveAllStopHooks ()
2166{
2167 m_stop_hooks.clear();
2168}
2169
2170Target::StopHookSP
2171Target::GetStopHookByID (lldb::user_id_t user_id)
2172{
2173 StopHookSP found_hook;
2174
2175 StopHookCollection::iterator specified_hook_iter;
2176 specified_hook_iter = m_stop_hooks.find (user_id);
2177 if (specified_hook_iter != m_stop_hooks.end())
2178 found_hook = (*specified_hook_iter).second;
2179 return found_hook;
2180}
2181
2182bool
2183Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2184{
2185 StopHookCollection::iterator specified_hook_iter;
2186 specified_hook_iter = m_stop_hooks.find (user_id);
2187 if (specified_hook_iter == m_stop_hooks.end())
2188 return false;
2189
2190 (*specified_hook_iter).second->SetIsActive (active_state);
2191 return true;
2192}
2193
2194void
2195Target::SetAllStopHooksActiveState (bool active_state)
2196{
2197 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2198 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2199 {
2200 (*pos).second->SetIsActive (active_state);
2201 }
2202}
2203
2204void
2205Target::RunStopHooks ()
2206{
Jim Ingham6026ca32011-05-12 02:06:14 +00002207 if (m_suppress_stop_hooks)
2208 return;
2209
Jim Ingham9575d842011-03-11 03:53:59 +00002210 if (!m_process_sp)
2211 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002212
2213 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2214 // since in that case we do not want to run the stop-hooks
2215 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2216 return;
2217
Jim Ingham9575d842011-03-11 03:53:59 +00002218 if (m_stop_hooks.empty())
2219 return;
2220
2221 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2222
2223 // If there aren't any active stop hooks, don't bother either:
2224 bool any_active_hooks = false;
2225 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2226 {
2227 if ((*pos).second->IsActive())
2228 {
2229 any_active_hooks = true;
2230 break;
2231 }
2232 }
2233 if (!any_active_hooks)
2234 return;
2235
2236 CommandReturnObject result;
2237
2238 std::vector<ExecutionContext> exc_ctx_with_reasons;
2239 std::vector<SymbolContext> sym_ctx_with_reasons;
2240
2241 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2242 size_t num_threads = cur_threadlist.GetSize();
2243 for (size_t i = 0; i < num_threads; i++)
2244 {
2245 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2246 if (cur_thread_sp->ThreadStoppedForAReason())
2247 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002248 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002249 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2250 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2251 }
2252 }
2253
2254 // If no threads stopped for a reason, don't run the stop-hooks.
2255 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2256 if (num_exe_ctx == 0)
2257 return;
2258
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002259 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2260 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002261
2262 bool keep_going = true;
2263 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002264 bool print_hook_header;
2265 bool print_thread_header;
2266
2267 if (num_exe_ctx == 1)
2268 print_thread_header = false;
2269 else
2270 print_thread_header = true;
2271
2272 if (m_stop_hooks.size() == 1)
2273 print_hook_header = false;
2274 else
2275 print_hook_header = true;
2276
Jim Ingham9575d842011-03-11 03:53:59 +00002277 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2278 {
2279 // result.Clear();
2280 StopHookSP cur_hook_sp = (*pos).second;
2281 if (!cur_hook_sp->IsActive())
2282 continue;
2283
2284 bool any_thread_matched = false;
2285 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2286 {
2287 if ((cur_hook_sp->GetSpecifier () == NULL
2288 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2289 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002290 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002291 {
2292 if (!hooks_ran)
2293 {
Jim Ingham9575d842011-03-11 03:53:59 +00002294 hooks_ran = true;
2295 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002296 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002297 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002298 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2299 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2300 NULL);
2301 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002302 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002303 else
Daniel Malead01b2952012-11-29 21:49:15 +00002304 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002305 any_thread_matched = true;
2306 }
2307
Jim Ingham381e25b2011-03-22 01:47:27 +00002308 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002309 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002310
2311 CommandInterpreterRunOptions options;
2312 options.SetStopOnContinue (true);
2313 options.SetStopOnError (true);
2314 options.SetEchoCommands (false);
2315 options.SetPrintResults (true);
2316 options.SetAddToHistory (false);
2317
2318 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2319 &exc_ctx_with_reasons[i],
2320 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002321 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002322
2323 // If the command started the target going again, we should bag out of
2324 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002325 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2326 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002327 {
Daniel Malead01b2952012-11-29 21:49:15 +00002328 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002329 keep_going = false;
2330 }
2331 }
2332 }
2333 }
Jason Molenda879cf772011-09-23 00:42:55 +00002334
Caroline Tice969ed3d2011-05-02 20:41:46 +00002335 result.GetImmediateOutputStream()->Flush();
2336 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002337}
2338
Greg Claytonfbb76342013-11-20 21:07:01 +00002339const TargetPropertiesSP &
2340Target::GetGlobalProperties()
2341{
2342 static TargetPropertiesSP g_settings_sp;
2343 if (!g_settings_sp)
2344 {
2345 g_settings_sp.reset (new TargetProperties (NULL));
2346 }
2347 return g_settings_sp;
2348}
2349
2350Error
2351Target::Install (ProcessLaunchInfo *launch_info)
2352{
2353 Error error;
2354 PlatformSP platform_sp (GetPlatform());
2355 if (platform_sp)
2356 {
2357 if (platform_sp->IsRemote())
2358 {
2359 if (platform_sp->IsConnected())
2360 {
2361 // Install all files that have an install path, and always install the
2362 // main executable when connected to a remote platform
2363 const ModuleList& modules = GetImages();
2364 const size_t num_images = modules.GetSize();
2365 for (size_t idx = 0; idx < num_images; ++idx)
2366 {
2367 const bool is_main_executable = idx == 0;
2368 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2369 if (module_sp)
2370 {
2371 FileSpec local_file (module_sp->GetFileSpec());
2372 if (local_file)
2373 {
2374 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2375 if (!remote_file)
2376 {
2377 if (is_main_executable) // TODO: add setting for always installing main executable???
2378 {
2379 // Always install the main executable
Chaoren Linf34f4102015-05-09 01:21:32 +00002380 remote_file = FileSpec(module_sp->GetFileSpec().GetFilename().AsCString(),
2381 false, module_sp->GetArchitecture());
Greg Claytonfbb76342013-11-20 21:07:01 +00002382 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
Greg Claytonfbb76342013-11-20 21:07:01 +00002383 }
2384 }
2385 if (remote_file)
2386 {
2387 error = platform_sp->Install(local_file, remote_file);
2388 if (error.Success())
2389 {
2390 module_sp->SetPlatformFileSpec(remote_file);
2391 if (is_main_executable)
2392 {
Chaoren Lince36c4c2015-05-05 18:43:19 +00002393 platform_sp->SetFilePermissions(remote_file.GetPath(false).c_str(), 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002394 if (launch_info)
2395 launch_info->SetExecutableFile(remote_file, false);
2396 }
2397 }
2398 else
2399 break;
2400 }
2401 }
2402 }
2403 }
2404 }
2405 }
2406 }
2407 return error;
2408}
Greg Clayton7b242382011-07-08 00:48:09 +00002409
Greg Claytond5944cd2013-12-06 01:12:00 +00002410bool
2411Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2412{
2413 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2414}
2415
2416bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002417Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2418{
2419 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2420}
2421
2422bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002423Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2424{
2425 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2426 if (old_section_load_addr != new_section_load_addr)
2427 {
2428 uint32_t stop_id = 0;
2429 ProcessSP process_sp(GetProcessSP());
2430 if (process_sp)
2431 stop_id = process_sp->GetStopID();
2432 else
2433 stop_id = m_section_load_history.GetLastStopID();
2434 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2435 return true; // Return true if the section load address was changed...
2436 }
2437 return false; // Return false to indicate nothing changed
2438
2439}
2440
Greg Clayton8012cad2014-11-17 19:39:20 +00002441size_t
2442Target::UnloadModuleSections (const ModuleList &module_list)
2443{
2444 size_t section_unload_count = 0;
2445 size_t num_modules = module_list.GetSize();
2446 for (size_t i=0; i<num_modules; ++i)
2447 {
2448 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2449 }
2450 return section_unload_count;
2451}
2452
2453size_t
2454Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2455{
2456 uint32_t stop_id = 0;
2457 ProcessSP process_sp(GetProcessSP());
2458 if (process_sp)
2459 stop_id = process_sp->GetStopID();
2460 else
2461 stop_id = m_section_load_history.GetLastStopID();
2462 SectionList *sections = module_sp->GetSectionList();
2463 size_t section_unload_count = 0;
2464 if (sections)
2465 {
2466 const uint32_t num_sections = sections->GetNumSections(0);
2467 for (uint32_t i = 0; i < num_sections; ++i)
2468 {
2469 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2470 }
2471 }
2472 return section_unload_count;
2473}
2474
Greg Claytond5944cd2013-12-06 01:12:00 +00002475bool
2476Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2477{
2478 uint32_t stop_id = 0;
2479 ProcessSP process_sp(GetProcessSP());
2480 if (process_sp)
2481 stop_id = process_sp->GetStopID();
2482 else
2483 stop_id = m_section_load_history.GetLastStopID();
2484 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2485}
2486
2487bool
2488Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2489{
2490 uint32_t stop_id = 0;
2491 ProcessSP process_sp(GetProcessSP());
2492 if (process_sp)
2493 stop_id = process_sp->GetStopID();
2494 else
2495 stop_id = m_section_load_history.GetLastStopID();
2496 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2497}
2498
2499void
2500Target::ClearAllLoadedSections ()
2501{
2502 m_section_load_history.Clear();
2503}
2504
Greg Claytonb09c5382013-12-13 17:20:18 +00002505
2506Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002507Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002508{
2509 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002510 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2511
2512 if (log)
2513 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2514
Greg Claytonb09c5382013-12-13 17:20:18 +00002515 StateType state = eStateInvalid;
2516
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002517 // Scope to temporarily get the process state in case someone has manually
2518 // remotely connected already to a process and we can skip the platform
2519 // launching.
2520 {
2521 ProcessSP process_sp (GetProcessSP());
2522
2523 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002524 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002525 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002526 if (log)
2527 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2528 }
2529 else
2530 {
2531 if (log)
2532 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2533 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002534 }
2535
Greg Claytonb09c5382013-12-13 17:20:18 +00002536 launch_info.GetFlags().Set (eLaunchFlagDebug);
2537
2538 // Get the value of synchronous execution here. If you wait till after you have started to
2539 // run, then you could have hit a breakpoint, whose command might switch the value, and
2540 // then you'll pick up that incorrect value.
2541 Debugger &debugger = GetDebugger();
2542 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2543
2544 PlatformSP platform_sp (GetPlatform());
2545
2546 // Finalize the file actions, and if none were given, default to opening
2547 // up a pseudo terminal
2548 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002549 if (log)
2550 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2551 __FUNCTION__,
2552 platform_sp ? "true" : "false",
2553 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2554 default_to_use_pty ? "true" : "false");
2555
Greg Claytonb09c5382013-12-13 17:20:18 +00002556 launch_info.FinalizeFileActions (this, default_to_use_pty);
2557
2558 if (state == eStateConnected)
2559 {
2560 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2561 {
2562 error.SetErrorString("can't launch in tty when launching through a remote connection");
2563 return error;
2564 }
2565 }
2566
2567 if (!launch_info.GetArchitecture().IsValid())
2568 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002569
2570 // 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 +00002571 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2572 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002573 if (log)
2574 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2575
Greg Claytonb09c5382013-12-13 17:20:18 +00002576 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2577 debugger,
2578 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002579 error);
2580 }
2581 else
2582 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002583 if (log)
2584 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2585
Greg Claytonb09c5382013-12-13 17:20:18 +00002586 if (state == eStateConnected)
2587 {
2588 assert(m_process_sp);
2589 }
2590 else
2591 {
Todd Fiala015d8182014-07-22 23:41:36 +00002592 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002593 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002594 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002595 }
Todd Fiala015d8182014-07-22 23:41:36 +00002596
2597 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002598 if (m_process_sp)
2599 error = m_process_sp->Launch (launch_info);
2600 }
2601
2602 if (!m_process_sp)
2603 {
2604 if (error.Success())
2605 error.SetErrorString("failed to launch or debug process");
2606 return error;
2607 }
2608
2609 if (error.Success())
2610 {
Ilia K064e69f2015-03-23 21:16:25 +00002611 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00002612 {
Ilia K064e69f2015-03-23 21:16:25 +00002613 EventSP event_sp;
Greg Clayton44d93782014-01-27 23:43:24 +00002614 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00002615 if (!hijack_listener_sp)
2616 {
2617 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
2618 launch_info.SetHijackListener(hijack_listener_sp);
2619 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
2620 }
Todd Fialaac33cc92014-10-09 01:02:08 +00002621
Ilia K064e69f2015-03-23 21:16:25 +00002622 StateType state = m_process_sp->WaitForProcessToStop (NULL, &event_sp, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002623
2624 if (state == eStateStopped)
2625 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002626 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00002627 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002628 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00002629 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002630 error = m_process_sp->PrivateResume();
2631 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00002632 {
Ilia K064e69f2015-03-23 21:16:25 +00002633 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
2634 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2635 if (!StateIsStoppedState(state, must_be_alive))
2636 {
2637 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
2638 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002639 }
2640 }
Ilia K064e69f2015-03-23 21:16:25 +00002641 else
2642 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002643 m_process_sp->RestoreProcessEvents();
2644 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00002645 }
2646 if (!error.Success())
2647 {
Ilia K064e69f2015-03-23 21:16:25 +00002648 Error error2;
2649 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
2650 error = error2;
2651 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002652 }
2653 else
2654 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002655 assert(synchronous_execution && launch_info.GetFlags().Test(eLaunchFlagStopAtEntry));
Ilia K064e69f2015-03-23 21:16:25 +00002656
2657 // Target was stopped at entry as was intended. Need to notify the listeners about it.
2658 m_process_sp->RestoreProcessEvents();
2659 m_process_sp->HandlePrivateEvent(event_sp);
Greg Claytonb09c5382013-12-13 17:20:18 +00002660 }
2661 }
Greg Clayton40286e02014-04-30 20:29:09 +00002662 else if (state == eStateExited)
2663 {
Zachary Turner10687b02014-10-20 17:46:43 +00002664 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002665 const int exit_status = m_process_sp->GetExitStatus();
2666 const char *exit_desc = m_process_sp->GetExitDescription();
2667#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'."
2668 if (exit_desc && exit_desc[0])
2669 {
2670 if (with_shell)
2671 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2672 else
2673 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2674 }
2675 else
2676 {
2677 if (with_shell)
2678 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2679 else
2680 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2681 }
2682 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002683 else
2684 {
2685 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2686 }
2687 }
Greg Clayton44d93782014-01-27 23:43:24 +00002688 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002689 }
2690 else
2691 {
Greg Clayton44d93782014-01-27 23:43:24 +00002692 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002693 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002694 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002695 }
2696 return error;
2697}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002698
2699Error
2700Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
2701{
2702 auto state = eStateInvalid;
2703 auto process_sp = GetProcessSP ();
2704 if (process_sp)
2705 {
2706 state = process_sp->GetState ();
2707 if (process_sp->IsAlive () && state != eStateConnected)
2708 {
2709 if (state == eStateAttaching)
2710 return Error ("process attach is in progress");
2711 return Error ("a process is already being debugged");
2712 }
2713 }
2714
2715 ListenerSP hijack_listener_sp (new Listener ("lldb.Target.Attach.attach.hijack"));
2716 attach_info.SetHijackListener (hijack_listener_sp);
2717
2718 const ModuleSP old_exec_module_sp = GetExecutableModule ();
2719
2720 // If no process info was specified, then use the target executable
2721 // name as the process to attach to by default
2722 if (!attach_info.ProcessInfoSpecified ())
2723 {
2724 if (old_exec_module_sp)
2725 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
2726
2727 if (!attach_info.ProcessInfoSpecified ())
2728 {
2729 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
2730 }
2731 }
2732
2733 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
2734
2735 Error error;
2736 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
2737 {
2738 SetPlatform (platform_sp);
2739 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
2740 }
2741 else
2742 {
2743 if (state != eStateConnected)
2744 {
2745 const char *plugin_name = attach_info.GetProcessPluginName ();
2746 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
2747 if (process_sp == nullptr)
2748 {
2749 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
2750 return error;
2751 }
2752 }
2753 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
2754 error = process_sp->Attach (attach_info);
2755 }
2756
2757 if (error.Success () && process_sp)
2758 {
2759 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
2760 process_sp->RestoreProcessEvents ();
2761
2762 if (state != eStateStopped)
2763 {
2764 const char *exit_desc = process_sp->GetExitDescription ();
2765 if (exit_desc)
2766 error.SetErrorStringWithFormat ("attach failed: %s", exit_desc);
2767 else
2768 error.SetErrorString ("attach failed: process did not stop (no such process or permission problem?)");
Jason Molendaede31932015-04-17 05:01:58 +00002769 process_sp->Destroy (false);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002770 }
2771 }
2772 return error;
2773}
2774
Jim Ingham9575d842011-03-11 03:53:59 +00002775//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002776// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002777//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002778Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2779 UserID (uid),
2780 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002781 m_commands (),
2782 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002783 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002784 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002785{
2786}
2787
2788Target::StopHook::StopHook (const StopHook &rhs) :
2789 UserID (rhs.GetID()),
2790 m_target_sp (rhs.m_target_sp),
2791 m_commands (rhs.m_commands),
2792 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002793 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002794 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002795{
2796 if (rhs.m_thread_spec_ap.get() != NULL)
2797 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2798}
2799
2800
2801Target::StopHook::~StopHook ()
2802{
2803}
2804
2805void
Zachary Turner32abc6e2015-03-03 19:23:09 +00002806Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
2807{
2808 m_specifier_sp.reset(specifier);
2809}
2810
2811void
Jim Ingham9575d842011-03-11 03:53:59 +00002812Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2813{
2814 m_thread_spec_ap.reset (specifier);
2815}
2816
2817
2818void
2819Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2820{
2821 int indent_level = s->GetIndentLevel();
2822
2823 s->SetIndentLevel(indent_level + 2);
2824
Daniel Malead01b2952012-11-29 21:49:15 +00002825 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002826 if (m_active)
2827 s->Indent ("State: enabled\n");
2828 else
2829 s->Indent ("State: disabled\n");
2830
2831 if (m_specifier_sp)
2832 {
2833 s->Indent();
2834 s->PutCString ("Specifier:\n");
2835 s->SetIndentLevel (indent_level + 4);
2836 m_specifier_sp->GetDescription (s, level);
2837 s->SetIndentLevel (indent_level + 2);
2838 }
2839
2840 if (m_thread_spec_ap.get() != NULL)
2841 {
2842 StreamString tmp;
2843 s->Indent("Thread:\n");
2844 m_thread_spec_ap->GetDescription (&tmp, level);
2845 s->SetIndentLevel (indent_level + 4);
2846 s->Indent (tmp.GetData());
2847 s->PutCString ("\n");
2848 s->SetIndentLevel (indent_level + 2);
2849 }
2850
2851 s->Indent ("Commands: \n");
2852 s->SetIndentLevel (indent_level + 4);
2853 uint32_t num_commands = m_commands.GetSize();
2854 for (uint32_t i = 0; i < num_commands; i++)
2855 {
2856 s->Indent(m_commands.GetStringAtIndex(i));
2857 s->PutCString ("\n");
2858 }
2859 s->SetIndentLevel (indent_level);
2860}
2861
Greg Clayton67cc0632012-08-22 17:17:09 +00002862//--------------------------------------------------------------
2863// class TargetProperties
2864//--------------------------------------------------------------
2865
2866OptionEnumValueElement
2867lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002868{
Greg Clayton67cc0632012-08-22 17:17:09 +00002869 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2870 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2871 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2872 { 0, NULL, NULL }
2873};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002874
Greg Clayton1f746072012-08-29 21:13:06 +00002875static OptionEnumValueElement
2876g_inline_breakpoint_enums[] =
2877{
2878 { 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."},
2879 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2880 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2881 { 0, NULL, NULL }
2882};
2883
Jim Ingham0f063ba2013-03-02 00:26:47 +00002884typedef enum x86DisassemblyFlavor
2885{
2886 eX86DisFlavorDefault,
2887 eX86DisFlavorIntel,
2888 eX86DisFlavorATT
2889} x86DisassemblyFlavor;
2890
2891static OptionEnumValueElement
2892g_x86_dis_flavor_value_types[] =
2893{
2894 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2895 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2896 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2897 { 0, NULL, NULL }
2898};
2899
Enrico Granata397ddd52013-05-21 20:13:34 +00002900static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002901g_hex_immediate_style_values[] =
2902{
2903 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2904 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2905 { 0, NULL, NULL }
2906};
2907
2908static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002909g_load_script_from_sym_file_values[] =
2910{
2911 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2912 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2913 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2914 { 0, NULL, NULL }
2915};
2916
Greg Claytonfd814c52013-08-13 01:42:25 +00002917
2918static OptionEnumValueElement
2919g_memory_module_load_level_values[] =
2920{
Greg Clayton86eac942013-08-13 21:32:34 +00002921 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002922 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2923 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2924 { 0, NULL, NULL }
2925};
2926
Greg Clayton67cc0632012-08-22 17:17:09 +00002927static PropertyDefinition
2928g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002929{
Greg Clayton67cc0632012-08-22 17:17:09 +00002930 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2931 { "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 +00002932 { "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 +00002933 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2934 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2935 { "source-map" , OptionValue::eTypePathMap , false, 0 , NULL, NULL, "Source path remappings used to track the change of location between a source file when built, and "
2936 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2937 "some part (starting at the root) of the path to the file when it was built, "
2938 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2939 "Each element of the array is checked in order and the first one that results in a match wins." },
2940 { "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 +00002941 { "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 +00002942 { "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 +00002943 { "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 +00002944 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2945 { "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 +00002946 { "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 +00002947 { "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 +00002948 { "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." },
2949 { "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 +00002950 { "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." },
2951 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2952 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2953 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2954 { "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 +00002955 { "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 +00002956 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2957 { "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 +00002958 { "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 +00002959 "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. "
2960 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2961 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002962 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2963 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2964 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002965 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002966 // 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.
2967 { "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 +00002968 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2969 { "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 +00002970 { "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 +00002971 { "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 +00002972 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2973 "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. "
2974 "This setting helps users control how much information gets loaded when loading modules from memory."
2975 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2976 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2977 "'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 +00002978 { "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 +00002979 { "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 +00002980 { "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." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002981 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2982};
Enrico Granata560558e2015-02-11 02:35:39 +00002983
Greg Clayton67cc0632012-08-22 17:17:09 +00002984enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002985{
Greg Clayton67cc0632012-08-22 17:17:09 +00002986 ePropertyDefaultArch,
2987 ePropertyExprPrefix,
2988 ePropertyPreferDynamic,
2989 ePropertyEnableSynthetic,
2990 ePropertySkipPrologue,
2991 ePropertySourceMap,
2992 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002993 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00002994 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00002995 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00002996 ePropertyMaxChildrenCount,
2997 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002998 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002999 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003000 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003001 ePropertyRunArgs,
3002 ePropertyEnvVars,
3003 ePropertyInheritEnv,
3004 ePropertyInputPath,
3005 ePropertyOutputPath,
3006 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003007 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003008 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003009 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003010 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003011 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003012 ePropertyUseHexImmediates,
3013 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003014 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003015 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003016 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003017 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003018 ePropertyTrapHandlerNames,
3019 ePropertyDisplayRuntimeSupportValues
Greg Clayton67cc0632012-08-22 17:17:09 +00003020};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003021
Caroline Ticedaccaa92010-09-20 20:44:43 +00003022
Greg Clayton67cc0632012-08-22 17:17:09 +00003023class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003024{
Greg Clayton67cc0632012-08-22 17:17:09 +00003025public:
3026 TargetOptionValueProperties (const ConstString &name) :
3027 OptionValueProperties (name),
3028 m_target (NULL),
3029 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003030 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003031 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003032
Greg Clayton67cc0632012-08-22 17:17:09 +00003033 // This constructor is used when creating TargetOptionValueProperties when it
3034 // is part of a new lldb_private::Target instance. It will copy all current
3035 // global property values as needed
3036 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3037 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3038 m_target (target),
3039 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003040 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003041 }
3042
3043 virtual const Property *
3044 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3045 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003046 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003047 // try and grab the setting from the current target if there is one. Else we just
3048 // use the one from this instance.
3049 if (idx == ePropertyEnvVars)
3050 GetHostEnvironmentIfNeeded ();
3051
3052 if (exe_ctx)
3053 {
3054 Target *target = exe_ctx->GetTargetPtr();
3055 if (target)
3056 {
3057 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3058 if (this != target_properties)
3059 return target_properties->ProtectedGetPropertyAtIndex (idx);
3060 }
3061 }
3062 return ProtectedGetPropertyAtIndex (idx);
3063 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003064
3065 lldb::TargetSP
3066 GetTargetSP ()
3067 {
3068 return m_target->shared_from_this();
3069 }
3070
Greg Clayton67cc0632012-08-22 17:17:09 +00003071protected:
3072
3073 void
3074 GetHostEnvironmentIfNeeded () const
3075 {
3076 if (!m_got_host_env)
3077 {
3078 if (m_target)
3079 {
3080 m_got_host_env = true;
3081 const uint32_t idx = ePropertyInheritEnv;
3082 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3083 {
3084 PlatformSP platform_sp (m_target->GetPlatform());
3085 if (platform_sp)
3086 {
3087 StringList env;
3088 if (platform_sp->GetEnvironment(env))
3089 {
3090 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3091 if (env_dict)
3092 {
3093 const bool can_replace = false;
3094 const size_t envc = env.GetSize();
3095 for (size_t idx=0; idx<envc; idx++)
3096 {
3097 const char *env_entry = env.GetStringAtIndex (idx);
3098 if (env_entry)
3099 {
3100 const char *equal_pos = ::strchr(env_entry, '=');
3101 ConstString key;
3102 // It is ok to have environment variables with no values
3103 const char *value = NULL;
3104 if (equal_pos)
3105 {
3106 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3107 if (equal_pos[1])
3108 value = equal_pos + 1;
3109 }
3110 else
3111 {
3112 key.SetCString(env_entry);
3113 }
3114 // Don't allow existing keys to be replaced with ones we get from the platform environment
3115 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3116 }
3117 }
3118 }
3119 }
3120 }
3121 }
3122 }
3123 }
3124 }
3125 Target *m_target;
3126 mutable bool m_got_host_env;
3127};
3128
Greg Claytonfbb76342013-11-20 21:07:01 +00003129//----------------------------------------------------------------------
3130// TargetProperties
3131//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003132TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003133 Properties (),
3134 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003135{
3136 if (target)
3137 {
3138 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003139
3140 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3141 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3142 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3143 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3144 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3145 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3146 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3147 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3148 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3149 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3150
3151 // Update m_launch_info once it was created
3152 Arg0ValueChangedCallback(this, NULL);
3153 RunArgsValueChangedCallback(this, NULL);
3154 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3155 InputPathValueChangedCallback(this, NULL);
3156 OutputPathValueChangedCallback(this, NULL);
3157 ErrorPathValueChangedCallback(this, NULL);
3158 DetachOnErrorValueChangedCallback(this, NULL);
3159 DisableASLRValueChangedCallback(this, NULL);
3160 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003161 }
3162 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003163 {
3164 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3165 m_collection_sp->Initialize(g_properties);
3166 m_collection_sp->AppendProperty(ConstString("process"),
3167 ConstString("Settings specify to processes."),
3168 true,
3169 Process::GetGlobalProperties()->GetValueProperties());
3170 }
Ilia K8f37ca52015-02-13 14:31:06 +00003171
Caroline Ticedaccaa92010-09-20 20:44:43 +00003172}
3173
Greg Clayton67cc0632012-08-22 17:17:09 +00003174TargetProperties::~TargetProperties ()
3175{
3176}
3177ArchSpec
3178TargetProperties::GetDefaultArchitecture () const
3179{
3180 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3181 if (value)
3182 return value->GetCurrentValue();
3183 return ArchSpec();
3184}
3185
3186void
3187TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3188{
3189 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3190 if (value)
3191 return value->SetCurrentValue(arch, true);
3192}
3193
3194lldb::DynamicValueType
3195TargetProperties::GetPreferDynamicValue() const
3196{
3197 const uint32_t idx = ePropertyPreferDynamic;
3198 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3199}
3200
3201bool
3202TargetProperties::GetDisableASLR () const
3203{
3204 const uint32_t idx = ePropertyDisableASLR;
3205 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3206}
3207
3208void
3209TargetProperties::SetDisableASLR (bool b)
3210{
3211 const uint32_t idx = ePropertyDisableASLR;
3212 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3213}
3214
3215bool
Jim Ingham106d0282014-06-25 02:32:56 +00003216TargetProperties::GetDetachOnError () const
3217{
3218 const uint32_t idx = ePropertyDetachOnError;
3219 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3220}
3221
3222void
3223TargetProperties::SetDetachOnError (bool b)
3224{
3225 const uint32_t idx = ePropertyDetachOnError;
3226 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3227}
3228
3229bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003230TargetProperties::GetDisableSTDIO () const
3231{
3232 const uint32_t idx = ePropertyDisableSTDIO;
3233 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3234}
3235
3236void
3237TargetProperties::SetDisableSTDIO (bool b)
3238{
3239 const uint32_t idx = ePropertyDisableSTDIO;
3240 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3241}
3242
Jim Ingham0f063ba2013-03-02 00:26:47 +00003243const char *
3244TargetProperties::GetDisassemblyFlavor () const
3245{
3246 const uint32_t idx = ePropertyDisassemblyFlavor;
3247 const char *return_value;
3248
3249 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3250 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3251 return return_value;
3252}
3253
Greg Clayton1f746072012-08-29 21:13:06 +00003254InlineStrategy
3255TargetProperties::GetInlineStrategy () const
3256{
3257 const uint32_t idx = ePropertyInlineStrategy;
3258 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3259}
3260
Greg Clayton45392552012-10-17 22:57:12 +00003261const char *
3262TargetProperties::GetArg0 () const
3263{
3264 const uint32_t idx = ePropertyArg0;
3265 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3266}
3267
3268void
3269TargetProperties::SetArg0 (const char *arg)
3270{
3271 const uint32_t idx = ePropertyArg0;
3272 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003273 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003274}
3275
Greg Clayton67cc0632012-08-22 17:17:09 +00003276bool
3277TargetProperties::GetRunArguments (Args &args) const
3278{
3279 const uint32_t idx = ePropertyRunArgs;
3280 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3281}
3282
3283void
3284TargetProperties::SetRunArguments (const Args &args)
3285{
3286 const uint32_t idx = ePropertyRunArgs;
3287 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003288 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003289}
3290
3291size_t
3292TargetProperties::GetEnvironmentAsArgs (Args &env) const
3293{
3294 const uint32_t idx = ePropertyEnvVars;
3295 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3296}
3297
Ilia K8f37ca52015-02-13 14:31:06 +00003298void
3299TargetProperties::SetEnvironmentFromArgs (const Args &env)
3300{
3301 const uint32_t idx = ePropertyEnvVars;
3302 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003303 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003304}
3305
Greg Clayton67cc0632012-08-22 17:17:09 +00003306bool
3307TargetProperties::GetSkipPrologue() const
3308{
3309 const uint32_t idx = ePropertySkipPrologue;
3310 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3311}
3312
3313PathMappingList &
3314TargetProperties::GetSourcePathMap () const
3315{
3316 const uint32_t idx = ePropertySourceMap;
3317 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3318 assert(option_value);
3319 return option_value->GetCurrentValue();
3320}
3321
3322FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003323TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003324{
3325 const uint32_t idx = ePropertyExecutableSearchPaths;
3326 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3327 assert(option_value);
3328 return option_value->GetCurrentValue();
3329}
3330
Michael Sartaina7499c92013-07-01 19:45:50 +00003331FileSpecList &
3332TargetProperties::GetDebugFileSearchPaths ()
3333{
3334 const uint32_t idx = ePropertyDebugFileSearchPaths;
3335 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3336 assert(option_value);
3337 return option_value->GetCurrentValue();
3338}
3339
Sean Callanan85054342015-04-03 15:39:47 +00003340FileSpecList &
3341TargetProperties::GetClangModuleSearchPaths ()
3342{
3343 const uint32_t idx = ePropertyClangModuleSearchPaths;
3344 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3345 assert(option_value);
3346 return option_value->GetCurrentValue();
3347}
3348
Greg Clayton67cc0632012-08-22 17:17:09 +00003349bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003350TargetProperties::GetEnableAutoImportClangModules() const
3351{
3352 const uint32_t idx = ePropertyAutoImportClangModules;
3353 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3354}
3355
3356bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003357TargetProperties::GetEnableSyntheticValue () const
3358{
3359 const uint32_t idx = ePropertyEnableSynthetic;
3360 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3361}
3362
3363uint32_t
3364TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3365{
3366 const uint32_t idx = ePropertyMaxChildrenCount;
3367 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3368}
3369
3370uint32_t
3371TargetProperties::GetMaximumSizeOfStringSummary() const
3372{
3373 const uint32_t idx = ePropertyMaxSummaryLength;
3374 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3375}
3376
Enrico Granatad325bf92013-06-04 22:54:16 +00003377uint32_t
3378TargetProperties::GetMaximumMemReadSize () const
3379{
3380 const uint32_t idx = ePropertyMaxMemReadSize;
3381 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3382}
3383
Greg Clayton67cc0632012-08-22 17:17:09 +00003384FileSpec
3385TargetProperties::GetStandardInputPath () const
3386{
3387 const uint32_t idx = ePropertyInputPath;
3388 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3389}
3390
3391void
3392TargetProperties::SetStandardInputPath (const char *p)
3393{
3394 const uint32_t idx = ePropertyInputPath;
3395 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3396}
3397
3398FileSpec
3399TargetProperties::GetStandardOutputPath () const
3400{
3401 const uint32_t idx = ePropertyOutputPath;
3402 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3403}
3404
3405void
3406TargetProperties::SetStandardOutputPath (const char *p)
3407{
3408 const uint32_t idx = ePropertyOutputPath;
3409 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3410}
3411
3412FileSpec
3413TargetProperties::GetStandardErrorPath () const
3414{
3415 const uint32_t idx = ePropertyErrorPath;
3416 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3417}
3418
Greg Clayton6920b522012-08-22 18:39:03 +00003419const char *
3420TargetProperties::GetExpressionPrefixContentsAsCString ()
3421{
3422 const uint32_t idx = ePropertyExprPrefix;
3423 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3424 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003425 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003426 const bool null_terminate = true;
3427 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003428 if (data_sp)
3429 return (const char *) data_sp->GetBytes();
3430 }
Greg Clayton6920b522012-08-22 18:39:03 +00003431 return NULL;
3432}
3433
Greg Clayton67cc0632012-08-22 17:17:09 +00003434void
3435TargetProperties::SetStandardErrorPath (const char *p)
3436{
3437 const uint32_t idx = ePropertyErrorPath;
3438 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3439}
3440
3441bool
3442TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3443{
3444 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3445 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3446}
3447
Jim Ingham17d023f2013-03-13 17:58:04 +00003448bool
Daniel Malead79ae052013-08-07 21:54:09 +00003449TargetProperties::GetUseHexImmediates () const
3450{
3451 const uint32_t idx = ePropertyUseHexImmediates;
3452 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3453}
3454
3455bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003456TargetProperties::GetUseFastStepping () const
3457{
3458 const uint32_t idx = ePropertyUseFastStepping;
3459 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3460}
3461
Greg Claytonfb6621e2013-12-06 21:59:52 +00003462bool
3463TargetProperties::GetDisplayExpressionsInCrashlogs () const
3464{
3465 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3466 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3467}
3468
Enrico Granata397ddd52013-05-21 20:13:34 +00003469LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003470TargetProperties::GetLoadScriptFromSymbolFile () const
3471{
3472 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003473 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003474}
3475
Daniel Malead79ae052013-08-07 21:54:09 +00003476Disassembler::HexImmediateStyle
3477TargetProperties::GetHexImmediateStyle () const
3478{
3479 const uint32_t idx = ePropertyHexImmediateStyle;
3480 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3481}
3482
Greg Claytonfd814c52013-08-13 01:42:25 +00003483MemoryModuleLoadLevel
3484TargetProperties::GetMemoryModuleLoadLevel() const
3485{
3486 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3487 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3488}
3489
Jason Molendaa4bea722014-02-14 05:06:49 +00003490bool
3491TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3492{
3493 const uint32_t idx = ePropertyTrapHandlerNames;
3494 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3495}
Greg Claytonfd814c52013-08-13 01:42:25 +00003496
Jason Molendaa4bea722014-02-14 05:06:49 +00003497void
3498TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3499{
3500 const uint32_t idx = ePropertyTrapHandlerNames;
3501 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3502}
Greg Clayton67cc0632012-08-22 17:17:09 +00003503
Enrico Granata560558e2015-02-11 02:35:39 +00003504bool
3505TargetProperties::GetDisplayRuntimeSupportValues () const
3506{
3507 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3508 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3509}
3510
3511void
3512TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3513{
3514 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3515 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3516}
3517
Ilia K8f37ca52015-02-13 14:31:06 +00003518const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003519TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003520{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003521 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003522 return m_launch_info;
3523}
3524
3525void
3526TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3527{
3528 m_launch_info = launch_info;
3529 SetArg0(launch_info.GetArg0());
3530 SetRunArguments(launch_info.GetArguments());
3531 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3532 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3533 if (input_file_action)
3534 {
3535 const char *input_path = input_file_action->GetPath();
3536 if (input_path)
3537 SetStandardInputPath(input_path);
3538 }
3539 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3540 if (output_file_action)
3541 {
3542 const char *output_path = output_file_action->GetPath();
3543 if (output_path)
3544 SetStandardOutputPath(output_path);
3545 }
3546 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3547 if (error_file_action)
3548 {
3549 const char *error_path = error_file_action->GetPath();
3550 if (error_path)
3551 SetStandardErrorPath(error_path);
3552 }
3553 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3554 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3555 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3556}
3557
3558void
3559TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3560{
3561 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3562 this_->m_launch_info.SetArg0(this_->GetArg0());
3563}
3564
3565void
3566TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3567{
3568 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3569 Args args;
3570 if (this_->GetRunArguments(args))
3571 this_->m_launch_info.GetArguments() = args;
3572}
3573
3574void
3575TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3576{
3577 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3578 Args args;
3579 if (this_->GetEnvironmentAsArgs(args))
3580 this_->m_launch_info.GetEnvironmentEntries() = args;
3581}
3582
3583void
3584TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3585{
3586 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3587 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath().GetPath().c_str(), true, false);
3588}
3589
3590void
3591TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3592{
3593 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3594 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath().GetPath().c_str(), false, true);
3595}
3596
3597void
3598TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3599{
3600 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3601 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath().GetPath().c_str(), false, true);
3602}
3603
3604void
3605TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
3606{
3607 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3608 if (this_->GetDetachOnError())
3609 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3610 else
3611 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3612}
3613
3614void
3615TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
3616{
3617 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3618 if (this_->GetDisableASLR())
3619 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3620 else
3621 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3622}
3623
3624void
3625TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
3626{
3627 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3628 if (this_->GetDisableSTDIO())
3629 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
3630 else
3631 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
3632}
Ilia Keb2c19a2015-03-10 21:59:55 +00003633
3634//----------------------------------------------------------------------
3635// Target::TargetEventData
3636//----------------------------------------------------------------------
3637
3638Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
3639 EventData (),
3640 m_target_sp (target_sp),
3641 m_module_list ()
3642{
3643}
3644
3645Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
3646 EventData (),
3647 m_target_sp (target_sp),
3648 m_module_list (module_list)
3649{
3650}
3651
3652Target::TargetEventData::~TargetEventData()
3653{
3654}
3655
3656const ConstString &
3657Target::TargetEventData::GetFlavorString ()
3658{
3659 static ConstString g_flavor ("Target::TargetEventData");
3660 return g_flavor;
3661}
3662
3663void
3664Target::TargetEventData::Dump (Stream *s) const
3665{
3666}
3667
3668const Target::TargetEventData *
3669Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3670{
3671 if (event_ptr)
3672 {
3673 const EventData *event_data = event_ptr->GetData();
3674 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3675 return static_cast <const TargetEventData *> (event_ptr->GetData());
3676 }
3677 return NULL;
3678}
3679
3680TargetSP
3681Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
3682{
3683 TargetSP target_sp;
3684 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
3685 if (event_data)
3686 target_sp = event_data->m_target_sp;
3687 return target_sp;
3688}
3689
3690ModuleList
3691Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
3692{
3693 ModuleList module_list;
3694 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
3695 if (event_data)
3696 module_list = event_data->m_module_list;
3697 return module_list;
3698}