blob: 91779ad0d9100207994a2c350429685f6481ca08 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000030#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000031#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000032#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000034#include "lldb/Core/Timer.h"
35#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000036#include "lldb/Expression/ClangASTSource.h"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000037#include "lldb/Expression/ClangPersistentVariables.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000038#include "lldb/Expression/ClangUserExpression.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000039#include "lldb/Expression/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000040#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000042#include "lldb/Interpreter/CommandInterpreter.h"
43#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000044#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000045#include "lldb/Interpreter/OptionValues.h"
46#include "lldb/Interpreter/Property.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/lldb-private-log.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000048#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049#include "lldb/Symbol/ObjectFile.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
Zachary Turner32abc6e2015-03-03 19:23:09 +000062namespace {
63// This event data class is for use by the TargetList to broadcast new target notifications.
64class TargetEventData : public EventData
65{
66public:
67 TargetEventData(const lldb::TargetSP &new_target_sp)
68 : EventData()
69 , m_target_sp(new_target_sp)
70 {
71 }
72
73 virtual ~TargetEventData()
74 {
75 }
76
77 static const ConstString &
78 GetFlavorString()
79 {
80 static ConstString g_flavor("Target::TargetEventData");
81 return g_flavor;
82 }
83
84 virtual const ConstString &
85 GetFlavor() const
86 {
87 return GetFlavorString();
88 }
89
90 lldb::TargetSP &
91 GetTarget()
92 {
93 return m_target_sp;
94 }
95
96 virtual void
97 Dump(Stream *s) const
98 {
99 }
100
101 static const lldb::TargetSP
102 GetTargetFromEvent(const lldb::EventSP &event_sp)
103 {
104 TargetSP target_sp;
105
106 const TargetEventData *data = GetEventDataFromEvent (event_sp.get());
107 if (data)
108 target_sp = data->m_target_sp;
109
110 return target_sp;
111 }
112
113 static const TargetEventData *
114 GetEventDataFromEvent(const Event *event_ptr)
115 {
116 if (event_ptr)
117 {
118 const EventData *event_data = event_ptr->GetData();
119 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
120 return static_cast <const TargetEventData *> (event_ptr->GetData());
121 }
122 return nullptr;
123 }
124
125private:
126 lldb::TargetSP m_target_sp;
127
128 DISALLOW_COPY_AND_ASSIGN (TargetEventData);
129};
130}
131
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000132ConstString &
133Target::GetStaticBroadcasterClass ()
134{
135 static ConstString class_name ("lldb.target");
136 return class_name;
137}
138
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139//----------------------------------------------------------------------
140// Target constructor
141//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +0000142Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +0000143 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +0000144 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +0000145 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +0000146 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +0000147 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +0000148 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +0000149 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +0000150 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +0000151 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152 m_breakpoint_list (false),
153 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +0000154 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +0000155 m_process_sp (),
156 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +0000158 m_scratch_ast_context_ap (),
159 m_scratch_ast_source_ap (),
160 m_ast_importer_ap (),
Zachary Turner32abc6e2015-03-03 19:23:09 +0000161 m_persistent_variables (new ClangPersistentVariables),
Greg Clayton9585fbf2013-03-19 00:20:55 +0000162 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +0000163 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +0000164 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +0000165 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +0000166 m_suppress_stop_hooks (false),
167 m_is_dummy_target(is_dummy_target)
168
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169{
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000170 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
171 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
172 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +0000173 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +0000174 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000175
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000176 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000177
Greg Clayton5160ce52013-03-27 23:08:40 +0000178 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000180 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000181 if (m_arch.IsValid())
182 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000183 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 +0000184 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185}
186
Jim Ingham893c9322014-11-22 01:42:44 +0000187void
188Target::PrimeFromDummyTarget(Target *target)
189{
190 if (!target)
191 return;
192
193 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000194
195 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
196 {
197 if (breakpoint_sp->IsInternal())
198 continue;
199
200 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
201 AddBreakpoint (new_bp, false);
202 }
Jim Ingham893c9322014-11-22 01:42:44 +0000203}
204
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205//----------------------------------------------------------------------
206// Destructor
207//----------------------------------------------------------------------
208Target::~Target()
209{
Greg Clayton5160ce52013-03-27 23:08:40 +0000210 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000212 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213 DeleteCurrentProcess ();
214}
215
216void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000217Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218{
Greg Clayton89411422010-10-08 00:21:05 +0000219// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000220 if (description_level != lldb::eDescriptionLevelBrief)
221 {
222 s->Indent();
223 s->PutCString("Target\n");
224 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000225 m_images.Dump(s);
226 m_breakpoint_list.Dump(s);
227 m_internal_breakpoint_list.Dump(s);
228 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000229 }
230 else
231 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000232 Module *exe_module = GetExecutableModulePointer();
233 if (exe_module)
234 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000235 else
236 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000237 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238}
239
240void
Greg Clayton90ba8112012-12-05 00:16:59 +0000241Target::CleanupProcess ()
242{
243 // Do any cleanup of the target we need to do between process instances.
244 // NB It is better to do this before destroying the process in case the
245 // clean up needs some help from the process.
246 m_breakpoint_list.ClearAllBreakpointSites();
247 m_internal_breakpoint_list.ClearAllBreakpointSites();
248 // Disable watchpoints just on the debugger side.
249 Mutex::Locker locker;
250 this->GetWatchpointList().GetListMutex(locker);
251 DisableAllWatchpoints(false);
252 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000253 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000254}
255
256void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000257Target::DeleteCurrentProcess ()
258{
259 if (m_process_sp.get())
260 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000261 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262 if (m_process_sp->IsAlive())
263 m_process_sp->Destroy();
Jim Inghamd0a3e122011-02-16 17:54:55 +0000264
265 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266
Greg Clayton90ba8112012-12-05 00:16:59 +0000267 CleanupProcess ();
268
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 m_process_sp.reset();
270 }
271}
272
273const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000274Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275{
276 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000277 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 return m_process_sp;
279}
280
281const lldb::ProcessSP &
282Target::GetProcessSP () const
283{
284 return m_process_sp;
285}
286
Greg Clayton3418c852011-08-10 02:10:13 +0000287void
288Target::Destroy()
289{
290 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000291 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000292 DeleteCurrentProcess ();
293 m_platform_sp.reset();
294 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000295 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000296 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000297 const bool notify = false;
298 m_breakpoint_list.RemoveAll(notify);
299 m_internal_breakpoint_list.RemoveAll(notify);
300 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000301 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000302 m_search_filter_sp.reset();
303 m_image_search_paths.Clear(notify);
Zachary Turner32abc6e2015-03-03 19:23:09 +0000304 m_persistent_variables->Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000305 m_stop_hooks.clear();
306 m_stop_hook_next_id = 0;
307 m_suppress_stop_hooks = false;
308}
309
310
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311BreakpointList &
312Target::GetBreakpointList(bool internal)
313{
314 if (internal)
315 return m_internal_breakpoint_list;
316 else
317 return m_breakpoint_list;
318}
319
320const BreakpointList &
321Target::GetBreakpointList(bool internal) const
322{
323 if (internal)
324 return m_internal_breakpoint_list;
325 else
326 return m_breakpoint_list;
327}
328
329BreakpointSP
330Target::GetBreakpointByID (break_id_t break_id)
331{
332 BreakpointSP bp_sp;
333
334 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
335 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
336 else
337 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
338
339 return bp_sp;
340}
341
342BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000343Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000344 const FileSpecList *source_file_spec_list,
345 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000346 bool internal,
347 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348{
Jim Ingham87df91b2011-09-23 00:54:11 +0000349 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
350 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000351 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000352}
353
354
355BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000356Target::CreateBreakpoint (const FileSpecList *containingModules,
357 const FileSpec &file,
358 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000359 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000360 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000361 bool internal,
362 bool hardware)
Jim Ingham969795f2011-09-21 01:17:13 +0000363{
Greg Clayton1f746072012-08-29 21:13:06 +0000364 if (check_inlines == eLazyBoolCalculate)
365 {
366 const InlineStrategy inline_strategy = GetInlineStrategy();
367 switch (inline_strategy)
368 {
369 case eInlineBreakpointsNever:
370 check_inlines = eLazyBoolNo;
371 break;
372
373 case eInlineBreakpointsHeaders:
374 if (file.IsSourceImplementationFile())
375 check_inlines = eLazyBoolNo;
376 else
377 check_inlines = eLazyBoolYes;
378 break;
379
380 case eInlineBreakpointsAlways:
381 check_inlines = eLazyBoolYes;
382 break;
383 }
384 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000385 SearchFilterSP filter_sp;
386 if (check_inlines == eLazyBoolNo)
387 {
388 // Not checking for inlines, we are looking only for matching compile units
389 FileSpecList compile_unit_list;
390 compile_unit_list.Append (file);
391 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
392 }
393 else
394 {
395 filter_sp = GetSearchFilterForModuleList (containingModules);
396 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000397 if (skip_prologue == eLazyBoolCalculate)
398 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
399
Greg Clayton1f746072012-08-29 21:13:06 +0000400 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
401 file,
402 line_no,
403 check_inlines,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000404 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000405 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406}
407
408
409BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000410Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 Address so_addr;
413 // Attempt to resolve our load address if possible, though it is ok if
414 // it doesn't resolve to section/offset.
415
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000416 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000417 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000418 if (!so_addr.IsValid())
419 {
420 // The address didn't resolve, so just set this as an absolute address
421 so_addr.SetOffset (addr);
422 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000423 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 return bp_sp;
425}
426
427BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000428Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000430 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000432 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433}
434
435BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000436Target::CreateBreakpoint (const FileSpecList *containingModules,
437 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000438 const char *func_name,
439 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000440 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000441 bool internal,
442 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000444 BreakpointSP bp_sp;
445 if (func_name)
446 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000447 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000448
449 if (skip_prologue == eLazyBoolCalculate)
450 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
451
Greg Claytond16e1e52011-07-12 17:06:17 +0000452 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
453 func_name,
454 func_name_type_mask,
455 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000456 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000457 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000458 }
459 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460}
461
Jim Inghamfab10e82012-03-06 00:37:27 +0000462lldb::BreakpointSP
463Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000464 const FileSpecList *containingSourceFiles,
465 const std::vector<std::string> &func_names,
466 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000467 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000468 bool internal,
469 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000470{
471 BreakpointSP bp_sp;
472 size_t num_names = func_names.size();
473 if (num_names > 0)
474 {
475 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000476
477 if (skip_prologue == eLazyBoolCalculate)
478 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
479
480 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000481 func_names,
482 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000483 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000484 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000485 }
486 return bp_sp;
487}
488
Jim Ingham133e0fb2012-03-03 02:05:11 +0000489BreakpointSP
490Target::CreateBreakpoint (const FileSpecList *containingModules,
491 const FileSpecList *containingSourceFiles,
492 const char *func_names[],
493 size_t num_names,
494 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000495 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000496 bool internal,
497 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000498{
499 BreakpointSP bp_sp;
500 if (num_names > 0)
501 {
502 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
503
Greg Clayton03da4cc2013-04-19 21:31:16 +0000504 if (skip_prologue == eLazyBoolCalculate)
505 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
506
507 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000508 func_names,
509 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000510 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000511 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000512 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000513 }
514 return bp_sp;
515}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516
517SearchFilterSP
518Target::GetSearchFilterForModule (const FileSpec *containingModule)
519{
520 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 if (containingModule != NULL)
522 {
523 // TODO: We should look into sharing module based search filters
524 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000525 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 }
527 else
528 {
529 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000530 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 filter_sp = m_search_filter_sp;
532 }
533 return filter_sp;
534}
535
Jim Ingham969795f2011-09-21 01:17:13 +0000536SearchFilterSP
537Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
538{
539 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000540 if (containingModules && containingModules->GetSize() != 0)
541 {
542 // TODO: We should look into sharing module based search filters
543 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000544 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000545 }
546 else
547 {
548 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000549 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000550 filter_sp = m_search_filter_sp;
551 }
552 return filter_sp;
553}
554
Jim Ingham87df91b2011-09-23 00:54:11 +0000555SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000556Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
557 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000558{
559 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
560 return GetSearchFilterForModuleList(containingModules);
561
562 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000563 if (containingModules == NULL)
564 {
565 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
566 // but that will take a little reworking.
567
Greg Claytone1cd1be2012-01-29 20:56:30 +0000568 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000569 }
570 else
571 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000572 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000573 }
574 return filter_sp;
575}
576
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000578Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000579 const FileSpecList *containingSourceFiles,
580 RegularExpression &func_regex,
581 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000582 bool internal,
583 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584{
Jim Ingham87df91b2011-09-23 00:54:11 +0000585 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000586 bool skip =
587 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
588 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000589 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
590 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000591 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000592
Jim Ingham1460e4b2014-01-10 23:46:59 +0000593 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594}
595
Jim Ingham219ba192012-03-05 04:47:34 +0000596lldb::BreakpointSP
597Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal)
598{
599 return LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
600}
601
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000602BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000603Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604{
605 BreakpointSP bp_sp;
606 if (filter_sp && resolver_sp)
607 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000608 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000610 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000612 return bp_sp;
613}
614
615void
616Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
617{
618 if (!bp_sp)
619 return;
620 if (internal)
621 m_internal_breakpoint_list.Add (bp_sp, false);
622 else
623 m_breakpoint_list.Add (bp_sp, true);
624
625 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
626 if (log)
627 {
628 StreamString s;
629 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
630 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
631 }
632
633 bp_sp->ResolveBreakpoint();
634
635 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000636 {
637 m_last_created_breakpoint = bp_sp;
638 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639}
640
Johnny Chen86364b42011-09-20 23:28:55 +0000641bool
642Target::ProcessIsValid()
643{
644 return (m_process_sp && m_process_sp->IsAlive());
645}
646
Johnny Chenb90827e2012-06-04 23:19:54 +0000647static bool
648CheckIfWatchpointsExhausted(Target *target, Error &error)
649{
650 uint32_t num_supported_hardware_watchpoints;
651 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
652 if (rc.Success())
653 {
654 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
655 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
656 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
657 num_supported_hardware_watchpoints);
658 }
659 return false;
660}
661
Johnny Chen01a67862011-10-14 00:42:25 +0000662// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000663// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000664WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000665Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000666{
Greg Clayton5160ce52013-03-27 23:08:40 +0000667 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000668 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000669 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000670 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000671
Johnny Chen01a67862011-10-14 00:42:25 +0000672 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000673 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000674 {
675 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000676 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000677 }
Jim Inghamc6462312013-06-18 21:52:48 +0000678
Johnny Chen45e541f2011-09-14 22:20:15 +0000679 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000680 {
681 if (size == 0)
682 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
683 else
Daniel Malead01b2952012-11-29 21:49:15 +0000684 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000685 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000686 }
Jim Inghamc6462312013-06-18 21:52:48 +0000687
688 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
689 {
690 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
691 }
Johnny Chen7313a642011-09-13 01:15:36 +0000692
Johnny Chen01a67862011-10-14 00:42:25 +0000693 // Currently we only support one watchpoint per address, with total number
694 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000695
696 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000697 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 +0000698 Mutex::Locker locker;
699 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000700 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000701 if (matched_sp)
702 {
Johnny Chen0c406372011-09-14 20:23:45 +0000703 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000704 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000705 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
706 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000707 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000708 if (size == old_size && kind == old_type)
709 {
Johnny Chen01a67862011-10-14 00:42:25 +0000710 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000711 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000712 }
713 else
714 {
Johnny Chen01a67862011-10-14 00:42:25 +0000715 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000716 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
717 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000718 }
Johnny Chen3c532582011-09-13 23:29:31 +0000719 }
720
Jason Molenda727e3922012-12-05 23:07:34 +0000721 if (!wp_sp)
722 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000723 wp_sp.reset(new Watchpoint(*this, addr, size, type));
724 wp_sp->SetWatchpointType(kind, notify);
725 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000726 }
Johnny Chen0c406372011-09-14 20:23:45 +0000727
Jim Ingham1b5792e2012-12-18 02:03:49 +0000728 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000729 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000730 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
731 __FUNCTION__,
732 error.Success() ? "succeeded" : "failed",
733 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000734
Jason Molenda727e3922012-12-05 23:07:34 +0000735 if (error.Fail())
736 {
Johnny Chen41b77262012-03-26 22:00:10 +0000737 // Enabling the watchpoint on the device side failed.
738 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000739 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000740 // See if we could provide more helpful error message.
741 if (!CheckIfWatchpointsExhausted(this, error))
742 {
743 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000744 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000745 }
Johnny Chen01a67862011-10-14 00:42:25 +0000746 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000747 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000748 else
Johnny Chen01a67862011-10-14 00:42:25 +0000749 m_last_created_watchpoint = wp_sp;
750 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000751}
752
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753void
754Target::RemoveAllBreakpoints (bool internal_also)
755{
Greg Clayton5160ce52013-03-27 23:08:40 +0000756 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 if (log)
758 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
759
Greg Clayton9fed0d82010-07-23 23:33:17 +0000760 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000762 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000763
764 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765}
766
767void
768Target::DisableAllBreakpoints (bool internal_also)
769{
Greg Clayton5160ce52013-03-27 23:08:40 +0000770 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771 if (log)
772 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
773
774 m_breakpoint_list.SetEnabledAll (false);
775 if (internal_also)
776 m_internal_breakpoint_list.SetEnabledAll (false);
777}
778
779void
780Target::EnableAllBreakpoints (bool internal_also)
781{
Greg Clayton5160ce52013-03-27 23:08:40 +0000782 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783 if (log)
784 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
785
786 m_breakpoint_list.SetEnabledAll (true);
787 if (internal_also)
788 m_internal_breakpoint_list.SetEnabledAll (true);
789}
790
791bool
792Target::RemoveBreakpointByID (break_id_t break_id)
793{
Greg Clayton5160ce52013-03-27 23:08:40 +0000794 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 if (log)
796 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
797
798 if (DisableBreakpointByID (break_id))
799 {
800 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000801 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000803 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000804 if (m_last_created_breakpoint)
805 {
806 if (m_last_created_breakpoint->GetID() == break_id)
807 m_last_created_breakpoint.reset();
808 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000809 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000810 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 return true;
812 }
813 return false;
814}
815
816bool
817Target::DisableBreakpointByID (break_id_t break_id)
818{
Greg Clayton5160ce52013-03-27 23:08:40 +0000819 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820 if (log)
821 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
822
823 BreakpointSP bp_sp;
824
825 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
826 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
827 else
828 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
829 if (bp_sp)
830 {
831 bp_sp->SetEnabled (false);
832 return true;
833 }
834 return false;
835}
836
837bool
838Target::EnableBreakpointByID (break_id_t break_id)
839{
Greg Clayton5160ce52013-03-27 23:08:40 +0000840 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 if (log)
842 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
843 __FUNCTION__,
844 break_id,
845 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
846
847 BreakpointSP bp_sp;
848
849 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
850 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
851 else
852 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
853
854 if (bp_sp)
855 {
856 bp_sp->SetEnabled (true);
857 return true;
858 }
859 return false;
860}
861
Johnny Chenedf50372011-09-23 21:21:43 +0000862// The flag 'end_to_end', default to true, signifies that the operation is
863// performed end to end, for both the debugger and the debuggee.
864
Johnny Chen01a67862011-10-14 00:42:25 +0000865// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
866// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000867bool
Johnny Chen01a67862011-10-14 00:42:25 +0000868Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000869{
Greg Clayton5160ce52013-03-27 23:08:40 +0000870 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000871 if (log)
872 log->Printf ("Target::%s\n", __FUNCTION__);
873
Johnny Chenedf50372011-09-23 21:21:43 +0000874 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000875 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000876 return true;
877 }
878
879 // Otherwise, it's an end to end operation.
880
Johnny Chen86364b42011-09-20 23:28:55 +0000881 if (!ProcessIsValid())
882 return false;
883
Johnny Chen01a67862011-10-14 00:42:25 +0000884 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000885 for (size_t i = 0; i < num_watchpoints; ++i)
886 {
Johnny Chen01a67862011-10-14 00:42:25 +0000887 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
888 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000889 return false;
890
Johnny Chen01a67862011-10-14 00:42:25 +0000891 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000892 if (rc.Fail())
893 return false;
894 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000895 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000896 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000897 return true; // Success!
898}
899
Johnny Chen01a67862011-10-14 00:42:25 +0000900// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
901// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000902bool
Johnny Chen01a67862011-10-14 00:42:25 +0000903Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000904{
Greg Clayton5160ce52013-03-27 23:08:40 +0000905 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000906 if (log)
907 log->Printf ("Target::%s\n", __FUNCTION__);
908
Johnny Chenedf50372011-09-23 21:21:43 +0000909 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000910 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000911 return true;
912 }
913
914 // Otherwise, it's an end to end operation.
915
Johnny Chen86364b42011-09-20 23:28:55 +0000916 if (!ProcessIsValid())
917 return false;
918
Johnny Chen01a67862011-10-14 00:42:25 +0000919 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000920 for (size_t i = 0; i < num_watchpoints; ++i)
921 {
Johnny Chen01a67862011-10-14 00:42:25 +0000922 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
923 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000924 return false;
925
Johnny Chen01a67862011-10-14 00:42:25 +0000926 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000927 if (rc.Fail())
928 return false;
929 }
Johnny Chen86364b42011-09-20 23:28:55 +0000930 return true; // Success!
931}
932
Johnny Chen01a67862011-10-14 00:42:25 +0000933// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
934// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000935bool
Johnny Chen01a67862011-10-14 00:42:25 +0000936Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000937{
Greg Clayton5160ce52013-03-27 23:08:40 +0000938 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000939 if (log)
940 log->Printf ("Target::%s\n", __FUNCTION__);
941
Johnny Chenedf50372011-09-23 21:21:43 +0000942 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000943 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000944 return true;
945 }
946
947 // Otherwise, it's an end to end operation.
948
Johnny Chen86364b42011-09-20 23:28:55 +0000949 if (!ProcessIsValid())
950 return false;
951
Johnny Chen01a67862011-10-14 00:42:25 +0000952 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000953 for (size_t i = 0; i < num_watchpoints; ++i)
954 {
Johnny Chen01a67862011-10-14 00:42:25 +0000955 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
956 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000957 return false;
958
Johnny Chen01a67862011-10-14 00:42:25 +0000959 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000960 if (rc.Fail())
961 return false;
962 }
Johnny Chen86364b42011-09-20 23:28:55 +0000963 return true; // Success!
964}
965
Johnny Chena4d6bc92012-02-25 06:44:30 +0000966// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
967bool
968Target::ClearAllWatchpointHitCounts ()
969{
Greg Clayton5160ce52013-03-27 23:08:40 +0000970 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000971 if (log)
972 log->Printf ("Target::%s\n", __FUNCTION__);
973
974 size_t num_watchpoints = m_watchpoint_list.GetSize();
975 for (size_t i = 0; i < num_watchpoints; ++i)
976 {
977 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
978 if (!wp_sp)
979 return false;
980
981 wp_sp->ResetHitCount();
982 }
983 return true; // Success!
984}
985
Enrico Granata5e3fe042015-02-11 00:37:54 +0000986// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
987bool
988Target::ClearAllWatchpointHistoricValues ()
989{
990 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
991 if (log)
992 log->Printf ("Target::%s\n", __FUNCTION__);
993
994 size_t num_watchpoints = m_watchpoint_list.GetSize();
995 for (size_t i = 0; i < num_watchpoints; ++i)
996 {
997 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
998 if (!wp_sp)
999 return false;
1000
1001 wp_sp->ResetHistoricValues();
1002 }
1003 return true; // Success!
1004}
1005
Johnny Chen01a67862011-10-14 00:42:25 +00001006// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +00001007// during these operations.
1008bool
Johnny Chen01a67862011-10-14 00:42:25 +00001009Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001010{
Greg Clayton5160ce52013-03-27 23:08:40 +00001011 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001012 if (log)
1013 log->Printf ("Target::%s\n", __FUNCTION__);
1014
1015 if (!ProcessIsValid())
1016 return false;
1017
Johnny Chen01a67862011-10-14 00:42:25 +00001018 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +00001019 for (size_t i = 0; i < num_watchpoints; ++i)
1020 {
Johnny Chen01a67862011-10-14 00:42:25 +00001021 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1022 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001023 return false;
1024
Johnny Chen01a67862011-10-14 00:42:25 +00001025 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001026 }
1027 return true; // Success!
1028}
1029
Johnny Chen01a67862011-10-14 00:42:25 +00001030// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001031bool
Johnny Chen01a67862011-10-14 00:42:25 +00001032Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001033{
Greg Clayton5160ce52013-03-27 23:08:40 +00001034 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001035 if (log)
1036 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1037
1038 if (!ProcessIsValid())
1039 return false;
1040
Johnny Chen01a67862011-10-14 00:42:25 +00001041 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1042 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001043 {
Johnny Chen01a67862011-10-14 00:42:25 +00001044 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001045 if (rc.Success())
1046 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001047
Johnny Chenf04ee932011-09-22 18:04:58 +00001048 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001049 }
1050 return false;
1051}
1052
Johnny Chen01a67862011-10-14 00:42:25 +00001053// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001054bool
Johnny Chen01a67862011-10-14 00:42:25 +00001055Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001056{
Greg Clayton5160ce52013-03-27 23:08:40 +00001057 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001058 if (log)
1059 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1060
1061 if (!ProcessIsValid())
1062 return false;
1063
Johnny Chen01a67862011-10-14 00:42:25 +00001064 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1065 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001066 {
Johnny Chen01a67862011-10-14 00:42:25 +00001067 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001068 if (rc.Success())
1069 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001070
Johnny Chenf04ee932011-09-22 18:04:58 +00001071 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001072 }
1073 return false;
1074}
1075
Johnny Chen01a67862011-10-14 00:42:25 +00001076// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001077bool
Johnny Chen01a67862011-10-14 00:42:25 +00001078Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001079{
Greg Clayton5160ce52013-03-27 23:08:40 +00001080 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001081 if (log)
1082 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1083
Jim Inghamb0b45132013-07-02 02:09:46 +00001084 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1085 if (watch_to_remove_sp == m_last_created_watchpoint)
1086 m_last_created_watchpoint.reset();
1087
Johnny Chen01a67862011-10-14 00:42:25 +00001088 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001089 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001090 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001091 return true;
1092 }
1093 return false;
1094}
1095
Johnny Chen01a67862011-10-14 00:42:25 +00001096// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001097bool
Johnny Chen01a67862011-10-14 00:42:25 +00001098Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001099{
Greg Clayton5160ce52013-03-27 23:08:40 +00001100 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001101 if (log)
1102 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1103
1104 if (!ProcessIsValid())
1105 return false;
1106
Johnny Chen01a67862011-10-14 00:42:25 +00001107 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1108 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001109 {
Johnny Chen01a67862011-10-14 00:42:25 +00001110 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001111 return true;
1112 }
1113 return false;
1114}
1115
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116ModuleSP
1117Target::GetExecutableModule ()
1118{
Greg Claytonaa149cb2011-08-11 02:48:45 +00001119 return m_images.GetModuleAtIndex(0);
1120}
1121
1122Module*
1123Target::GetExecutableModulePointer ()
1124{
1125 return m_images.GetModulePointerAtIndex(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126}
1127
Enrico Granata17598482012-11-08 02:22:02 +00001128static void
1129LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1130{
1131 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001132 StreamString feedback_stream;
1133 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001134 {
Enrico Granata97303392013-05-21 00:00:30 +00001135 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001136 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001137 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1138 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001139 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001140 if (feedback_stream.GetSize())
1141 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1142 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001143}
1144
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145void
Greg Claytonb35db632013-11-09 00:03:31 +00001146Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147{
Greg Claytonb35db632013-11-09 00:03:31 +00001148 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001149 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150 m_images.Clear();
1151 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001152 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001153 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001154}
1155
1156void
Greg Claytonb35db632013-11-09 00:03:31 +00001157Target::DidExec ()
1158{
1159 // When a process exec's we need to know about it so we can do some cleanup.
1160 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1161 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1162}
1163
1164void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001165Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1166{
1167 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001168 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169
1170 if (executable_sp.get())
1171 {
1172 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001173 "Target::SetExecutableModule (executable = '%s')",
1174 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001175
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001176 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177
Jim Ingham5aee1622010-08-09 23:31:02 +00001178 // 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 +00001179 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001180 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001181 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001182 if (log)
1183 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1184 }
1185
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001187 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001189 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190 {
1191 executable_objfile->GetDependentModules(dependent_files);
1192 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1193 {
Greg Claytonded470d2011-03-19 01:12:21 +00001194 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1195 FileSpec platform_dependent_file_spec;
1196 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001197 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001198 else
1199 platform_dependent_file_spec = dependent_file_spec;
1200
Greg Claytonb9a01b32012-02-26 05:51:37 +00001201 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1202 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 if (image_module_sp.get())
1204 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205 ObjectFile *objfile = image_module_sp->GetObjectFile();
1206 if (objfile)
1207 objfile->GetDependentModules(dependent_files);
1208 }
1209 }
1210 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211 }
1212}
1213
1214
Jim Ingham5aee1622010-08-09 23:31:02 +00001215bool
1216Target::SetArchitecture (const ArchSpec &arch_spec)
1217{
Greg Clayton5160ce52013-03-27 23:08:40 +00001218 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001219 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001220 {
Greg Clayton70512312012-05-08 01:45:38 +00001221 // If we haven't got a valid arch spec, or the architectures are
1222 // compatible, so just update the architecture. Architectures can be
1223 // equal, yet the triple OS and vendor might change, so we need to do
1224 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001225 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001226 if (log)
1227 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 +00001228 return true;
1229 }
1230 else
1231 {
1232 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001233 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001234 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 +00001235 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001236 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001237
Greg Claytonb35db632013-11-09 00:03:31 +00001238 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001239 // Need to do something about unsetting breakpoints.
1240
1241 if (executable_sp)
1242 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001243 if (log)
1244 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 +00001245 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1246 Error error = ModuleList::GetSharedModule (module_spec,
1247 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001248 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001249 NULL,
1250 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001251
1252 if (!error.Fail() && executable_sp)
1253 {
1254 SetExecutableModule (executable_sp, true);
1255 return true;
1256 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001257 }
1258 }
Greg Clayton70512312012-05-08 01:45:38 +00001259 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001260}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262void
Enrico Granataefe637d2012-11-08 19:16:03 +00001263Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001264{
1265}
1266
1267void
Enrico Granataefe637d2012-11-08 19:16:03 +00001268Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001269{
1270 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001271 if (m_valid)
1272 {
1273 ModuleList my_module_list;
1274 my_module_list.Append(module_sp);
1275 LoadScriptingResourceForModule(module_sp, this);
1276 ModulesDidLoad (my_module_list);
1277 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001278}
1279
1280void
Enrico Granataefe637d2012-11-08 19:16:03 +00001281Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001282{
1283 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001284 if (m_valid)
1285 {
1286 ModuleList my_module_list;
1287 my_module_list.Append(module_sp);
1288 ModulesDidUnload (my_module_list, false);
1289 }
Enrico Granata17598482012-11-08 02:22:02 +00001290}
1291
1292void
Enrico Granataefe637d2012-11-08 19:16:03 +00001293Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294{
Jim Inghame716ae02011-08-03 01:00:06 +00001295 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001296 if (m_valid)
1297 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001298}
1299
1300void
1301Target::ModulesDidLoad (ModuleList &module_list)
1302{
Greg Clayton23f8c952014-03-24 23:10:19 +00001303 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001304 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001305 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001306 if (m_process_sp)
1307 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001308 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001309 }
Enrico Granata17598482012-11-08 02:22:02 +00001310 // TODO: make event data that packages up the module_list
1311 BroadcastEvent (eBroadcastBitModulesLoaded, NULL);
1312 }
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);
Jim Ingham31caf982013-06-04 23:01:35 +00001331 BroadcastEvent(eBroadcastBitSymbolsLoaded, NULL);
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);
Enrico Granata17598482012-11-08 02:22:02 +00001342 // TODO: make event data that packages up the module_list
1343 BroadcastEvent (eBroadcastBitModulesUnloaded, NULL);
1344 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001345}
1346
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001347bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001348Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001349{
Greg Clayton67cc0632012-08-22 17:17:09 +00001350 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001351 {
1352 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001353 ModuleSpec module_spec (module_file_spec);
1354 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001355
1356 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1357 // black list.
1358 if (num_modules > 0)
1359 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001360 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001361 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001362 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001363 return false;
1364 }
1365 return true;
1366 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001367 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001368 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001369}
1370
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001371bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001372Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001373{
Greg Clayton67cc0632012-08-22 17:17:09 +00001374 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001375 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001376 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001377 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001378 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001379 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001380}
1381
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001382size_t
Greg Claytondb598232011-01-07 01:57:07 +00001383Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1384{
Greg Claytone72dfb32012-02-24 01:59:29 +00001385 SectionSP section_sp (addr.GetSection());
1386 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001387 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001388 // 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 +00001389 if (section_sp->IsEncrypted())
1390 {
Greg Clayton57f06302012-05-25 17:05:55 +00001391 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001392 return 0;
1393 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001394 ModuleSP module_sp (section_sp->GetModule());
1395 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001396 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001397 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1398 if (objfile)
1399 {
1400 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1401 addr.GetOffset(),
1402 dst,
1403 dst_len);
1404 if (bytes_read > 0)
1405 return bytes_read;
1406 else
1407 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1408 }
Greg Claytondb598232011-01-07 01:57:07 +00001409 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001410 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001411 }
1412 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001413 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001414 }
1415 else
Greg Claytondb598232011-01-07 01:57:07 +00001416 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001417
Greg Claytondb598232011-01-07 01:57:07 +00001418 return 0;
1419}
1420
1421size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001422Target::ReadMemory (const Address& addr,
1423 bool prefer_file_cache,
1424 void *dst,
1425 size_t dst_len,
1426 Error &error,
1427 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001430
Enrico Granata9128ee22011-09-06 19:20:51 +00001431 // if we end up reading this from process memory, we will fill this
1432 // with the actual load address
1433 if (load_addr_ptr)
1434 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1435
Greg Claytondb598232011-01-07 01:57:07 +00001436 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001437
1438 addr_t load_addr = LLDB_INVALID_ADDRESS;
1439 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001440 Address resolved_addr;
1441 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001442 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001443 SectionLoadList &section_load_list = GetSectionLoadList();
1444 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001445 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001446 // No sections are loaded, so we must assume we are not running
1447 // yet and anything we are given is a file address.
1448 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1449 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001450 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001451 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001452 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001453 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001454 // we have manually loaded some sections with "target modules load ..."
1455 // or because we have have a live process that has sections loaded
1456 // through the dynamic loader
1457 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001458 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001459 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001460 }
Greg Clayton357132e2011-03-26 19:14:58 +00001461 if (!resolved_addr.IsValid())
1462 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001463
Greg Claytonc749eb82011-07-11 05:12:02 +00001464
Greg Claytondb598232011-01-07 01:57:07 +00001465 if (prefer_file_cache)
1466 {
1467 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1468 if (bytes_read > 0)
1469 return bytes_read;
1470 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001471
Johnny Chen86364b42011-09-20 23:28:55 +00001472 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001473 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001474 if (load_addr == LLDB_INVALID_ADDRESS)
1475 load_addr = resolved_addr.GetLoadAddress (this);
1476
Greg Claytondda4f7b2010-06-30 23:03:03 +00001477 if (load_addr == LLDB_INVALID_ADDRESS)
1478 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001479 ModuleSP addr_module_sp (resolved_addr.GetModule());
1480 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001481 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001482 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001483 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001484 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001485 else
Daniel Malead01b2952012-11-29 21:49:15 +00001486 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001487 }
1488 else
1489 {
Greg Claytondb598232011-01-07 01:57:07 +00001490 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491 if (bytes_read != dst_len)
1492 {
1493 if (error.Success())
1494 {
1495 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001496 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001497 else
Daniel Malead01b2952012-11-29 21:49:15 +00001498 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 +00001499 }
1500 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001501 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001502 {
1503 if (load_addr_ptr)
1504 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001505 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001506 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001507 // If the address is not section offset we have an address that
1508 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001509 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001510 // more we can do. If it is section offset, we might be able to
1511 // read cached memory from the object file.
1512 if (!resolved_addr.IsSectionOffset())
1513 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001516
Greg Claytonc749eb82011-07-11 05:12:02 +00001517 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001518 {
Greg Claytondb598232011-01-07 01:57:07 +00001519 // If we didn't already try and read from the object file cache, then
1520 // try it after failing to read from the process.
1521 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001522 }
1523 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001524}
1525
Greg Claytond16e1e52011-07-12 17:06:17 +00001526size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001527Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1528{
1529 char buf[256];
1530 out_str.clear();
1531 addr_t curr_addr = addr.GetLoadAddress(this);
1532 Address address(addr);
1533 while (1)
1534 {
1535 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1536 if (length == 0)
1537 break;
1538 out_str.append(buf, length);
1539 // If we got "length - 1" bytes, we didn't get the whole C string, we
1540 // need to read some more characters
1541 if (length == sizeof(buf) - 1)
1542 curr_addr += length;
1543 else
1544 break;
1545 address = Address(curr_addr);
1546 }
1547 return out_str.size();
1548}
1549
1550
1551size_t
1552Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1553{
1554 size_t total_cstr_len = 0;
1555 if (dst && dst_max_len)
1556 {
1557 result_error.Clear();
1558 // NULL out everything just to be safe
1559 memset (dst, 0, dst_max_len);
1560 Error error;
1561 addr_t curr_addr = addr.GetLoadAddress(this);
1562 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001563
1564 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1565 // think this really needs to be tied to the memory cache subsystem's
1566 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001567 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001568
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001569 size_t bytes_left = dst_max_len - 1;
1570 char *curr_dst = dst;
1571
1572 while (bytes_left > 0)
1573 {
1574 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1575 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1576 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1577
1578 if (bytes_read == 0)
1579 {
1580 result_error = error;
1581 dst[total_cstr_len] = '\0';
1582 break;
1583 }
1584 const size_t len = strlen(curr_dst);
1585
1586 total_cstr_len += len;
1587
1588 if (len < bytes_to_read)
1589 break;
1590
1591 curr_dst += bytes_read;
1592 curr_addr += bytes_read;
1593 bytes_left -= bytes_read;
1594 address = Address(curr_addr);
1595 }
1596 }
1597 else
1598 {
1599 if (dst == NULL)
1600 result_error.SetErrorString("invalid arguments");
1601 else
1602 result_error.Clear();
1603 }
1604 return total_cstr_len;
1605}
1606
1607size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001608Target::ReadScalarIntegerFromMemory (const Address& addr,
1609 bool prefer_file_cache,
1610 uint32_t byte_size,
1611 bool is_signed,
1612 Scalar &scalar,
1613 Error &error)
1614{
1615 uint64_t uval;
1616
1617 if (byte_size <= sizeof(uval))
1618 {
1619 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1620 if (bytes_read == byte_size)
1621 {
1622 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001623 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001624 if (byte_size <= 4)
1625 scalar = data.GetMaxU32 (&offset, byte_size);
1626 else
1627 scalar = data.GetMaxU64 (&offset, byte_size);
1628
1629 if (is_signed)
1630 scalar.SignExtend(byte_size * 8);
1631 return bytes_read;
1632 }
1633 }
1634 else
1635 {
1636 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1637 }
1638 return 0;
1639}
1640
1641uint64_t
1642Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1643 bool prefer_file_cache,
1644 size_t integer_byte_size,
1645 uint64_t fail_value,
1646 Error &error)
1647{
1648 Scalar scalar;
1649 if (ReadScalarIntegerFromMemory (addr,
1650 prefer_file_cache,
1651 integer_byte_size,
1652 false,
1653 scalar,
1654 error))
1655 return scalar.ULongLong(fail_value);
1656 return fail_value;
1657}
1658
1659bool
1660Target::ReadPointerFromMemory (const Address& addr,
1661 bool prefer_file_cache,
1662 Error &error,
1663 Address &pointer_addr)
1664{
1665 Scalar scalar;
1666 if (ReadScalarIntegerFromMemory (addr,
1667 prefer_file_cache,
1668 m_arch.GetAddressByteSize(),
1669 false,
1670 scalar,
1671 error))
1672 {
1673 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1674 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1675 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001676 SectionLoadList &section_load_list = GetSectionLoadList();
1677 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001678 {
1679 // No sections are loaded, so we must assume we are not running
1680 // yet and anything we are given is a file address.
1681 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1682 }
1683 else
1684 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001685 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001686 // we have manually loaded some sections with "target modules load ..."
1687 // or because we have have a live process that has sections loaded
1688 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001689 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001690 }
1691 // We weren't able to resolve the pointer value, so just return
1692 // an address with no section
1693 if (!pointer_addr.IsValid())
1694 pointer_addr.SetOffset (pointer_vm_addr);
1695 return true;
1696
1697 }
1698 }
1699 return false;
1700}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701
1702ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001703Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001705 ModuleSP module_sp;
1706
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707 Error error;
1708
Jim Ingham4a94c912012-05-17 18:38:42 +00001709 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1710 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001711
Jim Ingham4a94c912012-05-17 18:38:42 +00001712 if (module_spec.GetUUID().IsValid())
1713 module_sp = m_images.FindFirstModule(module_spec);
1714
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001715 if (!module_sp)
1716 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001717 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1718 bool did_create_module = false;
1719
1720 // If there are image search path entries, try to use them first to acquire a suitable image.
1721 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001722 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001723 ModuleSpec transformed_spec (module_spec);
1724 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1725 {
1726 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1727 error = ModuleList::GetSharedModule (transformed_spec,
1728 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001729 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001730 &old_module_sp,
1731 &did_create_module);
1732 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001733 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001734
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001735 if (!module_sp)
1736 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001737 // If we have a UUID, we can check our global shared module list in case
1738 // we already have it. If we don't have a valid UUID, then we can't since
1739 // the path in "module_spec" will be a platform path, and we will need to
1740 // let the platform find that file. For example, we could be asking for
1741 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1742 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1743 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1744 // cache.
1745 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001746 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001747 // We have a UUID, it is OK to check the global module list...
1748 error = ModuleList::GetSharedModule (module_spec,
1749 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001750 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001751 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001752 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001753 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001754
1755 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001756 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001757 // The platform is responsible for finding and caching an appropriate
1758 // module in the shared module cache.
1759 if (m_platform_sp)
1760 {
1761 FileSpec platform_file_spec;
1762 error = m_platform_sp->GetSharedModule (module_spec,
1763 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001764 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001765 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001766 &did_create_module);
1767 }
1768 else
1769 {
1770 error.SetErrorString("no platform is currently set");
1771 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001772 }
1773 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001774
Jim Ingham4a94c912012-05-17 18:38:42 +00001775 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1776 // module in the list already, and if there was, let's remove it.
1777 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001778 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001779 ObjectFile *objfile = module_sp->GetObjectFile();
1780 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001781 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001782 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001783 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001784 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1785 case ObjectFile::eTypeExecutable: /// A normal executable
1786 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1787 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1788 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1789 break;
1790 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1791 if (error_ptr)
1792 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1793 return ModuleSP();
1794 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1795 if (error_ptr)
1796 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1797 return ModuleSP();
1798 default:
1799 if (error_ptr)
1800 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1801 return ModuleSP();
1802 }
1803 // GetSharedModule is not guaranteed to find the old shared module, for instance
1804 // in the common case where you pass in the UUID, it is only going to find the one
1805 // module matching the UUID. In fact, it has no good way to know what the "old module"
1806 // relevant to this target is, since there might be many copies of a module with this file spec
1807 // in various running debug sessions, but only one of them will belong to this target.
1808 // So let's remove the UUID from the module list, and look in the target's module list.
1809 // Only do this if there is SOMETHING else in the module spec...
1810 if (!old_module_sp)
1811 {
1812 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001813 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001814 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1815 module_spec_copy.GetUUID().Clear();
1816
1817 ModuleList found_modules;
1818 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1819 if (num_found == 1)
1820 {
1821 old_module_sp = found_modules.GetModuleAtIndex(0);
1822 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001823 }
1824 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001825
1826 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1827 {
1828 m_images.ReplaceModule(old_module_sp, module_sp);
1829 Module *old_module_ptr = old_module_sp.get();
1830 old_module_sp.reset();
1831 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1832 }
1833 else
1834 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001835 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001836 else
1837 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001838 }
1839 }
1840 if (error_ptr)
1841 *error_ptr = error;
1842 return module_sp;
1843}
1844
1845
Greg Claytond9e416c2012-02-18 05:35:26 +00001846TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847Target::CalculateTarget ()
1848{
Greg Claytond9e416c2012-02-18 05:35:26 +00001849 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001850}
1851
Greg Claytond9e416c2012-02-18 05:35:26 +00001852ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853Target::CalculateProcess ()
1854{
Greg Claytond9e416c2012-02-18 05:35:26 +00001855 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001856}
1857
Greg Claytond9e416c2012-02-18 05:35:26 +00001858ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001859Target::CalculateThread ()
1860{
Greg Claytond9e416c2012-02-18 05:35:26 +00001861 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001862}
1863
Jason Molendab57e4a12013-11-04 09:33:30 +00001864StackFrameSP
1865Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866{
Jason Molendab57e4a12013-11-04 09:33:30 +00001867 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001868}
1869
1870void
Greg Clayton0603aa92010-10-04 01:05:56 +00001871Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001872{
Greg Claytonc14ee322011-09-22 04:58:26 +00001873 exe_ctx.Clear();
1874 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875}
1876
1877PathMappingList &
1878Target::GetImageSearchPathList ()
1879{
1880 return m_image_search_paths;
1881}
1882
1883void
1884Target::ImageSearchPathsChanged
1885(
1886 const PathMappingList &path_list,
1887 void *baton
1888)
1889{
1890 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001891 ModuleSP exe_module_sp (target->GetExecutableModule());
1892 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001893 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894}
1895
1896ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001897Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898{
Greg Clayton73da2442011-08-03 01:23:55 +00001899 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001900 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001901 {
Greg Clayton73da2442011-08-03 01:23:55 +00001902 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001903 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001904 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001905 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001906 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1907 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001908 return m_scratch_ast_context_ap.get();
1909}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001910
Sean Callanan686b2312011-11-16 18:20:47 +00001911ClangASTImporter *
1912Target::GetClangASTImporter()
1913{
1914 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1915
1916 if (!ast_importer)
1917 {
1918 ast_importer = new ClangASTImporter();
1919 m_ast_importer_ap.reset(ast_importer);
1920 }
1921
1922 return ast_importer;
1923}
1924
Greg Clayton99d0faf2010-11-18 23:32:35 +00001925void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001926Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001927{
Greg Clayton6920b522012-08-22 18:39:03 +00001928 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001929}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001930
Greg Clayton99d0faf2010-11-18 23:32:35 +00001931void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001932Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001933{
Greg Clayton6920b522012-08-22 18:39:03 +00001934 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001935}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001936
Greg Claytonc859e2d2012-02-13 23:10:39 +00001937FileSpecList
1938Target::GetDefaultExecutableSearchPaths ()
1939{
Greg Clayton67cc0632012-08-22 17:17:09 +00001940 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1941 if (properties_sp)
1942 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001943 return FileSpecList();
1944}
1945
Michael Sartaina7499c92013-07-01 19:45:50 +00001946FileSpecList
1947Target::GetDefaultDebugFileSearchPaths ()
1948{
1949 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1950 if (properties_sp)
1951 return properties_sp->GetDebugFileSearchPaths();
1952 return FileSpecList();
1953}
1954
Caroline Ticedaccaa92010-09-20 20:44:43 +00001955ArchSpec
1956Target::GetDefaultArchitecture ()
1957{
Greg Clayton67cc0632012-08-22 17:17:09 +00001958 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1959 if (properties_sp)
1960 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001961 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001962}
1963
1964void
Greg Clayton67cc0632012-08-22 17:17:09 +00001965Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001966{
Greg Clayton67cc0632012-08-22 17:17:09 +00001967 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1968 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001969 {
1970 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 +00001971 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001972 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001973}
1974
Greg Clayton0603aa92010-10-04 01:05:56 +00001975Target *
1976Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1977{
1978 // The target can either exist in the "process" of ExecutionContext, or in
1979 // the "target_sp" member of SymbolContext. This accessor helper function
1980 // will get the target from one of these locations.
1981
1982 Target *target = NULL;
1983 if (sc_ptr != NULL)
1984 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001985 if (target == NULL && exe_ctx_ptr)
1986 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001987 return target;
1988}
1989
Jim Ingham1624a2d2014-05-05 02:26:40 +00001990ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001991Target::EvaluateExpression
1992(
1993 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001994 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001995 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001996 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001997)
1998{
Enrico Granata97fca502012-09-18 17:43:16 +00001999 result_valobj_sp.reset();
2000
Jim Ingham8646d3c2014-05-05 02:47:44 +00002001 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002002
Greg Claytond1767f02011-12-08 02:13:16 +00002003 if (expr_cstr == NULL || expr_cstr[0] == '\0')
2004 return execution_results;
2005
Jim Ingham6026ca32011-05-12 02:06:14 +00002006 // We shouldn't run stop hooks in expressions.
2007 // Be sure to reset this if you return anywhere within this function.
2008 bool old_suppress_value = m_suppress_stop_hooks;
2009 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002010
2011 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002012
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002013 if (frame)
2014 {
2015 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002016 }
2017 else if (m_process_sp)
2018 {
2019 m_process_sp->CalculateExecutionContext(exe_ctx);
2020 }
2021 else
2022 {
2023 CalculateExecutionContext(exe_ctx);
2024 }
2025
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002026 // Make sure we aren't just trying to see the value of a persistent
2027 // variable (something like "$0")
2028 lldb::ClangExpressionVariableSP persistent_var_sp;
2029 // Only check for persistent variables the expression starts with a '$'
2030 if (expr_cstr[0] == '$')
Zachary Turner32abc6e2015-03-03 19:23:09 +00002031 persistent_var_sp = m_persistent_variables->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002032
2033 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002034 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002035 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002036 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002037 }
2038 else
2039 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002040 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002041 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002042 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002043 options,
2044 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002045 prefix,
2046 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002047 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002048 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002049
2050 m_suppress_stop_hooks = old_suppress_value;
2051
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002052 return execution_results;
2053}
2054
Zachary Turner32abc6e2015-03-03 19:23:09 +00002055ClangPersistentVariables &
2056Target::GetPersistentVariables()
2057{
2058 return *m_persistent_variables;
2059}
2060
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002061lldb::addr_t
2062Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2063{
2064 addr_t code_addr = load_addr;
2065 switch (m_arch.GetMachine())
2066 {
2067 case llvm::Triple::arm:
2068 case llvm::Triple::thumb:
2069 switch (addr_class)
2070 {
2071 case eAddressClassData:
2072 case eAddressClassDebug:
2073 return LLDB_INVALID_ADDRESS;
2074
2075 case eAddressClassUnknown:
2076 case eAddressClassInvalid:
2077 case eAddressClassCode:
2078 case eAddressClassCodeAlternateISA:
2079 case eAddressClassRuntime:
2080 // Check if bit zero it no set?
2081 if ((code_addr & 1ull) == 0)
2082 {
2083 // Bit zero isn't set, check if the address is a multiple of 2?
2084 if (code_addr & 2ull)
2085 {
2086 // The address is a multiple of 2 so it must be thumb, set bit zero
2087 code_addr |= 1ull;
2088 }
2089 else if (addr_class == eAddressClassCodeAlternateISA)
2090 {
2091 // We checked the address and the address claims to be the alternate ISA
2092 // which means thumb, so set bit zero.
2093 code_addr |= 1ull;
2094 }
2095 }
2096 break;
2097 }
2098 break;
2099
2100 default:
2101 break;
2102 }
2103 return code_addr;
2104}
2105
2106lldb::addr_t
2107Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2108{
2109 addr_t opcode_addr = load_addr;
2110 switch (m_arch.GetMachine())
2111 {
2112 case llvm::Triple::arm:
2113 case llvm::Triple::thumb:
2114 switch (addr_class)
2115 {
2116 case eAddressClassData:
2117 case eAddressClassDebug:
2118 return LLDB_INVALID_ADDRESS;
2119
2120 case eAddressClassInvalid:
2121 case eAddressClassUnknown:
2122 case eAddressClassCode:
2123 case eAddressClassCodeAlternateISA:
2124 case eAddressClassRuntime:
2125 opcode_addr &= ~(1ull);
2126 break;
2127 }
2128 break;
2129
2130 default:
2131 break;
2132 }
2133 return opcode_addr;
2134}
2135
Greg Clayton9585fbf2013-03-19 00:20:55 +00002136SourceManager &
2137Target::GetSourceManager ()
2138{
2139 if (m_source_manager_ap.get() == NULL)
2140 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2141 return *m_source_manager_ap;
2142}
2143
Sean Callanan9998acd2014-12-05 01:21:59 +00002144ClangModulesDeclVendor *
2145Target::GetClangModulesDeclVendor ()
2146{
2147 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2148
2149 {
2150 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2151
2152 if (!m_clang_modules_decl_vendor_ap)
2153 {
2154 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2155 }
2156 }
2157
2158 return m_clang_modules_decl_vendor_ap.get();
2159}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002160
Greg Clayton44d93782014-01-27 23:43:24 +00002161Target::StopHookSP
2162Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002163{
2164 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002165 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2166 m_stop_hooks[new_uid] = stop_hook_sp;
2167 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002168}
2169
2170bool
2171Target::RemoveStopHookByID (lldb::user_id_t user_id)
2172{
2173 size_t num_removed;
2174 num_removed = m_stop_hooks.erase (user_id);
2175 if (num_removed == 0)
2176 return false;
2177 else
2178 return true;
2179}
2180
2181void
2182Target::RemoveAllStopHooks ()
2183{
2184 m_stop_hooks.clear();
2185}
2186
2187Target::StopHookSP
2188Target::GetStopHookByID (lldb::user_id_t user_id)
2189{
2190 StopHookSP found_hook;
2191
2192 StopHookCollection::iterator specified_hook_iter;
2193 specified_hook_iter = m_stop_hooks.find (user_id);
2194 if (specified_hook_iter != m_stop_hooks.end())
2195 found_hook = (*specified_hook_iter).second;
2196 return found_hook;
2197}
2198
2199bool
2200Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2201{
2202 StopHookCollection::iterator specified_hook_iter;
2203 specified_hook_iter = m_stop_hooks.find (user_id);
2204 if (specified_hook_iter == m_stop_hooks.end())
2205 return false;
2206
2207 (*specified_hook_iter).second->SetIsActive (active_state);
2208 return true;
2209}
2210
2211void
2212Target::SetAllStopHooksActiveState (bool active_state)
2213{
2214 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2215 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2216 {
2217 (*pos).second->SetIsActive (active_state);
2218 }
2219}
2220
2221void
2222Target::RunStopHooks ()
2223{
Jim Ingham6026ca32011-05-12 02:06:14 +00002224 if (m_suppress_stop_hooks)
2225 return;
2226
Jim Ingham9575d842011-03-11 03:53:59 +00002227 if (!m_process_sp)
2228 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002229
2230 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2231 // since in that case we do not want to run the stop-hooks
2232 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2233 return;
2234
Jim Ingham9575d842011-03-11 03:53:59 +00002235 if (m_stop_hooks.empty())
2236 return;
2237
2238 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2239
2240 // If there aren't any active stop hooks, don't bother either:
2241 bool any_active_hooks = false;
2242 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2243 {
2244 if ((*pos).second->IsActive())
2245 {
2246 any_active_hooks = true;
2247 break;
2248 }
2249 }
2250 if (!any_active_hooks)
2251 return;
2252
2253 CommandReturnObject result;
2254
2255 std::vector<ExecutionContext> exc_ctx_with_reasons;
2256 std::vector<SymbolContext> sym_ctx_with_reasons;
2257
2258 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2259 size_t num_threads = cur_threadlist.GetSize();
2260 for (size_t i = 0; i < num_threads; i++)
2261 {
2262 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2263 if (cur_thread_sp->ThreadStoppedForAReason())
2264 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002265 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002266 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2267 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2268 }
2269 }
2270
2271 // If no threads stopped for a reason, don't run the stop-hooks.
2272 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2273 if (num_exe_ctx == 0)
2274 return;
2275
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002276 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2277 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002278
2279 bool keep_going = true;
2280 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002281 bool print_hook_header;
2282 bool print_thread_header;
2283
2284 if (num_exe_ctx == 1)
2285 print_thread_header = false;
2286 else
2287 print_thread_header = true;
2288
2289 if (m_stop_hooks.size() == 1)
2290 print_hook_header = false;
2291 else
2292 print_hook_header = true;
2293
Jim Ingham9575d842011-03-11 03:53:59 +00002294 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2295 {
2296 // result.Clear();
2297 StopHookSP cur_hook_sp = (*pos).second;
2298 if (!cur_hook_sp->IsActive())
2299 continue;
2300
2301 bool any_thread_matched = false;
2302 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2303 {
2304 if ((cur_hook_sp->GetSpecifier () == NULL
2305 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2306 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002307 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002308 {
2309 if (!hooks_ran)
2310 {
Jim Ingham9575d842011-03-11 03:53:59 +00002311 hooks_ran = true;
2312 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002313 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002314 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002315 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2316 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2317 NULL);
2318 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002319 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002320 else
Daniel Malead01b2952012-11-29 21:49:15 +00002321 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002322 any_thread_matched = true;
2323 }
2324
Jim Ingham381e25b2011-03-22 01:47:27 +00002325 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002326 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002327
2328 CommandInterpreterRunOptions options;
2329 options.SetStopOnContinue (true);
2330 options.SetStopOnError (true);
2331 options.SetEchoCommands (false);
2332 options.SetPrintResults (true);
2333 options.SetAddToHistory (false);
2334
2335 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2336 &exc_ctx_with_reasons[i],
2337 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002338 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002339
2340 // If the command started the target going again, we should bag out of
2341 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002342 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2343 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002344 {
Daniel Malead01b2952012-11-29 21:49:15 +00002345 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002346 keep_going = false;
2347 }
2348 }
2349 }
2350 }
Jason Molenda879cf772011-09-23 00:42:55 +00002351
Caroline Tice969ed3d2011-05-02 20:41:46 +00002352 result.GetImmediateOutputStream()->Flush();
2353 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002354}
2355
Greg Claytonfbb76342013-11-20 21:07:01 +00002356const TargetPropertiesSP &
2357Target::GetGlobalProperties()
2358{
2359 static TargetPropertiesSP g_settings_sp;
2360 if (!g_settings_sp)
2361 {
2362 g_settings_sp.reset (new TargetProperties (NULL));
2363 }
2364 return g_settings_sp;
2365}
2366
2367Error
2368Target::Install (ProcessLaunchInfo *launch_info)
2369{
2370 Error error;
2371 PlatformSP platform_sp (GetPlatform());
2372 if (platform_sp)
2373 {
2374 if (platform_sp->IsRemote())
2375 {
2376 if (platform_sp->IsConnected())
2377 {
2378 // Install all files that have an install path, and always install the
2379 // main executable when connected to a remote platform
2380 const ModuleList& modules = GetImages();
2381 const size_t num_images = modules.GetSize();
2382 for (size_t idx = 0; idx < num_images; ++idx)
2383 {
2384 const bool is_main_executable = idx == 0;
2385 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2386 if (module_sp)
2387 {
2388 FileSpec local_file (module_sp->GetFileSpec());
2389 if (local_file)
2390 {
2391 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2392 if (!remote_file)
2393 {
2394 if (is_main_executable) // TODO: add setting for always installing main executable???
2395 {
2396 // Always install the main executable
2397 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
2398 remote_file.GetFilename() = module_sp->GetFileSpec().GetFilename();
2399 }
2400 }
2401 if (remote_file)
2402 {
2403 error = platform_sp->Install(local_file, remote_file);
2404 if (error.Success())
2405 {
2406 module_sp->SetPlatformFileSpec(remote_file);
2407 if (is_main_executable)
2408 {
2409 if (launch_info)
2410 launch_info->SetExecutableFile(remote_file, false);
2411 }
2412 }
2413 else
2414 break;
2415 }
2416 }
2417 }
2418 }
2419 }
2420 }
2421 }
2422 return error;
2423}
Greg Clayton7b242382011-07-08 00:48:09 +00002424
Greg Claytond5944cd2013-12-06 01:12:00 +00002425bool
2426Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2427{
2428 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2429}
2430
2431bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002432Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2433{
2434 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2435}
2436
2437bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002438Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2439{
2440 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2441 if (old_section_load_addr != new_section_load_addr)
2442 {
2443 uint32_t stop_id = 0;
2444 ProcessSP process_sp(GetProcessSP());
2445 if (process_sp)
2446 stop_id = process_sp->GetStopID();
2447 else
2448 stop_id = m_section_load_history.GetLastStopID();
2449 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2450 return true; // Return true if the section load address was changed...
2451 }
2452 return false; // Return false to indicate nothing changed
2453
2454}
2455
Greg Clayton8012cad2014-11-17 19:39:20 +00002456size_t
2457Target::UnloadModuleSections (const ModuleList &module_list)
2458{
2459 size_t section_unload_count = 0;
2460 size_t num_modules = module_list.GetSize();
2461 for (size_t i=0; i<num_modules; ++i)
2462 {
2463 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2464 }
2465 return section_unload_count;
2466}
2467
2468size_t
2469Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2470{
2471 uint32_t stop_id = 0;
2472 ProcessSP process_sp(GetProcessSP());
2473 if (process_sp)
2474 stop_id = process_sp->GetStopID();
2475 else
2476 stop_id = m_section_load_history.GetLastStopID();
2477 SectionList *sections = module_sp->GetSectionList();
2478 size_t section_unload_count = 0;
2479 if (sections)
2480 {
2481 const uint32_t num_sections = sections->GetNumSections(0);
2482 for (uint32_t i = 0; i < num_sections; ++i)
2483 {
2484 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2485 }
2486 }
2487 return section_unload_count;
2488}
2489
Greg Claytond5944cd2013-12-06 01:12:00 +00002490bool
2491Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2492{
2493 uint32_t stop_id = 0;
2494 ProcessSP process_sp(GetProcessSP());
2495 if (process_sp)
2496 stop_id = process_sp->GetStopID();
2497 else
2498 stop_id = m_section_load_history.GetLastStopID();
2499 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2500}
2501
2502bool
2503Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2504{
2505 uint32_t stop_id = 0;
2506 ProcessSP process_sp(GetProcessSP());
2507 if (process_sp)
2508 stop_id = process_sp->GetStopID();
2509 else
2510 stop_id = m_section_load_history.GetLastStopID();
2511 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2512}
2513
2514void
2515Target::ClearAllLoadedSections ()
2516{
2517 m_section_load_history.Clear();
2518}
2519
Greg Claytonb09c5382013-12-13 17:20:18 +00002520
2521Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002522Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002523{
2524 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002525 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2526
2527 if (log)
2528 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2529
Greg Claytonb09c5382013-12-13 17:20:18 +00002530 StateType state = eStateInvalid;
2531
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002532 // Scope to temporarily get the process state in case someone has manually
2533 // remotely connected already to a process and we can skip the platform
2534 // launching.
2535 {
2536 ProcessSP process_sp (GetProcessSP());
2537
2538 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002539 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002540 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002541 if (log)
2542 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2543 }
2544 else
2545 {
2546 if (log)
2547 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2548 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002549 }
2550
Greg Claytonb09c5382013-12-13 17:20:18 +00002551 launch_info.GetFlags().Set (eLaunchFlagDebug);
2552
2553 // Get the value of synchronous execution here. If you wait till after you have started to
2554 // run, then you could have hit a breakpoint, whose command might switch the value, and
2555 // then you'll pick up that incorrect value.
2556 Debugger &debugger = GetDebugger();
2557 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2558
2559 PlatformSP platform_sp (GetPlatform());
2560
2561 // Finalize the file actions, and if none were given, default to opening
2562 // up a pseudo terminal
2563 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002564 if (log)
2565 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2566 __FUNCTION__,
2567 platform_sp ? "true" : "false",
2568 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2569 default_to_use_pty ? "true" : "false");
2570
Greg Claytonb09c5382013-12-13 17:20:18 +00002571 launch_info.FinalizeFileActions (this, default_to_use_pty);
2572
2573 if (state == eStateConnected)
2574 {
2575 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2576 {
2577 error.SetErrorString("can't launch in tty when launching through a remote connection");
2578 return error;
2579 }
2580 }
2581
2582 if (!launch_info.GetArchitecture().IsValid())
2583 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002584
2585 // 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 +00002586 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2587 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002588 if (log)
2589 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2590
Greg Claytonb09c5382013-12-13 17:20:18 +00002591 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2592 debugger,
2593 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002594 error);
2595 }
2596 else
2597 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002598 if (log)
2599 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2600
Greg Claytonb09c5382013-12-13 17:20:18 +00002601 if (state == eStateConnected)
2602 {
2603 assert(m_process_sp);
2604 }
2605 else
2606 {
Todd Fiala015d8182014-07-22 23:41:36 +00002607 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002608 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002609 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002610 }
Todd Fiala015d8182014-07-22 23:41:36 +00002611
2612 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002613 if (m_process_sp)
2614 error = m_process_sp->Launch (launch_info);
2615 }
2616
2617 if (!m_process_sp)
2618 {
2619 if (error.Success())
2620 error.SetErrorString("failed to launch or debug process");
2621 return error;
2622 }
2623
2624 if (error.Success())
2625 {
2626 if (launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
2627 {
Greg Clayton44d93782014-01-27 23:43:24 +00002628 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Todd Fialaac33cc92014-10-09 01:02:08 +00002629
Greg Claytondc6224e2014-10-21 01:00:42 +00002630 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002631
2632 if (state == eStateStopped)
2633 {
Greg Clayton44d93782014-01-27 23:43:24 +00002634 if (!synchronous_execution)
2635 m_process_sp->RestoreProcessEvents ();
2636
2637 error = m_process_sp->PrivateResume();
Todd Fialaa3b89e22014-08-12 14:33:19 +00002638
Greg Claytonb09c5382013-12-13 17:20:18 +00002639 if (error.Success())
2640 {
Todd Fialaa3b89e22014-08-12 14:33:19 +00002641 // there is a race condition where this thread will return up the call stack to the main command
2642 // handler and show an (lldb) prompt before HandlePrivateEvent (from PrivateStateThread) has
2643 // a chance to call PushProcessIOHandler()
2644 m_process_sp->SyncIOHandler(2000);
2645
Greg Claytonb09c5382013-12-13 17:20:18 +00002646 if (synchronous_execution)
2647 {
Greg Claytondc6224e2014-10-21 01:00:42 +00002648 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
Greg Claytonb09c5382013-12-13 17:20:18 +00002649 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2650 if (!StateIsStoppedState(state, must_be_alive))
2651 {
Greg Clayton44d93782014-01-27 23:43:24 +00002652 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
Greg Claytonb09c5382013-12-13 17:20:18 +00002653 }
2654 }
2655 }
2656 else
2657 {
Greg Clayton44d93782014-01-27 23:43:24 +00002658 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002659 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002660 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002661 }
2662 }
Greg Clayton40286e02014-04-30 20:29:09 +00002663 else if (state == eStateExited)
2664 {
Zachary Turner10687b02014-10-20 17:46:43 +00002665 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002666 const int exit_status = m_process_sp->GetExitStatus();
2667 const char *exit_desc = m_process_sp->GetExitDescription();
2668#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'."
2669 if (exit_desc && exit_desc[0])
2670 {
2671 if (with_shell)
2672 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2673 else
2674 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2675 }
2676 else
2677 {
2678 if (with_shell)
2679 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2680 else
2681 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2682 }
2683 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002684 else
2685 {
2686 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2687 }
2688 }
Greg Clayton44d93782014-01-27 23:43:24 +00002689 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002690 }
2691 else
2692 {
Greg Clayton44d93782014-01-27 23:43:24 +00002693 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002694 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002695 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002696 }
2697 return error;
2698}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002699
2700Error
2701Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
2702{
2703 auto state = eStateInvalid;
2704 auto process_sp = GetProcessSP ();
2705 if (process_sp)
2706 {
2707 state = process_sp->GetState ();
2708 if (process_sp->IsAlive () && state != eStateConnected)
2709 {
2710 if (state == eStateAttaching)
2711 return Error ("process attach is in progress");
2712 return Error ("a process is already being debugged");
2713 }
2714 }
2715
2716 ListenerSP hijack_listener_sp (new Listener ("lldb.Target.Attach.attach.hijack"));
2717 attach_info.SetHijackListener (hijack_listener_sp);
2718
2719 const ModuleSP old_exec_module_sp = GetExecutableModule ();
2720
2721 // If no process info was specified, then use the target executable
2722 // name as the process to attach to by default
2723 if (!attach_info.ProcessInfoSpecified ())
2724 {
2725 if (old_exec_module_sp)
2726 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
2727
2728 if (!attach_info.ProcessInfoSpecified ())
2729 {
2730 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
2731 }
2732 }
2733
2734 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
2735
2736 Error error;
2737 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
2738 {
2739 SetPlatform (platform_sp);
2740 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
2741 }
2742 else
2743 {
2744 if (state != eStateConnected)
2745 {
2746 const char *plugin_name = attach_info.GetProcessPluginName ();
2747 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
2748 if (process_sp == nullptr)
2749 {
2750 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
2751 return error;
2752 }
2753 }
2754 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
2755 error = process_sp->Attach (attach_info);
2756 }
2757
2758 if (error.Success () && process_sp)
2759 {
2760 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
2761 process_sp->RestoreProcessEvents ();
2762
2763 if (state != eStateStopped)
2764 {
2765 const char *exit_desc = process_sp->GetExitDescription ();
2766 if (exit_desc)
2767 error.SetErrorStringWithFormat ("attach failed: %s", exit_desc);
2768 else
2769 error.SetErrorString ("attach failed: process did not stop (no such process or permission problem?)");
2770 process_sp->Destroy ();
2771 }
2772 }
2773 return error;
2774}
2775
Jim Ingham9575d842011-03-11 03:53:59 +00002776//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002777// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002778//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002779Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2780 UserID (uid),
2781 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002782 m_commands (),
2783 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002784 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002785 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002786{
2787}
2788
2789Target::StopHook::StopHook (const StopHook &rhs) :
2790 UserID (rhs.GetID()),
2791 m_target_sp (rhs.m_target_sp),
2792 m_commands (rhs.m_commands),
2793 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002794 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002795 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002796{
2797 if (rhs.m_thread_spec_ap.get() != NULL)
2798 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2799}
2800
2801
2802Target::StopHook::~StopHook ()
2803{
2804}
2805
2806void
Zachary Turner32abc6e2015-03-03 19:23:09 +00002807Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
2808{
2809 m_specifier_sp.reset(specifier);
2810}
2811
2812void
Jim Ingham9575d842011-03-11 03:53:59 +00002813Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2814{
2815 m_thread_spec_ap.reset (specifier);
2816}
2817
2818
2819void
2820Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2821{
2822 int indent_level = s->GetIndentLevel();
2823
2824 s->SetIndentLevel(indent_level + 2);
2825
Daniel Malead01b2952012-11-29 21:49:15 +00002826 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002827 if (m_active)
2828 s->Indent ("State: enabled\n");
2829 else
2830 s->Indent ("State: disabled\n");
2831
2832 if (m_specifier_sp)
2833 {
2834 s->Indent();
2835 s->PutCString ("Specifier:\n");
2836 s->SetIndentLevel (indent_level + 4);
2837 m_specifier_sp->GetDescription (s, level);
2838 s->SetIndentLevel (indent_level + 2);
2839 }
2840
2841 if (m_thread_spec_ap.get() != NULL)
2842 {
2843 StreamString tmp;
2844 s->Indent("Thread:\n");
2845 m_thread_spec_ap->GetDescription (&tmp, level);
2846 s->SetIndentLevel (indent_level + 4);
2847 s->Indent (tmp.GetData());
2848 s->PutCString ("\n");
2849 s->SetIndentLevel (indent_level + 2);
2850 }
2851
2852 s->Indent ("Commands: \n");
2853 s->SetIndentLevel (indent_level + 4);
2854 uint32_t num_commands = m_commands.GetSize();
2855 for (uint32_t i = 0; i < num_commands; i++)
2856 {
2857 s->Indent(m_commands.GetStringAtIndex(i));
2858 s->PutCString ("\n");
2859 }
2860 s->SetIndentLevel (indent_level);
2861}
2862
Greg Clayton67cc0632012-08-22 17:17:09 +00002863//--------------------------------------------------------------
2864// class TargetProperties
2865//--------------------------------------------------------------
2866
2867OptionEnumValueElement
2868lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002869{
Greg Clayton67cc0632012-08-22 17:17:09 +00002870 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2871 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2872 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2873 { 0, NULL, NULL }
2874};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002875
Greg Clayton1f746072012-08-29 21:13:06 +00002876static OptionEnumValueElement
2877g_inline_breakpoint_enums[] =
2878{
2879 { 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."},
2880 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2881 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2882 { 0, NULL, NULL }
2883};
2884
Jim Ingham0f063ba2013-03-02 00:26:47 +00002885typedef enum x86DisassemblyFlavor
2886{
2887 eX86DisFlavorDefault,
2888 eX86DisFlavorIntel,
2889 eX86DisFlavorATT
2890} x86DisassemblyFlavor;
2891
2892static OptionEnumValueElement
2893g_x86_dis_flavor_value_types[] =
2894{
2895 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2896 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2897 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2898 { 0, NULL, NULL }
2899};
2900
Enrico Granata397ddd52013-05-21 20:13:34 +00002901static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002902g_hex_immediate_style_values[] =
2903{
2904 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2905 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2906 { 0, NULL, NULL }
2907};
2908
2909static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002910g_load_script_from_sym_file_values[] =
2911{
2912 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2913 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2914 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2915 { 0, NULL, NULL }
2916};
2917
Greg Claytonfd814c52013-08-13 01:42:25 +00002918
2919static OptionEnumValueElement
2920g_memory_module_load_level_values[] =
2921{
Greg Clayton86eac942013-08-13 21:32:34 +00002922 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002923 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2924 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2925 { 0, NULL, NULL }
2926};
2927
Greg Clayton67cc0632012-08-22 17:17:09 +00002928static PropertyDefinition
2929g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002930{
Greg Clayton67cc0632012-08-22 17:17:09 +00002931 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
2932 { "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 +00002933 { "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 +00002934 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2935 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2936 { "source-map" , OptionValue::eTypePathMap , false, 0 , NULL, NULL, "Source path remappings used to track the change of location between a source file when built, and "
2937 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2938 "some part (starting at the root) of the path to the file when it was built, "
2939 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2940 "Each element of the array is checked in order and the first one that results in a match wins." },
2941 { "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 +00002942 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating debug symbol files." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002943 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2944 { "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 +00002945 { "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 +00002946 { "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 +00002947 { "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." },
2948 { "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 +00002949 { "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." },
2950 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2951 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2952 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2953 { "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 +00002954 { "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 +00002955 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2956 { "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 +00002957 { "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 +00002958 "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. "
2959 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2960 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002961 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2962 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2963 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002964 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002965 // 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.
2966 { "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 +00002967 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2968 { "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 +00002969 { "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 +00002970 { "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 +00002971 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2972 "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. "
2973 "This setting helps users control how much information gets loaded when loading modules from memory."
2974 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2975 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2976 "'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 +00002977 { "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 +00002978 { "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 +00002979 { "display-runtime-support-values" , OptionValue::eTypeBoolean , false, false, NULL, NULL, "If true, LLDB will show variables that are meant to support the operation of a language's runtime support." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002980 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2981};
Enrico Granata560558e2015-02-11 02:35:39 +00002982
Greg Clayton67cc0632012-08-22 17:17:09 +00002983enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002984{
Greg Clayton67cc0632012-08-22 17:17:09 +00002985 ePropertyDefaultArch,
2986 ePropertyExprPrefix,
2987 ePropertyPreferDynamic,
2988 ePropertyEnableSynthetic,
2989 ePropertySkipPrologue,
2990 ePropertySourceMap,
2991 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00002992 ePropertyDebugFileSearchPaths,
Greg Clayton67cc0632012-08-22 17:17:09 +00002993 ePropertyMaxChildrenCount,
2994 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00002995 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00002996 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00002997 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00002998 ePropertyRunArgs,
2999 ePropertyEnvVars,
3000 ePropertyInheritEnv,
3001 ePropertyInputPath,
3002 ePropertyOutputPath,
3003 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003004 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003005 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003006 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003007 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003008 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003009 ePropertyUseHexImmediates,
3010 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003011 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003012 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003013 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003014 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003015 ePropertyTrapHandlerNames,
3016 ePropertyDisplayRuntimeSupportValues
Greg Clayton67cc0632012-08-22 17:17:09 +00003017};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003018
Caroline Ticedaccaa92010-09-20 20:44:43 +00003019
Greg Clayton67cc0632012-08-22 17:17:09 +00003020class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003021{
Greg Clayton67cc0632012-08-22 17:17:09 +00003022public:
3023 TargetOptionValueProperties (const ConstString &name) :
3024 OptionValueProperties (name),
3025 m_target (NULL),
3026 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003027 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003028 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003029
Greg Clayton67cc0632012-08-22 17:17:09 +00003030 // This constructor is used when creating TargetOptionValueProperties when it
3031 // is part of a new lldb_private::Target instance. It will copy all current
3032 // global property values as needed
3033 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3034 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3035 m_target (target),
3036 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003037 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003038 }
3039
3040 virtual const Property *
3041 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3042 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003043 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003044 // try and grab the setting from the current target if there is one. Else we just
3045 // use the one from this instance.
3046 if (idx == ePropertyEnvVars)
3047 GetHostEnvironmentIfNeeded ();
3048
3049 if (exe_ctx)
3050 {
3051 Target *target = exe_ctx->GetTargetPtr();
3052 if (target)
3053 {
3054 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3055 if (this != target_properties)
3056 return target_properties->ProtectedGetPropertyAtIndex (idx);
3057 }
3058 }
3059 return ProtectedGetPropertyAtIndex (idx);
3060 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003061
3062 lldb::TargetSP
3063 GetTargetSP ()
3064 {
3065 return m_target->shared_from_this();
3066 }
3067
Greg Clayton67cc0632012-08-22 17:17:09 +00003068protected:
3069
3070 void
3071 GetHostEnvironmentIfNeeded () const
3072 {
3073 if (!m_got_host_env)
3074 {
3075 if (m_target)
3076 {
3077 m_got_host_env = true;
3078 const uint32_t idx = ePropertyInheritEnv;
3079 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3080 {
3081 PlatformSP platform_sp (m_target->GetPlatform());
3082 if (platform_sp)
3083 {
3084 StringList env;
3085 if (platform_sp->GetEnvironment(env))
3086 {
3087 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3088 if (env_dict)
3089 {
3090 const bool can_replace = false;
3091 const size_t envc = env.GetSize();
3092 for (size_t idx=0; idx<envc; idx++)
3093 {
3094 const char *env_entry = env.GetStringAtIndex (idx);
3095 if (env_entry)
3096 {
3097 const char *equal_pos = ::strchr(env_entry, '=');
3098 ConstString key;
3099 // It is ok to have environment variables with no values
3100 const char *value = NULL;
3101 if (equal_pos)
3102 {
3103 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3104 if (equal_pos[1])
3105 value = equal_pos + 1;
3106 }
3107 else
3108 {
3109 key.SetCString(env_entry);
3110 }
3111 // Don't allow existing keys to be replaced with ones we get from the platform environment
3112 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3113 }
3114 }
3115 }
3116 }
3117 }
3118 }
3119 }
3120 }
3121 }
3122 Target *m_target;
3123 mutable bool m_got_host_env;
3124};
3125
Greg Claytonfbb76342013-11-20 21:07:01 +00003126//----------------------------------------------------------------------
3127// TargetProperties
3128//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003129TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003130 Properties (),
3131 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003132{
3133 if (target)
3134 {
3135 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003136
3137 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3138 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3139 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3140 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3141 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3142 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3143 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3144 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3145 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3146 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3147
3148 // Update m_launch_info once it was created
3149 Arg0ValueChangedCallback(this, NULL);
3150 RunArgsValueChangedCallback(this, NULL);
3151 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3152 InputPathValueChangedCallback(this, NULL);
3153 OutputPathValueChangedCallback(this, NULL);
3154 ErrorPathValueChangedCallback(this, NULL);
3155 DetachOnErrorValueChangedCallback(this, NULL);
3156 DisableASLRValueChangedCallback(this, NULL);
3157 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003158 }
3159 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003160 {
3161 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3162 m_collection_sp->Initialize(g_properties);
3163 m_collection_sp->AppendProperty(ConstString("process"),
3164 ConstString("Settings specify to processes."),
3165 true,
3166 Process::GetGlobalProperties()->GetValueProperties());
3167 }
Ilia K8f37ca52015-02-13 14:31:06 +00003168
Caroline Ticedaccaa92010-09-20 20:44:43 +00003169}
3170
Greg Clayton67cc0632012-08-22 17:17:09 +00003171TargetProperties::~TargetProperties ()
3172{
3173}
3174ArchSpec
3175TargetProperties::GetDefaultArchitecture () const
3176{
3177 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3178 if (value)
3179 return value->GetCurrentValue();
3180 return ArchSpec();
3181}
3182
3183void
3184TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3185{
3186 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3187 if (value)
3188 return value->SetCurrentValue(arch, true);
3189}
3190
3191lldb::DynamicValueType
3192TargetProperties::GetPreferDynamicValue() const
3193{
3194 const uint32_t idx = ePropertyPreferDynamic;
3195 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3196}
3197
3198bool
3199TargetProperties::GetDisableASLR () const
3200{
3201 const uint32_t idx = ePropertyDisableASLR;
3202 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3203}
3204
3205void
3206TargetProperties::SetDisableASLR (bool b)
3207{
3208 const uint32_t idx = ePropertyDisableASLR;
3209 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3210}
3211
3212bool
Jim Ingham106d0282014-06-25 02:32:56 +00003213TargetProperties::GetDetachOnError () const
3214{
3215 const uint32_t idx = ePropertyDetachOnError;
3216 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3217}
3218
3219void
3220TargetProperties::SetDetachOnError (bool b)
3221{
3222 const uint32_t idx = ePropertyDetachOnError;
3223 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3224}
3225
3226bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003227TargetProperties::GetDisableSTDIO () const
3228{
3229 const uint32_t idx = ePropertyDisableSTDIO;
3230 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3231}
3232
3233void
3234TargetProperties::SetDisableSTDIO (bool b)
3235{
3236 const uint32_t idx = ePropertyDisableSTDIO;
3237 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3238}
3239
Jim Ingham0f063ba2013-03-02 00:26:47 +00003240const char *
3241TargetProperties::GetDisassemblyFlavor () const
3242{
3243 const uint32_t idx = ePropertyDisassemblyFlavor;
3244 const char *return_value;
3245
3246 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3247 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3248 return return_value;
3249}
3250
Greg Clayton1f746072012-08-29 21:13:06 +00003251InlineStrategy
3252TargetProperties::GetInlineStrategy () const
3253{
3254 const uint32_t idx = ePropertyInlineStrategy;
3255 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3256}
3257
Greg Clayton45392552012-10-17 22:57:12 +00003258const char *
3259TargetProperties::GetArg0 () const
3260{
3261 const uint32_t idx = ePropertyArg0;
3262 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3263}
3264
3265void
3266TargetProperties::SetArg0 (const char *arg)
3267{
3268 const uint32_t idx = ePropertyArg0;
3269 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
3270}
3271
Greg Clayton67cc0632012-08-22 17:17:09 +00003272bool
3273TargetProperties::GetRunArguments (Args &args) const
3274{
3275 const uint32_t idx = ePropertyRunArgs;
3276 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3277}
3278
3279void
3280TargetProperties::SetRunArguments (const Args &args)
3281{
3282 const uint32_t idx = ePropertyRunArgs;
3283 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3284}
3285
3286size_t
3287TargetProperties::GetEnvironmentAsArgs (Args &env) const
3288{
3289 const uint32_t idx = ePropertyEnvVars;
3290 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3291}
3292
Ilia K8f37ca52015-02-13 14:31:06 +00003293void
3294TargetProperties::SetEnvironmentFromArgs (const Args &env)
3295{
3296 const uint32_t idx = ePropertyEnvVars;
3297 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
3298}
3299
Greg Clayton67cc0632012-08-22 17:17:09 +00003300bool
3301TargetProperties::GetSkipPrologue() const
3302{
3303 const uint32_t idx = ePropertySkipPrologue;
3304 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3305}
3306
3307PathMappingList &
3308TargetProperties::GetSourcePathMap () const
3309{
3310 const uint32_t idx = ePropertySourceMap;
3311 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3312 assert(option_value);
3313 return option_value->GetCurrentValue();
3314}
3315
3316FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003317TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003318{
3319 const uint32_t idx = ePropertyExecutableSearchPaths;
3320 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3321 assert(option_value);
3322 return option_value->GetCurrentValue();
3323}
3324
Michael Sartaina7499c92013-07-01 19:45:50 +00003325FileSpecList &
3326TargetProperties::GetDebugFileSearchPaths ()
3327{
3328 const uint32_t idx = ePropertyDebugFileSearchPaths;
3329 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3330 assert(option_value);
3331 return option_value->GetCurrentValue();
3332}
3333
Greg Clayton67cc0632012-08-22 17:17:09 +00003334bool
3335TargetProperties::GetEnableSyntheticValue () const
3336{
3337 const uint32_t idx = ePropertyEnableSynthetic;
3338 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3339}
3340
3341uint32_t
3342TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3343{
3344 const uint32_t idx = ePropertyMaxChildrenCount;
3345 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3346}
3347
3348uint32_t
3349TargetProperties::GetMaximumSizeOfStringSummary() const
3350{
3351 const uint32_t idx = ePropertyMaxSummaryLength;
3352 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3353}
3354
Enrico Granatad325bf92013-06-04 22:54:16 +00003355uint32_t
3356TargetProperties::GetMaximumMemReadSize () const
3357{
3358 const uint32_t idx = ePropertyMaxMemReadSize;
3359 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3360}
3361
Greg Clayton67cc0632012-08-22 17:17:09 +00003362FileSpec
3363TargetProperties::GetStandardInputPath () const
3364{
3365 const uint32_t idx = ePropertyInputPath;
3366 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3367}
3368
3369void
3370TargetProperties::SetStandardInputPath (const char *p)
3371{
3372 const uint32_t idx = ePropertyInputPath;
3373 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3374}
3375
3376FileSpec
3377TargetProperties::GetStandardOutputPath () const
3378{
3379 const uint32_t idx = ePropertyOutputPath;
3380 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3381}
3382
3383void
3384TargetProperties::SetStandardOutputPath (const char *p)
3385{
3386 const uint32_t idx = ePropertyOutputPath;
3387 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3388}
3389
3390FileSpec
3391TargetProperties::GetStandardErrorPath () const
3392{
3393 const uint32_t idx = ePropertyErrorPath;
3394 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3395}
3396
Greg Clayton6920b522012-08-22 18:39:03 +00003397const char *
3398TargetProperties::GetExpressionPrefixContentsAsCString ()
3399{
3400 const uint32_t idx = ePropertyExprPrefix;
3401 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3402 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003403 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003404 const bool null_terminate = true;
3405 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003406 if (data_sp)
3407 return (const char *) data_sp->GetBytes();
3408 }
Greg Clayton6920b522012-08-22 18:39:03 +00003409 return NULL;
3410}
3411
Greg Clayton67cc0632012-08-22 17:17:09 +00003412void
3413TargetProperties::SetStandardErrorPath (const char *p)
3414{
3415 const uint32_t idx = ePropertyErrorPath;
3416 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3417}
3418
3419bool
3420TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3421{
3422 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3423 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3424}
3425
Jim Ingham17d023f2013-03-13 17:58:04 +00003426bool
Daniel Malead79ae052013-08-07 21:54:09 +00003427TargetProperties::GetUseHexImmediates () const
3428{
3429 const uint32_t idx = ePropertyUseHexImmediates;
3430 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3431}
3432
3433bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003434TargetProperties::GetUseFastStepping () const
3435{
3436 const uint32_t idx = ePropertyUseFastStepping;
3437 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3438}
3439
Greg Claytonfb6621e2013-12-06 21:59:52 +00003440bool
3441TargetProperties::GetDisplayExpressionsInCrashlogs () const
3442{
3443 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3444 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3445}
3446
Enrico Granata397ddd52013-05-21 20:13:34 +00003447LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003448TargetProperties::GetLoadScriptFromSymbolFile () const
3449{
3450 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003451 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003452}
3453
Daniel Malead79ae052013-08-07 21:54:09 +00003454Disassembler::HexImmediateStyle
3455TargetProperties::GetHexImmediateStyle () const
3456{
3457 const uint32_t idx = ePropertyHexImmediateStyle;
3458 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3459}
3460
Greg Claytonfd814c52013-08-13 01:42:25 +00003461MemoryModuleLoadLevel
3462TargetProperties::GetMemoryModuleLoadLevel() const
3463{
3464 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3465 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3466}
3467
Jason Molendaa4bea722014-02-14 05:06:49 +00003468bool
3469TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3470{
3471 const uint32_t idx = ePropertyTrapHandlerNames;
3472 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3473}
Greg Claytonfd814c52013-08-13 01:42:25 +00003474
Jason Molendaa4bea722014-02-14 05:06:49 +00003475void
3476TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3477{
3478 const uint32_t idx = ePropertyTrapHandlerNames;
3479 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3480}
Greg Clayton67cc0632012-08-22 17:17:09 +00003481
Enrico Granata560558e2015-02-11 02:35:39 +00003482bool
3483TargetProperties::GetDisplayRuntimeSupportValues () const
3484{
3485 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3486 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3487}
3488
3489void
3490TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3491{
3492 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3493 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3494}
3495
Ilia K8f37ca52015-02-13 14:31:06 +00003496const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003497TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003498{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003499 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003500 return m_launch_info;
3501}
3502
3503void
3504TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3505{
3506 m_launch_info = launch_info;
3507 SetArg0(launch_info.GetArg0());
3508 SetRunArguments(launch_info.GetArguments());
3509 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3510 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3511 if (input_file_action)
3512 {
3513 const char *input_path = input_file_action->GetPath();
3514 if (input_path)
3515 SetStandardInputPath(input_path);
3516 }
3517 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3518 if (output_file_action)
3519 {
3520 const char *output_path = output_file_action->GetPath();
3521 if (output_path)
3522 SetStandardOutputPath(output_path);
3523 }
3524 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3525 if (error_file_action)
3526 {
3527 const char *error_path = error_file_action->GetPath();
3528 if (error_path)
3529 SetStandardErrorPath(error_path);
3530 }
3531 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3532 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3533 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3534}
3535
3536void
3537TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3538{
3539 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3540 this_->m_launch_info.SetArg0(this_->GetArg0());
3541}
3542
3543void
3544TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3545{
3546 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3547 Args args;
3548 if (this_->GetRunArguments(args))
3549 this_->m_launch_info.GetArguments() = args;
3550}
3551
3552void
3553TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3554{
3555 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3556 Args args;
3557 if (this_->GetEnvironmentAsArgs(args))
3558 this_->m_launch_info.GetEnvironmentEntries() = args;
3559}
3560
3561void
3562TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3563{
3564 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3565 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath().GetPath().c_str(), true, false);
3566}
3567
3568void
3569TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3570{
3571 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3572 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath().GetPath().c_str(), false, true);
3573}
3574
3575void
3576TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3577{
3578 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3579 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath().GetPath().c_str(), false, true);
3580}
3581
3582void
3583TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
3584{
3585 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3586 if (this_->GetDetachOnError())
3587 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3588 else
3589 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3590}
3591
3592void
3593TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
3594{
3595 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3596 if (this_->GetDisableASLR())
3597 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3598 else
3599 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3600}
3601
3602void
3603TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
3604{
3605 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3606 if (this_->GetDisableSTDIO())
3607 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
3608 else
3609 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
3610}