blob: 993706fa889b1e7d6cad39ea6ea7cb4efcc2ae76 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000028#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000029#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000030#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000034#include "lldb/Expression/ClangASTSource.h"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000035#include "lldb/Expression/ClangPersistentVariables.h"
Jim Ingham151c0322015-09-15 21:13:50 +000036#include "lldb/Expression/UserExpression.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000037#include "lldb/Expression/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000038#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000040#include "lldb/Interpreter/CommandInterpreter.h"
41#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000042#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000043#include "lldb/Interpreter/OptionValues.h"
44#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000045#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ObjectFile.h"
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000047#include "lldb/Symbol/Function.h"
48#include "lldb/Symbol/Symbol.h"
Jim Ingham151c0322015-09-15 21:13:50 +000049#include "lldb/Target/Language.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000050#include "lldb/Target/LanguageRuntime.h"
51#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000053#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000054#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000055#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000056#include "lldb/Target/Thread.h"
57#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
59using namespace lldb;
60using namespace lldb_private;
61
Jim Ingham4bddaeb2012-02-16 06:50:00 +000062ConstString &
63Target::GetStaticBroadcasterClass ()
64{
65 static ConstString class_name ("lldb.target");
66 return class_name;
67}
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069//----------------------------------------------------------------------
70// Target constructor
71//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000072Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000073 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000074 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000075 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000076 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000077 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000078 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000079 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000080 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000081 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 m_breakpoint_list (false),
83 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000084 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000085 m_process_sp (),
86 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000087 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000088 m_scratch_ast_context_ap (),
89 m_scratch_ast_source_ap (),
90 m_ast_importer_ap (),
Zachary Turner32abc6e2015-03-03 19:23:09 +000091 m_persistent_variables (new ClangPersistentVariables),
Greg Clayton9585fbf2013-03-19 00:20:55 +000092 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000093 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000094 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000095 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000096 m_suppress_stop_hooks (false),
97 m_is_dummy_target(is_dummy_target)
98
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099{
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000100 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
101 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
102 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +0000103 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +0000104 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000105
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000106 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000107
Greg Clayton5160ce52013-03-27 23:08:40 +0000108 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000110 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000111 if (m_arch.IsValid())
112 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000113 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 +0000114 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115}
116
Jim Ingham893c9322014-11-22 01:42:44 +0000117void
118Target::PrimeFromDummyTarget(Target *target)
119{
120 if (!target)
121 return;
122
123 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000124
125 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
126 {
127 if (breakpoint_sp->IsInternal())
128 continue;
129
130 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
131 AddBreakpoint (new_bp, false);
132 }
Jim Ingham893c9322014-11-22 01:42:44 +0000133}
134
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135//----------------------------------------------------------------------
136// Destructor
137//----------------------------------------------------------------------
138Target::~Target()
139{
Greg Clayton5160ce52013-03-27 23:08:40 +0000140 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000142 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143 DeleteCurrentProcess ();
144}
145
146void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000147Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148{
Greg Clayton89411422010-10-08 00:21:05 +0000149// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000150 if (description_level != lldb::eDescriptionLevelBrief)
151 {
152 s->Indent();
153 s->PutCString("Target\n");
154 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000155 m_images.Dump(s);
156 m_breakpoint_list.Dump(s);
157 m_internal_breakpoint_list.Dump(s);
158 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000159 }
160 else
161 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000162 Module *exe_module = GetExecutableModulePointer();
163 if (exe_module)
164 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000165 else
166 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000167 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168}
169
170void
Greg Clayton90ba8112012-12-05 00:16:59 +0000171Target::CleanupProcess ()
172{
173 // Do any cleanup of the target we need to do between process instances.
174 // NB It is better to do this before destroying the process in case the
175 // clean up needs some help from the process.
176 m_breakpoint_list.ClearAllBreakpointSites();
177 m_internal_breakpoint_list.ClearAllBreakpointSites();
178 // Disable watchpoints just on the debugger side.
179 Mutex::Locker locker;
180 this->GetWatchpointList().GetListMutex(locker);
181 DisableAllWatchpoints(false);
182 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000183 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000184}
185
186void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187Target::DeleteCurrentProcess ()
188{
189 if (m_process_sp.get())
190 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000191 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 if (m_process_sp->IsAlive())
Jason Molendaede31932015-04-17 05:01:58 +0000193 m_process_sp->Destroy(false);
Jim Inghamd0a3e122011-02-16 17:54:55 +0000194
195 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000196
Greg Clayton90ba8112012-12-05 00:16:59 +0000197 CleanupProcess ();
198
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 m_process_sp.reset();
200 }
201}
202
203const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000204Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205{
206 DeleteCurrentProcess ();
Zachary Turner7529df92015-09-01 20:02:29 +0000207 m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208 return m_process_sp;
209}
210
211const lldb::ProcessSP &
212Target::GetProcessSP () const
213{
214 return m_process_sp;
215}
216
Greg Clayton3418c852011-08-10 02:10:13 +0000217void
218Target::Destroy()
219{
220 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000221 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000222 DeleteCurrentProcess ();
223 m_platform_sp.reset();
224 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000225 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000226 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000227 const bool notify = false;
228 m_breakpoint_list.RemoveAll(notify);
229 m_internal_breakpoint_list.RemoveAll(notify);
230 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000231 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000232 m_search_filter_sp.reset();
233 m_image_search_paths.Clear(notify);
Zachary Turner32abc6e2015-03-03 19:23:09 +0000234 m_persistent_variables->Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000235 m_stop_hooks.clear();
236 m_stop_hook_next_id = 0;
237 m_suppress_stop_hooks = false;
238}
239
240
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241BreakpointList &
242Target::GetBreakpointList(bool internal)
243{
244 if (internal)
245 return m_internal_breakpoint_list;
246 else
247 return m_breakpoint_list;
248}
249
250const BreakpointList &
251Target::GetBreakpointList(bool internal) const
252{
253 if (internal)
254 return m_internal_breakpoint_list;
255 else
256 return m_breakpoint_list;
257}
258
259BreakpointSP
260Target::GetBreakpointByID (break_id_t break_id)
261{
262 BreakpointSP bp_sp;
263
264 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
265 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
266 else
267 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
268
269 return bp_sp;
270}
271
272BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000273Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000274 const FileSpecList *source_file_spec_list,
275 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000276 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000277 bool hardware,
278 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279{
Jim Ingham87df91b2011-09-23 00:54:11 +0000280 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000281 if (move_to_nearest_code == eLazyBoolCalculate)
282 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
283 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex, !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000284 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000285}
286
287
288BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000289Target::CreateBreakpoint (const FileSpecList *containingModules,
290 const FileSpec &file,
291 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000292 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000293 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000294 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000295 bool hardware,
296 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000297{
Greg Clayton1f746072012-08-29 21:13:06 +0000298 if (check_inlines == eLazyBoolCalculate)
299 {
300 const InlineStrategy inline_strategy = GetInlineStrategy();
301 switch (inline_strategy)
302 {
303 case eInlineBreakpointsNever:
304 check_inlines = eLazyBoolNo;
305 break;
306
307 case eInlineBreakpointsHeaders:
308 if (file.IsSourceImplementationFile())
309 check_inlines = eLazyBoolNo;
310 else
311 check_inlines = eLazyBoolYes;
312 break;
313
314 case eInlineBreakpointsAlways:
315 check_inlines = eLazyBoolYes;
316 break;
317 }
318 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000319 SearchFilterSP filter_sp;
320 if (check_inlines == eLazyBoolNo)
321 {
322 // Not checking for inlines, we are looking only for matching compile units
323 FileSpecList compile_unit_list;
324 compile_unit_list.Append (file);
325 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
326 }
327 else
328 {
329 filter_sp = GetSearchFilterForModuleList (containingModules);
330 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000331 if (skip_prologue == eLazyBoolCalculate)
332 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000333 if (move_to_nearest_code == eLazyBoolCalculate)
334 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000335
Greg Clayton1f746072012-08-29 21:13:06 +0000336 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
337 file,
338 line_no,
339 check_inlines,
Ilia K055ad9b2015-05-18 13:41:01 +0000340 skip_prologue,
341 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000342 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343}
344
345
346BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000347Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000350
351 // Check for any reason we want to move this breakpoint to other address.
352 addr = GetBreakableLoadAddress(addr);
353
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 // Attempt to resolve our load address if possible, though it is ok if
355 // it doesn't resolve to section/offset.
356
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000357 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000358 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000359 if (!so_addr.IsValid())
360 {
361 // The address didn't resolve, so just set this as an absolute address
362 so_addr.SetOffset (addr);
363 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000364 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 return bp_sp;
366}
367
368BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000369Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000371 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000372 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000373 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374}
375
376BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000377Target::CreateBreakpoint (const FileSpecList *containingModules,
378 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000379 const char *func_name,
380 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000381 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000382 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000383 bool internal,
384 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000386 BreakpointSP bp_sp;
387 if (func_name)
388 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000389 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000390
391 if (skip_prologue == eLazyBoolCalculate)
392 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000393 if (language == lldb::eLanguageTypeUnknown)
394 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000395
Greg Claytond16e1e52011-07-12 17:06:17 +0000396 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
397 func_name,
398 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000399 language,
Greg Claytond16e1e52011-07-12 17:06:17 +0000400 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000401 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000402 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000403 }
404 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405}
406
Jim Inghamfab10e82012-03-06 00:37:27 +0000407lldb::BreakpointSP
408Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000409 const FileSpecList *containingSourceFiles,
410 const std::vector<std::string> &func_names,
411 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000412 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000413 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000414 bool internal,
415 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000416{
417 BreakpointSP bp_sp;
418 size_t num_names = func_names.size();
419 if (num_names > 0)
420 {
421 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000422
423 if (skip_prologue == eLazyBoolCalculate)
424 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000425 if (language == lldb::eLanguageTypeUnknown)
426 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000427
428 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000429 func_names,
430 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000431 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000432 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000433 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000434 }
435 return bp_sp;
436}
437
Jim Ingham133e0fb2012-03-03 02:05:11 +0000438BreakpointSP
439Target::CreateBreakpoint (const FileSpecList *containingModules,
440 const FileSpecList *containingSourceFiles,
441 const char *func_names[],
442 size_t num_names,
443 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000444 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000445 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000446 bool internal,
447 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000448{
449 BreakpointSP bp_sp;
450 if (num_names > 0)
451 {
452 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
453
Greg Clayton03da4cc2013-04-19 21:31:16 +0000454 if (skip_prologue == eLazyBoolCalculate)
455 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000456 if (language == lldb::eLanguageTypeUnknown)
457 language = GetLanguage();
458
Greg Clayton03da4cc2013-04-19 21:31:16 +0000459
460 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000461 func_names,
462 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000463 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000464 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000465 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000466 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000467 }
468 return bp_sp;
469}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470
471SearchFilterSP
472Target::GetSearchFilterForModule (const FileSpec *containingModule)
473{
474 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 if (containingModule != NULL)
476 {
477 // TODO: We should look into sharing module based search filters
478 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000479 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 }
481 else
482 {
483 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000484 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 filter_sp = m_search_filter_sp;
486 }
487 return filter_sp;
488}
489
Jim Ingham969795f2011-09-21 01:17:13 +0000490SearchFilterSP
491Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
492{
493 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000494 if (containingModules && containingModules->GetSize() != 0)
495 {
496 // TODO: We should look into sharing module based search filters
497 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000498 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000499 }
500 else
501 {
502 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000503 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000504 filter_sp = m_search_filter_sp;
505 }
506 return filter_sp;
507}
508
Jim Ingham87df91b2011-09-23 00:54:11 +0000509SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000510Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
511 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000512{
513 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
514 return GetSearchFilterForModuleList(containingModules);
515
516 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000517 if (containingModules == NULL)
518 {
519 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
520 // but that will take a little reworking.
521
Greg Claytone1cd1be2012-01-29 20:56:30 +0000522 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000523 }
524 else
525 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000526 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000527 }
528 return filter_sp;
529}
530
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000532Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000533 const FileSpecList *containingSourceFiles,
534 RegularExpression &func_regex,
535 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000536 bool internal,
537 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538{
Jim Ingham87df91b2011-09-23 00:54:11 +0000539 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000540 bool skip =
541 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
542 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000543 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
544 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000545 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546
Jim Ingham1460e4b2014-01-10 23:46:59 +0000547 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000548}
549
Jim Ingham219ba192012-03-05 04:47:34 +0000550lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000551Target::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 +0000552{
Jim Inghama72b31c2015-04-22 19:42:18 +0000553 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
554 if (exc_bkpt_sp && additional_args)
555 {
556 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
557 if (precondition_sp && additional_args)
558 {
559 if (error)
560 *error = precondition_sp->ConfigurePrecondition(*additional_args);
561 else
562 precondition_sp->ConfigurePrecondition(*additional_args);
563 }
564 }
565 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000566}
Jim Inghama72b31c2015-04-22 19:42:18 +0000567
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000569Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570{
571 BreakpointSP bp_sp;
572 if (filter_sp && resolver_sp)
573 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000574 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000576 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000578 return bp_sp;
579}
580
581void
582Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
583{
584 if (!bp_sp)
585 return;
586 if (internal)
587 m_internal_breakpoint_list.Add (bp_sp, false);
588 else
589 m_breakpoint_list.Add (bp_sp, true);
590
591 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
592 if (log)
593 {
594 StreamString s;
595 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
596 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
597 }
598
599 bp_sp->ResolveBreakpoint();
600
601 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000602 {
603 m_last_created_breakpoint = bp_sp;
604 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605}
606
Johnny Chen86364b42011-09-20 23:28:55 +0000607bool
608Target::ProcessIsValid()
609{
610 return (m_process_sp && m_process_sp->IsAlive());
611}
612
Johnny Chenb90827e2012-06-04 23:19:54 +0000613static bool
614CheckIfWatchpointsExhausted(Target *target, Error &error)
615{
616 uint32_t num_supported_hardware_watchpoints;
617 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
618 if (rc.Success())
619 {
620 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
621 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
622 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
623 num_supported_hardware_watchpoints);
624 }
625 return false;
626}
627
Johnny Chen01a67862011-10-14 00:42:25 +0000628// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000629// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000630WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000631Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000632{
Greg Clayton5160ce52013-03-27 23:08:40 +0000633 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000634 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000635 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000636 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000637
Johnny Chen01a67862011-10-14 00:42:25 +0000638 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000639 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000640 {
641 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000642 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000643 }
Jim Inghamc6462312013-06-18 21:52:48 +0000644
Johnny Chen45e541f2011-09-14 22:20:15 +0000645 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000646 {
647 if (size == 0)
648 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
649 else
Daniel Malead01b2952012-11-29 21:49:15 +0000650 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000651 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000652 }
Jim Inghamc6462312013-06-18 21:52:48 +0000653
654 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
655 {
656 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
657 }
Johnny Chen7313a642011-09-13 01:15:36 +0000658
Johnny Chen01a67862011-10-14 00:42:25 +0000659 // Currently we only support one watchpoint per address, with total number
660 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000661
662 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000663 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 +0000664 Mutex::Locker locker;
665 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000666 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000667 if (matched_sp)
668 {
Johnny Chen0c406372011-09-14 20:23:45 +0000669 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000670 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000671 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
672 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000673 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000674 if (size == old_size && kind == old_type)
675 {
Johnny Chen01a67862011-10-14 00:42:25 +0000676 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000677 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000678 }
679 else
680 {
Johnny Chen01a67862011-10-14 00:42:25 +0000681 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000682 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
683 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000684 }
Johnny Chen3c532582011-09-13 23:29:31 +0000685 }
686
Jason Molenda727e3922012-12-05 23:07:34 +0000687 if (!wp_sp)
688 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000689 wp_sp.reset(new Watchpoint(*this, addr, size, type));
690 wp_sp->SetWatchpointType(kind, notify);
691 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000692 }
Johnny Chen0c406372011-09-14 20:23:45 +0000693
Jim Ingham1b5792e2012-12-18 02:03:49 +0000694 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000695 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000696 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
697 __FUNCTION__,
698 error.Success() ? "succeeded" : "failed",
699 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000700
Jason Molenda727e3922012-12-05 23:07:34 +0000701 if (error.Fail())
702 {
Johnny Chen41b77262012-03-26 22:00:10 +0000703 // Enabling the watchpoint on the device side failed.
704 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000705 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000706 // See if we could provide more helpful error message.
707 if (!CheckIfWatchpointsExhausted(this, error))
708 {
709 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000710 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000711 }
Johnny Chen01a67862011-10-14 00:42:25 +0000712 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000713 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000714 else
Johnny Chen01a67862011-10-14 00:42:25 +0000715 m_last_created_watchpoint = wp_sp;
716 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000717}
718
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719void
720Target::RemoveAllBreakpoints (bool internal_also)
721{
Greg Clayton5160ce52013-03-27 23:08:40 +0000722 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000723 if (log)
724 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
725
Greg Clayton9fed0d82010-07-23 23:33:17 +0000726 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000728 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000729
730 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731}
732
733void
734Target::DisableAllBreakpoints (bool internal_also)
735{
Greg Clayton5160ce52013-03-27 23:08:40 +0000736 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 if (log)
738 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
739
740 m_breakpoint_list.SetEnabledAll (false);
741 if (internal_also)
742 m_internal_breakpoint_list.SetEnabledAll (false);
743}
744
745void
746Target::EnableAllBreakpoints (bool internal_also)
747{
Greg Clayton5160ce52013-03-27 23:08:40 +0000748 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749 if (log)
750 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
751
752 m_breakpoint_list.SetEnabledAll (true);
753 if (internal_also)
754 m_internal_breakpoint_list.SetEnabledAll (true);
755}
756
757bool
758Target::RemoveBreakpointByID (break_id_t break_id)
759{
Greg Clayton5160ce52013-03-27 23:08:40 +0000760 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 if (log)
762 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
763
764 if (DisableBreakpointByID (break_id))
765 {
766 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000767 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000769 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000770 if (m_last_created_breakpoint)
771 {
772 if (m_last_created_breakpoint->GetID() == break_id)
773 m_last_created_breakpoint.reset();
774 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000775 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000776 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 return true;
778 }
779 return false;
780}
781
782bool
783Target::DisableBreakpointByID (break_id_t break_id)
784{
Greg Clayton5160ce52013-03-27 23:08:40 +0000785 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000786 if (log)
787 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
788
789 BreakpointSP bp_sp;
790
791 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
792 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
793 else
794 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
795 if (bp_sp)
796 {
797 bp_sp->SetEnabled (false);
798 return true;
799 }
800 return false;
801}
802
803bool
804Target::EnableBreakpointByID (break_id_t break_id)
805{
Greg Clayton5160ce52013-03-27 23:08:40 +0000806 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 if (log)
808 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
809 __FUNCTION__,
810 break_id,
811 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
812
813 BreakpointSP bp_sp;
814
815 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
816 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
817 else
818 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
819
820 if (bp_sp)
821 {
822 bp_sp->SetEnabled (true);
823 return true;
824 }
825 return false;
826}
827
Johnny Chenedf50372011-09-23 21:21:43 +0000828// The flag 'end_to_end', default to true, signifies that the operation is
829// performed end to end, for both the debugger and the debuggee.
830
Johnny Chen01a67862011-10-14 00:42:25 +0000831// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
832// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000833bool
Johnny Chen01a67862011-10-14 00:42:25 +0000834Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000835{
Greg Clayton5160ce52013-03-27 23:08:40 +0000836 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000837 if (log)
838 log->Printf ("Target::%s\n", __FUNCTION__);
839
Johnny Chenedf50372011-09-23 21:21:43 +0000840 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000841 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000842 return true;
843 }
844
845 // Otherwise, it's an end to end operation.
846
Johnny Chen86364b42011-09-20 23:28:55 +0000847 if (!ProcessIsValid())
848 return false;
849
Johnny Chen01a67862011-10-14 00:42:25 +0000850 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000851 for (size_t i = 0; i < num_watchpoints; ++i)
852 {
Johnny Chen01a67862011-10-14 00:42:25 +0000853 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
854 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000855 return false;
856
Johnny Chen01a67862011-10-14 00:42:25 +0000857 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000858 if (rc.Fail())
859 return false;
860 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000861 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000862 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000863 return true; // Success!
864}
865
Johnny Chen01a67862011-10-14 00:42:25 +0000866// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
867// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000868bool
Johnny Chen01a67862011-10-14 00:42:25 +0000869Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000870{
Greg Clayton5160ce52013-03-27 23:08:40 +0000871 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000872 if (log)
873 log->Printf ("Target::%s\n", __FUNCTION__);
874
Johnny Chenedf50372011-09-23 21:21:43 +0000875 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000876 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000877 return true;
878 }
879
880 // Otherwise, it's an end to end operation.
881
Johnny Chen86364b42011-09-20 23:28:55 +0000882 if (!ProcessIsValid())
883 return false;
884
Johnny Chen01a67862011-10-14 00:42:25 +0000885 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000886 for (size_t i = 0; i < num_watchpoints; ++i)
887 {
Johnny Chen01a67862011-10-14 00:42:25 +0000888 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
889 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000890 return false;
891
Johnny Chen01a67862011-10-14 00:42:25 +0000892 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000893 if (rc.Fail())
894 return false;
895 }
Johnny Chen86364b42011-09-20 23:28:55 +0000896 return true; // Success!
897}
898
Johnny Chen01a67862011-10-14 00:42:25 +0000899// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
900// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000901bool
Johnny Chen01a67862011-10-14 00:42:25 +0000902Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000903{
Greg Clayton5160ce52013-03-27 23:08:40 +0000904 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000905 if (log)
906 log->Printf ("Target::%s\n", __FUNCTION__);
907
Johnny Chenedf50372011-09-23 21:21:43 +0000908 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000909 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000910 return true;
911 }
912
913 // Otherwise, it's an end to end operation.
914
Johnny Chen86364b42011-09-20 23:28:55 +0000915 if (!ProcessIsValid())
916 return false;
917
Johnny Chen01a67862011-10-14 00:42:25 +0000918 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000919 for (size_t i = 0; i < num_watchpoints; ++i)
920 {
Johnny Chen01a67862011-10-14 00:42:25 +0000921 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
922 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000923 return false;
924
Johnny Chen01a67862011-10-14 00:42:25 +0000925 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000926 if (rc.Fail())
927 return false;
928 }
Johnny Chen86364b42011-09-20 23:28:55 +0000929 return true; // Success!
930}
931
Johnny Chena4d6bc92012-02-25 06:44:30 +0000932// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
933bool
934Target::ClearAllWatchpointHitCounts ()
935{
Greg Clayton5160ce52013-03-27 23:08:40 +0000936 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000937 if (log)
938 log->Printf ("Target::%s\n", __FUNCTION__);
939
940 size_t num_watchpoints = m_watchpoint_list.GetSize();
941 for (size_t i = 0; i < num_watchpoints; ++i)
942 {
943 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
944 if (!wp_sp)
945 return false;
946
947 wp_sp->ResetHitCount();
948 }
949 return true; // Success!
950}
951
Enrico Granata5e3fe042015-02-11 00:37:54 +0000952// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
953bool
954Target::ClearAllWatchpointHistoricValues ()
955{
956 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
957 if (log)
958 log->Printf ("Target::%s\n", __FUNCTION__);
959
960 size_t num_watchpoints = m_watchpoint_list.GetSize();
961 for (size_t i = 0; i < num_watchpoints; ++i)
962 {
963 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
964 if (!wp_sp)
965 return false;
966
967 wp_sp->ResetHistoricValues();
968 }
969 return true; // Success!
970}
971
Johnny Chen01a67862011-10-14 00:42:25 +0000972// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000973// during these operations.
974bool
Johnny Chen01a67862011-10-14 00:42:25 +0000975Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000976{
Greg Clayton5160ce52013-03-27 23:08:40 +0000977 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000978 if (log)
979 log->Printf ("Target::%s\n", __FUNCTION__);
980
981 if (!ProcessIsValid())
982 return false;
983
Johnny Chen01a67862011-10-14 00:42:25 +0000984 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000985 for (size_t i = 0; i < num_watchpoints; ++i)
986 {
Johnny Chen01a67862011-10-14 00:42:25 +0000987 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
988 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000989 return false;
990
Johnny Chen01a67862011-10-14 00:42:25 +0000991 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000992 }
993 return true; // Success!
994}
995
Johnny Chen01a67862011-10-14 00:42:25 +0000996// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000997bool
Johnny Chen01a67862011-10-14 00:42:25 +0000998Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000999{
Greg Clayton5160ce52013-03-27 23:08:40 +00001000 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001001 if (log)
1002 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1003
1004 if (!ProcessIsValid())
1005 return false;
1006
Johnny Chen01a67862011-10-14 00:42:25 +00001007 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1008 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001009 {
Johnny Chen01a67862011-10-14 00:42:25 +00001010 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001011 if (rc.Success())
1012 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001013
Johnny Chenf04ee932011-09-22 18:04:58 +00001014 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001015 }
1016 return false;
1017}
1018
Johnny Chen01a67862011-10-14 00:42:25 +00001019// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001020bool
Johnny Chen01a67862011-10-14 00:42:25 +00001021Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001022{
Greg Clayton5160ce52013-03-27 23:08:40 +00001023 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001024 if (log)
1025 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1026
1027 if (!ProcessIsValid())
1028 return false;
1029
Johnny Chen01a67862011-10-14 00:42:25 +00001030 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1031 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001032 {
Johnny Chen01a67862011-10-14 00:42:25 +00001033 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001034 if (rc.Success())
1035 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001036
Johnny Chenf04ee932011-09-22 18:04:58 +00001037 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001038 }
1039 return false;
1040}
1041
Johnny Chen01a67862011-10-14 00:42:25 +00001042// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001043bool
Johnny Chen01a67862011-10-14 00:42:25 +00001044Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001045{
Greg Clayton5160ce52013-03-27 23:08:40 +00001046 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001047 if (log)
1048 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1049
Jim Inghamb0b45132013-07-02 02:09:46 +00001050 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1051 if (watch_to_remove_sp == m_last_created_watchpoint)
1052 m_last_created_watchpoint.reset();
1053
Johnny Chen01a67862011-10-14 00:42:25 +00001054 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001055 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001056 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001057 return true;
1058 }
1059 return false;
1060}
1061
Johnny Chen01a67862011-10-14 00:42:25 +00001062// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001063bool
Johnny Chen01a67862011-10-14 00:42:25 +00001064Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001065{
Greg Clayton5160ce52013-03-27 23:08:40 +00001066 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001067 if (log)
1068 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1069
1070 if (!ProcessIsValid())
1071 return false;
1072
Johnny Chen01a67862011-10-14 00:42:25 +00001073 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1074 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001075 {
Johnny Chen01a67862011-10-14 00:42:25 +00001076 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001077 return true;
1078 }
1079 return false;
1080}
1081
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001082ModuleSP
1083Target::GetExecutableModule ()
1084{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001085 // search for the first executable in the module list
1086 for (size_t i = 0; i < m_images.GetSize(); ++i)
1087 {
1088 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1089 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1090 if (obj == nullptr)
1091 continue;
1092 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1093 return module_sp;
1094 }
1095 // as fall back return the first module loaded
1096 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001097}
1098
1099Module*
1100Target::GetExecutableModulePointer ()
1101{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001102 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001103}
1104
Enrico Granata17598482012-11-08 02:22:02 +00001105static void
1106LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1107{
1108 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001109 StreamString feedback_stream;
1110 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001111 {
Enrico Granata97303392013-05-21 00:00:30 +00001112 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001113 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001114 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1115 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001116 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001117 if (feedback_stream.GetSize())
1118 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1119 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001120}
1121
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001122void
Greg Claytonb35db632013-11-09 00:03:31 +00001123Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124{
Greg Claytonb35db632013-11-09 00:03:31 +00001125 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001126 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127 m_images.Clear();
1128 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001129 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001130 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001131}
1132
1133void
Greg Claytonb35db632013-11-09 00:03:31 +00001134Target::DidExec ()
1135{
1136 // When a process exec's we need to know about it so we can do some cleanup.
1137 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1138 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1139}
1140
1141void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001142Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1143{
1144 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001145 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146
1147 if (executable_sp.get())
1148 {
1149 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001150 "Target::SetExecutableModule (executable = '%s')",
1151 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001153 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001154
Jim Ingham5aee1622010-08-09 23:31:02 +00001155 // 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 +00001156 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001157 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001158 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001159 if (log)
1160 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1161 }
1162
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001164 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001165
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001166 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 {
1168 executable_objfile->GetDependentModules(dependent_files);
1169 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1170 {
Greg Claytonded470d2011-03-19 01:12:21 +00001171 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1172 FileSpec platform_dependent_file_spec;
1173 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001174 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001175 else
1176 platform_dependent_file_spec = dependent_file_spec;
1177
Greg Claytonb9a01b32012-02-26 05:51:37 +00001178 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1179 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001180 if (image_module_sp.get())
1181 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001182 ObjectFile *objfile = image_module_sp->GetObjectFile();
1183 if (objfile)
1184 objfile->GetDependentModules(dependent_files);
1185 }
1186 }
1187 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188 }
1189}
1190
1191
Jim Ingham5aee1622010-08-09 23:31:02 +00001192bool
1193Target::SetArchitecture (const ArchSpec &arch_spec)
1194{
Greg Clayton5160ce52013-03-27 23:08:40 +00001195 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001196 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001197 {
Greg Clayton70512312012-05-08 01:45:38 +00001198 // If we haven't got a valid arch spec, or the architectures are
1199 // compatible, so just update the architecture. Architectures can be
1200 // equal, yet the triple OS and vendor might change, so we need to do
1201 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001202 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001203 if (log)
1204 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 +00001205 return true;
1206 }
1207 else
1208 {
1209 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001210 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001211 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 +00001212 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001213 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001214
Greg Claytonb35db632013-11-09 00:03:31 +00001215 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001216 // Need to do something about unsetting breakpoints.
1217
1218 if (executable_sp)
1219 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001220 if (log)
1221 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 +00001222 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1223 Error error = ModuleList::GetSharedModule (module_spec,
1224 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001225 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001226 NULL,
1227 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001228
1229 if (!error.Fail() && executable_sp)
1230 {
1231 SetExecutableModule (executable_sp, true);
1232 return true;
1233 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001234 }
1235 }
Greg Clayton70512312012-05-08 01:45:38 +00001236 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001237}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001239bool
1240Target::MergeArchitecture (const ArchSpec &arch_spec)
1241{
1242 if (arch_spec.IsValid())
1243 {
1244 if (m_arch.IsCompatibleMatch(arch_spec))
1245 {
1246 // The current target arch is compatible with "arch_spec", see if we
1247 // can improve our current architecture using bits from "arch_spec"
1248
1249 // Merge bits from arch_spec into "merged_arch" and set our architecture
1250 ArchSpec merged_arch (m_arch);
1251 merged_arch.MergeFrom (arch_spec);
1252 return SetArchitecture(merged_arch);
1253 }
1254 else
1255 {
1256 // The new architecture is different, we just need to replace it
1257 return SetArchitecture(arch_spec);
1258 }
1259 }
1260 return false;
1261}
1262
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263void
Enrico Granataefe637d2012-11-08 19:16:03 +00001264Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001265{
1266}
1267
1268void
Enrico Granataefe637d2012-11-08 19:16:03 +00001269Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270{
1271 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001272 if (m_valid)
1273 {
1274 ModuleList my_module_list;
1275 my_module_list.Append(module_sp);
1276 LoadScriptingResourceForModule(module_sp, this);
1277 ModulesDidLoad (my_module_list);
1278 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279}
1280
1281void
Enrico Granataefe637d2012-11-08 19:16:03 +00001282Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001283{
1284 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001285 if (m_valid)
1286 {
1287 ModuleList my_module_list;
1288 my_module_list.Append(module_sp);
1289 ModulesDidUnload (my_module_list, false);
1290 }
Enrico Granata17598482012-11-08 02:22:02 +00001291}
1292
1293void
Enrico Granataefe637d2012-11-08 19:16:03 +00001294Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295{
Jim Inghame716ae02011-08-03 01:00:06 +00001296 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001297 if (m_valid)
1298 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299}
1300
1301void
1302Target::ModulesDidLoad (ModuleList &module_list)
1303{
Greg Clayton23f8c952014-03-24 23:10:19 +00001304 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001305 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001306 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001307 if (m_process_sp)
1308 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001309 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001310 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001311 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001312 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001313}
1314
1315void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001316Target::SymbolsDidLoad (ModuleList &module_list)
1317{
Greg Clayton23f8c952014-03-24 23:10:19 +00001318 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001319 {
Jim Ingham31caf982013-06-04 23:01:35 +00001320 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001321 {
Jim Ingham31caf982013-06-04 23:01:35 +00001322 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1323 if (runtime)
1324 {
1325 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1326 objc_runtime->SymbolsDidLoad(module_list);
1327 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001328 }
Jim Ingham31caf982013-06-04 23:01:35 +00001329
Greg Clayton095eeaa2013-11-05 23:28:00 +00001330 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001331 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001332 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001333}
1334
1335void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001336Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001337{
Greg Clayton23f8c952014-03-24 23:10:19 +00001338 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001339 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001340 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001341 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001342 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001343 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001344}
1345
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001346bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001347Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001348{
Greg Clayton67cc0632012-08-22 17:17:09 +00001349 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001350 {
1351 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001352 ModuleSpec module_spec (module_file_spec);
1353 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001354
1355 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1356 // black list.
1357 if (num_modules > 0)
1358 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001359 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001360 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001361 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001362 return false;
1363 }
1364 return true;
1365 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001366 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001367 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001368}
1369
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001370bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001371Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001372{
Greg Clayton67cc0632012-08-22 17:17:09 +00001373 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001374 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001375 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001376 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001377 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001378 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001379}
1380
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001381size_t
Greg Claytondb598232011-01-07 01:57:07 +00001382Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1383{
Greg Claytone72dfb32012-02-24 01:59:29 +00001384 SectionSP section_sp (addr.GetSection());
1385 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001386 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001387 // 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 +00001388 if (section_sp->IsEncrypted())
1389 {
Greg Clayton57f06302012-05-25 17:05:55 +00001390 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001391 return 0;
1392 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001393 ModuleSP module_sp (section_sp->GetModule());
1394 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001395 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001396 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1397 if (objfile)
1398 {
1399 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1400 addr.GetOffset(),
1401 dst,
1402 dst_len);
1403 if (bytes_read > 0)
1404 return bytes_read;
1405 else
1406 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1407 }
Greg Claytondb598232011-01-07 01:57:07 +00001408 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001409 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001410 }
1411 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001412 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001413 }
1414 else
Greg Claytondb598232011-01-07 01:57:07 +00001415 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001416
Greg Claytondb598232011-01-07 01:57:07 +00001417 return 0;
1418}
1419
1420size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001421Target::ReadMemory (const Address& addr,
1422 bool prefer_file_cache,
1423 void *dst,
1424 size_t dst_len,
1425 Error &error,
1426 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001429
Enrico Granata9128ee22011-09-06 19:20:51 +00001430 // if we end up reading this from process memory, we will fill this
1431 // with the actual load address
1432 if (load_addr_ptr)
1433 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1434
Greg Claytondb598232011-01-07 01:57:07 +00001435 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001436
1437 addr_t load_addr = LLDB_INVALID_ADDRESS;
1438 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001439 Address resolved_addr;
1440 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001441 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001442 SectionLoadList &section_load_list = GetSectionLoadList();
1443 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001444 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001445 // No sections are loaded, so we must assume we are not running
1446 // yet and anything we are given is a file address.
1447 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1448 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001449 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001450 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001451 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001452 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001453 // we have manually loaded some sections with "target modules load ..."
1454 // or because we have have a live process that has sections loaded
1455 // through the dynamic loader
1456 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001457 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001458 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001459 }
Greg Clayton357132e2011-03-26 19:14:58 +00001460 if (!resolved_addr.IsValid())
1461 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001462
Greg Claytonc749eb82011-07-11 05:12:02 +00001463
Greg Claytondb598232011-01-07 01:57:07 +00001464 if (prefer_file_cache)
1465 {
1466 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1467 if (bytes_read > 0)
1468 return bytes_read;
1469 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001470
Johnny Chen86364b42011-09-20 23:28:55 +00001471 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001472 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001473 if (load_addr == LLDB_INVALID_ADDRESS)
1474 load_addr = resolved_addr.GetLoadAddress (this);
1475
Greg Claytondda4f7b2010-06-30 23:03:03 +00001476 if (load_addr == LLDB_INVALID_ADDRESS)
1477 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001478 ModuleSP addr_module_sp (resolved_addr.GetModule());
1479 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001480 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001481 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001482 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001483 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001484 else
Daniel Malead01b2952012-11-29 21:49:15 +00001485 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001486 }
1487 else
1488 {
Greg Claytondb598232011-01-07 01:57:07 +00001489 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001490 if (bytes_read != dst_len)
1491 {
1492 if (error.Success())
1493 {
1494 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001495 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 else
Daniel Malead01b2952012-11-29 21:49:15 +00001497 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 +00001498 }
1499 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001500 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001501 {
1502 if (load_addr_ptr)
1503 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001504 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001505 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001506 // If the address is not section offset we have an address that
1507 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001508 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001509 // more we can do. If it is section offset, we might be able to
1510 // read cached memory from the object file.
1511 if (!resolved_addr.IsSectionOffset())
1512 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001513 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001515
Greg Claytonc749eb82011-07-11 05:12:02 +00001516 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001517 {
Greg Claytondb598232011-01-07 01:57:07 +00001518 // If we didn't already try and read from the object file cache, then
1519 // try it after failing to read from the process.
1520 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001521 }
1522 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001523}
1524
Greg Claytond16e1e52011-07-12 17:06:17 +00001525size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001526Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1527{
1528 char buf[256];
1529 out_str.clear();
1530 addr_t curr_addr = addr.GetLoadAddress(this);
1531 Address address(addr);
1532 while (1)
1533 {
1534 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1535 if (length == 0)
1536 break;
1537 out_str.append(buf, length);
1538 // If we got "length - 1" bytes, we didn't get the whole C string, we
1539 // need to read some more characters
1540 if (length == sizeof(buf) - 1)
1541 curr_addr += length;
1542 else
1543 break;
1544 address = Address(curr_addr);
1545 }
1546 return out_str.size();
1547}
1548
1549
1550size_t
1551Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1552{
1553 size_t total_cstr_len = 0;
1554 if (dst && dst_max_len)
1555 {
1556 result_error.Clear();
1557 // NULL out everything just to be safe
1558 memset (dst, 0, dst_max_len);
1559 Error error;
1560 addr_t curr_addr = addr.GetLoadAddress(this);
1561 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001562
1563 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1564 // think this really needs to be tied to the memory cache subsystem's
1565 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001566 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001567
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001568 size_t bytes_left = dst_max_len - 1;
1569 char *curr_dst = dst;
1570
1571 while (bytes_left > 0)
1572 {
1573 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1574 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1575 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1576
1577 if (bytes_read == 0)
1578 {
1579 result_error = error;
1580 dst[total_cstr_len] = '\0';
1581 break;
1582 }
1583 const size_t len = strlen(curr_dst);
1584
1585 total_cstr_len += len;
1586
1587 if (len < bytes_to_read)
1588 break;
1589
1590 curr_dst += bytes_read;
1591 curr_addr += bytes_read;
1592 bytes_left -= bytes_read;
1593 address = Address(curr_addr);
1594 }
1595 }
1596 else
1597 {
1598 if (dst == NULL)
1599 result_error.SetErrorString("invalid arguments");
1600 else
1601 result_error.Clear();
1602 }
1603 return total_cstr_len;
1604}
1605
1606size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001607Target::ReadScalarIntegerFromMemory (const Address& addr,
1608 bool prefer_file_cache,
1609 uint32_t byte_size,
1610 bool is_signed,
1611 Scalar &scalar,
1612 Error &error)
1613{
1614 uint64_t uval;
1615
1616 if (byte_size <= sizeof(uval))
1617 {
1618 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1619 if (bytes_read == byte_size)
1620 {
1621 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001622 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001623 if (byte_size <= 4)
1624 scalar = data.GetMaxU32 (&offset, byte_size);
1625 else
1626 scalar = data.GetMaxU64 (&offset, byte_size);
1627
1628 if (is_signed)
1629 scalar.SignExtend(byte_size * 8);
1630 return bytes_read;
1631 }
1632 }
1633 else
1634 {
1635 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1636 }
1637 return 0;
1638}
1639
1640uint64_t
1641Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1642 bool prefer_file_cache,
1643 size_t integer_byte_size,
1644 uint64_t fail_value,
1645 Error &error)
1646{
1647 Scalar scalar;
1648 if (ReadScalarIntegerFromMemory (addr,
1649 prefer_file_cache,
1650 integer_byte_size,
1651 false,
1652 scalar,
1653 error))
1654 return scalar.ULongLong(fail_value);
1655 return fail_value;
1656}
1657
1658bool
1659Target::ReadPointerFromMemory (const Address& addr,
1660 bool prefer_file_cache,
1661 Error &error,
1662 Address &pointer_addr)
1663{
1664 Scalar scalar;
1665 if (ReadScalarIntegerFromMemory (addr,
1666 prefer_file_cache,
1667 m_arch.GetAddressByteSize(),
1668 false,
1669 scalar,
1670 error))
1671 {
1672 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1673 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1674 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001675 SectionLoadList &section_load_list = GetSectionLoadList();
1676 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001677 {
1678 // No sections are loaded, so we must assume we are not running
1679 // yet and anything we are given is a file address.
1680 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1681 }
1682 else
1683 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001684 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001685 // we have manually loaded some sections with "target modules load ..."
1686 // or because we have have a live process that has sections loaded
1687 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001688 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001689 }
1690 // We weren't able to resolve the pointer value, so just return
1691 // an address with no section
1692 if (!pointer_addr.IsValid())
1693 pointer_addr.SetOffset (pointer_vm_addr);
1694 return true;
1695
1696 }
1697 }
1698 return false;
1699}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700
1701ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001702Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704 ModuleSP module_sp;
1705
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001706 Error error;
1707
Jim Ingham4a94c912012-05-17 18:38:42 +00001708 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1709 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001710
Jim Ingham4a94c912012-05-17 18:38:42 +00001711 if (module_spec.GetUUID().IsValid())
1712 module_sp = m_images.FindFirstModule(module_spec);
1713
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001714 if (!module_sp)
1715 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001716 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1717 bool did_create_module = false;
1718
1719 // If there are image search path entries, try to use them first to acquire a suitable image.
1720 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001721 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001722 ModuleSpec transformed_spec (module_spec);
1723 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1724 {
1725 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1726 error = ModuleList::GetSharedModule (transformed_spec,
1727 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001728 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001729 &old_module_sp,
1730 &did_create_module);
1731 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001732 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001733
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001734 if (!module_sp)
1735 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001736 // If we have a UUID, we can check our global shared module list in case
1737 // we already have it. If we don't have a valid UUID, then we can't since
1738 // the path in "module_spec" will be a platform path, and we will need to
1739 // let the platform find that file. For example, we could be asking for
1740 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1741 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1742 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1743 // cache.
1744 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001745 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001746 // We have a UUID, it is OK to check the global module list...
1747 error = ModuleList::GetSharedModule (module_spec,
1748 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001749 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001750 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001751 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001752 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001753
1754 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001755 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001756 // The platform is responsible for finding and caching an appropriate
1757 // module in the shared module cache.
1758 if (m_platform_sp)
1759 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001760 error = m_platform_sp->GetSharedModule (module_spec,
1761 m_process_sp.get(),
1762 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001763 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001764 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001765 &did_create_module);
1766 }
1767 else
1768 {
1769 error.SetErrorString("no platform is currently set");
1770 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001771 }
1772 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001773
Jim Ingham4a94c912012-05-17 18:38:42 +00001774 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1775 // module in the list already, and if there was, let's remove it.
1776 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001777 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001778 ObjectFile *objfile = module_sp->GetObjectFile();
1779 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001780 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001781 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001782 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001783 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1784 case ObjectFile::eTypeExecutable: /// A normal executable
1785 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1786 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1787 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1788 break;
1789 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1790 if (error_ptr)
1791 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1792 return ModuleSP();
1793 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1794 if (error_ptr)
1795 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1796 return ModuleSP();
1797 default:
1798 if (error_ptr)
1799 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1800 return ModuleSP();
1801 }
1802 // GetSharedModule is not guaranteed to find the old shared module, for instance
1803 // in the common case where you pass in the UUID, it is only going to find the one
1804 // module matching the UUID. In fact, it has no good way to know what the "old module"
1805 // relevant to this target is, since there might be many copies of a module with this file spec
1806 // in various running debug sessions, but only one of them will belong to this target.
1807 // So let's remove the UUID from the module list, and look in the target's module list.
1808 // Only do this if there is SOMETHING else in the module spec...
1809 if (!old_module_sp)
1810 {
1811 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001812 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001813 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1814 module_spec_copy.GetUUID().Clear();
1815
1816 ModuleList found_modules;
1817 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1818 if (num_found == 1)
1819 {
1820 old_module_sp = found_modules.GetModuleAtIndex(0);
1821 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001822 }
1823 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001824
1825 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1826 {
1827 m_images.ReplaceModule(old_module_sp, module_sp);
1828 Module *old_module_ptr = old_module_sp.get();
1829 old_module_sp.reset();
1830 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1831 }
1832 else
1833 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001834 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001835 else
1836 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001837 }
1838 }
1839 if (error_ptr)
1840 *error_ptr = error;
1841 return module_sp;
1842}
1843
1844
Greg Claytond9e416c2012-02-18 05:35:26 +00001845TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001846Target::CalculateTarget ()
1847{
Greg Claytond9e416c2012-02-18 05:35:26 +00001848 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001849}
1850
Greg Claytond9e416c2012-02-18 05:35:26 +00001851ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001852Target::CalculateProcess ()
1853{
Greg Claytond9e416c2012-02-18 05:35:26 +00001854 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001855}
1856
Greg Claytond9e416c2012-02-18 05:35:26 +00001857ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001858Target::CalculateThread ()
1859{
Greg Claytond9e416c2012-02-18 05:35:26 +00001860 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001861}
1862
Jason Molendab57e4a12013-11-04 09:33:30 +00001863StackFrameSP
1864Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001865{
Jason Molendab57e4a12013-11-04 09:33:30 +00001866 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001867}
1868
1869void
Greg Clayton0603aa92010-10-04 01:05:56 +00001870Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001871{
Greg Claytonc14ee322011-09-22 04:58:26 +00001872 exe_ctx.Clear();
1873 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001874}
1875
1876PathMappingList &
1877Target::GetImageSearchPathList ()
1878{
1879 return m_image_search_paths;
1880}
1881
1882void
1883Target::ImageSearchPathsChanged
1884(
1885 const PathMappingList &path_list,
1886 void *baton
1887)
1888{
1889 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001890 ModuleSP exe_module_sp (target->GetExecutableModule());
1891 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001892 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001893}
1894
Jim Ingham151c0322015-09-15 21:13:50 +00001895TypeSystem *
1896Target::GetScratchTypeSystemForLanguage (lldb::LanguageType language, bool create_on_demand)
1897{
1898 if (Language::LanguageIsC(language)
1899 || Language::LanguageIsObjC(language)
1900 || Language::LanguageIsCPlusPlus(language)
1901 || language == eLanguageTypeUnknown)
1902 return GetScratchClangASTContext(create_on_demand);
1903 else
1904 return NULL;
1905}
1906
1907UserExpression *
1908Target::GetUserExpressionForLanguage(const char *expr,
1909 const char *expr_prefix,
1910 lldb::LanguageType language,
1911 Expression::ResultType desired_type,
1912 Error &error)
1913{
1914 TypeSystem *type_system = GetScratchTypeSystemForLanguage (language);
1915 UserExpression *user_expr = nullptr;
1916
1917 if (!type_system)
1918 {
1919 error.SetErrorStringWithFormat("Could not find type system for language: %s", Language::GetNameForLanguageType(language));
1920 return nullptr;
1921 }
1922
1923 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type);
1924 if (!user_expr)
1925 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1926
1927 return user_expr;
1928}
1929
1930FunctionCaller *
1931Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
1932 const CompilerType &return_type,
1933 const Address& function_address,
1934 const ValueList &arg_value_list,
1935 const char *name,
1936 Error &error)
1937{
1938 TypeSystem *type_system = GetScratchTypeSystemForLanguage (language);
1939 FunctionCaller *persistent_fn = nullptr;
1940
1941 if (!type_system)
1942 {
1943 error.SetErrorStringWithFormat("Could not find type system for language: %s", Language::GetNameForLanguageType(language));
1944 return persistent_fn;
1945 }
1946
1947 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
1948 if (!persistent_fn)
1949 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1950
1951 return persistent_fn;
1952}
1953
1954UtilityFunction *
1955Target::GetUtilityFunctionForLanguage (const char *text,
1956 lldb::LanguageType language,
1957 const char *name,
1958 Error &error)
1959{
1960 TypeSystem *type_system = GetScratchTypeSystemForLanguage (language);
1961 UtilityFunction *utility_fn = nullptr;
1962
1963 if (!type_system)
1964 {
1965 error.SetErrorStringWithFormat("Could not find type system for language: %s", Language::GetNameForLanguageType(language));
1966 return utility_fn;
1967 }
1968
1969 utility_fn = type_system->GetUtilityFunction (text, name);
1970 if (!utility_fn)
1971 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1972
1973 return utility_fn;
1974}
1975
1976
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001978Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979{
Greg Clayton73da2442011-08-03 01:23:55 +00001980 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001981 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001982 {
Jim Ingham151c0322015-09-15 21:13:50 +00001983 m_scratch_ast_context_ap.reset (new ClangASTContextForExpressions(*this));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001984 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001985 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001986 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001987 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1988 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001989 return m_scratch_ast_context_ap.get();
1990}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001991
Sean Callanan686b2312011-11-16 18:20:47 +00001992ClangASTImporter *
1993Target::GetClangASTImporter()
1994{
1995 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1996
1997 if (!ast_importer)
1998 {
1999 ast_importer = new ClangASTImporter();
2000 m_ast_importer_ap.reset(ast_importer);
2001 }
2002
2003 return ast_importer;
2004}
2005
Greg Clayton99d0faf2010-11-18 23:32:35 +00002006void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002007Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002008{
Greg Clayton6920b522012-08-22 18:39:03 +00002009 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002010}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002011
Greg Clayton99d0faf2010-11-18 23:32:35 +00002012void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002013Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002014{
Greg Clayton6920b522012-08-22 18:39:03 +00002015 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002016}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002017
Greg Claytonc859e2d2012-02-13 23:10:39 +00002018FileSpecList
2019Target::GetDefaultExecutableSearchPaths ()
2020{
Greg Clayton67cc0632012-08-22 17:17:09 +00002021 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2022 if (properties_sp)
2023 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002024 return FileSpecList();
2025}
2026
Michael Sartaina7499c92013-07-01 19:45:50 +00002027FileSpecList
2028Target::GetDefaultDebugFileSearchPaths ()
2029{
2030 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2031 if (properties_sp)
2032 return properties_sp->GetDebugFileSearchPaths();
2033 return FileSpecList();
2034}
2035
Sean Callanan85054342015-04-03 15:39:47 +00002036FileSpecList
2037Target::GetDefaultClangModuleSearchPaths ()
2038{
2039 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2040 if (properties_sp)
2041 return properties_sp->GetClangModuleSearchPaths();
2042 return FileSpecList();
2043}
2044
Caroline Ticedaccaa92010-09-20 20:44:43 +00002045ArchSpec
2046Target::GetDefaultArchitecture ()
2047{
Greg Clayton67cc0632012-08-22 17:17:09 +00002048 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2049 if (properties_sp)
2050 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002051 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002052}
2053
2054void
Greg Clayton67cc0632012-08-22 17:17:09 +00002055Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002056{
Greg Clayton67cc0632012-08-22 17:17:09 +00002057 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2058 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002059 {
2060 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 +00002061 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002062 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002063}
2064
Greg Clayton0603aa92010-10-04 01:05:56 +00002065Target *
2066Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2067{
2068 // The target can either exist in the "process" of ExecutionContext, or in
2069 // the "target_sp" member of SymbolContext. This accessor helper function
2070 // will get the target from one of these locations.
2071
2072 Target *target = NULL;
2073 if (sc_ptr != NULL)
2074 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00002075 if (target == NULL && exe_ctx_ptr)
2076 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002077 return target;
2078}
2079
Jim Ingham1624a2d2014-05-05 02:26:40 +00002080ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002081Target::EvaluateExpression
2082(
2083 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00002084 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00002085 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00002086 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002087)
2088{
Enrico Granata97fca502012-09-18 17:43:16 +00002089 result_valobj_sp.reset();
2090
Jim Ingham8646d3c2014-05-05 02:47:44 +00002091 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002092
Greg Claytond1767f02011-12-08 02:13:16 +00002093 if (expr_cstr == NULL || expr_cstr[0] == '\0')
2094 return execution_results;
2095
Jim Ingham6026ca32011-05-12 02:06:14 +00002096 // We shouldn't run stop hooks in expressions.
2097 // Be sure to reset this if you return anywhere within this function.
2098 bool old_suppress_value = m_suppress_stop_hooks;
2099 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002100
2101 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002102
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002103 if (frame)
2104 {
2105 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002106 }
2107 else if (m_process_sp)
2108 {
2109 m_process_sp->CalculateExecutionContext(exe_ctx);
2110 }
2111 else
2112 {
2113 CalculateExecutionContext(exe_ctx);
2114 }
2115
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002116 // Make sure we aren't just trying to see the value of a persistent
2117 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002118 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002119 // Only check for persistent variables the expression starts with a '$'
2120 if (expr_cstr[0] == '$')
Zachary Turner32abc6e2015-03-03 19:23:09 +00002121 persistent_var_sp = m_persistent_variables->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002122
2123 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002124 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002125 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002126 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002127 }
2128 else
2129 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002130 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002131 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002132 execution_results = UserExpression::Evaluate (exe_ctx,
2133 options,
2134 expr_cstr,
2135 prefix,
2136 result_valobj_sp,
2137 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002138 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002139
2140 m_suppress_stop_hooks = old_suppress_value;
2141
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002142 return execution_results;
2143}
2144
Zachary Turner32abc6e2015-03-03 19:23:09 +00002145ClangPersistentVariables &
2146Target::GetPersistentVariables()
2147{
2148 return *m_persistent_variables;
2149}
2150
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002151lldb::addr_t
2152Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2153{
2154 addr_t code_addr = load_addr;
2155 switch (m_arch.GetMachine())
2156 {
2157 case llvm::Triple::arm:
2158 case llvm::Triple::thumb:
2159 switch (addr_class)
2160 {
2161 case eAddressClassData:
2162 case eAddressClassDebug:
2163 return LLDB_INVALID_ADDRESS;
2164
2165 case eAddressClassUnknown:
2166 case eAddressClassInvalid:
2167 case eAddressClassCode:
2168 case eAddressClassCodeAlternateISA:
2169 case eAddressClassRuntime:
2170 // Check if bit zero it no set?
2171 if ((code_addr & 1ull) == 0)
2172 {
2173 // Bit zero isn't set, check if the address is a multiple of 2?
2174 if (code_addr & 2ull)
2175 {
2176 // The address is a multiple of 2 so it must be thumb, set bit zero
2177 code_addr |= 1ull;
2178 }
2179 else if (addr_class == eAddressClassCodeAlternateISA)
2180 {
2181 // We checked the address and the address claims to be the alternate ISA
2182 // which means thumb, so set bit zero.
2183 code_addr |= 1ull;
2184 }
2185 }
2186 break;
2187 }
2188 break;
2189
2190 default:
2191 break;
2192 }
2193 return code_addr;
2194}
2195
2196lldb::addr_t
2197Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2198{
2199 addr_t opcode_addr = load_addr;
2200 switch (m_arch.GetMachine())
2201 {
2202 case llvm::Triple::arm:
2203 case llvm::Triple::thumb:
2204 switch (addr_class)
2205 {
2206 case eAddressClassData:
2207 case eAddressClassDebug:
2208 return LLDB_INVALID_ADDRESS;
2209
2210 case eAddressClassInvalid:
2211 case eAddressClassUnknown:
2212 case eAddressClassCode:
2213 case eAddressClassCodeAlternateISA:
2214 case eAddressClassRuntime:
2215 opcode_addr &= ~(1ull);
2216 break;
2217 }
2218 break;
2219
2220 default:
2221 break;
2222 }
2223 return opcode_addr;
2224}
2225
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002226lldb::addr_t
2227Target::GetBreakableLoadAddress (lldb::addr_t addr)
2228{
2229 addr_t breakable_addr = addr;
2230 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2231
2232 switch (m_arch.GetMachine())
2233 {
2234 default:
2235 break;
2236 case llvm::Triple::mips:
2237 case llvm::Triple::mipsel:
2238 case llvm::Triple::mips64:
2239 case llvm::Triple::mips64el:
2240 {
2241 addr_t function_start = 0;
2242 addr_t current_offset = 0;
2243 uint32_t loop_count = 0;
2244 Address resolved_addr;
2245 uint32_t arch_flags = m_arch.GetFlags ();
2246 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2247 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2248 SectionLoadList &section_load_list = GetSectionLoadList();
2249
2250 if (section_load_list.IsEmpty())
2251 // No sections are loaded, so we must assume we are not running yet
2252 // and need to operate only on file address.
2253 m_images.ResolveFileAddress (addr, resolved_addr);
2254 else
2255 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2256
2257 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2258 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2259 if (temp_addr_module_sp)
2260 {
2261 SymbolContext sc;
2262 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002263 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002264 if (sc.function)
2265 {
2266 function_start = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(this);
2267 if (function_start == LLDB_INVALID_ADDRESS)
2268 function_start = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
2269 }
2270 else if (sc.symbol)
2271 {
2272 Address sym_addr = sc.symbol->GetAddress();
2273 function_start = sym_addr.GetFileAddress();
2274 }
2275 current_offset = addr - function_start;
2276 }
2277
2278 // If breakpoint address is start of function then we dont have to do anything.
2279 if (current_offset == 0)
2280 return breakable_addr;
2281 else
2282 loop_count = current_offset / 2;
2283
2284 if (loop_count > 3)
2285 {
2286 // Scan previous 6 bytes
2287 if (IsMips16 | IsMicromips)
2288 loop_count = 3;
2289 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2290 else
2291 loop_count = 2;
2292 }
2293
2294 // Create Disassembler Instance
2295 lldb::DisassemblerSP disasm_sp (Disassembler::FindPlugin(m_arch, NULL, NULL));
2296
2297 ExecutionContext exe_ctx;
2298 CalculateExecutionContext(exe_ctx);
2299 InstructionList instruction_list;
2300 InstructionSP prev_insn;
2301 bool prefer_file_cache = true; // Read from file
2302 uint32_t inst_to_choose = 0;
2303
2304 for (uint32_t i = 1; i <= loop_count; i++)
2305 {
2306 // Adjust the address to read from.
2307 resolved_addr.Slide (-2);
2308 AddressRange range(resolved_addr, i*2);
2309 uint32_t insn_size = 0;
2310
Greg Clayton6071e6f2015-08-26 22:57:51 +00002311 disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002312
2313 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2314 if (num_insns)
2315 {
2316 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2317 insn_size = prev_insn->GetOpcode().GetByteSize();
2318 if (i == 1 && insn_size == 2)
2319 {
2320 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2321 instruction_list.Append(prev_insn);
2322 inst_to_choose = 1;
2323 }
2324 else if (i == 2)
2325 {
2326 // Here we may get one 4-byte instruction or two 2-byte instructions.
2327 if (num_insns == 2)
2328 {
2329 // Looks like there are two 2-byte instructions above our breakpoint target address.
2330 // 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.
2331 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2332 // and whatever i=1 iteration has found out is true.
2333 inst_to_choose = 1;
2334 break;
2335 }
2336 else if (insn_size == 4)
2337 {
2338 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2339 // Lets try scanning upper 2 bytes to verify this.
2340 instruction_list.Append(prev_insn);
2341 inst_to_choose = 2;
2342 }
2343 }
2344 else if (i == 3)
2345 {
2346 if (insn_size == 4)
2347 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2348 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2349 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2350 inst_to_choose = 0;
2351 else
2352 // This is straight-forward
2353 inst_to_choose = 2;
2354 break;
2355 }
2356 }
2357 else
2358 {
2359 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2360 if (i > 1)
2361 {
2362 inst_to_choose = i - 1;
2363 break;
2364 }
2365 }
2366 }
2367
2368 // Check if we are able to find any valid instruction.
2369 if (inst_to_choose)
2370 {
2371 if (inst_to_choose > instruction_list.GetSize())
2372 inst_to_choose--;
2373 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2374
2375 if (prev_insn->HasDelaySlot())
2376 {
2377 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2378 // Adjust the breakable address
2379 breakable_addr = addr - shift_size;
2380 if (log)
2381 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);
2382 }
2383 }
2384 break;
2385 }
2386 }
2387 return breakable_addr;
2388}
2389
Greg Clayton9585fbf2013-03-19 00:20:55 +00002390SourceManager &
2391Target::GetSourceManager ()
2392{
2393 if (m_source_manager_ap.get() == NULL)
2394 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2395 return *m_source_manager_ap;
2396}
2397
Sean Callanan9998acd2014-12-05 01:21:59 +00002398ClangModulesDeclVendor *
2399Target::GetClangModulesDeclVendor ()
2400{
2401 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2402
2403 {
2404 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2405
2406 if (!m_clang_modules_decl_vendor_ap)
2407 {
2408 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2409 }
2410 }
2411
2412 return m_clang_modules_decl_vendor_ap.get();
2413}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002414
Greg Clayton44d93782014-01-27 23:43:24 +00002415Target::StopHookSP
2416Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002417{
2418 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002419 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2420 m_stop_hooks[new_uid] = stop_hook_sp;
2421 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002422}
2423
2424bool
2425Target::RemoveStopHookByID (lldb::user_id_t user_id)
2426{
2427 size_t num_removed;
2428 num_removed = m_stop_hooks.erase (user_id);
2429 if (num_removed == 0)
2430 return false;
2431 else
2432 return true;
2433}
2434
2435void
2436Target::RemoveAllStopHooks ()
2437{
2438 m_stop_hooks.clear();
2439}
2440
2441Target::StopHookSP
2442Target::GetStopHookByID (lldb::user_id_t user_id)
2443{
2444 StopHookSP found_hook;
2445
2446 StopHookCollection::iterator specified_hook_iter;
2447 specified_hook_iter = m_stop_hooks.find (user_id);
2448 if (specified_hook_iter != m_stop_hooks.end())
2449 found_hook = (*specified_hook_iter).second;
2450 return found_hook;
2451}
2452
2453bool
2454Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2455{
2456 StopHookCollection::iterator specified_hook_iter;
2457 specified_hook_iter = m_stop_hooks.find (user_id);
2458 if (specified_hook_iter == m_stop_hooks.end())
2459 return false;
2460
2461 (*specified_hook_iter).second->SetIsActive (active_state);
2462 return true;
2463}
2464
2465void
2466Target::SetAllStopHooksActiveState (bool active_state)
2467{
2468 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2469 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2470 {
2471 (*pos).second->SetIsActive (active_state);
2472 }
2473}
2474
2475void
2476Target::RunStopHooks ()
2477{
Jim Ingham6026ca32011-05-12 02:06:14 +00002478 if (m_suppress_stop_hooks)
2479 return;
2480
Jim Ingham9575d842011-03-11 03:53:59 +00002481 if (!m_process_sp)
2482 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002483
2484 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2485 // since in that case we do not want to run the stop-hooks
2486 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2487 return;
2488
Jim Ingham9575d842011-03-11 03:53:59 +00002489 if (m_stop_hooks.empty())
2490 return;
2491
2492 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2493
2494 // If there aren't any active stop hooks, don't bother either:
2495 bool any_active_hooks = false;
2496 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2497 {
2498 if ((*pos).second->IsActive())
2499 {
2500 any_active_hooks = true;
2501 break;
2502 }
2503 }
2504 if (!any_active_hooks)
2505 return;
2506
2507 CommandReturnObject result;
2508
2509 std::vector<ExecutionContext> exc_ctx_with_reasons;
2510 std::vector<SymbolContext> sym_ctx_with_reasons;
2511
2512 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2513 size_t num_threads = cur_threadlist.GetSize();
2514 for (size_t i = 0; i < num_threads; i++)
2515 {
2516 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2517 if (cur_thread_sp->ThreadStoppedForAReason())
2518 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002519 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002520 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2521 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2522 }
2523 }
2524
2525 // If no threads stopped for a reason, don't run the stop-hooks.
2526 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2527 if (num_exe_ctx == 0)
2528 return;
2529
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002530 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2531 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002532
2533 bool keep_going = true;
2534 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002535 bool print_hook_header;
2536 bool print_thread_header;
2537
2538 if (num_exe_ctx == 1)
2539 print_thread_header = false;
2540 else
2541 print_thread_header = true;
2542
2543 if (m_stop_hooks.size() == 1)
2544 print_hook_header = false;
2545 else
2546 print_hook_header = true;
2547
Jim Ingham9575d842011-03-11 03:53:59 +00002548 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2549 {
2550 // result.Clear();
2551 StopHookSP cur_hook_sp = (*pos).second;
2552 if (!cur_hook_sp->IsActive())
2553 continue;
2554
2555 bool any_thread_matched = false;
2556 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2557 {
2558 if ((cur_hook_sp->GetSpecifier () == NULL
2559 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2560 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002561 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002562 {
2563 if (!hooks_ran)
2564 {
Jim Ingham9575d842011-03-11 03:53:59 +00002565 hooks_ran = true;
2566 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002567 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002568 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002569 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2570 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2571 NULL);
2572 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002573 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002574 else
Daniel Malead01b2952012-11-29 21:49:15 +00002575 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002576 any_thread_matched = true;
2577 }
2578
Jim Ingham381e25b2011-03-22 01:47:27 +00002579 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002580 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002581
2582 CommandInterpreterRunOptions options;
2583 options.SetStopOnContinue (true);
2584 options.SetStopOnError (true);
2585 options.SetEchoCommands (false);
2586 options.SetPrintResults (true);
2587 options.SetAddToHistory (false);
2588
2589 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2590 &exc_ctx_with_reasons[i],
2591 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002592 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002593
2594 // If the command started the target going again, we should bag out of
2595 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002596 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2597 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002598 {
Daniel Malead01b2952012-11-29 21:49:15 +00002599 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002600 keep_going = false;
2601 }
2602 }
2603 }
2604 }
Jason Molenda879cf772011-09-23 00:42:55 +00002605
Caroline Tice969ed3d2011-05-02 20:41:46 +00002606 result.GetImmediateOutputStream()->Flush();
2607 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002608}
2609
Greg Claytonfbb76342013-11-20 21:07:01 +00002610const TargetPropertiesSP &
2611Target::GetGlobalProperties()
2612{
2613 static TargetPropertiesSP g_settings_sp;
2614 if (!g_settings_sp)
2615 {
2616 g_settings_sp.reset (new TargetProperties (NULL));
2617 }
2618 return g_settings_sp;
2619}
2620
2621Error
2622Target::Install (ProcessLaunchInfo *launch_info)
2623{
2624 Error error;
2625 PlatformSP platform_sp (GetPlatform());
2626 if (platform_sp)
2627 {
2628 if (platform_sp->IsRemote())
2629 {
2630 if (platform_sp->IsConnected())
2631 {
2632 // Install all files that have an install path, and always install the
2633 // main executable when connected to a remote platform
2634 const ModuleList& modules = GetImages();
2635 const size_t num_images = modules.GetSize();
2636 for (size_t idx = 0; idx < num_images; ++idx)
2637 {
2638 const bool is_main_executable = idx == 0;
2639 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2640 if (module_sp)
2641 {
2642 FileSpec local_file (module_sp->GetFileSpec());
2643 if (local_file)
2644 {
2645 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2646 if (!remote_file)
2647 {
2648 if (is_main_executable) // TODO: add setting for always installing main executable???
2649 {
2650 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002651 remote_file = platform_sp->GetRemoteWorkingDirectory();
2652 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002653 }
2654 }
2655 if (remote_file)
2656 {
2657 error = platform_sp->Install(local_file, remote_file);
2658 if (error.Success())
2659 {
2660 module_sp->SetPlatformFileSpec(remote_file);
2661 if (is_main_executable)
2662 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002663 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002664 if (launch_info)
2665 launch_info->SetExecutableFile(remote_file, false);
2666 }
2667 }
2668 else
2669 break;
2670 }
2671 }
2672 }
2673 }
2674 }
2675 }
2676 }
2677 return error;
2678}
Greg Clayton7b242382011-07-08 00:48:09 +00002679
Greg Claytond5944cd2013-12-06 01:12:00 +00002680bool
2681Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2682{
2683 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2684}
2685
2686bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002687Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2688{
2689 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2690}
2691
2692bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002693Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2694{
2695 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2696 if (old_section_load_addr != new_section_load_addr)
2697 {
2698 uint32_t stop_id = 0;
2699 ProcessSP process_sp(GetProcessSP());
2700 if (process_sp)
2701 stop_id = process_sp->GetStopID();
2702 else
2703 stop_id = m_section_load_history.GetLastStopID();
2704 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2705 return true; // Return true if the section load address was changed...
2706 }
2707 return false; // Return false to indicate nothing changed
2708
2709}
2710
Greg Clayton8012cad2014-11-17 19:39:20 +00002711size_t
2712Target::UnloadModuleSections (const ModuleList &module_list)
2713{
2714 size_t section_unload_count = 0;
2715 size_t num_modules = module_list.GetSize();
2716 for (size_t i=0; i<num_modules; ++i)
2717 {
2718 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2719 }
2720 return section_unload_count;
2721}
2722
2723size_t
2724Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2725{
2726 uint32_t stop_id = 0;
2727 ProcessSP process_sp(GetProcessSP());
2728 if (process_sp)
2729 stop_id = process_sp->GetStopID();
2730 else
2731 stop_id = m_section_load_history.GetLastStopID();
2732 SectionList *sections = module_sp->GetSectionList();
2733 size_t section_unload_count = 0;
2734 if (sections)
2735 {
2736 const uint32_t num_sections = sections->GetNumSections(0);
2737 for (uint32_t i = 0; i < num_sections; ++i)
2738 {
2739 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2740 }
2741 }
2742 return section_unload_count;
2743}
2744
Greg Claytond5944cd2013-12-06 01:12:00 +00002745bool
2746Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2747{
2748 uint32_t stop_id = 0;
2749 ProcessSP process_sp(GetProcessSP());
2750 if (process_sp)
2751 stop_id = process_sp->GetStopID();
2752 else
2753 stop_id = m_section_load_history.GetLastStopID();
2754 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2755}
2756
2757bool
2758Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2759{
2760 uint32_t stop_id = 0;
2761 ProcessSP process_sp(GetProcessSP());
2762 if (process_sp)
2763 stop_id = process_sp->GetStopID();
2764 else
2765 stop_id = m_section_load_history.GetLastStopID();
2766 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2767}
2768
2769void
2770Target::ClearAllLoadedSections ()
2771{
2772 m_section_load_history.Clear();
2773}
2774
Greg Claytonb09c5382013-12-13 17:20:18 +00002775
2776Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002777Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002778{
2779 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002780 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2781
2782 if (log)
2783 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2784
Greg Claytonb09c5382013-12-13 17:20:18 +00002785 StateType state = eStateInvalid;
2786
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002787 // Scope to temporarily get the process state in case someone has manually
2788 // remotely connected already to a process and we can skip the platform
2789 // launching.
2790 {
2791 ProcessSP process_sp (GetProcessSP());
2792
2793 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002794 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002795 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002796 if (log)
2797 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2798 }
2799 else
2800 {
2801 if (log)
2802 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2803 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002804 }
2805
Greg Claytonb09c5382013-12-13 17:20:18 +00002806 launch_info.GetFlags().Set (eLaunchFlagDebug);
2807
2808 // Get the value of synchronous execution here. If you wait till after you have started to
2809 // run, then you could have hit a breakpoint, whose command might switch the value, and
2810 // then you'll pick up that incorrect value.
2811 Debugger &debugger = GetDebugger();
2812 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2813
2814 PlatformSP platform_sp (GetPlatform());
2815
2816 // Finalize the file actions, and if none were given, default to opening
2817 // up a pseudo terminal
2818 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002819 if (log)
2820 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2821 __FUNCTION__,
2822 platform_sp ? "true" : "false",
2823 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2824 default_to_use_pty ? "true" : "false");
2825
Greg Claytonb09c5382013-12-13 17:20:18 +00002826 launch_info.FinalizeFileActions (this, default_to_use_pty);
2827
2828 if (state == eStateConnected)
2829 {
2830 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2831 {
2832 error.SetErrorString("can't launch in tty when launching through a remote connection");
2833 return error;
2834 }
2835 }
2836
2837 if (!launch_info.GetArchitecture().IsValid())
2838 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002839
2840 // 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 +00002841 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2842 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002843 if (log)
2844 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2845
Greg Clayton5df78fa2015-05-23 03:54:53 +00002846 // Get a weak pointer to the previous process if we have one
2847 ProcessWP process_wp;
2848 if (m_process_sp)
2849 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00002850 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2851 debugger,
2852 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002853 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00002854
2855 // Cleanup the old process since someone might still have a strong
2856 // reference to this process and we would like to allow it to cleanup
2857 // as much as it can without the object being destroyed. We try to
2858 // lock the shared pointer and if that works, then someone else still
2859 // has a strong reference to the process.
2860
2861 ProcessSP old_process_sp(process_wp.lock());
2862 if (old_process_sp)
2863 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00002864 }
2865 else
2866 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002867 if (log)
2868 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2869
Greg Claytonb09c5382013-12-13 17:20:18 +00002870 if (state == eStateConnected)
2871 {
2872 assert(m_process_sp);
2873 }
2874 else
2875 {
Todd Fiala015d8182014-07-22 23:41:36 +00002876 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002877 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002878 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002879 }
Todd Fiala015d8182014-07-22 23:41:36 +00002880
2881 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002882 if (m_process_sp)
2883 error = m_process_sp->Launch (launch_info);
2884 }
2885
2886 if (!m_process_sp)
2887 {
2888 if (error.Success())
2889 error.SetErrorString("failed to launch or debug process");
2890 return error;
2891 }
2892
2893 if (error.Success())
2894 {
Ilia K064e69f2015-03-23 21:16:25 +00002895 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00002896 {
Greg Clayton44d93782014-01-27 23:43:24 +00002897 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00002898 if (!hijack_listener_sp)
2899 {
2900 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
2901 launch_info.SetHijackListener(hijack_listener_sp);
2902 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
2903 }
Todd Fialaac33cc92014-10-09 01:02:08 +00002904
Ilia K38810f42015-05-20 10:15:47 +00002905 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002906
2907 if (state == eStateStopped)
2908 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002909 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00002910 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002911 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00002912 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002913 error = m_process_sp->PrivateResume();
2914 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00002915 {
Ilia K064e69f2015-03-23 21:16:25 +00002916 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
2917 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2918 if (!StateIsStoppedState(state, must_be_alive))
2919 {
2920 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
2921 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002922 }
2923 }
Ilia K064e69f2015-03-23 21:16:25 +00002924 else
2925 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002926 m_process_sp->RestoreProcessEvents();
2927 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00002928 }
2929 if (!error.Success())
2930 {
Ilia K064e69f2015-03-23 21:16:25 +00002931 Error error2;
2932 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
2933 error = error2;
2934 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002935 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002936 }
Greg Clayton40286e02014-04-30 20:29:09 +00002937 else if (state == eStateExited)
2938 {
Zachary Turner10687b02014-10-20 17:46:43 +00002939 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002940 const int exit_status = m_process_sp->GetExitStatus();
2941 const char *exit_desc = m_process_sp->GetExitDescription();
2942#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'."
2943 if (exit_desc && exit_desc[0])
2944 {
2945 if (with_shell)
2946 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2947 else
2948 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2949 }
2950 else
2951 {
2952 if (with_shell)
2953 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2954 else
2955 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2956 }
2957 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002958 else
2959 {
2960 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2961 }
2962 }
Greg Clayton44d93782014-01-27 23:43:24 +00002963 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002964 }
2965 else
2966 {
Greg Clayton44d93782014-01-27 23:43:24 +00002967 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002968 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002969 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002970 }
2971 return error;
2972}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002973
2974Error
2975Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
2976{
2977 auto state = eStateInvalid;
2978 auto process_sp = GetProcessSP ();
2979 if (process_sp)
2980 {
2981 state = process_sp->GetState ();
2982 if (process_sp->IsAlive () && state != eStateConnected)
2983 {
2984 if (state == eStateAttaching)
2985 return Error ("process attach is in progress");
2986 return Error ("a process is already being debugged");
2987 }
2988 }
2989
2990 ListenerSP hijack_listener_sp (new Listener ("lldb.Target.Attach.attach.hijack"));
2991 attach_info.SetHijackListener (hijack_listener_sp);
2992
2993 const ModuleSP old_exec_module_sp = GetExecutableModule ();
2994
2995 // If no process info was specified, then use the target executable
2996 // name as the process to attach to by default
2997 if (!attach_info.ProcessInfoSpecified ())
2998 {
2999 if (old_exec_module_sp)
3000 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3001
3002 if (!attach_info.ProcessInfoSpecified ())
3003 {
3004 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3005 }
3006 }
3007
3008 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
3009
3010 Error error;
3011 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3012 {
3013 SetPlatform (platform_sp);
3014 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3015 }
3016 else
3017 {
3018 if (state != eStateConnected)
3019 {
3020 const char *plugin_name = attach_info.GetProcessPluginName ();
3021 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3022 if (process_sp == nullptr)
3023 {
3024 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3025 return error;
3026 }
3027 }
3028 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
3029 error = process_sp->Attach (attach_info);
3030 }
3031
3032 if (error.Success () && process_sp)
3033 {
3034 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
3035 process_sp->RestoreProcessEvents ();
3036
3037 if (state != eStateStopped)
3038 {
3039 const char *exit_desc = process_sp->GetExitDescription ();
3040 if (exit_desc)
3041 error.SetErrorStringWithFormat ("attach failed: %s", exit_desc);
3042 else
3043 error.SetErrorString ("attach failed: process did not stop (no such process or permission problem?)");
Jason Molendaede31932015-04-17 05:01:58 +00003044 process_sp->Destroy (false);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003045 }
3046 }
3047 return error;
3048}
3049
Jim Ingham9575d842011-03-11 03:53:59 +00003050//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003051// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003052//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003053Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3054 UserID (uid),
3055 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003056 m_commands (),
3057 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003058 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003059 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003060{
3061}
3062
3063Target::StopHook::StopHook (const StopHook &rhs) :
3064 UserID (rhs.GetID()),
3065 m_target_sp (rhs.m_target_sp),
3066 m_commands (rhs.m_commands),
3067 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003068 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003069 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003070{
3071 if (rhs.m_thread_spec_ap.get() != NULL)
3072 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3073}
3074
3075
3076Target::StopHook::~StopHook ()
3077{
3078}
3079
3080void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003081Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3082{
3083 m_specifier_sp.reset(specifier);
3084}
3085
3086void
Jim Ingham9575d842011-03-11 03:53:59 +00003087Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3088{
3089 m_thread_spec_ap.reset (specifier);
3090}
3091
3092
3093void
3094Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3095{
3096 int indent_level = s->GetIndentLevel();
3097
3098 s->SetIndentLevel(indent_level + 2);
3099
Daniel Malead01b2952012-11-29 21:49:15 +00003100 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003101 if (m_active)
3102 s->Indent ("State: enabled\n");
3103 else
3104 s->Indent ("State: disabled\n");
3105
3106 if (m_specifier_sp)
3107 {
3108 s->Indent();
3109 s->PutCString ("Specifier:\n");
3110 s->SetIndentLevel (indent_level + 4);
3111 m_specifier_sp->GetDescription (s, level);
3112 s->SetIndentLevel (indent_level + 2);
3113 }
3114
3115 if (m_thread_spec_ap.get() != NULL)
3116 {
3117 StreamString tmp;
3118 s->Indent("Thread:\n");
3119 m_thread_spec_ap->GetDescription (&tmp, level);
3120 s->SetIndentLevel (indent_level + 4);
3121 s->Indent (tmp.GetData());
3122 s->PutCString ("\n");
3123 s->SetIndentLevel (indent_level + 2);
3124 }
3125
3126 s->Indent ("Commands: \n");
3127 s->SetIndentLevel (indent_level + 4);
3128 uint32_t num_commands = m_commands.GetSize();
3129 for (uint32_t i = 0; i < num_commands; i++)
3130 {
3131 s->Indent(m_commands.GetStringAtIndex(i));
3132 s->PutCString ("\n");
3133 }
3134 s->SetIndentLevel (indent_level);
3135}
3136
Greg Clayton67cc0632012-08-22 17:17:09 +00003137//--------------------------------------------------------------
3138// class TargetProperties
3139//--------------------------------------------------------------
3140
3141OptionEnumValueElement
3142lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003143{
Greg Clayton67cc0632012-08-22 17:17:09 +00003144 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3145 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3146 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
3147 { 0, NULL, NULL }
3148};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003149
Greg Clayton1f746072012-08-29 21:13:06 +00003150static OptionEnumValueElement
3151g_inline_breakpoint_enums[] =
3152{
3153 { 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."},
3154 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3155 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
3156 { 0, NULL, NULL }
3157};
3158
Jim Ingham0f063ba2013-03-02 00:26:47 +00003159typedef enum x86DisassemblyFlavor
3160{
3161 eX86DisFlavorDefault,
3162 eX86DisFlavorIntel,
3163 eX86DisFlavorATT
3164} x86DisassemblyFlavor;
3165
3166static OptionEnumValueElement
3167g_x86_dis_flavor_value_types[] =
3168{
3169 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3170 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3171 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
3172 { 0, NULL, NULL }
3173};
3174
Enrico Granata397ddd52013-05-21 20:13:34 +00003175static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003176g_hex_immediate_style_values[] =
3177{
3178 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3179 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
3180 { 0, NULL, NULL }
3181};
3182
3183static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003184g_load_script_from_sym_file_values[] =
3185{
3186 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3187 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3188 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
3189 { 0, NULL, NULL }
3190};
3191
Greg Claytonfd814c52013-08-13 01:42:25 +00003192
3193static OptionEnumValueElement
3194g_memory_module_load_level_values[] =
3195{
Greg Clayton86eac942013-08-13 21:32:34 +00003196 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003197 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3198 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
3199 { 0, NULL, NULL }
3200};
3201
Greg Clayton67cc0632012-08-22 17:17:09 +00003202static PropertyDefinition
3203g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003204{
Greg Clayton67cc0632012-08-22 17:17:09 +00003205 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
Ilia K055ad9b2015-05-18 13:41:01 +00003206 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Move breakpoints to nearest code." },
Dawn Perchike0c96ac2015-07-30 22:10:08 +00003207 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , NULL, NULL, "The language to use when interpreting expressions entered in commands." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003208 { "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 +00003209 { "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 +00003210 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
3211 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
Jim Ingham0ca778e2015-09-15 18:03:00 +00003212 { "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 +00003213 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3214 "some part (starting at the root) of the path to the file when it was built, "
3215 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3216 "Each element of the array is checked in order and the first one that results in a match wins." },
3217 { "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 +00003218 { "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 +00003219 { "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 +00003220 { "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 +00003221 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
3222 { "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 +00003223 { "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 +00003224 { "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 +00003225 { "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." },
3226 { "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 +00003227 { "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." },
3228 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
3229 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
3230 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
3231 { "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 +00003232 { "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 +00003233 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
3234 { "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 +00003235 { "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 +00003236 "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 +00003237 "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 +00003238 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003239 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3240 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3241 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003242 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003243 // 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.
3244 { "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 +00003245 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
3246 { "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 +00003247 { "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 +00003248 { "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 +00003249 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
3250 "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. "
3251 "This setting helps users control how much information gets loaded when loading modules from memory."
3252 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3253 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3254 "'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 +00003255 { "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 +00003256 { "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 +00003257 { "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 +00003258 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, NULL, NULL, "Disable lock-step debugging, instead control threads independently." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003259 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
3260};
Enrico Granata560558e2015-02-11 02:35:39 +00003261
Greg Clayton67cc0632012-08-22 17:17:09 +00003262enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003263{
Greg Clayton67cc0632012-08-22 17:17:09 +00003264 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003265 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003266 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003267 ePropertyExprPrefix,
3268 ePropertyPreferDynamic,
3269 ePropertyEnableSynthetic,
3270 ePropertySkipPrologue,
3271 ePropertySourceMap,
3272 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003273 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003274 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003275 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003276 ePropertyMaxChildrenCount,
3277 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003278 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003279 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003280 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003281 ePropertyRunArgs,
3282 ePropertyEnvVars,
3283 ePropertyInheritEnv,
3284 ePropertyInputPath,
3285 ePropertyOutputPath,
3286 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003287 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003288 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003289 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003290 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003291 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003292 ePropertyUseHexImmediates,
3293 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003294 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003295 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003296 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003297 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003298 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003299 ePropertyDisplayRuntimeSupportValues,
3300 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003301};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003302
Caroline Ticedaccaa92010-09-20 20:44:43 +00003303
Greg Clayton67cc0632012-08-22 17:17:09 +00003304class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003305{
Greg Clayton67cc0632012-08-22 17:17:09 +00003306public:
3307 TargetOptionValueProperties (const ConstString &name) :
3308 OptionValueProperties (name),
3309 m_target (NULL),
3310 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003311 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003312 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003313
Greg Clayton67cc0632012-08-22 17:17:09 +00003314 // This constructor is used when creating TargetOptionValueProperties when it
3315 // is part of a new lldb_private::Target instance. It will copy all current
3316 // global property values as needed
3317 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3318 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3319 m_target (target),
3320 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003321 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003322 }
3323
3324 virtual const Property *
3325 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3326 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003327 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003328 // try and grab the setting from the current target if there is one. Else we just
3329 // use the one from this instance.
3330 if (idx == ePropertyEnvVars)
3331 GetHostEnvironmentIfNeeded ();
3332
3333 if (exe_ctx)
3334 {
3335 Target *target = exe_ctx->GetTargetPtr();
3336 if (target)
3337 {
3338 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3339 if (this != target_properties)
3340 return target_properties->ProtectedGetPropertyAtIndex (idx);
3341 }
3342 }
3343 return ProtectedGetPropertyAtIndex (idx);
3344 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003345
3346 lldb::TargetSP
3347 GetTargetSP ()
3348 {
3349 return m_target->shared_from_this();
3350 }
3351
Greg Clayton67cc0632012-08-22 17:17:09 +00003352protected:
3353
3354 void
3355 GetHostEnvironmentIfNeeded () const
3356 {
3357 if (!m_got_host_env)
3358 {
3359 if (m_target)
3360 {
3361 m_got_host_env = true;
3362 const uint32_t idx = ePropertyInheritEnv;
3363 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3364 {
3365 PlatformSP platform_sp (m_target->GetPlatform());
3366 if (platform_sp)
3367 {
3368 StringList env;
3369 if (platform_sp->GetEnvironment(env))
3370 {
3371 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3372 if (env_dict)
3373 {
3374 const bool can_replace = false;
3375 const size_t envc = env.GetSize();
3376 for (size_t idx=0; idx<envc; idx++)
3377 {
3378 const char *env_entry = env.GetStringAtIndex (idx);
3379 if (env_entry)
3380 {
3381 const char *equal_pos = ::strchr(env_entry, '=');
3382 ConstString key;
3383 // It is ok to have environment variables with no values
3384 const char *value = NULL;
3385 if (equal_pos)
3386 {
3387 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3388 if (equal_pos[1])
3389 value = equal_pos + 1;
3390 }
3391 else
3392 {
3393 key.SetCString(env_entry);
3394 }
3395 // Don't allow existing keys to be replaced with ones we get from the platform environment
3396 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3397 }
3398 }
3399 }
3400 }
3401 }
3402 }
3403 }
3404 }
3405 }
3406 Target *m_target;
3407 mutable bool m_got_host_env;
3408};
3409
Greg Claytonfbb76342013-11-20 21:07:01 +00003410//----------------------------------------------------------------------
3411// TargetProperties
3412//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003413TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003414 Properties (),
3415 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003416{
3417 if (target)
3418 {
3419 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003420
3421 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3422 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3423 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3424 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3425 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3426 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3427 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3428 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3429 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3430 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3431
3432 // Update m_launch_info once it was created
3433 Arg0ValueChangedCallback(this, NULL);
3434 RunArgsValueChangedCallback(this, NULL);
3435 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3436 InputPathValueChangedCallback(this, NULL);
3437 OutputPathValueChangedCallback(this, NULL);
3438 ErrorPathValueChangedCallback(this, NULL);
3439 DetachOnErrorValueChangedCallback(this, NULL);
3440 DisableASLRValueChangedCallback(this, NULL);
3441 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003442 }
3443 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003444 {
3445 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3446 m_collection_sp->Initialize(g_properties);
3447 m_collection_sp->AppendProperty(ConstString("process"),
3448 ConstString("Settings specify to processes."),
3449 true,
3450 Process::GetGlobalProperties()->GetValueProperties());
3451 }
Ilia K8f37ca52015-02-13 14:31:06 +00003452
Caroline Ticedaccaa92010-09-20 20:44:43 +00003453}
3454
Greg Clayton67cc0632012-08-22 17:17:09 +00003455TargetProperties::~TargetProperties ()
3456{
3457}
3458ArchSpec
3459TargetProperties::GetDefaultArchitecture () const
3460{
3461 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3462 if (value)
3463 return value->GetCurrentValue();
3464 return ArchSpec();
3465}
3466
3467void
3468TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3469{
3470 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3471 if (value)
3472 return value->SetCurrentValue(arch, true);
3473}
3474
Ilia K055ad9b2015-05-18 13:41:01 +00003475bool
3476TargetProperties::GetMoveToNearestCode() const
3477{
3478 const uint32_t idx = ePropertyMoveToNearestCode;
3479 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3480}
3481
Greg Clayton67cc0632012-08-22 17:17:09 +00003482lldb::DynamicValueType
3483TargetProperties::GetPreferDynamicValue() const
3484{
3485 const uint32_t idx = ePropertyPreferDynamic;
3486 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3487}
3488
3489bool
Greg Clayton15484402015-05-15 18:40:24 +00003490TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3491{
3492 const uint32_t idx = ePropertyPreferDynamic;
3493 return m_collection_sp->SetPropertyAtIndexAsEnumeration(NULL, idx, d);
3494}
3495
3496
3497bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003498TargetProperties::GetDisableASLR () const
3499{
3500 const uint32_t idx = ePropertyDisableASLR;
3501 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3502}
3503
3504void
3505TargetProperties::SetDisableASLR (bool b)
3506{
3507 const uint32_t idx = ePropertyDisableASLR;
3508 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3509}
3510
3511bool
Jim Ingham106d0282014-06-25 02:32:56 +00003512TargetProperties::GetDetachOnError () const
3513{
3514 const uint32_t idx = ePropertyDetachOnError;
3515 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3516}
3517
3518void
3519TargetProperties::SetDetachOnError (bool b)
3520{
3521 const uint32_t idx = ePropertyDetachOnError;
3522 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3523}
3524
3525bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003526TargetProperties::GetDisableSTDIO () const
3527{
3528 const uint32_t idx = ePropertyDisableSTDIO;
3529 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3530}
3531
3532void
3533TargetProperties::SetDisableSTDIO (bool b)
3534{
3535 const uint32_t idx = ePropertyDisableSTDIO;
3536 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3537}
3538
Jim Ingham0f063ba2013-03-02 00:26:47 +00003539const char *
3540TargetProperties::GetDisassemblyFlavor () const
3541{
3542 const uint32_t idx = ePropertyDisassemblyFlavor;
3543 const char *return_value;
3544
3545 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3546 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3547 return return_value;
3548}
3549
Greg Clayton1f746072012-08-29 21:13:06 +00003550InlineStrategy
3551TargetProperties::GetInlineStrategy () const
3552{
3553 const uint32_t idx = ePropertyInlineStrategy;
3554 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3555}
3556
Greg Clayton45392552012-10-17 22:57:12 +00003557const char *
3558TargetProperties::GetArg0 () const
3559{
3560 const uint32_t idx = ePropertyArg0;
3561 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3562}
3563
3564void
3565TargetProperties::SetArg0 (const char *arg)
3566{
3567 const uint32_t idx = ePropertyArg0;
3568 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003569 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003570}
3571
Greg Clayton67cc0632012-08-22 17:17:09 +00003572bool
3573TargetProperties::GetRunArguments (Args &args) const
3574{
3575 const uint32_t idx = ePropertyRunArgs;
3576 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3577}
3578
3579void
3580TargetProperties::SetRunArguments (const Args &args)
3581{
3582 const uint32_t idx = ePropertyRunArgs;
3583 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003584 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003585}
3586
3587size_t
3588TargetProperties::GetEnvironmentAsArgs (Args &env) const
3589{
3590 const uint32_t idx = ePropertyEnvVars;
3591 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3592}
3593
Ilia K8f37ca52015-02-13 14:31:06 +00003594void
3595TargetProperties::SetEnvironmentFromArgs (const Args &env)
3596{
3597 const uint32_t idx = ePropertyEnvVars;
3598 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003599 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003600}
3601
Greg Clayton67cc0632012-08-22 17:17:09 +00003602bool
3603TargetProperties::GetSkipPrologue() const
3604{
3605 const uint32_t idx = ePropertySkipPrologue;
3606 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3607}
3608
3609PathMappingList &
3610TargetProperties::GetSourcePathMap () const
3611{
3612 const uint32_t idx = ePropertySourceMap;
3613 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3614 assert(option_value);
3615 return option_value->GetCurrentValue();
3616}
3617
3618FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003619TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003620{
3621 const uint32_t idx = ePropertyExecutableSearchPaths;
3622 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3623 assert(option_value);
3624 return option_value->GetCurrentValue();
3625}
3626
Michael Sartaina7499c92013-07-01 19:45:50 +00003627FileSpecList &
3628TargetProperties::GetDebugFileSearchPaths ()
3629{
3630 const uint32_t idx = ePropertyDebugFileSearchPaths;
3631 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3632 assert(option_value);
3633 return option_value->GetCurrentValue();
3634}
3635
Sean Callanan85054342015-04-03 15:39:47 +00003636FileSpecList &
3637TargetProperties::GetClangModuleSearchPaths ()
3638{
3639 const uint32_t idx = ePropertyClangModuleSearchPaths;
3640 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3641 assert(option_value);
3642 return option_value->GetCurrentValue();
3643}
3644
Greg Clayton67cc0632012-08-22 17:17:09 +00003645bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003646TargetProperties::GetEnableAutoImportClangModules() const
3647{
3648 const uint32_t idx = ePropertyAutoImportClangModules;
3649 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3650}
3651
3652bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003653TargetProperties::GetEnableSyntheticValue () const
3654{
3655 const uint32_t idx = ePropertyEnableSynthetic;
3656 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3657}
3658
3659uint32_t
3660TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3661{
3662 const uint32_t idx = ePropertyMaxChildrenCount;
3663 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3664}
3665
3666uint32_t
3667TargetProperties::GetMaximumSizeOfStringSummary() const
3668{
3669 const uint32_t idx = ePropertyMaxSummaryLength;
3670 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3671}
3672
Enrico Granatad325bf92013-06-04 22:54:16 +00003673uint32_t
3674TargetProperties::GetMaximumMemReadSize () const
3675{
3676 const uint32_t idx = ePropertyMaxMemReadSize;
3677 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3678}
3679
Greg Clayton67cc0632012-08-22 17:17:09 +00003680FileSpec
3681TargetProperties::GetStandardInputPath () const
3682{
3683 const uint32_t idx = ePropertyInputPath;
3684 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3685}
3686
3687void
3688TargetProperties::SetStandardInputPath (const char *p)
3689{
3690 const uint32_t idx = ePropertyInputPath;
3691 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3692}
3693
3694FileSpec
3695TargetProperties::GetStandardOutputPath () const
3696{
3697 const uint32_t idx = ePropertyOutputPath;
3698 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3699}
3700
3701void
3702TargetProperties::SetStandardOutputPath (const char *p)
3703{
3704 const uint32_t idx = ePropertyOutputPath;
3705 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3706}
3707
3708FileSpec
3709TargetProperties::GetStandardErrorPath () const
3710{
3711 const uint32_t idx = ePropertyErrorPath;
3712 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3713}
3714
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003715LanguageType
3716TargetProperties::GetLanguage () const
3717{
3718 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage (NULL, ePropertyLanguage);
3719 if (value)
3720 return value->GetCurrentValue();
3721 return LanguageType();
3722}
3723
Greg Clayton6920b522012-08-22 18:39:03 +00003724const char *
3725TargetProperties::GetExpressionPrefixContentsAsCString ()
3726{
3727 const uint32_t idx = ePropertyExprPrefix;
3728 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3729 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003730 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003731 const bool null_terminate = true;
3732 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003733 if (data_sp)
3734 return (const char *) data_sp->GetBytes();
3735 }
Greg Clayton6920b522012-08-22 18:39:03 +00003736 return NULL;
3737}
3738
Greg Clayton67cc0632012-08-22 17:17:09 +00003739void
3740TargetProperties::SetStandardErrorPath (const char *p)
3741{
3742 const uint32_t idx = ePropertyErrorPath;
3743 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3744}
3745
3746bool
3747TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3748{
3749 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3750 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3751}
3752
Jim Ingham17d023f2013-03-13 17:58:04 +00003753bool
Daniel Malead79ae052013-08-07 21:54:09 +00003754TargetProperties::GetUseHexImmediates () const
3755{
3756 const uint32_t idx = ePropertyUseHexImmediates;
3757 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3758}
3759
3760bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003761TargetProperties::GetUseFastStepping () const
3762{
3763 const uint32_t idx = ePropertyUseFastStepping;
3764 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3765}
3766
Greg Claytonfb6621e2013-12-06 21:59:52 +00003767bool
3768TargetProperties::GetDisplayExpressionsInCrashlogs () const
3769{
3770 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3771 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3772}
3773
Enrico Granata397ddd52013-05-21 20:13:34 +00003774LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003775TargetProperties::GetLoadScriptFromSymbolFile () const
3776{
3777 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003778 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003779}
3780
Daniel Malead79ae052013-08-07 21:54:09 +00003781Disassembler::HexImmediateStyle
3782TargetProperties::GetHexImmediateStyle () const
3783{
3784 const uint32_t idx = ePropertyHexImmediateStyle;
3785 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3786}
3787
Greg Claytonfd814c52013-08-13 01:42:25 +00003788MemoryModuleLoadLevel
3789TargetProperties::GetMemoryModuleLoadLevel() const
3790{
3791 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3792 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3793}
3794
Jason Molendaa4bea722014-02-14 05:06:49 +00003795bool
3796TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3797{
3798 const uint32_t idx = ePropertyTrapHandlerNames;
3799 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3800}
Greg Claytonfd814c52013-08-13 01:42:25 +00003801
Jason Molendaa4bea722014-02-14 05:06:49 +00003802void
3803TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3804{
3805 const uint32_t idx = ePropertyTrapHandlerNames;
3806 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3807}
Greg Clayton67cc0632012-08-22 17:17:09 +00003808
Enrico Granata560558e2015-02-11 02:35:39 +00003809bool
3810TargetProperties::GetDisplayRuntimeSupportValues () const
3811{
3812 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3813 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3814}
3815
3816void
3817TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3818{
3819 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3820 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3821}
3822
Ewan Crawford78baa192015-05-13 09:18:18 +00003823bool
3824TargetProperties::GetNonStopModeEnabled () const
3825{
3826 const uint32_t idx = ePropertyNonStopModeEnabled;
3827 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3828}
3829
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00003830void
3831TargetProperties::SetNonStopModeEnabled (bool b)
3832{
3833 const uint32_t idx = ePropertyNonStopModeEnabled;
3834 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3835}
3836
Ilia K8f37ca52015-02-13 14:31:06 +00003837const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003838TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003839{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003840 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003841 return m_launch_info;
3842}
3843
3844void
3845TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3846{
3847 m_launch_info = launch_info;
3848 SetArg0(launch_info.GetArg0());
3849 SetRunArguments(launch_info.GetArguments());
3850 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3851 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3852 if (input_file_action)
3853 {
3854 const char *input_path = input_file_action->GetPath();
3855 if (input_path)
3856 SetStandardInputPath(input_path);
3857 }
3858 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3859 if (output_file_action)
3860 {
3861 const char *output_path = output_file_action->GetPath();
3862 if (output_path)
3863 SetStandardOutputPath(output_path);
3864 }
3865 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3866 if (error_file_action)
3867 {
3868 const char *error_path = error_file_action->GetPath();
3869 if (error_path)
3870 SetStandardErrorPath(error_path);
3871 }
3872 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3873 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3874 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3875}
3876
3877void
3878TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3879{
3880 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3881 this_->m_launch_info.SetArg0(this_->GetArg0());
3882}
3883
3884void
3885TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3886{
3887 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3888 Args args;
3889 if (this_->GetRunArguments(args))
3890 this_->m_launch_info.GetArguments() = args;
3891}
3892
3893void
3894TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3895{
3896 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3897 Args args;
3898 if (this_->GetEnvironmentAsArgs(args))
3899 this_->m_launch_info.GetEnvironmentEntries() = args;
3900}
3901
3902void
3903TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3904{
3905 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003906 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00003907}
3908
3909void
3910TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3911{
3912 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003913 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00003914}
3915
3916void
3917TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3918{
3919 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00003920 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00003921}
3922
3923void
3924TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
3925{
3926 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3927 if (this_->GetDetachOnError())
3928 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3929 else
3930 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3931}
3932
3933void
3934TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
3935{
3936 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3937 if (this_->GetDisableASLR())
3938 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3939 else
3940 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3941}
3942
3943void
3944TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
3945{
3946 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3947 if (this_->GetDisableSTDIO())
3948 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
3949 else
3950 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
3951}
Ilia Keb2c19a2015-03-10 21:59:55 +00003952
3953//----------------------------------------------------------------------
3954// Target::TargetEventData
3955//----------------------------------------------------------------------
3956
3957Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
3958 EventData (),
3959 m_target_sp (target_sp),
3960 m_module_list ()
3961{
3962}
3963
3964Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
3965 EventData (),
3966 m_target_sp (target_sp),
3967 m_module_list (module_list)
3968{
3969}
3970
3971Target::TargetEventData::~TargetEventData()
3972{
3973}
3974
3975const ConstString &
3976Target::TargetEventData::GetFlavorString ()
3977{
3978 static ConstString g_flavor ("Target::TargetEventData");
3979 return g_flavor;
3980}
3981
3982void
3983Target::TargetEventData::Dump (Stream *s) const
3984{
3985}
3986
3987const Target::TargetEventData *
3988Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3989{
3990 if (event_ptr)
3991 {
3992 const EventData *event_data = event_ptr->GetData();
3993 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3994 return static_cast <const TargetEventData *> (event_ptr->GetData());
3995 }
3996 return NULL;
3997}
3998
3999TargetSP
4000Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4001{
4002 TargetSP target_sp;
4003 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4004 if (event_data)
4005 target_sp = event_data->m_target_sp;
4006 return target_sp;
4007}
4008
4009ModuleList
4010Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4011{
4012 ModuleList module_list;
4013 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4014 if (event_data)
4015 module_list = event_data->m_module_list;
4016 return module_list;
4017}