blob: 8a7996ae96577988c07f0d0adaacc44a2ba873d2 [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())
192 m_process_sp->Destroy();
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
526Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
527{
528 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
529}
530
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000532Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533{
534 BreakpointSP bp_sp;
535 if (filter_sp && resolver_sp)
536 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000537 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000539 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000541 return bp_sp;
542}
543
544void
545Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
546{
547 if (!bp_sp)
548 return;
549 if (internal)
550 m_internal_breakpoint_list.Add (bp_sp, false);
551 else
552 m_breakpoint_list.Add (bp_sp, true);
553
554 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
555 if (log)
556 {
557 StreamString s;
558 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
559 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
560 }
561
562 bp_sp->ResolveBreakpoint();
563
564 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000565 {
566 m_last_created_breakpoint = bp_sp;
567 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568}
569
Johnny Chen86364b42011-09-20 23:28:55 +0000570bool
571Target::ProcessIsValid()
572{
573 return (m_process_sp && m_process_sp->IsAlive());
574}
575
Johnny Chenb90827e2012-06-04 23:19:54 +0000576static bool
577CheckIfWatchpointsExhausted(Target *target, Error &error)
578{
579 uint32_t num_supported_hardware_watchpoints;
580 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
581 if (rc.Success())
582 {
583 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
584 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
585 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
586 num_supported_hardware_watchpoints);
587 }
588 return false;
589}
590
Johnny Chen01a67862011-10-14 00:42:25 +0000591// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000592// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000593WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000594Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000595{
Greg Clayton5160ce52013-03-27 23:08:40 +0000596 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000597 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000598 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000599 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000600
Johnny Chen01a67862011-10-14 00:42:25 +0000601 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000602 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000603 {
604 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000605 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000606 }
Jim Inghamc6462312013-06-18 21:52:48 +0000607
Johnny Chen45e541f2011-09-14 22:20:15 +0000608 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000609 {
610 if (size == 0)
611 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
612 else
Daniel Malead01b2952012-11-29 21:49:15 +0000613 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000614 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000615 }
Jim Inghamc6462312013-06-18 21:52:48 +0000616
617 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
618 {
619 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
620 }
Johnny Chen7313a642011-09-13 01:15:36 +0000621
Johnny Chen01a67862011-10-14 00:42:25 +0000622 // Currently we only support one watchpoint per address, with total number
623 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000624
625 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000626 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 +0000627 Mutex::Locker locker;
628 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000629 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000630 if (matched_sp)
631 {
Johnny Chen0c406372011-09-14 20:23:45 +0000632 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000633 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000634 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
635 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000636 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000637 if (size == old_size && kind == old_type)
638 {
Johnny Chen01a67862011-10-14 00:42:25 +0000639 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000640 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000641 }
642 else
643 {
Johnny Chen01a67862011-10-14 00:42:25 +0000644 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000645 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
646 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000647 }
Johnny Chen3c532582011-09-13 23:29:31 +0000648 }
649
Jason Molenda727e3922012-12-05 23:07:34 +0000650 if (!wp_sp)
651 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000652 wp_sp.reset(new Watchpoint(*this, addr, size, type));
653 wp_sp->SetWatchpointType(kind, notify);
654 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000655 }
Johnny Chen0c406372011-09-14 20:23:45 +0000656
Jim Ingham1b5792e2012-12-18 02:03:49 +0000657 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000658 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000659 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
660 __FUNCTION__,
661 error.Success() ? "succeeded" : "failed",
662 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000663
Jason Molenda727e3922012-12-05 23:07:34 +0000664 if (error.Fail())
665 {
Johnny Chen41b77262012-03-26 22:00:10 +0000666 // Enabling the watchpoint on the device side failed.
667 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000668 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000669 // See if we could provide more helpful error message.
670 if (!CheckIfWatchpointsExhausted(this, error))
671 {
672 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000673 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000674 }
Johnny Chen01a67862011-10-14 00:42:25 +0000675 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000676 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000677 else
Johnny Chen01a67862011-10-14 00:42:25 +0000678 m_last_created_watchpoint = wp_sp;
679 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000680}
681
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682void
683Target::RemoveAllBreakpoints (bool internal_also)
684{
Greg Clayton5160ce52013-03-27 23:08:40 +0000685 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686 if (log)
687 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
688
Greg Clayton9fed0d82010-07-23 23:33:17 +0000689 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000691 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000692
693 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694}
695
696void
697Target::DisableAllBreakpoints (bool internal_also)
698{
Greg Clayton5160ce52013-03-27 23:08:40 +0000699 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700 if (log)
701 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
702
703 m_breakpoint_list.SetEnabledAll (false);
704 if (internal_also)
705 m_internal_breakpoint_list.SetEnabledAll (false);
706}
707
708void
709Target::EnableAllBreakpoints (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 (true);
716 if (internal_also)
717 m_internal_breakpoint_list.SetEnabledAll (true);
718}
719
720bool
721Target::RemoveBreakpointByID (break_id_t break_id)
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 (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
726
727 if (DisableBreakpointByID (break_id))
728 {
729 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000730 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000732 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000733 if (m_last_created_breakpoint)
734 {
735 if (m_last_created_breakpoint->GetID() == break_id)
736 m_last_created_breakpoint.reset();
737 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000738 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000739 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740 return true;
741 }
742 return false;
743}
744
745bool
746Target::DisableBreakpointByID (break_id_t break_id)
747{
Greg Clayton5160ce52013-03-27 23:08:40 +0000748 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749 if (log)
750 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
751
752 BreakpointSP bp_sp;
753
754 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
755 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
756 else
757 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
758 if (bp_sp)
759 {
760 bp_sp->SetEnabled (false);
761 return true;
762 }
763 return false;
764}
765
766bool
767Target::EnableBreakpointByID (break_id_t break_id)
768{
Greg Clayton5160ce52013-03-27 23:08:40 +0000769 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 if (log)
771 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
772 __FUNCTION__,
773 break_id,
774 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
775
776 BreakpointSP bp_sp;
777
778 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
779 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
780 else
781 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
782
783 if (bp_sp)
784 {
785 bp_sp->SetEnabled (true);
786 return true;
787 }
788 return false;
789}
790
Johnny Chenedf50372011-09-23 21:21:43 +0000791// The flag 'end_to_end', default to true, signifies that the operation is
792// performed end to end, for both the debugger and the debuggee.
793
Johnny Chen01a67862011-10-14 00:42:25 +0000794// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
795// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000796bool
Johnny Chen01a67862011-10-14 00:42:25 +0000797Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000798{
Greg Clayton5160ce52013-03-27 23:08:40 +0000799 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000800 if (log)
801 log->Printf ("Target::%s\n", __FUNCTION__);
802
Johnny Chenedf50372011-09-23 21:21:43 +0000803 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000804 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000805 return true;
806 }
807
808 // Otherwise, it's an end to end operation.
809
Johnny Chen86364b42011-09-20 23:28:55 +0000810 if (!ProcessIsValid())
811 return false;
812
Johnny Chen01a67862011-10-14 00:42:25 +0000813 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000814 for (size_t i = 0; i < num_watchpoints; ++i)
815 {
Johnny Chen01a67862011-10-14 00:42:25 +0000816 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
817 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000818 return false;
819
Johnny Chen01a67862011-10-14 00:42:25 +0000820 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000821 if (rc.Fail())
822 return false;
823 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000824 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000825 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000826 return true; // Success!
827}
828
Johnny Chen01a67862011-10-14 00:42:25 +0000829// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
830// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000831bool
Johnny Chen01a67862011-10-14 00:42:25 +0000832Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000833{
Greg Clayton5160ce52013-03-27 23:08:40 +0000834 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000835 if (log)
836 log->Printf ("Target::%s\n", __FUNCTION__);
837
Johnny Chenedf50372011-09-23 21:21:43 +0000838 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000839 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000840 return true;
841 }
842
843 // Otherwise, it's an end to end operation.
844
Johnny Chen86364b42011-09-20 23:28:55 +0000845 if (!ProcessIsValid())
846 return false;
847
Johnny Chen01a67862011-10-14 00:42:25 +0000848 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000849 for (size_t i = 0; i < num_watchpoints; ++i)
850 {
Johnny Chen01a67862011-10-14 00:42:25 +0000851 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
852 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000853 return false;
854
Johnny Chen01a67862011-10-14 00:42:25 +0000855 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000856 if (rc.Fail())
857 return false;
858 }
Johnny Chen86364b42011-09-20 23:28:55 +0000859 return true; // Success!
860}
861
Johnny Chen01a67862011-10-14 00:42:25 +0000862// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
863// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000864bool
Johnny Chen01a67862011-10-14 00:42:25 +0000865Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000866{
Greg Clayton5160ce52013-03-27 23:08:40 +0000867 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000868 if (log)
869 log->Printf ("Target::%s\n", __FUNCTION__);
870
Johnny Chenedf50372011-09-23 21:21:43 +0000871 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000872 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000873 return true;
874 }
875
876 // Otherwise, it's an end to end operation.
877
Johnny Chen86364b42011-09-20 23:28:55 +0000878 if (!ProcessIsValid())
879 return false;
880
Johnny Chen01a67862011-10-14 00:42:25 +0000881 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000882 for (size_t i = 0; i < num_watchpoints; ++i)
883 {
Johnny Chen01a67862011-10-14 00:42:25 +0000884 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
885 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000886 return false;
887
Johnny Chen01a67862011-10-14 00:42:25 +0000888 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000889 if (rc.Fail())
890 return false;
891 }
Johnny Chen86364b42011-09-20 23:28:55 +0000892 return true; // Success!
893}
894
Johnny Chena4d6bc92012-02-25 06:44:30 +0000895// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
896bool
897Target::ClearAllWatchpointHitCounts ()
898{
Greg Clayton5160ce52013-03-27 23:08:40 +0000899 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000900 if (log)
901 log->Printf ("Target::%s\n", __FUNCTION__);
902
903 size_t num_watchpoints = m_watchpoint_list.GetSize();
904 for (size_t i = 0; i < num_watchpoints; ++i)
905 {
906 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
907 if (!wp_sp)
908 return false;
909
910 wp_sp->ResetHitCount();
911 }
912 return true; // Success!
913}
914
Enrico Granata5e3fe042015-02-11 00:37:54 +0000915// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
916bool
917Target::ClearAllWatchpointHistoricValues ()
918{
919 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
920 if (log)
921 log->Printf ("Target::%s\n", __FUNCTION__);
922
923 size_t num_watchpoints = m_watchpoint_list.GetSize();
924 for (size_t i = 0; i < num_watchpoints; ++i)
925 {
926 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
927 if (!wp_sp)
928 return false;
929
930 wp_sp->ResetHistoricValues();
931 }
932 return true; // Success!
933}
934
Johnny Chen01a67862011-10-14 00:42:25 +0000935// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000936// during these operations.
937bool
Johnny Chen01a67862011-10-14 00:42:25 +0000938Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000939{
Greg Clayton5160ce52013-03-27 23:08:40 +0000940 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000941 if (log)
942 log->Printf ("Target::%s\n", __FUNCTION__);
943
944 if (!ProcessIsValid())
945 return false;
946
Johnny Chen01a67862011-10-14 00:42:25 +0000947 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000948 for (size_t i = 0; i < num_watchpoints; ++i)
949 {
Johnny Chen01a67862011-10-14 00:42:25 +0000950 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
951 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000952 return false;
953
Johnny Chen01a67862011-10-14 00:42:25 +0000954 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000955 }
956 return true; // Success!
957}
958
Johnny Chen01a67862011-10-14 00:42:25 +0000959// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000960bool
Johnny Chen01a67862011-10-14 00:42:25 +0000961Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000962{
Greg Clayton5160ce52013-03-27 23:08:40 +0000963 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000964 if (log)
965 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
966
967 if (!ProcessIsValid())
968 return false;
969
Johnny Chen01a67862011-10-14 00:42:25 +0000970 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
971 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000972 {
Johnny Chen01a67862011-10-14 00:42:25 +0000973 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000974 if (rc.Success())
975 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000976
Johnny Chenf04ee932011-09-22 18:04:58 +0000977 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000978 }
979 return false;
980}
981
Johnny Chen01a67862011-10-14 00:42:25 +0000982// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000983bool
Johnny Chen01a67862011-10-14 00:42:25 +0000984Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000985{
Greg Clayton5160ce52013-03-27 23:08:40 +0000986 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000987 if (log)
988 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
989
990 if (!ProcessIsValid())
991 return false;
992
Johnny Chen01a67862011-10-14 00:42:25 +0000993 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
994 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000995 {
Johnny Chen01a67862011-10-14 00:42:25 +0000996 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000997 if (rc.Success())
998 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000999
Johnny Chenf04ee932011-09-22 18:04:58 +00001000 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001001 }
1002 return false;
1003}
1004
Johnny Chen01a67862011-10-14 00:42:25 +00001005// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001006bool
Johnny Chen01a67862011-10-14 00:42:25 +00001007Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001008{
Greg Clayton5160ce52013-03-27 23:08:40 +00001009 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001010 if (log)
1011 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1012
Jim Inghamb0b45132013-07-02 02:09:46 +00001013 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1014 if (watch_to_remove_sp == m_last_created_watchpoint)
1015 m_last_created_watchpoint.reset();
1016
Johnny Chen01a67862011-10-14 00:42:25 +00001017 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001018 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001019 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001020 return true;
1021 }
1022 return false;
1023}
1024
Johnny Chen01a67862011-10-14 00:42:25 +00001025// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001026bool
Johnny Chen01a67862011-10-14 00:42:25 +00001027Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001028{
Greg Clayton5160ce52013-03-27 23:08:40 +00001029 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001030 if (log)
1031 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1032
1033 if (!ProcessIsValid())
1034 return false;
1035
Johnny Chen01a67862011-10-14 00:42:25 +00001036 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1037 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001038 {
Johnny Chen01a67862011-10-14 00:42:25 +00001039 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001040 return true;
1041 }
1042 return false;
1043}
1044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001045ModuleSP
1046Target::GetExecutableModule ()
1047{
Greg Claytonaa149cb2011-08-11 02:48:45 +00001048 return m_images.GetModuleAtIndex(0);
1049}
1050
1051Module*
1052Target::GetExecutableModulePointer ()
1053{
1054 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001055}
1056
Enrico Granata17598482012-11-08 02:22:02 +00001057static void
1058LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1059{
1060 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001061 StreamString feedback_stream;
1062 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001063 {
Enrico Granata97303392013-05-21 00:00:30 +00001064 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001065 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001066 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1067 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001068 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001069 if (feedback_stream.GetSize())
1070 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1071 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001072}
1073
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001074void
Greg Claytonb35db632013-11-09 00:03:31 +00001075Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001076{
Greg Claytonb35db632013-11-09 00:03:31 +00001077 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001078 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 m_images.Clear();
1080 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001081 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001082 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001083}
1084
1085void
Greg Claytonb35db632013-11-09 00:03:31 +00001086Target::DidExec ()
1087{
1088 // When a process exec's we need to know about it so we can do some cleanup.
1089 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1090 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1091}
1092
1093void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001094Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1095{
1096 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001097 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001098
1099 if (executable_sp.get())
1100 {
1101 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001102 "Target::SetExecutableModule (executable = '%s')",
1103 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001105 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106
Jim Ingham5aee1622010-08-09 23:31:02 +00001107 // 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 +00001108 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001109 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001110 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001111 if (log)
1112 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1113 }
1114
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001115 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001116 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001118 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119 {
1120 executable_objfile->GetDependentModules(dependent_files);
1121 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1122 {
Greg Claytonded470d2011-03-19 01:12:21 +00001123 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1124 FileSpec platform_dependent_file_spec;
1125 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001126 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001127 else
1128 platform_dependent_file_spec = dependent_file_spec;
1129
Greg Claytonb9a01b32012-02-26 05:51:37 +00001130 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1131 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132 if (image_module_sp.get())
1133 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134 ObjectFile *objfile = image_module_sp->GetObjectFile();
1135 if (objfile)
1136 objfile->GetDependentModules(dependent_files);
1137 }
1138 }
1139 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001140 }
1141}
1142
1143
Jim Ingham5aee1622010-08-09 23:31:02 +00001144bool
1145Target::SetArchitecture (const ArchSpec &arch_spec)
1146{
Greg Clayton5160ce52013-03-27 23:08:40 +00001147 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001148 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001149 {
Greg Clayton70512312012-05-08 01:45:38 +00001150 // If we haven't got a valid arch spec, or the architectures are
1151 // compatible, so just update the architecture. Architectures can be
1152 // equal, yet the triple OS and vendor might change, so we need to do
1153 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001154 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001155 if (log)
1156 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 +00001157 return true;
1158 }
1159 else
1160 {
1161 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001162 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001163 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 +00001164 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001165 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001166
Greg Claytonb35db632013-11-09 00:03:31 +00001167 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001168 // Need to do something about unsetting breakpoints.
1169
1170 if (executable_sp)
1171 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001172 if (log)
1173 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 +00001174 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1175 Error error = ModuleList::GetSharedModule (module_spec,
1176 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001177 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001178 NULL,
1179 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001180
1181 if (!error.Fail() && executable_sp)
1182 {
1183 SetExecutableModule (executable_sp, true);
1184 return true;
1185 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001186 }
1187 }
Greg Clayton70512312012-05-08 01:45:38 +00001188 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001189}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001191bool
1192Target::MergeArchitecture (const ArchSpec &arch_spec)
1193{
1194 if (arch_spec.IsValid())
1195 {
1196 if (m_arch.IsCompatibleMatch(arch_spec))
1197 {
1198 // The current target arch is compatible with "arch_spec", see if we
1199 // can improve our current architecture using bits from "arch_spec"
1200
1201 // Merge bits from arch_spec into "merged_arch" and set our architecture
1202 ArchSpec merged_arch (m_arch);
1203 merged_arch.MergeFrom (arch_spec);
1204 return SetArchitecture(merged_arch);
1205 }
1206 else
1207 {
1208 // The new architecture is different, we just need to replace it
1209 return SetArchitecture(arch_spec);
1210 }
1211 }
1212 return false;
1213}
1214
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215void
Enrico Granataefe637d2012-11-08 19:16:03 +00001216Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001217{
1218}
1219
1220void
Enrico Granataefe637d2012-11-08 19:16:03 +00001221Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222{
1223 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001224 if (m_valid)
1225 {
1226 ModuleList my_module_list;
1227 my_module_list.Append(module_sp);
1228 LoadScriptingResourceForModule(module_sp, this);
1229 ModulesDidLoad (my_module_list);
1230 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231}
1232
1233void
Enrico Granataefe637d2012-11-08 19:16:03 +00001234Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001235{
1236 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001237 if (m_valid)
1238 {
1239 ModuleList my_module_list;
1240 my_module_list.Append(module_sp);
1241 ModulesDidUnload (my_module_list, false);
1242 }
Enrico Granata17598482012-11-08 02:22:02 +00001243}
1244
1245void
Enrico Granataefe637d2012-11-08 19:16:03 +00001246Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247{
Jim Inghame716ae02011-08-03 01:00:06 +00001248 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001249 if (m_valid)
1250 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251}
1252
1253void
1254Target::ModulesDidLoad (ModuleList &module_list)
1255{
Greg Clayton23f8c952014-03-24 23:10:19 +00001256 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001257 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001258 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001259 if (m_process_sp)
1260 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001261 m_process_sp->ModulesDidLoad (module_list);
Stephane Sezer5a1774d2015-03-23 18:36:54 +00001262
1263 // This assumes there can only be one libobjc loaded.
1264 ObjCLanguageRuntime *objc_runtime = m_process_sp->GetObjCLanguageRuntime ();
1265 if (objc_runtime && !objc_runtime->HasReadObjCLibrary ())
1266 {
1267 Mutex::Locker locker (module_list.GetMutex ());
1268
1269 size_t num_modules = module_list.GetSize();
1270 for (size_t i = 0; i < num_modules; i++)
1271 {
1272 auto mod = module_list.GetModuleAtIndex (i);
1273 if (objc_runtime->IsModuleObjCLibrary (mod))
1274 {
1275 objc_runtime->ReadObjCLibrary (mod);
1276 break;
1277 }
1278 }
1279 }
Jason Molendaeef51062013-11-05 03:57:19 +00001280 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001281 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001282 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001283}
1284
1285void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001286Target::SymbolsDidLoad (ModuleList &module_list)
1287{
Greg Clayton23f8c952014-03-24 23:10:19 +00001288 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001289 {
Jim Ingham31caf982013-06-04 23:01:35 +00001290 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001291 {
Jim Ingham31caf982013-06-04 23:01:35 +00001292 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1293 if (runtime)
1294 {
1295 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1296 objc_runtime->SymbolsDidLoad(module_list);
1297 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001298 }
Jim Ingham31caf982013-06-04 23:01:35 +00001299
Greg Clayton095eeaa2013-11-05 23:28:00 +00001300 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001301 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001302 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001303}
1304
1305void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001306Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001307{
Greg Clayton23f8c952014-03-24 23:10:19 +00001308 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001309 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001310 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001311 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001312 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001313 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314}
1315
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001316bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001317Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001318{
Greg Clayton67cc0632012-08-22 17:17:09 +00001319 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001320 {
1321 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001322 ModuleSpec module_spec (module_file_spec);
1323 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001324
1325 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1326 // black list.
1327 if (num_modules > 0)
1328 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001329 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001330 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001331 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001332 return false;
1333 }
1334 return true;
1335 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001336 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001337 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001338}
1339
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001340bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001341Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001342{
Greg Clayton67cc0632012-08-22 17:17:09 +00001343 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001344 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001345 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001346 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001347 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001348 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001349}
1350
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351size_t
Greg Claytondb598232011-01-07 01:57:07 +00001352Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1353{
Greg Claytone72dfb32012-02-24 01:59:29 +00001354 SectionSP section_sp (addr.GetSection());
1355 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001356 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001357 // 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 +00001358 if (section_sp->IsEncrypted())
1359 {
Greg Clayton57f06302012-05-25 17:05:55 +00001360 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001361 return 0;
1362 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001363 ModuleSP module_sp (section_sp->GetModule());
1364 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001365 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001366 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1367 if (objfile)
1368 {
1369 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1370 addr.GetOffset(),
1371 dst,
1372 dst_len);
1373 if (bytes_read > 0)
1374 return bytes_read;
1375 else
1376 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1377 }
Greg Claytondb598232011-01-07 01:57:07 +00001378 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001379 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001380 }
1381 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001382 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001383 }
1384 else
Greg Claytondb598232011-01-07 01:57:07 +00001385 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001386
Greg Claytondb598232011-01-07 01:57:07 +00001387 return 0;
1388}
1389
1390size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001391Target::ReadMemory (const Address& addr,
1392 bool prefer_file_cache,
1393 void *dst,
1394 size_t dst_len,
1395 Error &error,
1396 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001398 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001399
Enrico Granata9128ee22011-09-06 19:20:51 +00001400 // if we end up reading this from process memory, we will fill this
1401 // with the actual load address
1402 if (load_addr_ptr)
1403 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1404
Greg Claytondb598232011-01-07 01:57:07 +00001405 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001406
1407 addr_t load_addr = LLDB_INVALID_ADDRESS;
1408 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001409 Address resolved_addr;
1410 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001411 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001412 SectionLoadList &section_load_list = GetSectionLoadList();
1413 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001414 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001415 // No sections are loaded, so we must assume we are not running
1416 // yet and anything we are given is a file address.
1417 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1418 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001419 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001420 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001421 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001422 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001423 // we have manually loaded some sections with "target modules load ..."
1424 // or because we have have a live process that has sections loaded
1425 // through the dynamic loader
1426 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001427 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001428 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001429 }
Greg Clayton357132e2011-03-26 19:14:58 +00001430 if (!resolved_addr.IsValid())
1431 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001432
Greg Claytonc749eb82011-07-11 05:12:02 +00001433
Greg Claytondb598232011-01-07 01:57:07 +00001434 if (prefer_file_cache)
1435 {
1436 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1437 if (bytes_read > 0)
1438 return bytes_read;
1439 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001440
Johnny Chen86364b42011-09-20 23:28:55 +00001441 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001442 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001443 if (load_addr == LLDB_INVALID_ADDRESS)
1444 load_addr = resolved_addr.GetLoadAddress (this);
1445
Greg Claytondda4f7b2010-06-30 23:03:03 +00001446 if (load_addr == LLDB_INVALID_ADDRESS)
1447 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001448 ModuleSP addr_module_sp (resolved_addr.GetModule());
1449 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001450 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001451 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001452 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001453 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001454 else
Daniel Malead01b2952012-11-29 21:49:15 +00001455 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001456 }
1457 else
1458 {
Greg Claytondb598232011-01-07 01:57:07 +00001459 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460 if (bytes_read != dst_len)
1461 {
1462 if (error.Success())
1463 {
1464 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001465 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466 else
Daniel Malead01b2952012-11-29 21:49:15 +00001467 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 +00001468 }
1469 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001470 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001471 {
1472 if (load_addr_ptr)
1473 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001474 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001475 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001476 // If the address is not section offset we have an address that
1477 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001478 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001479 // more we can do. If it is section offset, we might be able to
1480 // read cached memory from the object file.
1481 if (!resolved_addr.IsSectionOffset())
1482 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001483 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001485
Greg Claytonc749eb82011-07-11 05:12:02 +00001486 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001487 {
Greg Claytondb598232011-01-07 01:57:07 +00001488 // If we didn't already try and read from the object file cache, then
1489 // try it after failing to read from the process.
1490 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001491 }
1492 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001493}
1494
Greg Claytond16e1e52011-07-12 17:06:17 +00001495size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001496Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1497{
1498 char buf[256];
1499 out_str.clear();
1500 addr_t curr_addr = addr.GetLoadAddress(this);
1501 Address address(addr);
1502 while (1)
1503 {
1504 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1505 if (length == 0)
1506 break;
1507 out_str.append(buf, length);
1508 // If we got "length - 1" bytes, we didn't get the whole C string, we
1509 // need to read some more characters
1510 if (length == sizeof(buf) - 1)
1511 curr_addr += length;
1512 else
1513 break;
1514 address = Address(curr_addr);
1515 }
1516 return out_str.size();
1517}
1518
1519
1520size_t
1521Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1522{
1523 size_t total_cstr_len = 0;
1524 if (dst && dst_max_len)
1525 {
1526 result_error.Clear();
1527 // NULL out everything just to be safe
1528 memset (dst, 0, dst_max_len);
1529 Error error;
1530 addr_t curr_addr = addr.GetLoadAddress(this);
1531 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001532
1533 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1534 // think this really needs to be tied to the memory cache subsystem's
1535 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001536 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001537
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001538 size_t bytes_left = dst_max_len - 1;
1539 char *curr_dst = dst;
1540
1541 while (bytes_left > 0)
1542 {
1543 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1544 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1545 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1546
1547 if (bytes_read == 0)
1548 {
1549 result_error = error;
1550 dst[total_cstr_len] = '\0';
1551 break;
1552 }
1553 const size_t len = strlen(curr_dst);
1554
1555 total_cstr_len += len;
1556
1557 if (len < bytes_to_read)
1558 break;
1559
1560 curr_dst += bytes_read;
1561 curr_addr += bytes_read;
1562 bytes_left -= bytes_read;
1563 address = Address(curr_addr);
1564 }
1565 }
1566 else
1567 {
1568 if (dst == NULL)
1569 result_error.SetErrorString("invalid arguments");
1570 else
1571 result_error.Clear();
1572 }
1573 return total_cstr_len;
1574}
1575
1576size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001577Target::ReadScalarIntegerFromMemory (const Address& addr,
1578 bool prefer_file_cache,
1579 uint32_t byte_size,
1580 bool is_signed,
1581 Scalar &scalar,
1582 Error &error)
1583{
1584 uint64_t uval;
1585
1586 if (byte_size <= sizeof(uval))
1587 {
1588 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1589 if (bytes_read == byte_size)
1590 {
1591 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001592 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001593 if (byte_size <= 4)
1594 scalar = data.GetMaxU32 (&offset, byte_size);
1595 else
1596 scalar = data.GetMaxU64 (&offset, byte_size);
1597
1598 if (is_signed)
1599 scalar.SignExtend(byte_size * 8);
1600 return bytes_read;
1601 }
1602 }
1603 else
1604 {
1605 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1606 }
1607 return 0;
1608}
1609
1610uint64_t
1611Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1612 bool prefer_file_cache,
1613 size_t integer_byte_size,
1614 uint64_t fail_value,
1615 Error &error)
1616{
1617 Scalar scalar;
1618 if (ReadScalarIntegerFromMemory (addr,
1619 prefer_file_cache,
1620 integer_byte_size,
1621 false,
1622 scalar,
1623 error))
1624 return scalar.ULongLong(fail_value);
1625 return fail_value;
1626}
1627
1628bool
1629Target::ReadPointerFromMemory (const Address& addr,
1630 bool prefer_file_cache,
1631 Error &error,
1632 Address &pointer_addr)
1633{
1634 Scalar scalar;
1635 if (ReadScalarIntegerFromMemory (addr,
1636 prefer_file_cache,
1637 m_arch.GetAddressByteSize(),
1638 false,
1639 scalar,
1640 error))
1641 {
1642 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1643 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1644 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001645 SectionLoadList &section_load_list = GetSectionLoadList();
1646 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001647 {
1648 // No sections are loaded, so we must assume we are not running
1649 // yet and anything we are given is a file address.
1650 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1651 }
1652 else
1653 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001654 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001655 // we have manually loaded some sections with "target modules load ..."
1656 // or because we have have a live process that has sections loaded
1657 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001658 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001659 }
1660 // We weren't able to resolve the pointer value, so just return
1661 // an address with no section
1662 if (!pointer_addr.IsValid())
1663 pointer_addr.SetOffset (pointer_vm_addr);
1664 return true;
1665
1666 }
1667 }
1668 return false;
1669}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001670
1671ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001672Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001673{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001674 ModuleSP module_sp;
1675
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001676 Error error;
1677
Jim Ingham4a94c912012-05-17 18:38:42 +00001678 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1679 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001680
Jim Ingham4a94c912012-05-17 18:38:42 +00001681 if (module_spec.GetUUID().IsValid())
1682 module_sp = m_images.FindFirstModule(module_spec);
1683
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001684 if (!module_sp)
1685 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001686 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1687 bool did_create_module = false;
1688
1689 // If there are image search path entries, try to use them first to acquire a suitable image.
1690 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001691 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001692 ModuleSpec transformed_spec (module_spec);
1693 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1694 {
1695 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1696 error = ModuleList::GetSharedModule (transformed_spec,
1697 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001698 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001699 &old_module_sp,
1700 &did_create_module);
1701 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001702 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001703
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001704 if (!module_sp)
1705 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001706 // If we have a UUID, we can check our global shared module list in case
1707 // we already have it. If we don't have a valid UUID, then we can't since
1708 // the path in "module_spec" will be a platform path, and we will need to
1709 // let the platform find that file. For example, we could be asking for
1710 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1711 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1712 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1713 // cache.
1714 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001715 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001716 // We have a UUID, it is OK to check the global module list...
1717 error = ModuleList::GetSharedModule (module_spec,
1718 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001719 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001720 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001721 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001722 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001723
1724 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001725 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001726 // The platform is responsible for finding and caching an appropriate
1727 // module in the shared module cache.
1728 if (m_platform_sp)
1729 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001730 error = m_platform_sp->GetSharedModule (module_spec,
1731 m_process_sp.get(),
1732 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001733 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001734 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001735 &did_create_module);
1736 }
1737 else
1738 {
1739 error.SetErrorString("no platform is currently set");
1740 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001741 }
1742 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001743
Jim Ingham4a94c912012-05-17 18:38:42 +00001744 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1745 // module in the list already, and if there was, let's remove it.
1746 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001747 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001748 ObjectFile *objfile = module_sp->GetObjectFile();
1749 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001750 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001751 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001752 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001753 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1754 case ObjectFile::eTypeExecutable: /// A normal executable
1755 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1756 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1757 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1758 break;
1759 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1760 if (error_ptr)
1761 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1762 return ModuleSP();
1763 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1764 if (error_ptr)
1765 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1766 return ModuleSP();
1767 default:
1768 if (error_ptr)
1769 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1770 return ModuleSP();
1771 }
1772 // GetSharedModule is not guaranteed to find the old shared module, for instance
1773 // in the common case where you pass in the UUID, it is only going to find the one
1774 // module matching the UUID. In fact, it has no good way to know what the "old module"
1775 // relevant to this target is, since there might be many copies of a module with this file spec
1776 // in various running debug sessions, but only one of them will belong to this target.
1777 // So let's remove the UUID from the module list, and look in the target's module list.
1778 // Only do this if there is SOMETHING else in the module spec...
1779 if (!old_module_sp)
1780 {
1781 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001782 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001783 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1784 module_spec_copy.GetUUID().Clear();
1785
1786 ModuleList found_modules;
1787 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1788 if (num_found == 1)
1789 {
1790 old_module_sp = found_modules.GetModuleAtIndex(0);
1791 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001792 }
1793 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001794
1795 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1796 {
1797 m_images.ReplaceModule(old_module_sp, module_sp);
1798 Module *old_module_ptr = old_module_sp.get();
1799 old_module_sp.reset();
1800 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1801 }
1802 else
1803 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001804 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001805 else
1806 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001807 }
1808 }
1809 if (error_ptr)
1810 *error_ptr = error;
1811 return module_sp;
1812}
1813
1814
Greg Claytond9e416c2012-02-18 05:35:26 +00001815TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001816Target::CalculateTarget ()
1817{
Greg Claytond9e416c2012-02-18 05:35:26 +00001818 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819}
1820
Greg Claytond9e416c2012-02-18 05:35:26 +00001821ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001822Target::CalculateProcess ()
1823{
Greg Claytond9e416c2012-02-18 05:35:26 +00001824 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001825}
1826
Greg Claytond9e416c2012-02-18 05:35:26 +00001827ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828Target::CalculateThread ()
1829{
Greg Claytond9e416c2012-02-18 05:35:26 +00001830 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001831}
1832
Jason Molendab57e4a12013-11-04 09:33:30 +00001833StackFrameSP
1834Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001835{
Jason Molendab57e4a12013-11-04 09:33:30 +00001836 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001837}
1838
1839void
Greg Clayton0603aa92010-10-04 01:05:56 +00001840Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841{
Greg Claytonc14ee322011-09-22 04:58:26 +00001842 exe_ctx.Clear();
1843 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844}
1845
1846PathMappingList &
1847Target::GetImageSearchPathList ()
1848{
1849 return m_image_search_paths;
1850}
1851
1852void
1853Target::ImageSearchPathsChanged
1854(
1855 const PathMappingList &path_list,
1856 void *baton
1857)
1858{
1859 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001860 ModuleSP exe_module_sp (target->GetExecutableModule());
1861 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001862 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001863}
1864
1865ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001866Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001867{
Greg Clayton73da2442011-08-03 01:23:55 +00001868 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001869 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001870 {
Greg Clayton73da2442011-08-03 01:23:55 +00001871 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001872 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001873 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001874 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001875 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1876 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877 return m_scratch_ast_context_ap.get();
1878}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001879
Sean Callanan686b2312011-11-16 18:20:47 +00001880ClangASTImporter *
1881Target::GetClangASTImporter()
1882{
1883 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1884
1885 if (!ast_importer)
1886 {
1887 ast_importer = new ClangASTImporter();
1888 m_ast_importer_ap.reset(ast_importer);
1889 }
1890
1891 return ast_importer;
1892}
1893
Greg Clayton99d0faf2010-11-18 23:32:35 +00001894void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001895Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001896{
Greg Clayton6920b522012-08-22 18:39:03 +00001897 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001898}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001899
Greg Clayton99d0faf2010-11-18 23:32:35 +00001900void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001901Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001902{
Greg Clayton6920b522012-08-22 18:39:03 +00001903 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001904}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001905
Greg Claytonc859e2d2012-02-13 23:10:39 +00001906FileSpecList
1907Target::GetDefaultExecutableSearchPaths ()
1908{
Greg Clayton67cc0632012-08-22 17:17:09 +00001909 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1910 if (properties_sp)
1911 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001912 return FileSpecList();
1913}
1914
Michael Sartaina7499c92013-07-01 19:45:50 +00001915FileSpecList
1916Target::GetDefaultDebugFileSearchPaths ()
1917{
1918 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1919 if (properties_sp)
1920 return properties_sp->GetDebugFileSearchPaths();
1921 return FileSpecList();
1922}
1923
Caroline Ticedaccaa92010-09-20 20:44:43 +00001924ArchSpec
1925Target::GetDefaultArchitecture ()
1926{
Greg Clayton67cc0632012-08-22 17:17:09 +00001927 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1928 if (properties_sp)
1929 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001930 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001931}
1932
1933void
Greg Clayton67cc0632012-08-22 17:17:09 +00001934Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001935{
Greg Clayton67cc0632012-08-22 17:17:09 +00001936 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1937 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001938 {
1939 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 +00001940 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001941 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001942}
1943
Greg Clayton0603aa92010-10-04 01:05:56 +00001944Target *
1945Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1946{
1947 // The target can either exist in the "process" of ExecutionContext, or in
1948 // the "target_sp" member of SymbolContext. This accessor helper function
1949 // will get the target from one of these locations.
1950
1951 Target *target = NULL;
1952 if (sc_ptr != NULL)
1953 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001954 if (target == NULL && exe_ctx_ptr)
1955 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001956 return target;
1957}
1958
Jim Ingham1624a2d2014-05-05 02:26:40 +00001959ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001960Target::EvaluateExpression
1961(
1962 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001963 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001964 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001965 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001966)
1967{
Enrico Granata97fca502012-09-18 17:43:16 +00001968 result_valobj_sp.reset();
1969
Jim Ingham8646d3c2014-05-05 02:47:44 +00001970 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001971
Greg Claytond1767f02011-12-08 02:13:16 +00001972 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1973 return execution_results;
1974
Jim Ingham6026ca32011-05-12 02:06:14 +00001975 // We shouldn't run stop hooks in expressions.
1976 // Be sure to reset this if you return anywhere within this function.
1977 bool old_suppress_value = m_suppress_stop_hooks;
1978 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001979
1980 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001981
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001982 if (frame)
1983 {
1984 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001985 }
1986 else if (m_process_sp)
1987 {
1988 m_process_sp->CalculateExecutionContext(exe_ctx);
1989 }
1990 else
1991 {
1992 CalculateExecutionContext(exe_ctx);
1993 }
1994
Sean Callanan0bf0baf2013-01-12 02:04:23 +00001995 // Make sure we aren't just trying to see the value of a persistent
1996 // variable (something like "$0")
1997 lldb::ClangExpressionVariableSP persistent_var_sp;
1998 // Only check for persistent variables the expression starts with a '$'
1999 if (expr_cstr[0] == '$')
Zachary Turner32abc6e2015-03-03 19:23:09 +00002000 persistent_var_sp = m_persistent_variables->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002001
2002 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002003 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002004 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002005 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002006 }
2007 else
2008 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002009 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002010 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002011 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002012 options,
2013 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002014 prefix,
2015 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002016 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002017 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002018
2019 m_suppress_stop_hooks = old_suppress_value;
2020
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002021 return execution_results;
2022}
2023
Zachary Turner32abc6e2015-03-03 19:23:09 +00002024ClangPersistentVariables &
2025Target::GetPersistentVariables()
2026{
2027 return *m_persistent_variables;
2028}
2029
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002030lldb::addr_t
2031Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2032{
2033 addr_t code_addr = load_addr;
2034 switch (m_arch.GetMachine())
2035 {
2036 case llvm::Triple::arm:
2037 case llvm::Triple::thumb:
2038 switch (addr_class)
2039 {
2040 case eAddressClassData:
2041 case eAddressClassDebug:
2042 return LLDB_INVALID_ADDRESS;
2043
2044 case eAddressClassUnknown:
2045 case eAddressClassInvalid:
2046 case eAddressClassCode:
2047 case eAddressClassCodeAlternateISA:
2048 case eAddressClassRuntime:
2049 // Check if bit zero it no set?
2050 if ((code_addr & 1ull) == 0)
2051 {
2052 // Bit zero isn't set, check if the address is a multiple of 2?
2053 if (code_addr & 2ull)
2054 {
2055 // The address is a multiple of 2 so it must be thumb, set bit zero
2056 code_addr |= 1ull;
2057 }
2058 else if (addr_class == eAddressClassCodeAlternateISA)
2059 {
2060 // We checked the address and the address claims to be the alternate ISA
2061 // which means thumb, so set bit zero.
2062 code_addr |= 1ull;
2063 }
2064 }
2065 break;
2066 }
2067 break;
2068
2069 default:
2070 break;
2071 }
2072 return code_addr;
2073}
2074
2075lldb::addr_t
2076Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2077{
2078 addr_t opcode_addr = load_addr;
2079 switch (m_arch.GetMachine())
2080 {
2081 case llvm::Triple::arm:
2082 case llvm::Triple::thumb:
2083 switch (addr_class)
2084 {
2085 case eAddressClassData:
2086 case eAddressClassDebug:
2087 return LLDB_INVALID_ADDRESS;
2088
2089 case eAddressClassInvalid:
2090 case eAddressClassUnknown:
2091 case eAddressClassCode:
2092 case eAddressClassCodeAlternateISA:
2093 case eAddressClassRuntime:
2094 opcode_addr &= ~(1ull);
2095 break;
2096 }
2097 break;
2098
2099 default:
2100 break;
2101 }
2102 return opcode_addr;
2103}
2104
Greg Clayton9585fbf2013-03-19 00:20:55 +00002105SourceManager &
2106Target::GetSourceManager ()
2107{
2108 if (m_source_manager_ap.get() == NULL)
2109 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2110 return *m_source_manager_ap;
2111}
2112
Sean Callanan9998acd2014-12-05 01:21:59 +00002113ClangModulesDeclVendor *
2114Target::GetClangModulesDeclVendor ()
2115{
2116 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2117
2118 {
2119 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2120
2121 if (!m_clang_modules_decl_vendor_ap)
2122 {
2123 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2124 }
2125 }
2126
2127 return m_clang_modules_decl_vendor_ap.get();
2128}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002129
Greg Clayton44d93782014-01-27 23:43:24 +00002130Target::StopHookSP
2131Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002132{
2133 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002134 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2135 m_stop_hooks[new_uid] = stop_hook_sp;
2136 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002137}
2138
2139bool
2140Target::RemoveStopHookByID (lldb::user_id_t user_id)
2141{
2142 size_t num_removed;
2143 num_removed = m_stop_hooks.erase (user_id);
2144 if (num_removed == 0)
2145 return false;
2146 else
2147 return true;
2148}
2149
2150void
2151Target::RemoveAllStopHooks ()
2152{
2153 m_stop_hooks.clear();
2154}
2155
2156Target::StopHookSP
2157Target::GetStopHookByID (lldb::user_id_t user_id)
2158{
2159 StopHookSP found_hook;
2160
2161 StopHookCollection::iterator specified_hook_iter;
2162 specified_hook_iter = m_stop_hooks.find (user_id);
2163 if (specified_hook_iter != m_stop_hooks.end())
2164 found_hook = (*specified_hook_iter).second;
2165 return found_hook;
2166}
2167
2168bool
2169Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2170{
2171 StopHookCollection::iterator specified_hook_iter;
2172 specified_hook_iter = m_stop_hooks.find (user_id);
2173 if (specified_hook_iter == m_stop_hooks.end())
2174 return false;
2175
2176 (*specified_hook_iter).second->SetIsActive (active_state);
2177 return true;
2178}
2179
2180void
2181Target::SetAllStopHooksActiveState (bool active_state)
2182{
2183 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2184 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2185 {
2186 (*pos).second->SetIsActive (active_state);
2187 }
2188}
2189
2190void
2191Target::RunStopHooks ()
2192{
Jim Ingham6026ca32011-05-12 02:06:14 +00002193 if (m_suppress_stop_hooks)
2194 return;
2195
Jim Ingham9575d842011-03-11 03:53:59 +00002196 if (!m_process_sp)
2197 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002198
2199 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2200 // since in that case we do not want to run the stop-hooks
2201 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2202 return;
2203
Jim Ingham9575d842011-03-11 03:53:59 +00002204 if (m_stop_hooks.empty())
2205 return;
2206
2207 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2208
2209 // If there aren't any active stop hooks, don't bother either:
2210 bool any_active_hooks = false;
2211 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2212 {
2213 if ((*pos).second->IsActive())
2214 {
2215 any_active_hooks = true;
2216 break;
2217 }
2218 }
2219 if (!any_active_hooks)
2220 return;
2221
2222 CommandReturnObject result;
2223
2224 std::vector<ExecutionContext> exc_ctx_with_reasons;
2225 std::vector<SymbolContext> sym_ctx_with_reasons;
2226
2227 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2228 size_t num_threads = cur_threadlist.GetSize();
2229 for (size_t i = 0; i < num_threads; i++)
2230 {
2231 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2232 if (cur_thread_sp->ThreadStoppedForAReason())
2233 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002234 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002235 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2236 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2237 }
2238 }
2239
2240 // If no threads stopped for a reason, don't run the stop-hooks.
2241 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2242 if (num_exe_ctx == 0)
2243 return;
2244
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002245 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2246 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002247
2248 bool keep_going = true;
2249 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002250 bool print_hook_header;
2251 bool print_thread_header;
2252
2253 if (num_exe_ctx == 1)
2254 print_thread_header = false;
2255 else
2256 print_thread_header = true;
2257
2258 if (m_stop_hooks.size() == 1)
2259 print_hook_header = false;
2260 else
2261 print_hook_header = true;
2262
Jim Ingham9575d842011-03-11 03:53:59 +00002263 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2264 {
2265 // result.Clear();
2266 StopHookSP cur_hook_sp = (*pos).second;
2267 if (!cur_hook_sp->IsActive())
2268 continue;
2269
2270 bool any_thread_matched = false;
2271 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2272 {
2273 if ((cur_hook_sp->GetSpecifier () == NULL
2274 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2275 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002276 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002277 {
2278 if (!hooks_ran)
2279 {
Jim Ingham9575d842011-03-11 03:53:59 +00002280 hooks_ran = true;
2281 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002282 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002283 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002284 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2285 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2286 NULL);
2287 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002288 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002289 else
Daniel Malead01b2952012-11-29 21:49:15 +00002290 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002291 any_thread_matched = true;
2292 }
2293
Jim Ingham381e25b2011-03-22 01:47:27 +00002294 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002295 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002296
2297 CommandInterpreterRunOptions options;
2298 options.SetStopOnContinue (true);
2299 options.SetStopOnError (true);
2300 options.SetEchoCommands (false);
2301 options.SetPrintResults (true);
2302 options.SetAddToHistory (false);
2303
2304 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2305 &exc_ctx_with_reasons[i],
2306 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002307 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002308
2309 // If the command started the target going again, we should bag out of
2310 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002311 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2312 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002313 {
Daniel Malead01b2952012-11-29 21:49:15 +00002314 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002315 keep_going = false;
2316 }
2317 }
2318 }
2319 }
Jason Molenda879cf772011-09-23 00:42:55 +00002320
Caroline Tice969ed3d2011-05-02 20:41:46 +00002321 result.GetImmediateOutputStream()->Flush();
2322 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002323}
2324
Greg Claytonfbb76342013-11-20 21:07:01 +00002325const TargetPropertiesSP &
2326Target::GetGlobalProperties()
2327{
2328 static TargetPropertiesSP g_settings_sp;
2329 if (!g_settings_sp)
2330 {
2331 g_settings_sp.reset (new TargetProperties (NULL));
2332 }
2333 return g_settings_sp;
2334}
2335
2336Error
2337Target::Install (ProcessLaunchInfo *launch_info)
2338{
2339 Error error;
2340 PlatformSP platform_sp (GetPlatform());
2341 if (platform_sp)
2342 {
2343 if (platform_sp->IsRemote())
2344 {
2345 if (platform_sp->IsConnected())
2346 {
2347 // Install all files that have an install path, and always install the
2348 // main executable when connected to a remote platform
2349 const ModuleList& modules = GetImages();
2350 const size_t num_images = modules.GetSize();
2351 for (size_t idx = 0; idx < num_images; ++idx)
2352 {
2353 const bool is_main_executable = idx == 0;
2354 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2355 if (module_sp)
2356 {
2357 FileSpec local_file (module_sp->GetFileSpec());
2358 if (local_file)
2359 {
2360 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2361 if (!remote_file)
2362 {
2363 if (is_main_executable) // TODO: add setting for always installing main executable???
2364 {
2365 // Always install the main executable
2366 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2367 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2368 }
2369 }
2370 if (remote_file)
2371 {
2372 error = platform_sp->Install(local_file, remote_file);
2373 if (error.Success())
2374 {
2375 module_sp->SetPlatformFileSpec(remote_file);
2376 if (is_main_executable)
2377 {
2378 if (launch_info)
2379 launch_info->SetExecutableFile(remote_file, false);
2380 }
2381 }
2382 else
2383 break;
2384 }
2385 }
2386 }
2387 }
2388 }
2389 }
2390 }
2391 return error;
2392}
Greg Clayton7b242382011-07-08 00:48:09 +00002393
Greg Claytond5944cd2013-12-06 01:12:00 +00002394bool
2395Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2396{
2397 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2398}
2399
2400bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002401Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2402{
2403 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2404}
2405
2406bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002407Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2408{
2409 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2410 if (old_section_load_addr != new_section_load_addr)
2411 {
2412 uint32_t stop_id = 0;
2413 ProcessSP process_sp(GetProcessSP());
2414 if (process_sp)
2415 stop_id = process_sp->GetStopID();
2416 else
2417 stop_id = m_section_load_history.GetLastStopID();
2418 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2419 return true; // Return true if the section load address was changed...
2420 }
2421 return false; // Return false to indicate nothing changed
2422
2423}
2424
Greg Clayton8012cad2014-11-17 19:39:20 +00002425size_t
2426Target::UnloadModuleSections (const ModuleList &module_list)
2427{
2428 size_t section_unload_count = 0;
2429 size_t num_modules = module_list.GetSize();
2430 for (size_t i=0; i<num_modules; ++i)
2431 {
2432 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2433 }
2434 return section_unload_count;
2435}
2436
2437size_t
2438Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2439{
2440 uint32_t stop_id = 0;
2441 ProcessSP process_sp(GetProcessSP());
2442 if (process_sp)
2443 stop_id = process_sp->GetStopID();
2444 else
2445 stop_id = m_section_load_history.GetLastStopID();
2446 SectionList *sections = module_sp->GetSectionList();
2447 size_t section_unload_count = 0;
2448 if (sections)
2449 {
2450 const uint32_t num_sections = sections->GetNumSections(0);
2451 for (uint32_t i = 0; i < num_sections; ++i)
2452 {
2453 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2454 }
2455 }
2456 return section_unload_count;
2457}
2458
Greg Claytond5944cd2013-12-06 01:12:00 +00002459bool
2460Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2461{
2462 uint32_t stop_id = 0;
2463 ProcessSP process_sp(GetProcessSP());
2464 if (process_sp)
2465 stop_id = process_sp->GetStopID();
2466 else
2467 stop_id = m_section_load_history.GetLastStopID();
2468 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2469}
2470
2471bool
2472Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2473{
2474 uint32_t stop_id = 0;
2475 ProcessSP process_sp(GetProcessSP());
2476 if (process_sp)
2477 stop_id = process_sp->GetStopID();
2478 else
2479 stop_id = m_section_load_history.GetLastStopID();
2480 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2481}
2482
2483void
2484Target::ClearAllLoadedSections ()
2485{
2486 m_section_load_history.Clear();
2487}
2488
Greg Claytonb09c5382013-12-13 17:20:18 +00002489
2490Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002491Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002492{
2493 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002494 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2495
2496 if (log)
2497 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2498
Greg Claytonb09c5382013-12-13 17:20:18 +00002499 StateType state = eStateInvalid;
2500
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002501 // Scope to temporarily get the process state in case someone has manually
2502 // remotely connected already to a process and we can skip the platform
2503 // launching.
2504 {
2505 ProcessSP process_sp (GetProcessSP());
2506
2507 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002508 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002509 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002510 if (log)
2511 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2512 }
2513 else
2514 {
2515 if (log)
2516 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2517 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002518 }
2519
Greg Claytonb09c5382013-12-13 17:20:18 +00002520 launch_info.GetFlags().Set (eLaunchFlagDebug);
2521
2522 // Get the value of synchronous execution here. If you wait till after you have started to
2523 // run, then you could have hit a breakpoint, whose command might switch the value, and
2524 // then you'll pick up that incorrect value.
2525 Debugger &debugger = GetDebugger();
2526 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2527
2528 PlatformSP platform_sp (GetPlatform());
2529
2530 // Finalize the file actions, and if none were given, default to opening
2531 // up a pseudo terminal
2532 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002533 if (log)
2534 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2535 __FUNCTION__,
2536 platform_sp ? "true" : "false",
2537 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2538 default_to_use_pty ? "true" : "false");
2539
Greg Claytonb09c5382013-12-13 17:20:18 +00002540 launch_info.FinalizeFileActions (this, default_to_use_pty);
2541
2542 if (state == eStateConnected)
2543 {
2544 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2545 {
2546 error.SetErrorString("can't launch in tty when launching through a remote connection");
2547 return error;
2548 }
2549 }
2550
2551 if (!launch_info.GetArchitecture().IsValid())
2552 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002553
2554 // 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 +00002555 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2556 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002557 if (log)
2558 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2559
Greg Claytonb09c5382013-12-13 17:20:18 +00002560 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2561 debugger,
2562 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002563 error);
2564 }
2565 else
2566 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002567 if (log)
2568 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2569
Greg Claytonb09c5382013-12-13 17:20:18 +00002570 if (state == eStateConnected)
2571 {
2572 assert(m_process_sp);
2573 }
2574 else
2575 {
Todd Fiala015d8182014-07-22 23:41:36 +00002576 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002577 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002578 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002579 }
Todd Fiala015d8182014-07-22 23:41:36 +00002580
2581 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002582 if (m_process_sp)
2583 error = m_process_sp->Launch (launch_info);
2584 }
2585
2586 if (!m_process_sp)
2587 {
2588 if (error.Success())
2589 error.SetErrorString("failed to launch or debug process");
2590 return error;
2591 }
2592
2593 if (error.Success())
2594 {
Ilia K064e69f2015-03-23 21:16:25 +00002595 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00002596 {
Ilia K064e69f2015-03-23 21:16:25 +00002597 EventSP event_sp;
Greg Clayton44d93782014-01-27 23:43:24 +00002598 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00002599 if (!hijack_listener_sp)
2600 {
2601 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
2602 launch_info.SetHijackListener(hijack_listener_sp);
2603 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
2604 }
Todd Fialaac33cc92014-10-09 01:02:08 +00002605
Ilia K064e69f2015-03-23 21:16:25 +00002606 StateType state = m_process_sp->WaitForProcessToStop (NULL, &event_sp, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002607
2608 if (state == eStateStopped)
2609 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002610 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00002611 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002612 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00002613 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002614 error = m_process_sp->PrivateResume();
2615 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00002616 {
Ilia K064e69f2015-03-23 21:16:25 +00002617 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
2618 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2619 if (!StateIsStoppedState(state, must_be_alive))
2620 {
2621 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
2622 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002623 }
2624 }
Ilia K064e69f2015-03-23 21:16:25 +00002625 else
2626 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002627 m_process_sp->RestoreProcessEvents();
2628 error = m_process_sp->PrivateResume();
2629 if (error.Success())
2630 {
2631 // there is a race condition where this thread will return up the call stack to the main command
2632 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
2633 // a chance to call PushProcessIOHandler()
2634 m_process_sp->SyncIOHandler(2000);
2635 }
2636 }
2637 if (!error.Success())
2638 {
Ilia K064e69f2015-03-23 21:16:25 +00002639 Error error2;
2640 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
2641 error = error2;
2642 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002643 }
2644 else
2645 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002646 assert(synchronous_execution && launch_info.GetFlags().Test(eLaunchFlagStopAtEntry));
Ilia K064e69f2015-03-23 21:16:25 +00002647
2648 // Target was stopped at entry as was intended. Need to notify the listeners about it.
2649 m_process_sp->RestoreProcessEvents();
2650 m_process_sp->HandlePrivateEvent(event_sp);
2651
2652 // there is a race condition where this thread will return up the call stack to the main command
2653 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
2654 // a chance to call PushProcessIOHandler()
2655 m_process_sp->SyncIOHandler(2000);
Greg Claytonb09c5382013-12-13 17:20:18 +00002656 }
2657 }
Greg Clayton40286e02014-04-30 20:29:09 +00002658 else if (state == eStateExited)
2659 {
Zachary Turner10687b02014-10-20 17:46:43 +00002660 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002661 const int exit_status = m_process_sp->GetExitStatus();
2662 const char *exit_desc = m_process_sp->GetExitDescription();
2663#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'."
2664 if (exit_desc && exit_desc[0])
2665 {
2666 if (with_shell)
2667 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2668 else
2669 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2670 }
2671 else
2672 {
2673 if (with_shell)
2674 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2675 else
2676 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2677 }
2678 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002679 else
2680 {
2681 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2682 }
2683 }
Greg Clayton44d93782014-01-27 23:43:24 +00002684 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002685 }
2686 else
2687 {
Greg Clayton44d93782014-01-27 23:43:24 +00002688 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002689 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002690 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002691 }
2692 return error;
2693}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002694
2695Error
2696Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
2697{
2698 auto state = eStateInvalid;
2699 auto process_sp = GetProcessSP ();
2700 if (process_sp)
2701 {
2702 state = process_sp->GetState ();
2703 if (process_sp->IsAlive () && state != eStateConnected)
2704 {
2705 if (state == eStateAttaching)
2706 return Error ("process attach is in progress");
2707 return Error ("a process is already being debugged");
2708 }
2709 }
2710
2711 ListenerSP hijack_listener_sp (new Listener ("lldb.Target.Attach.attach.hijack"));
2712 attach_info.SetHijackListener (hijack_listener_sp);
2713
2714 const ModuleSP old_exec_module_sp = GetExecutableModule ();
2715
2716 // If no process info was specified, then use the target executable
2717 // name as the process to attach to by default
2718 if (!attach_info.ProcessInfoSpecified ())
2719 {
2720 if (old_exec_module_sp)
2721 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
2722
2723 if (!attach_info.ProcessInfoSpecified ())
2724 {
2725 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
2726 }
2727 }
2728
2729 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
2730
2731 Error error;
2732 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
2733 {
2734 SetPlatform (platform_sp);
2735 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
2736 }
2737 else
2738 {
2739 if (state != eStateConnected)
2740 {
2741 const char *plugin_name = attach_info.GetProcessPluginName ();
2742 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
2743 if (process_sp == nullptr)
2744 {
2745 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
2746 return error;
2747 }
2748 }
2749 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
2750 error = process_sp->Attach (attach_info);
2751 }
2752
2753 if (error.Success () && process_sp)
2754 {
2755 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
2756 process_sp->RestoreProcessEvents ();
2757
2758 if (state != eStateStopped)
2759 {
2760 const char *exit_desc = process_sp->GetExitDescription ();
2761 if (exit_desc)
2762 error.SetErrorStringWithFormat ("attach failed: %s", exit_desc);
2763 else
2764 error.SetErrorString ("attach failed: process did not stop (no such process or permission problem?)");
2765 process_sp->Destroy ();
2766 }
2767 }
2768 return error;
2769}
2770
Jim Ingham9575d842011-03-11 03:53:59 +00002771//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002772// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002773//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002774Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2775 UserID (uid),
2776 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002777 m_commands (),
2778 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002779 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002780 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002781{
2782}
2783
2784Target::StopHook::StopHook (const StopHook &rhs) :
2785 UserID (rhs.GetID()),
2786 m_target_sp (rhs.m_target_sp),
2787 m_commands (rhs.m_commands),
2788 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002789 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002790 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002791{
2792 if (rhs.m_thread_spec_ap.get() != NULL)
2793 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2794}
2795
2796
2797Target::StopHook::~StopHook ()
2798{
2799}
2800
2801void
Zachary Turner32abc6e2015-03-03 19:23:09 +00002802Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
2803{
2804 m_specifier_sp.reset(specifier);
2805}
2806
2807void
Jim Ingham9575d842011-03-11 03:53:59 +00002808Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2809{
2810 m_thread_spec_ap.reset (specifier);
2811}
2812
2813
2814void
2815Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2816{
2817 int indent_level = s->GetIndentLevel();
2818
2819 s->SetIndentLevel(indent_level + 2);
2820
Daniel Malead01b2952012-11-29 21:49:15 +00002821 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002822 if (m_active)
2823 s->Indent ("State: enabled\n");
2824 else
2825 s->Indent ("State: disabled\n");
2826
2827 if (m_specifier_sp)
2828 {
2829 s->Indent();
2830 s->PutCString ("Specifier:\n");
2831 s->SetIndentLevel (indent_level + 4);
2832 m_specifier_sp->GetDescription (s, level);
2833 s->SetIndentLevel (indent_level + 2);
2834 }
2835
2836 if (m_thread_spec_ap.get() != NULL)
2837 {
2838 StreamString tmp;
2839 s->Indent("Thread:\n");
2840 m_thread_spec_ap->GetDescription (&tmp, level);
2841 s->SetIndentLevel (indent_level + 4);
2842 s->Indent (tmp.GetData());
2843 s->PutCString ("\n");
2844 s->SetIndentLevel (indent_level + 2);
2845 }
2846
2847 s->Indent ("Commands: \n");
2848 s->SetIndentLevel (indent_level + 4);
2849 uint32_t num_commands = m_commands.GetSize();
2850 for (uint32_t i = 0; i < num_commands; i++)
2851 {
2852 s->Indent(m_commands.GetStringAtIndex(i));
2853 s->PutCString ("\n");
2854 }
2855 s->SetIndentLevel (indent_level);
2856}
2857
Greg Clayton67cc0632012-08-22 17:17:09 +00002858//--------------------------------------------------------------
2859// class TargetProperties
2860//--------------------------------------------------------------
2861
2862OptionEnumValueElement
2863lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002864{
Greg Clayton67cc0632012-08-22 17:17:09 +00002865 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2866 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2867 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2868 { 0, NULL, NULL }
2869};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002870
Greg Clayton1f746072012-08-29 21:13:06 +00002871static OptionEnumValueElement
2872g_inline_breakpoint_enums[] =
2873{
2874 { 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."},
2875 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2876 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2877 { 0, NULL, NULL }
2878};
2879
Jim Ingham0f063ba2013-03-02 00:26:47 +00002880typedef enum x86DisassemblyFlavor
2881{
2882 eX86DisFlavorDefault,
2883 eX86DisFlavorIntel,
2884 eX86DisFlavorATT
2885} x86DisassemblyFlavor;
2886
2887static OptionEnumValueElement
2888g_x86_dis_flavor_value_types[] =
2889{
2890 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2891 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2892 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2893 { 0, NULL, NULL }
2894};
2895
Enrico Granata397ddd52013-05-21 20:13:34 +00002896static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002897g_hex_immediate_style_values[] =
2898{
2899 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2900 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2901 { 0, NULL, NULL }
2902};
2903
2904static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002905g_load_script_from_sym_file_values[] =
2906{
2907 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2908 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2909 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2910 { 0, NULL, NULL }
2911};
2912
Greg Claytonfd814c52013-08-13 01:42:25 +00002913
2914static OptionEnumValueElement
2915g_memory_module_load_level_values[] =
2916{
Greg Clayton86eac942013-08-13 21:32:34 +00002917 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002918 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2919 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2920 { 0, NULL, NULL }
2921};
2922
Greg Clayton67cc0632012-08-22 17:17:09 +00002923static PropertyDefinition
2924g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002925{
Greg Clayton67cc0632012-08-22 17:17:09 +00002926 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2927 { "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 +00002928 { "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 +00002929 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2930 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2931 { "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 "
2932 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2933 "some part (starting at the root) of the path to the file when it was built, "
2934 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2935 "Each element of the array is checked in order and the first one that results in a match wins." },
2936 { "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 +00002937 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating debug symbol files." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002938 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2939 { "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 +00002940 { "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 +00002941 { "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 +00002942 { "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." },
2943 { "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 +00002944 { "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." },
2945 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2946 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2947 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2948 { "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 +00002949 { "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 +00002950 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2951 { "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 +00002952 { "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 +00002953 "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. "
2954 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2955 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002956 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2957 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2958 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002959 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002960 // 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.
2961 { "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 +00002962 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2963 { "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 +00002964 { "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 +00002965 { "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 +00002966 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2967 "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. "
2968 "This setting helps users control how much information gets loaded when loading modules from memory."
2969 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2970 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2971 "'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 +00002972 { "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 +00002973 { "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 +00002974 { "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 +00002975 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2976};
Enrico Granata560558e2015-02-11 02:35:39 +00002977
Greg Clayton67cc0632012-08-22 17:17:09 +00002978enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002979{
Greg Clayton67cc0632012-08-22 17:17:09 +00002980 ePropertyDefaultArch,
2981 ePropertyExprPrefix,
2982 ePropertyPreferDynamic,
2983 ePropertyEnableSynthetic,
2984 ePropertySkipPrologue,
2985 ePropertySourceMap,
2986 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002987 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002988 ePropertyMaxChildrenCount,
2989 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002990 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002991 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002992 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002993 ePropertyRunArgs,
2994 ePropertyEnvVars,
2995 ePropertyInheritEnv,
2996 ePropertyInputPath,
2997 ePropertyOutputPath,
2998 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00002999 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003000 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003001 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003002 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003003 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003004 ePropertyUseHexImmediates,
3005 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003006 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003007 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003008 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003009 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003010 ePropertyTrapHandlerNames,
3011 ePropertyDisplayRuntimeSupportValues
Greg Clayton67cc0632012-08-22 17:17:09 +00003012};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003013
Caroline Ticedaccaa92010-09-20 20:44:43 +00003014
Greg Clayton67cc0632012-08-22 17:17:09 +00003015class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003016{
Greg Clayton67cc0632012-08-22 17:17:09 +00003017public:
3018 TargetOptionValueProperties (const ConstString &name) :
3019 OptionValueProperties (name),
3020 m_target (NULL),
3021 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003022 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003023 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003024
Greg Clayton67cc0632012-08-22 17:17:09 +00003025 // This constructor is used when creating TargetOptionValueProperties when it
3026 // is part of a new lldb_private::Target instance. It will copy all current
3027 // global property values as needed
3028 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3029 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3030 m_target (target),
3031 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003032 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003033 }
3034
3035 virtual const Property *
3036 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3037 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003038 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003039 // try and grab the setting from the current target if there is one. Else we just
3040 // use the one from this instance.
3041 if (idx == ePropertyEnvVars)
3042 GetHostEnvironmentIfNeeded ();
3043
3044 if (exe_ctx)
3045 {
3046 Target *target = exe_ctx->GetTargetPtr();
3047 if (target)
3048 {
3049 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3050 if (this != target_properties)
3051 return target_properties->ProtectedGetPropertyAtIndex (idx);
3052 }
3053 }
3054 return ProtectedGetPropertyAtIndex (idx);
3055 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003056
3057 lldb::TargetSP
3058 GetTargetSP ()
3059 {
3060 return m_target->shared_from_this();
3061 }
3062
Greg Clayton67cc0632012-08-22 17:17:09 +00003063protected:
3064
3065 void
3066 GetHostEnvironmentIfNeeded () const
3067 {
3068 if (!m_got_host_env)
3069 {
3070 if (m_target)
3071 {
3072 m_got_host_env = true;
3073 const uint32_t idx = ePropertyInheritEnv;
3074 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3075 {
3076 PlatformSP platform_sp (m_target->GetPlatform());
3077 if (platform_sp)
3078 {
3079 StringList env;
3080 if (platform_sp->GetEnvironment(env))
3081 {
3082 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3083 if (env_dict)
3084 {
3085 const bool can_replace = false;
3086 const size_t envc = env.GetSize();
3087 for (size_t idx=0; idx<envc; idx++)
3088 {
3089 const char *env_entry = env.GetStringAtIndex (idx);
3090 if (env_entry)
3091 {
3092 const char *equal_pos = ::strchr(env_entry, '=');
3093 ConstString key;
3094 // It is ok to have environment variables with no values
3095 const char *value = NULL;
3096 if (equal_pos)
3097 {
3098 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3099 if (equal_pos[1])
3100 value = equal_pos + 1;
3101 }
3102 else
3103 {
3104 key.SetCString(env_entry);
3105 }
3106 // Don't allow existing keys to be replaced with ones we get from the platform environment
3107 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3108 }
3109 }
3110 }
3111 }
3112 }
3113 }
3114 }
3115 }
3116 }
3117 Target *m_target;
3118 mutable bool m_got_host_env;
3119};
3120
Greg Claytonfbb76342013-11-20 21:07:01 +00003121//----------------------------------------------------------------------
3122// TargetProperties
3123//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003124TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003125 Properties (),
3126 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003127{
3128 if (target)
3129 {
3130 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003131
3132 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3133 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3134 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3135 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3136 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3137 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3138 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3139 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3140 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3141 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3142
3143 // Update m_launch_info once it was created
3144 Arg0ValueChangedCallback(this, NULL);
3145 RunArgsValueChangedCallback(this, NULL);
3146 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3147 InputPathValueChangedCallback(this, NULL);
3148 OutputPathValueChangedCallback(this, NULL);
3149 ErrorPathValueChangedCallback(this, NULL);
3150 DetachOnErrorValueChangedCallback(this, NULL);
3151 DisableASLRValueChangedCallback(this, NULL);
3152 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003153 }
3154 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003155 {
3156 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3157 m_collection_sp->Initialize(g_properties);
3158 m_collection_sp->AppendProperty(ConstString("process"),
3159 ConstString("Settings specify to processes."),
3160 true,
3161 Process::GetGlobalProperties()->GetValueProperties());
3162 }
Ilia K8f37ca52015-02-13 14:31:06 +00003163
Caroline Ticedaccaa92010-09-20 20:44:43 +00003164}
3165
Greg Clayton67cc0632012-08-22 17:17:09 +00003166TargetProperties::~TargetProperties ()
3167{
3168}
3169ArchSpec
3170TargetProperties::GetDefaultArchitecture () const
3171{
3172 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3173 if (value)
3174 return value->GetCurrentValue();
3175 return ArchSpec();
3176}
3177
3178void
3179TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3180{
3181 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3182 if (value)
3183 return value->SetCurrentValue(arch, true);
3184}
3185
3186lldb::DynamicValueType
3187TargetProperties::GetPreferDynamicValue() const
3188{
3189 const uint32_t idx = ePropertyPreferDynamic;
3190 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3191}
3192
3193bool
3194TargetProperties::GetDisableASLR () const
3195{
3196 const uint32_t idx = ePropertyDisableASLR;
3197 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3198}
3199
3200void
3201TargetProperties::SetDisableASLR (bool b)
3202{
3203 const uint32_t idx = ePropertyDisableASLR;
3204 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3205}
3206
3207bool
Jim Ingham106d0282014-06-25 02:32:56 +00003208TargetProperties::GetDetachOnError () const
3209{
3210 const uint32_t idx = ePropertyDetachOnError;
3211 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3212}
3213
3214void
3215TargetProperties::SetDetachOnError (bool b)
3216{
3217 const uint32_t idx = ePropertyDetachOnError;
3218 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3219}
3220
3221bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003222TargetProperties::GetDisableSTDIO () const
3223{
3224 const uint32_t idx = ePropertyDisableSTDIO;
3225 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3226}
3227
3228void
3229TargetProperties::SetDisableSTDIO (bool b)
3230{
3231 const uint32_t idx = ePropertyDisableSTDIO;
3232 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3233}
3234
Jim Ingham0f063ba2013-03-02 00:26:47 +00003235const char *
3236TargetProperties::GetDisassemblyFlavor () const
3237{
3238 const uint32_t idx = ePropertyDisassemblyFlavor;
3239 const char *return_value;
3240
3241 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3242 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3243 return return_value;
3244}
3245
Greg Clayton1f746072012-08-29 21:13:06 +00003246InlineStrategy
3247TargetProperties::GetInlineStrategy () const
3248{
3249 const uint32_t idx = ePropertyInlineStrategy;
3250 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3251}
3252
Greg Clayton45392552012-10-17 22:57:12 +00003253const char *
3254TargetProperties::GetArg0 () const
3255{
3256 const uint32_t idx = ePropertyArg0;
3257 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3258}
3259
3260void
3261TargetProperties::SetArg0 (const char *arg)
3262{
3263 const uint32_t idx = ePropertyArg0;
3264 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003265 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003266}
3267
Greg Clayton67cc0632012-08-22 17:17:09 +00003268bool
3269TargetProperties::GetRunArguments (Args &args) const
3270{
3271 const uint32_t idx = ePropertyRunArgs;
3272 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3273}
3274
3275void
3276TargetProperties::SetRunArguments (const Args &args)
3277{
3278 const uint32_t idx = ePropertyRunArgs;
3279 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003280 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003281}
3282
3283size_t
3284TargetProperties::GetEnvironmentAsArgs (Args &env) const
3285{
3286 const uint32_t idx = ePropertyEnvVars;
3287 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3288}
3289
Ilia K8f37ca52015-02-13 14:31:06 +00003290void
3291TargetProperties::SetEnvironmentFromArgs (const Args &env)
3292{
3293 const uint32_t idx = ePropertyEnvVars;
3294 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003295 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003296}
3297
Greg Clayton67cc0632012-08-22 17:17:09 +00003298bool
3299TargetProperties::GetSkipPrologue() const
3300{
3301 const uint32_t idx = ePropertySkipPrologue;
3302 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3303}
3304
3305PathMappingList &
3306TargetProperties::GetSourcePathMap () const
3307{
3308 const uint32_t idx = ePropertySourceMap;
3309 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3310 assert(option_value);
3311 return option_value->GetCurrentValue();
3312}
3313
3314FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003315TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003316{
3317 const uint32_t idx = ePropertyExecutableSearchPaths;
3318 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3319 assert(option_value);
3320 return option_value->GetCurrentValue();
3321}
3322
Michael Sartaina7499c92013-07-01 19:45:50 +00003323FileSpecList &
3324TargetProperties::GetDebugFileSearchPaths ()
3325{
3326 const uint32_t idx = ePropertyDebugFileSearchPaths;
3327 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3328 assert(option_value);
3329 return option_value->GetCurrentValue();
3330}
3331
Greg Clayton67cc0632012-08-22 17:17:09 +00003332bool
3333TargetProperties::GetEnableSyntheticValue () const
3334{
3335 const uint32_t idx = ePropertyEnableSynthetic;
3336 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3337}
3338
3339uint32_t
3340TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3341{
3342 const uint32_t idx = ePropertyMaxChildrenCount;
3343 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3344}
3345
3346uint32_t
3347TargetProperties::GetMaximumSizeOfStringSummary() const
3348{
3349 const uint32_t idx = ePropertyMaxSummaryLength;
3350 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3351}
3352
Enrico Granatad325bf92013-06-04 22:54:16 +00003353uint32_t
3354TargetProperties::GetMaximumMemReadSize () const
3355{
3356 const uint32_t idx = ePropertyMaxMemReadSize;
3357 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3358}
3359
Greg Clayton67cc0632012-08-22 17:17:09 +00003360FileSpec
3361TargetProperties::GetStandardInputPath () const
3362{
3363 const uint32_t idx = ePropertyInputPath;
3364 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3365}
3366
3367void
3368TargetProperties::SetStandardInputPath (const char *p)
3369{
3370 const uint32_t idx = ePropertyInputPath;
3371 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3372}
3373
3374FileSpec
3375TargetProperties::GetStandardOutputPath () const
3376{
3377 const uint32_t idx = ePropertyOutputPath;
3378 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3379}
3380
3381void
3382TargetProperties::SetStandardOutputPath (const char *p)
3383{
3384 const uint32_t idx = ePropertyOutputPath;
3385 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3386}
3387
3388FileSpec
3389TargetProperties::GetStandardErrorPath () const
3390{
3391 const uint32_t idx = ePropertyErrorPath;
3392 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3393}
3394
Greg Clayton6920b522012-08-22 18:39:03 +00003395const char *
3396TargetProperties::GetExpressionPrefixContentsAsCString ()
3397{
3398 const uint32_t idx = ePropertyExprPrefix;
3399 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3400 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003401 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003402 const bool null_terminate = true;
3403 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003404 if (data_sp)
3405 return (const char *) data_sp->GetBytes();
3406 }
Greg Clayton6920b522012-08-22 18:39:03 +00003407 return NULL;
3408}
3409
Greg Clayton67cc0632012-08-22 17:17:09 +00003410void
3411TargetProperties::SetStandardErrorPath (const char *p)
3412{
3413 const uint32_t idx = ePropertyErrorPath;
3414 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3415}
3416
3417bool
3418TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3419{
3420 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3421 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3422}
3423
Jim Ingham17d023f2013-03-13 17:58:04 +00003424bool
Daniel Malead79ae052013-08-07 21:54:09 +00003425TargetProperties::GetUseHexImmediates () const
3426{
3427 const uint32_t idx = ePropertyUseHexImmediates;
3428 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3429}
3430
3431bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003432TargetProperties::GetUseFastStepping () const
3433{
3434 const uint32_t idx = ePropertyUseFastStepping;
3435 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3436}
3437
Greg Claytonfb6621e2013-12-06 21:59:52 +00003438bool
3439TargetProperties::GetDisplayExpressionsInCrashlogs () const
3440{
3441 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3442 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3443}
3444
Enrico Granata397ddd52013-05-21 20:13:34 +00003445LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003446TargetProperties::GetLoadScriptFromSymbolFile () const
3447{
3448 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003449 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003450}
3451
Daniel Malead79ae052013-08-07 21:54:09 +00003452Disassembler::HexImmediateStyle
3453TargetProperties::GetHexImmediateStyle () const
3454{
3455 const uint32_t idx = ePropertyHexImmediateStyle;
3456 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3457}
3458
Greg Claytonfd814c52013-08-13 01:42:25 +00003459MemoryModuleLoadLevel
3460TargetProperties::GetMemoryModuleLoadLevel() const
3461{
3462 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3463 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3464}
3465
Jason Molendaa4bea722014-02-14 05:06:49 +00003466bool
3467TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3468{
3469 const uint32_t idx = ePropertyTrapHandlerNames;
3470 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3471}
Greg Claytonfd814c52013-08-13 01:42:25 +00003472
Jason Molendaa4bea722014-02-14 05:06:49 +00003473void
3474TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3475{
3476 const uint32_t idx = ePropertyTrapHandlerNames;
3477 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3478}
Greg Clayton67cc0632012-08-22 17:17:09 +00003479
Enrico Granata560558e2015-02-11 02:35:39 +00003480bool
3481TargetProperties::GetDisplayRuntimeSupportValues () const
3482{
3483 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3484 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3485}
3486
3487void
3488TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3489{
3490 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3491 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3492}
3493
Ilia K8f37ca52015-02-13 14:31:06 +00003494const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003495TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003496{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003497 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003498 return m_launch_info;
3499}
3500
3501void
3502TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3503{
3504 m_launch_info = launch_info;
3505 SetArg0(launch_info.GetArg0());
3506 SetRunArguments(launch_info.GetArguments());
3507 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3508 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3509 if (input_file_action)
3510 {
3511 const char *input_path = input_file_action->GetPath();
3512 if (input_path)
3513 SetStandardInputPath(input_path);
3514 }
3515 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3516 if (output_file_action)
3517 {
3518 const char *output_path = output_file_action->GetPath();
3519 if (output_path)
3520 SetStandardOutputPath(output_path);
3521 }
3522 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3523 if (error_file_action)
3524 {
3525 const char *error_path = error_file_action->GetPath();
3526 if (error_path)
3527 SetStandardErrorPath(error_path);
3528 }
3529 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3530 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3531 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3532}
3533
3534void
3535TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3536{
3537 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3538 this_->m_launch_info.SetArg0(this_->GetArg0());
3539}
3540
3541void
3542TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3543{
3544 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3545 Args args;
3546 if (this_->GetRunArguments(args))
3547 this_->m_launch_info.GetArguments() = args;
3548}
3549
3550void
3551TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3552{
3553 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3554 Args args;
3555 if (this_->GetEnvironmentAsArgs(args))
3556 this_->m_launch_info.GetEnvironmentEntries() = args;
3557}
3558
3559void
3560TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3561{
3562 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3563 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath().GetPath().c_str(), true, false);
3564}
3565
3566void
3567TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3568{
3569 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3570 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath().GetPath().c_str(), false, true);
3571}
3572
3573void
3574TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3575{
3576 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3577 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath().GetPath().c_str(), false, true);
3578}
3579
3580void
3581TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
3582{
3583 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3584 if (this_->GetDetachOnError())
3585 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3586 else
3587 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3588}
3589
3590void
3591TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
3592{
3593 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3594 if (this_->GetDisableASLR())
3595 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3596 else
3597 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3598}
3599
3600void
3601TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
3602{
3603 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3604 if (this_->GetDisableSTDIO())
3605 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
3606 else
3607 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
3608}
Ilia Keb2c19a2015-03-10 21:59:55 +00003609
3610//----------------------------------------------------------------------
3611// Target::TargetEventData
3612//----------------------------------------------------------------------
3613
3614Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
3615 EventData (),
3616 m_target_sp (target_sp),
3617 m_module_list ()
3618{
3619}
3620
3621Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
3622 EventData (),
3623 m_target_sp (target_sp),
3624 m_module_list (module_list)
3625{
3626}
3627
3628Target::TargetEventData::~TargetEventData()
3629{
3630}
3631
3632const ConstString &
3633Target::TargetEventData::GetFlavorString ()
3634{
3635 static ConstString g_flavor ("Target::TargetEventData");
3636 return g_flavor;
3637}
3638
3639void
3640Target::TargetEventData::Dump (Stream *s) const
3641{
3642}
3643
3644const Target::TargetEventData *
3645Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3646{
3647 if (event_ptr)
3648 {
3649 const EventData *event_data = event_ptr->GetData();
3650 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3651 return static_cast <const TargetEventData *> (event_ptr->GetData());
3652 }
3653 return NULL;
3654}
3655
3656TargetSP
3657Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
3658{
3659 TargetSP target_sp;
3660 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
3661 if (event_data)
3662 target_sp = event_data->m_target_sp;
3663 return target_sp;
3664}
3665
3666ModuleList
3667Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
3668{
3669 ModuleList module_list;
3670 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
3671 if (event_data)
3672 module_list = event_data->m_module_list;
3673 return module_list;
3674}