blob: 7ea29a4b1b710eda23ccab7c6b1066e0ff130201 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000028#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000029#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000030#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Jim Ingham151c0322015-09-15 21:13:50 +000034#include "lldb/Expression/UserExpression.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000035#include "Plugins/ExpressionParser/Clang/ClangASTSource.h"
36#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
37#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000038#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000040#include "lldb/Interpreter/CommandInterpreter.h"
41#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000042#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000043#include "lldb/Interpreter/OptionValues.h"
44#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000045#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ObjectFile.h"
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000047#include "lldb/Symbol/Function.h"
48#include "lldb/Symbol/Symbol.h"
Jim Ingham151c0322015-09-15 21:13:50 +000049#include "lldb/Target/Language.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000050#include "lldb/Target/LanguageRuntime.h"
51#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000053#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000054#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000055#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000056#include "lldb/Target/Thread.h"
57#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
59using namespace lldb;
60using namespace lldb_private;
61
Jim Ingham4bddaeb2012-02-16 06:50:00 +000062ConstString &
63Target::GetStaticBroadcasterClass ()
64{
65 static ConstString class_name ("lldb.target");
66 return class_name;
67}
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069//----------------------------------------------------------------------
70// Target constructor
71//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000072Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000073 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000074 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000075 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000076 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000077 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000078 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000079 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000080 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000081 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 m_breakpoint_list (false),
83 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000084 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000085 m_process_sp (),
86 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000088 m_ast_importer_ap (),
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 ();
Zachary Turner7529df92015-09-01 20:02:29 +0000204 m_process_sp = Process::FindPlugin(shared_from_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);
Greg Clayton3418c852011-08-10 02:10:13 +0000231 m_stop_hooks.clear();
232 m_stop_hook_next_id = 0;
233 m_suppress_stop_hooks = false;
234}
235
236
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237BreakpointList &
238Target::GetBreakpointList(bool internal)
239{
240 if (internal)
241 return m_internal_breakpoint_list;
242 else
243 return m_breakpoint_list;
244}
245
246const BreakpointList &
247Target::GetBreakpointList(bool internal) const
248{
249 if (internal)
250 return m_internal_breakpoint_list;
251 else
252 return m_breakpoint_list;
253}
254
255BreakpointSP
256Target::GetBreakpointByID (break_id_t break_id)
257{
258 BreakpointSP bp_sp;
259
260 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
261 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
262 else
263 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
264
265 return bp_sp;
266}
267
268BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000269Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000270 const FileSpecList *source_file_spec_list,
271 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000272 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000273 bool hardware,
274 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275{
Jim Ingham87df91b2011-09-23 00:54:11 +0000276 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000277 if (move_to_nearest_code == eLazyBoolCalculate)
278 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
279 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex, !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000280 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000281}
282
283
284BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000285Target::CreateBreakpoint (const FileSpecList *containingModules,
286 const FileSpec &file,
287 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000288 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000289 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000290 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000291 bool hardware,
292 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000293{
Greg Clayton1f746072012-08-29 21:13:06 +0000294 if (check_inlines == eLazyBoolCalculate)
295 {
296 const InlineStrategy inline_strategy = GetInlineStrategy();
297 switch (inline_strategy)
298 {
299 case eInlineBreakpointsNever:
300 check_inlines = eLazyBoolNo;
301 break;
302
303 case eInlineBreakpointsHeaders:
304 if (file.IsSourceImplementationFile())
305 check_inlines = eLazyBoolNo;
306 else
307 check_inlines = eLazyBoolYes;
308 break;
309
310 case eInlineBreakpointsAlways:
311 check_inlines = eLazyBoolYes;
312 break;
313 }
314 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000315 SearchFilterSP filter_sp;
316 if (check_inlines == eLazyBoolNo)
317 {
318 // Not checking for inlines, we are looking only for matching compile units
319 FileSpecList compile_unit_list;
320 compile_unit_list.Append (file);
321 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
322 }
323 else
324 {
325 filter_sp = GetSearchFilterForModuleList (containingModules);
326 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000327 if (skip_prologue == eLazyBoolCalculate)
328 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000329 if (move_to_nearest_code == eLazyBoolCalculate)
330 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000331
Greg Clayton1f746072012-08-29 21:13:06 +0000332 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
333 file,
334 line_no,
335 check_inlines,
Ilia K055ad9b2015-05-18 13:41:01 +0000336 skip_prologue,
337 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000338 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339}
340
341
342BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000343Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000345 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000346
347 // Check for any reason we want to move this breakpoint to other address.
348 addr = GetBreakableLoadAddress(addr);
349
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000350 // Attempt to resolve our load address if possible, though it is ok if
351 // it doesn't resolve to section/offset.
352
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000353 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000354 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000355 if (!so_addr.IsValid())
356 {
357 // The address didn't resolve, so just set this as an absolute address
358 so_addr.SetOffset (addr);
359 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000360 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000361 return bp_sp;
362}
363
364BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000365Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000366{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000367 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000369 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370}
371
372BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000373Target::CreateBreakpoint (const FileSpecList *containingModules,
374 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000375 const char *func_name,
376 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000377 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000378 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000379 bool internal,
380 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000382 BreakpointSP bp_sp;
383 if (func_name)
384 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000385 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000386
387 if (skip_prologue == eLazyBoolCalculate)
388 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000389 if (language == lldb::eLanguageTypeUnknown)
390 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000391
Greg Claytond16e1e52011-07-12 17:06:17 +0000392 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
393 func_name,
394 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000395 language,
Greg Claytond16e1e52011-07-12 17:06:17 +0000396 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000397 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000398 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000399 }
400 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401}
402
Jim Inghamfab10e82012-03-06 00:37:27 +0000403lldb::BreakpointSP
404Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000405 const FileSpecList *containingSourceFiles,
406 const std::vector<std::string> &func_names,
407 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000408 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000409 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000410 bool internal,
411 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000412{
413 BreakpointSP bp_sp;
414 size_t num_names = func_names.size();
415 if (num_names > 0)
416 {
417 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000418
419 if (skip_prologue == eLazyBoolCalculate)
420 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000421 if (language == lldb::eLanguageTypeUnknown)
422 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000423
424 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000425 func_names,
426 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000427 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000428 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000429 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000430 }
431 return bp_sp;
432}
433
Jim Ingham133e0fb2012-03-03 02:05:11 +0000434BreakpointSP
435Target::CreateBreakpoint (const FileSpecList *containingModules,
436 const FileSpecList *containingSourceFiles,
437 const char *func_names[],
438 size_t num_names,
439 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000440 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000441 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000442 bool internal,
443 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000444{
445 BreakpointSP bp_sp;
446 if (num_names > 0)
447 {
448 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
449
Greg Clayton03da4cc2013-04-19 21:31:16 +0000450 if (skip_prologue == eLazyBoolCalculate)
451 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000452 if (language == lldb::eLanguageTypeUnknown)
453 language = GetLanguage();
454
Greg Clayton03da4cc2013-04-19 21:31:16 +0000455
456 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000457 func_names,
458 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000459 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000460 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000461 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000462 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000463 }
464 return bp_sp;
465}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466
467SearchFilterSP
468Target::GetSearchFilterForModule (const FileSpec *containingModule)
469{
470 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 if (containingModule != NULL)
472 {
473 // TODO: We should look into sharing module based search filters
474 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000475 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 }
477 else
478 {
479 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000480 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 filter_sp = m_search_filter_sp;
482 }
483 return filter_sp;
484}
485
Jim Ingham969795f2011-09-21 01:17:13 +0000486SearchFilterSP
487Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
488{
489 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000490 if (containingModules && containingModules->GetSize() != 0)
491 {
492 // TODO: We should look into sharing module based search filters
493 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000494 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000495 }
496 else
497 {
498 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000499 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000500 filter_sp = m_search_filter_sp;
501 }
502 return filter_sp;
503}
504
Jim Ingham87df91b2011-09-23 00:54:11 +0000505SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000506Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
507 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000508{
509 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
510 return GetSearchFilterForModuleList(containingModules);
511
512 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000513 if (containingModules == NULL)
514 {
515 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
516 // but that will take a little reworking.
517
Greg Claytone1cd1be2012-01-29 20:56:30 +0000518 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000519 }
520 else
521 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000522 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000523 }
524 return filter_sp;
525}
526
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000528Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000529 const FileSpecList *containingSourceFiles,
530 RegularExpression &func_regex,
531 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000532 bool internal,
533 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534{
Jim Ingham87df91b2011-09-23 00:54:11 +0000535 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000536 bool skip =
537 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
538 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000539 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
540 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000541 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542
Jim Ingham1460e4b2014-01-10 23:46:59 +0000543 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544}
545
Jim Ingham219ba192012-03-05 04:47:34 +0000546lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000547Target::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 +0000548{
Jim Inghama72b31c2015-04-22 19:42:18 +0000549 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
550 if (exc_bkpt_sp && additional_args)
551 {
552 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
553 if (precondition_sp && additional_args)
554 {
555 if (error)
556 *error = precondition_sp->ConfigurePrecondition(*additional_args);
557 else
558 precondition_sp->ConfigurePrecondition(*additional_args);
559 }
560 }
561 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000562}
Jim Inghama72b31c2015-04-22 19:42:18 +0000563
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000565Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566{
567 BreakpointSP bp_sp;
568 if (filter_sp && resolver_sp)
569 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000570 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000571 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000572 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000574 return bp_sp;
575}
576
577void
578Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
579{
580 if (!bp_sp)
581 return;
582 if (internal)
583 m_internal_breakpoint_list.Add (bp_sp, false);
584 else
585 m_breakpoint_list.Add (bp_sp, true);
586
587 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
588 if (log)
589 {
590 StreamString s;
591 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
592 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
593 }
594
595 bp_sp->ResolveBreakpoint();
596
597 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000598 {
599 m_last_created_breakpoint = bp_sp;
600 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601}
602
Johnny Chen86364b42011-09-20 23:28:55 +0000603bool
604Target::ProcessIsValid()
605{
606 return (m_process_sp && m_process_sp->IsAlive());
607}
608
Johnny Chenb90827e2012-06-04 23:19:54 +0000609static bool
610CheckIfWatchpointsExhausted(Target *target, Error &error)
611{
612 uint32_t num_supported_hardware_watchpoints;
613 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
614 if (rc.Success())
615 {
616 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
617 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
618 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
619 num_supported_hardware_watchpoints);
620 }
621 return false;
622}
623
Johnny Chen01a67862011-10-14 00:42:25 +0000624// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000625// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000626WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000627Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000628{
Greg Clayton5160ce52013-03-27 23:08:40 +0000629 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000630 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000631 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000632 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000633
Johnny Chen01a67862011-10-14 00:42:25 +0000634 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000635 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000636 {
637 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000638 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000639 }
Jim Inghamc6462312013-06-18 21:52:48 +0000640
Johnny Chen45e541f2011-09-14 22:20:15 +0000641 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000642 {
643 if (size == 0)
644 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
645 else
Daniel Malead01b2952012-11-29 21:49:15 +0000646 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000647 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000648 }
Jim Inghamc6462312013-06-18 21:52:48 +0000649
650 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
651 {
652 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
653 }
Johnny Chen7313a642011-09-13 01:15:36 +0000654
Johnny Chen01a67862011-10-14 00:42:25 +0000655 // Currently we only support one watchpoint per address, with total number
656 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000657
658 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000659 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 +0000660 Mutex::Locker locker;
661 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000662 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000663 if (matched_sp)
664 {
Johnny Chen0c406372011-09-14 20:23:45 +0000665 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000666 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000667 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
668 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000669 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000670 if (size == old_size && kind == old_type)
671 {
Johnny Chen01a67862011-10-14 00:42:25 +0000672 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000673 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000674 }
675 else
676 {
Johnny Chen01a67862011-10-14 00:42:25 +0000677 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000678 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
679 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000680 }
Johnny Chen3c532582011-09-13 23:29:31 +0000681 }
682
Jason Molenda727e3922012-12-05 23:07:34 +0000683 if (!wp_sp)
684 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000685 wp_sp.reset(new Watchpoint(*this, addr, size, type));
686 wp_sp->SetWatchpointType(kind, notify);
687 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000688 }
Johnny Chen0c406372011-09-14 20:23:45 +0000689
Jim Ingham1b5792e2012-12-18 02:03:49 +0000690 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000691 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000692 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
693 __FUNCTION__,
694 error.Success() ? "succeeded" : "failed",
695 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000696
Jason Molenda727e3922012-12-05 23:07:34 +0000697 if (error.Fail())
698 {
Johnny Chen41b77262012-03-26 22:00:10 +0000699 // Enabling the watchpoint on the device side failed.
700 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000701 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000702 // See if we could provide more helpful error message.
703 if (!CheckIfWatchpointsExhausted(this, error))
704 {
705 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000706 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000707 }
Johnny Chen01a67862011-10-14 00:42:25 +0000708 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000709 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000710 else
Johnny Chen01a67862011-10-14 00:42:25 +0000711 m_last_created_watchpoint = wp_sp;
712 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000713}
714
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715void
716Target::RemoveAllBreakpoints (bool internal_also)
717{
Greg Clayton5160ce52013-03-27 23:08:40 +0000718 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 if (log)
720 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
721
Greg Clayton9fed0d82010-07-23 23:33:17 +0000722 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000724 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000725
726 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727}
728
729void
730Target::DisableAllBreakpoints (bool internal_also)
731{
Greg Clayton5160ce52013-03-27 23:08:40 +0000732 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 if (log)
734 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
735
736 m_breakpoint_list.SetEnabledAll (false);
737 if (internal_also)
738 m_internal_breakpoint_list.SetEnabledAll (false);
739}
740
741void
742Target::EnableAllBreakpoints (bool internal_also)
743{
Greg Clayton5160ce52013-03-27 23:08:40 +0000744 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745 if (log)
746 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
747
748 m_breakpoint_list.SetEnabledAll (true);
749 if (internal_also)
750 m_internal_breakpoint_list.SetEnabledAll (true);
751}
752
753bool
754Target::RemoveBreakpointByID (break_id_t break_id)
755{
Greg Clayton5160ce52013-03-27 23:08:40 +0000756 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 if (log)
758 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
759
760 if (DisableBreakpointByID (break_id))
761 {
762 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000763 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000765 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000766 if (m_last_created_breakpoint)
767 {
768 if (m_last_created_breakpoint->GetID() == break_id)
769 m_last_created_breakpoint.reset();
770 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000771 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000772 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 return true;
774 }
775 return false;
776}
777
778bool
779Target::DisableBreakpointByID (break_id_t break_id)
780{
Greg Clayton5160ce52013-03-27 23:08:40 +0000781 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000782 if (log)
783 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
784
785 BreakpointSP bp_sp;
786
787 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
788 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
789 else
790 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
791 if (bp_sp)
792 {
793 bp_sp->SetEnabled (false);
794 return true;
795 }
796 return false;
797}
798
799bool
800Target::EnableBreakpointByID (break_id_t break_id)
801{
Greg Clayton5160ce52013-03-27 23:08:40 +0000802 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 if (log)
804 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
805 __FUNCTION__,
806 break_id,
807 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
808
809 BreakpointSP bp_sp;
810
811 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
812 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
813 else
814 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
815
816 if (bp_sp)
817 {
818 bp_sp->SetEnabled (true);
819 return true;
820 }
821 return false;
822}
823
Johnny Chenedf50372011-09-23 21:21:43 +0000824// The flag 'end_to_end', default to true, signifies that the operation is
825// performed end to end, for both the debugger and the debuggee.
826
Johnny Chen01a67862011-10-14 00:42:25 +0000827// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
828// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000829bool
Johnny Chen01a67862011-10-14 00:42:25 +0000830Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000831{
Greg Clayton5160ce52013-03-27 23:08:40 +0000832 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000833 if (log)
834 log->Printf ("Target::%s\n", __FUNCTION__);
835
Johnny Chenedf50372011-09-23 21:21:43 +0000836 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000837 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000838 return true;
839 }
840
841 // Otherwise, it's an end to end operation.
842
Johnny Chen86364b42011-09-20 23:28:55 +0000843 if (!ProcessIsValid())
844 return false;
845
Johnny Chen01a67862011-10-14 00:42:25 +0000846 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000847 for (size_t i = 0; i < num_watchpoints; ++i)
848 {
Johnny Chen01a67862011-10-14 00:42:25 +0000849 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
850 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000851 return false;
852
Johnny Chen01a67862011-10-14 00:42:25 +0000853 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000854 if (rc.Fail())
855 return false;
856 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000857 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000858 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000859 return true; // Success!
860}
861
Johnny Chen01a67862011-10-14 00:42:25 +0000862// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
863// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000864bool
Johnny Chen01a67862011-10-14 00:42:25 +0000865Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000866{
Greg Clayton5160ce52013-03-27 23:08:40 +0000867 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000868 if (log)
869 log->Printf ("Target::%s\n", __FUNCTION__);
870
Johnny Chenedf50372011-09-23 21:21:43 +0000871 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000872 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000873 return true;
874 }
875
876 // Otherwise, it's an end to end operation.
877
Johnny Chen86364b42011-09-20 23:28:55 +0000878 if (!ProcessIsValid())
879 return false;
880
Johnny Chen01a67862011-10-14 00:42:25 +0000881 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000882 for (size_t i = 0; i < num_watchpoints; ++i)
883 {
Johnny Chen01a67862011-10-14 00:42:25 +0000884 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
885 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000886 return false;
887
Johnny Chen01a67862011-10-14 00:42:25 +0000888 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000889 if (rc.Fail())
890 return false;
891 }
Johnny Chen86364b42011-09-20 23:28:55 +0000892 return true; // Success!
893}
894
Johnny Chen01a67862011-10-14 00:42:25 +0000895// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
896// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000897bool
Johnny Chen01a67862011-10-14 00:42:25 +0000898Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000899{
Greg Clayton5160ce52013-03-27 23:08:40 +0000900 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000901 if (log)
902 log->Printf ("Target::%s\n", __FUNCTION__);
903
Johnny Chenedf50372011-09-23 21:21:43 +0000904 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000905 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000906 return true;
907 }
908
909 // Otherwise, it's an end to end operation.
910
Johnny Chen86364b42011-09-20 23:28:55 +0000911 if (!ProcessIsValid())
912 return false;
913
Johnny Chen01a67862011-10-14 00:42:25 +0000914 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000915 for (size_t i = 0; i < num_watchpoints; ++i)
916 {
Johnny Chen01a67862011-10-14 00:42:25 +0000917 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
918 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000919 return false;
920
Johnny Chen01a67862011-10-14 00:42:25 +0000921 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000922 if (rc.Fail())
923 return false;
924 }
Johnny Chen86364b42011-09-20 23:28:55 +0000925 return true; // Success!
926}
927
Johnny Chena4d6bc92012-02-25 06:44:30 +0000928// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
929bool
930Target::ClearAllWatchpointHitCounts ()
931{
Greg Clayton5160ce52013-03-27 23:08:40 +0000932 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000933 if (log)
934 log->Printf ("Target::%s\n", __FUNCTION__);
935
936 size_t num_watchpoints = m_watchpoint_list.GetSize();
937 for (size_t i = 0; i < num_watchpoints; ++i)
938 {
939 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
940 if (!wp_sp)
941 return false;
942
943 wp_sp->ResetHitCount();
944 }
945 return true; // Success!
946}
947
Enrico Granata5e3fe042015-02-11 00:37:54 +0000948// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
949bool
950Target::ClearAllWatchpointHistoricValues ()
951{
952 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
953 if (log)
954 log->Printf ("Target::%s\n", __FUNCTION__);
955
956 size_t num_watchpoints = m_watchpoint_list.GetSize();
957 for (size_t i = 0; i < num_watchpoints; ++i)
958 {
959 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
960 if (!wp_sp)
961 return false;
962
963 wp_sp->ResetHistoricValues();
964 }
965 return true; // Success!
966}
967
Johnny Chen01a67862011-10-14 00:42:25 +0000968// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000969// during these operations.
970bool
Johnny Chen01a67862011-10-14 00:42:25 +0000971Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000972{
Greg Clayton5160ce52013-03-27 23:08:40 +0000973 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000974 if (log)
975 log->Printf ("Target::%s\n", __FUNCTION__);
976
977 if (!ProcessIsValid())
978 return false;
979
Johnny Chen01a67862011-10-14 00:42:25 +0000980 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000981 for (size_t i = 0; i < num_watchpoints; ++i)
982 {
Johnny Chen01a67862011-10-14 00:42:25 +0000983 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
984 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000985 return false;
986
Johnny Chen01a67862011-10-14 00:42:25 +0000987 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000988 }
989 return true; // Success!
990}
991
Johnny Chen01a67862011-10-14 00:42:25 +0000992// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000993bool
Johnny Chen01a67862011-10-14 00:42:25 +0000994Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000995{
Greg Clayton5160ce52013-03-27 23:08:40 +0000996 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000997 if (log)
998 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
999
1000 if (!ProcessIsValid())
1001 return false;
1002
Johnny Chen01a67862011-10-14 00:42:25 +00001003 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1004 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001005 {
Johnny Chen01a67862011-10-14 00:42:25 +00001006 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001007 if (rc.Success())
1008 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001009
Johnny Chenf04ee932011-09-22 18:04:58 +00001010 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001011 }
1012 return false;
1013}
1014
Johnny Chen01a67862011-10-14 00:42:25 +00001015// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001016bool
Johnny Chen01a67862011-10-14 00:42:25 +00001017Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001018{
Greg Clayton5160ce52013-03-27 23:08:40 +00001019 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001020 if (log)
1021 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1022
1023 if (!ProcessIsValid())
1024 return false;
1025
Johnny Chen01a67862011-10-14 00:42:25 +00001026 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1027 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001028 {
Johnny Chen01a67862011-10-14 00:42:25 +00001029 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001030 if (rc.Success())
1031 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001032
Johnny Chenf04ee932011-09-22 18:04:58 +00001033 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001034 }
1035 return false;
1036}
1037
Johnny Chen01a67862011-10-14 00:42:25 +00001038// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001039bool
Johnny Chen01a67862011-10-14 00:42:25 +00001040Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001041{
Greg Clayton5160ce52013-03-27 23:08:40 +00001042 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001043 if (log)
1044 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1045
Jim Inghamb0b45132013-07-02 02:09:46 +00001046 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1047 if (watch_to_remove_sp == m_last_created_watchpoint)
1048 m_last_created_watchpoint.reset();
1049
Johnny Chen01a67862011-10-14 00:42:25 +00001050 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001051 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001052 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001053 return true;
1054 }
1055 return false;
1056}
1057
Johnny Chen01a67862011-10-14 00:42:25 +00001058// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001059bool
Johnny Chen01a67862011-10-14 00:42:25 +00001060Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001061{
Greg Clayton5160ce52013-03-27 23:08:40 +00001062 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001063 if (log)
1064 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1065
1066 if (!ProcessIsValid())
1067 return false;
1068
Johnny Chen01a67862011-10-14 00:42:25 +00001069 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1070 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001071 {
Johnny Chen01a67862011-10-14 00:42:25 +00001072 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001073 return true;
1074 }
1075 return false;
1076}
1077
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078ModuleSP
1079Target::GetExecutableModule ()
1080{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001081 // search for the first executable in the module list
1082 for (size_t i = 0; i < m_images.GetSize(); ++i)
1083 {
1084 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1085 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1086 if (obj == nullptr)
1087 continue;
1088 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1089 return module_sp;
1090 }
1091 // as fall back return the first module loaded
1092 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001093}
1094
1095Module*
1096Target::GetExecutableModulePointer ()
1097{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001098 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001099}
1100
Enrico Granata17598482012-11-08 02:22:02 +00001101static void
1102LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1103{
1104 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001105 StreamString feedback_stream;
1106 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001107 {
Enrico Granata97303392013-05-21 00:00:30 +00001108 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001109 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001110 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1111 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001112 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001113 if (feedback_stream.GetSize())
1114 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1115 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001116}
1117
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001118void
Greg Claytonb35db632013-11-09 00:03:31 +00001119Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120{
Greg Claytonb35db632013-11-09 00:03:31 +00001121 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001122 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123 m_images.Clear();
Greg Clayton5beec212015-10-08 21:04:34 +00001124 m_scratch_type_system_map.Clear();
Sean Callanan686b2312011-11-16 18:20:47 +00001125 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001126}
1127
1128void
Greg Claytonb35db632013-11-09 00:03:31 +00001129Target::DidExec ()
1130{
1131 // When a process exec's we need to know about it so we can do some cleanup.
1132 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1133 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1134}
1135
1136void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001137Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1138{
1139 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001140 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001141
1142 if (executable_sp.get())
1143 {
1144 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001145 "Target::SetExecutableModule (executable = '%s')",
1146 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001148 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149
Jim Ingham5aee1622010-08-09 23:31:02 +00001150 // 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 +00001151 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001152 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001153 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001154 if (log)
1155 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1156 }
1157
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001159 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001161 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162 {
1163 executable_objfile->GetDependentModules(dependent_files);
1164 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1165 {
Greg Claytonded470d2011-03-19 01:12:21 +00001166 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1167 FileSpec platform_dependent_file_spec;
1168 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001169 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001170 else
1171 platform_dependent_file_spec = dependent_file_spec;
1172
Greg Claytonb9a01b32012-02-26 05:51:37 +00001173 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1174 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175 if (image_module_sp.get())
1176 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 ObjectFile *objfile = image_module_sp->GetObjectFile();
1178 if (objfile)
1179 objfile->GetDependentModules(dependent_files);
1180 }
1181 }
1182 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183 }
1184}
1185
1186
Jim Ingham5aee1622010-08-09 23:31:02 +00001187bool
1188Target::SetArchitecture (const ArchSpec &arch_spec)
1189{
Greg Clayton5160ce52013-03-27 23:08:40 +00001190 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001191 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001192 {
Greg Clayton70512312012-05-08 01:45:38 +00001193 // If we haven't got a valid arch spec, or the architectures are
1194 // compatible, so just update the architecture. Architectures can be
1195 // equal, yet the triple OS and vendor might change, so we need to do
1196 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001197 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001198 if (log)
1199 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 +00001200 return true;
1201 }
1202 else
1203 {
1204 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001205 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001206 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 +00001207 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001208 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001209
Greg Claytonb35db632013-11-09 00:03:31 +00001210 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001211 // Need to do something about unsetting breakpoints.
1212
1213 if (executable_sp)
1214 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001215 if (log)
1216 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 +00001217 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1218 Error error = ModuleList::GetSharedModule (module_spec,
1219 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001220 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001221 NULL,
1222 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001223
1224 if (!error.Fail() && executable_sp)
1225 {
1226 SetExecutableModule (executable_sp, true);
1227 return true;
1228 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001229 }
1230 }
Greg Clayton70512312012-05-08 01:45:38 +00001231 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001232}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001234bool
1235Target::MergeArchitecture (const ArchSpec &arch_spec)
1236{
1237 if (arch_spec.IsValid())
1238 {
1239 if (m_arch.IsCompatibleMatch(arch_spec))
1240 {
1241 // The current target arch is compatible with "arch_spec", see if we
1242 // can improve our current architecture using bits from "arch_spec"
1243
1244 // Merge bits from arch_spec into "merged_arch" and set our architecture
1245 ArchSpec merged_arch (m_arch);
1246 merged_arch.MergeFrom (arch_spec);
1247 return SetArchitecture(merged_arch);
1248 }
1249 else
1250 {
1251 // The new architecture is different, we just need to replace it
1252 return SetArchitecture(arch_spec);
1253 }
1254 }
1255 return false;
1256}
1257
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258void
Enrico Granataefe637d2012-11-08 19:16:03 +00001259Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001260{
1261}
1262
1263void
Enrico Granataefe637d2012-11-08 19:16:03 +00001264Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001265{
1266 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001267 if (m_valid)
1268 {
1269 ModuleList my_module_list;
1270 my_module_list.Append(module_sp);
1271 LoadScriptingResourceForModule(module_sp, this);
1272 ModulesDidLoad (my_module_list);
1273 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274}
1275
1276void
Enrico Granataefe637d2012-11-08 19:16:03 +00001277Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001278{
1279 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001280 if (m_valid)
1281 {
1282 ModuleList my_module_list;
1283 my_module_list.Append(module_sp);
1284 ModulesDidUnload (my_module_list, false);
1285 }
Enrico Granata17598482012-11-08 02:22:02 +00001286}
1287
1288void
Enrico Granataefe637d2012-11-08 19:16:03 +00001289Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290{
Jim Inghame716ae02011-08-03 01:00:06 +00001291 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001292 if (m_valid)
1293 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294}
1295
1296void
1297Target::ModulesDidLoad (ModuleList &module_list)
1298{
Greg Clayton23f8c952014-03-24 23:10:19 +00001299 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001300 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001301 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001302 if (m_process_sp)
1303 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001304 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001305 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001306 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001307 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001308}
1309
1310void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001311Target::SymbolsDidLoad (ModuleList &module_list)
1312{
Greg Clayton23f8c952014-03-24 23:10:19 +00001313 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001314 {
Jim Ingham31caf982013-06-04 23:01:35 +00001315 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001316 {
Jim Ingham31caf982013-06-04 23:01:35 +00001317 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1318 if (runtime)
1319 {
1320 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1321 objc_runtime->SymbolsDidLoad(module_list);
1322 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001323 }
Jim Ingham31caf982013-06-04 23:01:35 +00001324
Greg Clayton095eeaa2013-11-05 23:28:00 +00001325 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001326 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001327 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001328}
1329
1330void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001331Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001332{
Greg Clayton23f8c952014-03-24 23:10:19 +00001333 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001334 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001335 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001336 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001337 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001338 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339}
1340
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001341bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001342Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001343{
Greg Clayton67cc0632012-08-22 17:17:09 +00001344 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001345 {
1346 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001347 ModuleSpec module_spec (module_file_spec);
1348 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001349
1350 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1351 // black list.
1352 if (num_modules > 0)
1353 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001354 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001355 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001356 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001357 return false;
1358 }
1359 return true;
1360 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001361 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001362 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001363}
1364
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001365bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001366Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001367{
Greg Clayton67cc0632012-08-22 17:17:09 +00001368 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001369 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001370 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001371 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001372 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001373 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001374}
1375
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001376size_t
Greg Claytondb598232011-01-07 01:57:07 +00001377Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1378{
Greg Claytone72dfb32012-02-24 01:59:29 +00001379 SectionSP section_sp (addr.GetSection());
1380 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001381 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001382 // 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 +00001383 if (section_sp->IsEncrypted())
1384 {
Greg Clayton57f06302012-05-25 17:05:55 +00001385 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001386 return 0;
1387 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001388 ModuleSP module_sp (section_sp->GetModule());
1389 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001390 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001391 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1392 if (objfile)
1393 {
1394 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1395 addr.GetOffset(),
1396 dst,
1397 dst_len);
1398 if (bytes_read > 0)
1399 return bytes_read;
1400 else
1401 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1402 }
Greg Claytondb598232011-01-07 01:57:07 +00001403 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001404 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001405 }
1406 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001407 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001408 }
1409 else
Greg Claytondb598232011-01-07 01:57:07 +00001410 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001411
Greg Claytondb598232011-01-07 01:57:07 +00001412 return 0;
1413}
1414
1415size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001416Target::ReadMemory (const Address& addr,
1417 bool prefer_file_cache,
1418 void *dst,
1419 size_t dst_len,
1420 Error &error,
1421 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001422{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001424
Enrico Granata9128ee22011-09-06 19:20:51 +00001425 // if we end up reading this from process memory, we will fill this
1426 // with the actual load address
1427 if (load_addr_ptr)
1428 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1429
Greg Claytondb598232011-01-07 01:57:07 +00001430 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001431
1432 addr_t load_addr = LLDB_INVALID_ADDRESS;
1433 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001434 Address resolved_addr;
1435 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001436 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001437 SectionLoadList &section_load_list = GetSectionLoadList();
1438 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001439 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001440 // No sections are loaded, so we must assume we are not running
1441 // yet and anything we are given is a file address.
1442 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1443 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001444 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001445 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001446 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001447 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001448 // we have manually loaded some sections with "target modules load ..."
1449 // or because we have have a live process that has sections loaded
1450 // through the dynamic loader
1451 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001452 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001453 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001454 }
Greg Clayton357132e2011-03-26 19:14:58 +00001455 if (!resolved_addr.IsValid())
1456 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001457
Greg Claytonc749eb82011-07-11 05:12:02 +00001458
Greg Claytondb598232011-01-07 01:57:07 +00001459 if (prefer_file_cache)
1460 {
1461 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1462 if (bytes_read > 0)
1463 return bytes_read;
1464 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001465
Johnny Chen86364b42011-09-20 23:28:55 +00001466 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001467 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001468 if (load_addr == LLDB_INVALID_ADDRESS)
1469 load_addr = resolved_addr.GetLoadAddress (this);
1470
Greg Claytondda4f7b2010-06-30 23:03:03 +00001471 if (load_addr == LLDB_INVALID_ADDRESS)
1472 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001473 ModuleSP addr_module_sp (resolved_addr.GetModule());
1474 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001475 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001476 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001477 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001478 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001479 else
Daniel Malead01b2952012-11-29 21:49:15 +00001480 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001481 }
1482 else
1483 {
Greg Claytondb598232011-01-07 01:57:07 +00001484 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485 if (bytes_read != dst_len)
1486 {
1487 if (error.Success())
1488 {
1489 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001490 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491 else
Daniel Malead01b2952012-11-29 21:49:15 +00001492 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 +00001493 }
1494 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001495 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001496 {
1497 if (load_addr_ptr)
1498 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001499 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001500 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001501 // If the address is not section offset we have an address that
1502 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001503 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001504 // more we can do. If it is section offset, we might be able to
1505 // read cached memory from the object file.
1506 if (!resolved_addr.IsSectionOffset())
1507 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001508 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001510
Greg Claytonc749eb82011-07-11 05:12:02 +00001511 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001512 {
Greg Claytondb598232011-01-07 01:57:07 +00001513 // If we didn't already try and read from the object file cache, then
1514 // try it after failing to read from the process.
1515 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001516 }
1517 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001518}
1519
Greg Claytond16e1e52011-07-12 17:06:17 +00001520size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001521Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1522{
1523 char buf[256];
1524 out_str.clear();
1525 addr_t curr_addr = addr.GetLoadAddress(this);
1526 Address address(addr);
1527 while (1)
1528 {
1529 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1530 if (length == 0)
1531 break;
1532 out_str.append(buf, length);
1533 // If we got "length - 1" bytes, we didn't get the whole C string, we
1534 // need to read some more characters
1535 if (length == sizeof(buf) - 1)
1536 curr_addr += length;
1537 else
1538 break;
1539 address = Address(curr_addr);
1540 }
1541 return out_str.size();
1542}
1543
1544
1545size_t
1546Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1547{
1548 size_t total_cstr_len = 0;
1549 if (dst && dst_max_len)
1550 {
1551 result_error.Clear();
1552 // NULL out everything just to be safe
1553 memset (dst, 0, dst_max_len);
1554 Error error;
1555 addr_t curr_addr = addr.GetLoadAddress(this);
1556 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001557
1558 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1559 // think this really needs to be tied to the memory cache subsystem's
1560 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001561 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001562
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001563 size_t bytes_left = dst_max_len - 1;
1564 char *curr_dst = dst;
1565
1566 while (bytes_left > 0)
1567 {
1568 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1569 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1570 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1571
1572 if (bytes_read == 0)
1573 {
1574 result_error = error;
1575 dst[total_cstr_len] = '\0';
1576 break;
1577 }
1578 const size_t len = strlen(curr_dst);
1579
1580 total_cstr_len += len;
1581
1582 if (len < bytes_to_read)
1583 break;
1584
1585 curr_dst += bytes_read;
1586 curr_addr += bytes_read;
1587 bytes_left -= bytes_read;
1588 address = Address(curr_addr);
1589 }
1590 }
1591 else
1592 {
1593 if (dst == NULL)
1594 result_error.SetErrorString("invalid arguments");
1595 else
1596 result_error.Clear();
1597 }
1598 return total_cstr_len;
1599}
1600
1601size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001602Target::ReadScalarIntegerFromMemory (const Address& addr,
1603 bool prefer_file_cache,
1604 uint32_t byte_size,
1605 bool is_signed,
1606 Scalar &scalar,
1607 Error &error)
1608{
1609 uint64_t uval;
1610
1611 if (byte_size <= sizeof(uval))
1612 {
1613 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1614 if (bytes_read == byte_size)
1615 {
1616 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001617 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001618 if (byte_size <= 4)
1619 scalar = data.GetMaxU32 (&offset, byte_size);
1620 else
1621 scalar = data.GetMaxU64 (&offset, byte_size);
1622
1623 if (is_signed)
1624 scalar.SignExtend(byte_size * 8);
1625 return bytes_read;
1626 }
1627 }
1628 else
1629 {
1630 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1631 }
1632 return 0;
1633}
1634
1635uint64_t
1636Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1637 bool prefer_file_cache,
1638 size_t integer_byte_size,
1639 uint64_t fail_value,
1640 Error &error)
1641{
1642 Scalar scalar;
1643 if (ReadScalarIntegerFromMemory (addr,
1644 prefer_file_cache,
1645 integer_byte_size,
1646 false,
1647 scalar,
1648 error))
1649 return scalar.ULongLong(fail_value);
1650 return fail_value;
1651}
1652
1653bool
1654Target::ReadPointerFromMemory (const Address& addr,
1655 bool prefer_file_cache,
1656 Error &error,
1657 Address &pointer_addr)
1658{
1659 Scalar scalar;
1660 if (ReadScalarIntegerFromMemory (addr,
1661 prefer_file_cache,
1662 m_arch.GetAddressByteSize(),
1663 false,
1664 scalar,
1665 error))
1666 {
1667 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1668 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1669 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001670 SectionLoadList &section_load_list = GetSectionLoadList();
1671 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001672 {
1673 // No sections are loaded, so we must assume we are not running
1674 // yet and anything we are given is a file address.
1675 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1676 }
1677 else
1678 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001679 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001680 // we have manually loaded some sections with "target modules load ..."
1681 // or because we have have a live process that has sections loaded
1682 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001683 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001684 }
1685 // We weren't able to resolve the pointer value, so just return
1686 // an address with no section
1687 if (!pointer_addr.IsValid())
1688 pointer_addr.SetOffset (pointer_vm_addr);
1689 return true;
1690
1691 }
1692 }
1693 return false;
1694}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001695
1696ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001697Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001698{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 ModuleSP module_sp;
1700
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701 Error error;
1702
Jim Ingham4a94c912012-05-17 18:38:42 +00001703 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1704 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001705
Jim Ingham4a94c912012-05-17 18:38:42 +00001706 if (module_spec.GetUUID().IsValid())
1707 module_sp = m_images.FindFirstModule(module_spec);
1708
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001709 if (!module_sp)
1710 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001711 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1712 bool did_create_module = false;
1713
1714 // If there are image search path entries, try to use them first to acquire a suitable image.
1715 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001716 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001717 ModuleSpec transformed_spec (module_spec);
1718 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1719 {
1720 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1721 error = ModuleList::GetSharedModule (transformed_spec,
1722 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001723 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001724 &old_module_sp,
1725 &did_create_module);
1726 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001727 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001728
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001729 if (!module_sp)
1730 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001731 // If we have a UUID, we can check our global shared module list in case
1732 // we already have it. If we don't have a valid UUID, then we can't since
1733 // the path in "module_spec" will be a platform path, and we will need to
1734 // let the platform find that file. For example, we could be asking for
1735 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1736 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1737 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1738 // cache.
1739 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001740 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001741 // We have a UUID, it is OK to check the global module list...
1742 error = ModuleList::GetSharedModule (module_spec,
1743 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001744 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001745 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001746 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001747 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001748
1749 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001750 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001751 // The platform is responsible for finding and caching an appropriate
1752 // module in the shared module cache.
1753 if (m_platform_sp)
1754 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001755 error = m_platform_sp->GetSharedModule (module_spec,
1756 m_process_sp.get(),
1757 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001758 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001759 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001760 &did_create_module);
1761 }
1762 else
1763 {
1764 error.SetErrorString("no platform is currently set");
1765 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001766 }
1767 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768
Jim Ingham4a94c912012-05-17 18:38:42 +00001769 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1770 // module in the list already, and if there was, let's remove it.
1771 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001773 ObjectFile *objfile = module_sp->GetObjectFile();
1774 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001775 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001776 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001777 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001778 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1779 case ObjectFile::eTypeExecutable: /// A normal executable
1780 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1781 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1782 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1783 break;
1784 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1785 if (error_ptr)
1786 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1787 return ModuleSP();
1788 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1789 if (error_ptr)
1790 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1791 return ModuleSP();
1792 default:
1793 if (error_ptr)
1794 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1795 return ModuleSP();
1796 }
1797 // GetSharedModule is not guaranteed to find the old shared module, for instance
1798 // in the common case where you pass in the UUID, it is only going to find the one
1799 // module matching the UUID. In fact, it has no good way to know what the "old module"
1800 // relevant to this target is, since there might be many copies of a module with this file spec
1801 // in various running debug sessions, but only one of them will belong to this target.
1802 // So let's remove the UUID from the module list, and look in the target's module list.
1803 // Only do this if there is SOMETHING else in the module spec...
1804 if (!old_module_sp)
1805 {
1806 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001807 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001808 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1809 module_spec_copy.GetUUID().Clear();
1810
1811 ModuleList found_modules;
1812 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1813 if (num_found == 1)
1814 {
1815 old_module_sp = found_modules.GetModuleAtIndex(0);
1816 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001817 }
1818 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001819
1820 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1821 {
1822 m_images.ReplaceModule(old_module_sp, module_sp);
1823 Module *old_module_ptr = old_module_sp.get();
1824 old_module_sp.reset();
1825 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1826 }
1827 else
1828 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001829 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001830 else
1831 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001832 }
1833 }
1834 if (error_ptr)
1835 *error_ptr = error;
1836 return module_sp;
1837}
1838
1839
Greg Claytond9e416c2012-02-18 05:35:26 +00001840TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001841Target::CalculateTarget ()
1842{
Greg Claytond9e416c2012-02-18 05:35:26 +00001843 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001844}
1845
Greg Claytond9e416c2012-02-18 05:35:26 +00001846ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847Target::CalculateProcess ()
1848{
Greg Claytond9e416c2012-02-18 05:35:26 +00001849 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001850}
1851
Greg Claytond9e416c2012-02-18 05:35:26 +00001852ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853Target::CalculateThread ()
1854{
Greg Claytond9e416c2012-02-18 05:35:26 +00001855 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001856}
1857
Jason Molendab57e4a12013-11-04 09:33:30 +00001858StackFrameSP
1859Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001860{
Jason Molendab57e4a12013-11-04 09:33:30 +00001861 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862}
1863
1864void
Greg Clayton0603aa92010-10-04 01:05:56 +00001865Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866{
Greg Claytonc14ee322011-09-22 04:58:26 +00001867 exe_ctx.Clear();
1868 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869}
1870
1871PathMappingList &
1872Target::GetImageSearchPathList ()
1873{
1874 return m_image_search_paths;
1875}
1876
1877void
1878Target::ImageSearchPathsChanged
1879(
1880 const PathMappingList &path_list,
1881 void *baton
1882)
1883{
1884 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001885 ModuleSP exe_module_sp (target->GetExecutableModule());
1886 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001887 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001888}
1889
Jim Ingham151c0322015-09-15 21:13:50 +00001890TypeSystem *
Sean Callanana994b0b2015-10-02 18:40:30 +00001891Target::GetScratchTypeSystemForLanguage (Error *error, lldb::LanguageType language, bool create_on_demand)
Jim Ingham151c0322015-09-15 21:13:50 +00001892{
Greg Clayton5beec212015-10-08 21:04:34 +00001893 if (!m_valid)
1894 return nullptr;
1895
Sean Callanana994b0b2015-10-02 18:40:30 +00001896 if (error)
1897 {
1898 error->Clear();
1899 }
1900
Sean Callananb92bd752015-10-01 16:28:02 +00001901 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Sean Callanana994b0b2015-10-02 18:40:30 +00001902 || language == eLanguageTypeUnknown)
1903 {
Sean Callananc7b054a2015-10-09 18:01:10 +00001904 std::set<lldb::LanguageType> languages_for_types;
1905 std::set<lldb::LanguageType> languages_for_expressions;
1906
1907 Language::GetLanguagesSupportingTypeSystems(languages_for_types, languages_for_expressions);
1908
1909 if (languages_for_expressions.count(eLanguageTypeC))
1910 {
1911 language = eLanguageTypeC; // LLDB's default. Override by setting the target language.
1912 }
1913 else
1914 {
1915 if (languages_for_expressions.empty())
1916 {
1917 return nullptr;
1918 }
1919 else
1920 {
1921 language = *languages_for_expressions.begin();
1922 }
1923 }
Sean Callananb92bd752015-10-01 16:28:02 +00001924 }
Sean Callananb92bd752015-10-01 16:28:02 +00001925
Greg Clayton5beec212015-10-08 21:04:34 +00001926 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this, create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00001927}
1928
1929PersistentExpressionState *
1930Target::GetPersistentExpressionStateForLanguage (lldb::LanguageType language)
1931{
Sean Callanana994b0b2015-10-02 18:40:30 +00001932 TypeSystem *type_system = GetScratchTypeSystemForLanguage(nullptr, language, true);
Sean Callananb92bd752015-10-01 16:28:02 +00001933
1934 if (type_system)
1935 {
1936 return type_system->GetPersistentExpressionState();
1937 }
Jim Ingham151c0322015-09-15 21:13:50 +00001938 else
Sean Callananb92bd752015-10-01 16:28:02 +00001939 {
1940 return nullptr;
1941 }
Jim Ingham151c0322015-09-15 21:13:50 +00001942}
1943
1944UserExpression *
1945Target::GetUserExpressionForLanguage(const char *expr,
Sean Callanana994b0b2015-10-02 18:40:30 +00001946 const char *expr_prefix,
1947 lldb::LanguageType language,
1948 Expression::ResultType desired_type,
1949 Error &error)
Jim Ingham151c0322015-09-15 21:13:50 +00001950{
Sean Callanana994b0b2015-10-02 18:40:30 +00001951 Error type_system_error;
1952
1953 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00001954 UserExpression *user_expr = nullptr;
1955
1956 if (!type_system)
1957 {
Sean Callanana994b0b2015-10-02 18:40:30 +00001958 error.SetErrorStringWithFormat("Could not find type system for language %s: %s", Language::GetNameForLanguageType(language), type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00001959 return nullptr;
1960 }
1961
1962 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type);
1963 if (!user_expr)
1964 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1965
1966 return user_expr;
1967}
1968
1969FunctionCaller *
1970Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
1971 const CompilerType &return_type,
1972 const Address& function_address,
1973 const ValueList &arg_value_list,
1974 const char *name,
1975 Error &error)
1976{
Sean Callanana994b0b2015-10-02 18:40:30 +00001977 Error type_system_error;
1978 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00001979 FunctionCaller *persistent_fn = nullptr;
1980
1981 if (!type_system)
1982 {
Sean Callanana994b0b2015-10-02 18:40:30 +00001983 error.SetErrorStringWithFormat("Could not find type system for language %s: %s", Language::GetNameForLanguageType(language), type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00001984 return persistent_fn;
1985 }
1986
1987 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
1988 if (!persistent_fn)
1989 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1990
1991 return persistent_fn;
1992}
1993
1994UtilityFunction *
1995Target::GetUtilityFunctionForLanguage (const char *text,
1996 lldb::LanguageType language,
1997 const char *name,
1998 Error &error)
1999{
Sean Callanana994b0b2015-10-02 18:40:30 +00002000 Error type_system_error;
2001 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002002 UtilityFunction *utility_fn = nullptr;
2003
2004 if (!type_system)
2005 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002006 error.SetErrorStringWithFormat("Could not find type system for language %s: %s", Language::GetNameForLanguageType(language), type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00002007 return utility_fn;
2008 }
2009
2010 utility_fn = type_system->GetUtilityFunction (text, name);
2011 if (!utility_fn)
2012 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2013
2014 return utility_fn;
2015}
2016
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002017ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00002018Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019{
Greg Clayton5beec212015-10-08 21:04:34 +00002020 if (m_valid)
Sean Callanan4bf80d52011-11-15 22:27:19 +00002021 {
Greg Clayton5beec212015-10-08 21:04:34 +00002022 if (TypeSystem* type_system = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC, create_on_demand))
2023 return llvm::dyn_cast<ClangASTContext>(type_system);
Sean Callanan4bf80d52011-11-15 22:27:19 +00002024 }
Greg Clayton5beec212015-10-08 21:04:34 +00002025 return nullptr;
Sean Callananb92bd752015-10-01 16:28:02 +00002026}
2027
Caroline Ticedaccaa92010-09-20 20:44:43 +00002028
Sean Callanan686b2312011-11-16 18:20:47 +00002029ClangASTImporter *
2030Target::GetClangASTImporter()
2031{
Greg Clayton5beec212015-10-08 21:04:34 +00002032 if (m_valid)
Sean Callanan686b2312011-11-16 18:20:47 +00002033 {
Greg Clayton5beec212015-10-08 21:04:34 +00002034 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
2035
2036 if (!ast_importer)
2037 {
2038 ast_importer = new ClangASTImporter();
2039 m_ast_importer_ap.reset(ast_importer);
2040 }
2041 return ast_importer;
Sean Callanan686b2312011-11-16 18:20:47 +00002042 }
Greg Clayton5beec212015-10-08 21:04:34 +00002043 return nullptr;
Sean Callanan686b2312011-11-16 18:20:47 +00002044}
2045
Greg Clayton99d0faf2010-11-18 23:32:35 +00002046void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002047Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002048{
Greg Clayton6920b522012-08-22 18:39:03 +00002049 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002050}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002051
Greg Clayton99d0faf2010-11-18 23:32:35 +00002052void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002053Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002054{
Greg Clayton6920b522012-08-22 18:39:03 +00002055 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002056}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002057
Greg Claytonc859e2d2012-02-13 23:10:39 +00002058FileSpecList
2059Target::GetDefaultExecutableSearchPaths ()
2060{
Greg Clayton67cc0632012-08-22 17:17:09 +00002061 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2062 if (properties_sp)
2063 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002064 return FileSpecList();
2065}
2066
Michael Sartaina7499c92013-07-01 19:45:50 +00002067FileSpecList
2068Target::GetDefaultDebugFileSearchPaths ()
2069{
2070 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2071 if (properties_sp)
2072 return properties_sp->GetDebugFileSearchPaths();
2073 return FileSpecList();
2074}
2075
Sean Callanan85054342015-04-03 15:39:47 +00002076FileSpecList
2077Target::GetDefaultClangModuleSearchPaths ()
2078{
2079 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2080 if (properties_sp)
2081 return properties_sp->GetClangModuleSearchPaths();
2082 return FileSpecList();
2083}
2084
Caroline Ticedaccaa92010-09-20 20:44:43 +00002085ArchSpec
2086Target::GetDefaultArchitecture ()
2087{
Greg Clayton67cc0632012-08-22 17:17:09 +00002088 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2089 if (properties_sp)
2090 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002091 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002092}
2093
2094void
Greg Clayton67cc0632012-08-22 17:17:09 +00002095Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002096{
Greg Clayton67cc0632012-08-22 17:17:09 +00002097 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2098 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002099 {
2100 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 +00002101 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002102 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002103}
2104
Greg Clayton0603aa92010-10-04 01:05:56 +00002105Target *
2106Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2107{
2108 // The target can either exist in the "process" of ExecutionContext, or in
2109 // the "target_sp" member of SymbolContext. This accessor helper function
2110 // will get the target from one of these locations.
2111
2112 Target *target = NULL;
2113 if (sc_ptr != NULL)
2114 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00002115 if (target == NULL && exe_ctx_ptr)
2116 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002117 return target;
2118}
2119
Jim Ingham1624a2d2014-05-05 02:26:40 +00002120ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002121Target::EvaluateExpression
2122(
2123 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00002124 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00002125 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00002126 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002127)
2128{
Enrico Granata97fca502012-09-18 17:43:16 +00002129 result_valobj_sp.reset();
2130
Jim Ingham8646d3c2014-05-05 02:47:44 +00002131 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002132
Greg Claytond1767f02011-12-08 02:13:16 +00002133 if (expr_cstr == NULL || expr_cstr[0] == '\0')
2134 return execution_results;
2135
Jim Ingham6026ca32011-05-12 02:06:14 +00002136 // We shouldn't run stop hooks in expressions.
2137 // Be sure to reset this if you return anywhere within this function.
2138 bool old_suppress_value = m_suppress_stop_hooks;
2139 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002140
2141 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002142
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002143 if (frame)
2144 {
2145 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002146 }
2147 else if (m_process_sp)
2148 {
2149 m_process_sp->CalculateExecutionContext(exe_ctx);
2150 }
2151 else
2152 {
2153 CalculateExecutionContext(exe_ctx);
2154 }
2155
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002156 // Make sure we aren't just trying to see the value of a persistent
2157 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002158 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002159 // Only check for persistent variables the expression starts with a '$'
2160 if (expr_cstr[0] == '$')
Sean Callanana994b0b2015-10-02 18:40:30 +00002161 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002162
2163 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002164 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002165 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002166 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002167 }
2168 else
2169 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002170 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002171 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002172 execution_results = UserExpression::Evaluate (exe_ctx,
2173 options,
2174 expr_cstr,
2175 prefix,
2176 result_valobj_sp,
2177 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002178 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002179
2180 m_suppress_stop_hooks = old_suppress_value;
2181
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002182 return execution_results;
2183}
2184
Greg Clayton5beec212015-10-08 21:04:34 +00002185
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002186lldb::ExpressionVariableSP
2187Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002188{
Greg Clayton5beec212015-10-08 21:04:34 +00002189 lldb::ExpressionVariableSP variable_sp;
2190 m_scratch_type_system_map.ForEach([this, name, &variable_sp](TypeSystem *type_system) -> bool
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002191 {
Greg Clayton5beec212015-10-08 21:04:34 +00002192 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002193 {
Greg Clayton5beec212015-10-08 21:04:34 +00002194 variable_sp = persistent_state->GetVariable(name);
2195
2196 if (variable_sp)
2197 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002198 }
Greg Clayton5beec212015-10-08 21:04:34 +00002199 return true; // Keep iterating the ForEach
2200 });
2201 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002202}
2203
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002204lldb::addr_t
Sean Callananb92bd752015-10-01 16:28:02 +00002205Target::GetPersistentSymbol(const ConstString &name)
2206{
Greg Clayton5beec212015-10-08 21:04:34 +00002207 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Sean Callananb92bd752015-10-01 16:28:02 +00002208
Greg Clayton5beec212015-10-08 21:04:34 +00002209 m_scratch_type_system_map.ForEach([this, name, &address](TypeSystem *type_system) -> bool
Sean Callananb92bd752015-10-01 16:28:02 +00002210 {
Greg Clayton5beec212015-10-08 21:04:34 +00002211 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callananb92bd752015-10-01 16:28:02 +00002212 {
Greg Clayton5beec212015-10-08 21:04:34 +00002213 address = persistent_state->LookupSymbol(name);
2214 if (address != LLDB_INVALID_ADDRESS)
2215 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002216 }
Greg Clayton5beec212015-10-08 21:04:34 +00002217 return true; // Keep iterating the ForEach
2218 });
2219 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002220}
2221
2222lldb::addr_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002223Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2224{
2225 addr_t code_addr = load_addr;
2226 switch (m_arch.GetMachine())
2227 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002228 case llvm::Triple::mips:
2229 case llvm::Triple::mipsel:
2230 case llvm::Triple::mips64:
2231 case llvm::Triple::mips64el:
2232 switch (addr_class)
2233 {
2234 case eAddressClassData:
2235 case eAddressClassDebug:
2236 return LLDB_INVALID_ADDRESS;
2237
2238 case eAddressClassUnknown:
2239 case eAddressClassInvalid:
2240 case eAddressClassCode:
2241 case eAddressClassCodeAlternateISA:
2242 case eAddressClassRuntime:
2243 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2244 code_addr |= 1ull;
2245 break;
2246 }
2247 break;
2248
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002249 case llvm::Triple::arm:
2250 case llvm::Triple::thumb:
2251 switch (addr_class)
2252 {
2253 case eAddressClassData:
2254 case eAddressClassDebug:
2255 return LLDB_INVALID_ADDRESS;
2256
2257 case eAddressClassUnknown:
2258 case eAddressClassInvalid:
2259 case eAddressClassCode:
2260 case eAddressClassCodeAlternateISA:
2261 case eAddressClassRuntime:
2262 // Check if bit zero it no set?
2263 if ((code_addr & 1ull) == 0)
2264 {
2265 // Bit zero isn't set, check if the address is a multiple of 2?
2266 if (code_addr & 2ull)
2267 {
2268 // The address is a multiple of 2 so it must be thumb, set bit zero
2269 code_addr |= 1ull;
2270 }
2271 else if (addr_class == eAddressClassCodeAlternateISA)
2272 {
2273 // We checked the address and the address claims to be the alternate ISA
2274 // which means thumb, so set bit zero.
2275 code_addr |= 1ull;
2276 }
2277 }
2278 break;
2279 }
2280 break;
2281
2282 default:
2283 break;
2284 }
2285 return code_addr;
2286}
2287
2288lldb::addr_t
2289Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2290{
2291 addr_t opcode_addr = load_addr;
2292 switch (m_arch.GetMachine())
2293 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002294 case llvm::Triple::mips:
2295 case llvm::Triple::mipsel:
2296 case llvm::Triple::mips64:
2297 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002298 case llvm::Triple::arm:
2299 case llvm::Triple::thumb:
2300 switch (addr_class)
2301 {
2302 case eAddressClassData:
2303 case eAddressClassDebug:
2304 return LLDB_INVALID_ADDRESS;
2305
2306 case eAddressClassInvalid:
2307 case eAddressClassUnknown:
2308 case eAddressClassCode:
2309 case eAddressClassCodeAlternateISA:
2310 case eAddressClassRuntime:
2311 opcode_addr &= ~(1ull);
2312 break;
2313 }
2314 break;
2315
2316 default:
2317 break;
2318 }
2319 return opcode_addr;
2320}
2321
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002322lldb::addr_t
2323Target::GetBreakableLoadAddress (lldb::addr_t addr)
2324{
2325 addr_t breakable_addr = addr;
2326 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2327
2328 switch (m_arch.GetMachine())
2329 {
2330 default:
2331 break;
2332 case llvm::Triple::mips:
2333 case llvm::Triple::mipsel:
2334 case llvm::Triple::mips64:
2335 case llvm::Triple::mips64el:
2336 {
2337 addr_t function_start = 0;
2338 addr_t current_offset = 0;
2339 uint32_t loop_count = 0;
2340 Address resolved_addr;
2341 uint32_t arch_flags = m_arch.GetFlags ();
2342 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2343 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2344 SectionLoadList &section_load_list = GetSectionLoadList();
2345
2346 if (section_load_list.IsEmpty())
2347 // No sections are loaded, so we must assume we are not running yet
2348 // and need to operate only on file address.
2349 m_images.ResolveFileAddress (addr, resolved_addr);
2350 else
2351 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2352
2353 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2354 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2355 if (temp_addr_module_sp)
2356 {
2357 SymbolContext sc;
2358 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002359 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002360 if (sc.function)
2361 {
2362 function_start = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(this);
2363 if (function_start == LLDB_INVALID_ADDRESS)
2364 function_start = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
2365 }
2366 else if (sc.symbol)
2367 {
2368 Address sym_addr = sc.symbol->GetAddress();
2369 function_start = sym_addr.GetFileAddress();
2370 }
2371 current_offset = addr - function_start;
2372 }
2373
2374 // If breakpoint address is start of function then we dont have to do anything.
2375 if (current_offset == 0)
2376 return breakable_addr;
2377 else
2378 loop_count = current_offset / 2;
2379
2380 if (loop_count > 3)
2381 {
2382 // Scan previous 6 bytes
2383 if (IsMips16 | IsMicromips)
2384 loop_count = 3;
2385 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2386 else
2387 loop_count = 2;
2388 }
2389
2390 // Create Disassembler Instance
2391 lldb::DisassemblerSP disasm_sp (Disassembler::FindPlugin(m_arch, NULL, NULL));
2392
2393 ExecutionContext exe_ctx;
2394 CalculateExecutionContext(exe_ctx);
2395 InstructionList instruction_list;
2396 InstructionSP prev_insn;
2397 bool prefer_file_cache = true; // Read from file
2398 uint32_t inst_to_choose = 0;
2399
2400 for (uint32_t i = 1; i <= loop_count; i++)
2401 {
2402 // Adjust the address to read from.
2403 resolved_addr.Slide (-2);
2404 AddressRange range(resolved_addr, i*2);
2405 uint32_t insn_size = 0;
2406
Greg Clayton6071e6f2015-08-26 22:57:51 +00002407 disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002408
2409 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2410 if (num_insns)
2411 {
2412 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2413 insn_size = prev_insn->GetOpcode().GetByteSize();
2414 if (i == 1 && insn_size == 2)
2415 {
2416 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2417 instruction_list.Append(prev_insn);
2418 inst_to_choose = 1;
2419 }
2420 else if (i == 2)
2421 {
2422 // Here we may get one 4-byte instruction or two 2-byte instructions.
2423 if (num_insns == 2)
2424 {
2425 // Looks like there are two 2-byte instructions above our breakpoint target address.
2426 // Now the upper 2-byte instruction is either a valid 2-byte instruction or could be a part of it's upper 4-byte instruction.
2427 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2428 // and whatever i=1 iteration has found out is true.
2429 inst_to_choose = 1;
2430 break;
2431 }
2432 else if (insn_size == 4)
2433 {
2434 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2435 // Lets try scanning upper 2 bytes to verify this.
2436 instruction_list.Append(prev_insn);
2437 inst_to_choose = 2;
2438 }
2439 }
2440 else if (i == 3)
2441 {
2442 if (insn_size == 4)
2443 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2444 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2445 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2446 inst_to_choose = 0;
2447 else
2448 // This is straight-forward
2449 inst_to_choose = 2;
2450 break;
2451 }
2452 }
2453 else
2454 {
2455 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2456 if (i > 1)
2457 {
2458 inst_to_choose = i - 1;
2459 break;
2460 }
2461 }
2462 }
2463
2464 // Check if we are able to find any valid instruction.
2465 if (inst_to_choose)
2466 {
2467 if (inst_to_choose > instruction_list.GetSize())
2468 inst_to_choose--;
2469 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2470
2471 if (prev_insn->HasDelaySlot())
2472 {
2473 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2474 // Adjust the breakable address
2475 breakable_addr = addr - shift_size;
2476 if (log)
2477 log->Printf ("Target::%s Breakpoint at 0x%8.8" PRIx64 " is adjusted to 0x%8.8" PRIx64 " due to delay slot\n", __FUNCTION__, addr, breakable_addr);
2478 }
2479 }
2480 break;
2481 }
2482 }
2483 return breakable_addr;
2484}
2485
Greg Clayton9585fbf2013-03-19 00:20:55 +00002486SourceManager &
2487Target::GetSourceManager ()
2488{
2489 if (m_source_manager_ap.get() == NULL)
2490 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2491 return *m_source_manager_ap;
2492}
2493
Sean Callanan9998acd2014-12-05 01:21:59 +00002494ClangModulesDeclVendor *
2495Target::GetClangModulesDeclVendor ()
2496{
2497 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2498
2499 {
2500 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2501
2502 if (!m_clang_modules_decl_vendor_ap)
2503 {
2504 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2505 }
2506 }
2507
2508 return m_clang_modules_decl_vendor_ap.get();
2509}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002510
Greg Clayton44d93782014-01-27 23:43:24 +00002511Target::StopHookSP
2512Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002513{
2514 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002515 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2516 m_stop_hooks[new_uid] = stop_hook_sp;
2517 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002518}
2519
2520bool
2521Target::RemoveStopHookByID (lldb::user_id_t user_id)
2522{
2523 size_t num_removed;
2524 num_removed = m_stop_hooks.erase (user_id);
2525 if (num_removed == 0)
2526 return false;
2527 else
2528 return true;
2529}
2530
2531void
2532Target::RemoveAllStopHooks ()
2533{
2534 m_stop_hooks.clear();
2535}
2536
2537Target::StopHookSP
2538Target::GetStopHookByID (lldb::user_id_t user_id)
2539{
2540 StopHookSP found_hook;
2541
2542 StopHookCollection::iterator specified_hook_iter;
2543 specified_hook_iter = m_stop_hooks.find (user_id);
2544 if (specified_hook_iter != m_stop_hooks.end())
2545 found_hook = (*specified_hook_iter).second;
2546 return found_hook;
2547}
2548
2549bool
2550Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2551{
2552 StopHookCollection::iterator specified_hook_iter;
2553 specified_hook_iter = m_stop_hooks.find (user_id);
2554 if (specified_hook_iter == m_stop_hooks.end())
2555 return false;
2556
2557 (*specified_hook_iter).second->SetIsActive (active_state);
2558 return true;
2559}
2560
2561void
2562Target::SetAllStopHooksActiveState (bool active_state)
2563{
2564 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2565 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2566 {
2567 (*pos).second->SetIsActive (active_state);
2568 }
2569}
2570
2571void
2572Target::RunStopHooks ()
2573{
Jim Ingham6026ca32011-05-12 02:06:14 +00002574 if (m_suppress_stop_hooks)
2575 return;
2576
Jim Ingham9575d842011-03-11 03:53:59 +00002577 if (!m_process_sp)
2578 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002579
2580 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2581 // since in that case we do not want to run the stop-hooks
2582 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2583 return;
2584
Jim Ingham9575d842011-03-11 03:53:59 +00002585 if (m_stop_hooks.empty())
2586 return;
2587
2588 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2589
2590 // If there aren't any active stop hooks, don't bother either:
2591 bool any_active_hooks = false;
2592 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2593 {
2594 if ((*pos).second->IsActive())
2595 {
2596 any_active_hooks = true;
2597 break;
2598 }
2599 }
2600 if (!any_active_hooks)
2601 return;
2602
2603 CommandReturnObject result;
2604
2605 std::vector<ExecutionContext> exc_ctx_with_reasons;
2606 std::vector<SymbolContext> sym_ctx_with_reasons;
2607
2608 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2609 size_t num_threads = cur_threadlist.GetSize();
2610 for (size_t i = 0; i < num_threads; i++)
2611 {
2612 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2613 if (cur_thread_sp->ThreadStoppedForAReason())
2614 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002615 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002616 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2617 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2618 }
2619 }
2620
2621 // If no threads stopped for a reason, don't run the stop-hooks.
2622 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2623 if (num_exe_ctx == 0)
2624 return;
2625
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002626 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2627 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002628
2629 bool keep_going = true;
2630 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002631 bool print_hook_header;
2632 bool print_thread_header;
2633
2634 if (num_exe_ctx == 1)
2635 print_thread_header = false;
2636 else
2637 print_thread_header = true;
2638
2639 if (m_stop_hooks.size() == 1)
2640 print_hook_header = false;
2641 else
2642 print_hook_header = true;
2643
Jim Ingham9575d842011-03-11 03:53:59 +00002644 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2645 {
2646 // result.Clear();
2647 StopHookSP cur_hook_sp = (*pos).second;
2648 if (!cur_hook_sp->IsActive())
2649 continue;
2650
2651 bool any_thread_matched = false;
2652 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2653 {
2654 if ((cur_hook_sp->GetSpecifier () == NULL
2655 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2656 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002657 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002658 {
2659 if (!hooks_ran)
2660 {
Jim Ingham9575d842011-03-11 03:53:59 +00002661 hooks_ran = true;
2662 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002663 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002664 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002665 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2666 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2667 NULL);
2668 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002669 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002670 else
Daniel Malead01b2952012-11-29 21:49:15 +00002671 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002672 any_thread_matched = true;
2673 }
2674
Jim Ingham381e25b2011-03-22 01:47:27 +00002675 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002676 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002677
2678 CommandInterpreterRunOptions options;
2679 options.SetStopOnContinue (true);
2680 options.SetStopOnError (true);
2681 options.SetEchoCommands (false);
2682 options.SetPrintResults (true);
2683 options.SetAddToHistory (false);
2684
2685 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2686 &exc_ctx_with_reasons[i],
2687 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002688 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002689
2690 // If the command started the target going again, we should bag out of
2691 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002692 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2693 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002694 {
Daniel Malead01b2952012-11-29 21:49:15 +00002695 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002696 keep_going = false;
2697 }
2698 }
2699 }
2700 }
Jason Molenda879cf772011-09-23 00:42:55 +00002701
Caroline Tice969ed3d2011-05-02 20:41:46 +00002702 result.GetImmediateOutputStream()->Flush();
2703 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002704}
2705
Greg Claytonfbb76342013-11-20 21:07:01 +00002706const TargetPropertiesSP &
2707Target::GetGlobalProperties()
2708{
2709 static TargetPropertiesSP g_settings_sp;
2710 if (!g_settings_sp)
2711 {
2712 g_settings_sp.reset (new TargetProperties (NULL));
2713 }
2714 return g_settings_sp;
2715}
2716
2717Error
2718Target::Install (ProcessLaunchInfo *launch_info)
2719{
2720 Error error;
2721 PlatformSP platform_sp (GetPlatform());
2722 if (platform_sp)
2723 {
2724 if (platform_sp->IsRemote())
2725 {
2726 if (platform_sp->IsConnected())
2727 {
2728 // Install all files that have an install path, and always install the
2729 // main executable when connected to a remote platform
2730 const ModuleList& modules = GetImages();
2731 const size_t num_images = modules.GetSize();
2732 for (size_t idx = 0; idx < num_images; ++idx)
2733 {
2734 const bool is_main_executable = idx == 0;
2735 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2736 if (module_sp)
2737 {
2738 FileSpec local_file (module_sp->GetFileSpec());
2739 if (local_file)
2740 {
2741 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2742 if (!remote_file)
2743 {
2744 if (is_main_executable) // TODO: add setting for always installing main executable???
2745 {
2746 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002747 remote_file = platform_sp->GetRemoteWorkingDirectory();
2748 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002749 }
2750 }
2751 if (remote_file)
2752 {
2753 error = platform_sp->Install(local_file, remote_file);
2754 if (error.Success())
2755 {
2756 module_sp->SetPlatformFileSpec(remote_file);
2757 if (is_main_executable)
2758 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002759 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002760 if (launch_info)
2761 launch_info->SetExecutableFile(remote_file, false);
2762 }
2763 }
2764 else
2765 break;
2766 }
2767 }
2768 }
2769 }
2770 }
2771 }
2772 }
2773 return error;
2774}
Greg Clayton7b242382011-07-08 00:48:09 +00002775
Greg Claytond5944cd2013-12-06 01:12:00 +00002776bool
2777Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2778{
2779 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2780}
2781
2782bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002783Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2784{
2785 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2786}
2787
2788bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002789Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2790{
2791 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2792 if (old_section_load_addr != new_section_load_addr)
2793 {
2794 uint32_t stop_id = 0;
2795 ProcessSP process_sp(GetProcessSP());
2796 if (process_sp)
2797 stop_id = process_sp->GetStopID();
2798 else
2799 stop_id = m_section_load_history.GetLastStopID();
2800 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2801 return true; // Return true if the section load address was changed...
2802 }
2803 return false; // Return false to indicate nothing changed
2804
2805}
2806
Greg Clayton8012cad2014-11-17 19:39:20 +00002807size_t
2808Target::UnloadModuleSections (const ModuleList &module_list)
2809{
2810 size_t section_unload_count = 0;
2811 size_t num_modules = module_list.GetSize();
2812 for (size_t i=0; i<num_modules; ++i)
2813 {
2814 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2815 }
2816 return section_unload_count;
2817}
2818
2819size_t
2820Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2821{
2822 uint32_t stop_id = 0;
2823 ProcessSP process_sp(GetProcessSP());
2824 if (process_sp)
2825 stop_id = process_sp->GetStopID();
2826 else
2827 stop_id = m_section_load_history.GetLastStopID();
2828 SectionList *sections = module_sp->GetSectionList();
2829 size_t section_unload_count = 0;
2830 if (sections)
2831 {
2832 const uint32_t num_sections = sections->GetNumSections(0);
2833 for (uint32_t i = 0; i < num_sections; ++i)
2834 {
2835 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2836 }
2837 }
2838 return section_unload_count;
2839}
2840
Greg Claytond5944cd2013-12-06 01:12:00 +00002841bool
2842Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2843{
2844 uint32_t stop_id = 0;
2845 ProcessSP process_sp(GetProcessSP());
2846 if (process_sp)
2847 stop_id = process_sp->GetStopID();
2848 else
2849 stop_id = m_section_load_history.GetLastStopID();
2850 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2851}
2852
2853bool
2854Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2855{
2856 uint32_t stop_id = 0;
2857 ProcessSP process_sp(GetProcessSP());
2858 if (process_sp)
2859 stop_id = process_sp->GetStopID();
2860 else
2861 stop_id = m_section_load_history.GetLastStopID();
2862 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2863}
2864
2865void
2866Target::ClearAllLoadedSections ()
2867{
2868 m_section_load_history.Clear();
2869}
2870
Greg Claytonb09c5382013-12-13 17:20:18 +00002871
2872Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002873Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002874{
2875 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002876 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2877
2878 if (log)
2879 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2880
Greg Claytonb09c5382013-12-13 17:20:18 +00002881 StateType state = eStateInvalid;
2882
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002883 // Scope to temporarily get the process state in case someone has manually
2884 // remotely connected already to a process and we can skip the platform
2885 // launching.
2886 {
2887 ProcessSP process_sp (GetProcessSP());
2888
2889 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002890 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002891 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002892 if (log)
2893 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2894 }
2895 else
2896 {
2897 if (log)
2898 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2899 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002900 }
2901
Greg Claytonb09c5382013-12-13 17:20:18 +00002902 launch_info.GetFlags().Set (eLaunchFlagDebug);
2903
2904 // Get the value of synchronous execution here. If you wait till after you have started to
2905 // run, then you could have hit a breakpoint, whose command might switch the value, and
2906 // then you'll pick up that incorrect value.
2907 Debugger &debugger = GetDebugger();
2908 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2909
2910 PlatformSP platform_sp (GetPlatform());
2911
2912 // Finalize the file actions, and if none were given, default to opening
2913 // up a pseudo terminal
2914 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002915 if (log)
2916 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2917 __FUNCTION__,
2918 platform_sp ? "true" : "false",
2919 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2920 default_to_use_pty ? "true" : "false");
2921
Greg Claytonb09c5382013-12-13 17:20:18 +00002922 launch_info.FinalizeFileActions (this, default_to_use_pty);
2923
2924 if (state == eStateConnected)
2925 {
2926 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2927 {
2928 error.SetErrorString("can't launch in tty when launching through a remote connection");
2929 return error;
2930 }
2931 }
2932
2933 if (!launch_info.GetArchitecture().IsValid())
2934 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002935
2936 // 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 +00002937 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2938 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002939 if (log)
2940 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2941
Greg Clayton5df78fa2015-05-23 03:54:53 +00002942 // Get a weak pointer to the previous process if we have one
2943 ProcessWP process_wp;
2944 if (m_process_sp)
2945 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00002946 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2947 debugger,
2948 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002949 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00002950
2951 // Cleanup the old process since someone might still have a strong
2952 // reference to this process and we would like to allow it to cleanup
2953 // as much as it can without the object being destroyed. We try to
2954 // lock the shared pointer and if that works, then someone else still
2955 // has a strong reference to the process.
2956
2957 ProcessSP old_process_sp(process_wp.lock());
2958 if (old_process_sp)
2959 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00002960 }
2961 else
2962 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002963 if (log)
2964 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2965
Greg Claytonb09c5382013-12-13 17:20:18 +00002966 if (state == eStateConnected)
2967 {
2968 assert(m_process_sp);
2969 }
2970 else
2971 {
Todd Fiala015d8182014-07-22 23:41:36 +00002972 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002973 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002974 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002975 }
Todd Fiala015d8182014-07-22 23:41:36 +00002976
2977 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002978 if (m_process_sp)
2979 error = m_process_sp->Launch (launch_info);
2980 }
2981
2982 if (!m_process_sp)
2983 {
2984 if (error.Success())
2985 error.SetErrorString("failed to launch or debug process");
2986 return error;
2987 }
2988
2989 if (error.Success())
2990 {
Ilia K064e69f2015-03-23 21:16:25 +00002991 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00002992 {
Greg Clayton44d93782014-01-27 23:43:24 +00002993 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00002994 if (!hijack_listener_sp)
2995 {
2996 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
2997 launch_info.SetHijackListener(hijack_listener_sp);
2998 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
2999 }
Todd Fialaac33cc92014-10-09 01:02:08 +00003000
Ilia K38810f42015-05-20 10:15:47 +00003001 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00003002
3003 if (state == eStateStopped)
3004 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003005 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003006 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003007 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00003008 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003009 error = m_process_sp->PrivateResume();
3010 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00003011 {
Ilia K064e69f2015-03-23 21:16:25 +00003012 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
3013 const bool must_be_alive = false; // eStateExited is ok, so this must be false
3014 if (!StateIsStoppedState(state, must_be_alive))
3015 {
3016 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
3017 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003018 }
3019 }
Ilia K064e69f2015-03-23 21:16:25 +00003020 else
3021 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003022 m_process_sp->RestoreProcessEvents();
3023 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00003024 }
3025 if (!error.Success())
3026 {
Ilia K064e69f2015-03-23 21:16:25 +00003027 Error error2;
3028 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
3029 error = error2;
3030 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003031 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003032 }
Greg Clayton40286e02014-04-30 20:29:09 +00003033 else if (state == eStateExited)
3034 {
Zachary Turner10687b02014-10-20 17:46:43 +00003035 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00003036 const int exit_status = m_process_sp->GetExitStatus();
3037 const char *exit_desc = m_process_sp->GetExitDescription();
3038#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'."
3039 if (exit_desc && exit_desc[0])
3040 {
3041 if (with_shell)
3042 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
3043 else
3044 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
3045 }
3046 else
3047 {
3048 if (with_shell)
3049 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
3050 else
3051 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
3052 }
3053 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003054 else
3055 {
3056 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
3057 }
3058 }
Greg Clayton44d93782014-01-27 23:43:24 +00003059 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00003060 }
3061 else
3062 {
Greg Clayton44d93782014-01-27 23:43:24 +00003063 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003064 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003065 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003066 }
3067 return error;
3068}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003069
3070Error
3071Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3072{
3073 auto state = eStateInvalid;
3074 auto process_sp = GetProcessSP ();
3075 if (process_sp)
3076 {
3077 state = process_sp->GetState ();
3078 if (process_sp->IsAlive () && state != eStateConnected)
3079 {
3080 if (state == eStateAttaching)
3081 return Error ("process attach is in progress");
3082 return Error ("a process is already being debugged");
3083 }
3084 }
3085
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003086 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3087
3088 // If no process info was specified, then use the target executable
3089 // name as the process to attach to by default
3090 if (!attach_info.ProcessInfoSpecified ())
3091 {
3092 if (old_exec_module_sp)
3093 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3094
3095 if (!attach_info.ProcessInfoSpecified ())
3096 {
3097 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3098 }
3099 }
3100
3101 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
Greg Claytonb3788ea2015-10-05 22:58:37 +00003102 ListenerSP hijack_listener_sp;
3103 const bool async = attach_info.GetAsync();
3104 if (async == false)
3105 {
3106 hijack_listener_sp.reset (new Listener ("lldb.Target.Attach.attach.hijack"));
3107 attach_info.SetHijackListener (hijack_listener_sp);
3108 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003109
3110 Error error;
3111 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3112 {
3113 SetPlatform (platform_sp);
3114 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3115 }
3116 else
3117 {
3118 if (state != eStateConnected)
3119 {
3120 const char *plugin_name = attach_info.GetProcessPluginName ();
3121 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3122 if (process_sp == nullptr)
3123 {
3124 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3125 return error;
3126 }
3127 }
Greg Claytonb3788ea2015-10-05 22:58:37 +00003128 if (hijack_listener_sp)
3129 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003130 error = process_sp->Attach (attach_info);
3131 }
3132
Greg Claytonb3788ea2015-10-05 22:58:37 +00003133 if (error.Success () && process_sp && async == false)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003134 {
3135 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
3136 process_sp->RestoreProcessEvents ();
3137
3138 if (state != eStateStopped)
3139 {
3140 const char *exit_desc = process_sp->GetExitDescription ();
3141 if (exit_desc)
Greg Claytonb3788ea2015-10-05 22:58:37 +00003142 error.SetErrorStringWithFormat ("%s", exit_desc);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003143 else
Greg Claytonb3788ea2015-10-05 22:58:37 +00003144 error.SetErrorString ("process did not stop (no such process or permission problem?)");
Jason Molendaede31932015-04-17 05:01:58 +00003145 process_sp->Destroy (false);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003146 }
3147 }
3148 return error;
3149}
3150
Jim Ingham9575d842011-03-11 03:53:59 +00003151//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003152// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003153//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003154Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3155 UserID (uid),
3156 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003157 m_commands (),
3158 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003159 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003160 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003161{
3162}
3163
3164Target::StopHook::StopHook (const StopHook &rhs) :
3165 UserID (rhs.GetID()),
3166 m_target_sp (rhs.m_target_sp),
3167 m_commands (rhs.m_commands),
3168 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003169 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003170 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003171{
3172 if (rhs.m_thread_spec_ap.get() != NULL)
3173 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3174}
3175
3176
3177Target::StopHook::~StopHook ()
3178{
3179}
3180
3181void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003182Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3183{
3184 m_specifier_sp.reset(specifier);
3185}
3186
3187void
Jim Ingham9575d842011-03-11 03:53:59 +00003188Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3189{
3190 m_thread_spec_ap.reset (specifier);
3191}
3192
3193
3194void
3195Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3196{
3197 int indent_level = s->GetIndentLevel();
3198
3199 s->SetIndentLevel(indent_level + 2);
3200
Daniel Malead01b2952012-11-29 21:49:15 +00003201 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003202 if (m_active)
3203 s->Indent ("State: enabled\n");
3204 else
3205 s->Indent ("State: disabled\n");
3206
3207 if (m_specifier_sp)
3208 {
3209 s->Indent();
3210 s->PutCString ("Specifier:\n");
3211 s->SetIndentLevel (indent_level + 4);
3212 m_specifier_sp->GetDescription (s, level);
3213 s->SetIndentLevel (indent_level + 2);
3214 }
3215
3216 if (m_thread_spec_ap.get() != NULL)
3217 {
3218 StreamString tmp;
3219 s->Indent("Thread:\n");
3220 m_thread_spec_ap->GetDescription (&tmp, level);
3221 s->SetIndentLevel (indent_level + 4);
3222 s->Indent (tmp.GetData());
3223 s->PutCString ("\n");
3224 s->SetIndentLevel (indent_level + 2);
3225 }
3226
3227 s->Indent ("Commands: \n");
3228 s->SetIndentLevel (indent_level + 4);
3229 uint32_t num_commands = m_commands.GetSize();
3230 for (uint32_t i = 0; i < num_commands; i++)
3231 {
3232 s->Indent(m_commands.GetStringAtIndex(i));
3233 s->PutCString ("\n");
3234 }
3235 s->SetIndentLevel (indent_level);
3236}
3237
Greg Clayton67cc0632012-08-22 17:17:09 +00003238//--------------------------------------------------------------
3239// class TargetProperties
3240//--------------------------------------------------------------
3241
3242OptionEnumValueElement
3243lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003244{
Greg Clayton67cc0632012-08-22 17:17:09 +00003245 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3246 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3247 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
3248 { 0, NULL, NULL }
3249};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003250
Greg Clayton1f746072012-08-29 21:13:06 +00003251static OptionEnumValueElement
3252g_inline_breakpoint_enums[] =
3253{
3254 { 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."},
3255 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3256 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
3257 { 0, NULL, NULL }
3258};
3259
Jim Ingham0f063ba2013-03-02 00:26:47 +00003260typedef enum x86DisassemblyFlavor
3261{
3262 eX86DisFlavorDefault,
3263 eX86DisFlavorIntel,
3264 eX86DisFlavorATT
3265} x86DisassemblyFlavor;
3266
3267static OptionEnumValueElement
3268g_x86_dis_flavor_value_types[] =
3269{
3270 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3271 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3272 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
3273 { 0, NULL, NULL }
3274};
3275
Enrico Granata397ddd52013-05-21 20:13:34 +00003276static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003277g_hex_immediate_style_values[] =
3278{
3279 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3280 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
3281 { 0, NULL, NULL }
3282};
3283
3284static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003285g_load_script_from_sym_file_values[] =
3286{
3287 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3288 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3289 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
3290 { 0, NULL, NULL }
3291};
3292
Greg Claytonfd814c52013-08-13 01:42:25 +00003293
3294static OptionEnumValueElement
3295g_memory_module_load_level_values[] =
3296{
Greg Clayton86eac942013-08-13 21:32:34 +00003297 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003298 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3299 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
3300 { 0, NULL, NULL }
3301};
3302
Greg Clayton67cc0632012-08-22 17:17:09 +00003303static PropertyDefinition
3304g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003305{
Greg Clayton67cc0632012-08-22 17:17:09 +00003306 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
Ilia K055ad9b2015-05-18 13:41:01 +00003307 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Move breakpoints to nearest code." },
Dawn Perchike0c96ac2015-07-30 22:10:08 +00003308 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , NULL, NULL, "The language to use when interpreting expressions entered in commands." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003309 { "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 +00003310 { "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 +00003311 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
3312 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
Jim Ingham0ca778e2015-09-15 18:03:00 +00003313 { "source-map" , OptionValue::eTypePathMap , false, 0 , NULL, NULL, "Source path remappings are used to track the change of location between a source file when built, and "
Greg Clayton67cc0632012-08-22 17:17:09 +00003314 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3315 "some part (starting at the root) of the path to the file when it was built, "
3316 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3317 "Each element of the array is checked in order and the first one that results in a match wins." },
3318 { "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 +00003319 { "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 +00003320 { "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 +00003321 { "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 +00003322 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
3323 { "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 +00003324 { "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 +00003325 { "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 +00003326 { "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." },
3327 { "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 +00003328 { "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." },
3329 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
3330 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
3331 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
3332 { "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 +00003333 { "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 +00003334 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
3335 { "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 +00003336 { "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 +00003337 "Breakpoint locations can end up being inlined by the compiler, so that a compile unit 'a.c' might contain an inlined function from another source file. "
Jim Ingham63733852015-06-16 21:39:56 +00003338 "Usually this is limited to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
Greg Clayton1f746072012-08-29 21:13:06 +00003339 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003340 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3341 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3342 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003343 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003344 // 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.
3345 { "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 +00003346 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
3347 { "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 +00003348 { "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 +00003349 { "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 +00003350 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
3351 "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. "
3352 "This setting helps users control how much information gets loaded when loading modules from memory."
3353 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3354 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3355 "'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 +00003356 { "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 +00003357 { "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 +00003358 { "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 +00003359 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, NULL, NULL, "Disable lock-step debugging, instead control threads independently." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003360 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
3361};
Enrico Granata560558e2015-02-11 02:35:39 +00003362
Greg Clayton67cc0632012-08-22 17:17:09 +00003363enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003364{
Greg Clayton67cc0632012-08-22 17:17:09 +00003365 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003366 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003367 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003368 ePropertyExprPrefix,
3369 ePropertyPreferDynamic,
3370 ePropertyEnableSynthetic,
3371 ePropertySkipPrologue,
3372 ePropertySourceMap,
3373 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003374 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003375 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003376 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003377 ePropertyMaxChildrenCount,
3378 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003379 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003380 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003381 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003382 ePropertyRunArgs,
3383 ePropertyEnvVars,
3384 ePropertyInheritEnv,
3385 ePropertyInputPath,
3386 ePropertyOutputPath,
3387 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003388 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003389 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003390 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003391 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003392 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003393 ePropertyUseHexImmediates,
3394 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003395 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003396 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003397 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003398 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003399 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003400 ePropertyDisplayRuntimeSupportValues,
3401 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003402};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003403
Caroline Ticedaccaa92010-09-20 20:44:43 +00003404
Greg Clayton67cc0632012-08-22 17:17:09 +00003405class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003406{
Greg Clayton67cc0632012-08-22 17:17:09 +00003407public:
3408 TargetOptionValueProperties (const ConstString &name) :
3409 OptionValueProperties (name),
3410 m_target (NULL),
3411 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003412 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003413 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003414
Greg Clayton67cc0632012-08-22 17:17:09 +00003415 // This constructor is used when creating TargetOptionValueProperties when it
3416 // is part of a new lldb_private::Target instance. It will copy all current
3417 // global property values as needed
3418 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3419 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3420 m_target (target),
3421 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003422 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003423 }
3424
3425 virtual const Property *
3426 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3427 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003428 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003429 // try and grab the setting from the current target if there is one. Else we just
3430 // use the one from this instance.
3431 if (idx == ePropertyEnvVars)
3432 GetHostEnvironmentIfNeeded ();
3433
3434 if (exe_ctx)
3435 {
3436 Target *target = exe_ctx->GetTargetPtr();
3437 if (target)
3438 {
3439 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3440 if (this != target_properties)
3441 return target_properties->ProtectedGetPropertyAtIndex (idx);
3442 }
3443 }
3444 return ProtectedGetPropertyAtIndex (idx);
3445 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003446
3447 lldb::TargetSP
3448 GetTargetSP ()
3449 {
3450 return m_target->shared_from_this();
3451 }
3452
Greg Clayton67cc0632012-08-22 17:17:09 +00003453protected:
3454
3455 void
3456 GetHostEnvironmentIfNeeded () const
3457 {
3458 if (!m_got_host_env)
3459 {
3460 if (m_target)
3461 {
3462 m_got_host_env = true;
3463 const uint32_t idx = ePropertyInheritEnv;
3464 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3465 {
3466 PlatformSP platform_sp (m_target->GetPlatform());
3467 if (platform_sp)
3468 {
3469 StringList env;
3470 if (platform_sp->GetEnvironment(env))
3471 {
3472 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3473 if (env_dict)
3474 {
3475 const bool can_replace = false;
3476 const size_t envc = env.GetSize();
3477 for (size_t idx=0; idx<envc; idx++)
3478 {
3479 const char *env_entry = env.GetStringAtIndex (idx);
3480 if (env_entry)
3481 {
3482 const char *equal_pos = ::strchr(env_entry, '=');
3483 ConstString key;
3484 // It is ok to have environment variables with no values
3485 const char *value = NULL;
3486 if (equal_pos)
3487 {
3488 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3489 if (equal_pos[1])
3490 value = equal_pos + 1;
3491 }
3492 else
3493 {
3494 key.SetCString(env_entry);
3495 }
3496 // Don't allow existing keys to be replaced with ones we get from the platform environment
3497 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3498 }
3499 }
3500 }
3501 }
3502 }
3503 }
3504 }
3505 }
3506 }
3507 Target *m_target;
3508 mutable bool m_got_host_env;
3509};
3510
Greg Claytonfbb76342013-11-20 21:07:01 +00003511//----------------------------------------------------------------------
3512// TargetProperties
3513//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003514TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003515 Properties (),
3516 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003517{
3518 if (target)
3519 {
3520 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003521
3522 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3523 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3524 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3525 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3526 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3527 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3528 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3529 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3530 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3531 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3532
3533 // Update m_launch_info once it was created
3534 Arg0ValueChangedCallback(this, NULL);
3535 RunArgsValueChangedCallback(this, NULL);
3536 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3537 InputPathValueChangedCallback(this, NULL);
3538 OutputPathValueChangedCallback(this, NULL);
3539 ErrorPathValueChangedCallback(this, NULL);
3540 DetachOnErrorValueChangedCallback(this, NULL);
3541 DisableASLRValueChangedCallback(this, NULL);
3542 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003543 }
3544 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003545 {
3546 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3547 m_collection_sp->Initialize(g_properties);
3548 m_collection_sp->AppendProperty(ConstString("process"),
3549 ConstString("Settings specify to processes."),
3550 true,
3551 Process::GetGlobalProperties()->GetValueProperties());
3552 }
Ilia K8f37ca52015-02-13 14:31:06 +00003553
Caroline Ticedaccaa92010-09-20 20:44:43 +00003554}
3555
Greg Clayton67cc0632012-08-22 17:17:09 +00003556TargetProperties::~TargetProperties ()
3557{
3558}
3559ArchSpec
3560TargetProperties::GetDefaultArchitecture () const
3561{
3562 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3563 if (value)
3564 return value->GetCurrentValue();
3565 return ArchSpec();
3566}
3567
3568void
3569TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3570{
3571 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3572 if (value)
3573 return value->SetCurrentValue(arch, true);
3574}
3575
Ilia K055ad9b2015-05-18 13:41:01 +00003576bool
3577TargetProperties::GetMoveToNearestCode() const
3578{
3579 const uint32_t idx = ePropertyMoveToNearestCode;
3580 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3581}
3582
Greg Clayton67cc0632012-08-22 17:17:09 +00003583lldb::DynamicValueType
3584TargetProperties::GetPreferDynamicValue() const
3585{
3586 const uint32_t idx = ePropertyPreferDynamic;
3587 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3588}
3589
3590bool
Greg Clayton15484402015-05-15 18:40:24 +00003591TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3592{
3593 const uint32_t idx = ePropertyPreferDynamic;
3594 return m_collection_sp->SetPropertyAtIndexAsEnumeration(NULL, idx, d);
3595}
3596
3597
3598bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003599TargetProperties::GetDisableASLR () const
3600{
3601 const uint32_t idx = ePropertyDisableASLR;
3602 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3603}
3604
3605void
3606TargetProperties::SetDisableASLR (bool b)
3607{
3608 const uint32_t idx = ePropertyDisableASLR;
3609 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3610}
3611
3612bool
Jim Ingham106d0282014-06-25 02:32:56 +00003613TargetProperties::GetDetachOnError () const
3614{
3615 const uint32_t idx = ePropertyDetachOnError;
3616 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3617}
3618
3619void
3620TargetProperties::SetDetachOnError (bool b)
3621{
3622 const uint32_t idx = ePropertyDetachOnError;
3623 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3624}
3625
3626bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003627TargetProperties::GetDisableSTDIO () const
3628{
3629 const uint32_t idx = ePropertyDisableSTDIO;
3630 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3631}
3632
3633void
3634TargetProperties::SetDisableSTDIO (bool b)
3635{
3636 const uint32_t idx = ePropertyDisableSTDIO;
3637 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3638}
3639
Jim Ingham0f063ba2013-03-02 00:26:47 +00003640const char *
3641TargetProperties::GetDisassemblyFlavor () const
3642{
3643 const uint32_t idx = ePropertyDisassemblyFlavor;
3644 const char *return_value;
3645
3646 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3647 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3648 return return_value;
3649}
3650
Greg Clayton1f746072012-08-29 21:13:06 +00003651InlineStrategy
3652TargetProperties::GetInlineStrategy () const
3653{
3654 const uint32_t idx = ePropertyInlineStrategy;
3655 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3656}
3657
Greg Clayton45392552012-10-17 22:57:12 +00003658const char *
3659TargetProperties::GetArg0 () const
3660{
3661 const uint32_t idx = ePropertyArg0;
3662 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3663}
3664
3665void
3666TargetProperties::SetArg0 (const char *arg)
3667{
3668 const uint32_t idx = ePropertyArg0;
3669 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003670 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003671}
3672
Greg Clayton67cc0632012-08-22 17:17:09 +00003673bool
3674TargetProperties::GetRunArguments (Args &args) const
3675{
3676 const uint32_t idx = ePropertyRunArgs;
3677 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3678}
3679
3680void
3681TargetProperties::SetRunArguments (const Args &args)
3682{
3683 const uint32_t idx = ePropertyRunArgs;
3684 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003685 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003686}
3687
3688size_t
3689TargetProperties::GetEnvironmentAsArgs (Args &env) const
3690{
3691 const uint32_t idx = ePropertyEnvVars;
3692 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3693}
3694
Ilia K8f37ca52015-02-13 14:31:06 +00003695void
3696TargetProperties::SetEnvironmentFromArgs (const Args &env)
3697{
3698 const uint32_t idx = ePropertyEnvVars;
3699 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003700 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003701}
3702
Greg Clayton67cc0632012-08-22 17:17:09 +00003703bool
3704TargetProperties::GetSkipPrologue() const
3705{
3706 const uint32_t idx = ePropertySkipPrologue;
3707 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3708}
3709
3710PathMappingList &
3711TargetProperties::GetSourcePathMap () const
3712{
3713 const uint32_t idx = ePropertySourceMap;
3714 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3715 assert(option_value);
3716 return option_value->GetCurrentValue();
3717}
3718
3719FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003720TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003721{
3722 const uint32_t idx = ePropertyExecutableSearchPaths;
3723 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3724 assert(option_value);
3725 return option_value->GetCurrentValue();
3726}
3727
Michael Sartaina7499c92013-07-01 19:45:50 +00003728FileSpecList &
3729TargetProperties::GetDebugFileSearchPaths ()
3730{
3731 const uint32_t idx = ePropertyDebugFileSearchPaths;
3732 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3733 assert(option_value);
3734 return option_value->GetCurrentValue();
3735}
3736
Sean Callanan85054342015-04-03 15:39:47 +00003737FileSpecList &
3738TargetProperties::GetClangModuleSearchPaths ()
3739{
3740 const uint32_t idx = ePropertyClangModuleSearchPaths;
3741 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3742 assert(option_value);
3743 return option_value->GetCurrentValue();
3744}
3745
Greg Clayton67cc0632012-08-22 17:17:09 +00003746bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003747TargetProperties::GetEnableAutoImportClangModules() const
3748{
3749 const uint32_t idx = ePropertyAutoImportClangModules;
3750 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3751}
3752
3753bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003754TargetProperties::GetEnableSyntheticValue () const
3755{
3756 const uint32_t idx = ePropertyEnableSynthetic;
3757 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3758}
3759
3760uint32_t
3761TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3762{
3763 const uint32_t idx = ePropertyMaxChildrenCount;
3764 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3765}
3766
3767uint32_t
3768TargetProperties::GetMaximumSizeOfStringSummary() const
3769{
3770 const uint32_t idx = ePropertyMaxSummaryLength;
3771 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3772}
3773
Enrico Granatad325bf92013-06-04 22:54:16 +00003774uint32_t
3775TargetProperties::GetMaximumMemReadSize () const
3776{
3777 const uint32_t idx = ePropertyMaxMemReadSize;
3778 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3779}
3780
Greg Clayton67cc0632012-08-22 17:17:09 +00003781FileSpec
3782TargetProperties::GetStandardInputPath () const
3783{
3784 const uint32_t idx = ePropertyInputPath;
3785 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3786}
3787
3788void
3789TargetProperties::SetStandardInputPath (const char *p)
3790{
3791 const uint32_t idx = ePropertyInputPath;
3792 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3793}
3794
3795FileSpec
3796TargetProperties::GetStandardOutputPath () const
3797{
3798 const uint32_t idx = ePropertyOutputPath;
3799 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3800}
3801
3802void
3803TargetProperties::SetStandardOutputPath (const char *p)
3804{
3805 const uint32_t idx = ePropertyOutputPath;
3806 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3807}
3808
3809FileSpec
3810TargetProperties::GetStandardErrorPath () const
3811{
3812 const uint32_t idx = ePropertyErrorPath;
3813 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3814}
3815
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003816LanguageType
3817TargetProperties::GetLanguage () const
3818{
3819 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage (NULL, ePropertyLanguage);
3820 if (value)
3821 return value->GetCurrentValue();
3822 return LanguageType();
3823}
3824
Greg Clayton6920b522012-08-22 18:39:03 +00003825const char *
3826TargetProperties::GetExpressionPrefixContentsAsCString ()
3827{
3828 const uint32_t idx = ePropertyExprPrefix;
3829 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3830 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003831 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003832 const bool null_terminate = true;
3833 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003834 if (data_sp)
3835 return (const char *) data_sp->GetBytes();
3836 }
Greg Clayton6920b522012-08-22 18:39:03 +00003837 return NULL;
3838}
3839
Greg Clayton67cc0632012-08-22 17:17:09 +00003840void
3841TargetProperties::SetStandardErrorPath (const char *p)
3842{
3843 const uint32_t idx = ePropertyErrorPath;
3844 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3845}
3846
3847bool
3848TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3849{
3850 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3851 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3852}
3853
Jim Ingham17d023f2013-03-13 17:58:04 +00003854bool
Daniel Malead79ae052013-08-07 21:54:09 +00003855TargetProperties::GetUseHexImmediates () const
3856{
3857 const uint32_t idx = ePropertyUseHexImmediates;
3858 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3859}
3860
3861bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003862TargetProperties::GetUseFastStepping () const
3863{
3864 const uint32_t idx = ePropertyUseFastStepping;
3865 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3866}
3867
Greg Claytonfb6621e2013-12-06 21:59:52 +00003868bool
3869TargetProperties::GetDisplayExpressionsInCrashlogs () const
3870{
3871 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3872 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3873}
3874
Enrico Granata397ddd52013-05-21 20:13:34 +00003875LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003876TargetProperties::GetLoadScriptFromSymbolFile () const
3877{
3878 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003879 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003880}
3881
Daniel Malead79ae052013-08-07 21:54:09 +00003882Disassembler::HexImmediateStyle
3883TargetProperties::GetHexImmediateStyle () const
3884{
3885 const uint32_t idx = ePropertyHexImmediateStyle;
3886 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3887}
3888
Greg Claytonfd814c52013-08-13 01:42:25 +00003889MemoryModuleLoadLevel
3890TargetProperties::GetMemoryModuleLoadLevel() const
3891{
3892 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3893 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3894}
3895
Jason Molendaa4bea722014-02-14 05:06:49 +00003896bool
3897TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3898{
3899 const uint32_t idx = ePropertyTrapHandlerNames;
3900 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3901}
Greg Claytonfd814c52013-08-13 01:42:25 +00003902
Jason Molendaa4bea722014-02-14 05:06:49 +00003903void
3904TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3905{
3906 const uint32_t idx = ePropertyTrapHandlerNames;
3907 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3908}
Greg Clayton67cc0632012-08-22 17:17:09 +00003909
Enrico Granata560558e2015-02-11 02:35:39 +00003910bool
3911TargetProperties::GetDisplayRuntimeSupportValues () const
3912{
3913 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3914 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3915}
3916
3917void
3918TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3919{
3920 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3921 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3922}
3923
Ewan Crawford78baa192015-05-13 09:18:18 +00003924bool
3925TargetProperties::GetNonStopModeEnabled () const
3926{
3927 const uint32_t idx = ePropertyNonStopModeEnabled;
3928 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3929}
3930
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00003931void
3932TargetProperties::SetNonStopModeEnabled (bool b)
3933{
3934 const uint32_t idx = ePropertyNonStopModeEnabled;
3935 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3936}
3937
Ilia K8f37ca52015-02-13 14:31:06 +00003938const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003939TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003940{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003941 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003942 return m_launch_info;
3943}
3944
3945void
3946TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3947{
3948 m_launch_info = launch_info;
3949 SetArg0(launch_info.GetArg0());
3950 SetRunArguments(launch_info.GetArguments());
3951 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3952 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3953 if (input_file_action)
3954 {
3955 const char *input_path = input_file_action->GetPath();
3956 if (input_path)
3957 SetStandardInputPath(input_path);
3958 }
3959 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3960 if (output_file_action)
3961 {
3962 const char *output_path = output_file_action->GetPath();
3963 if (output_path)
3964 SetStandardOutputPath(output_path);
3965 }
3966 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3967 if (error_file_action)
3968 {
3969 const char *error_path = error_file_action->GetPath();
3970 if (error_path)
3971 SetStandardErrorPath(error_path);
3972 }
3973 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3974 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3975 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3976}
3977
3978void
3979TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3980{
3981 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3982 this_->m_launch_info.SetArg0(this_->GetArg0());
3983}
3984
3985void
3986TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3987{
3988 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3989 Args args;
3990 if (this_->GetRunArguments(args))
3991 this_->m_launch_info.GetArguments() = args;
3992}
3993
3994void
3995TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3996{
3997 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3998 Args args;
3999 if (this_->GetEnvironmentAsArgs(args))
4000 this_->m_launch_info.GetEnvironmentEntries() = args;
4001}
4002
4003void
4004TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4005{
4006 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004007 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004008}
4009
4010void
4011TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4012{
4013 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004014 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004015}
4016
4017void
4018TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4019{
4020 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004021 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004022}
4023
4024void
4025TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
4026{
4027 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4028 if (this_->GetDetachOnError())
4029 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4030 else
4031 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
4032}
4033
4034void
4035TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
4036{
4037 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4038 if (this_->GetDisableASLR())
4039 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4040 else
4041 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
4042}
4043
4044void
4045TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
4046{
4047 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4048 if (this_->GetDisableSTDIO())
4049 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4050 else
4051 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4052}
Ilia Keb2c19a2015-03-10 21:59:55 +00004053
4054//----------------------------------------------------------------------
4055// Target::TargetEventData
4056//----------------------------------------------------------------------
4057
4058Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
4059 EventData (),
4060 m_target_sp (target_sp),
4061 m_module_list ()
4062{
4063}
4064
4065Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
4066 EventData (),
4067 m_target_sp (target_sp),
4068 m_module_list (module_list)
4069{
4070}
4071
4072Target::TargetEventData::~TargetEventData()
4073{
4074}
4075
4076const ConstString &
4077Target::TargetEventData::GetFlavorString ()
4078{
4079 static ConstString g_flavor ("Target::TargetEventData");
4080 return g_flavor;
4081}
4082
4083void
4084Target::TargetEventData::Dump (Stream *s) const
4085{
4086}
4087
4088const Target::TargetEventData *
4089Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4090{
4091 if (event_ptr)
4092 {
4093 const EventData *event_data = event_ptr->GetData();
4094 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4095 return static_cast <const TargetEventData *> (event_ptr->GetData());
4096 }
4097 return NULL;
4098}
4099
4100TargetSP
4101Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4102{
4103 TargetSP target_sp;
4104 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4105 if (event_data)
4106 target_sp = event_data->m_target_sp;
4107 return target_sp;
4108}
4109
4110ModuleList
4111Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4112{
4113 ModuleList module_list;
4114 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4115 if (event_data)
4116 module_list = event_data->m_module_list;
4117 return module_list;
4118}