blob: 13b5c6efc738293b51cc879da96ffd66253cb700 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000028#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000029#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000030#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000034#include "lldb/Expression/ClangASTSource.h"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000035#include "lldb/Expression/ClangPersistentVariables.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000036#include "lldb/Expression/ClangUserExpression.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000037#include "lldb/Expression/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000038#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000040#include "lldb/Interpreter/CommandInterpreter.h"
41#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000042#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000043#include "lldb/Interpreter/OptionValues.h"
44#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000045#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ObjectFile.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000047#include "lldb/Target/LanguageRuntime.h"
48#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000050#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000051#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000052#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000053#include "lldb/Target/Thread.h"
54#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
56using namespace lldb;
57using namespace lldb_private;
58
Jim Ingham4bddaeb2012-02-16 06:50:00 +000059ConstString &
60Target::GetStaticBroadcasterClass ()
61{
62 static ConstString class_name ("lldb.target");
63 return class_name;
64}
65
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066//----------------------------------------------------------------------
67// Target constructor
68//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000069Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000070 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000071 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000072 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000073 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000074 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000075 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000076 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000077 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000078 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079 m_breakpoint_list (false),
80 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000081 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000082 m_process_sp (),
83 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000085 m_scratch_ast_context_ap (),
86 m_scratch_ast_source_ap (),
87 m_ast_importer_ap (),
Zachary Turner32abc6e2015-03-03 19:23:09 +000088 m_persistent_variables (new ClangPersistentVariables),
Greg Clayton9585fbf2013-03-19 00:20:55 +000089 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000090 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000091 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000092 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000093 m_suppress_stop_hooks (false),
94 m_is_dummy_target(is_dummy_target)
95
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000097 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
98 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
99 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +0000100 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +0000101 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000102
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000103 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000104
Greg Clayton5160ce52013-03-27 23:08:40 +0000105 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000107 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000108 if (m_arch.IsValid())
109 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000110 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 +0000111 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000112}
113
Jim Ingham893c9322014-11-22 01:42:44 +0000114void
115Target::PrimeFromDummyTarget(Target *target)
116{
117 if (!target)
118 return;
119
120 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000121
122 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
123 {
124 if (breakpoint_sp->IsInternal())
125 continue;
126
127 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
128 AddBreakpoint (new_bp, false);
129 }
Jim Ingham893c9322014-11-22 01:42:44 +0000130}
131
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132//----------------------------------------------------------------------
133// Destructor
134//----------------------------------------------------------------------
135Target::~Target()
136{
Greg Clayton5160ce52013-03-27 23:08:40 +0000137 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000139 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 DeleteCurrentProcess ();
141}
142
143void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000144Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145{
Greg Clayton89411422010-10-08 00:21:05 +0000146// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000147 if (description_level != lldb::eDescriptionLevelBrief)
148 {
149 s->Indent();
150 s->PutCString("Target\n");
151 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000152 m_images.Dump(s);
153 m_breakpoint_list.Dump(s);
154 m_internal_breakpoint_list.Dump(s);
155 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000156 }
157 else
158 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000159 Module *exe_module = GetExecutableModulePointer();
160 if (exe_module)
161 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000162 else
163 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000164 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165}
166
167void
Greg Clayton90ba8112012-12-05 00:16:59 +0000168Target::CleanupProcess ()
169{
170 // Do any cleanup of the target we need to do between process instances.
171 // NB It is better to do this before destroying the process in case the
172 // clean up needs some help from the process.
173 m_breakpoint_list.ClearAllBreakpointSites();
174 m_internal_breakpoint_list.ClearAllBreakpointSites();
175 // Disable watchpoints just on the debugger side.
176 Mutex::Locker locker;
177 this->GetWatchpointList().GetListMutex(locker);
178 DisableAllWatchpoints(false);
179 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000180 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000181}
182
183void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184Target::DeleteCurrentProcess ()
185{
186 if (m_process_sp.get())
187 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000188 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 if (m_process_sp->IsAlive())
Jason Molendaede31932015-04-17 05:01:58 +0000190 m_process_sp->Destroy(false);
Jim Inghamd0a3e122011-02-16 17:54:55 +0000191
192 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193
Greg Clayton90ba8112012-12-05 00:16:59 +0000194 CleanupProcess ();
195
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196 m_process_sp.reset();
197 }
198}
199
200const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000201Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202{
203 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000204 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 return m_process_sp;
206}
207
208const lldb::ProcessSP &
209Target::GetProcessSP () const
210{
211 return m_process_sp;
212}
213
Greg Clayton3418c852011-08-10 02:10:13 +0000214void
215Target::Destroy()
216{
217 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000218 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000219 DeleteCurrentProcess ();
220 m_platform_sp.reset();
221 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000222 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000223 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000224 const bool notify = false;
225 m_breakpoint_list.RemoveAll(notify);
226 m_internal_breakpoint_list.RemoveAll(notify);
227 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000228 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000229 m_search_filter_sp.reset();
230 m_image_search_paths.Clear(notify);
Zachary Turner32abc6e2015-03-03 19:23:09 +0000231 m_persistent_variables->Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000232 m_stop_hooks.clear();
233 m_stop_hook_next_id = 0;
234 m_suppress_stop_hooks = false;
235}
236
237
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238BreakpointList &
239Target::GetBreakpointList(bool internal)
240{
241 if (internal)
242 return m_internal_breakpoint_list;
243 else
244 return m_breakpoint_list;
245}
246
247const BreakpointList &
248Target::GetBreakpointList(bool internal) const
249{
250 if (internal)
251 return m_internal_breakpoint_list;
252 else
253 return m_breakpoint_list;
254}
255
256BreakpointSP
257Target::GetBreakpointByID (break_id_t break_id)
258{
259 BreakpointSP bp_sp;
260
261 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
262 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
263 else
264 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
265
266 return bp_sp;
267}
268
269BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000270Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000271 const FileSpecList *source_file_spec_list,
272 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000273 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000274 bool hardware,
275 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276{
Jim Ingham87df91b2011-09-23 00:54:11 +0000277 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000278 if (move_to_nearest_code == eLazyBoolCalculate)
279 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
280 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex, !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000281 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000282}
283
284
285BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000286Target::CreateBreakpoint (const FileSpecList *containingModules,
287 const FileSpec &file,
288 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000289 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000290 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000291 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000292 bool hardware,
293 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000294{
Greg Clayton1f746072012-08-29 21:13:06 +0000295 if (check_inlines == eLazyBoolCalculate)
296 {
297 const InlineStrategy inline_strategy = GetInlineStrategy();
298 switch (inline_strategy)
299 {
300 case eInlineBreakpointsNever:
301 check_inlines = eLazyBoolNo;
302 break;
303
304 case eInlineBreakpointsHeaders:
305 if (file.IsSourceImplementationFile())
306 check_inlines = eLazyBoolNo;
307 else
308 check_inlines = eLazyBoolYes;
309 break;
310
311 case eInlineBreakpointsAlways:
312 check_inlines = eLazyBoolYes;
313 break;
314 }
315 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000316 SearchFilterSP filter_sp;
317 if (check_inlines == eLazyBoolNo)
318 {
319 // Not checking for inlines, we are looking only for matching compile units
320 FileSpecList compile_unit_list;
321 compile_unit_list.Append (file);
322 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
323 }
324 else
325 {
326 filter_sp = GetSearchFilterForModuleList (containingModules);
327 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000328 if (skip_prologue == eLazyBoolCalculate)
329 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000330 if (move_to_nearest_code == eLazyBoolCalculate)
331 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000332
Greg Clayton1f746072012-08-29 21:13:06 +0000333 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
334 file,
335 line_no,
336 check_inlines,
Ilia K055ad9b2015-05-18 13:41:01 +0000337 skip_prologue,
338 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000339 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340}
341
342
343BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000344Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 Address so_addr;
347 // Attempt to resolve our load address if possible, though it is ok if
348 // it doesn't resolve to section/offset.
349
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000350 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000351 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000352 if (!so_addr.IsValid())
353 {
354 // The address didn't resolve, so just set this as an absolute address
355 so_addr.SetOffset (addr);
356 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000357 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 return bp_sp;
359}
360
361BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000362Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000364 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000366 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367}
368
369BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000370Target::CreateBreakpoint (const FileSpecList *containingModules,
371 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000372 const char *func_name,
373 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000374 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000375 bool internal,
376 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000378 BreakpointSP bp_sp;
379 if (func_name)
380 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000381 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000382
383 if (skip_prologue == eLazyBoolCalculate)
384 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
385
Greg Claytond16e1e52011-07-12 17:06:17 +0000386 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
387 func_name,
388 func_name_type_mask,
389 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000390 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000391 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000392 }
393 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394}
395
Jim Inghamfab10e82012-03-06 00:37:27 +0000396lldb::BreakpointSP
397Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000398 const FileSpecList *containingSourceFiles,
399 const std::vector<std::string> &func_names,
400 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000401 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000402 bool internal,
403 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000404{
405 BreakpointSP bp_sp;
406 size_t num_names = func_names.size();
407 if (num_names > 0)
408 {
409 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000410
411 if (skip_prologue == eLazyBoolCalculate)
412 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
413
414 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000415 func_names,
416 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000417 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000418 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000419 }
420 return bp_sp;
421}
422
Jim Ingham133e0fb2012-03-03 02:05:11 +0000423BreakpointSP
424Target::CreateBreakpoint (const FileSpecList *containingModules,
425 const FileSpecList *containingSourceFiles,
426 const char *func_names[],
427 size_t num_names,
428 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000429 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000430 bool internal,
431 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000432{
433 BreakpointSP bp_sp;
434 if (num_names > 0)
435 {
436 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
437
Greg Clayton03da4cc2013-04-19 21:31:16 +0000438 if (skip_prologue == eLazyBoolCalculate)
439 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
440
441 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000442 func_names,
443 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000444 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000445 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000446 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000447 }
448 return bp_sp;
449}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450
451SearchFilterSP
452Target::GetSearchFilterForModule (const FileSpec *containingModule)
453{
454 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 if (containingModule != NULL)
456 {
457 // TODO: We should look into sharing module based search filters
458 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000459 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 }
461 else
462 {
463 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000464 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 filter_sp = m_search_filter_sp;
466 }
467 return filter_sp;
468}
469
Jim Ingham969795f2011-09-21 01:17:13 +0000470SearchFilterSP
471Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
472{
473 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000474 if (containingModules && containingModules->GetSize() != 0)
475 {
476 // TODO: We should look into sharing module based search filters
477 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000478 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000479 }
480 else
481 {
482 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000483 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000484 filter_sp = m_search_filter_sp;
485 }
486 return filter_sp;
487}
488
Jim Ingham87df91b2011-09-23 00:54:11 +0000489SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000490Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
491 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000492{
493 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
494 return GetSearchFilterForModuleList(containingModules);
495
496 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000497 if (containingModules == NULL)
498 {
499 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
500 // but that will take a little reworking.
501
Greg Claytone1cd1be2012-01-29 20:56:30 +0000502 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000503 }
504 else
505 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000506 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000507 }
508 return filter_sp;
509}
510
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000512Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000513 const FileSpecList *containingSourceFiles,
514 RegularExpression &func_regex,
515 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000516 bool internal,
517 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000518{
Jim Ingham87df91b2011-09-23 00:54:11 +0000519 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000520 bool skip =
521 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
522 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000523 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
524 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000525 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526
Jim Ingham1460e4b2014-01-10 23:46:59 +0000527 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528}
529
Jim Ingham219ba192012-03-05 04:47:34 +0000530lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000531Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal, Args *additional_args, Error *error)
Jim Ingham219ba192012-03-05 04:47:34 +0000532{
Jim Inghama72b31c2015-04-22 19:42:18 +0000533 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
534 if (exc_bkpt_sp && additional_args)
535 {
536 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
537 if (precondition_sp && additional_args)
538 {
539 if (error)
540 *error = precondition_sp->ConfigurePrecondition(*additional_args);
541 else
542 precondition_sp->ConfigurePrecondition(*additional_args);
543 }
544 }
545 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000546}
Jim Inghama72b31c2015-04-22 19:42:18 +0000547
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000549Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550{
551 BreakpointSP bp_sp;
552 if (filter_sp && resolver_sp)
553 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000554 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000556 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000558 return bp_sp;
559}
560
561void
562Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
563{
564 if (!bp_sp)
565 return;
566 if (internal)
567 m_internal_breakpoint_list.Add (bp_sp, false);
568 else
569 m_breakpoint_list.Add (bp_sp, true);
570
571 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
572 if (log)
573 {
574 StreamString s;
575 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
576 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
577 }
578
579 bp_sp->ResolveBreakpoint();
580
581 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000582 {
583 m_last_created_breakpoint = bp_sp;
584 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
Johnny Chen86364b42011-09-20 23:28:55 +0000587bool
588Target::ProcessIsValid()
589{
590 return (m_process_sp && m_process_sp->IsAlive());
591}
592
Johnny Chenb90827e2012-06-04 23:19:54 +0000593static bool
594CheckIfWatchpointsExhausted(Target *target, Error &error)
595{
596 uint32_t num_supported_hardware_watchpoints;
597 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
598 if (rc.Success())
599 {
600 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
601 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
602 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
603 num_supported_hardware_watchpoints);
604 }
605 return false;
606}
607
Johnny Chen01a67862011-10-14 00:42:25 +0000608// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000609// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000610WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000611Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000612{
Greg Clayton5160ce52013-03-27 23:08:40 +0000613 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000614 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000615 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000616 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000617
Johnny Chen01a67862011-10-14 00:42:25 +0000618 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000619 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000620 {
621 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000622 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000623 }
Jim Inghamc6462312013-06-18 21:52:48 +0000624
Johnny Chen45e541f2011-09-14 22:20:15 +0000625 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000626 {
627 if (size == 0)
628 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
629 else
Daniel Malead01b2952012-11-29 21:49:15 +0000630 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000631 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000632 }
Jim Inghamc6462312013-06-18 21:52:48 +0000633
634 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
635 {
636 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
637 }
Johnny Chen7313a642011-09-13 01:15:36 +0000638
Johnny Chen01a67862011-10-14 00:42:25 +0000639 // Currently we only support one watchpoint per address, with total number
640 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000641
642 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000643 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 +0000644 Mutex::Locker locker;
645 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000646 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000647 if (matched_sp)
648 {
Johnny Chen0c406372011-09-14 20:23:45 +0000649 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000650 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000651 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
652 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000653 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000654 if (size == old_size && kind == old_type)
655 {
Johnny Chen01a67862011-10-14 00:42:25 +0000656 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000657 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000658 }
659 else
660 {
Johnny Chen01a67862011-10-14 00:42:25 +0000661 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000662 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
663 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000664 }
Johnny Chen3c532582011-09-13 23:29:31 +0000665 }
666
Jason Molenda727e3922012-12-05 23:07:34 +0000667 if (!wp_sp)
668 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000669 wp_sp.reset(new Watchpoint(*this, addr, size, type));
670 wp_sp->SetWatchpointType(kind, notify);
671 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000672 }
Johnny Chen0c406372011-09-14 20:23:45 +0000673
Jim Ingham1b5792e2012-12-18 02:03:49 +0000674 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000675 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000676 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
677 __FUNCTION__,
678 error.Success() ? "succeeded" : "failed",
679 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000680
Jason Molenda727e3922012-12-05 23:07:34 +0000681 if (error.Fail())
682 {
Johnny Chen41b77262012-03-26 22:00:10 +0000683 // Enabling the watchpoint on the device side failed.
684 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000685 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000686 // See if we could provide more helpful error message.
687 if (!CheckIfWatchpointsExhausted(this, error))
688 {
689 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000690 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000691 }
Johnny Chen01a67862011-10-14 00:42:25 +0000692 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000693 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000694 else
Johnny Chen01a67862011-10-14 00:42:25 +0000695 m_last_created_watchpoint = wp_sp;
696 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000697}
698
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699void
700Target::RemoveAllBreakpoints (bool internal_also)
701{
Greg Clayton5160ce52013-03-27 23:08:40 +0000702 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 if (log)
704 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
705
Greg Clayton9fed0d82010-07-23 23:33:17 +0000706 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000708 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000709
710 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711}
712
713void
714Target::DisableAllBreakpoints (bool internal_also)
715{
Greg Clayton5160ce52013-03-27 23:08:40 +0000716 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 if (log)
718 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
719
720 m_breakpoint_list.SetEnabledAll (false);
721 if (internal_also)
722 m_internal_breakpoint_list.SetEnabledAll (false);
723}
724
725void
726Target::EnableAllBreakpoints (bool internal_also)
727{
Greg Clayton5160ce52013-03-27 23:08:40 +0000728 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 if (log)
730 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
731
732 m_breakpoint_list.SetEnabledAll (true);
733 if (internal_also)
734 m_internal_breakpoint_list.SetEnabledAll (true);
735}
736
737bool
738Target::RemoveBreakpointByID (break_id_t break_id)
739{
Greg Clayton5160ce52013-03-27 23:08:40 +0000740 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741 if (log)
742 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
743
744 if (DisableBreakpointByID (break_id))
745 {
746 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000747 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000749 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000750 if (m_last_created_breakpoint)
751 {
752 if (m_last_created_breakpoint->GetID() == break_id)
753 m_last_created_breakpoint.reset();
754 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000755 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000756 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 return true;
758 }
759 return false;
760}
761
762bool
763Target::DisableBreakpointByID (break_id_t break_id)
764{
Greg Clayton5160ce52013-03-27 23:08:40 +0000765 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766 if (log)
767 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
768
769 BreakpointSP bp_sp;
770
771 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
772 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
773 else
774 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
775 if (bp_sp)
776 {
777 bp_sp->SetEnabled (false);
778 return true;
779 }
780 return false;
781}
782
783bool
784Target::EnableBreakpointByID (break_id_t break_id)
785{
Greg Clayton5160ce52013-03-27 23:08:40 +0000786 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000787 if (log)
788 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
789 __FUNCTION__,
790 break_id,
791 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
792
793 BreakpointSP bp_sp;
794
795 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
796 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
797 else
798 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
799
800 if (bp_sp)
801 {
802 bp_sp->SetEnabled (true);
803 return true;
804 }
805 return false;
806}
807
Johnny Chenedf50372011-09-23 21:21:43 +0000808// The flag 'end_to_end', default to true, signifies that the operation is
809// performed end to end, for both the debugger and the debuggee.
810
Johnny Chen01a67862011-10-14 00:42:25 +0000811// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
812// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000813bool
Johnny Chen01a67862011-10-14 00:42:25 +0000814Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000815{
Greg Clayton5160ce52013-03-27 23:08:40 +0000816 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000817 if (log)
818 log->Printf ("Target::%s\n", __FUNCTION__);
819
Johnny Chenedf50372011-09-23 21:21:43 +0000820 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000821 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000822 return true;
823 }
824
825 // Otherwise, it's an end to end operation.
826
Johnny Chen86364b42011-09-20 23:28:55 +0000827 if (!ProcessIsValid())
828 return false;
829
Johnny Chen01a67862011-10-14 00:42:25 +0000830 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000831 for (size_t i = 0; i < num_watchpoints; ++i)
832 {
Johnny Chen01a67862011-10-14 00:42:25 +0000833 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
834 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000835 return false;
836
Johnny Chen01a67862011-10-14 00:42:25 +0000837 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000838 if (rc.Fail())
839 return false;
840 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000841 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000842 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000843 return true; // Success!
844}
845
Johnny Chen01a67862011-10-14 00:42:25 +0000846// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
847// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000848bool
Johnny Chen01a67862011-10-14 00:42:25 +0000849Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000850{
Greg Clayton5160ce52013-03-27 23:08:40 +0000851 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000852 if (log)
853 log->Printf ("Target::%s\n", __FUNCTION__);
854
Johnny Chenedf50372011-09-23 21:21:43 +0000855 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000856 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000857 return true;
858 }
859
860 // Otherwise, it's an end to end operation.
861
Johnny Chen86364b42011-09-20 23:28:55 +0000862 if (!ProcessIsValid())
863 return false;
864
Johnny Chen01a67862011-10-14 00:42:25 +0000865 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000866 for (size_t i = 0; i < num_watchpoints; ++i)
867 {
Johnny Chen01a67862011-10-14 00:42:25 +0000868 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
869 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000870 return false;
871
Johnny Chen01a67862011-10-14 00:42:25 +0000872 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000873 if (rc.Fail())
874 return false;
875 }
Johnny Chen86364b42011-09-20 23:28:55 +0000876 return true; // Success!
877}
878
Johnny Chen01a67862011-10-14 00:42:25 +0000879// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
880// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000881bool
Johnny Chen01a67862011-10-14 00:42:25 +0000882Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000883{
Greg Clayton5160ce52013-03-27 23:08:40 +0000884 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000885 if (log)
886 log->Printf ("Target::%s\n", __FUNCTION__);
887
Johnny Chenedf50372011-09-23 21:21:43 +0000888 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000889 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000890 return true;
891 }
892
893 // Otherwise, it's an end to end operation.
894
Johnny Chen86364b42011-09-20 23:28:55 +0000895 if (!ProcessIsValid())
896 return false;
897
Johnny Chen01a67862011-10-14 00:42:25 +0000898 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000899 for (size_t i = 0; i < num_watchpoints; ++i)
900 {
Johnny Chen01a67862011-10-14 00:42:25 +0000901 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
902 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000903 return false;
904
Johnny Chen01a67862011-10-14 00:42:25 +0000905 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000906 if (rc.Fail())
907 return false;
908 }
Johnny Chen86364b42011-09-20 23:28:55 +0000909 return true; // Success!
910}
911
Johnny Chena4d6bc92012-02-25 06:44:30 +0000912// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
913bool
914Target::ClearAllWatchpointHitCounts ()
915{
Greg Clayton5160ce52013-03-27 23:08:40 +0000916 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000917 if (log)
918 log->Printf ("Target::%s\n", __FUNCTION__);
919
920 size_t num_watchpoints = m_watchpoint_list.GetSize();
921 for (size_t i = 0; i < num_watchpoints; ++i)
922 {
923 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
924 if (!wp_sp)
925 return false;
926
927 wp_sp->ResetHitCount();
928 }
929 return true; // Success!
930}
931
Enrico Granata5e3fe042015-02-11 00:37:54 +0000932// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
933bool
934Target::ClearAllWatchpointHistoricValues ()
935{
936 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
937 if (log)
938 log->Printf ("Target::%s\n", __FUNCTION__);
939
940 size_t num_watchpoints = m_watchpoint_list.GetSize();
941 for (size_t i = 0; i < num_watchpoints; ++i)
942 {
943 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
944 if (!wp_sp)
945 return false;
946
947 wp_sp->ResetHistoricValues();
948 }
949 return true; // Success!
950}
951
Johnny Chen01a67862011-10-14 00:42:25 +0000952// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000953// during these operations.
954bool
Johnny Chen01a67862011-10-14 00:42:25 +0000955Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000956{
Greg Clayton5160ce52013-03-27 23:08:40 +0000957 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000958 if (log)
959 log->Printf ("Target::%s\n", __FUNCTION__);
960
961 if (!ProcessIsValid())
962 return false;
963
Johnny Chen01a67862011-10-14 00:42:25 +0000964 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000965 for (size_t i = 0; i < num_watchpoints; ++i)
966 {
Johnny Chen01a67862011-10-14 00:42:25 +0000967 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
968 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000969 return false;
970
Johnny Chen01a67862011-10-14 00:42:25 +0000971 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000972 }
973 return true; // Success!
974}
975
Johnny Chen01a67862011-10-14 00:42:25 +0000976// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000977bool
Johnny Chen01a67862011-10-14 00:42:25 +0000978Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000979{
Greg Clayton5160ce52013-03-27 23:08:40 +0000980 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000981 if (log)
982 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
983
984 if (!ProcessIsValid())
985 return false;
986
Johnny Chen01a67862011-10-14 00:42:25 +0000987 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
988 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000989 {
Johnny Chen01a67862011-10-14 00:42:25 +0000990 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000991 if (rc.Success())
992 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000993
Johnny Chenf04ee932011-09-22 18:04:58 +0000994 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000995 }
996 return false;
997}
998
Johnny Chen01a67862011-10-14 00:42:25 +0000999// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001000bool
Johnny Chen01a67862011-10-14 00:42:25 +00001001Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001002{
Greg Clayton5160ce52013-03-27 23:08:40 +00001003 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001004 if (log)
1005 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1006
1007 if (!ProcessIsValid())
1008 return false;
1009
Johnny Chen01a67862011-10-14 00:42:25 +00001010 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1011 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001012 {
Johnny Chen01a67862011-10-14 00:42:25 +00001013 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001014 if (rc.Success())
1015 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001016
Johnny Chenf04ee932011-09-22 18:04:58 +00001017 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001018 }
1019 return false;
1020}
1021
Johnny Chen01a67862011-10-14 00:42:25 +00001022// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001023bool
Johnny Chen01a67862011-10-14 00:42:25 +00001024Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001025{
Greg Clayton5160ce52013-03-27 23:08:40 +00001026 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001027 if (log)
1028 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1029
Jim Inghamb0b45132013-07-02 02:09:46 +00001030 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1031 if (watch_to_remove_sp == m_last_created_watchpoint)
1032 m_last_created_watchpoint.reset();
1033
Johnny Chen01a67862011-10-14 00:42:25 +00001034 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001035 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001036 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001037 return true;
1038 }
1039 return false;
1040}
1041
Johnny Chen01a67862011-10-14 00:42:25 +00001042// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001043bool
Johnny Chen01a67862011-10-14 00:42:25 +00001044Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001045{
Greg Clayton5160ce52013-03-27 23:08:40 +00001046 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001047 if (log)
1048 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1049
1050 if (!ProcessIsValid())
1051 return false;
1052
Johnny Chen01a67862011-10-14 00:42:25 +00001053 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1054 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001055 {
Johnny Chen01a67862011-10-14 00:42:25 +00001056 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001057 return true;
1058 }
1059 return false;
1060}
1061
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001062ModuleSP
1063Target::GetExecutableModule ()
1064{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001065 // search for the first executable in the module list
1066 for (size_t i = 0; i < m_images.GetSize(); ++i)
1067 {
1068 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1069 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1070 if (obj == nullptr)
1071 continue;
1072 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1073 return module_sp;
1074 }
1075 // as fall back return the first module loaded
1076 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001077}
1078
1079Module*
1080Target::GetExecutableModulePointer ()
1081{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001082 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001083}
1084
Enrico Granata17598482012-11-08 02:22:02 +00001085static void
1086LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1087{
1088 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001089 StreamString feedback_stream;
1090 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001091 {
Enrico Granata97303392013-05-21 00:00:30 +00001092 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001093 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001094 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1095 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001096 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001097 if (feedback_stream.GetSize())
1098 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1099 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001100}
1101
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001102void
Greg Claytonb35db632013-11-09 00:03:31 +00001103Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104{
Greg Claytonb35db632013-11-09 00:03:31 +00001105 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001106 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001107 m_images.Clear();
1108 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001109 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001110 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001111}
1112
1113void
Greg Claytonb35db632013-11-09 00:03:31 +00001114Target::DidExec ()
1115{
1116 // When a process exec's we need to know about it so we can do some cleanup.
1117 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1118 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1119}
1120
1121void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001122Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1123{
1124 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001125 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126
1127 if (executable_sp.get())
1128 {
1129 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001130 "Target::SetExecutableModule (executable = '%s')",
1131 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001133 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134
Jim Ingham5aee1622010-08-09 23:31:02 +00001135 // 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 +00001136 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001137 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001138 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001139 if (log)
1140 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1141 }
1142
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001143 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001144 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001146 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147 {
1148 executable_objfile->GetDependentModules(dependent_files);
1149 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1150 {
Greg Claytonded470d2011-03-19 01:12:21 +00001151 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1152 FileSpec platform_dependent_file_spec;
1153 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001154 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001155 else
1156 platform_dependent_file_spec = dependent_file_spec;
1157
Greg Claytonb9a01b32012-02-26 05:51:37 +00001158 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1159 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160 if (image_module_sp.get())
1161 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162 ObjectFile *objfile = image_module_sp->GetObjectFile();
1163 if (objfile)
1164 objfile->GetDependentModules(dependent_files);
1165 }
1166 }
1167 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001168 }
1169}
1170
1171
Jim Ingham5aee1622010-08-09 23:31:02 +00001172bool
1173Target::SetArchitecture (const ArchSpec &arch_spec)
1174{
Greg Clayton5160ce52013-03-27 23:08:40 +00001175 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001176 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001177 {
Greg Clayton70512312012-05-08 01:45:38 +00001178 // If we haven't got a valid arch spec, or the architectures are
1179 // compatible, so just update the architecture. Architectures can be
1180 // equal, yet the triple OS and vendor might change, so we need to do
1181 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001182 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001183 if (log)
1184 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 +00001185 return true;
1186 }
1187 else
1188 {
1189 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001190 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001191 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 +00001192 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001193 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001194
Greg Claytonb35db632013-11-09 00:03:31 +00001195 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001196 // Need to do something about unsetting breakpoints.
1197
1198 if (executable_sp)
1199 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001200 if (log)
1201 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 +00001202 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1203 Error error = ModuleList::GetSharedModule (module_spec,
1204 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001205 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001206 NULL,
1207 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001208
1209 if (!error.Fail() && executable_sp)
1210 {
1211 SetExecutableModule (executable_sp, true);
1212 return true;
1213 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001214 }
1215 }
Greg Clayton70512312012-05-08 01:45:38 +00001216 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001217}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001219bool
1220Target::MergeArchitecture (const ArchSpec &arch_spec)
1221{
1222 if (arch_spec.IsValid())
1223 {
1224 if (m_arch.IsCompatibleMatch(arch_spec))
1225 {
1226 // The current target arch is compatible with "arch_spec", see if we
1227 // can improve our current architecture using bits from "arch_spec"
1228
1229 // Merge bits from arch_spec into "merged_arch" and set our architecture
1230 ArchSpec merged_arch (m_arch);
1231 merged_arch.MergeFrom (arch_spec);
1232 return SetArchitecture(merged_arch);
1233 }
1234 else
1235 {
1236 // The new architecture is different, we just need to replace it
1237 return SetArchitecture(arch_spec);
1238 }
1239 }
1240 return false;
1241}
1242
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243void
Enrico Granataefe637d2012-11-08 19:16:03 +00001244Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001245{
1246}
1247
1248void
Enrico Granataefe637d2012-11-08 19:16:03 +00001249Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250{
1251 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001252 if (m_valid)
1253 {
1254 ModuleList my_module_list;
1255 my_module_list.Append(module_sp);
1256 LoadScriptingResourceForModule(module_sp, this);
1257 ModulesDidLoad (my_module_list);
1258 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259}
1260
1261void
Enrico Granataefe637d2012-11-08 19:16:03 +00001262Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001263{
1264 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001265 if (m_valid)
1266 {
1267 ModuleList my_module_list;
1268 my_module_list.Append(module_sp);
1269 ModulesDidUnload (my_module_list, false);
1270 }
Enrico Granata17598482012-11-08 02:22:02 +00001271}
1272
1273void
Enrico Granataefe637d2012-11-08 19:16:03 +00001274Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275{
Jim Inghame716ae02011-08-03 01:00:06 +00001276 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001277 if (m_valid)
1278 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279}
1280
1281void
1282Target::ModulesDidLoad (ModuleList &module_list)
1283{
Greg Clayton23f8c952014-03-24 23:10:19 +00001284 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001285 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001286 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001287 if (m_process_sp)
1288 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001289 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001290 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001291 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001292 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001293}
1294
1295void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001296Target::SymbolsDidLoad (ModuleList &module_list)
1297{
Greg Clayton23f8c952014-03-24 23:10:19 +00001298 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001299 {
Jim Ingham31caf982013-06-04 23:01:35 +00001300 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001301 {
Jim Ingham31caf982013-06-04 23:01:35 +00001302 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1303 if (runtime)
1304 {
1305 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1306 objc_runtime->SymbolsDidLoad(module_list);
1307 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001308 }
Jim Ingham31caf982013-06-04 23:01:35 +00001309
Greg Clayton095eeaa2013-11-05 23:28:00 +00001310 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001311 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001312 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001313}
1314
1315void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001316Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317{
Greg Clayton23f8c952014-03-24 23:10:19 +00001318 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001319 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001320 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001321 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001322 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001323 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001324}
1325
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001326bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001327Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001328{
Greg Clayton67cc0632012-08-22 17:17:09 +00001329 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001330 {
1331 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001332 ModuleSpec module_spec (module_file_spec);
1333 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001334
1335 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1336 // black list.
1337 if (num_modules > 0)
1338 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001339 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001340 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001341 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001342 return false;
1343 }
1344 return true;
1345 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001346 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001347 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001348}
1349
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001350bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001351Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001352{
Greg Clayton67cc0632012-08-22 17:17:09 +00001353 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001354 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001355 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001356 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001357 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001358 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001359}
1360
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361size_t
Greg Claytondb598232011-01-07 01:57:07 +00001362Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1363{
Greg Claytone72dfb32012-02-24 01:59:29 +00001364 SectionSP section_sp (addr.GetSection());
1365 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001366 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001367 // 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 +00001368 if (section_sp->IsEncrypted())
1369 {
Greg Clayton57f06302012-05-25 17:05:55 +00001370 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001371 return 0;
1372 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001373 ModuleSP module_sp (section_sp->GetModule());
1374 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001375 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001376 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1377 if (objfile)
1378 {
1379 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1380 addr.GetOffset(),
1381 dst,
1382 dst_len);
1383 if (bytes_read > 0)
1384 return bytes_read;
1385 else
1386 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1387 }
Greg Claytondb598232011-01-07 01:57:07 +00001388 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001389 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001390 }
1391 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001392 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001393 }
1394 else
Greg Claytondb598232011-01-07 01:57:07 +00001395 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001396
Greg Claytondb598232011-01-07 01:57:07 +00001397 return 0;
1398}
1399
1400size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001401Target::ReadMemory (const Address& addr,
1402 bool prefer_file_cache,
1403 void *dst,
1404 size_t dst_len,
1405 Error &error,
1406 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001407{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001409
Enrico Granata9128ee22011-09-06 19:20:51 +00001410 // if we end up reading this from process memory, we will fill this
1411 // with the actual load address
1412 if (load_addr_ptr)
1413 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1414
Greg Claytondb598232011-01-07 01:57:07 +00001415 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001416
1417 addr_t load_addr = LLDB_INVALID_ADDRESS;
1418 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001419 Address resolved_addr;
1420 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001421 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001422 SectionLoadList &section_load_list = GetSectionLoadList();
1423 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001424 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001425 // No sections are loaded, so we must assume we are not running
1426 // yet and anything we are given is a file address.
1427 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1428 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001429 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001430 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001431 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001432 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001433 // we have manually loaded some sections with "target modules load ..."
1434 // or because we have have a live process that has sections loaded
1435 // through the dynamic loader
1436 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001437 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001438 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001439 }
Greg Clayton357132e2011-03-26 19:14:58 +00001440 if (!resolved_addr.IsValid())
1441 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001442
Greg Claytonc749eb82011-07-11 05:12:02 +00001443
Greg Claytondb598232011-01-07 01:57:07 +00001444 if (prefer_file_cache)
1445 {
1446 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1447 if (bytes_read > 0)
1448 return bytes_read;
1449 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001450
Johnny Chen86364b42011-09-20 23:28:55 +00001451 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001452 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001453 if (load_addr == LLDB_INVALID_ADDRESS)
1454 load_addr = resolved_addr.GetLoadAddress (this);
1455
Greg Claytondda4f7b2010-06-30 23:03:03 +00001456 if (load_addr == LLDB_INVALID_ADDRESS)
1457 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001458 ModuleSP addr_module_sp (resolved_addr.GetModule());
1459 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001460 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001461 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001462 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001463 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001464 else
Daniel Malead01b2952012-11-29 21:49:15 +00001465 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001466 }
1467 else
1468 {
Greg Claytondb598232011-01-07 01:57:07 +00001469 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001470 if (bytes_read != dst_len)
1471 {
1472 if (error.Success())
1473 {
1474 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001475 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 else
Daniel Malead01b2952012-11-29 21:49:15 +00001477 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 +00001478 }
1479 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001480 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001481 {
1482 if (load_addr_ptr)
1483 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001484 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001485 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001486 // If the address is not section offset we have an address that
1487 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001488 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001489 // more we can do. If it is section offset, we might be able to
1490 // read cached memory from the object file.
1491 if (!resolved_addr.IsSectionOffset())
1492 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001493 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001494 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001495
Greg Claytonc749eb82011-07-11 05:12:02 +00001496 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001497 {
Greg Claytondb598232011-01-07 01:57:07 +00001498 // If we didn't already try and read from the object file cache, then
1499 // try it after failing to read from the process.
1500 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001501 }
1502 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503}
1504
Greg Claytond16e1e52011-07-12 17:06:17 +00001505size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001506Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1507{
1508 char buf[256];
1509 out_str.clear();
1510 addr_t curr_addr = addr.GetLoadAddress(this);
1511 Address address(addr);
1512 while (1)
1513 {
1514 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1515 if (length == 0)
1516 break;
1517 out_str.append(buf, length);
1518 // If we got "length - 1" bytes, we didn't get the whole C string, we
1519 // need to read some more characters
1520 if (length == sizeof(buf) - 1)
1521 curr_addr += length;
1522 else
1523 break;
1524 address = Address(curr_addr);
1525 }
1526 return out_str.size();
1527}
1528
1529
1530size_t
1531Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1532{
1533 size_t total_cstr_len = 0;
1534 if (dst && dst_max_len)
1535 {
1536 result_error.Clear();
1537 // NULL out everything just to be safe
1538 memset (dst, 0, dst_max_len);
1539 Error error;
1540 addr_t curr_addr = addr.GetLoadAddress(this);
1541 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001542
1543 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1544 // think this really needs to be tied to the memory cache subsystem's
1545 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001546 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001547
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001548 size_t bytes_left = dst_max_len - 1;
1549 char *curr_dst = dst;
1550
1551 while (bytes_left > 0)
1552 {
1553 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1554 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1555 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1556
1557 if (bytes_read == 0)
1558 {
1559 result_error = error;
1560 dst[total_cstr_len] = '\0';
1561 break;
1562 }
1563 const size_t len = strlen(curr_dst);
1564
1565 total_cstr_len += len;
1566
1567 if (len < bytes_to_read)
1568 break;
1569
1570 curr_dst += bytes_read;
1571 curr_addr += bytes_read;
1572 bytes_left -= bytes_read;
1573 address = Address(curr_addr);
1574 }
1575 }
1576 else
1577 {
1578 if (dst == NULL)
1579 result_error.SetErrorString("invalid arguments");
1580 else
1581 result_error.Clear();
1582 }
1583 return total_cstr_len;
1584}
1585
1586size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001587Target::ReadScalarIntegerFromMemory (const Address& addr,
1588 bool prefer_file_cache,
1589 uint32_t byte_size,
1590 bool is_signed,
1591 Scalar &scalar,
1592 Error &error)
1593{
1594 uint64_t uval;
1595
1596 if (byte_size <= sizeof(uval))
1597 {
1598 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1599 if (bytes_read == byte_size)
1600 {
1601 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001602 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001603 if (byte_size <= 4)
1604 scalar = data.GetMaxU32 (&offset, byte_size);
1605 else
1606 scalar = data.GetMaxU64 (&offset, byte_size);
1607
1608 if (is_signed)
1609 scalar.SignExtend(byte_size * 8);
1610 return bytes_read;
1611 }
1612 }
1613 else
1614 {
1615 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1616 }
1617 return 0;
1618}
1619
1620uint64_t
1621Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1622 bool prefer_file_cache,
1623 size_t integer_byte_size,
1624 uint64_t fail_value,
1625 Error &error)
1626{
1627 Scalar scalar;
1628 if (ReadScalarIntegerFromMemory (addr,
1629 prefer_file_cache,
1630 integer_byte_size,
1631 false,
1632 scalar,
1633 error))
1634 return scalar.ULongLong(fail_value);
1635 return fail_value;
1636}
1637
1638bool
1639Target::ReadPointerFromMemory (const Address& addr,
1640 bool prefer_file_cache,
1641 Error &error,
1642 Address &pointer_addr)
1643{
1644 Scalar scalar;
1645 if (ReadScalarIntegerFromMemory (addr,
1646 prefer_file_cache,
1647 m_arch.GetAddressByteSize(),
1648 false,
1649 scalar,
1650 error))
1651 {
1652 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1653 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1654 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001655 SectionLoadList &section_load_list = GetSectionLoadList();
1656 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001657 {
1658 // No sections are loaded, so we must assume we are not running
1659 // yet and anything we are given is a file address.
1660 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1661 }
1662 else
1663 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001664 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001665 // we have manually loaded some sections with "target modules load ..."
1666 // or because we have have a live process that has sections loaded
1667 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001668 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001669 }
1670 // We weren't able to resolve the pointer value, so just return
1671 // an address with no section
1672 if (!pointer_addr.IsValid())
1673 pointer_addr.SetOffset (pointer_vm_addr);
1674 return true;
1675
1676 }
1677 }
1678 return false;
1679}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680
1681ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001682Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001684 ModuleSP module_sp;
1685
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686 Error error;
1687
Jim Ingham4a94c912012-05-17 18:38:42 +00001688 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1689 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001690
Jim Ingham4a94c912012-05-17 18:38:42 +00001691 if (module_spec.GetUUID().IsValid())
1692 module_sp = m_images.FindFirstModule(module_spec);
1693
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001694 if (!module_sp)
1695 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001696 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1697 bool did_create_module = false;
1698
1699 // If there are image search path entries, try to use them first to acquire a suitable image.
1700 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001701 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001702 ModuleSpec transformed_spec (module_spec);
1703 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1704 {
1705 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1706 error = ModuleList::GetSharedModule (transformed_spec,
1707 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001708 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001709 &old_module_sp,
1710 &did_create_module);
1711 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001712 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001713
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001714 if (!module_sp)
1715 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001716 // If we have a UUID, we can check our global shared module list in case
1717 // we already have it. If we don't have a valid UUID, then we can't since
1718 // the path in "module_spec" will be a platform path, and we will need to
1719 // let the platform find that file. For example, we could be asking for
1720 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1721 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1722 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1723 // cache.
1724 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001725 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001726 // We have a UUID, it is OK to check the global module list...
1727 error = ModuleList::GetSharedModule (module_spec,
1728 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001729 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001730 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001731 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001732 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001733
1734 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001735 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001736 // The platform is responsible for finding and caching an appropriate
1737 // module in the shared module cache.
1738 if (m_platform_sp)
1739 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001740 error = m_platform_sp->GetSharedModule (module_spec,
1741 m_process_sp.get(),
1742 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001743 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001744 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001745 &did_create_module);
1746 }
1747 else
1748 {
1749 error.SetErrorString("no platform is currently set");
1750 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001751 }
1752 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001753
Jim Ingham4a94c912012-05-17 18:38:42 +00001754 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1755 // module in the list already, and if there was, let's remove it.
1756 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001757 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001758 ObjectFile *objfile = module_sp->GetObjectFile();
1759 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001760 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001761 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001762 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001763 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1764 case ObjectFile::eTypeExecutable: /// A normal executable
1765 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1766 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1767 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1768 break;
1769 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1770 if (error_ptr)
1771 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1772 return ModuleSP();
1773 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1774 if (error_ptr)
1775 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1776 return ModuleSP();
1777 default:
1778 if (error_ptr)
1779 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1780 return ModuleSP();
1781 }
1782 // GetSharedModule is not guaranteed to find the old shared module, for instance
1783 // in the common case where you pass in the UUID, it is only going to find the one
1784 // module matching the UUID. In fact, it has no good way to know what the "old module"
1785 // relevant to this target is, since there might be many copies of a module with this file spec
1786 // in various running debug sessions, but only one of them will belong to this target.
1787 // So let's remove the UUID from the module list, and look in the target's module list.
1788 // Only do this if there is SOMETHING else in the module spec...
1789 if (!old_module_sp)
1790 {
1791 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001792 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001793 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1794 module_spec_copy.GetUUID().Clear();
1795
1796 ModuleList found_modules;
1797 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1798 if (num_found == 1)
1799 {
1800 old_module_sp = found_modules.GetModuleAtIndex(0);
1801 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001802 }
1803 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001804
1805 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1806 {
1807 m_images.ReplaceModule(old_module_sp, module_sp);
1808 Module *old_module_ptr = old_module_sp.get();
1809 old_module_sp.reset();
1810 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1811 }
1812 else
1813 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001814 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001815 else
1816 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001817 }
1818 }
1819 if (error_ptr)
1820 *error_ptr = error;
1821 return module_sp;
1822}
1823
1824
Greg Claytond9e416c2012-02-18 05:35:26 +00001825TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826Target::CalculateTarget ()
1827{
Greg Claytond9e416c2012-02-18 05:35:26 +00001828 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001829}
1830
Greg Claytond9e416c2012-02-18 05:35:26 +00001831ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001832Target::CalculateProcess ()
1833{
Greg Claytond9e416c2012-02-18 05:35:26 +00001834 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001835}
1836
Greg Claytond9e416c2012-02-18 05:35:26 +00001837ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001838Target::CalculateThread ()
1839{
Greg Claytond9e416c2012-02-18 05:35:26 +00001840 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841}
1842
Jason Molendab57e4a12013-11-04 09:33:30 +00001843StackFrameSP
1844Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001845{
Jason Molendab57e4a12013-11-04 09:33:30 +00001846 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847}
1848
1849void
Greg Clayton0603aa92010-10-04 01:05:56 +00001850Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001851{
Greg Claytonc14ee322011-09-22 04:58:26 +00001852 exe_ctx.Clear();
1853 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001854}
1855
1856PathMappingList &
1857Target::GetImageSearchPathList ()
1858{
1859 return m_image_search_paths;
1860}
1861
1862void
1863Target::ImageSearchPathsChanged
1864(
1865 const PathMappingList &path_list,
1866 void *baton
1867)
1868{
1869 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001870 ModuleSP exe_module_sp (target->GetExecutableModule());
1871 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001872 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873}
1874
1875ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001876Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877{
Greg Clayton73da2442011-08-03 01:23:55 +00001878 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001879 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001880 {
Greg Clayton73da2442011-08-03 01:23:55 +00001881 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001882 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001883 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001884 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001885 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1886 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001887 return m_scratch_ast_context_ap.get();
1888}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001889
Sean Callanan686b2312011-11-16 18:20:47 +00001890ClangASTImporter *
1891Target::GetClangASTImporter()
1892{
1893 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1894
1895 if (!ast_importer)
1896 {
1897 ast_importer = new ClangASTImporter();
1898 m_ast_importer_ap.reset(ast_importer);
1899 }
1900
1901 return ast_importer;
1902}
1903
Greg Clayton99d0faf2010-11-18 23:32:35 +00001904void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001905Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001906{
Greg Clayton6920b522012-08-22 18:39:03 +00001907 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001908}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001909
Greg Clayton99d0faf2010-11-18 23:32:35 +00001910void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001911Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001912{
Greg Clayton6920b522012-08-22 18:39:03 +00001913 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001914}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001915
Greg Claytonc859e2d2012-02-13 23:10:39 +00001916FileSpecList
1917Target::GetDefaultExecutableSearchPaths ()
1918{
Greg Clayton67cc0632012-08-22 17:17:09 +00001919 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1920 if (properties_sp)
1921 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001922 return FileSpecList();
1923}
1924
Michael Sartaina7499c92013-07-01 19:45:50 +00001925FileSpecList
1926Target::GetDefaultDebugFileSearchPaths ()
1927{
1928 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1929 if (properties_sp)
1930 return properties_sp->GetDebugFileSearchPaths();
1931 return FileSpecList();
1932}
1933
Sean Callanan85054342015-04-03 15:39:47 +00001934FileSpecList
1935Target::GetDefaultClangModuleSearchPaths ()
1936{
1937 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1938 if (properties_sp)
1939 return properties_sp->GetClangModuleSearchPaths();
1940 return FileSpecList();
1941}
1942
Caroline Ticedaccaa92010-09-20 20:44:43 +00001943ArchSpec
1944Target::GetDefaultArchitecture ()
1945{
Greg Clayton67cc0632012-08-22 17:17:09 +00001946 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1947 if (properties_sp)
1948 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001949 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001950}
1951
1952void
Greg Clayton67cc0632012-08-22 17:17:09 +00001953Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001954{
Greg Clayton67cc0632012-08-22 17:17:09 +00001955 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1956 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001957 {
1958 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 +00001959 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001960 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001961}
1962
Greg Clayton0603aa92010-10-04 01:05:56 +00001963Target *
1964Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1965{
1966 // The target can either exist in the "process" of ExecutionContext, or in
1967 // the "target_sp" member of SymbolContext. This accessor helper function
1968 // will get the target from one of these locations.
1969
1970 Target *target = NULL;
1971 if (sc_ptr != NULL)
1972 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001973 if (target == NULL && exe_ctx_ptr)
1974 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001975 return target;
1976}
1977
Jim Ingham1624a2d2014-05-05 02:26:40 +00001978ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001979Target::EvaluateExpression
1980(
1981 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001982 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001983 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001984 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001985)
1986{
Enrico Granata97fca502012-09-18 17:43:16 +00001987 result_valobj_sp.reset();
1988
Jim Ingham8646d3c2014-05-05 02:47:44 +00001989 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001990
Greg Claytond1767f02011-12-08 02:13:16 +00001991 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1992 return execution_results;
1993
Jim Ingham6026ca32011-05-12 02:06:14 +00001994 // We shouldn't run stop hooks in expressions.
1995 // Be sure to reset this if you return anywhere within this function.
1996 bool old_suppress_value = m_suppress_stop_hooks;
1997 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001998
1999 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002000
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002001 if (frame)
2002 {
2003 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002004 }
2005 else if (m_process_sp)
2006 {
2007 m_process_sp->CalculateExecutionContext(exe_ctx);
2008 }
2009 else
2010 {
2011 CalculateExecutionContext(exe_ctx);
2012 }
2013
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002014 // Make sure we aren't just trying to see the value of a persistent
2015 // variable (something like "$0")
2016 lldb::ClangExpressionVariableSP persistent_var_sp;
2017 // Only check for persistent variables the expression starts with a '$'
2018 if (expr_cstr[0] == '$')
Zachary Turner32abc6e2015-03-03 19:23:09 +00002019 persistent_var_sp = m_persistent_variables->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002020
2021 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002022 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002023 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002024 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002025 }
2026 else
2027 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002028 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002029 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002030 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002031 options,
2032 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002033 prefix,
2034 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002035 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002036 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002037
2038 m_suppress_stop_hooks = old_suppress_value;
2039
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002040 return execution_results;
2041}
2042
Zachary Turner32abc6e2015-03-03 19:23:09 +00002043ClangPersistentVariables &
2044Target::GetPersistentVariables()
2045{
2046 return *m_persistent_variables;
2047}
2048
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002049lldb::addr_t
2050Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2051{
2052 addr_t code_addr = load_addr;
2053 switch (m_arch.GetMachine())
2054 {
2055 case llvm::Triple::arm:
2056 case llvm::Triple::thumb:
2057 switch (addr_class)
2058 {
2059 case eAddressClassData:
2060 case eAddressClassDebug:
2061 return LLDB_INVALID_ADDRESS;
2062
2063 case eAddressClassUnknown:
2064 case eAddressClassInvalid:
2065 case eAddressClassCode:
2066 case eAddressClassCodeAlternateISA:
2067 case eAddressClassRuntime:
2068 // Check if bit zero it no set?
2069 if ((code_addr & 1ull) == 0)
2070 {
2071 // Bit zero isn't set, check if the address is a multiple of 2?
2072 if (code_addr & 2ull)
2073 {
2074 // The address is a multiple of 2 so it must be thumb, set bit zero
2075 code_addr |= 1ull;
2076 }
2077 else if (addr_class == eAddressClassCodeAlternateISA)
2078 {
2079 // We checked the address and the address claims to be the alternate ISA
2080 // which means thumb, so set bit zero.
2081 code_addr |= 1ull;
2082 }
2083 }
2084 break;
2085 }
2086 break;
2087
2088 default:
2089 break;
2090 }
2091 return code_addr;
2092}
2093
2094lldb::addr_t
2095Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2096{
2097 addr_t opcode_addr = load_addr;
2098 switch (m_arch.GetMachine())
2099 {
2100 case llvm::Triple::arm:
2101 case llvm::Triple::thumb:
2102 switch (addr_class)
2103 {
2104 case eAddressClassData:
2105 case eAddressClassDebug:
2106 return LLDB_INVALID_ADDRESS;
2107
2108 case eAddressClassInvalid:
2109 case eAddressClassUnknown:
2110 case eAddressClassCode:
2111 case eAddressClassCodeAlternateISA:
2112 case eAddressClassRuntime:
2113 opcode_addr &= ~(1ull);
2114 break;
2115 }
2116 break;
2117
2118 default:
2119 break;
2120 }
2121 return opcode_addr;
2122}
2123
Greg Clayton9585fbf2013-03-19 00:20:55 +00002124SourceManager &
2125Target::GetSourceManager ()
2126{
2127 if (m_source_manager_ap.get() == NULL)
2128 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2129 return *m_source_manager_ap;
2130}
2131
Sean Callanan9998acd2014-12-05 01:21:59 +00002132ClangModulesDeclVendor *
2133Target::GetClangModulesDeclVendor ()
2134{
2135 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2136
2137 {
2138 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2139
2140 if (!m_clang_modules_decl_vendor_ap)
2141 {
2142 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2143 }
2144 }
2145
2146 return m_clang_modules_decl_vendor_ap.get();
2147}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002148
Greg Clayton44d93782014-01-27 23:43:24 +00002149Target::StopHookSP
2150Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002151{
2152 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002153 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2154 m_stop_hooks[new_uid] = stop_hook_sp;
2155 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002156}
2157
2158bool
2159Target::RemoveStopHookByID (lldb::user_id_t user_id)
2160{
2161 size_t num_removed;
2162 num_removed = m_stop_hooks.erase (user_id);
2163 if (num_removed == 0)
2164 return false;
2165 else
2166 return true;
2167}
2168
2169void
2170Target::RemoveAllStopHooks ()
2171{
2172 m_stop_hooks.clear();
2173}
2174
2175Target::StopHookSP
2176Target::GetStopHookByID (lldb::user_id_t user_id)
2177{
2178 StopHookSP found_hook;
2179
2180 StopHookCollection::iterator specified_hook_iter;
2181 specified_hook_iter = m_stop_hooks.find (user_id);
2182 if (specified_hook_iter != m_stop_hooks.end())
2183 found_hook = (*specified_hook_iter).second;
2184 return found_hook;
2185}
2186
2187bool
2188Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2189{
2190 StopHookCollection::iterator specified_hook_iter;
2191 specified_hook_iter = m_stop_hooks.find (user_id);
2192 if (specified_hook_iter == m_stop_hooks.end())
2193 return false;
2194
2195 (*specified_hook_iter).second->SetIsActive (active_state);
2196 return true;
2197}
2198
2199void
2200Target::SetAllStopHooksActiveState (bool active_state)
2201{
2202 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2203 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2204 {
2205 (*pos).second->SetIsActive (active_state);
2206 }
2207}
2208
2209void
2210Target::RunStopHooks ()
2211{
Jim Ingham6026ca32011-05-12 02:06:14 +00002212 if (m_suppress_stop_hooks)
2213 return;
2214
Jim Ingham9575d842011-03-11 03:53:59 +00002215 if (!m_process_sp)
2216 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002217
2218 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2219 // since in that case we do not want to run the stop-hooks
2220 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2221 return;
2222
Jim Ingham9575d842011-03-11 03:53:59 +00002223 if (m_stop_hooks.empty())
2224 return;
2225
2226 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2227
2228 // If there aren't any active stop hooks, don't bother either:
2229 bool any_active_hooks = false;
2230 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2231 {
2232 if ((*pos).second->IsActive())
2233 {
2234 any_active_hooks = true;
2235 break;
2236 }
2237 }
2238 if (!any_active_hooks)
2239 return;
2240
2241 CommandReturnObject result;
2242
2243 std::vector<ExecutionContext> exc_ctx_with_reasons;
2244 std::vector<SymbolContext> sym_ctx_with_reasons;
2245
2246 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2247 size_t num_threads = cur_threadlist.GetSize();
2248 for (size_t i = 0; i < num_threads; i++)
2249 {
2250 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2251 if (cur_thread_sp->ThreadStoppedForAReason())
2252 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002253 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002254 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2255 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2256 }
2257 }
2258
2259 // If no threads stopped for a reason, don't run the stop-hooks.
2260 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2261 if (num_exe_ctx == 0)
2262 return;
2263
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002264 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2265 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002266
2267 bool keep_going = true;
2268 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002269 bool print_hook_header;
2270 bool print_thread_header;
2271
2272 if (num_exe_ctx == 1)
2273 print_thread_header = false;
2274 else
2275 print_thread_header = true;
2276
2277 if (m_stop_hooks.size() == 1)
2278 print_hook_header = false;
2279 else
2280 print_hook_header = true;
2281
Jim Ingham9575d842011-03-11 03:53:59 +00002282 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2283 {
2284 // result.Clear();
2285 StopHookSP cur_hook_sp = (*pos).second;
2286 if (!cur_hook_sp->IsActive())
2287 continue;
2288
2289 bool any_thread_matched = false;
2290 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2291 {
2292 if ((cur_hook_sp->GetSpecifier () == NULL
2293 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2294 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002295 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002296 {
2297 if (!hooks_ran)
2298 {
Jim Ingham9575d842011-03-11 03:53:59 +00002299 hooks_ran = true;
2300 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002301 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002302 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002303 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2304 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2305 NULL);
2306 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002307 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002308 else
Daniel Malead01b2952012-11-29 21:49:15 +00002309 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002310 any_thread_matched = true;
2311 }
2312
Jim Ingham381e25b2011-03-22 01:47:27 +00002313 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002314 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002315
2316 CommandInterpreterRunOptions options;
2317 options.SetStopOnContinue (true);
2318 options.SetStopOnError (true);
2319 options.SetEchoCommands (false);
2320 options.SetPrintResults (true);
2321 options.SetAddToHistory (false);
2322
2323 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2324 &exc_ctx_with_reasons[i],
2325 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002326 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002327
2328 // If the command started the target going again, we should bag out of
2329 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002330 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2331 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002332 {
Daniel Malead01b2952012-11-29 21:49:15 +00002333 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002334 keep_going = false;
2335 }
2336 }
2337 }
2338 }
Jason Molenda879cf772011-09-23 00:42:55 +00002339
Caroline Tice969ed3d2011-05-02 20:41:46 +00002340 result.GetImmediateOutputStream()->Flush();
2341 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002342}
2343
Greg Claytonfbb76342013-11-20 21:07:01 +00002344const TargetPropertiesSP &
2345Target::GetGlobalProperties()
2346{
2347 static TargetPropertiesSP g_settings_sp;
2348 if (!g_settings_sp)
2349 {
2350 g_settings_sp.reset (new TargetProperties (NULL));
2351 }
2352 return g_settings_sp;
2353}
2354
2355Error
2356Target::Install (ProcessLaunchInfo *launch_info)
2357{
2358 Error error;
2359 PlatformSP platform_sp (GetPlatform());
2360 if (platform_sp)
2361 {
2362 if (platform_sp->IsRemote())
2363 {
2364 if (platform_sp->IsConnected())
2365 {
2366 // Install all files that have an install path, and always install the
2367 // main executable when connected to a remote platform
2368 const ModuleList& modules = GetImages();
2369 const size_t num_images = modules.GetSize();
2370 for (size_t idx = 0; idx < num_images; ++idx)
2371 {
2372 const bool is_main_executable = idx == 0;
2373 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2374 if (module_sp)
2375 {
2376 FileSpec local_file (module_sp->GetFileSpec());
2377 if (local_file)
2378 {
2379 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2380 if (!remote_file)
2381 {
2382 if (is_main_executable) // TODO: add setting for always installing main executable???
2383 {
2384 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002385 remote_file = platform_sp->GetRemoteWorkingDirectory();
2386 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002387 }
2388 }
2389 if (remote_file)
2390 {
2391 error = platform_sp->Install(local_file, remote_file);
2392 if (error.Success())
2393 {
2394 module_sp->SetPlatformFileSpec(remote_file);
2395 if (is_main_executable)
2396 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002397 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002398 if (launch_info)
2399 launch_info->SetExecutableFile(remote_file, false);
2400 }
2401 }
2402 else
2403 break;
2404 }
2405 }
2406 }
2407 }
2408 }
2409 }
2410 }
2411 return error;
2412}
Greg Clayton7b242382011-07-08 00:48:09 +00002413
Greg Claytond5944cd2013-12-06 01:12:00 +00002414bool
2415Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2416{
2417 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2418}
2419
2420bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002421Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2422{
2423 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2424}
2425
2426bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002427Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2428{
2429 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2430 if (old_section_load_addr != new_section_load_addr)
2431 {
2432 uint32_t stop_id = 0;
2433 ProcessSP process_sp(GetProcessSP());
2434 if (process_sp)
2435 stop_id = process_sp->GetStopID();
2436 else
2437 stop_id = m_section_load_history.GetLastStopID();
2438 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2439 return true; // Return true if the section load address was changed...
2440 }
2441 return false; // Return false to indicate nothing changed
2442
2443}
2444
Greg Clayton8012cad2014-11-17 19:39:20 +00002445size_t
2446Target::UnloadModuleSections (const ModuleList &module_list)
2447{
2448 size_t section_unload_count = 0;
2449 size_t num_modules = module_list.GetSize();
2450 for (size_t i=0; i<num_modules; ++i)
2451 {
2452 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2453 }
2454 return section_unload_count;
2455}
2456
2457size_t
2458Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2459{
2460 uint32_t stop_id = 0;
2461 ProcessSP process_sp(GetProcessSP());
2462 if (process_sp)
2463 stop_id = process_sp->GetStopID();
2464 else
2465 stop_id = m_section_load_history.GetLastStopID();
2466 SectionList *sections = module_sp->GetSectionList();
2467 size_t section_unload_count = 0;
2468 if (sections)
2469 {
2470 const uint32_t num_sections = sections->GetNumSections(0);
2471 for (uint32_t i = 0; i < num_sections; ++i)
2472 {
2473 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2474 }
2475 }
2476 return section_unload_count;
2477}
2478
Greg Claytond5944cd2013-12-06 01:12:00 +00002479bool
2480Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2481{
2482 uint32_t stop_id = 0;
2483 ProcessSP process_sp(GetProcessSP());
2484 if (process_sp)
2485 stop_id = process_sp->GetStopID();
2486 else
2487 stop_id = m_section_load_history.GetLastStopID();
2488 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2489}
2490
2491bool
2492Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2493{
2494 uint32_t stop_id = 0;
2495 ProcessSP process_sp(GetProcessSP());
2496 if (process_sp)
2497 stop_id = process_sp->GetStopID();
2498 else
2499 stop_id = m_section_load_history.GetLastStopID();
2500 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2501}
2502
2503void
2504Target::ClearAllLoadedSections ()
2505{
2506 m_section_load_history.Clear();
2507}
2508
Greg Claytonb09c5382013-12-13 17:20:18 +00002509
2510Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002511Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002512{
2513 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002514 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2515
2516 if (log)
2517 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2518
Greg Claytonb09c5382013-12-13 17:20:18 +00002519 StateType state = eStateInvalid;
2520
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002521 // Scope to temporarily get the process state in case someone has manually
2522 // remotely connected already to a process and we can skip the platform
2523 // launching.
2524 {
2525 ProcessSP process_sp (GetProcessSP());
2526
2527 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002528 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002529 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002530 if (log)
2531 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2532 }
2533 else
2534 {
2535 if (log)
2536 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2537 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002538 }
2539
Greg Claytonb09c5382013-12-13 17:20:18 +00002540 launch_info.GetFlags().Set (eLaunchFlagDebug);
2541
2542 // Get the value of synchronous execution here. If you wait till after you have started to
2543 // run, then you could have hit a breakpoint, whose command might switch the value, and
2544 // then you'll pick up that incorrect value.
2545 Debugger &debugger = GetDebugger();
2546 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2547
2548 PlatformSP platform_sp (GetPlatform());
2549
2550 // Finalize the file actions, and if none were given, default to opening
2551 // up a pseudo terminal
2552 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002553 if (log)
2554 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2555 __FUNCTION__,
2556 platform_sp ? "true" : "false",
2557 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2558 default_to_use_pty ? "true" : "false");
2559
Greg Claytonb09c5382013-12-13 17:20:18 +00002560 launch_info.FinalizeFileActions (this, default_to_use_pty);
2561
2562 if (state == eStateConnected)
2563 {
2564 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2565 {
2566 error.SetErrorString("can't launch in tty when launching through a remote connection");
2567 return error;
2568 }
2569 }
2570
2571 if (!launch_info.GetArchitecture().IsValid())
2572 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002573
2574 // 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 +00002575 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2576 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002577 if (log)
2578 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2579
Greg Clayton5df78fa2015-05-23 03:54:53 +00002580 // Get a weak pointer to the previous process if we have one
2581 ProcessWP process_wp;
2582 if (m_process_sp)
2583 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00002584 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2585 debugger,
2586 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002587 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00002588
2589 // Cleanup the old process since someone might still have a strong
2590 // reference to this process and we would like to allow it to cleanup
2591 // as much as it can without the object being destroyed. We try to
2592 // lock the shared pointer and if that works, then someone else still
2593 // has a strong reference to the process.
2594
2595 ProcessSP old_process_sp(process_wp.lock());
2596 if (old_process_sp)
2597 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00002598 }
2599 else
2600 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002601 if (log)
2602 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2603
Greg Claytonb09c5382013-12-13 17:20:18 +00002604 if (state == eStateConnected)
2605 {
2606 assert(m_process_sp);
2607 }
2608 else
2609 {
Todd Fiala015d8182014-07-22 23:41:36 +00002610 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002611 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002612 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002613 }
Todd Fiala015d8182014-07-22 23:41:36 +00002614
2615 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002616 if (m_process_sp)
2617 error = m_process_sp->Launch (launch_info);
2618 }
2619
2620 if (!m_process_sp)
2621 {
2622 if (error.Success())
2623 error.SetErrorString("failed to launch or debug process");
2624 return error;
2625 }
2626
2627 if (error.Success())
2628 {
Ilia K064e69f2015-03-23 21:16:25 +00002629 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00002630 {
Greg Clayton44d93782014-01-27 23:43:24 +00002631 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00002632 if (!hijack_listener_sp)
2633 {
2634 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
2635 launch_info.SetHijackListener(hijack_listener_sp);
2636 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
2637 }
Todd Fialaac33cc92014-10-09 01:02:08 +00002638
Ilia K38810f42015-05-20 10:15:47 +00002639 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002640
2641 if (state == eStateStopped)
2642 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002643 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00002644 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002645 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00002646 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002647 error = m_process_sp->PrivateResume();
2648 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00002649 {
Ilia K064e69f2015-03-23 21:16:25 +00002650 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
2651 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2652 if (!StateIsStoppedState(state, must_be_alive))
2653 {
2654 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
2655 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002656 }
2657 }
Ilia K064e69f2015-03-23 21:16:25 +00002658 else
2659 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002660 m_process_sp->RestoreProcessEvents();
2661 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00002662 }
2663 if (!error.Success())
2664 {
Ilia K064e69f2015-03-23 21:16:25 +00002665 Error error2;
2666 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
2667 error = error2;
2668 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002669 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002670 }
Greg Clayton40286e02014-04-30 20:29:09 +00002671 else if (state == eStateExited)
2672 {
Zachary Turner10687b02014-10-20 17:46:43 +00002673 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002674 const int exit_status = m_process_sp->GetExitStatus();
2675 const char *exit_desc = m_process_sp->GetExitDescription();
2676#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'."
2677 if (exit_desc && exit_desc[0])
2678 {
2679 if (with_shell)
2680 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2681 else
2682 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2683 }
2684 else
2685 {
2686 if (with_shell)
2687 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2688 else
2689 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2690 }
2691 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002692 else
2693 {
2694 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2695 }
2696 }
Greg Clayton44d93782014-01-27 23:43:24 +00002697 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002698 }
2699 else
2700 {
Greg Clayton44d93782014-01-27 23:43:24 +00002701 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002702 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002703 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002704 }
2705 return error;
2706}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002707
2708Error
2709Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
2710{
2711 auto state = eStateInvalid;
2712 auto process_sp = GetProcessSP ();
2713 if (process_sp)
2714 {
2715 state = process_sp->GetState ();
2716 if (process_sp->IsAlive () && state != eStateConnected)
2717 {
2718 if (state == eStateAttaching)
2719 return Error ("process attach is in progress");
2720 return Error ("a process is already being debugged");
2721 }
2722 }
2723
2724 ListenerSP hijack_listener_sp (new Listener ("lldb.Target.Attach.attach.hijack"));
2725 attach_info.SetHijackListener (hijack_listener_sp);
2726
2727 const ModuleSP old_exec_module_sp = GetExecutableModule ();
2728
2729 // If no process info was specified, then use the target executable
2730 // name as the process to attach to by default
2731 if (!attach_info.ProcessInfoSpecified ())
2732 {
2733 if (old_exec_module_sp)
2734 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
2735
2736 if (!attach_info.ProcessInfoSpecified ())
2737 {
2738 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
2739 }
2740 }
2741
2742 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
2743
2744 Error error;
2745 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
2746 {
2747 SetPlatform (platform_sp);
2748 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
2749 }
2750 else
2751 {
2752 if (state != eStateConnected)
2753 {
2754 const char *plugin_name = attach_info.GetProcessPluginName ();
2755 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
2756 if (process_sp == nullptr)
2757 {
2758 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
2759 return error;
2760 }
2761 }
2762 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
2763 error = process_sp->Attach (attach_info);
2764 }
2765
2766 if (error.Success () && process_sp)
2767 {
2768 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
2769 process_sp->RestoreProcessEvents ();
2770
2771 if (state != eStateStopped)
2772 {
2773 const char *exit_desc = process_sp->GetExitDescription ();
2774 if (exit_desc)
2775 error.SetErrorStringWithFormat ("attach failed: %s", exit_desc);
2776 else
2777 error.SetErrorString ("attach failed: process did not stop (no such process or permission problem?)");
Jason Molendaede31932015-04-17 05:01:58 +00002778 process_sp->Destroy (false);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002779 }
2780 }
2781 return error;
2782}
2783
Jim Ingham9575d842011-03-11 03:53:59 +00002784//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002785// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002786//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002787Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2788 UserID (uid),
2789 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002790 m_commands (),
2791 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002792 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002793 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002794{
2795}
2796
2797Target::StopHook::StopHook (const StopHook &rhs) :
2798 UserID (rhs.GetID()),
2799 m_target_sp (rhs.m_target_sp),
2800 m_commands (rhs.m_commands),
2801 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002802 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002803 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002804{
2805 if (rhs.m_thread_spec_ap.get() != NULL)
2806 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2807}
2808
2809
2810Target::StopHook::~StopHook ()
2811{
2812}
2813
2814void
Zachary Turner32abc6e2015-03-03 19:23:09 +00002815Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
2816{
2817 m_specifier_sp.reset(specifier);
2818}
2819
2820void
Jim Ingham9575d842011-03-11 03:53:59 +00002821Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2822{
2823 m_thread_spec_ap.reset (specifier);
2824}
2825
2826
2827void
2828Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2829{
2830 int indent_level = s->GetIndentLevel();
2831
2832 s->SetIndentLevel(indent_level + 2);
2833
Daniel Malead01b2952012-11-29 21:49:15 +00002834 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002835 if (m_active)
2836 s->Indent ("State: enabled\n");
2837 else
2838 s->Indent ("State: disabled\n");
2839
2840 if (m_specifier_sp)
2841 {
2842 s->Indent();
2843 s->PutCString ("Specifier:\n");
2844 s->SetIndentLevel (indent_level + 4);
2845 m_specifier_sp->GetDescription (s, level);
2846 s->SetIndentLevel (indent_level + 2);
2847 }
2848
2849 if (m_thread_spec_ap.get() != NULL)
2850 {
2851 StreamString tmp;
2852 s->Indent("Thread:\n");
2853 m_thread_spec_ap->GetDescription (&tmp, level);
2854 s->SetIndentLevel (indent_level + 4);
2855 s->Indent (tmp.GetData());
2856 s->PutCString ("\n");
2857 s->SetIndentLevel (indent_level + 2);
2858 }
2859
2860 s->Indent ("Commands: \n");
2861 s->SetIndentLevel (indent_level + 4);
2862 uint32_t num_commands = m_commands.GetSize();
2863 for (uint32_t i = 0; i < num_commands; i++)
2864 {
2865 s->Indent(m_commands.GetStringAtIndex(i));
2866 s->PutCString ("\n");
2867 }
2868 s->SetIndentLevel (indent_level);
2869}
2870
Greg Clayton67cc0632012-08-22 17:17:09 +00002871//--------------------------------------------------------------
2872// class TargetProperties
2873//--------------------------------------------------------------
2874
2875OptionEnumValueElement
2876lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002877{
Greg Clayton67cc0632012-08-22 17:17:09 +00002878 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2879 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2880 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2881 { 0, NULL, NULL }
2882};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002883
Greg Clayton1f746072012-08-29 21:13:06 +00002884static OptionEnumValueElement
2885g_inline_breakpoint_enums[] =
2886{
2887 { 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."},
2888 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2889 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2890 { 0, NULL, NULL }
2891};
2892
Jim Ingham0f063ba2013-03-02 00:26:47 +00002893typedef enum x86DisassemblyFlavor
2894{
2895 eX86DisFlavorDefault,
2896 eX86DisFlavorIntel,
2897 eX86DisFlavorATT
2898} x86DisassemblyFlavor;
2899
2900static OptionEnumValueElement
2901g_x86_dis_flavor_value_types[] =
2902{
2903 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2904 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2905 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2906 { 0, NULL, NULL }
2907};
2908
Enrico Granata397ddd52013-05-21 20:13:34 +00002909static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002910g_hex_immediate_style_values[] =
2911{
2912 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2913 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2914 { 0, NULL, NULL }
2915};
2916
2917static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002918g_load_script_from_sym_file_values[] =
2919{
2920 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2921 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2922 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2923 { 0, NULL, NULL }
2924};
2925
Greg Claytonfd814c52013-08-13 01:42:25 +00002926
2927static OptionEnumValueElement
2928g_memory_module_load_level_values[] =
2929{
Greg Clayton86eac942013-08-13 21:32:34 +00002930 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002931 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2932 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2933 { 0, NULL, NULL }
2934};
2935
Greg Clayton67cc0632012-08-22 17:17:09 +00002936static PropertyDefinition
2937g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002938{
Greg Clayton67cc0632012-08-22 17:17:09 +00002939 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
Ilia K055ad9b2015-05-18 13:41:01 +00002940 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Move breakpoints to nearest code." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002941 { "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 +00002942 { "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 +00002943 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2944 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2945 { "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 "
2946 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2947 "some part (starting at the root) of the path to the file when it was built, "
2948 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2949 "Each element of the array is checked in order and the first one that results in a match wins." },
2950 { "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 +00002951 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating debug symbol files." },
Sean Callanan85054342015-04-03 15:39:47 +00002952 { "clang-module-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating modules for Clang." },
Sean Callananf0c5aeb2015-04-20 16:31:29 +00002953 { "auto-import-clang-modules" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Automatically load Clang modules referred to by the program." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002954 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2955 { "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 +00002956 { "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 +00002957 { "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 +00002958 { "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." },
2959 { "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 +00002960 { "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." },
2961 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2962 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2963 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2964 { "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 +00002965 { "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 +00002966 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2967 { "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 +00002968 { "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 +00002969 "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. "
2970 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2971 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002972 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2973 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2974 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002975 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002976 // 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.
2977 { "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 +00002978 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2979 { "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 +00002980 { "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 +00002981 { "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 +00002982 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2983 "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. "
2984 "This setting helps users control how much information gets loaded when loading modules from memory."
2985 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2986 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2987 "'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 +00002988 { "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 +00002989 { "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 +00002990 { "display-runtime-support-values" , OptionValue::eTypeBoolean , false, false, NULL, NULL, "If true, LLDB will show variables that are meant to support the operation of a language's runtime support." },
Ewan Crawford78baa192015-05-13 09:18:18 +00002991 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, NULL, NULL, "Disable lock-step debugging, instead control threads independently." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002992 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2993};
Enrico Granata560558e2015-02-11 02:35:39 +00002994
Greg Clayton67cc0632012-08-22 17:17:09 +00002995enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002996{
Greg Clayton67cc0632012-08-22 17:17:09 +00002997 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00002998 ePropertyMoveToNearestCode,
Greg Clayton67cc0632012-08-22 17:17:09 +00002999 ePropertyExprPrefix,
3000 ePropertyPreferDynamic,
3001 ePropertyEnableSynthetic,
3002 ePropertySkipPrologue,
3003 ePropertySourceMap,
3004 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003005 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003006 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003007 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003008 ePropertyMaxChildrenCount,
3009 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003010 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003011 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003012 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003013 ePropertyRunArgs,
3014 ePropertyEnvVars,
3015 ePropertyInheritEnv,
3016 ePropertyInputPath,
3017 ePropertyOutputPath,
3018 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003019 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003020 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003021 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003022 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003023 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003024 ePropertyUseHexImmediates,
3025 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003026 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003027 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003028 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003029 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003030 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003031 ePropertyDisplayRuntimeSupportValues,
3032 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003033};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003034
Caroline Ticedaccaa92010-09-20 20:44:43 +00003035
Greg Clayton67cc0632012-08-22 17:17:09 +00003036class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003037{
Greg Clayton67cc0632012-08-22 17:17:09 +00003038public:
3039 TargetOptionValueProperties (const ConstString &name) :
3040 OptionValueProperties (name),
3041 m_target (NULL),
3042 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003043 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003044 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003045
Greg Clayton67cc0632012-08-22 17:17:09 +00003046 // This constructor is used when creating TargetOptionValueProperties when it
3047 // is part of a new lldb_private::Target instance. It will copy all current
3048 // global property values as needed
3049 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3050 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3051 m_target (target),
3052 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003053 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003054 }
3055
3056 virtual const Property *
3057 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3058 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003059 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003060 // try and grab the setting from the current target if there is one. Else we just
3061 // use the one from this instance.
3062 if (idx == ePropertyEnvVars)
3063 GetHostEnvironmentIfNeeded ();
3064
3065 if (exe_ctx)
3066 {
3067 Target *target = exe_ctx->GetTargetPtr();
3068 if (target)
3069 {
3070 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3071 if (this != target_properties)
3072 return target_properties->ProtectedGetPropertyAtIndex (idx);
3073 }
3074 }
3075 return ProtectedGetPropertyAtIndex (idx);
3076 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003077
3078 lldb::TargetSP
3079 GetTargetSP ()
3080 {
3081 return m_target->shared_from_this();
3082 }
3083
Greg Clayton67cc0632012-08-22 17:17:09 +00003084protected:
3085
3086 void
3087 GetHostEnvironmentIfNeeded () const
3088 {
3089 if (!m_got_host_env)
3090 {
3091 if (m_target)
3092 {
3093 m_got_host_env = true;
3094 const uint32_t idx = ePropertyInheritEnv;
3095 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3096 {
3097 PlatformSP platform_sp (m_target->GetPlatform());
3098 if (platform_sp)
3099 {
3100 StringList env;
3101 if (platform_sp->GetEnvironment(env))
3102 {
3103 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3104 if (env_dict)
3105 {
3106 const bool can_replace = false;
3107 const size_t envc = env.GetSize();
3108 for (size_t idx=0; idx<envc; idx++)
3109 {
3110 const char *env_entry = env.GetStringAtIndex (idx);
3111 if (env_entry)
3112 {
3113 const char *equal_pos = ::strchr(env_entry, '=');
3114 ConstString key;
3115 // It is ok to have environment variables with no values
3116 const char *value = NULL;
3117 if (equal_pos)
3118 {
3119 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3120 if (equal_pos[1])
3121 value = equal_pos + 1;
3122 }
3123 else
3124 {
3125 key.SetCString(env_entry);
3126 }
3127 // Don't allow existing keys to be replaced with ones we get from the platform environment
3128 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3129 }
3130 }
3131 }
3132 }
3133 }
3134 }
3135 }
3136 }
3137 }
3138 Target *m_target;
3139 mutable bool m_got_host_env;
3140};
3141
Greg Claytonfbb76342013-11-20 21:07:01 +00003142//----------------------------------------------------------------------
3143// TargetProperties
3144//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003145TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003146 Properties (),
3147 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003148{
3149 if (target)
3150 {
3151 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003152
3153 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3154 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3155 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3156 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3157 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3158 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3159 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3160 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3161 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3162 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3163
3164 // Update m_launch_info once it was created
3165 Arg0ValueChangedCallback(this, NULL);
3166 RunArgsValueChangedCallback(this, NULL);
3167 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3168 InputPathValueChangedCallback(this, NULL);
3169 OutputPathValueChangedCallback(this, NULL);
3170 ErrorPathValueChangedCallback(this, NULL);
3171 DetachOnErrorValueChangedCallback(this, NULL);
3172 DisableASLRValueChangedCallback(this, NULL);
3173 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003174 }
3175 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003176 {
3177 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3178 m_collection_sp->Initialize(g_properties);
3179 m_collection_sp->AppendProperty(ConstString("process"),
3180 ConstString("Settings specify to processes."),
3181 true,
3182 Process::GetGlobalProperties()->GetValueProperties());
3183 }
Ilia K8f37ca52015-02-13 14:31:06 +00003184
Caroline Ticedaccaa92010-09-20 20:44:43 +00003185}
3186
Greg Clayton67cc0632012-08-22 17:17:09 +00003187TargetProperties::~TargetProperties ()
3188{
3189}
3190ArchSpec
3191TargetProperties::GetDefaultArchitecture () const
3192{
3193 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3194 if (value)
3195 return value->GetCurrentValue();
3196 return ArchSpec();
3197}
3198
3199void
3200TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3201{
3202 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3203 if (value)
3204 return value->SetCurrentValue(arch, true);
3205}
3206
Ilia K055ad9b2015-05-18 13:41:01 +00003207bool
3208TargetProperties::GetMoveToNearestCode() const
3209{
3210 const uint32_t idx = ePropertyMoveToNearestCode;
3211 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3212}
3213
Greg Clayton67cc0632012-08-22 17:17:09 +00003214lldb::DynamicValueType
3215TargetProperties::GetPreferDynamicValue() const
3216{
3217 const uint32_t idx = ePropertyPreferDynamic;
3218 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3219}
3220
3221bool
Greg Clayton15484402015-05-15 18:40:24 +00003222TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3223{
3224 const uint32_t idx = ePropertyPreferDynamic;
3225 return m_collection_sp->SetPropertyAtIndexAsEnumeration(NULL, idx, d);
3226}
3227
3228
3229bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003230TargetProperties::GetDisableASLR () const
3231{
3232 const uint32_t idx = ePropertyDisableASLR;
3233 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3234}
3235
3236void
3237TargetProperties::SetDisableASLR (bool b)
3238{
3239 const uint32_t idx = ePropertyDisableASLR;
3240 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3241}
3242
3243bool
Jim Ingham106d0282014-06-25 02:32:56 +00003244TargetProperties::GetDetachOnError () const
3245{
3246 const uint32_t idx = ePropertyDetachOnError;
3247 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3248}
3249
3250void
3251TargetProperties::SetDetachOnError (bool b)
3252{
3253 const uint32_t idx = ePropertyDetachOnError;
3254 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3255}
3256
3257bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003258TargetProperties::GetDisableSTDIO () const
3259{
3260 const uint32_t idx = ePropertyDisableSTDIO;
3261 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3262}
3263
3264void
3265TargetProperties::SetDisableSTDIO (bool b)
3266{
3267 const uint32_t idx = ePropertyDisableSTDIO;
3268 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3269}
3270
Jim Ingham0f063ba2013-03-02 00:26:47 +00003271const char *
3272TargetProperties::GetDisassemblyFlavor () const
3273{
3274 const uint32_t idx = ePropertyDisassemblyFlavor;
3275 const char *return_value;
3276
3277 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3278 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3279 return return_value;
3280}
3281
Greg Clayton1f746072012-08-29 21:13:06 +00003282InlineStrategy
3283TargetProperties::GetInlineStrategy () const
3284{
3285 const uint32_t idx = ePropertyInlineStrategy;
3286 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3287}
3288
Greg Clayton45392552012-10-17 22:57:12 +00003289const char *
3290TargetProperties::GetArg0 () const
3291{
3292 const uint32_t idx = ePropertyArg0;
3293 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3294}
3295
3296void
3297TargetProperties::SetArg0 (const char *arg)
3298{
3299 const uint32_t idx = ePropertyArg0;
3300 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003301 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003302}
3303
Greg Clayton67cc0632012-08-22 17:17:09 +00003304bool
3305TargetProperties::GetRunArguments (Args &args) const
3306{
3307 const uint32_t idx = ePropertyRunArgs;
3308 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3309}
3310
3311void
3312TargetProperties::SetRunArguments (const Args &args)
3313{
3314 const uint32_t idx = ePropertyRunArgs;
3315 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003316 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003317}
3318
3319size_t
3320TargetProperties::GetEnvironmentAsArgs (Args &env) const
3321{
3322 const uint32_t idx = ePropertyEnvVars;
3323 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3324}
3325
Ilia K8f37ca52015-02-13 14:31:06 +00003326void
3327TargetProperties::SetEnvironmentFromArgs (const Args &env)
3328{
3329 const uint32_t idx = ePropertyEnvVars;
3330 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003331 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003332}
3333
Greg Clayton67cc0632012-08-22 17:17:09 +00003334bool
3335TargetProperties::GetSkipPrologue() const
3336{
3337 const uint32_t idx = ePropertySkipPrologue;
3338 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3339}
3340
3341PathMappingList &
3342TargetProperties::GetSourcePathMap () const
3343{
3344 const uint32_t idx = ePropertySourceMap;
3345 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3346 assert(option_value);
3347 return option_value->GetCurrentValue();
3348}
3349
3350FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003351TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003352{
3353 const uint32_t idx = ePropertyExecutableSearchPaths;
3354 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3355 assert(option_value);
3356 return option_value->GetCurrentValue();
3357}
3358
Michael Sartaina7499c92013-07-01 19:45:50 +00003359FileSpecList &
3360TargetProperties::GetDebugFileSearchPaths ()
3361{
3362 const uint32_t idx = ePropertyDebugFileSearchPaths;
3363 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3364 assert(option_value);
3365 return option_value->GetCurrentValue();
3366}
3367
Sean Callanan85054342015-04-03 15:39:47 +00003368FileSpecList &
3369TargetProperties::GetClangModuleSearchPaths ()
3370{
3371 const uint32_t idx = ePropertyClangModuleSearchPaths;
3372 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3373 assert(option_value);
3374 return option_value->GetCurrentValue();
3375}
3376
Greg Clayton67cc0632012-08-22 17:17:09 +00003377bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003378TargetProperties::GetEnableAutoImportClangModules() const
3379{
3380 const uint32_t idx = ePropertyAutoImportClangModules;
3381 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3382}
3383
3384bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003385TargetProperties::GetEnableSyntheticValue () const
3386{
3387 const uint32_t idx = ePropertyEnableSynthetic;
3388 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3389}
3390
3391uint32_t
3392TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3393{
3394 const uint32_t idx = ePropertyMaxChildrenCount;
3395 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3396}
3397
3398uint32_t
3399TargetProperties::GetMaximumSizeOfStringSummary() const
3400{
3401 const uint32_t idx = ePropertyMaxSummaryLength;
3402 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3403}
3404
Enrico Granatad325bf92013-06-04 22:54:16 +00003405uint32_t
3406TargetProperties::GetMaximumMemReadSize () const
3407{
3408 const uint32_t idx = ePropertyMaxMemReadSize;
3409 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3410}
3411
Greg Clayton67cc0632012-08-22 17:17:09 +00003412FileSpec
3413TargetProperties::GetStandardInputPath () const
3414{
3415 const uint32_t idx = ePropertyInputPath;
3416 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3417}
3418
3419void
3420TargetProperties::SetStandardInputPath (const char *p)
3421{
3422 const uint32_t idx = ePropertyInputPath;
3423 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3424}
3425
3426FileSpec
3427TargetProperties::GetStandardOutputPath () const
3428{
3429 const uint32_t idx = ePropertyOutputPath;
3430 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3431}
3432
3433void
3434TargetProperties::SetStandardOutputPath (const char *p)
3435{
3436 const uint32_t idx = ePropertyOutputPath;
3437 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3438}
3439
3440FileSpec
3441TargetProperties::GetStandardErrorPath () const
3442{
3443 const uint32_t idx = ePropertyErrorPath;
3444 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3445}
3446
Greg Clayton6920b522012-08-22 18:39:03 +00003447const char *
3448TargetProperties::GetExpressionPrefixContentsAsCString ()
3449{
3450 const uint32_t idx = ePropertyExprPrefix;
3451 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3452 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003453 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003454 const bool null_terminate = true;
3455 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003456 if (data_sp)
3457 return (const char *) data_sp->GetBytes();
3458 }
Greg Clayton6920b522012-08-22 18:39:03 +00003459 return NULL;
3460}
3461
Greg Clayton67cc0632012-08-22 17:17:09 +00003462void
3463TargetProperties::SetStandardErrorPath (const char *p)
3464{
3465 const uint32_t idx = ePropertyErrorPath;
3466 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3467}
3468
3469bool
3470TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3471{
3472 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3473 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3474}
3475
Jim Ingham17d023f2013-03-13 17:58:04 +00003476bool
Daniel Malead79ae052013-08-07 21:54:09 +00003477TargetProperties::GetUseHexImmediates () const
3478{
3479 const uint32_t idx = ePropertyUseHexImmediates;
3480 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3481}
3482
3483bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003484TargetProperties::GetUseFastStepping () const
3485{
3486 const uint32_t idx = ePropertyUseFastStepping;
3487 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3488}
3489
Greg Claytonfb6621e2013-12-06 21:59:52 +00003490bool
3491TargetProperties::GetDisplayExpressionsInCrashlogs () const
3492{
3493 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3494 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3495}
3496
Enrico Granata397ddd52013-05-21 20:13:34 +00003497LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003498TargetProperties::GetLoadScriptFromSymbolFile () const
3499{
3500 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003501 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003502}
3503
Daniel Malead79ae052013-08-07 21:54:09 +00003504Disassembler::HexImmediateStyle
3505TargetProperties::GetHexImmediateStyle () const
3506{
3507 const uint32_t idx = ePropertyHexImmediateStyle;
3508 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3509}
3510
Greg Claytonfd814c52013-08-13 01:42:25 +00003511MemoryModuleLoadLevel
3512TargetProperties::GetMemoryModuleLoadLevel() const
3513{
3514 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3515 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3516}
3517
Jason Molendaa4bea722014-02-14 05:06:49 +00003518bool
3519TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3520{
3521 const uint32_t idx = ePropertyTrapHandlerNames;
3522 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3523}
Greg Claytonfd814c52013-08-13 01:42:25 +00003524
Jason Molendaa4bea722014-02-14 05:06:49 +00003525void
3526TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3527{
3528 const uint32_t idx = ePropertyTrapHandlerNames;
3529 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3530}
Greg Clayton67cc0632012-08-22 17:17:09 +00003531
Enrico Granata560558e2015-02-11 02:35:39 +00003532bool
3533TargetProperties::GetDisplayRuntimeSupportValues () const
3534{
3535 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3536 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3537}
3538
3539void
3540TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3541{
3542 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3543 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3544}
3545
Ewan Crawford78baa192015-05-13 09:18:18 +00003546bool
3547TargetProperties::GetNonStopModeEnabled () const
3548{
3549 const uint32_t idx = ePropertyNonStopModeEnabled;
3550 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3551}
3552
Ilia K8f37ca52015-02-13 14:31:06 +00003553const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003554TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003555{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003556 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003557 return m_launch_info;
3558}
3559
3560void
3561TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3562{
3563 m_launch_info = launch_info;
3564 SetArg0(launch_info.GetArg0());
3565 SetRunArguments(launch_info.GetArguments());
3566 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3567 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3568 if (input_file_action)
3569 {
3570 const char *input_path = input_file_action->GetPath();
3571 if (input_path)
3572 SetStandardInputPath(input_path);
3573 }
3574 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3575 if (output_file_action)
3576 {
3577 const char *output_path = output_file_action->GetPath();
3578 if (output_path)
3579 SetStandardOutputPath(output_path);
3580 }
3581 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3582 if (error_file_action)
3583 {
3584 const char *error_path = error_file_action->GetPath();
3585 if (error_path)
3586 SetStandardErrorPath(error_path);
3587 }
3588 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3589 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3590 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3591}
3592
3593void
3594TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3595{
3596 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3597 this_->m_launch_info.SetArg0(this_->GetArg0());
3598}
3599
3600void
3601TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3602{
3603 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3604 Args args;
3605 if (this_->GetRunArguments(args))
3606 this_->m_launch_info.GetArguments() = args;
3607}
3608
3609void
3610TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3611{
3612 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3613 Args args;
3614 if (this_->GetEnvironmentAsArgs(args))
3615 this_->m_launch_info.GetEnvironmentEntries() = args;
3616}
3617
3618void
3619TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3620{
3621 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003622 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00003623}
3624
3625void
3626TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3627{
3628 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003629 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00003630}
3631
3632void
3633TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3634{
3635 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003636 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00003637}
3638
3639void
3640TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
3641{
3642 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3643 if (this_->GetDetachOnError())
3644 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3645 else
3646 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3647}
3648
3649void
3650TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
3651{
3652 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3653 if (this_->GetDisableASLR())
3654 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3655 else
3656 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3657}
3658
3659void
3660TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
3661{
3662 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3663 if (this_->GetDisableSTDIO())
3664 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
3665 else
3666 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
3667}
Ilia Keb2c19a2015-03-10 21:59:55 +00003668
3669//----------------------------------------------------------------------
3670// Target::TargetEventData
3671//----------------------------------------------------------------------
3672
3673Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
3674 EventData (),
3675 m_target_sp (target_sp),
3676 m_module_list ()
3677{
3678}
3679
3680Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
3681 EventData (),
3682 m_target_sp (target_sp),
3683 m_module_list (module_list)
3684{
3685}
3686
3687Target::TargetEventData::~TargetEventData()
3688{
3689}
3690
3691const ConstString &
3692Target::TargetEventData::GetFlavorString ()
3693{
3694 static ConstString g_flavor ("Target::TargetEventData");
3695 return g_flavor;
3696}
3697
3698void
3699Target::TargetEventData::Dump (Stream *s) const
3700{
3701}
3702
3703const Target::TargetEventData *
3704Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3705{
3706 if (event_ptr)
3707 {
3708 const EventData *event_data = event_ptr->GetData();
3709 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3710 return static_cast <const TargetEventData *> (event_ptr->GetData());
3711 }
3712 return NULL;
3713}
3714
3715TargetSP
3716Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
3717{
3718 TargetSP target_sp;
3719 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
3720 if (event_data)
3721 target_sp = event_data->m_target_sp;
3722 return target_sp;
3723}
3724
3725ModuleList
3726Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
3727{
3728 ModuleList module_list;
3729 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
3730 if (event_data)
3731 module_list = event_data->m_module_list;
3732 return module_list;
3733}