blob: f8d547de1e784c48d4184a929771fb6b0a595dab [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Target/Target.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Breakpoint/BreakpointResolver.h"
19#include "lldb/Breakpoint/BreakpointResolverAddress.h"
20#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000021#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000023#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000024#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/Event.h"
26#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000027#include "lldb/Core/Module.h"
28#include "lldb/Core/ModuleSpec.h"
29#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000030#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000031#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000032#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000034#include "lldb/Core/Timer.h"
35#include "lldb/Core/ValueObject.h"
Sean Callanan4bf80d52011-11-15 22:27:19 +000036#include "lldb/Expression/ClangASTSource.h"
Zachary Turneraf0f45f2015-03-03 21:05:17 +000037#include "lldb/Expression/ClangPersistentVariables.h"
Greg Claytoneb0103f2011-04-07 22:46:35 +000038#include "lldb/Expression/ClangUserExpression.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000039#include "lldb/Expression/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000040#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000042#include "lldb/Interpreter/CommandInterpreter.h"
43#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000044#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000045#include "lldb/Interpreter/OptionValues.h"
46#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000047#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Symbol/ObjectFile.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000049#include "lldb/Target/LanguageRuntime.h"
50#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000052#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000053#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000054#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000055#include "lldb/Target/Thread.h"
56#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
58using namespace lldb;
59using namespace lldb_private;
60
Jim Ingham4bddaeb2012-02-16 06:50:00 +000061ConstString &
62Target::GetStaticBroadcasterClass ()
63{
64 static ConstString class_name ("lldb.target");
65 return class_name;
66}
67
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068//----------------------------------------------------------------------
69// Target constructor
70//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000071Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000072 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000073 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000074 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000075 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000076 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000077 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000078 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000079 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000080 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 m_breakpoint_list (false),
82 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000083 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000084 m_process_sp (),
85 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000087 m_scratch_ast_context_ap (),
88 m_scratch_ast_source_ap (),
89 m_ast_importer_ap (),
Zachary Turner32abc6e2015-03-03 19:23:09 +000090 m_persistent_variables (new ClangPersistentVariables),
Greg Clayton9585fbf2013-03-19 00:20:55 +000091 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000092 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000093 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000094 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000095 m_suppress_stop_hooks (false),
96 m_is_dummy_target(is_dummy_target)
97
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000099 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
100 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
101 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +0000102 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +0000103 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000104
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000105 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000106
Greg Clayton5160ce52013-03-27 23:08:40 +0000107 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000109 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000110 if (m_arch.IsValid())
111 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000112 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET, "Target::Target created with architecture %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jason Molendae1b68ad2012-12-05 00:25:49 +0000113 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
Jim Ingham893c9322014-11-22 01:42:44 +0000116void
117Target::PrimeFromDummyTarget(Target *target)
118{
119 if (!target)
120 return;
121
122 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000123
124 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
125 {
126 if (breakpoint_sp->IsInternal())
127 continue;
128
129 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
130 AddBreakpoint (new_bp, false);
131 }
Jim Ingham893c9322014-11-22 01:42:44 +0000132}
133
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134//----------------------------------------------------------------------
135// Destructor
136//----------------------------------------------------------------------
137Target::~Target()
138{
Greg Clayton5160ce52013-03-27 23:08:40 +0000139 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000141 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 DeleteCurrentProcess ();
143}
144
145void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000146Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147{
Greg Clayton89411422010-10-08 00:21:05 +0000148// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000149 if (description_level != lldb::eDescriptionLevelBrief)
150 {
151 s->Indent();
152 s->PutCString("Target\n");
153 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000154 m_images.Dump(s);
155 m_breakpoint_list.Dump(s);
156 m_internal_breakpoint_list.Dump(s);
157 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000158 }
159 else
160 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000161 Module *exe_module = GetExecutableModulePointer();
162 if (exe_module)
163 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000164 else
165 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000166 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167}
168
169void
Greg Clayton90ba8112012-12-05 00:16:59 +0000170Target::CleanupProcess ()
171{
172 // Do any cleanup of the target we need to do between process instances.
173 // NB It is better to do this before destroying the process in case the
174 // clean up needs some help from the process.
175 m_breakpoint_list.ClearAllBreakpointSites();
176 m_internal_breakpoint_list.ClearAllBreakpointSites();
177 // Disable watchpoints just on the debugger side.
178 Mutex::Locker locker;
179 this->GetWatchpointList().GetListMutex(locker);
180 DisableAllWatchpoints(false);
181 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000182 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000183}
184
185void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186Target::DeleteCurrentProcess ()
187{
188 if (m_process_sp.get())
189 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000190 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 if (m_process_sp->IsAlive())
Jason Molendaede31932015-04-17 05:01:58 +0000192 m_process_sp->Destroy(false);
Jim Inghamd0a3e122011-02-16 17:54:55 +0000193
194 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195
Greg Clayton90ba8112012-12-05 00:16:59 +0000196 CleanupProcess ();
197
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 m_process_sp.reset();
199 }
200}
201
202const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000203Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204{
205 DeleteCurrentProcess ();
Greg Claytonc3776bf2012-02-09 06:16:32 +0000206 m_process_sp = Process::FindPlugin(*this, plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207 return m_process_sp;
208}
209
210const lldb::ProcessSP &
211Target::GetProcessSP () const
212{
213 return m_process_sp;
214}
215
Greg Clayton3418c852011-08-10 02:10:13 +0000216void
217Target::Destroy()
218{
219 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000220 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000221 DeleteCurrentProcess ();
222 m_platform_sp.reset();
223 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000224 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000225 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000226 const bool notify = false;
227 m_breakpoint_list.RemoveAll(notify);
228 m_internal_breakpoint_list.RemoveAll(notify);
229 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000230 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000231 m_search_filter_sp.reset();
232 m_image_search_paths.Clear(notify);
Zachary Turner32abc6e2015-03-03 19:23:09 +0000233 m_persistent_variables->Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000234 m_stop_hooks.clear();
235 m_stop_hook_next_id = 0;
236 m_suppress_stop_hooks = false;
237}
238
239
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240BreakpointList &
241Target::GetBreakpointList(bool internal)
242{
243 if (internal)
244 return m_internal_breakpoint_list;
245 else
246 return m_breakpoint_list;
247}
248
249const BreakpointList &
250Target::GetBreakpointList(bool internal) const
251{
252 if (internal)
253 return m_internal_breakpoint_list;
254 else
255 return m_breakpoint_list;
256}
257
258BreakpointSP
259Target::GetBreakpointByID (break_id_t break_id)
260{
261 BreakpointSP bp_sp;
262
263 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
264 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
265 else
266 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
267
268 return bp_sp;
269}
270
271BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000272Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000273 const FileSpecList *source_file_spec_list,
274 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000275 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000276 bool hardware,
277 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278{
Jim Ingham87df91b2011-09-23 00:54:11 +0000279 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000280 if (move_to_nearest_code == eLazyBoolCalculate)
281 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
282 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex, !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000283 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000284}
285
286
287BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000288Target::CreateBreakpoint (const FileSpecList *containingModules,
289 const FileSpec &file,
290 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000291 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000292 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000293 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000294 bool hardware,
295 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000296{
Greg Clayton1f746072012-08-29 21:13:06 +0000297 if (check_inlines == eLazyBoolCalculate)
298 {
299 const InlineStrategy inline_strategy = GetInlineStrategy();
300 switch (inline_strategy)
301 {
302 case eInlineBreakpointsNever:
303 check_inlines = eLazyBoolNo;
304 break;
305
306 case eInlineBreakpointsHeaders:
307 if (file.IsSourceImplementationFile())
308 check_inlines = eLazyBoolNo;
309 else
310 check_inlines = eLazyBoolYes;
311 break;
312
313 case eInlineBreakpointsAlways:
314 check_inlines = eLazyBoolYes;
315 break;
316 }
317 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000318 SearchFilterSP filter_sp;
319 if (check_inlines == eLazyBoolNo)
320 {
321 // Not checking for inlines, we are looking only for matching compile units
322 FileSpecList compile_unit_list;
323 compile_unit_list.Append (file);
324 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
325 }
326 else
327 {
328 filter_sp = GetSearchFilterForModuleList (containingModules);
329 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000330 if (skip_prologue == eLazyBoolCalculate)
331 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000332 if (move_to_nearest_code == eLazyBoolCalculate)
333 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000334
Greg Clayton1f746072012-08-29 21:13:06 +0000335 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
336 file,
337 line_no,
338 check_inlines,
Ilia K055ad9b2015-05-18 13:41:01 +0000339 skip_prologue,
340 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000341 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000342}
343
344
345BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000346Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000347{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000348 Address so_addr;
349 // Attempt to resolve our load address if possible, though it is ok if
350 // it doesn't resolve to section/offset.
351
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000352 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000353 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000354 if (!so_addr.IsValid())
355 {
356 // The address didn't resolve, so just set this as an absolute address
357 so_addr.SetOffset (addr);
358 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000359 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 return bp_sp;
361}
362
363BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000364Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000366 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000367 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000368 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369}
370
371BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000372Target::CreateBreakpoint (const FileSpecList *containingModules,
373 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000374 const char *func_name,
375 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000376 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000377 bool internal,
378 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000380 BreakpointSP bp_sp;
381 if (func_name)
382 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000383 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000384
385 if (skip_prologue == eLazyBoolCalculate)
386 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
387
Greg Claytond16e1e52011-07-12 17:06:17 +0000388 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
389 func_name,
390 func_name_type_mask,
391 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000392 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000393 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000394 }
395 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396}
397
Jim Inghamfab10e82012-03-06 00:37:27 +0000398lldb::BreakpointSP
399Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000400 const FileSpecList *containingSourceFiles,
401 const std::vector<std::string> &func_names,
402 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000403 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000404 bool internal,
405 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000406{
407 BreakpointSP bp_sp;
408 size_t num_names = func_names.size();
409 if (num_names > 0)
410 {
411 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000412
413 if (skip_prologue == eLazyBoolCalculate)
414 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
415
416 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000417 func_names,
418 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000419 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000420 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000421 }
422 return bp_sp;
423}
424
Jim Ingham133e0fb2012-03-03 02:05:11 +0000425BreakpointSP
426Target::CreateBreakpoint (const FileSpecList *containingModules,
427 const FileSpecList *containingSourceFiles,
428 const char *func_names[],
429 size_t num_names,
430 uint32_t func_name_type_mask,
Jim Inghama8558b62012-05-22 00:12:20 +0000431 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000432 bool internal,
433 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000434{
435 BreakpointSP bp_sp;
436 if (num_names > 0)
437 {
438 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
439
Greg Clayton03da4cc2013-04-19 21:31:16 +0000440 if (skip_prologue == eLazyBoolCalculate)
441 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
442
443 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000444 func_names,
445 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000446 func_name_type_mask,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000447 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000448 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000449 }
450 return bp_sp;
451}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452
453SearchFilterSP
454Target::GetSearchFilterForModule (const FileSpec *containingModule)
455{
456 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 if (containingModule != NULL)
458 {
459 // TODO: We should look into sharing module based search filters
460 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000461 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 }
463 else
464 {
465 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000466 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 filter_sp = m_search_filter_sp;
468 }
469 return filter_sp;
470}
471
Jim Ingham969795f2011-09-21 01:17:13 +0000472SearchFilterSP
473Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
474{
475 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000476 if (containingModules && containingModules->GetSize() != 0)
477 {
478 // TODO: We should look into sharing module based search filters
479 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000480 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000481 }
482 else
483 {
484 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000485 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000486 filter_sp = m_search_filter_sp;
487 }
488 return filter_sp;
489}
490
Jim Ingham87df91b2011-09-23 00:54:11 +0000491SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000492Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
493 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000494{
495 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
496 return GetSearchFilterForModuleList(containingModules);
497
498 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000499 if (containingModules == NULL)
500 {
501 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
502 // but that will take a little reworking.
503
Greg Claytone1cd1be2012-01-29 20:56:30 +0000504 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000505 }
506 else
507 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000508 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000509 }
510 return filter_sp;
511}
512
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000514Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000515 const FileSpecList *containingSourceFiles,
516 RegularExpression &func_regex,
517 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000518 bool internal,
519 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520{
Jim Ingham87df91b2011-09-23 00:54:11 +0000521 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000522 bool skip =
523 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
524 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000525 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
526 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000527 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000528
Jim Ingham1460e4b2014-01-10 23:46:59 +0000529 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530}
531
Jim Ingham219ba192012-03-05 04:47:34 +0000532lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000533Target::CreateExceptionBreakpoint (enum lldb::LanguageType language, bool catch_bp, bool throw_bp, bool internal, Args *additional_args, Error *error)
Jim Ingham219ba192012-03-05 04:47:34 +0000534{
Jim Inghama72b31c2015-04-22 19:42:18 +0000535 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
536 if (exc_bkpt_sp && additional_args)
537 {
538 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
539 if (precondition_sp && additional_args)
540 {
541 if (error)
542 *error = precondition_sp->ConfigurePrecondition(*additional_args);
543 else
544 precondition_sp->ConfigurePrecondition(*additional_args);
545 }
546 }
547 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000548}
Jim Inghama72b31c2015-04-22 19:42:18 +0000549
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000551Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552{
553 BreakpointSP bp_sp;
554 if (filter_sp && resolver_sp)
555 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000556 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000558 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000560 return bp_sp;
561}
562
563void
564Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
565{
566 if (!bp_sp)
567 return;
568 if (internal)
569 m_internal_breakpoint_list.Add (bp_sp, false);
570 else
571 m_breakpoint_list.Add (bp_sp, true);
572
573 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
574 if (log)
575 {
576 StreamString s;
577 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
578 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
579 }
580
581 bp_sp->ResolveBreakpoint();
582
583 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000584 {
585 m_last_created_breakpoint = bp_sp;
586 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
Johnny Chen86364b42011-09-20 23:28:55 +0000589bool
590Target::ProcessIsValid()
591{
592 return (m_process_sp && m_process_sp->IsAlive());
593}
594
Johnny Chenb90827e2012-06-04 23:19:54 +0000595static bool
596CheckIfWatchpointsExhausted(Target *target, Error &error)
597{
598 uint32_t num_supported_hardware_watchpoints;
599 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
600 if (rc.Success())
601 {
602 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
603 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
604 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
605 num_supported_hardware_watchpoints);
606 }
607 return false;
608}
609
Johnny Chen01a67862011-10-14 00:42:25 +0000610// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000611// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000612WatchpointSP
Jim Inghama7dfb662012-10-23 07:20:06 +0000613Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const ClangASTType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000614{
Greg Clayton5160ce52013-03-27 23:08:40 +0000615 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000616 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000617 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000618 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000619
Johnny Chen01a67862011-10-14 00:42:25 +0000620 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000621 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000622 {
623 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000624 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000625 }
Jim Inghamc6462312013-06-18 21:52:48 +0000626
Johnny Chen45e541f2011-09-14 22:20:15 +0000627 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000628 {
629 if (size == 0)
630 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
631 else
Daniel Malead01b2952012-11-29 21:49:15 +0000632 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000633 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000634 }
Jim Inghamc6462312013-06-18 21:52:48 +0000635
636 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
637 {
638 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
639 }
Johnny Chen7313a642011-09-13 01:15:36 +0000640
Johnny Chen01a67862011-10-14 00:42:25 +0000641 // Currently we only support one watchpoint per address, with total number
642 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000643
644 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000645 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 +0000646 Mutex::Locker locker;
647 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000648 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000649 if (matched_sp)
650 {
Johnny Chen0c406372011-09-14 20:23:45 +0000651 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000652 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000653 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
654 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000655 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000656 if (size == old_size && kind == old_type)
657 {
Johnny Chen01a67862011-10-14 00:42:25 +0000658 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000659 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000660 }
661 else
662 {
Johnny Chen01a67862011-10-14 00:42:25 +0000663 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000664 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
665 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000666 }
Johnny Chen3c532582011-09-13 23:29:31 +0000667 }
668
Jason Molenda727e3922012-12-05 23:07:34 +0000669 if (!wp_sp)
670 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000671 wp_sp.reset(new Watchpoint(*this, addr, size, type));
672 wp_sp->SetWatchpointType(kind, notify);
673 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000674 }
Johnny Chen0c406372011-09-14 20:23:45 +0000675
Jim Ingham1b5792e2012-12-18 02:03:49 +0000676 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000677 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000678 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
679 __FUNCTION__,
680 error.Success() ? "succeeded" : "failed",
681 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000682
Jason Molenda727e3922012-12-05 23:07:34 +0000683 if (error.Fail())
684 {
Johnny Chen41b77262012-03-26 22:00:10 +0000685 // Enabling the watchpoint on the device side failed.
686 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000687 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000688 // See if we could provide more helpful error message.
689 if (!CheckIfWatchpointsExhausted(this, error))
690 {
691 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000692 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000693 }
Johnny Chen01a67862011-10-14 00:42:25 +0000694 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000695 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000696 else
Johnny Chen01a67862011-10-14 00:42:25 +0000697 m_last_created_watchpoint = wp_sp;
698 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000699}
700
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701void
702Target::RemoveAllBreakpoints (bool internal_also)
703{
Greg Clayton5160ce52013-03-27 23:08:40 +0000704 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000705 if (log)
706 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
707
Greg Clayton9fed0d82010-07-23 23:33:17 +0000708 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000710 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000711
712 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713}
714
715void
716Target::DisableAllBreakpoints (bool internal_also)
717{
Greg Clayton5160ce52013-03-27 23:08:40 +0000718 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 if (log)
720 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
721
722 m_breakpoint_list.SetEnabledAll (false);
723 if (internal_also)
724 m_internal_breakpoint_list.SetEnabledAll (false);
725}
726
727void
728Target::EnableAllBreakpoints (bool internal_also)
729{
Greg Clayton5160ce52013-03-27 23:08:40 +0000730 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 if (log)
732 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
733
734 m_breakpoint_list.SetEnabledAll (true);
735 if (internal_also)
736 m_internal_breakpoint_list.SetEnabledAll (true);
737}
738
739bool
740Target::RemoveBreakpointByID (break_id_t break_id)
741{
Greg Clayton5160ce52013-03-27 23:08:40 +0000742 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 if (log)
744 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
745
746 if (DisableBreakpointByID (break_id))
747 {
748 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000749 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000750 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000751 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000752 if (m_last_created_breakpoint)
753 {
754 if (m_last_created_breakpoint->GetID() == break_id)
755 m_last_created_breakpoint.reset();
756 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000757 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000758 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 return true;
760 }
761 return false;
762}
763
764bool
765Target::DisableBreakpointByID (break_id_t break_id)
766{
Greg Clayton5160ce52013-03-27 23:08:40 +0000767 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 if (log)
769 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
770
771 BreakpointSP bp_sp;
772
773 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
774 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
775 else
776 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
777 if (bp_sp)
778 {
779 bp_sp->SetEnabled (false);
780 return true;
781 }
782 return false;
783}
784
785bool
786Target::EnableBreakpointByID (break_id_t break_id)
787{
Greg Clayton5160ce52013-03-27 23:08:40 +0000788 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789 if (log)
790 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
791 __FUNCTION__,
792 break_id,
793 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
794
795 BreakpointSP bp_sp;
796
797 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
798 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
799 else
800 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
801
802 if (bp_sp)
803 {
804 bp_sp->SetEnabled (true);
805 return true;
806 }
807 return false;
808}
809
Johnny Chenedf50372011-09-23 21:21:43 +0000810// The flag 'end_to_end', default to true, signifies that the operation is
811// performed end to end, for both the debugger and the debuggee.
812
Johnny Chen01a67862011-10-14 00:42:25 +0000813// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
814// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000815bool
Johnny Chen01a67862011-10-14 00:42:25 +0000816Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000817{
Greg Clayton5160ce52013-03-27 23:08:40 +0000818 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000819 if (log)
820 log->Printf ("Target::%s\n", __FUNCTION__);
821
Johnny Chenedf50372011-09-23 21:21:43 +0000822 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000823 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000824 return true;
825 }
826
827 // Otherwise, it's an end to end operation.
828
Johnny Chen86364b42011-09-20 23:28:55 +0000829 if (!ProcessIsValid())
830 return false;
831
Johnny Chen01a67862011-10-14 00:42:25 +0000832 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000833 for (size_t i = 0; i < num_watchpoints; ++i)
834 {
Johnny Chen01a67862011-10-14 00:42:25 +0000835 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
836 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000837 return false;
838
Johnny Chen01a67862011-10-14 00:42:25 +0000839 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000840 if (rc.Fail())
841 return false;
842 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000843 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000844 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000845 return true; // Success!
846}
847
Johnny Chen01a67862011-10-14 00:42:25 +0000848// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
849// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000850bool
Johnny Chen01a67862011-10-14 00:42:25 +0000851Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000852{
Greg Clayton5160ce52013-03-27 23:08:40 +0000853 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000854 if (log)
855 log->Printf ("Target::%s\n", __FUNCTION__);
856
Johnny Chenedf50372011-09-23 21:21:43 +0000857 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000858 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000859 return true;
860 }
861
862 // Otherwise, it's an end to end operation.
863
Johnny Chen86364b42011-09-20 23:28:55 +0000864 if (!ProcessIsValid())
865 return false;
866
Johnny Chen01a67862011-10-14 00:42:25 +0000867 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000868 for (size_t i = 0; i < num_watchpoints; ++i)
869 {
Johnny Chen01a67862011-10-14 00:42:25 +0000870 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
871 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000872 return false;
873
Johnny Chen01a67862011-10-14 00:42:25 +0000874 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000875 if (rc.Fail())
876 return false;
877 }
Johnny Chen86364b42011-09-20 23:28:55 +0000878 return true; // Success!
879}
880
Johnny Chen01a67862011-10-14 00:42:25 +0000881// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
882// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000883bool
Johnny Chen01a67862011-10-14 00:42:25 +0000884Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000885{
Greg Clayton5160ce52013-03-27 23:08:40 +0000886 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000887 if (log)
888 log->Printf ("Target::%s\n", __FUNCTION__);
889
Johnny Chenedf50372011-09-23 21:21:43 +0000890 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000891 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000892 return true;
893 }
894
895 // Otherwise, it's an end to end operation.
896
Johnny Chen86364b42011-09-20 23:28:55 +0000897 if (!ProcessIsValid())
898 return false;
899
Johnny Chen01a67862011-10-14 00:42:25 +0000900 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000901 for (size_t i = 0; i < num_watchpoints; ++i)
902 {
Johnny Chen01a67862011-10-14 00:42:25 +0000903 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
904 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000905 return false;
906
Johnny Chen01a67862011-10-14 00:42:25 +0000907 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000908 if (rc.Fail())
909 return false;
910 }
Johnny Chen86364b42011-09-20 23:28:55 +0000911 return true; // Success!
912}
913
Johnny Chena4d6bc92012-02-25 06:44:30 +0000914// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
915bool
916Target::ClearAllWatchpointHitCounts ()
917{
Greg Clayton5160ce52013-03-27 23:08:40 +0000918 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000919 if (log)
920 log->Printf ("Target::%s\n", __FUNCTION__);
921
922 size_t num_watchpoints = m_watchpoint_list.GetSize();
923 for (size_t i = 0; i < num_watchpoints; ++i)
924 {
925 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
926 if (!wp_sp)
927 return false;
928
929 wp_sp->ResetHitCount();
930 }
931 return true; // Success!
932}
933
Enrico Granata5e3fe042015-02-11 00:37:54 +0000934// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
935bool
936Target::ClearAllWatchpointHistoricValues ()
937{
938 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
939 if (log)
940 log->Printf ("Target::%s\n", __FUNCTION__);
941
942 size_t num_watchpoints = m_watchpoint_list.GetSize();
943 for (size_t i = 0; i < num_watchpoints; ++i)
944 {
945 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
946 if (!wp_sp)
947 return false;
948
949 wp_sp->ResetHistoricValues();
950 }
951 return true; // Success!
952}
953
Johnny Chen01a67862011-10-14 00:42:25 +0000954// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +0000955// during these operations.
956bool
Johnny Chen01a67862011-10-14 00:42:25 +0000957Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000958{
Greg Clayton5160ce52013-03-27 23:08:40 +0000959 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +0000960 if (log)
961 log->Printf ("Target::%s\n", __FUNCTION__);
962
963 if (!ProcessIsValid())
964 return false;
965
Johnny Chen01a67862011-10-14 00:42:25 +0000966 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +0000967 for (size_t i = 0; i < num_watchpoints; ++i)
968 {
Johnny Chen01a67862011-10-14 00:42:25 +0000969 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
970 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +0000971 return false;
972
Johnny Chen01a67862011-10-14 00:42:25 +0000973 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +0000974 }
975 return true; // Success!
976}
977
Johnny Chen01a67862011-10-14 00:42:25 +0000978// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +0000979bool
Johnny Chen01a67862011-10-14 00:42:25 +0000980Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +0000981{
Greg Clayton5160ce52013-03-27 23:08:40 +0000982 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000983 if (log)
984 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
985
986 if (!ProcessIsValid())
987 return false;
988
Johnny Chen01a67862011-10-14 00:42:25 +0000989 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
990 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000991 {
Johnny Chen01a67862011-10-14 00:42:25 +0000992 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +0000993 if (rc.Success())
994 return true;
Johnny Chen86364b42011-09-20 23:28:55 +0000995
Johnny Chenf04ee932011-09-22 18:04:58 +0000996 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +0000997 }
998 return false;
999}
1000
Johnny Chen01a67862011-10-14 00:42:25 +00001001// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001002bool
Johnny Chen01a67862011-10-14 00:42:25 +00001003Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001004{
Greg Clayton5160ce52013-03-27 23:08:40 +00001005 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001006 if (log)
1007 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1008
1009 if (!ProcessIsValid())
1010 return false;
1011
Johnny Chen01a67862011-10-14 00:42:25 +00001012 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1013 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001014 {
Johnny Chen01a67862011-10-14 00:42:25 +00001015 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001016 if (rc.Success())
1017 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001018
Johnny Chenf04ee932011-09-22 18:04:58 +00001019 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001020 }
1021 return false;
1022}
1023
Johnny Chen01a67862011-10-14 00:42:25 +00001024// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001025bool
Johnny Chen01a67862011-10-14 00:42:25 +00001026Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001027{
Greg Clayton5160ce52013-03-27 23:08:40 +00001028 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001029 if (log)
1030 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1031
Jim Inghamb0b45132013-07-02 02:09:46 +00001032 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1033 if (watch_to_remove_sp == m_last_created_watchpoint)
1034 m_last_created_watchpoint.reset();
1035
Johnny Chen01a67862011-10-14 00:42:25 +00001036 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001037 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001038 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001039 return true;
1040 }
1041 return false;
1042}
1043
Johnny Chen01a67862011-10-14 00:42:25 +00001044// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001045bool
Johnny Chen01a67862011-10-14 00:42:25 +00001046Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001047{
Greg Clayton5160ce52013-03-27 23:08:40 +00001048 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001049 if (log)
1050 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1051
1052 if (!ProcessIsValid())
1053 return false;
1054
Johnny Chen01a67862011-10-14 00:42:25 +00001055 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1056 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001057 {
Johnny Chen01a67862011-10-14 00:42:25 +00001058 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001059 return true;
1060 }
1061 return false;
1062}
1063
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064ModuleSP
1065Target::GetExecutableModule ()
1066{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001067 // search for the first executable in the module list
1068 for (size_t i = 0; i < m_images.GetSize(); ++i)
1069 {
1070 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1071 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1072 if (obj == nullptr)
1073 continue;
1074 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1075 return module_sp;
1076 }
1077 // as fall back return the first module loaded
1078 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001079}
1080
1081Module*
1082Target::GetExecutableModulePointer ()
1083{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001084 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001085}
1086
Enrico Granata17598482012-11-08 02:22:02 +00001087static void
1088LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1089{
1090 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001091 StreamString feedback_stream;
1092 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001093 {
Enrico Granata97303392013-05-21 00:00:30 +00001094 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001095 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001096 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1097 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001098 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001099 if (feedback_stream.GetSize())
1100 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1101 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001102}
1103
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001104void
Greg Claytonb35db632013-11-09 00:03:31 +00001105Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106{
Greg Claytonb35db632013-11-09 00:03:31 +00001107 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001108 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109 m_images.Clear();
1110 m_scratch_ast_context_ap.reset();
Sean Callanan4bf80d52011-11-15 22:27:19 +00001111 m_scratch_ast_source_ap.reset();
Sean Callanan686b2312011-11-16 18:20:47 +00001112 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001113}
1114
1115void
Greg Claytonb35db632013-11-09 00:03:31 +00001116Target::DidExec ()
1117{
1118 // When a process exec's we need to know about it so we can do some cleanup.
1119 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1120 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1121}
1122
1123void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001124Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1125{
1126 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001127 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128
1129 if (executable_sp.get())
1130 {
1131 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001132 "Target::SetExecutableModule (executable = '%s')",
1133 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001135 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136
Jim Ingham5aee1622010-08-09 23:31:02 +00001137 // 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 +00001138 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001139 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001140 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001141 if (log)
1142 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1143 }
1144
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001145 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001146 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001148 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001149 {
1150 executable_objfile->GetDependentModules(dependent_files);
1151 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1152 {
Greg Claytonded470d2011-03-19 01:12:21 +00001153 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1154 FileSpec platform_dependent_file_spec;
1155 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001156 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001157 else
1158 platform_dependent_file_spec = dependent_file_spec;
1159
Greg Claytonb9a01b32012-02-26 05:51:37 +00001160 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1161 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162 if (image_module_sp.get())
1163 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001164 ObjectFile *objfile = image_module_sp->GetObjectFile();
1165 if (objfile)
1166 objfile->GetDependentModules(dependent_files);
1167 }
1168 }
1169 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 }
1171}
1172
1173
Jim Ingham5aee1622010-08-09 23:31:02 +00001174bool
1175Target::SetArchitecture (const ArchSpec &arch_spec)
1176{
Greg Clayton5160ce52013-03-27 23:08:40 +00001177 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001178 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001179 {
Greg Clayton70512312012-05-08 01:45:38 +00001180 // If we haven't got a valid arch spec, or the architectures are
1181 // compatible, so just update the architecture. Architectures can be
1182 // equal, yet the triple OS and vendor might change, so we need to do
1183 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001184 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001185 if (log)
1186 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 +00001187 return true;
1188 }
1189 else
1190 {
1191 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001192 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001193 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 +00001194 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001195 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001196
Greg Claytonb35db632013-11-09 00:03:31 +00001197 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001198 // Need to do something about unsetting breakpoints.
1199
1200 if (executable_sp)
1201 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001202 if (log)
1203 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 +00001204 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1205 Error error = ModuleList::GetSharedModule (module_spec,
1206 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001207 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001208 NULL,
1209 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001210
1211 if (!error.Fail() && executable_sp)
1212 {
1213 SetExecutableModule (executable_sp, true);
1214 return true;
1215 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001216 }
1217 }
Greg Clayton70512312012-05-08 01:45:38 +00001218 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001219}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001221bool
1222Target::MergeArchitecture (const ArchSpec &arch_spec)
1223{
1224 if (arch_spec.IsValid())
1225 {
1226 if (m_arch.IsCompatibleMatch(arch_spec))
1227 {
1228 // The current target arch is compatible with "arch_spec", see if we
1229 // can improve our current architecture using bits from "arch_spec"
1230
1231 // Merge bits from arch_spec into "merged_arch" and set our architecture
1232 ArchSpec merged_arch (m_arch);
1233 merged_arch.MergeFrom (arch_spec);
1234 return SetArchitecture(merged_arch);
1235 }
1236 else
1237 {
1238 // The new architecture is different, we just need to replace it
1239 return SetArchitecture(arch_spec);
1240 }
1241 }
1242 return false;
1243}
1244
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245void
Enrico Granataefe637d2012-11-08 19:16:03 +00001246Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001247{
1248}
1249
1250void
Enrico Granataefe637d2012-11-08 19:16:03 +00001251Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252{
1253 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001254 if (m_valid)
1255 {
1256 ModuleList my_module_list;
1257 my_module_list.Append(module_sp);
1258 LoadScriptingResourceForModule(module_sp, this);
1259 ModulesDidLoad (my_module_list);
1260 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001261}
1262
1263void
Enrico Granataefe637d2012-11-08 19:16:03 +00001264Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001265{
1266 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001267 if (m_valid)
1268 {
1269 ModuleList my_module_list;
1270 my_module_list.Append(module_sp);
1271 ModulesDidUnload (my_module_list, false);
1272 }
Enrico Granata17598482012-11-08 02:22:02 +00001273}
1274
1275void
Enrico Granataefe637d2012-11-08 19:16:03 +00001276Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277{
Jim Inghame716ae02011-08-03 01:00:06 +00001278 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001279 if (m_valid)
1280 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001281}
1282
1283void
1284Target::ModulesDidLoad (ModuleList &module_list)
1285{
Greg Clayton23f8c952014-03-24 23:10:19 +00001286 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001287 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001288 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001289 if (m_process_sp)
1290 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001291 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001292 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001293 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001294 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295}
1296
1297void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001298Target::SymbolsDidLoad (ModuleList &module_list)
1299{
Greg Clayton23f8c952014-03-24 23:10:19 +00001300 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001301 {
Jim Ingham31caf982013-06-04 23:01:35 +00001302 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001303 {
Jim Ingham31caf982013-06-04 23:01:35 +00001304 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1305 if (runtime)
1306 {
1307 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1308 objc_runtime->SymbolsDidLoad(module_list);
1309 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001310 }
Jim Ingham31caf982013-06-04 23:01:35 +00001311
Greg Clayton095eeaa2013-11-05 23:28:00 +00001312 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001313 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001314 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001315}
1316
1317void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001318Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319{
Greg Clayton23f8c952014-03-24 23:10:19 +00001320 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001321 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001322 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001323 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001324 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001325 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326}
1327
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001328bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001329Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001330{
Greg Clayton67cc0632012-08-22 17:17:09 +00001331 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001332 {
1333 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001334 ModuleSpec module_spec (module_file_spec);
1335 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001336
1337 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1338 // black list.
1339 if (num_modules > 0)
1340 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001341 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001342 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001343 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001344 return false;
1345 }
1346 return true;
1347 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001348 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001349 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001350}
1351
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001352bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001353Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001354{
Greg Clayton67cc0632012-08-22 17:17:09 +00001355 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001356 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001357 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001358 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001359 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001360 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001361}
1362
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363size_t
Greg Claytondb598232011-01-07 01:57:07 +00001364Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1365{
Greg Claytone72dfb32012-02-24 01:59:29 +00001366 SectionSP section_sp (addr.GetSection());
1367 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001368 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001369 // 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 +00001370 if (section_sp->IsEncrypted())
1371 {
Greg Clayton57f06302012-05-25 17:05:55 +00001372 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001373 return 0;
1374 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001375 ModuleSP module_sp (section_sp->GetModule());
1376 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001377 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001378 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1379 if (objfile)
1380 {
1381 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1382 addr.GetOffset(),
1383 dst,
1384 dst_len);
1385 if (bytes_read > 0)
1386 return bytes_read;
1387 else
1388 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1389 }
Greg Claytondb598232011-01-07 01:57:07 +00001390 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001391 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001392 }
1393 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001394 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001395 }
1396 else
Greg Claytondb598232011-01-07 01:57:07 +00001397 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001398
Greg Claytondb598232011-01-07 01:57:07 +00001399 return 0;
1400}
1401
1402size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001403Target::ReadMemory (const Address& addr,
1404 bool prefer_file_cache,
1405 void *dst,
1406 size_t dst_len,
1407 Error &error,
1408 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001409{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001410 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001411
Enrico Granata9128ee22011-09-06 19:20:51 +00001412 // if we end up reading this from process memory, we will fill this
1413 // with the actual load address
1414 if (load_addr_ptr)
1415 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1416
Greg Claytondb598232011-01-07 01:57:07 +00001417 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001418
1419 addr_t load_addr = LLDB_INVALID_ADDRESS;
1420 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001421 Address resolved_addr;
1422 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001423 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001424 SectionLoadList &section_load_list = GetSectionLoadList();
1425 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001426 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001427 // No sections are loaded, so we must assume we are not running
1428 // yet and anything we are given is a file address.
1429 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1430 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001431 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001432 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001433 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001434 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001435 // we have manually loaded some sections with "target modules load ..."
1436 // or because we have have a live process that has sections loaded
1437 // through the dynamic loader
1438 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001439 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001440 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001441 }
Greg Clayton357132e2011-03-26 19:14:58 +00001442 if (!resolved_addr.IsValid())
1443 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001444
Greg Claytonc749eb82011-07-11 05:12:02 +00001445
Greg Claytondb598232011-01-07 01:57:07 +00001446 if (prefer_file_cache)
1447 {
1448 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1449 if (bytes_read > 0)
1450 return bytes_read;
1451 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001452
Johnny Chen86364b42011-09-20 23:28:55 +00001453 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001454 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001455 if (load_addr == LLDB_INVALID_ADDRESS)
1456 load_addr = resolved_addr.GetLoadAddress (this);
1457
Greg Claytondda4f7b2010-06-30 23:03:03 +00001458 if (load_addr == LLDB_INVALID_ADDRESS)
1459 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001460 ModuleSP addr_module_sp (resolved_addr.GetModule());
1461 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001462 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001463 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001464 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001465 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001466 else
Daniel Malead01b2952012-11-29 21:49:15 +00001467 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001468 }
1469 else
1470 {
Greg Claytondb598232011-01-07 01:57:07 +00001471 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 if (bytes_read != dst_len)
1473 {
1474 if (error.Success())
1475 {
1476 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001477 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478 else
Daniel Malead01b2952012-11-29 21:49:15 +00001479 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 +00001480 }
1481 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001482 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001483 {
1484 if (load_addr_ptr)
1485 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001486 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001487 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001488 // If the address is not section offset we have an address that
1489 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001490 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001491 // more we can do. If it is section offset, we might be able to
1492 // read cached memory from the object file.
1493 if (!resolved_addr.IsSectionOffset())
1494 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001496 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001497
Greg Claytonc749eb82011-07-11 05:12:02 +00001498 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001499 {
Greg Claytondb598232011-01-07 01:57:07 +00001500 // If we didn't already try and read from the object file cache, then
1501 // try it after failing to read from the process.
1502 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001503 }
1504 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001505}
1506
Greg Claytond16e1e52011-07-12 17:06:17 +00001507size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001508Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1509{
1510 char buf[256];
1511 out_str.clear();
1512 addr_t curr_addr = addr.GetLoadAddress(this);
1513 Address address(addr);
1514 while (1)
1515 {
1516 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1517 if (length == 0)
1518 break;
1519 out_str.append(buf, length);
1520 // If we got "length - 1" bytes, we didn't get the whole C string, we
1521 // need to read some more characters
1522 if (length == sizeof(buf) - 1)
1523 curr_addr += length;
1524 else
1525 break;
1526 address = Address(curr_addr);
1527 }
1528 return out_str.size();
1529}
1530
1531
1532size_t
1533Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1534{
1535 size_t total_cstr_len = 0;
1536 if (dst && dst_max_len)
1537 {
1538 result_error.Clear();
1539 // NULL out everything just to be safe
1540 memset (dst, 0, dst_max_len);
1541 Error error;
1542 addr_t curr_addr = addr.GetLoadAddress(this);
1543 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001544
1545 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1546 // think this really needs to be tied to the memory cache subsystem's
1547 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001548 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001549
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001550 size_t bytes_left = dst_max_len - 1;
1551 char *curr_dst = dst;
1552
1553 while (bytes_left > 0)
1554 {
1555 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1556 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1557 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1558
1559 if (bytes_read == 0)
1560 {
1561 result_error = error;
1562 dst[total_cstr_len] = '\0';
1563 break;
1564 }
1565 const size_t len = strlen(curr_dst);
1566
1567 total_cstr_len += len;
1568
1569 if (len < bytes_to_read)
1570 break;
1571
1572 curr_dst += bytes_read;
1573 curr_addr += bytes_read;
1574 bytes_left -= bytes_read;
1575 address = Address(curr_addr);
1576 }
1577 }
1578 else
1579 {
1580 if (dst == NULL)
1581 result_error.SetErrorString("invalid arguments");
1582 else
1583 result_error.Clear();
1584 }
1585 return total_cstr_len;
1586}
1587
1588size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001589Target::ReadScalarIntegerFromMemory (const Address& addr,
1590 bool prefer_file_cache,
1591 uint32_t byte_size,
1592 bool is_signed,
1593 Scalar &scalar,
1594 Error &error)
1595{
1596 uint64_t uval;
1597
1598 if (byte_size <= sizeof(uval))
1599 {
1600 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1601 if (bytes_read == byte_size)
1602 {
1603 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001604 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001605 if (byte_size <= 4)
1606 scalar = data.GetMaxU32 (&offset, byte_size);
1607 else
1608 scalar = data.GetMaxU64 (&offset, byte_size);
1609
1610 if (is_signed)
1611 scalar.SignExtend(byte_size * 8);
1612 return bytes_read;
1613 }
1614 }
1615 else
1616 {
1617 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1618 }
1619 return 0;
1620}
1621
1622uint64_t
1623Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1624 bool prefer_file_cache,
1625 size_t integer_byte_size,
1626 uint64_t fail_value,
1627 Error &error)
1628{
1629 Scalar scalar;
1630 if (ReadScalarIntegerFromMemory (addr,
1631 prefer_file_cache,
1632 integer_byte_size,
1633 false,
1634 scalar,
1635 error))
1636 return scalar.ULongLong(fail_value);
1637 return fail_value;
1638}
1639
1640bool
1641Target::ReadPointerFromMemory (const Address& addr,
1642 bool prefer_file_cache,
1643 Error &error,
1644 Address &pointer_addr)
1645{
1646 Scalar scalar;
1647 if (ReadScalarIntegerFromMemory (addr,
1648 prefer_file_cache,
1649 m_arch.GetAddressByteSize(),
1650 false,
1651 scalar,
1652 error))
1653 {
1654 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1655 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1656 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001657 SectionLoadList &section_load_list = GetSectionLoadList();
1658 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001659 {
1660 // No sections are loaded, so we must assume we are not running
1661 // yet and anything we are given is a file address.
1662 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1663 }
1664 else
1665 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001666 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001667 // we have manually loaded some sections with "target modules load ..."
1668 // or because we have have a live process that has sections loaded
1669 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001670 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001671 }
1672 // We weren't able to resolve the pointer value, so just return
1673 // an address with no section
1674 if (!pointer_addr.IsValid())
1675 pointer_addr.SetOffset (pointer_vm_addr);
1676 return true;
1677
1678 }
1679 }
1680 return false;
1681}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001682
1683ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001684Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001685{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686 ModuleSP module_sp;
1687
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688 Error error;
1689
Jim Ingham4a94c912012-05-17 18:38:42 +00001690 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1691 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001692
Jim Ingham4a94c912012-05-17 18:38:42 +00001693 if (module_spec.GetUUID().IsValid())
1694 module_sp = m_images.FindFirstModule(module_spec);
1695
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001696 if (!module_sp)
1697 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001698 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1699 bool did_create_module = false;
1700
1701 // If there are image search path entries, try to use them first to acquire a suitable image.
1702 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001703 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001704 ModuleSpec transformed_spec (module_spec);
1705 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1706 {
1707 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1708 error = ModuleList::GetSharedModule (transformed_spec,
1709 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001710 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001711 &old_module_sp,
1712 &did_create_module);
1713 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001714 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001715
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001716 if (!module_sp)
1717 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001718 // If we have a UUID, we can check our global shared module list in case
1719 // we already have it. If we don't have a valid UUID, then we can't since
1720 // the path in "module_spec" will be a platform path, and we will need to
1721 // let the platform find that file. For example, we could be asking for
1722 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1723 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1724 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1725 // cache.
1726 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001727 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001728 // We have a UUID, it is OK to check the global module list...
1729 error = ModuleList::GetSharedModule (module_spec,
1730 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001731 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001732 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001733 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001734 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001735
1736 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001737 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001738 // The platform is responsible for finding and caching an appropriate
1739 // module in the shared module cache.
1740 if (m_platform_sp)
1741 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001742 error = m_platform_sp->GetSharedModule (module_spec,
1743 m_process_sp.get(),
1744 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001745 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001746 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001747 &did_create_module);
1748 }
1749 else
1750 {
1751 error.SetErrorString("no platform is currently set");
1752 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001753 }
1754 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001755
Jim Ingham4a94c912012-05-17 18:38:42 +00001756 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1757 // module in the list already, and if there was, let's remove it.
1758 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001759 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001760 ObjectFile *objfile = module_sp->GetObjectFile();
1761 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001762 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001763 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001764 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001765 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1766 case ObjectFile::eTypeExecutable: /// A normal executable
1767 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1768 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1769 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1770 break;
1771 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1772 if (error_ptr)
1773 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1774 return ModuleSP();
1775 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1776 if (error_ptr)
1777 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1778 return ModuleSP();
1779 default:
1780 if (error_ptr)
1781 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1782 return ModuleSP();
1783 }
1784 // GetSharedModule is not guaranteed to find the old shared module, for instance
1785 // in the common case where you pass in the UUID, it is only going to find the one
1786 // module matching the UUID. In fact, it has no good way to know what the "old module"
1787 // relevant to this target is, since there might be many copies of a module with this file spec
1788 // in various running debug sessions, but only one of them will belong to this target.
1789 // So let's remove the UUID from the module list, and look in the target's module list.
1790 // Only do this if there is SOMETHING else in the module spec...
1791 if (!old_module_sp)
1792 {
1793 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001794 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001795 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1796 module_spec_copy.GetUUID().Clear();
1797
1798 ModuleList found_modules;
1799 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1800 if (num_found == 1)
1801 {
1802 old_module_sp = found_modules.GetModuleAtIndex(0);
1803 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001804 }
1805 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001806
1807 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1808 {
1809 m_images.ReplaceModule(old_module_sp, module_sp);
1810 Module *old_module_ptr = old_module_sp.get();
1811 old_module_sp.reset();
1812 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1813 }
1814 else
1815 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001816 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001817 else
1818 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819 }
1820 }
1821 if (error_ptr)
1822 *error_ptr = error;
1823 return module_sp;
1824}
1825
1826
Greg Claytond9e416c2012-02-18 05:35:26 +00001827TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001828Target::CalculateTarget ()
1829{
Greg Claytond9e416c2012-02-18 05:35:26 +00001830 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001831}
1832
Greg Claytond9e416c2012-02-18 05:35:26 +00001833ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001834Target::CalculateProcess ()
1835{
Greg Claytond9e416c2012-02-18 05:35:26 +00001836 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001837}
1838
Greg Claytond9e416c2012-02-18 05:35:26 +00001839ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001840Target::CalculateThread ()
1841{
Greg Claytond9e416c2012-02-18 05:35:26 +00001842 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001843}
1844
Jason Molendab57e4a12013-11-04 09:33:30 +00001845StackFrameSP
1846Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847{
Jason Molendab57e4a12013-11-04 09:33:30 +00001848 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001849}
1850
1851void
Greg Clayton0603aa92010-10-04 01:05:56 +00001852Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853{
Greg Claytonc14ee322011-09-22 04:58:26 +00001854 exe_ctx.Clear();
1855 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001856}
1857
1858PathMappingList &
1859Target::GetImageSearchPathList ()
1860{
1861 return m_image_search_paths;
1862}
1863
1864void
1865Target::ImageSearchPathsChanged
1866(
1867 const PathMappingList &path_list,
1868 void *baton
1869)
1870{
1871 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001872 ModuleSP exe_module_sp (target->GetExecutableModule());
1873 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001874 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875}
1876
1877ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001878Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001879{
Greg Clayton73da2442011-08-03 01:23:55 +00001880 // Now see if we know the target triple, and if so, create our scratch AST context:
Johnny Chen60e2c6a2011-11-30 23:18:53 +00001881 if (m_scratch_ast_context_ap.get() == NULL && m_arch.IsValid() && create_on_demand)
Sean Callanan4bf80d52011-11-15 22:27:19 +00001882 {
Greg Clayton73da2442011-08-03 01:23:55 +00001883 m_scratch_ast_context_ap.reset (new ClangASTContext(m_arch.GetTriple().str().c_str()));
Greg Claytone1cd1be2012-01-29 20:56:30 +00001884 m_scratch_ast_source_ap.reset (new ClangASTSource(shared_from_this()));
Sean Callanan4bf80d52011-11-15 22:27:19 +00001885 m_scratch_ast_source_ap->InstallASTContext(m_scratch_ast_context_ap->getASTContext());
Todd Fiala955fe6f2014-02-27 17:18:23 +00001886 llvm::IntrusiveRefCntPtr<clang::ExternalASTSource> proxy_ast_source(m_scratch_ast_source_ap->CreateProxy());
Sean Callanan4bf80d52011-11-15 22:27:19 +00001887 m_scratch_ast_context_ap->SetExternalSource(proxy_ast_source);
1888 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001889 return m_scratch_ast_context_ap.get();
1890}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001891
Sean Callanan686b2312011-11-16 18:20:47 +00001892ClangASTImporter *
1893Target::GetClangASTImporter()
1894{
1895 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
1896
1897 if (!ast_importer)
1898 {
1899 ast_importer = new ClangASTImporter();
1900 m_ast_importer_ap.reset(ast_importer);
1901 }
1902
1903 return ast_importer;
1904}
1905
Greg Clayton99d0faf2010-11-18 23:32:35 +00001906void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001907Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00001908{
Greg Clayton6920b522012-08-22 18:39:03 +00001909 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001910}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001911
Greg Clayton99d0faf2010-11-18 23:32:35 +00001912void
Caroline Tice20bd37f2011-03-10 22:14:10 +00001913Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00001914{
Greg Clayton6920b522012-08-22 18:39:03 +00001915 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00001916}
Caroline Ticedaccaa92010-09-20 20:44:43 +00001917
Greg Claytonc859e2d2012-02-13 23:10:39 +00001918FileSpecList
1919Target::GetDefaultExecutableSearchPaths ()
1920{
Greg Clayton67cc0632012-08-22 17:17:09 +00001921 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1922 if (properties_sp)
1923 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00001924 return FileSpecList();
1925}
1926
Michael Sartaina7499c92013-07-01 19:45:50 +00001927FileSpecList
1928Target::GetDefaultDebugFileSearchPaths ()
1929{
1930 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1931 if (properties_sp)
1932 return properties_sp->GetDebugFileSearchPaths();
1933 return FileSpecList();
1934}
1935
Sean Callanan85054342015-04-03 15:39:47 +00001936FileSpecList
1937Target::GetDefaultClangModuleSearchPaths ()
1938{
1939 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1940 if (properties_sp)
1941 return properties_sp->GetClangModuleSearchPaths();
1942 return FileSpecList();
1943}
1944
Caroline Ticedaccaa92010-09-20 20:44:43 +00001945ArchSpec
1946Target::GetDefaultArchitecture ()
1947{
Greg Clayton67cc0632012-08-22 17:17:09 +00001948 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1949 if (properties_sp)
1950 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00001951 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00001952}
1953
1954void
Greg Clayton67cc0632012-08-22 17:17:09 +00001955Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00001956{
Greg Clayton67cc0632012-08-22 17:17:09 +00001957 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
1958 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00001959 {
1960 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 +00001961 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00001962 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00001963}
1964
Greg Clayton0603aa92010-10-04 01:05:56 +00001965Target *
1966Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
1967{
1968 // The target can either exist in the "process" of ExecutionContext, or in
1969 // the "target_sp" member of SymbolContext. This accessor helper function
1970 // will get the target from one of these locations.
1971
1972 Target *target = NULL;
1973 if (sc_ptr != NULL)
1974 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00001975 if (target == NULL && exe_ctx_ptr)
1976 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00001977 return target;
1978}
1979
Jim Ingham1624a2d2014-05-05 02:26:40 +00001980ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001981Target::EvaluateExpression
1982(
1983 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00001984 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00001985 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001986 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001987)
1988{
Enrico Granata97fca502012-09-18 17:43:16 +00001989 result_valobj_sp.reset();
1990
Jim Ingham8646d3c2014-05-05 02:47:44 +00001991 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001992
Greg Claytond1767f02011-12-08 02:13:16 +00001993 if (expr_cstr == NULL || expr_cstr[0] == '\0')
1994 return execution_results;
1995
Jim Ingham6026ca32011-05-12 02:06:14 +00001996 // We shouldn't run stop hooks in expressions.
1997 // Be sure to reset this if you return anywhere within this function.
1998 bool old_suppress_value = m_suppress_stop_hooks;
1999 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002000
2001 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002002
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002003 if (frame)
2004 {
2005 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002006 }
2007 else if (m_process_sp)
2008 {
2009 m_process_sp->CalculateExecutionContext(exe_ctx);
2010 }
2011 else
2012 {
2013 CalculateExecutionContext(exe_ctx);
2014 }
2015
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002016 // Make sure we aren't just trying to see the value of a persistent
2017 // variable (something like "$0")
2018 lldb::ClangExpressionVariableSP persistent_var_sp;
2019 // Only check for persistent variables the expression starts with a '$'
2020 if (expr_cstr[0] == '$')
Zachary Turner32abc6e2015-03-03 19:23:09 +00002021 persistent_var_sp = m_persistent_variables->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002022
2023 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002024 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002025 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002026 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002027 }
2028 else
2029 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002030 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002031 Error error;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002032 execution_results = ClangUserExpression::Evaluate (exe_ctx,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002033 options,
2034 expr_cstr,
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002035 prefix,
2036 result_valobj_sp,
Greg Clayton62afb9f2013-11-04 19:35:17 +00002037 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002038 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002039
2040 m_suppress_stop_hooks = old_suppress_value;
2041
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002042 return execution_results;
2043}
2044
Zachary Turner32abc6e2015-03-03 19:23:09 +00002045ClangPersistentVariables &
2046Target::GetPersistentVariables()
2047{
2048 return *m_persistent_variables;
2049}
2050
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002051lldb::addr_t
2052Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2053{
2054 addr_t code_addr = load_addr;
2055 switch (m_arch.GetMachine())
2056 {
2057 case llvm::Triple::arm:
2058 case llvm::Triple::thumb:
2059 switch (addr_class)
2060 {
2061 case eAddressClassData:
2062 case eAddressClassDebug:
2063 return LLDB_INVALID_ADDRESS;
2064
2065 case eAddressClassUnknown:
2066 case eAddressClassInvalid:
2067 case eAddressClassCode:
2068 case eAddressClassCodeAlternateISA:
2069 case eAddressClassRuntime:
2070 // Check if bit zero it no set?
2071 if ((code_addr & 1ull) == 0)
2072 {
2073 // Bit zero isn't set, check if the address is a multiple of 2?
2074 if (code_addr & 2ull)
2075 {
2076 // The address is a multiple of 2 so it must be thumb, set bit zero
2077 code_addr |= 1ull;
2078 }
2079 else if (addr_class == eAddressClassCodeAlternateISA)
2080 {
2081 // We checked the address and the address claims to be the alternate ISA
2082 // which means thumb, so set bit zero.
2083 code_addr |= 1ull;
2084 }
2085 }
2086 break;
2087 }
2088 break;
2089
2090 default:
2091 break;
2092 }
2093 return code_addr;
2094}
2095
2096lldb::addr_t
2097Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2098{
2099 addr_t opcode_addr = load_addr;
2100 switch (m_arch.GetMachine())
2101 {
2102 case llvm::Triple::arm:
2103 case llvm::Triple::thumb:
2104 switch (addr_class)
2105 {
2106 case eAddressClassData:
2107 case eAddressClassDebug:
2108 return LLDB_INVALID_ADDRESS;
2109
2110 case eAddressClassInvalid:
2111 case eAddressClassUnknown:
2112 case eAddressClassCode:
2113 case eAddressClassCodeAlternateISA:
2114 case eAddressClassRuntime:
2115 opcode_addr &= ~(1ull);
2116 break;
2117 }
2118 break;
2119
2120 default:
2121 break;
2122 }
2123 return opcode_addr;
2124}
2125
Greg Clayton9585fbf2013-03-19 00:20:55 +00002126SourceManager &
2127Target::GetSourceManager ()
2128{
2129 if (m_source_manager_ap.get() == NULL)
2130 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2131 return *m_source_manager_ap;
2132}
2133
Sean Callanan9998acd2014-12-05 01:21:59 +00002134ClangModulesDeclVendor *
2135Target::GetClangModulesDeclVendor ()
2136{
2137 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2138
2139 {
2140 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2141
2142 if (!m_clang_modules_decl_vendor_ap)
2143 {
2144 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2145 }
2146 }
2147
2148 return m_clang_modules_decl_vendor_ap.get();
2149}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002150
Greg Clayton44d93782014-01-27 23:43:24 +00002151Target::StopHookSP
2152Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002153{
2154 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002155 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2156 m_stop_hooks[new_uid] = stop_hook_sp;
2157 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002158}
2159
2160bool
2161Target::RemoveStopHookByID (lldb::user_id_t user_id)
2162{
2163 size_t num_removed;
2164 num_removed = m_stop_hooks.erase (user_id);
2165 if (num_removed == 0)
2166 return false;
2167 else
2168 return true;
2169}
2170
2171void
2172Target::RemoveAllStopHooks ()
2173{
2174 m_stop_hooks.clear();
2175}
2176
2177Target::StopHookSP
2178Target::GetStopHookByID (lldb::user_id_t user_id)
2179{
2180 StopHookSP found_hook;
2181
2182 StopHookCollection::iterator specified_hook_iter;
2183 specified_hook_iter = m_stop_hooks.find (user_id);
2184 if (specified_hook_iter != m_stop_hooks.end())
2185 found_hook = (*specified_hook_iter).second;
2186 return found_hook;
2187}
2188
2189bool
2190Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
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 return false;
2196
2197 (*specified_hook_iter).second->SetIsActive (active_state);
2198 return true;
2199}
2200
2201void
2202Target::SetAllStopHooksActiveState (bool active_state)
2203{
2204 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2205 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2206 {
2207 (*pos).second->SetIsActive (active_state);
2208 }
2209}
2210
2211void
2212Target::RunStopHooks ()
2213{
Jim Ingham6026ca32011-05-12 02:06:14 +00002214 if (m_suppress_stop_hooks)
2215 return;
2216
Jim Ingham9575d842011-03-11 03:53:59 +00002217 if (!m_process_sp)
2218 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002219
2220 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2221 // since in that case we do not want to run the stop-hooks
2222 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2223 return;
2224
Jim Ingham9575d842011-03-11 03:53:59 +00002225 if (m_stop_hooks.empty())
2226 return;
2227
2228 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2229
2230 // If there aren't any active stop hooks, don't bother either:
2231 bool any_active_hooks = false;
2232 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2233 {
2234 if ((*pos).second->IsActive())
2235 {
2236 any_active_hooks = true;
2237 break;
2238 }
2239 }
2240 if (!any_active_hooks)
2241 return;
2242
2243 CommandReturnObject result;
2244
2245 std::vector<ExecutionContext> exc_ctx_with_reasons;
2246 std::vector<SymbolContext> sym_ctx_with_reasons;
2247
2248 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2249 size_t num_threads = cur_threadlist.GetSize();
2250 for (size_t i = 0; i < num_threads; i++)
2251 {
2252 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2253 if (cur_thread_sp->ThreadStoppedForAReason())
2254 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002255 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002256 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2257 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2258 }
2259 }
2260
2261 // If no threads stopped for a reason, don't run the stop-hooks.
2262 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2263 if (num_exe_ctx == 0)
2264 return;
2265
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002266 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2267 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002268
2269 bool keep_going = true;
2270 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002271 bool print_hook_header;
2272 bool print_thread_header;
2273
2274 if (num_exe_ctx == 1)
2275 print_thread_header = false;
2276 else
2277 print_thread_header = true;
2278
2279 if (m_stop_hooks.size() == 1)
2280 print_hook_header = false;
2281 else
2282 print_hook_header = true;
2283
Jim Ingham9575d842011-03-11 03:53:59 +00002284 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2285 {
2286 // result.Clear();
2287 StopHookSP cur_hook_sp = (*pos).second;
2288 if (!cur_hook_sp->IsActive())
2289 continue;
2290
2291 bool any_thread_matched = false;
2292 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2293 {
2294 if ((cur_hook_sp->GetSpecifier () == NULL
2295 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2296 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002297 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002298 {
2299 if (!hooks_ran)
2300 {
Jim Ingham9575d842011-03-11 03:53:59 +00002301 hooks_ran = true;
2302 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002303 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002304 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002305 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2306 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2307 NULL);
2308 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002309 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002310 else
Daniel Malead01b2952012-11-29 21:49:15 +00002311 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002312 any_thread_matched = true;
2313 }
2314
Jim Ingham381e25b2011-03-22 01:47:27 +00002315 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002316 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002317
2318 CommandInterpreterRunOptions options;
2319 options.SetStopOnContinue (true);
2320 options.SetStopOnError (true);
2321 options.SetEchoCommands (false);
2322 options.SetPrintResults (true);
2323 options.SetAddToHistory (false);
2324
2325 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2326 &exc_ctx_with_reasons[i],
2327 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002328 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002329
2330 // If the command started the target going again, we should bag out of
2331 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002332 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2333 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002334 {
Daniel Malead01b2952012-11-29 21:49:15 +00002335 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002336 keep_going = false;
2337 }
2338 }
2339 }
2340 }
Jason Molenda879cf772011-09-23 00:42:55 +00002341
Caroline Tice969ed3d2011-05-02 20:41:46 +00002342 result.GetImmediateOutputStream()->Flush();
2343 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002344}
2345
Greg Claytonfbb76342013-11-20 21:07:01 +00002346const TargetPropertiesSP &
2347Target::GetGlobalProperties()
2348{
2349 static TargetPropertiesSP g_settings_sp;
2350 if (!g_settings_sp)
2351 {
2352 g_settings_sp.reset (new TargetProperties (NULL));
2353 }
2354 return g_settings_sp;
2355}
2356
2357Error
2358Target::Install (ProcessLaunchInfo *launch_info)
2359{
2360 Error error;
2361 PlatformSP platform_sp (GetPlatform());
2362 if (platform_sp)
2363 {
2364 if (platform_sp->IsRemote())
2365 {
2366 if (platform_sp->IsConnected())
2367 {
2368 // Install all files that have an install path, and always install the
2369 // main executable when connected to a remote platform
2370 const ModuleList& modules = GetImages();
2371 const size_t num_images = modules.GetSize();
2372 for (size_t idx = 0; idx < num_images; ++idx)
2373 {
2374 const bool is_main_executable = idx == 0;
2375 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2376 if (module_sp)
2377 {
2378 FileSpec local_file (module_sp->GetFileSpec());
2379 if (local_file)
2380 {
2381 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2382 if (!remote_file)
2383 {
2384 if (is_main_executable) // TODO: add setting for always installing main executable???
2385 {
2386 // Always install the main executable
Chaoren Linf34f4102015-05-09 01:21:32 +00002387 remote_file = FileSpec(module_sp->GetFileSpec().GetFilename().AsCString(),
2388 false, module_sp->GetArchitecture());
Greg Claytonfbb76342013-11-20 21:07:01 +00002389 remote_file.GetDirectory() = platform_sp->GetWorkingDirectory();
Greg Claytonfbb76342013-11-20 21:07:01 +00002390 }
2391 }
2392 if (remote_file)
2393 {
2394 error = platform_sp->Install(local_file, remote_file);
2395 if (error.Success())
2396 {
2397 module_sp->SetPlatformFileSpec(remote_file);
2398 if (is_main_executable)
2399 {
Chaoren Lince36c4c2015-05-05 18:43:19 +00002400 platform_sp->SetFilePermissions(remote_file.GetPath(false).c_str(), 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002401 if (launch_info)
2402 launch_info->SetExecutableFile(remote_file, false);
2403 }
2404 }
2405 else
2406 break;
2407 }
2408 }
2409 }
2410 }
2411 }
2412 }
2413 }
2414 return error;
2415}
Greg Clayton7b242382011-07-08 00:48:09 +00002416
Greg Claytond5944cd2013-12-06 01:12:00 +00002417bool
2418Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2419{
2420 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2421}
2422
2423bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002424Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2425{
2426 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2427}
2428
2429bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002430Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2431{
2432 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2433 if (old_section_load_addr != new_section_load_addr)
2434 {
2435 uint32_t stop_id = 0;
2436 ProcessSP process_sp(GetProcessSP());
2437 if (process_sp)
2438 stop_id = process_sp->GetStopID();
2439 else
2440 stop_id = m_section_load_history.GetLastStopID();
2441 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2442 return true; // Return true if the section load address was changed...
2443 }
2444 return false; // Return false to indicate nothing changed
2445
2446}
2447
Greg Clayton8012cad2014-11-17 19:39:20 +00002448size_t
2449Target::UnloadModuleSections (const ModuleList &module_list)
2450{
2451 size_t section_unload_count = 0;
2452 size_t num_modules = module_list.GetSize();
2453 for (size_t i=0; i<num_modules; ++i)
2454 {
2455 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2456 }
2457 return section_unload_count;
2458}
2459
2460size_t
2461Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2462{
2463 uint32_t stop_id = 0;
2464 ProcessSP process_sp(GetProcessSP());
2465 if (process_sp)
2466 stop_id = process_sp->GetStopID();
2467 else
2468 stop_id = m_section_load_history.GetLastStopID();
2469 SectionList *sections = module_sp->GetSectionList();
2470 size_t section_unload_count = 0;
2471 if (sections)
2472 {
2473 const uint32_t num_sections = sections->GetNumSections(0);
2474 for (uint32_t i = 0; i < num_sections; ++i)
2475 {
2476 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2477 }
2478 }
2479 return section_unload_count;
2480}
2481
Greg Claytond5944cd2013-12-06 01:12:00 +00002482bool
2483Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2484{
2485 uint32_t stop_id = 0;
2486 ProcessSP process_sp(GetProcessSP());
2487 if (process_sp)
2488 stop_id = process_sp->GetStopID();
2489 else
2490 stop_id = m_section_load_history.GetLastStopID();
2491 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2492}
2493
2494bool
2495Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2496{
2497 uint32_t stop_id = 0;
2498 ProcessSP process_sp(GetProcessSP());
2499 if (process_sp)
2500 stop_id = process_sp->GetStopID();
2501 else
2502 stop_id = m_section_load_history.GetLastStopID();
2503 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2504}
2505
2506void
2507Target::ClearAllLoadedSections ()
2508{
2509 m_section_load_history.Clear();
2510}
2511
Greg Claytonb09c5382013-12-13 17:20:18 +00002512
2513Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002514Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002515{
2516 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002517 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2518
2519 if (log)
2520 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2521
Greg Claytonb09c5382013-12-13 17:20:18 +00002522 StateType state = eStateInvalid;
2523
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002524 // Scope to temporarily get the process state in case someone has manually
2525 // remotely connected already to a process and we can skip the platform
2526 // launching.
2527 {
2528 ProcessSP process_sp (GetProcessSP());
2529
2530 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002531 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002532 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002533 if (log)
2534 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2535 }
2536 else
2537 {
2538 if (log)
2539 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2540 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002541 }
2542
Greg Claytonb09c5382013-12-13 17:20:18 +00002543 launch_info.GetFlags().Set (eLaunchFlagDebug);
2544
2545 // Get the value of synchronous execution here. If you wait till after you have started to
2546 // run, then you could have hit a breakpoint, whose command might switch the value, and
2547 // then you'll pick up that incorrect value.
2548 Debugger &debugger = GetDebugger();
2549 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2550
2551 PlatformSP platform_sp (GetPlatform());
2552
2553 // Finalize the file actions, and if none were given, default to opening
2554 // up a pseudo terminal
2555 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002556 if (log)
2557 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2558 __FUNCTION__,
2559 platform_sp ? "true" : "false",
2560 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2561 default_to_use_pty ? "true" : "false");
2562
Greg Claytonb09c5382013-12-13 17:20:18 +00002563 launch_info.FinalizeFileActions (this, default_to_use_pty);
2564
2565 if (state == eStateConnected)
2566 {
2567 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2568 {
2569 error.SetErrorString("can't launch in tty when launching through a remote connection");
2570 return error;
2571 }
2572 }
2573
2574 if (!launch_info.GetArchitecture().IsValid())
2575 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002576
2577 // 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 +00002578 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2579 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002580 if (log)
2581 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2582
Greg Claytonb09c5382013-12-13 17:20:18 +00002583 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2584 debugger,
2585 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002586 error);
2587 }
2588 else
2589 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002590 if (log)
2591 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2592
Greg Claytonb09c5382013-12-13 17:20:18 +00002593 if (state == eStateConnected)
2594 {
2595 assert(m_process_sp);
2596 }
2597 else
2598 {
Todd Fiala015d8182014-07-22 23:41:36 +00002599 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00002600 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00002601 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002602 }
Todd Fiala015d8182014-07-22 23:41:36 +00002603
2604 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00002605 if (m_process_sp)
2606 error = m_process_sp->Launch (launch_info);
2607 }
2608
2609 if (!m_process_sp)
2610 {
2611 if (error.Success())
2612 error.SetErrorString("failed to launch or debug process");
2613 return error;
2614 }
2615
2616 if (error.Success())
2617 {
Ilia K064e69f2015-03-23 21:16:25 +00002618 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00002619 {
Ilia K064e69f2015-03-23 21:16:25 +00002620 EventSP event_sp;
Greg Clayton44d93782014-01-27 23:43:24 +00002621 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00002622 if (!hijack_listener_sp)
2623 {
2624 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
2625 launch_info.SetHijackListener(hijack_listener_sp);
2626 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
2627 }
Todd Fialaac33cc92014-10-09 01:02:08 +00002628
Ilia K064e69f2015-03-23 21:16:25 +00002629 StateType state = m_process_sp->WaitForProcessToStop (NULL, &event_sp, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00002630
2631 if (state == eStateStopped)
2632 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002633 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00002634 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002635 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00002636 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002637 error = m_process_sp->PrivateResume();
2638 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00002639 {
Ilia K064e69f2015-03-23 21:16:25 +00002640 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
2641 const bool must_be_alive = false; // eStateExited is ok, so this must be false
2642 if (!StateIsStoppedState(state, must_be_alive))
2643 {
2644 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
2645 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002646 }
2647 }
Ilia K064e69f2015-03-23 21:16:25 +00002648 else
2649 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002650 m_process_sp->RestoreProcessEvents();
2651 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00002652 }
2653 if (!error.Success())
2654 {
Ilia K064e69f2015-03-23 21:16:25 +00002655 Error error2;
2656 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
2657 error = error2;
2658 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002659 }
2660 else
2661 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00002662 assert(synchronous_execution && launch_info.GetFlags().Test(eLaunchFlagStopAtEntry));
Ilia K064e69f2015-03-23 21:16:25 +00002663
2664 // Target was stopped at entry as was intended. Need to notify the listeners about it.
2665 m_process_sp->RestoreProcessEvents();
2666 m_process_sp->HandlePrivateEvent(event_sp);
Greg Claytonb09c5382013-12-13 17:20:18 +00002667 }
2668 }
Greg Clayton40286e02014-04-30 20:29:09 +00002669 else if (state == eStateExited)
2670 {
Zachary Turner10687b02014-10-20 17:46:43 +00002671 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00002672 const int exit_status = m_process_sp->GetExitStatus();
2673 const char *exit_desc = m_process_sp->GetExitDescription();
2674#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'."
2675 if (exit_desc && exit_desc[0])
2676 {
2677 if (with_shell)
2678 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
2679 else
2680 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
2681 }
2682 else
2683 {
2684 if (with_shell)
2685 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
2686 else
2687 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
2688 }
2689 }
Greg Claytonb09c5382013-12-13 17:20:18 +00002690 else
2691 {
2692 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
2693 }
2694 }
Greg Clayton44d93782014-01-27 23:43:24 +00002695 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00002696 }
2697 else
2698 {
Greg Clayton44d93782014-01-27 23:43:24 +00002699 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002700 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00002701 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00002702 }
2703 return error;
2704}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002705
2706Error
2707Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
2708{
2709 auto state = eStateInvalid;
2710 auto process_sp = GetProcessSP ();
2711 if (process_sp)
2712 {
2713 state = process_sp->GetState ();
2714 if (process_sp->IsAlive () && state != eStateConnected)
2715 {
2716 if (state == eStateAttaching)
2717 return Error ("process attach is in progress");
2718 return Error ("a process is already being debugged");
2719 }
2720 }
2721
2722 ListenerSP hijack_listener_sp (new Listener ("lldb.Target.Attach.attach.hijack"));
2723 attach_info.SetHijackListener (hijack_listener_sp);
2724
2725 const ModuleSP old_exec_module_sp = GetExecutableModule ();
2726
2727 // If no process info was specified, then use the target executable
2728 // name as the process to attach to by default
2729 if (!attach_info.ProcessInfoSpecified ())
2730 {
2731 if (old_exec_module_sp)
2732 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
2733
2734 if (!attach_info.ProcessInfoSpecified ())
2735 {
2736 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
2737 }
2738 }
2739
2740 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
2741
2742 Error error;
2743 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
2744 {
2745 SetPlatform (platform_sp);
2746 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
2747 }
2748 else
2749 {
2750 if (state != eStateConnected)
2751 {
2752 const char *plugin_name = attach_info.GetProcessPluginName ();
2753 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
2754 if (process_sp == nullptr)
2755 {
2756 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
2757 return error;
2758 }
2759 }
2760 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
2761 error = process_sp->Attach (attach_info);
2762 }
2763
2764 if (error.Success () && process_sp)
2765 {
2766 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
2767 process_sp->RestoreProcessEvents ();
2768
2769 if (state != eStateStopped)
2770 {
2771 const char *exit_desc = process_sp->GetExitDescription ();
2772 if (exit_desc)
2773 error.SetErrorStringWithFormat ("attach failed: %s", exit_desc);
2774 else
2775 error.SetErrorString ("attach failed: process did not stop (no such process or permission problem?)");
Jason Molendaede31932015-04-17 05:01:58 +00002776 process_sp->Destroy (false);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002777 }
2778 }
2779 return error;
2780}
2781
Jim Ingham9575d842011-03-11 03:53:59 +00002782//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00002783// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00002784//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00002785Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
2786 UserID (uid),
2787 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00002788 m_commands (),
2789 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00002790 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002791 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00002792{
2793}
2794
2795Target::StopHook::StopHook (const StopHook &rhs) :
2796 UserID (rhs.GetID()),
2797 m_target_sp (rhs.m_target_sp),
2798 m_commands (rhs.m_commands),
2799 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00002800 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002801 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00002802{
2803 if (rhs.m_thread_spec_ap.get() != NULL)
2804 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
2805}
2806
2807
2808Target::StopHook::~StopHook ()
2809{
2810}
2811
2812void
Zachary Turner32abc6e2015-03-03 19:23:09 +00002813Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
2814{
2815 m_specifier_sp.reset(specifier);
2816}
2817
2818void
Jim Ingham9575d842011-03-11 03:53:59 +00002819Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
2820{
2821 m_thread_spec_ap.reset (specifier);
2822}
2823
2824
2825void
2826Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
2827{
2828 int indent_level = s->GetIndentLevel();
2829
2830 s->SetIndentLevel(indent_level + 2);
2831
Daniel Malead01b2952012-11-29 21:49:15 +00002832 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002833 if (m_active)
2834 s->Indent ("State: enabled\n");
2835 else
2836 s->Indent ("State: disabled\n");
2837
2838 if (m_specifier_sp)
2839 {
2840 s->Indent();
2841 s->PutCString ("Specifier:\n");
2842 s->SetIndentLevel (indent_level + 4);
2843 m_specifier_sp->GetDescription (s, level);
2844 s->SetIndentLevel (indent_level + 2);
2845 }
2846
2847 if (m_thread_spec_ap.get() != NULL)
2848 {
2849 StreamString tmp;
2850 s->Indent("Thread:\n");
2851 m_thread_spec_ap->GetDescription (&tmp, level);
2852 s->SetIndentLevel (indent_level + 4);
2853 s->Indent (tmp.GetData());
2854 s->PutCString ("\n");
2855 s->SetIndentLevel (indent_level + 2);
2856 }
2857
2858 s->Indent ("Commands: \n");
2859 s->SetIndentLevel (indent_level + 4);
2860 uint32_t num_commands = m_commands.GetSize();
2861 for (uint32_t i = 0; i < num_commands; i++)
2862 {
2863 s->Indent(m_commands.GetStringAtIndex(i));
2864 s->PutCString ("\n");
2865 }
2866 s->SetIndentLevel (indent_level);
2867}
2868
Greg Clayton67cc0632012-08-22 17:17:09 +00002869//--------------------------------------------------------------
2870// class TargetProperties
2871//--------------------------------------------------------------
2872
2873OptionEnumValueElement
2874lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002875{
Greg Clayton67cc0632012-08-22 17:17:09 +00002876 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
2877 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
2878 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
2879 { 0, NULL, NULL }
2880};
Caroline Ticedaccaa92010-09-20 20:44:43 +00002881
Greg Clayton1f746072012-08-29 21:13:06 +00002882static OptionEnumValueElement
2883g_inline_breakpoint_enums[] =
2884{
2885 { 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."},
2886 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
2887 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
2888 { 0, NULL, NULL }
2889};
2890
Jim Ingham0f063ba2013-03-02 00:26:47 +00002891typedef enum x86DisassemblyFlavor
2892{
2893 eX86DisFlavorDefault,
2894 eX86DisFlavorIntel,
2895 eX86DisFlavorATT
2896} x86DisassemblyFlavor;
2897
2898static OptionEnumValueElement
2899g_x86_dis_flavor_value_types[] =
2900{
2901 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
2902 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
2903 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
2904 { 0, NULL, NULL }
2905};
2906
Enrico Granata397ddd52013-05-21 20:13:34 +00002907static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00002908g_hex_immediate_style_values[] =
2909{
2910 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
2911 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
2912 { 0, NULL, NULL }
2913};
2914
2915static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00002916g_load_script_from_sym_file_values[] =
2917{
2918 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
2919 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
2920 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
2921 { 0, NULL, NULL }
2922};
2923
Greg Claytonfd814c52013-08-13 01:42:25 +00002924
2925static OptionEnumValueElement
2926g_memory_module_load_level_values[] =
2927{
Greg Clayton86eac942013-08-13 21:32:34 +00002928 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00002929 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
2930 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
2931 { 0, NULL, NULL }
2932};
2933
Greg Clayton67cc0632012-08-22 17:17:09 +00002934static PropertyDefinition
2935g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00002936{
Greg Clayton67cc0632012-08-22 17:17:09 +00002937 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
Ilia K055ad9b2015-05-18 13:41:01 +00002938 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Move breakpoints to nearest code." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002939 { "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 +00002940 { "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 +00002941 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
2942 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
2943 { "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 "
2944 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
2945 "some part (starting at the root) of the path to the file when it was built, "
2946 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
2947 "Each element of the array is checked in order and the first one that results in a match wins." },
2948 { "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 +00002949 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating debug symbol files." },
Sean Callanan85054342015-04-03 15:39:47 +00002950 { "clang-module-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , NULL, NULL, "List of directories to be searched when locating modules for Clang." },
Sean Callananf0c5aeb2015-04-20 16:31:29 +00002951 { "auto-import-clang-modules" , OptionValue::eTypeBoolean , false, false , NULL, NULL, "Automatically load Clang modules referred to by the program." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002952 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
2953 { "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 +00002954 { "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 +00002955 { "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 +00002956 { "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." },
2957 { "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 +00002958 { "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." },
2959 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
2960 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
2961 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
2962 { "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 +00002963 { "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 +00002964 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
2965 { "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 +00002966 { "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 +00002967 "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. "
2968 "Usually this is limitted to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
2969 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00002970 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
2971 "and find that setting breakpoints is slow, then you can change this setting to headers. "
2972 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00002973 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00002974 // 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.
2975 { "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 +00002976 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
2977 { "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 +00002978 { "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 +00002979 { "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 +00002980 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
2981 "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. "
2982 "This setting helps users control how much information gets loaded when loading modules from memory."
2983 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
2984 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
2985 "'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 +00002986 { "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 +00002987 { "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 +00002988 { "display-runtime-support-values" , OptionValue::eTypeBoolean , false, false, NULL, NULL, "If true, LLDB will show variables that are meant to support the operation of a language's runtime support." },
Ewan Crawford78baa192015-05-13 09:18:18 +00002989 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, NULL, NULL, "Disable lock-step debugging, instead control threads independently." },
Greg Clayton67cc0632012-08-22 17:17:09 +00002990 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
2991};
Enrico Granata560558e2015-02-11 02:35:39 +00002992
Greg Clayton67cc0632012-08-22 17:17:09 +00002993enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00002994{
Greg Clayton67cc0632012-08-22 17:17:09 +00002995 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00002996 ePropertyMoveToNearestCode,
Greg Clayton67cc0632012-08-22 17:17:09 +00002997 ePropertyExprPrefix,
2998 ePropertyPreferDynamic,
2999 ePropertyEnableSynthetic,
3000 ePropertySkipPrologue,
3001 ePropertySourceMap,
3002 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003003 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003004 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003005 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003006 ePropertyMaxChildrenCount,
3007 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003008 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003009 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003010 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003011 ePropertyRunArgs,
3012 ePropertyEnvVars,
3013 ePropertyInheritEnv,
3014 ePropertyInputPath,
3015 ePropertyOutputPath,
3016 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003017 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003018 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003019 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003020 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003021 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003022 ePropertyUseHexImmediates,
3023 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003024 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003025 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003026 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003027 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003028 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003029 ePropertyDisplayRuntimeSupportValues,
3030 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003031};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003032
Caroline Ticedaccaa92010-09-20 20:44:43 +00003033
Greg Clayton67cc0632012-08-22 17:17:09 +00003034class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003035{
Greg Clayton67cc0632012-08-22 17:17:09 +00003036public:
3037 TargetOptionValueProperties (const ConstString &name) :
3038 OptionValueProperties (name),
3039 m_target (NULL),
3040 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003041 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003042 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003043
Greg Clayton67cc0632012-08-22 17:17:09 +00003044 // This constructor is used when creating TargetOptionValueProperties when it
3045 // is part of a new lldb_private::Target instance. It will copy all current
3046 // global property values as needed
3047 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3048 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3049 m_target (target),
3050 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003051 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003052 }
3053
3054 virtual const Property *
3055 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3056 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003057 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003058 // try and grab the setting from the current target if there is one. Else we just
3059 // use the one from this instance.
3060 if (idx == ePropertyEnvVars)
3061 GetHostEnvironmentIfNeeded ();
3062
3063 if (exe_ctx)
3064 {
3065 Target *target = exe_ctx->GetTargetPtr();
3066 if (target)
3067 {
3068 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3069 if (this != target_properties)
3070 return target_properties->ProtectedGetPropertyAtIndex (idx);
3071 }
3072 }
3073 return ProtectedGetPropertyAtIndex (idx);
3074 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003075
3076 lldb::TargetSP
3077 GetTargetSP ()
3078 {
3079 return m_target->shared_from_this();
3080 }
3081
Greg Clayton67cc0632012-08-22 17:17:09 +00003082protected:
3083
3084 void
3085 GetHostEnvironmentIfNeeded () const
3086 {
3087 if (!m_got_host_env)
3088 {
3089 if (m_target)
3090 {
3091 m_got_host_env = true;
3092 const uint32_t idx = ePropertyInheritEnv;
3093 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3094 {
3095 PlatformSP platform_sp (m_target->GetPlatform());
3096 if (platform_sp)
3097 {
3098 StringList env;
3099 if (platform_sp->GetEnvironment(env))
3100 {
3101 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3102 if (env_dict)
3103 {
3104 const bool can_replace = false;
3105 const size_t envc = env.GetSize();
3106 for (size_t idx=0; idx<envc; idx++)
3107 {
3108 const char *env_entry = env.GetStringAtIndex (idx);
3109 if (env_entry)
3110 {
3111 const char *equal_pos = ::strchr(env_entry, '=');
3112 ConstString key;
3113 // It is ok to have environment variables with no values
3114 const char *value = NULL;
3115 if (equal_pos)
3116 {
3117 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3118 if (equal_pos[1])
3119 value = equal_pos + 1;
3120 }
3121 else
3122 {
3123 key.SetCString(env_entry);
3124 }
3125 // Don't allow existing keys to be replaced with ones we get from the platform environment
3126 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3127 }
3128 }
3129 }
3130 }
3131 }
3132 }
3133 }
3134 }
3135 }
3136 Target *m_target;
3137 mutable bool m_got_host_env;
3138};
3139
Greg Claytonfbb76342013-11-20 21:07:01 +00003140//----------------------------------------------------------------------
3141// TargetProperties
3142//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003143TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003144 Properties (),
3145 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003146{
3147 if (target)
3148 {
3149 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003150
3151 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3152 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3153 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3154 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3155 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3156 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3157 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3158 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3159 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3160 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3161
3162 // Update m_launch_info once it was created
3163 Arg0ValueChangedCallback(this, NULL);
3164 RunArgsValueChangedCallback(this, NULL);
3165 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3166 InputPathValueChangedCallback(this, NULL);
3167 OutputPathValueChangedCallback(this, NULL);
3168 ErrorPathValueChangedCallback(this, NULL);
3169 DetachOnErrorValueChangedCallback(this, NULL);
3170 DisableASLRValueChangedCallback(this, NULL);
3171 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003172 }
3173 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003174 {
3175 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3176 m_collection_sp->Initialize(g_properties);
3177 m_collection_sp->AppendProperty(ConstString("process"),
3178 ConstString("Settings specify to processes."),
3179 true,
3180 Process::GetGlobalProperties()->GetValueProperties());
3181 }
Ilia K8f37ca52015-02-13 14:31:06 +00003182
Caroline Ticedaccaa92010-09-20 20:44:43 +00003183}
3184
Greg Clayton67cc0632012-08-22 17:17:09 +00003185TargetProperties::~TargetProperties ()
3186{
3187}
3188ArchSpec
3189TargetProperties::GetDefaultArchitecture () const
3190{
3191 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3192 if (value)
3193 return value->GetCurrentValue();
3194 return ArchSpec();
3195}
3196
3197void
3198TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3199{
3200 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3201 if (value)
3202 return value->SetCurrentValue(arch, true);
3203}
3204
Ilia K055ad9b2015-05-18 13:41:01 +00003205bool
3206TargetProperties::GetMoveToNearestCode() const
3207{
3208 const uint32_t idx = ePropertyMoveToNearestCode;
3209 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3210}
3211
Greg Clayton67cc0632012-08-22 17:17:09 +00003212lldb::DynamicValueType
3213TargetProperties::GetPreferDynamicValue() const
3214{
3215 const uint32_t idx = ePropertyPreferDynamic;
3216 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3217}
3218
3219bool
Greg Clayton15484402015-05-15 18:40:24 +00003220TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3221{
3222 const uint32_t idx = ePropertyPreferDynamic;
3223 return m_collection_sp->SetPropertyAtIndexAsEnumeration(NULL, idx, d);
3224}
3225
3226
3227bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003228TargetProperties::GetDisableASLR () const
3229{
3230 const uint32_t idx = ePropertyDisableASLR;
3231 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3232}
3233
3234void
3235TargetProperties::SetDisableASLR (bool b)
3236{
3237 const uint32_t idx = ePropertyDisableASLR;
3238 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3239}
3240
3241bool
Jim Ingham106d0282014-06-25 02:32:56 +00003242TargetProperties::GetDetachOnError () const
3243{
3244 const uint32_t idx = ePropertyDetachOnError;
3245 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3246}
3247
3248void
3249TargetProperties::SetDetachOnError (bool b)
3250{
3251 const uint32_t idx = ePropertyDetachOnError;
3252 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3253}
3254
3255bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003256TargetProperties::GetDisableSTDIO () const
3257{
3258 const uint32_t idx = ePropertyDisableSTDIO;
3259 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3260}
3261
3262void
3263TargetProperties::SetDisableSTDIO (bool b)
3264{
3265 const uint32_t idx = ePropertyDisableSTDIO;
3266 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3267}
3268
Jim Ingham0f063ba2013-03-02 00:26:47 +00003269const char *
3270TargetProperties::GetDisassemblyFlavor () const
3271{
3272 const uint32_t idx = ePropertyDisassemblyFlavor;
3273 const char *return_value;
3274
3275 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3276 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3277 return return_value;
3278}
3279
Greg Clayton1f746072012-08-29 21:13:06 +00003280InlineStrategy
3281TargetProperties::GetInlineStrategy () const
3282{
3283 const uint32_t idx = ePropertyInlineStrategy;
3284 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3285}
3286
Greg Clayton45392552012-10-17 22:57:12 +00003287const char *
3288TargetProperties::GetArg0 () const
3289{
3290 const uint32_t idx = ePropertyArg0;
3291 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3292}
3293
3294void
3295TargetProperties::SetArg0 (const char *arg)
3296{
3297 const uint32_t idx = ePropertyArg0;
3298 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003299 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003300}
3301
Greg Clayton67cc0632012-08-22 17:17:09 +00003302bool
3303TargetProperties::GetRunArguments (Args &args) const
3304{
3305 const uint32_t idx = ePropertyRunArgs;
3306 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3307}
3308
3309void
3310TargetProperties::SetRunArguments (const Args &args)
3311{
3312 const uint32_t idx = ePropertyRunArgs;
3313 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003314 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003315}
3316
3317size_t
3318TargetProperties::GetEnvironmentAsArgs (Args &env) const
3319{
3320 const uint32_t idx = ePropertyEnvVars;
3321 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3322}
3323
Ilia K8f37ca52015-02-13 14:31:06 +00003324void
3325TargetProperties::SetEnvironmentFromArgs (const Args &env)
3326{
3327 const uint32_t idx = ePropertyEnvVars;
3328 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003329 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003330}
3331
Greg Clayton67cc0632012-08-22 17:17:09 +00003332bool
3333TargetProperties::GetSkipPrologue() const
3334{
3335 const uint32_t idx = ePropertySkipPrologue;
3336 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3337}
3338
3339PathMappingList &
3340TargetProperties::GetSourcePathMap () const
3341{
3342 const uint32_t idx = ePropertySourceMap;
3343 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3344 assert(option_value);
3345 return option_value->GetCurrentValue();
3346}
3347
3348FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003349TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003350{
3351 const uint32_t idx = ePropertyExecutableSearchPaths;
3352 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3353 assert(option_value);
3354 return option_value->GetCurrentValue();
3355}
3356
Michael Sartaina7499c92013-07-01 19:45:50 +00003357FileSpecList &
3358TargetProperties::GetDebugFileSearchPaths ()
3359{
3360 const uint32_t idx = ePropertyDebugFileSearchPaths;
3361 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3362 assert(option_value);
3363 return option_value->GetCurrentValue();
3364}
3365
Sean Callanan85054342015-04-03 15:39:47 +00003366FileSpecList &
3367TargetProperties::GetClangModuleSearchPaths ()
3368{
3369 const uint32_t idx = ePropertyClangModuleSearchPaths;
3370 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3371 assert(option_value);
3372 return option_value->GetCurrentValue();
3373}
3374
Greg Clayton67cc0632012-08-22 17:17:09 +00003375bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003376TargetProperties::GetEnableAutoImportClangModules() const
3377{
3378 const uint32_t idx = ePropertyAutoImportClangModules;
3379 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3380}
3381
3382bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003383TargetProperties::GetEnableSyntheticValue () const
3384{
3385 const uint32_t idx = ePropertyEnableSynthetic;
3386 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3387}
3388
3389uint32_t
3390TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3391{
3392 const uint32_t idx = ePropertyMaxChildrenCount;
3393 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3394}
3395
3396uint32_t
3397TargetProperties::GetMaximumSizeOfStringSummary() const
3398{
3399 const uint32_t idx = ePropertyMaxSummaryLength;
3400 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3401}
3402
Enrico Granatad325bf92013-06-04 22:54:16 +00003403uint32_t
3404TargetProperties::GetMaximumMemReadSize () const
3405{
3406 const uint32_t idx = ePropertyMaxMemReadSize;
3407 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3408}
3409
Greg Clayton67cc0632012-08-22 17:17:09 +00003410FileSpec
3411TargetProperties::GetStandardInputPath () const
3412{
3413 const uint32_t idx = ePropertyInputPath;
3414 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3415}
3416
3417void
3418TargetProperties::SetStandardInputPath (const char *p)
3419{
3420 const uint32_t idx = ePropertyInputPath;
3421 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3422}
3423
3424FileSpec
3425TargetProperties::GetStandardOutputPath () const
3426{
3427 const uint32_t idx = ePropertyOutputPath;
3428 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3429}
3430
3431void
3432TargetProperties::SetStandardOutputPath (const char *p)
3433{
3434 const uint32_t idx = ePropertyOutputPath;
3435 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3436}
3437
3438FileSpec
3439TargetProperties::GetStandardErrorPath () const
3440{
3441 const uint32_t idx = ePropertyErrorPath;
3442 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3443}
3444
Greg Clayton6920b522012-08-22 18:39:03 +00003445const char *
3446TargetProperties::GetExpressionPrefixContentsAsCString ()
3447{
3448 const uint32_t idx = ePropertyExprPrefix;
3449 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3450 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003451 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003452 const bool null_terminate = true;
3453 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003454 if (data_sp)
3455 return (const char *) data_sp->GetBytes();
3456 }
Greg Clayton6920b522012-08-22 18:39:03 +00003457 return NULL;
3458}
3459
Greg Clayton67cc0632012-08-22 17:17:09 +00003460void
3461TargetProperties::SetStandardErrorPath (const char *p)
3462{
3463 const uint32_t idx = ePropertyErrorPath;
3464 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3465}
3466
3467bool
3468TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3469{
3470 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3471 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3472}
3473
Jim Ingham17d023f2013-03-13 17:58:04 +00003474bool
Daniel Malead79ae052013-08-07 21:54:09 +00003475TargetProperties::GetUseHexImmediates () const
3476{
3477 const uint32_t idx = ePropertyUseHexImmediates;
3478 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3479}
3480
3481bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003482TargetProperties::GetUseFastStepping () const
3483{
3484 const uint32_t idx = ePropertyUseFastStepping;
3485 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3486}
3487
Greg Claytonfb6621e2013-12-06 21:59:52 +00003488bool
3489TargetProperties::GetDisplayExpressionsInCrashlogs () const
3490{
3491 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3492 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3493}
3494
Enrico Granata397ddd52013-05-21 20:13:34 +00003495LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003496TargetProperties::GetLoadScriptFromSymbolFile () const
3497{
3498 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003499 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003500}
3501
Daniel Malead79ae052013-08-07 21:54:09 +00003502Disassembler::HexImmediateStyle
3503TargetProperties::GetHexImmediateStyle () const
3504{
3505 const uint32_t idx = ePropertyHexImmediateStyle;
3506 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3507}
3508
Greg Claytonfd814c52013-08-13 01:42:25 +00003509MemoryModuleLoadLevel
3510TargetProperties::GetMemoryModuleLoadLevel() const
3511{
3512 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3513 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3514}
3515
Jason Molendaa4bea722014-02-14 05:06:49 +00003516bool
3517TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3518{
3519 const uint32_t idx = ePropertyTrapHandlerNames;
3520 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3521}
Greg Claytonfd814c52013-08-13 01:42:25 +00003522
Jason Molendaa4bea722014-02-14 05:06:49 +00003523void
3524TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3525{
3526 const uint32_t idx = ePropertyTrapHandlerNames;
3527 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3528}
Greg Clayton67cc0632012-08-22 17:17:09 +00003529
Enrico Granata560558e2015-02-11 02:35:39 +00003530bool
3531TargetProperties::GetDisplayRuntimeSupportValues () const
3532{
3533 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3534 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3535}
3536
3537void
3538TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3539{
3540 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3541 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3542}
3543
Ewan Crawford78baa192015-05-13 09:18:18 +00003544bool
3545TargetProperties::GetNonStopModeEnabled () const
3546{
3547 const uint32_t idx = ePropertyNonStopModeEnabled;
3548 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3549}
3550
Ilia K8f37ca52015-02-13 14:31:06 +00003551const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003552TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003553{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003554 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003555 return m_launch_info;
3556}
3557
3558void
3559TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3560{
3561 m_launch_info = launch_info;
3562 SetArg0(launch_info.GetArg0());
3563 SetRunArguments(launch_info.GetArguments());
3564 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3565 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3566 if (input_file_action)
3567 {
3568 const char *input_path = input_file_action->GetPath();
3569 if (input_path)
3570 SetStandardInputPath(input_path);
3571 }
3572 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3573 if (output_file_action)
3574 {
3575 const char *output_path = output_file_action->GetPath();
3576 if (output_path)
3577 SetStandardOutputPath(output_path);
3578 }
3579 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3580 if (error_file_action)
3581 {
3582 const char *error_path = error_file_action->GetPath();
3583 if (error_path)
3584 SetStandardErrorPath(error_path);
3585 }
3586 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
3587 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
3588 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
3589}
3590
3591void
3592TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
3593{
3594 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3595 this_->m_launch_info.SetArg0(this_->GetArg0());
3596}
3597
3598void
3599TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
3600{
3601 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3602 Args args;
3603 if (this_->GetRunArguments(args))
3604 this_->m_launch_info.GetArguments() = args;
3605}
3606
3607void
3608TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
3609{
3610 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3611 Args args;
3612 if (this_->GetEnvironmentAsArgs(args))
3613 this_->m_launch_info.GetEnvironmentEntries() = args;
3614}
3615
3616void
3617TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3618{
3619 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3620 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath().GetPath().c_str(), true, false);
3621}
3622
3623void
3624TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3625{
3626 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3627 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath().GetPath().c_str(), false, true);
3628}
3629
3630void
3631TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
3632{
3633 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3634 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath().GetPath().c_str(), false, true);
3635}
3636
3637void
3638TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
3639{
3640 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3641 if (this_->GetDetachOnError())
3642 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
3643 else
3644 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
3645}
3646
3647void
3648TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
3649{
3650 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3651 if (this_->GetDisableASLR())
3652 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
3653 else
3654 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
3655}
3656
3657void
3658TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
3659{
3660 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
3661 if (this_->GetDisableSTDIO())
3662 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
3663 else
3664 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
3665}
Ilia Keb2c19a2015-03-10 21:59:55 +00003666
3667//----------------------------------------------------------------------
3668// Target::TargetEventData
3669//----------------------------------------------------------------------
3670
3671Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
3672 EventData (),
3673 m_target_sp (target_sp),
3674 m_module_list ()
3675{
3676}
3677
3678Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
3679 EventData (),
3680 m_target_sp (target_sp),
3681 m_module_list (module_list)
3682{
3683}
3684
3685Target::TargetEventData::~TargetEventData()
3686{
3687}
3688
3689const ConstString &
3690Target::TargetEventData::GetFlavorString ()
3691{
3692 static ConstString g_flavor ("Target::TargetEventData");
3693 return g_flavor;
3694}
3695
3696void
3697Target::TargetEventData::Dump (Stream *s) const
3698{
3699}
3700
3701const Target::TargetEventData *
3702Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
3703{
3704 if (event_ptr)
3705 {
3706 const EventData *event_data = event_ptr->GetData();
3707 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
3708 return static_cast <const TargetEventData *> (event_ptr->GetData());
3709 }
3710 return NULL;
3711}
3712
3713TargetSP
3714Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
3715{
3716 TargetSP target_sp;
3717 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
3718 if (event_data)
3719 target_sp = event_data->m_target_sp;
3720 return target_sp;
3721}
3722
3723ModuleList
3724Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
3725{
3726 ModuleList module_list;
3727 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
3728 if (event_data)
3729 module_list = event_data->m_module_list;
3730 return module_list;
3731}