blob: 92af4969540713f2aa8f7a1a901307d547f31ace [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Target/Target.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000028#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000029#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000030#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Sean Callanan66810412015-10-19 23:11:07 +000034#include "lldb/Expression/REPL.h"
Jim Ingham151c0322015-09-15 21:13:50 +000035#include "lldb/Expression/UserExpression.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000036#include "Plugins/ExpressionParser/Clang/ClangASTSource.h"
37#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
38#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000039#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000041#include "lldb/Interpreter/CommandInterpreter.h"
42#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000043#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000044#include "lldb/Interpreter/OptionValues.h"
45#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000046#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Symbol/ObjectFile.h"
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000048#include "lldb/Symbol/Function.h"
49#include "lldb/Symbol/Symbol.h"
Jim Ingham151c0322015-09-15 21:13:50 +000050#include "lldb/Target/Language.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000051#include "lldb/Target/LanguageRuntime.h"
52#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000054#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000055#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000056#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000057#include "lldb/Target/Thread.h"
58#include "lldb/Target/ThreadSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
60using namespace lldb;
61using namespace lldb_private;
62
Jim Ingham4bddaeb2012-02-16 06:50:00 +000063ConstString &
64Target::GetStaticBroadcasterClass ()
65{
66 static ConstString class_name ("lldb.target");
67 return class_name;
68}
69
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070//----------------------------------------------------------------------
71// Target constructor
72//----------------------------------------------------------------------
Jim Ingham893c9322014-11-22 01:42:44 +000073Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000074 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000075 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000076 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000077 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000078 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000079 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000080 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000081 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000082 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083 m_breakpoint_list (false),
84 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000085 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000086 m_process_sp (),
87 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000088 m_image_search_paths (ImageSearchPathsChanged, this),
Greg Claytone01e07b2013-04-18 18:10:51 +000089 m_ast_importer_ap (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000090 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000091 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000092 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000093 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000094 m_suppress_stop_hooks (false),
95 m_is_dummy_target(is_dummy_target)
96
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000098 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
99 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
100 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +0000101 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +0000102 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000103
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000104 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000105
Greg Clayton5160ce52013-03-27 23:08:40 +0000106 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000108 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000109 if (m_arch.IsValid())
110 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000111 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 +0000112 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113}
114
Jim Ingham893c9322014-11-22 01:42:44 +0000115void
116Target::PrimeFromDummyTarget(Target *target)
117{
118 if (!target)
119 return;
120
121 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000122
123 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
124 {
125 if (breakpoint_sp->IsInternal())
126 continue;
127
128 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
129 AddBreakpoint (new_bp, false);
130 }
Jim Ingham893c9322014-11-22 01:42:44 +0000131}
132
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133//----------------------------------------------------------------------
134// Destructor
135//----------------------------------------------------------------------
136Target::~Target()
137{
Greg Clayton5160ce52013-03-27 23:08:40 +0000138 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000140 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141 DeleteCurrentProcess ();
142}
143
144void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000145Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146{
Greg Clayton89411422010-10-08 00:21:05 +0000147// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000148 if (description_level != lldb::eDescriptionLevelBrief)
149 {
150 s->Indent();
151 s->PutCString("Target\n");
152 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000153 m_images.Dump(s);
154 m_breakpoint_list.Dump(s);
155 m_internal_breakpoint_list.Dump(s);
156 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000157 }
158 else
159 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000160 Module *exe_module = GetExecutableModulePointer();
161 if (exe_module)
162 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000163 else
164 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000165 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166}
167
168void
Greg Clayton90ba8112012-12-05 00:16:59 +0000169Target::CleanupProcess ()
170{
171 // Do any cleanup of the target we need to do between process instances.
172 // NB It is better to do this before destroying the process in case the
173 // clean up needs some help from the process.
174 m_breakpoint_list.ClearAllBreakpointSites();
175 m_internal_breakpoint_list.ClearAllBreakpointSites();
176 // Disable watchpoints just on the debugger side.
177 Mutex::Locker locker;
178 this->GetWatchpointList().GetListMutex(locker);
179 DisableAllWatchpoints(false);
180 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000181 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000182}
183
184void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185Target::DeleteCurrentProcess ()
186{
187 if (m_process_sp.get())
188 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000189 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 if (m_process_sp->IsAlive())
Jason Molendaede31932015-04-17 05:01:58 +0000191 m_process_sp->Destroy(false);
Jim Inghamd0a3e122011-02-16 17:54:55 +0000192
193 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194
Greg Clayton90ba8112012-12-05 00:16:59 +0000195 CleanupProcess ();
196
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197 m_process_sp.reset();
198 }
199}
200
201const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000202Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203{
204 DeleteCurrentProcess ();
Zachary Turner7529df92015-09-01 20:02:29 +0000205 m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 return m_process_sp;
207}
208
209const lldb::ProcessSP &
210Target::GetProcessSP () const
211{
212 return m_process_sp;
213}
214
Sean Callanan66810412015-10-19 23:11:07 +0000215lldb::REPLSP
216Target::GetREPL (lldb::LanguageType language, bool can_create)
217{
218 if (language == eLanguageTypeUnknown)
219 {
220 return REPLSP(); // must provide a language
221 }
222
223 REPLMap::iterator pos = m_repl_map.find(language);
224
225 if (pos != m_repl_map.end())
226 {
227 return pos->second;
228 }
229
230 if (!can_create)
231 {
232 return lldb::REPLSP();
233 }
234
235 lldb::REPLSP ret = REPL::Create(language, this);
236
237 if (ret)
238 {
239 m_repl_map[language] = ret;
240 return m_repl_map[language];
241 }
242
243 return nullptr;
244}
245
Greg Clayton3418c852011-08-10 02:10:13 +0000246void
247Target::Destroy()
248{
249 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000250 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000251 DeleteCurrentProcess ();
252 m_platform_sp.reset();
253 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000254 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000255 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000256 const bool notify = false;
257 m_breakpoint_list.RemoveAll(notify);
258 m_internal_breakpoint_list.RemoveAll(notify);
259 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000260 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000261 m_search_filter_sp.reset();
262 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000263 m_stop_hooks.clear();
264 m_stop_hook_next_id = 0;
265 m_suppress_stop_hooks = false;
266}
267
268
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269BreakpointList &
270Target::GetBreakpointList(bool internal)
271{
272 if (internal)
273 return m_internal_breakpoint_list;
274 else
275 return m_breakpoint_list;
276}
277
278const BreakpointList &
279Target::GetBreakpointList(bool internal) const
280{
281 if (internal)
282 return m_internal_breakpoint_list;
283 else
284 return m_breakpoint_list;
285}
286
287BreakpointSP
288Target::GetBreakpointByID (break_id_t break_id)
289{
290 BreakpointSP bp_sp;
291
292 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
293 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
294 else
295 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
296
297 return bp_sp;
298}
299
300BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000301Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000302 const FileSpecList *source_file_spec_list,
303 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000304 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000305 bool hardware,
306 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000307{
Jim Ingham87df91b2011-09-23 00:54:11 +0000308 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000309 if (move_to_nearest_code == eLazyBoolCalculate)
310 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
311 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex, !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000312 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000313}
314
315
316BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000317Target::CreateBreakpoint (const FileSpecList *containingModules,
318 const FileSpec &file,
319 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000320 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000321 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000322 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000323 bool hardware,
324 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000325{
Greg Clayton1f746072012-08-29 21:13:06 +0000326 if (check_inlines == eLazyBoolCalculate)
327 {
328 const InlineStrategy inline_strategy = GetInlineStrategy();
329 switch (inline_strategy)
330 {
331 case eInlineBreakpointsNever:
332 check_inlines = eLazyBoolNo;
333 break;
334
335 case eInlineBreakpointsHeaders:
336 if (file.IsSourceImplementationFile())
337 check_inlines = eLazyBoolNo;
338 else
339 check_inlines = eLazyBoolYes;
340 break;
341
342 case eInlineBreakpointsAlways:
343 check_inlines = eLazyBoolYes;
344 break;
345 }
346 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000347 SearchFilterSP filter_sp;
348 if (check_inlines == eLazyBoolNo)
349 {
350 // Not checking for inlines, we are looking only for matching compile units
351 FileSpecList compile_unit_list;
352 compile_unit_list.Append (file);
353 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
354 }
355 else
356 {
357 filter_sp = GetSearchFilterForModuleList (containingModules);
358 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000359 if (skip_prologue == eLazyBoolCalculate)
360 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000361 if (move_to_nearest_code == eLazyBoolCalculate)
362 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000363
Greg Clayton1f746072012-08-29 21:13:06 +0000364 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
365 file,
366 line_no,
367 check_inlines,
Ilia K055ad9b2015-05-18 13:41:01 +0000368 skip_prologue,
369 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000370 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371}
372
373
374BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000375Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000378
379 // Check for any reason we want to move this breakpoint to other address.
380 addr = GetBreakableLoadAddress(addr);
381
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 // Attempt to resolve our load address if possible, though it is ok if
383 // it doesn't resolve to section/offset.
384
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000385 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000386 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000387 if (!so_addr.IsValid())
388 {
389 // The address didn't resolve, so just set this as an absolute address
390 so_addr.SetOffset (addr);
391 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000392 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 return bp_sp;
394}
395
396BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000397Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000399 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000401 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000402}
403
404BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000405Target::CreateBreakpoint (const FileSpecList *containingModules,
406 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000407 const char *func_name,
408 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000409 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000410 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000411 bool internal,
412 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000414 BreakpointSP bp_sp;
415 if (func_name)
416 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000417 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000418
419 if (skip_prologue == eLazyBoolCalculate)
420 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000421 if (language == lldb::eLanguageTypeUnknown)
422 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000423
Greg Claytond16e1e52011-07-12 17:06:17 +0000424 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
425 func_name,
426 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000427 language,
Greg Claytond16e1e52011-07-12 17:06:17 +0000428 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000429 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000430 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000431 }
432 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433}
434
Jim Inghamfab10e82012-03-06 00:37:27 +0000435lldb::BreakpointSP
436Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000437 const FileSpecList *containingSourceFiles,
438 const std::vector<std::string> &func_names,
439 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000440 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000441 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000442 bool internal,
443 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000444{
445 BreakpointSP bp_sp;
446 size_t num_names = func_names.size();
447 if (num_names > 0)
448 {
449 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000450
451 if (skip_prologue == eLazyBoolCalculate)
452 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000453 if (language == lldb::eLanguageTypeUnknown)
454 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000455
456 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000457 func_names,
458 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000459 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000460 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000461 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000462 }
463 return bp_sp;
464}
465
Jim Ingham133e0fb2012-03-03 02:05:11 +0000466BreakpointSP
467Target::CreateBreakpoint (const FileSpecList *containingModules,
468 const FileSpecList *containingSourceFiles,
469 const char *func_names[],
470 size_t num_names,
471 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000472 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000473 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000474 bool internal,
475 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000476{
477 BreakpointSP bp_sp;
478 if (num_names > 0)
479 {
480 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
481
Greg Clayton03da4cc2013-04-19 21:31:16 +0000482 if (skip_prologue == eLazyBoolCalculate)
483 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000484 if (language == lldb::eLanguageTypeUnknown)
485 language = GetLanguage();
486
Greg Clayton03da4cc2013-04-19 21:31:16 +0000487
488 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000489 func_names,
490 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000491 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000492 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000493 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000494 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000495 }
496 return bp_sp;
497}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498
499SearchFilterSP
500Target::GetSearchFilterForModule (const FileSpec *containingModule)
501{
502 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503 if (containingModule != NULL)
504 {
505 // TODO: We should look into sharing module based search filters
506 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000507 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508 }
509 else
510 {
511 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000512 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 filter_sp = m_search_filter_sp;
514 }
515 return filter_sp;
516}
517
Jim Ingham969795f2011-09-21 01:17:13 +0000518SearchFilterSP
519Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
520{
521 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000522 if (containingModules && containingModules->GetSize() != 0)
523 {
524 // TODO: We should look into sharing module based search filters
525 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000526 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000527 }
528 else
529 {
530 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000531 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000532 filter_sp = m_search_filter_sp;
533 }
534 return filter_sp;
535}
536
Jim Ingham87df91b2011-09-23 00:54:11 +0000537SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000538Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
539 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000540{
541 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
542 return GetSearchFilterForModuleList(containingModules);
543
544 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000545 if (containingModules == NULL)
546 {
547 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
548 // but that will take a little reworking.
549
Greg Claytone1cd1be2012-01-29 20:56:30 +0000550 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000551 }
552 else
553 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000554 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000555 }
556 return filter_sp;
557}
558
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000560Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000561 const FileSpecList *containingSourceFiles,
562 RegularExpression &func_regex,
563 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000564 bool internal,
565 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566{
Jim Ingham87df91b2011-09-23 00:54:11 +0000567 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000568 bool skip =
569 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
570 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000571 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
572 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000573 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574
Jim Ingham1460e4b2014-01-10 23:46:59 +0000575 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576}
577
Jim Ingham219ba192012-03-05 04:47:34 +0000578lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000579Target::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 +0000580{
Jim Inghama72b31c2015-04-22 19:42:18 +0000581 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
582 if (exc_bkpt_sp && additional_args)
583 {
584 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
585 if (precondition_sp && additional_args)
586 {
587 if (error)
588 *error = precondition_sp->ConfigurePrecondition(*additional_args);
589 else
590 precondition_sp->ConfigurePrecondition(*additional_args);
591 }
592 }
593 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000594}
Jim Inghama72b31c2015-04-22 19:42:18 +0000595
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000597Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598{
599 BreakpointSP bp_sp;
600 if (filter_sp && resolver_sp)
601 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000602 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000604 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000605 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000606 return bp_sp;
607}
608
609void
610Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
611{
612 if (!bp_sp)
613 return;
614 if (internal)
615 m_internal_breakpoint_list.Add (bp_sp, false);
616 else
617 m_breakpoint_list.Add (bp_sp, true);
618
619 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
620 if (log)
621 {
622 StreamString s;
623 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
624 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
625 }
626
627 bp_sp->ResolveBreakpoint();
628
629 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000630 {
631 m_last_created_breakpoint = bp_sp;
632 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633}
634
Johnny Chen86364b42011-09-20 23:28:55 +0000635bool
636Target::ProcessIsValid()
637{
638 return (m_process_sp && m_process_sp->IsAlive());
639}
640
Johnny Chenb90827e2012-06-04 23:19:54 +0000641static bool
642CheckIfWatchpointsExhausted(Target *target, Error &error)
643{
644 uint32_t num_supported_hardware_watchpoints;
645 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
646 if (rc.Success())
647 {
648 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
649 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
650 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
651 num_supported_hardware_watchpoints);
652 }
653 return false;
654}
655
Johnny Chen01a67862011-10-14 00:42:25 +0000656// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000657// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000658WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000659Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000660{
Greg Clayton5160ce52013-03-27 23:08:40 +0000661 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000662 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000663 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000664 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000665
Johnny Chen01a67862011-10-14 00:42:25 +0000666 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000667 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000668 {
669 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000670 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000671 }
Jim Inghamc6462312013-06-18 21:52:48 +0000672
Johnny Chen45e541f2011-09-14 22:20:15 +0000673 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000674 {
675 if (size == 0)
676 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
677 else
Daniel Malead01b2952012-11-29 21:49:15 +0000678 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000679 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000680 }
Jim Inghamc6462312013-06-18 21:52:48 +0000681
682 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
683 {
684 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
685 }
Johnny Chen7313a642011-09-13 01:15:36 +0000686
Johnny Chen01a67862011-10-14 00:42:25 +0000687 // Currently we only support one watchpoint per address, with total number
688 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000689
690 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000691 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 +0000692 Mutex::Locker locker;
693 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000694 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000695 if (matched_sp)
696 {
Johnny Chen0c406372011-09-14 20:23:45 +0000697 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000698 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000699 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
700 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000701 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000702 if (size == old_size && kind == old_type)
703 {
Johnny Chen01a67862011-10-14 00:42:25 +0000704 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000705 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000706 }
707 else
708 {
Johnny Chen01a67862011-10-14 00:42:25 +0000709 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000710 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
711 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000712 }
Johnny Chen3c532582011-09-13 23:29:31 +0000713 }
714
Jason Molenda727e3922012-12-05 23:07:34 +0000715 if (!wp_sp)
716 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000717 wp_sp.reset(new Watchpoint(*this, addr, size, type));
718 wp_sp->SetWatchpointType(kind, notify);
719 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000720 }
Johnny Chen0c406372011-09-14 20:23:45 +0000721
Jim Ingham1b5792e2012-12-18 02:03:49 +0000722 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000723 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000724 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
725 __FUNCTION__,
726 error.Success() ? "succeeded" : "failed",
727 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000728
Jason Molenda727e3922012-12-05 23:07:34 +0000729 if (error.Fail())
730 {
Johnny Chen41b77262012-03-26 22:00:10 +0000731 // Enabling the watchpoint on the device side failed.
732 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000733 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000734 // See if we could provide more helpful error message.
735 if (!CheckIfWatchpointsExhausted(this, error))
736 {
737 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000738 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000739 }
Johnny Chen01a67862011-10-14 00:42:25 +0000740 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000741 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000742 else
Johnny Chen01a67862011-10-14 00:42:25 +0000743 m_last_created_watchpoint = wp_sp;
744 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000745}
746
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747void
748Target::RemoveAllBreakpoints (bool internal_also)
749{
Greg Clayton5160ce52013-03-27 23:08:40 +0000750 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 if (log)
752 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
753
Greg Clayton9fed0d82010-07-23 23:33:17 +0000754 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000756 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000757
758 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759}
760
761void
762Target::DisableAllBreakpoints (bool internal_also)
763{
Greg Clayton5160ce52013-03-27 23:08:40 +0000764 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 if (log)
766 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
767
768 m_breakpoint_list.SetEnabledAll (false);
769 if (internal_also)
770 m_internal_breakpoint_list.SetEnabledAll (false);
771}
772
773void
774Target::EnableAllBreakpoints (bool internal_also)
775{
Greg Clayton5160ce52013-03-27 23:08:40 +0000776 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 if (log)
778 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
779
780 m_breakpoint_list.SetEnabledAll (true);
781 if (internal_also)
782 m_internal_breakpoint_list.SetEnabledAll (true);
783}
784
785bool
786Target::RemoveBreakpointByID (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", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
791
792 if (DisableBreakpointByID (break_id))
793 {
794 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000795 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000797 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000798 if (m_last_created_breakpoint)
799 {
800 if (m_last_created_breakpoint->GetID() == break_id)
801 m_last_created_breakpoint.reset();
802 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000803 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000804 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 return true;
806 }
807 return false;
808}
809
810bool
811Target::DisableBreakpointByID (break_id_t break_id)
812{
Greg Clayton5160ce52013-03-27 23:08:40 +0000813 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 if (log)
815 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
816
817 BreakpointSP bp_sp;
818
819 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
820 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
821 else
822 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
823 if (bp_sp)
824 {
825 bp_sp->SetEnabled (false);
826 return true;
827 }
828 return false;
829}
830
831bool
832Target::EnableBreakpointByID (break_id_t break_id)
833{
Greg Clayton5160ce52013-03-27 23:08:40 +0000834 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 if (log)
836 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
837 __FUNCTION__,
838 break_id,
839 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
840
841 BreakpointSP bp_sp;
842
843 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
844 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
845 else
846 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
847
848 if (bp_sp)
849 {
850 bp_sp->SetEnabled (true);
851 return true;
852 }
853 return false;
854}
855
Johnny Chenedf50372011-09-23 21:21:43 +0000856// The flag 'end_to_end', default to true, signifies that the operation is
857// performed end to end, for both the debugger and the debuggee.
858
Johnny Chen01a67862011-10-14 00:42:25 +0000859// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
860// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000861bool
Johnny Chen01a67862011-10-14 00:42:25 +0000862Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000863{
Greg Clayton5160ce52013-03-27 23:08:40 +0000864 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000865 if (log)
866 log->Printf ("Target::%s\n", __FUNCTION__);
867
Johnny Chenedf50372011-09-23 21:21:43 +0000868 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000869 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000870 return true;
871 }
872
873 // Otherwise, it's an end to end operation.
874
Johnny Chen86364b42011-09-20 23:28:55 +0000875 if (!ProcessIsValid())
876 return false;
877
Johnny Chen01a67862011-10-14 00:42:25 +0000878 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000879 for (size_t i = 0; i < num_watchpoints; ++i)
880 {
Johnny Chen01a67862011-10-14 00:42:25 +0000881 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
882 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000883 return false;
884
Johnny Chen01a67862011-10-14 00:42:25 +0000885 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000886 if (rc.Fail())
887 return false;
888 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000889 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000890 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000891 return true; // Success!
892}
893
Johnny Chen01a67862011-10-14 00:42:25 +0000894// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
895// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000896bool
Johnny Chen01a67862011-10-14 00:42:25 +0000897Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000898{
Greg Clayton5160ce52013-03-27 23:08:40 +0000899 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000900 if (log)
901 log->Printf ("Target::%s\n", __FUNCTION__);
902
Johnny Chenedf50372011-09-23 21:21:43 +0000903 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000904 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000905 return true;
906 }
907
908 // Otherwise, it's an end to end operation.
909
Johnny Chen86364b42011-09-20 23:28:55 +0000910 if (!ProcessIsValid())
911 return false;
912
Johnny Chen01a67862011-10-14 00:42:25 +0000913 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000914 for (size_t i = 0; i < num_watchpoints; ++i)
915 {
Johnny Chen01a67862011-10-14 00:42:25 +0000916 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
917 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000918 return false;
919
Johnny Chen01a67862011-10-14 00:42:25 +0000920 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000921 if (rc.Fail())
922 return false;
923 }
Johnny Chen86364b42011-09-20 23:28:55 +0000924 return true; // Success!
925}
926
Johnny Chen01a67862011-10-14 00:42:25 +0000927// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
928// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000929bool
Johnny Chen01a67862011-10-14 00:42:25 +0000930Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000931{
Greg Clayton5160ce52013-03-27 23:08:40 +0000932 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000933 if (log)
934 log->Printf ("Target::%s\n", __FUNCTION__);
935
Johnny Chenedf50372011-09-23 21:21:43 +0000936 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000937 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000938 return true;
939 }
940
941 // Otherwise, it's an end to end operation.
942
Johnny Chen86364b42011-09-20 23:28:55 +0000943 if (!ProcessIsValid())
944 return false;
945
Johnny Chen01a67862011-10-14 00:42:25 +0000946 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000947 for (size_t i = 0; i < num_watchpoints; ++i)
948 {
Johnny Chen01a67862011-10-14 00:42:25 +0000949 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
950 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000951 return false;
952
Johnny Chen01a67862011-10-14 00:42:25 +0000953 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000954 if (rc.Fail())
955 return false;
956 }
Johnny Chen86364b42011-09-20 23:28:55 +0000957 return true; // Success!
958}
959
Johnny Chena4d6bc92012-02-25 06:44:30 +0000960// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
961bool
962Target::ClearAllWatchpointHitCounts ()
963{
Greg Clayton5160ce52013-03-27 23:08:40 +0000964 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000965 if (log)
966 log->Printf ("Target::%s\n", __FUNCTION__);
967
968 size_t num_watchpoints = m_watchpoint_list.GetSize();
969 for (size_t i = 0; i < num_watchpoints; ++i)
970 {
971 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
972 if (!wp_sp)
973 return false;
974
975 wp_sp->ResetHitCount();
976 }
977 return true; // Success!
978}
979
Enrico Granata5e3fe042015-02-11 00:37:54 +0000980// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
981bool
982Target::ClearAllWatchpointHistoricValues ()
983{
984 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
985 if (log)
986 log->Printf ("Target::%s\n", __FUNCTION__);
987
988 size_t num_watchpoints = m_watchpoint_list.GetSize();
989 for (size_t i = 0; i < num_watchpoints; ++i)
990 {
991 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
992 if (!wp_sp)
993 return false;
994
995 wp_sp->ResetHistoricValues();
996 }
997 return true; // Success!
998}
999
Johnny Chen01a67862011-10-14 00:42:25 +00001000// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +00001001// during these operations.
1002bool
Johnny Chen01a67862011-10-14 00:42:25 +00001003Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001004{
Greg Clayton5160ce52013-03-27 23:08:40 +00001005 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001006 if (log)
1007 log->Printf ("Target::%s\n", __FUNCTION__);
1008
1009 if (!ProcessIsValid())
1010 return false;
1011
Johnny Chen01a67862011-10-14 00:42:25 +00001012 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +00001013 for (size_t i = 0; i < num_watchpoints; ++i)
1014 {
Johnny Chen01a67862011-10-14 00:42:25 +00001015 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1016 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001017 return false;
1018
Johnny Chen01a67862011-10-14 00:42:25 +00001019 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001020 }
1021 return true; // Success!
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::DisableWatchpointByID (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
1032 if (!ProcessIsValid())
1033 return false;
1034
Johnny Chen01a67862011-10-14 00:42:25 +00001035 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1036 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001037 {
Johnny Chen01a67862011-10-14 00:42:25 +00001038 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001039 if (rc.Success())
1040 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001041
Johnny Chenf04ee932011-09-22 18:04:58 +00001042 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001043 }
1044 return false;
1045}
1046
Johnny Chen01a67862011-10-14 00:42:25 +00001047// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001048bool
Johnny Chen01a67862011-10-14 00:42:25 +00001049Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001050{
Greg Clayton5160ce52013-03-27 23:08:40 +00001051 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001052 if (log)
1053 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1054
1055 if (!ProcessIsValid())
1056 return false;
1057
Johnny Chen01a67862011-10-14 00:42:25 +00001058 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1059 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001060 {
Johnny Chen01a67862011-10-14 00:42:25 +00001061 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001062 if (rc.Success())
1063 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001064
Johnny Chenf04ee932011-09-22 18:04:58 +00001065 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001066 }
1067 return false;
1068}
1069
Johnny Chen01a67862011-10-14 00:42:25 +00001070// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001071bool
Johnny Chen01a67862011-10-14 00:42:25 +00001072Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001073{
Greg Clayton5160ce52013-03-27 23:08:40 +00001074 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001075 if (log)
1076 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1077
Jim Inghamb0b45132013-07-02 02:09:46 +00001078 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1079 if (watch_to_remove_sp == m_last_created_watchpoint)
1080 m_last_created_watchpoint.reset();
1081
Johnny Chen01a67862011-10-14 00:42:25 +00001082 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001083 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001084 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001085 return true;
1086 }
1087 return false;
1088}
1089
Johnny Chen01a67862011-10-14 00:42:25 +00001090// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001091bool
Johnny Chen01a67862011-10-14 00:42:25 +00001092Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001093{
Greg Clayton5160ce52013-03-27 23:08:40 +00001094 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001095 if (log)
1096 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1097
1098 if (!ProcessIsValid())
1099 return false;
1100
Johnny Chen01a67862011-10-14 00:42:25 +00001101 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1102 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001103 {
Johnny Chen01a67862011-10-14 00:42:25 +00001104 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001105 return true;
1106 }
1107 return false;
1108}
1109
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110ModuleSP
1111Target::GetExecutableModule ()
1112{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001113 // search for the first executable in the module list
1114 for (size_t i = 0; i < m_images.GetSize(); ++i)
1115 {
1116 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1117 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1118 if (obj == nullptr)
1119 continue;
1120 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1121 return module_sp;
1122 }
1123 // as fall back return the first module loaded
1124 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001125}
1126
1127Module*
1128Target::GetExecutableModulePointer ()
1129{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001130 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131}
1132
Enrico Granata17598482012-11-08 02:22:02 +00001133static void
1134LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1135{
1136 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001137 StreamString feedback_stream;
1138 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001139 {
Enrico Granata97303392013-05-21 00:00:30 +00001140 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001141 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001142 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1143 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001144 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001145 if (feedback_stream.GetSize())
1146 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1147 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001148}
1149
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150void
Greg Claytonb35db632013-11-09 00:03:31 +00001151Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001152{
Greg Claytonb35db632013-11-09 00:03:31 +00001153 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001154 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 m_images.Clear();
Greg Clayton5beec212015-10-08 21:04:34 +00001156 m_scratch_type_system_map.Clear();
Sean Callanan686b2312011-11-16 18:20:47 +00001157 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001158}
1159
1160void
Greg Claytonb35db632013-11-09 00:03:31 +00001161Target::DidExec ()
1162{
1163 // When a process exec's we need to know about it so we can do some cleanup.
1164 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1165 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1166}
1167
1168void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001169Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1170{
1171 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001172 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173
1174 if (executable_sp.get())
1175 {
1176 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001177 "Target::SetExecutableModule (executable = '%s')",
1178 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001180 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181
Jim Ingham5aee1622010-08-09 23:31:02 +00001182 // 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 +00001183 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001184 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001185 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001186 if (log)
1187 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1188 }
1189
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001191 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001193 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194 {
1195 executable_objfile->GetDependentModules(dependent_files);
1196 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1197 {
Greg Claytonded470d2011-03-19 01:12:21 +00001198 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1199 FileSpec platform_dependent_file_spec;
1200 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001201 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001202 else
1203 platform_dependent_file_spec = dependent_file_spec;
1204
Greg Claytonb9a01b32012-02-26 05:51:37 +00001205 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1206 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001207 if (image_module_sp.get())
1208 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209 ObjectFile *objfile = image_module_sp->GetObjectFile();
1210 if (objfile)
1211 objfile->GetDependentModules(dependent_files);
1212 }
1213 }
1214 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 }
1216}
1217
1218
Jim Ingham5aee1622010-08-09 23:31:02 +00001219bool
1220Target::SetArchitecture (const ArchSpec &arch_spec)
1221{
Greg Clayton5160ce52013-03-27 23:08:40 +00001222 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001223 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001224 {
Greg Clayton70512312012-05-08 01:45:38 +00001225 // If we haven't got a valid arch spec, or the architectures are
1226 // compatible, so just update the architecture. Architectures can be
1227 // equal, yet the triple OS and vendor might change, so we need to do
1228 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001229 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001230 if (log)
1231 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 +00001232 return true;
1233 }
1234 else
1235 {
1236 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001237 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001238 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 +00001239 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001240 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001241
Greg Claytonb35db632013-11-09 00:03:31 +00001242 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001243 // Need to do something about unsetting breakpoints.
1244
1245 if (executable_sp)
1246 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001247 if (log)
1248 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 +00001249 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1250 Error error = ModuleList::GetSharedModule (module_spec,
1251 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001252 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001253 NULL,
1254 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001255
1256 if (!error.Fail() && executable_sp)
1257 {
1258 SetExecutableModule (executable_sp, true);
1259 return true;
1260 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001261 }
1262 }
Greg Clayton70512312012-05-08 01:45:38 +00001263 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001264}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001265
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001266bool
1267Target::MergeArchitecture (const ArchSpec &arch_spec)
1268{
1269 if (arch_spec.IsValid())
1270 {
1271 if (m_arch.IsCompatibleMatch(arch_spec))
1272 {
1273 // The current target arch is compatible with "arch_spec", see if we
1274 // can improve our current architecture using bits from "arch_spec"
1275
1276 // Merge bits from arch_spec into "merged_arch" and set our architecture
1277 ArchSpec merged_arch (m_arch);
1278 merged_arch.MergeFrom (arch_spec);
1279 return SetArchitecture(merged_arch);
1280 }
1281 else
1282 {
1283 // The new architecture is different, we just need to replace it
1284 return SetArchitecture(arch_spec);
1285 }
1286 }
1287 return false;
1288}
1289
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001290void
Enrico Granataefe637d2012-11-08 19:16:03 +00001291Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001292{
1293}
1294
1295void
Enrico Granataefe637d2012-11-08 19:16:03 +00001296Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001297{
1298 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001299 if (m_valid)
1300 {
1301 ModuleList my_module_list;
1302 my_module_list.Append(module_sp);
1303 LoadScriptingResourceForModule(module_sp, this);
1304 ModulesDidLoad (my_module_list);
1305 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306}
1307
1308void
Enrico Granataefe637d2012-11-08 19:16:03 +00001309Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001310{
Adrian McCarthy6ecdbc82015-10-15 22:39:55 +00001311 // A module is being removed from this target.
Greg Clayton23f8c952014-03-24 23:10:19 +00001312 if (m_valid)
1313 {
1314 ModuleList my_module_list;
1315 my_module_list.Append(module_sp);
1316 ModulesDidUnload (my_module_list, false);
1317 }
Enrico Granata17598482012-11-08 02:22:02 +00001318}
1319
1320void
Enrico Granataefe637d2012-11-08 19:16:03 +00001321Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322{
Jim Inghame716ae02011-08-03 01:00:06 +00001323 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001324 if (m_valid)
1325 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326}
1327
1328void
1329Target::ModulesDidLoad (ModuleList &module_list)
1330{
Greg Clayton23f8c952014-03-24 23:10:19 +00001331 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001332 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001333 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001334 if (m_process_sp)
1335 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001336 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001337 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001338 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001339 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340}
1341
1342void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001343Target::SymbolsDidLoad (ModuleList &module_list)
1344{
Greg Clayton23f8c952014-03-24 23:10:19 +00001345 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001346 {
Jim Ingham31caf982013-06-04 23:01:35 +00001347 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001348 {
Jim Ingham31caf982013-06-04 23:01:35 +00001349 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1350 if (runtime)
1351 {
1352 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1353 objc_runtime->SymbolsDidLoad(module_list);
1354 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001355 }
Jim Ingham31caf982013-06-04 23:01:35 +00001356
Greg Clayton095eeaa2013-11-05 23:28:00 +00001357 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001358 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001359 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001360}
1361
1362void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001363Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001364{
Greg Clayton23f8c952014-03-24 23:10:19 +00001365 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001366 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001367 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001368 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001369 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001370 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371}
1372
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001373bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001374Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001375{
Greg Clayton67cc0632012-08-22 17:17:09 +00001376 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001377 {
1378 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001379 ModuleSpec module_spec (module_file_spec);
1380 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001381
1382 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1383 // black list.
1384 if (num_modules > 0)
1385 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001386 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001387 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001388 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001389 return false;
1390 }
1391 return true;
1392 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001393 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001394 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001395}
1396
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001397bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001398Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001399{
Greg Clayton67cc0632012-08-22 17:17:09 +00001400 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001401 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001402 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001403 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001404 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001405 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001406}
1407
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001408size_t
Greg Claytondb598232011-01-07 01:57:07 +00001409Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1410{
Greg Claytone72dfb32012-02-24 01:59:29 +00001411 SectionSP section_sp (addr.GetSection());
1412 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001413 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001414 // 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 +00001415 if (section_sp->IsEncrypted())
1416 {
Greg Clayton57f06302012-05-25 17:05:55 +00001417 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001418 return 0;
1419 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001420 ModuleSP module_sp (section_sp->GetModule());
1421 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001422 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001423 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1424 if (objfile)
1425 {
1426 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1427 addr.GetOffset(),
1428 dst,
1429 dst_len);
1430 if (bytes_read > 0)
1431 return bytes_read;
1432 else
1433 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1434 }
Greg Claytondb598232011-01-07 01:57:07 +00001435 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001436 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001437 }
1438 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001439 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001440 }
1441 else
Greg Claytondb598232011-01-07 01:57:07 +00001442 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001443
Greg Claytondb598232011-01-07 01:57:07 +00001444 return 0;
1445}
1446
1447size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001448Target::ReadMemory (const Address& addr,
1449 bool prefer_file_cache,
1450 void *dst,
1451 size_t dst_len,
1452 Error &error,
1453 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001454{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001456
Enrico Granata9128ee22011-09-06 19:20:51 +00001457 // if we end up reading this from process memory, we will fill this
1458 // with the actual load address
1459 if (load_addr_ptr)
1460 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1461
Greg Claytondb598232011-01-07 01:57:07 +00001462 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001463
1464 addr_t load_addr = LLDB_INVALID_ADDRESS;
1465 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001466 Address resolved_addr;
1467 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001468 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001469 SectionLoadList &section_load_list = GetSectionLoadList();
1470 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001471 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001472 // No sections are loaded, so we must assume we are not running
1473 // yet and anything we are given is a file address.
1474 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1475 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001476 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001477 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001478 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001479 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001480 // we have manually loaded some sections with "target modules load ..."
1481 // or because we have have a live process that has sections loaded
1482 // through the dynamic loader
1483 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001484 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001485 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001486 }
Greg Clayton357132e2011-03-26 19:14:58 +00001487 if (!resolved_addr.IsValid())
1488 resolved_addr = addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001489
Greg Claytonc749eb82011-07-11 05:12:02 +00001490
Greg Claytondb598232011-01-07 01:57:07 +00001491 if (prefer_file_cache)
1492 {
1493 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1494 if (bytes_read > 0)
1495 return bytes_read;
1496 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001497
Johnny Chen86364b42011-09-20 23:28:55 +00001498 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001499 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001500 if (load_addr == LLDB_INVALID_ADDRESS)
1501 load_addr = resolved_addr.GetLoadAddress (this);
1502
Greg Claytondda4f7b2010-06-30 23:03:03 +00001503 if (load_addr == LLDB_INVALID_ADDRESS)
1504 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001505 ModuleSP addr_module_sp (resolved_addr.GetModule());
1506 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001507 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001508 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001509 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001510 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001511 else
Daniel Malead01b2952012-11-29 21:49:15 +00001512 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001513 }
1514 else
1515 {
Greg Claytondb598232011-01-07 01:57:07 +00001516 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001517 if (bytes_read != dst_len)
1518 {
1519 if (error.Success())
1520 {
1521 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001522 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001523 else
Daniel Malead01b2952012-11-29 21:49:15 +00001524 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 +00001525 }
1526 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001527 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001528 {
1529 if (load_addr_ptr)
1530 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001531 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001532 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001533 // If the address is not section offset we have an address that
1534 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001535 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001536 // more we can do. If it is section offset, we might be able to
1537 // read cached memory from the object file.
1538 if (!resolved_addr.IsSectionOffset())
1539 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001540 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001541 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001542
Greg Claytonc749eb82011-07-11 05:12:02 +00001543 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001544 {
Greg Claytondb598232011-01-07 01:57:07 +00001545 // If we didn't already try and read from the object file cache, then
1546 // try it after failing to read from the process.
1547 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001548 }
1549 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001550}
1551
Greg Claytond16e1e52011-07-12 17:06:17 +00001552size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001553Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1554{
1555 char buf[256];
1556 out_str.clear();
1557 addr_t curr_addr = addr.GetLoadAddress(this);
1558 Address address(addr);
1559 while (1)
1560 {
1561 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1562 if (length == 0)
1563 break;
1564 out_str.append(buf, length);
1565 // If we got "length - 1" bytes, we didn't get the whole C string, we
1566 // need to read some more characters
1567 if (length == sizeof(buf) - 1)
1568 curr_addr += length;
1569 else
1570 break;
1571 address = Address(curr_addr);
1572 }
1573 return out_str.size();
1574}
1575
1576
1577size_t
1578Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1579{
1580 size_t total_cstr_len = 0;
1581 if (dst && dst_max_len)
1582 {
1583 result_error.Clear();
1584 // NULL out everything just to be safe
1585 memset (dst, 0, dst_max_len);
1586 Error error;
1587 addr_t curr_addr = addr.GetLoadAddress(this);
1588 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001589
1590 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1591 // think this really needs to be tied to the memory cache subsystem's
1592 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001593 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001594
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001595 size_t bytes_left = dst_max_len - 1;
1596 char *curr_dst = dst;
1597
1598 while (bytes_left > 0)
1599 {
1600 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1601 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1602 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1603
1604 if (bytes_read == 0)
1605 {
1606 result_error = error;
1607 dst[total_cstr_len] = '\0';
1608 break;
1609 }
1610 const size_t len = strlen(curr_dst);
1611
1612 total_cstr_len += len;
1613
1614 if (len < bytes_to_read)
1615 break;
1616
1617 curr_dst += bytes_read;
1618 curr_addr += bytes_read;
1619 bytes_left -= bytes_read;
1620 address = Address(curr_addr);
1621 }
1622 }
1623 else
1624 {
1625 if (dst == NULL)
1626 result_error.SetErrorString("invalid arguments");
1627 else
1628 result_error.Clear();
1629 }
1630 return total_cstr_len;
1631}
1632
1633size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001634Target::ReadScalarIntegerFromMemory (const Address& addr,
1635 bool prefer_file_cache,
1636 uint32_t byte_size,
1637 bool is_signed,
1638 Scalar &scalar,
1639 Error &error)
1640{
1641 uint64_t uval;
1642
1643 if (byte_size <= sizeof(uval))
1644 {
1645 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1646 if (bytes_read == byte_size)
1647 {
1648 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001649 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001650 if (byte_size <= 4)
1651 scalar = data.GetMaxU32 (&offset, byte_size);
1652 else
1653 scalar = data.GetMaxU64 (&offset, byte_size);
1654
1655 if (is_signed)
1656 scalar.SignExtend(byte_size * 8);
1657 return bytes_read;
1658 }
1659 }
1660 else
1661 {
1662 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1663 }
1664 return 0;
1665}
1666
1667uint64_t
1668Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1669 bool prefer_file_cache,
1670 size_t integer_byte_size,
1671 uint64_t fail_value,
1672 Error &error)
1673{
1674 Scalar scalar;
1675 if (ReadScalarIntegerFromMemory (addr,
1676 prefer_file_cache,
1677 integer_byte_size,
1678 false,
1679 scalar,
1680 error))
1681 return scalar.ULongLong(fail_value);
1682 return fail_value;
1683}
1684
1685bool
1686Target::ReadPointerFromMemory (const Address& addr,
1687 bool prefer_file_cache,
1688 Error &error,
1689 Address &pointer_addr)
1690{
1691 Scalar scalar;
1692 if (ReadScalarIntegerFromMemory (addr,
1693 prefer_file_cache,
1694 m_arch.GetAddressByteSize(),
1695 false,
1696 scalar,
1697 error))
1698 {
1699 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1700 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1701 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001702 SectionLoadList &section_load_list = GetSectionLoadList();
1703 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001704 {
1705 // No sections are loaded, so we must assume we are not running
1706 // yet and anything we are given is a file address.
1707 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1708 }
1709 else
1710 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001711 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001712 // we have manually loaded some sections with "target modules load ..."
1713 // or because we have have a live process that has sections loaded
1714 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001715 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001716 }
1717 // We weren't able to resolve the pointer value, so just return
1718 // an address with no section
1719 if (!pointer_addr.IsValid())
1720 pointer_addr.SetOffset (pointer_vm_addr);
1721 return true;
1722
1723 }
1724 }
1725 return false;
1726}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727
1728ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001729Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001730{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001731 ModuleSP module_sp;
1732
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001733 Error error;
1734
Jim Ingham4a94c912012-05-17 18:38:42 +00001735 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1736 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001737
Jim Ingham4a94c912012-05-17 18:38:42 +00001738 if (module_spec.GetUUID().IsValid())
1739 module_sp = m_images.FindFirstModule(module_spec);
1740
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001741 if (!module_sp)
1742 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001743 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1744 bool did_create_module = false;
1745
1746 // If there are image search path entries, try to use them first to acquire a suitable image.
1747 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001748 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001749 ModuleSpec transformed_spec (module_spec);
1750 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1751 {
1752 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1753 error = ModuleList::GetSharedModule (transformed_spec,
1754 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001755 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001756 &old_module_sp,
1757 &did_create_module);
1758 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001759 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001760
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001761 if (!module_sp)
1762 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001763 // If we have a UUID, we can check our global shared module list in case
1764 // we already have it. If we don't have a valid UUID, then we can't since
1765 // the path in "module_spec" will be a platform path, and we will need to
1766 // let the platform find that file. For example, we could be asking for
1767 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1768 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1769 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1770 // cache.
1771 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001772 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001773 // We have a UUID, it is OK to check the global module list...
1774 error = ModuleList::GetSharedModule (module_spec,
1775 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001776 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001777 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001778 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001779 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001780
1781 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001782 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001783 // The platform is responsible for finding and caching an appropriate
1784 // module in the shared module cache.
1785 if (m_platform_sp)
1786 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001787 error = m_platform_sp->GetSharedModule (module_spec,
1788 m_process_sp.get(),
1789 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001790 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001791 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001792 &did_create_module);
1793 }
1794 else
1795 {
1796 error.SetErrorString("no platform is currently set");
1797 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001798 }
1799 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800
Jim Ingham4a94c912012-05-17 18:38:42 +00001801 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1802 // module in the list already, and if there was, let's remove it.
1803 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001804 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001805 ObjectFile *objfile = module_sp->GetObjectFile();
1806 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001807 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001808 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001809 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001810 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1811 case ObjectFile::eTypeExecutable: /// A normal executable
1812 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1813 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1814 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1815 break;
1816 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1817 if (error_ptr)
1818 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1819 return ModuleSP();
1820 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1821 if (error_ptr)
1822 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1823 return ModuleSP();
1824 default:
1825 if (error_ptr)
1826 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1827 return ModuleSP();
1828 }
1829 // GetSharedModule is not guaranteed to find the old shared module, for instance
1830 // in the common case where you pass in the UUID, it is only going to find the one
1831 // module matching the UUID. In fact, it has no good way to know what the "old module"
1832 // relevant to this target is, since there might be many copies of a module with this file spec
1833 // in various running debug sessions, but only one of them will belong to this target.
1834 // So let's remove the UUID from the module list, and look in the target's module list.
1835 // Only do this if there is SOMETHING else in the module spec...
1836 if (!old_module_sp)
1837 {
1838 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001839 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001840 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1841 module_spec_copy.GetUUID().Clear();
1842
1843 ModuleList found_modules;
1844 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1845 if (num_found == 1)
1846 {
1847 old_module_sp = found_modules.GetModuleAtIndex(0);
1848 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001849 }
1850 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001851
1852 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1853 {
1854 m_images.ReplaceModule(old_module_sp, module_sp);
1855 Module *old_module_ptr = old_module_sp.get();
1856 old_module_sp.reset();
1857 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1858 }
1859 else
1860 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001861 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001862 else
1863 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001864 }
1865 }
1866 if (error_ptr)
1867 *error_ptr = error;
1868 return module_sp;
1869}
1870
1871
Greg Claytond9e416c2012-02-18 05:35:26 +00001872TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873Target::CalculateTarget ()
1874{
Greg Claytond9e416c2012-02-18 05:35:26 +00001875 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001876}
1877
Greg Claytond9e416c2012-02-18 05:35:26 +00001878ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001879Target::CalculateProcess ()
1880{
Greg Claytond9e416c2012-02-18 05:35:26 +00001881 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001882}
1883
Greg Claytond9e416c2012-02-18 05:35:26 +00001884ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001885Target::CalculateThread ()
1886{
Greg Claytond9e416c2012-02-18 05:35:26 +00001887 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001888}
1889
Jason Molendab57e4a12013-11-04 09:33:30 +00001890StackFrameSP
1891Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001892{
Jason Molendab57e4a12013-11-04 09:33:30 +00001893 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894}
1895
1896void
Greg Clayton0603aa92010-10-04 01:05:56 +00001897Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898{
Greg Claytonc14ee322011-09-22 04:58:26 +00001899 exe_ctx.Clear();
1900 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001901}
1902
1903PathMappingList &
1904Target::GetImageSearchPathList ()
1905{
1906 return m_image_search_paths;
1907}
1908
1909void
1910Target::ImageSearchPathsChanged
1911(
1912 const PathMappingList &path_list,
1913 void *baton
1914)
1915{
1916 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001917 ModuleSP exe_module_sp (target->GetExecutableModule());
1918 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001919 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001920}
1921
Jim Ingham151c0322015-09-15 21:13:50 +00001922TypeSystem *
Sean Callanana994b0b2015-10-02 18:40:30 +00001923Target::GetScratchTypeSystemForLanguage (Error *error, lldb::LanguageType language, bool create_on_demand)
Jim Ingham151c0322015-09-15 21:13:50 +00001924{
Greg Clayton5beec212015-10-08 21:04:34 +00001925 if (!m_valid)
1926 return nullptr;
1927
Sean Callanana994b0b2015-10-02 18:40:30 +00001928 if (error)
1929 {
1930 error->Clear();
1931 }
1932
Sean Callananb92bd752015-10-01 16:28:02 +00001933 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Sean Callanana994b0b2015-10-02 18:40:30 +00001934 || language == eLanguageTypeUnknown)
1935 {
Sean Callananc7b054a2015-10-09 18:01:10 +00001936 std::set<lldb::LanguageType> languages_for_types;
1937 std::set<lldb::LanguageType> languages_for_expressions;
1938
1939 Language::GetLanguagesSupportingTypeSystems(languages_for_types, languages_for_expressions);
1940
1941 if (languages_for_expressions.count(eLanguageTypeC))
1942 {
1943 language = eLanguageTypeC; // LLDB's default. Override by setting the target language.
1944 }
1945 else
1946 {
1947 if (languages_for_expressions.empty())
1948 {
1949 return nullptr;
1950 }
1951 else
1952 {
1953 language = *languages_for_expressions.begin();
1954 }
1955 }
Sean Callananb92bd752015-10-01 16:28:02 +00001956 }
Sean Callananb92bd752015-10-01 16:28:02 +00001957
Greg Clayton5beec212015-10-08 21:04:34 +00001958 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this, create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00001959}
1960
1961PersistentExpressionState *
1962Target::GetPersistentExpressionStateForLanguage (lldb::LanguageType language)
1963{
Sean Callanana994b0b2015-10-02 18:40:30 +00001964 TypeSystem *type_system = GetScratchTypeSystemForLanguage(nullptr, language, true);
Sean Callananb92bd752015-10-01 16:28:02 +00001965
1966 if (type_system)
1967 {
1968 return type_system->GetPersistentExpressionState();
1969 }
Jim Ingham151c0322015-09-15 21:13:50 +00001970 else
Sean Callananb92bd752015-10-01 16:28:02 +00001971 {
1972 return nullptr;
1973 }
Jim Ingham151c0322015-09-15 21:13:50 +00001974}
1975
1976UserExpression *
1977Target::GetUserExpressionForLanguage(const char *expr,
Sean Callanana994b0b2015-10-02 18:40:30 +00001978 const char *expr_prefix,
1979 lldb::LanguageType language,
1980 Expression::ResultType desired_type,
1981 Error &error)
Jim Ingham151c0322015-09-15 21:13:50 +00001982{
Sean Callanana994b0b2015-10-02 18:40:30 +00001983 Error type_system_error;
1984
1985 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00001986 UserExpression *user_expr = nullptr;
1987
1988 if (!type_system)
1989 {
Sean Callanana994b0b2015-10-02 18:40:30 +00001990 error.SetErrorStringWithFormat("Could not find type system for language %s: %s", Language::GetNameForLanguageType(language), type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00001991 return nullptr;
1992 }
1993
1994 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type);
1995 if (!user_expr)
1996 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
1997
1998 return user_expr;
1999}
2000
2001FunctionCaller *
2002Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
2003 const CompilerType &return_type,
2004 const Address& function_address,
2005 const ValueList &arg_value_list,
2006 const char *name,
2007 Error &error)
2008{
Sean Callanana994b0b2015-10-02 18:40:30 +00002009 Error type_system_error;
2010 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002011 FunctionCaller *persistent_fn = nullptr;
2012
2013 if (!type_system)
2014 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002015 error.SetErrorStringWithFormat("Could not find type system for language %s: %s", Language::GetNameForLanguageType(language), type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00002016 return persistent_fn;
2017 }
2018
2019 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
2020 if (!persistent_fn)
2021 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2022
2023 return persistent_fn;
2024}
2025
2026UtilityFunction *
2027Target::GetUtilityFunctionForLanguage (const char *text,
2028 lldb::LanguageType language,
2029 const char *name,
2030 Error &error)
2031{
Sean Callanana994b0b2015-10-02 18:40:30 +00002032 Error type_system_error;
2033 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002034 UtilityFunction *utility_fn = nullptr;
2035
2036 if (!type_system)
2037 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002038 error.SetErrorStringWithFormat("Could not find type system for language %s: %s", Language::GetNameForLanguageType(language), type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00002039 return utility_fn;
2040 }
2041
2042 utility_fn = type_system->GetUtilityFunction (text, name);
2043 if (!utility_fn)
2044 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2045
2046 return utility_fn;
2047}
2048
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002049ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00002050Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002051{
Greg Clayton5beec212015-10-08 21:04:34 +00002052 if (m_valid)
Sean Callanan4bf80d52011-11-15 22:27:19 +00002053 {
Greg Clayton5beec212015-10-08 21:04:34 +00002054 if (TypeSystem* type_system = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC, create_on_demand))
2055 return llvm::dyn_cast<ClangASTContext>(type_system);
Sean Callanan4bf80d52011-11-15 22:27:19 +00002056 }
Greg Clayton5beec212015-10-08 21:04:34 +00002057 return nullptr;
Sean Callananb92bd752015-10-01 16:28:02 +00002058}
2059
Caroline Ticedaccaa92010-09-20 20:44:43 +00002060
Sean Callanan686b2312011-11-16 18:20:47 +00002061ClangASTImporter *
2062Target::GetClangASTImporter()
2063{
Greg Clayton5beec212015-10-08 21:04:34 +00002064 if (m_valid)
Sean Callanan686b2312011-11-16 18:20:47 +00002065 {
Greg Clayton5beec212015-10-08 21:04:34 +00002066 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
2067
2068 if (!ast_importer)
2069 {
2070 ast_importer = new ClangASTImporter();
2071 m_ast_importer_ap.reset(ast_importer);
2072 }
2073 return ast_importer;
Sean Callanan686b2312011-11-16 18:20:47 +00002074 }
Greg Clayton5beec212015-10-08 21:04:34 +00002075 return nullptr;
Sean Callanan686b2312011-11-16 18:20:47 +00002076}
2077
Greg Clayton99d0faf2010-11-18 23:32:35 +00002078void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002079Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002080{
Greg Clayton6920b522012-08-22 18:39:03 +00002081 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002082}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002083
Greg Clayton99d0faf2010-11-18 23:32:35 +00002084void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002085Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002086{
Greg Clayton6920b522012-08-22 18:39:03 +00002087 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002088}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002089
Greg Claytonc859e2d2012-02-13 23:10:39 +00002090FileSpecList
2091Target::GetDefaultExecutableSearchPaths ()
2092{
Greg Clayton67cc0632012-08-22 17:17:09 +00002093 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2094 if (properties_sp)
2095 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002096 return FileSpecList();
2097}
2098
Michael Sartaina7499c92013-07-01 19:45:50 +00002099FileSpecList
2100Target::GetDefaultDebugFileSearchPaths ()
2101{
2102 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2103 if (properties_sp)
2104 return properties_sp->GetDebugFileSearchPaths();
2105 return FileSpecList();
2106}
2107
Sean Callanan85054342015-04-03 15:39:47 +00002108FileSpecList
2109Target::GetDefaultClangModuleSearchPaths ()
2110{
2111 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2112 if (properties_sp)
2113 return properties_sp->GetClangModuleSearchPaths();
2114 return FileSpecList();
2115}
2116
Caroline Ticedaccaa92010-09-20 20:44:43 +00002117ArchSpec
2118Target::GetDefaultArchitecture ()
2119{
Greg Clayton67cc0632012-08-22 17:17:09 +00002120 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2121 if (properties_sp)
2122 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002123 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002124}
2125
2126void
Greg Clayton67cc0632012-08-22 17:17:09 +00002127Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002128{
Greg Clayton67cc0632012-08-22 17:17:09 +00002129 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2130 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002131 {
2132 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 +00002133 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002134 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002135}
2136
Greg Clayton0603aa92010-10-04 01:05:56 +00002137Target *
2138Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2139{
2140 // The target can either exist in the "process" of ExecutionContext, or in
2141 // the "target_sp" member of SymbolContext. This accessor helper function
2142 // will get the target from one of these locations.
2143
2144 Target *target = NULL;
2145 if (sc_ptr != NULL)
2146 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00002147 if (target == NULL && exe_ctx_ptr)
2148 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002149 return target;
2150}
2151
Jim Ingham1624a2d2014-05-05 02:26:40 +00002152ExpressionResults
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002153Target::EvaluateExpression
2154(
2155 const char *expr_cstr,
Jason Molendab57e4a12013-11-04 09:33:30 +00002156 StackFrame *frame,
Enrico Granata3372f582012-07-16 23:10:35 +00002157 lldb::ValueObjectSP &result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00002158 const EvaluateExpressionOptions& options
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002159)
2160{
Enrico Granata97fca502012-09-18 17:43:16 +00002161 result_valobj_sp.reset();
2162
Jim Ingham8646d3c2014-05-05 02:47:44 +00002163 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002164
Greg Claytond1767f02011-12-08 02:13:16 +00002165 if (expr_cstr == NULL || expr_cstr[0] == '\0')
2166 return execution_results;
2167
Jim Ingham6026ca32011-05-12 02:06:14 +00002168 // We shouldn't run stop hooks in expressions.
2169 // Be sure to reset this if you return anywhere within this function.
2170 bool old_suppress_value = m_suppress_stop_hooks;
2171 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002172
2173 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002174
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002175 if (frame)
2176 {
2177 frame->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002178 }
2179 else if (m_process_sp)
2180 {
2181 m_process_sp->CalculateExecutionContext(exe_ctx);
2182 }
2183 else
2184 {
2185 CalculateExecutionContext(exe_ctx);
2186 }
2187
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002188 // Make sure we aren't just trying to see the value of a persistent
2189 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002190 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002191 // Only check for persistent variables the expression starts with a '$'
2192 if (expr_cstr[0] == '$')
Sean Callanana994b0b2015-10-02 18:40:30 +00002193 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002194
2195 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002196 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002197 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002198 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002199 }
2200 else
2201 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002202 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002203 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002204 execution_results = UserExpression::Evaluate (exe_ctx,
2205 options,
2206 expr_cstr,
2207 prefix,
2208 result_valobj_sp,
2209 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002210 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002211
2212 m_suppress_stop_hooks = old_suppress_value;
2213
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002214 return execution_results;
2215}
2216
Greg Clayton5beec212015-10-08 21:04:34 +00002217
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002218lldb::ExpressionVariableSP
2219Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002220{
Greg Clayton5beec212015-10-08 21:04:34 +00002221 lldb::ExpressionVariableSP variable_sp;
2222 m_scratch_type_system_map.ForEach([this, name, &variable_sp](TypeSystem *type_system) -> bool
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002223 {
Greg Clayton5beec212015-10-08 21:04:34 +00002224 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002225 {
Greg Clayton5beec212015-10-08 21:04:34 +00002226 variable_sp = persistent_state->GetVariable(name);
2227
2228 if (variable_sp)
2229 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002230 }
Greg Clayton5beec212015-10-08 21:04:34 +00002231 return true; // Keep iterating the ForEach
2232 });
2233 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002234}
2235
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002236lldb::addr_t
Sean Callananb92bd752015-10-01 16:28:02 +00002237Target::GetPersistentSymbol(const ConstString &name)
2238{
Greg Clayton5beec212015-10-08 21:04:34 +00002239 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Sean Callananb92bd752015-10-01 16:28:02 +00002240
Greg Clayton5beec212015-10-08 21:04:34 +00002241 m_scratch_type_system_map.ForEach([this, name, &address](TypeSystem *type_system) -> bool
Sean Callananb92bd752015-10-01 16:28:02 +00002242 {
Greg Clayton5beec212015-10-08 21:04:34 +00002243 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callananb92bd752015-10-01 16:28:02 +00002244 {
Greg Clayton5beec212015-10-08 21:04:34 +00002245 address = persistent_state->LookupSymbol(name);
2246 if (address != LLDB_INVALID_ADDRESS)
2247 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002248 }
Greg Clayton5beec212015-10-08 21:04:34 +00002249 return true; // Keep iterating the ForEach
2250 });
2251 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002252}
2253
2254lldb::addr_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002255Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2256{
2257 addr_t code_addr = load_addr;
2258 switch (m_arch.GetMachine())
2259 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002260 case llvm::Triple::mips:
2261 case llvm::Triple::mipsel:
2262 case llvm::Triple::mips64:
2263 case llvm::Triple::mips64el:
2264 switch (addr_class)
2265 {
2266 case eAddressClassData:
2267 case eAddressClassDebug:
2268 return LLDB_INVALID_ADDRESS;
2269
2270 case eAddressClassUnknown:
2271 case eAddressClassInvalid:
2272 case eAddressClassCode:
2273 case eAddressClassCodeAlternateISA:
2274 case eAddressClassRuntime:
2275 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2276 code_addr |= 1ull;
2277 break;
2278 }
2279 break;
2280
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002281 case llvm::Triple::arm:
2282 case llvm::Triple::thumb:
2283 switch (addr_class)
2284 {
2285 case eAddressClassData:
2286 case eAddressClassDebug:
2287 return LLDB_INVALID_ADDRESS;
2288
2289 case eAddressClassUnknown:
2290 case eAddressClassInvalid:
2291 case eAddressClassCode:
2292 case eAddressClassCodeAlternateISA:
2293 case eAddressClassRuntime:
2294 // Check if bit zero it no set?
2295 if ((code_addr & 1ull) == 0)
2296 {
2297 // Bit zero isn't set, check if the address is a multiple of 2?
2298 if (code_addr & 2ull)
2299 {
2300 // The address is a multiple of 2 so it must be thumb, set bit zero
2301 code_addr |= 1ull;
2302 }
2303 else if (addr_class == eAddressClassCodeAlternateISA)
2304 {
2305 // We checked the address and the address claims to be the alternate ISA
2306 // which means thumb, so set bit zero.
2307 code_addr |= 1ull;
2308 }
2309 }
2310 break;
2311 }
2312 break;
2313
2314 default:
2315 break;
2316 }
2317 return code_addr;
2318}
2319
2320lldb::addr_t
2321Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2322{
2323 addr_t opcode_addr = load_addr;
2324 switch (m_arch.GetMachine())
2325 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002326 case llvm::Triple::mips:
2327 case llvm::Triple::mipsel:
2328 case llvm::Triple::mips64:
2329 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002330 case llvm::Triple::arm:
2331 case llvm::Triple::thumb:
2332 switch (addr_class)
2333 {
2334 case eAddressClassData:
2335 case eAddressClassDebug:
2336 return LLDB_INVALID_ADDRESS;
2337
2338 case eAddressClassInvalid:
2339 case eAddressClassUnknown:
2340 case eAddressClassCode:
2341 case eAddressClassCodeAlternateISA:
2342 case eAddressClassRuntime:
2343 opcode_addr &= ~(1ull);
2344 break;
2345 }
2346 break;
2347
2348 default:
2349 break;
2350 }
2351 return opcode_addr;
2352}
2353
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002354lldb::addr_t
2355Target::GetBreakableLoadAddress (lldb::addr_t addr)
2356{
2357 addr_t breakable_addr = addr;
2358 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2359
2360 switch (m_arch.GetMachine())
2361 {
2362 default:
2363 break;
2364 case llvm::Triple::mips:
2365 case llvm::Triple::mipsel:
2366 case llvm::Triple::mips64:
2367 case llvm::Triple::mips64el:
2368 {
2369 addr_t function_start = 0;
2370 addr_t current_offset = 0;
2371 uint32_t loop_count = 0;
2372 Address resolved_addr;
2373 uint32_t arch_flags = m_arch.GetFlags ();
2374 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2375 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2376 SectionLoadList &section_load_list = GetSectionLoadList();
2377
2378 if (section_load_list.IsEmpty())
2379 // No sections are loaded, so we must assume we are not running yet
2380 // and need to operate only on file address.
2381 m_images.ResolveFileAddress (addr, resolved_addr);
2382 else
2383 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2384
2385 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2386 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2387 if (temp_addr_module_sp)
2388 {
2389 SymbolContext sc;
2390 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002391 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002392 if (sc.function)
2393 {
2394 function_start = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(this);
2395 if (function_start == LLDB_INVALID_ADDRESS)
2396 function_start = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
2397 }
2398 else if (sc.symbol)
2399 {
2400 Address sym_addr = sc.symbol->GetAddress();
2401 function_start = sym_addr.GetFileAddress();
2402 }
2403 current_offset = addr - function_start;
2404 }
2405
2406 // If breakpoint address is start of function then we dont have to do anything.
2407 if (current_offset == 0)
2408 return breakable_addr;
2409 else
2410 loop_count = current_offset / 2;
2411
2412 if (loop_count > 3)
2413 {
2414 // Scan previous 6 bytes
2415 if (IsMips16 | IsMicromips)
2416 loop_count = 3;
2417 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2418 else
2419 loop_count = 2;
2420 }
2421
2422 // Create Disassembler Instance
2423 lldb::DisassemblerSP disasm_sp (Disassembler::FindPlugin(m_arch, NULL, NULL));
2424
2425 ExecutionContext exe_ctx;
2426 CalculateExecutionContext(exe_ctx);
2427 InstructionList instruction_list;
2428 InstructionSP prev_insn;
2429 bool prefer_file_cache = true; // Read from file
2430 uint32_t inst_to_choose = 0;
2431
2432 for (uint32_t i = 1; i <= loop_count; i++)
2433 {
2434 // Adjust the address to read from.
2435 resolved_addr.Slide (-2);
2436 AddressRange range(resolved_addr, i*2);
2437 uint32_t insn_size = 0;
2438
Greg Clayton6071e6f2015-08-26 22:57:51 +00002439 disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002440
2441 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2442 if (num_insns)
2443 {
2444 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2445 insn_size = prev_insn->GetOpcode().GetByteSize();
2446 if (i == 1 && insn_size == 2)
2447 {
2448 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2449 instruction_list.Append(prev_insn);
2450 inst_to_choose = 1;
2451 }
2452 else if (i == 2)
2453 {
2454 // Here we may get one 4-byte instruction or two 2-byte instructions.
2455 if (num_insns == 2)
2456 {
2457 // Looks like there are two 2-byte instructions above our breakpoint target address.
2458 // Now the upper 2-byte instruction is either a valid 2-byte instruction or could be a part of it's upper 4-byte instruction.
2459 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2460 // and whatever i=1 iteration has found out is true.
2461 inst_to_choose = 1;
2462 break;
2463 }
2464 else if (insn_size == 4)
2465 {
2466 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2467 // Lets try scanning upper 2 bytes to verify this.
2468 instruction_list.Append(prev_insn);
2469 inst_to_choose = 2;
2470 }
2471 }
2472 else if (i == 3)
2473 {
2474 if (insn_size == 4)
2475 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2476 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2477 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2478 inst_to_choose = 0;
2479 else
2480 // This is straight-forward
2481 inst_to_choose = 2;
2482 break;
2483 }
2484 }
2485 else
2486 {
2487 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2488 if (i > 1)
2489 {
2490 inst_to_choose = i - 1;
2491 break;
2492 }
2493 }
2494 }
2495
2496 // Check if we are able to find any valid instruction.
2497 if (inst_to_choose)
2498 {
2499 if (inst_to_choose > instruction_list.GetSize())
2500 inst_to_choose--;
2501 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2502
2503 if (prev_insn->HasDelaySlot())
2504 {
2505 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2506 // Adjust the breakable address
2507 breakable_addr = addr - shift_size;
2508 if (log)
2509 log->Printf ("Target::%s Breakpoint at 0x%8.8" PRIx64 " is adjusted to 0x%8.8" PRIx64 " due to delay slot\n", __FUNCTION__, addr, breakable_addr);
2510 }
2511 }
2512 break;
2513 }
2514 }
2515 return breakable_addr;
2516}
2517
Greg Clayton9585fbf2013-03-19 00:20:55 +00002518SourceManager &
2519Target::GetSourceManager ()
2520{
2521 if (m_source_manager_ap.get() == NULL)
2522 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2523 return *m_source_manager_ap;
2524}
2525
Sean Callanan9998acd2014-12-05 01:21:59 +00002526ClangModulesDeclVendor *
2527Target::GetClangModulesDeclVendor ()
2528{
2529 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2530
2531 {
2532 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2533
2534 if (!m_clang_modules_decl_vendor_ap)
2535 {
2536 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2537 }
2538 }
2539
2540 return m_clang_modules_decl_vendor_ap.get();
2541}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002542
Greg Clayton44d93782014-01-27 23:43:24 +00002543Target::StopHookSP
2544Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002545{
2546 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002547 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2548 m_stop_hooks[new_uid] = stop_hook_sp;
2549 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002550}
2551
2552bool
2553Target::RemoveStopHookByID (lldb::user_id_t user_id)
2554{
2555 size_t num_removed;
2556 num_removed = m_stop_hooks.erase (user_id);
2557 if (num_removed == 0)
2558 return false;
2559 else
2560 return true;
2561}
2562
2563void
2564Target::RemoveAllStopHooks ()
2565{
2566 m_stop_hooks.clear();
2567}
2568
2569Target::StopHookSP
2570Target::GetStopHookByID (lldb::user_id_t user_id)
2571{
2572 StopHookSP found_hook;
2573
2574 StopHookCollection::iterator specified_hook_iter;
2575 specified_hook_iter = m_stop_hooks.find (user_id);
2576 if (specified_hook_iter != m_stop_hooks.end())
2577 found_hook = (*specified_hook_iter).second;
2578 return found_hook;
2579}
2580
2581bool
2582Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2583{
2584 StopHookCollection::iterator specified_hook_iter;
2585 specified_hook_iter = m_stop_hooks.find (user_id);
2586 if (specified_hook_iter == m_stop_hooks.end())
2587 return false;
2588
2589 (*specified_hook_iter).second->SetIsActive (active_state);
2590 return true;
2591}
2592
2593void
2594Target::SetAllStopHooksActiveState (bool active_state)
2595{
2596 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2597 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2598 {
2599 (*pos).second->SetIsActive (active_state);
2600 }
2601}
2602
2603void
2604Target::RunStopHooks ()
2605{
Jim Ingham6026ca32011-05-12 02:06:14 +00002606 if (m_suppress_stop_hooks)
2607 return;
2608
Jim Ingham9575d842011-03-11 03:53:59 +00002609 if (!m_process_sp)
2610 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002611
2612 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2613 // since in that case we do not want to run the stop-hooks
2614 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2615 return;
2616
Jim Ingham9575d842011-03-11 03:53:59 +00002617 if (m_stop_hooks.empty())
2618 return;
2619
2620 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2621
2622 // If there aren't any active stop hooks, don't bother either:
2623 bool any_active_hooks = false;
2624 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2625 {
2626 if ((*pos).second->IsActive())
2627 {
2628 any_active_hooks = true;
2629 break;
2630 }
2631 }
2632 if (!any_active_hooks)
2633 return;
2634
2635 CommandReturnObject result;
2636
2637 std::vector<ExecutionContext> exc_ctx_with_reasons;
2638 std::vector<SymbolContext> sym_ctx_with_reasons;
2639
2640 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2641 size_t num_threads = cur_threadlist.GetSize();
2642 for (size_t i = 0; i < num_threads; i++)
2643 {
2644 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2645 if (cur_thread_sp->ThreadStoppedForAReason())
2646 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002647 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002648 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2649 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2650 }
2651 }
2652
2653 // If no threads stopped for a reason, don't run the stop-hooks.
2654 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2655 if (num_exe_ctx == 0)
2656 return;
2657
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002658 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2659 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002660
2661 bool keep_going = true;
2662 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002663 bool print_hook_header;
2664 bool print_thread_header;
2665
2666 if (num_exe_ctx == 1)
2667 print_thread_header = false;
2668 else
2669 print_thread_header = true;
2670
2671 if (m_stop_hooks.size() == 1)
2672 print_hook_header = false;
2673 else
2674 print_hook_header = true;
2675
Jim Ingham9575d842011-03-11 03:53:59 +00002676 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2677 {
2678 // result.Clear();
2679 StopHookSP cur_hook_sp = (*pos).second;
2680 if (!cur_hook_sp->IsActive())
2681 continue;
2682
2683 bool any_thread_matched = false;
2684 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2685 {
2686 if ((cur_hook_sp->GetSpecifier () == NULL
2687 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2688 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002689 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002690 {
2691 if (!hooks_ran)
2692 {
Jim Ingham9575d842011-03-11 03:53:59 +00002693 hooks_ran = true;
2694 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002695 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002696 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002697 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2698 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2699 NULL);
2700 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002701 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002702 else
Daniel Malead01b2952012-11-29 21:49:15 +00002703 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002704 any_thread_matched = true;
2705 }
2706
Jim Ingham381e25b2011-03-22 01:47:27 +00002707 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002708 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002709
2710 CommandInterpreterRunOptions options;
2711 options.SetStopOnContinue (true);
2712 options.SetStopOnError (true);
2713 options.SetEchoCommands (false);
2714 options.SetPrintResults (true);
2715 options.SetAddToHistory (false);
2716
2717 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2718 &exc_ctx_with_reasons[i],
2719 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002720 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002721
2722 // If the command started the target going again, we should bag out of
2723 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002724 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2725 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002726 {
Daniel Malead01b2952012-11-29 21:49:15 +00002727 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002728 keep_going = false;
2729 }
2730 }
2731 }
2732 }
Jason Molenda879cf772011-09-23 00:42:55 +00002733
Caroline Tice969ed3d2011-05-02 20:41:46 +00002734 result.GetImmediateOutputStream()->Flush();
2735 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002736}
2737
Greg Claytonfbb76342013-11-20 21:07:01 +00002738const TargetPropertiesSP &
2739Target::GetGlobalProperties()
2740{
2741 static TargetPropertiesSP g_settings_sp;
2742 if (!g_settings_sp)
2743 {
2744 g_settings_sp.reset (new TargetProperties (NULL));
2745 }
2746 return g_settings_sp;
2747}
2748
2749Error
2750Target::Install (ProcessLaunchInfo *launch_info)
2751{
2752 Error error;
2753 PlatformSP platform_sp (GetPlatform());
2754 if (platform_sp)
2755 {
2756 if (platform_sp->IsRemote())
2757 {
2758 if (platform_sp->IsConnected())
2759 {
2760 // Install all files that have an install path, and always install the
2761 // main executable when connected to a remote platform
2762 const ModuleList& modules = GetImages();
2763 const size_t num_images = modules.GetSize();
2764 for (size_t idx = 0; idx < num_images; ++idx)
2765 {
2766 const bool is_main_executable = idx == 0;
2767 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2768 if (module_sp)
2769 {
2770 FileSpec local_file (module_sp->GetFileSpec());
2771 if (local_file)
2772 {
2773 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2774 if (!remote_file)
2775 {
2776 if (is_main_executable) // TODO: add setting for always installing main executable???
2777 {
2778 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002779 remote_file = platform_sp->GetRemoteWorkingDirectory();
2780 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002781 }
2782 }
2783 if (remote_file)
2784 {
2785 error = platform_sp->Install(local_file, remote_file);
2786 if (error.Success())
2787 {
2788 module_sp->SetPlatformFileSpec(remote_file);
2789 if (is_main_executable)
2790 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002791 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002792 if (launch_info)
2793 launch_info->SetExecutableFile(remote_file, false);
2794 }
2795 }
2796 else
2797 break;
2798 }
2799 }
2800 }
2801 }
2802 }
2803 }
2804 }
2805 return error;
2806}
Greg Clayton7b242382011-07-08 00:48:09 +00002807
Greg Claytond5944cd2013-12-06 01:12:00 +00002808bool
2809Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2810{
2811 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2812}
2813
2814bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002815Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2816{
2817 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2818}
2819
2820bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002821Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2822{
2823 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2824 if (old_section_load_addr != new_section_load_addr)
2825 {
2826 uint32_t stop_id = 0;
2827 ProcessSP process_sp(GetProcessSP());
2828 if (process_sp)
2829 stop_id = process_sp->GetStopID();
2830 else
2831 stop_id = m_section_load_history.GetLastStopID();
2832 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2833 return true; // Return true if the section load address was changed...
2834 }
2835 return false; // Return false to indicate nothing changed
2836
2837}
2838
Greg Clayton8012cad2014-11-17 19:39:20 +00002839size_t
2840Target::UnloadModuleSections (const ModuleList &module_list)
2841{
2842 size_t section_unload_count = 0;
2843 size_t num_modules = module_list.GetSize();
2844 for (size_t i=0; i<num_modules; ++i)
2845 {
2846 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2847 }
2848 return section_unload_count;
2849}
2850
2851size_t
2852Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2853{
2854 uint32_t stop_id = 0;
2855 ProcessSP process_sp(GetProcessSP());
2856 if (process_sp)
2857 stop_id = process_sp->GetStopID();
2858 else
2859 stop_id = m_section_load_history.GetLastStopID();
2860 SectionList *sections = module_sp->GetSectionList();
2861 size_t section_unload_count = 0;
2862 if (sections)
2863 {
2864 const uint32_t num_sections = sections->GetNumSections(0);
2865 for (uint32_t i = 0; i < num_sections; ++i)
2866 {
2867 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2868 }
2869 }
2870 return section_unload_count;
2871}
2872
Greg Claytond5944cd2013-12-06 01:12:00 +00002873bool
2874Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2875{
2876 uint32_t stop_id = 0;
2877 ProcessSP process_sp(GetProcessSP());
2878 if (process_sp)
2879 stop_id = process_sp->GetStopID();
2880 else
2881 stop_id = m_section_load_history.GetLastStopID();
2882 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2883}
2884
2885bool
2886Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2887{
2888 uint32_t stop_id = 0;
2889 ProcessSP process_sp(GetProcessSP());
2890 if (process_sp)
2891 stop_id = process_sp->GetStopID();
2892 else
2893 stop_id = m_section_load_history.GetLastStopID();
2894 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2895}
2896
2897void
2898Target::ClearAllLoadedSections ()
2899{
2900 m_section_load_history.Clear();
2901}
2902
Greg Claytonb09c5382013-12-13 17:20:18 +00002903
2904Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002905Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002906{
2907 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002908 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2909
2910 if (log)
2911 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2912
Greg Claytonb09c5382013-12-13 17:20:18 +00002913 StateType state = eStateInvalid;
2914
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002915 // Scope to temporarily get the process state in case someone has manually
2916 // remotely connected already to a process and we can skip the platform
2917 // launching.
2918 {
2919 ProcessSP process_sp (GetProcessSP());
2920
2921 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002922 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002923 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002924 if (log)
2925 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2926 }
2927 else
2928 {
2929 if (log)
2930 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2931 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002932 }
2933
Greg Claytonb09c5382013-12-13 17:20:18 +00002934 launch_info.GetFlags().Set (eLaunchFlagDebug);
2935
2936 // Get the value of synchronous execution here. If you wait till after you have started to
2937 // run, then you could have hit a breakpoint, whose command might switch the value, and
2938 // then you'll pick up that incorrect value.
2939 Debugger &debugger = GetDebugger();
2940 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2941
2942 PlatformSP platform_sp (GetPlatform());
2943
2944 // Finalize the file actions, and if none were given, default to opening
2945 // up a pseudo terminal
2946 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002947 if (log)
2948 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2949 __FUNCTION__,
2950 platform_sp ? "true" : "false",
2951 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2952 default_to_use_pty ? "true" : "false");
2953
Greg Claytonb09c5382013-12-13 17:20:18 +00002954 launch_info.FinalizeFileActions (this, default_to_use_pty);
2955
2956 if (state == eStateConnected)
2957 {
2958 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2959 {
2960 error.SetErrorString("can't launch in tty when launching through a remote connection");
2961 return error;
2962 }
2963 }
2964
2965 if (!launch_info.GetArchitecture().IsValid())
2966 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002967
2968 // 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 +00002969 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2970 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002971 if (log)
2972 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2973
Greg Clayton5df78fa2015-05-23 03:54:53 +00002974 // Get a weak pointer to the previous process if we have one
2975 ProcessWP process_wp;
2976 if (m_process_sp)
2977 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00002978 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2979 debugger,
2980 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002981 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00002982
2983 // Cleanup the old process since someone might still have a strong
2984 // reference to this process and we would like to allow it to cleanup
2985 // as much as it can without the object being destroyed. We try to
2986 // lock the shared pointer and if that works, then someone else still
2987 // has a strong reference to the process.
2988
2989 ProcessSP old_process_sp(process_wp.lock());
2990 if (old_process_sp)
2991 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00002992 }
2993 else
2994 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002995 if (log)
2996 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
2997
Greg Claytonb09c5382013-12-13 17:20:18 +00002998 if (state == eStateConnected)
2999 {
3000 assert(m_process_sp);
3001 }
3002 else
3003 {
Todd Fiala015d8182014-07-22 23:41:36 +00003004 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00003005 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00003006 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00003007 }
Todd Fiala015d8182014-07-22 23:41:36 +00003008
3009 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00003010 if (m_process_sp)
3011 error = m_process_sp->Launch (launch_info);
3012 }
3013
3014 if (!m_process_sp)
3015 {
3016 if (error.Success())
3017 error.SetErrorString("failed to launch or debug process");
3018 return error;
3019 }
3020
3021 if (error.Success())
3022 {
Ilia K064e69f2015-03-23 21:16:25 +00003023 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00003024 {
Greg Clayton44d93782014-01-27 23:43:24 +00003025 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00003026 if (!hijack_listener_sp)
3027 {
3028 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
3029 launch_info.SetHijackListener(hijack_listener_sp);
3030 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
3031 }
Todd Fialaac33cc92014-10-09 01:02:08 +00003032
Ilia K38810f42015-05-20 10:15:47 +00003033 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00003034
3035 if (state == eStateStopped)
3036 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003037 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003038 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003039 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00003040 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003041 error = m_process_sp->PrivateResume();
3042 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00003043 {
Ilia K064e69f2015-03-23 21:16:25 +00003044 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
3045 const bool must_be_alive = false; // eStateExited is ok, so this must be false
3046 if (!StateIsStoppedState(state, must_be_alive))
3047 {
3048 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
3049 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003050 }
3051 }
Ilia K064e69f2015-03-23 21:16:25 +00003052 else
3053 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003054 m_process_sp->RestoreProcessEvents();
3055 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00003056 }
3057 if (!error.Success())
3058 {
Ilia K064e69f2015-03-23 21:16:25 +00003059 Error error2;
3060 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
3061 error = error2;
3062 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003063 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003064 }
Greg Clayton40286e02014-04-30 20:29:09 +00003065 else if (state == eStateExited)
3066 {
Zachary Turner10687b02014-10-20 17:46:43 +00003067 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00003068 const int exit_status = m_process_sp->GetExitStatus();
3069 const char *exit_desc = m_process_sp->GetExitDescription();
3070#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'."
3071 if (exit_desc && exit_desc[0])
3072 {
3073 if (with_shell)
3074 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
3075 else
3076 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
3077 }
3078 else
3079 {
3080 if (with_shell)
3081 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
3082 else
3083 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
3084 }
3085 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003086 else
3087 {
3088 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
3089 }
3090 }
Greg Clayton44d93782014-01-27 23:43:24 +00003091 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00003092 }
3093 else
3094 {
Greg Clayton44d93782014-01-27 23:43:24 +00003095 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003096 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003097 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003098 }
3099 return error;
3100}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003101
3102Error
3103Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3104{
3105 auto state = eStateInvalid;
3106 auto process_sp = GetProcessSP ();
3107 if (process_sp)
3108 {
3109 state = process_sp->GetState ();
3110 if (process_sp->IsAlive () && state != eStateConnected)
3111 {
3112 if (state == eStateAttaching)
3113 return Error ("process attach is in progress");
3114 return Error ("a process is already being debugged");
3115 }
3116 }
3117
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003118 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3119
3120 // If no process info was specified, then use the target executable
3121 // name as the process to attach to by default
3122 if (!attach_info.ProcessInfoSpecified ())
3123 {
3124 if (old_exec_module_sp)
3125 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3126
3127 if (!attach_info.ProcessInfoSpecified ())
3128 {
3129 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3130 }
3131 }
3132
3133 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
Greg Claytonb3788ea2015-10-05 22:58:37 +00003134 ListenerSP hijack_listener_sp;
3135 const bool async = attach_info.GetAsync();
3136 if (async == false)
3137 {
3138 hijack_listener_sp.reset (new Listener ("lldb.Target.Attach.attach.hijack"));
3139 attach_info.SetHijackListener (hijack_listener_sp);
3140 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003141
3142 Error error;
3143 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3144 {
3145 SetPlatform (platform_sp);
3146 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3147 }
3148 else
3149 {
3150 if (state != eStateConnected)
3151 {
3152 const char *plugin_name = attach_info.GetProcessPluginName ();
3153 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3154 if (process_sp == nullptr)
3155 {
3156 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3157 return error;
3158 }
3159 }
Greg Claytonb3788ea2015-10-05 22:58:37 +00003160 if (hijack_listener_sp)
3161 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003162 error = process_sp->Attach (attach_info);
3163 }
3164
Greg Claytonb3788ea2015-10-05 22:58:37 +00003165 if (error.Success () && process_sp && async == false)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003166 {
3167 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
3168 process_sp->RestoreProcessEvents ();
3169
3170 if (state != eStateStopped)
3171 {
3172 const char *exit_desc = process_sp->GetExitDescription ();
3173 if (exit_desc)
Greg Claytonb3788ea2015-10-05 22:58:37 +00003174 error.SetErrorStringWithFormat ("%s", exit_desc);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003175 else
Greg Claytonb3788ea2015-10-05 22:58:37 +00003176 error.SetErrorString ("process did not stop (no such process or permission problem?)");
Jason Molendaede31932015-04-17 05:01:58 +00003177 process_sp->Destroy (false);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003178 }
3179 }
3180 return error;
3181}
3182
Jim Ingham9575d842011-03-11 03:53:59 +00003183//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003184// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003185//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003186Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3187 UserID (uid),
3188 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003189 m_commands (),
3190 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003191 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003192 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003193{
3194}
3195
3196Target::StopHook::StopHook (const StopHook &rhs) :
3197 UserID (rhs.GetID()),
3198 m_target_sp (rhs.m_target_sp),
3199 m_commands (rhs.m_commands),
3200 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003201 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003202 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003203{
3204 if (rhs.m_thread_spec_ap.get() != NULL)
3205 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3206}
3207
3208
3209Target::StopHook::~StopHook ()
3210{
3211}
3212
3213void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003214Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3215{
3216 m_specifier_sp.reset(specifier);
3217}
3218
3219void
Jim Ingham9575d842011-03-11 03:53:59 +00003220Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3221{
3222 m_thread_spec_ap.reset (specifier);
3223}
3224
3225
3226void
3227Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3228{
3229 int indent_level = s->GetIndentLevel();
3230
3231 s->SetIndentLevel(indent_level + 2);
3232
Daniel Malead01b2952012-11-29 21:49:15 +00003233 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003234 if (m_active)
3235 s->Indent ("State: enabled\n");
3236 else
3237 s->Indent ("State: disabled\n");
3238
3239 if (m_specifier_sp)
3240 {
3241 s->Indent();
3242 s->PutCString ("Specifier:\n");
3243 s->SetIndentLevel (indent_level + 4);
3244 m_specifier_sp->GetDescription (s, level);
3245 s->SetIndentLevel (indent_level + 2);
3246 }
3247
3248 if (m_thread_spec_ap.get() != NULL)
3249 {
3250 StreamString tmp;
3251 s->Indent("Thread:\n");
3252 m_thread_spec_ap->GetDescription (&tmp, level);
3253 s->SetIndentLevel (indent_level + 4);
3254 s->Indent (tmp.GetData());
3255 s->PutCString ("\n");
3256 s->SetIndentLevel (indent_level + 2);
3257 }
3258
3259 s->Indent ("Commands: \n");
3260 s->SetIndentLevel (indent_level + 4);
3261 uint32_t num_commands = m_commands.GetSize();
3262 for (uint32_t i = 0; i < num_commands; i++)
3263 {
3264 s->Indent(m_commands.GetStringAtIndex(i));
3265 s->PutCString ("\n");
3266 }
3267 s->SetIndentLevel (indent_level);
3268}
3269
Greg Clayton67cc0632012-08-22 17:17:09 +00003270//--------------------------------------------------------------
3271// class TargetProperties
3272//--------------------------------------------------------------
3273
3274OptionEnumValueElement
3275lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003276{
Greg Clayton67cc0632012-08-22 17:17:09 +00003277 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3278 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3279 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
3280 { 0, NULL, NULL }
3281};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003282
Greg Clayton1f746072012-08-29 21:13:06 +00003283static OptionEnumValueElement
3284g_inline_breakpoint_enums[] =
3285{
3286 { 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."},
3287 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3288 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
3289 { 0, NULL, NULL }
3290};
3291
Jim Ingham0f063ba2013-03-02 00:26:47 +00003292typedef enum x86DisassemblyFlavor
3293{
3294 eX86DisFlavorDefault,
3295 eX86DisFlavorIntel,
3296 eX86DisFlavorATT
3297} x86DisassemblyFlavor;
3298
3299static OptionEnumValueElement
3300g_x86_dis_flavor_value_types[] =
3301{
3302 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3303 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3304 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
3305 { 0, NULL, NULL }
3306};
3307
Enrico Granata397ddd52013-05-21 20:13:34 +00003308static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003309g_hex_immediate_style_values[] =
3310{
3311 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3312 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
3313 { 0, NULL, NULL }
3314};
3315
3316static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003317g_load_script_from_sym_file_values[] =
3318{
3319 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3320 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3321 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
3322 { 0, NULL, NULL }
3323};
3324
Greg Claytonfd814c52013-08-13 01:42:25 +00003325
3326static OptionEnumValueElement
3327g_memory_module_load_level_values[] =
3328{
Greg Clayton86eac942013-08-13 21:32:34 +00003329 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003330 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3331 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
3332 { 0, NULL, NULL }
3333};
3334
Greg Clayton67cc0632012-08-22 17:17:09 +00003335static PropertyDefinition
3336g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003337{
Greg Clayton67cc0632012-08-22 17:17:09 +00003338 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
Ilia K055ad9b2015-05-18 13:41:01 +00003339 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Move breakpoints to nearest code." },
Dawn Perchike0c96ac2015-07-30 22:10:08 +00003340 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , NULL, NULL, "The language to use when interpreting expressions entered in commands." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003341 { "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 +00003342 { "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 +00003343 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
3344 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
Jim Ingham0ca778e2015-09-15 18:03:00 +00003345 { "source-map" , OptionValue::eTypePathMap , false, 0 , NULL, NULL, "Source path remappings are used to track the change of location between a source file when built, and "
Greg Clayton67cc0632012-08-22 17:17:09 +00003346 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3347 "some part (starting at the root) of the path to the file when it was built, "
3348 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3349 "Each element of the array is checked in order and the first one that results in a match wins." },
3350 { "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 +00003351 { "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 +00003352 { "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 +00003353 { "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 +00003354 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
3355 { "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 +00003356 { "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 +00003357 { "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 +00003358 { "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." },
3359 { "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 +00003360 { "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." },
3361 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
3362 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
3363 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
3364 { "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 +00003365 { "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 +00003366 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
3367 { "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 +00003368 { "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 +00003369 "Breakpoint locations can end up being inlined by the compiler, so that a compile unit 'a.c' might contain an inlined function from another source file. "
Jim Ingham63733852015-06-16 21:39:56 +00003370 "Usually this is limited to breakpoint locations from inlined functions from header or other include files, or more accurately non-implementation source files. "
Greg Clayton1f746072012-08-29 21:13:06 +00003371 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003372 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3373 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3374 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003375 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003376 // 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.
3377 { "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 +00003378 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
3379 { "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 +00003380 { "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 +00003381 { "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 +00003382 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
3383 "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. "
3384 "This setting helps users control how much information gets loaded when loading modules from memory."
3385 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3386 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3387 "'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 +00003388 { "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 +00003389 { "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 +00003390 { "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 +00003391 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, NULL, NULL, "Disable lock-step debugging, instead control threads independently." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003392 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
3393};
Enrico Granata560558e2015-02-11 02:35:39 +00003394
Greg Clayton67cc0632012-08-22 17:17:09 +00003395enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003396{
Greg Clayton67cc0632012-08-22 17:17:09 +00003397 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003398 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003399 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003400 ePropertyExprPrefix,
3401 ePropertyPreferDynamic,
3402 ePropertyEnableSynthetic,
3403 ePropertySkipPrologue,
3404 ePropertySourceMap,
3405 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003406 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003407 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003408 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003409 ePropertyMaxChildrenCount,
3410 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003411 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003412 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003413 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003414 ePropertyRunArgs,
3415 ePropertyEnvVars,
3416 ePropertyInheritEnv,
3417 ePropertyInputPath,
3418 ePropertyOutputPath,
3419 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003420 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003421 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003422 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003423 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003424 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003425 ePropertyUseHexImmediates,
3426 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003427 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003428 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003429 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003430 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003431 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003432 ePropertyDisplayRuntimeSupportValues,
3433 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003434};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003435
Caroline Ticedaccaa92010-09-20 20:44:43 +00003436
Greg Clayton67cc0632012-08-22 17:17:09 +00003437class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003438{
Greg Clayton67cc0632012-08-22 17:17:09 +00003439public:
3440 TargetOptionValueProperties (const ConstString &name) :
3441 OptionValueProperties (name),
3442 m_target (NULL),
3443 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003444 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003445 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003446
Greg Clayton67cc0632012-08-22 17:17:09 +00003447 // This constructor is used when creating TargetOptionValueProperties when it
3448 // is part of a new lldb_private::Target instance. It will copy all current
3449 // global property values as needed
3450 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3451 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3452 m_target (target),
3453 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003454 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003455 }
3456
3457 virtual const Property *
3458 GetPropertyAtIndex (const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const
3459 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003460 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003461 // try and grab the setting from the current target if there is one. Else we just
3462 // use the one from this instance.
3463 if (idx == ePropertyEnvVars)
3464 GetHostEnvironmentIfNeeded ();
3465
3466 if (exe_ctx)
3467 {
3468 Target *target = exe_ctx->GetTargetPtr();
3469 if (target)
3470 {
3471 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3472 if (this != target_properties)
3473 return target_properties->ProtectedGetPropertyAtIndex (idx);
3474 }
3475 }
3476 return ProtectedGetPropertyAtIndex (idx);
3477 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003478
3479 lldb::TargetSP
3480 GetTargetSP ()
3481 {
3482 return m_target->shared_from_this();
3483 }
3484
Greg Clayton67cc0632012-08-22 17:17:09 +00003485protected:
3486
3487 void
3488 GetHostEnvironmentIfNeeded () const
3489 {
3490 if (!m_got_host_env)
3491 {
3492 if (m_target)
3493 {
3494 m_got_host_env = true;
3495 const uint32_t idx = ePropertyInheritEnv;
3496 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3497 {
3498 PlatformSP platform_sp (m_target->GetPlatform());
3499 if (platform_sp)
3500 {
3501 StringList env;
3502 if (platform_sp->GetEnvironment(env))
3503 {
3504 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3505 if (env_dict)
3506 {
3507 const bool can_replace = false;
3508 const size_t envc = env.GetSize();
3509 for (size_t idx=0; idx<envc; idx++)
3510 {
3511 const char *env_entry = env.GetStringAtIndex (idx);
3512 if (env_entry)
3513 {
3514 const char *equal_pos = ::strchr(env_entry, '=');
3515 ConstString key;
3516 // It is ok to have environment variables with no values
3517 const char *value = NULL;
3518 if (equal_pos)
3519 {
3520 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3521 if (equal_pos[1])
3522 value = equal_pos + 1;
3523 }
3524 else
3525 {
3526 key.SetCString(env_entry);
3527 }
3528 // Don't allow existing keys to be replaced with ones we get from the platform environment
3529 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3530 }
3531 }
3532 }
3533 }
3534 }
3535 }
3536 }
3537 }
3538 }
3539 Target *m_target;
3540 mutable bool m_got_host_env;
3541};
3542
Greg Claytonfbb76342013-11-20 21:07:01 +00003543//----------------------------------------------------------------------
3544// TargetProperties
3545//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003546TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003547 Properties (),
3548 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003549{
3550 if (target)
3551 {
3552 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003553
3554 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3555 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3556 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3557 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3558 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3559 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3560 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3561 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3562 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3563 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3564
3565 // Update m_launch_info once it was created
3566 Arg0ValueChangedCallback(this, NULL);
3567 RunArgsValueChangedCallback(this, NULL);
3568 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3569 InputPathValueChangedCallback(this, NULL);
3570 OutputPathValueChangedCallback(this, NULL);
3571 ErrorPathValueChangedCallback(this, NULL);
3572 DetachOnErrorValueChangedCallback(this, NULL);
3573 DisableASLRValueChangedCallback(this, NULL);
3574 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003575 }
3576 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003577 {
3578 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3579 m_collection_sp->Initialize(g_properties);
3580 m_collection_sp->AppendProperty(ConstString("process"),
3581 ConstString("Settings specify to processes."),
3582 true,
3583 Process::GetGlobalProperties()->GetValueProperties());
3584 }
Ilia K8f37ca52015-02-13 14:31:06 +00003585
Caroline Ticedaccaa92010-09-20 20:44:43 +00003586}
3587
Greg Clayton67cc0632012-08-22 17:17:09 +00003588TargetProperties::~TargetProperties ()
3589{
3590}
3591ArchSpec
3592TargetProperties::GetDefaultArchitecture () const
3593{
3594 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3595 if (value)
3596 return value->GetCurrentValue();
3597 return ArchSpec();
3598}
3599
3600void
3601TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3602{
3603 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3604 if (value)
3605 return value->SetCurrentValue(arch, true);
3606}
3607
Ilia K055ad9b2015-05-18 13:41:01 +00003608bool
3609TargetProperties::GetMoveToNearestCode() const
3610{
3611 const uint32_t idx = ePropertyMoveToNearestCode;
3612 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3613}
3614
Greg Clayton67cc0632012-08-22 17:17:09 +00003615lldb::DynamicValueType
3616TargetProperties::GetPreferDynamicValue() const
3617{
3618 const uint32_t idx = ePropertyPreferDynamic;
3619 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3620}
3621
3622bool
Greg Clayton15484402015-05-15 18:40:24 +00003623TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3624{
3625 const uint32_t idx = ePropertyPreferDynamic;
3626 return m_collection_sp->SetPropertyAtIndexAsEnumeration(NULL, idx, d);
3627}
3628
3629
3630bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003631TargetProperties::GetDisableASLR () const
3632{
3633 const uint32_t idx = ePropertyDisableASLR;
3634 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3635}
3636
3637void
3638TargetProperties::SetDisableASLR (bool b)
3639{
3640 const uint32_t idx = ePropertyDisableASLR;
3641 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3642}
3643
3644bool
Jim Ingham106d0282014-06-25 02:32:56 +00003645TargetProperties::GetDetachOnError () const
3646{
3647 const uint32_t idx = ePropertyDetachOnError;
3648 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3649}
3650
3651void
3652TargetProperties::SetDetachOnError (bool b)
3653{
3654 const uint32_t idx = ePropertyDetachOnError;
3655 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3656}
3657
3658bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003659TargetProperties::GetDisableSTDIO () const
3660{
3661 const uint32_t idx = ePropertyDisableSTDIO;
3662 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3663}
3664
3665void
3666TargetProperties::SetDisableSTDIO (bool b)
3667{
3668 const uint32_t idx = ePropertyDisableSTDIO;
3669 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3670}
3671
Jim Ingham0f063ba2013-03-02 00:26:47 +00003672const char *
3673TargetProperties::GetDisassemblyFlavor () const
3674{
3675 const uint32_t idx = ePropertyDisassemblyFlavor;
3676 const char *return_value;
3677
3678 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3679 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3680 return return_value;
3681}
3682
Greg Clayton1f746072012-08-29 21:13:06 +00003683InlineStrategy
3684TargetProperties::GetInlineStrategy () const
3685{
3686 const uint32_t idx = ePropertyInlineStrategy;
3687 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3688}
3689
Greg Clayton45392552012-10-17 22:57:12 +00003690const char *
3691TargetProperties::GetArg0 () const
3692{
3693 const uint32_t idx = ePropertyArg0;
3694 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3695}
3696
3697void
3698TargetProperties::SetArg0 (const char *arg)
3699{
3700 const uint32_t idx = ePropertyArg0;
3701 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003702 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003703}
3704
Greg Clayton67cc0632012-08-22 17:17:09 +00003705bool
3706TargetProperties::GetRunArguments (Args &args) const
3707{
3708 const uint32_t idx = ePropertyRunArgs;
3709 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3710}
3711
3712void
3713TargetProperties::SetRunArguments (const Args &args)
3714{
3715 const uint32_t idx = ePropertyRunArgs;
3716 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003717 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003718}
3719
3720size_t
3721TargetProperties::GetEnvironmentAsArgs (Args &env) const
3722{
3723 const uint32_t idx = ePropertyEnvVars;
3724 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3725}
3726
Ilia K8f37ca52015-02-13 14:31:06 +00003727void
3728TargetProperties::SetEnvironmentFromArgs (const Args &env)
3729{
3730 const uint32_t idx = ePropertyEnvVars;
3731 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003732 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003733}
3734
Greg Clayton67cc0632012-08-22 17:17:09 +00003735bool
3736TargetProperties::GetSkipPrologue() const
3737{
3738 const uint32_t idx = ePropertySkipPrologue;
3739 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3740}
3741
3742PathMappingList &
3743TargetProperties::GetSourcePathMap () const
3744{
3745 const uint32_t idx = ePropertySourceMap;
3746 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3747 assert(option_value);
3748 return option_value->GetCurrentValue();
3749}
3750
3751FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003752TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003753{
3754 const uint32_t idx = ePropertyExecutableSearchPaths;
3755 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3756 assert(option_value);
3757 return option_value->GetCurrentValue();
3758}
3759
Michael Sartaina7499c92013-07-01 19:45:50 +00003760FileSpecList &
3761TargetProperties::GetDebugFileSearchPaths ()
3762{
3763 const uint32_t idx = ePropertyDebugFileSearchPaths;
3764 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3765 assert(option_value);
3766 return option_value->GetCurrentValue();
3767}
3768
Sean Callanan85054342015-04-03 15:39:47 +00003769FileSpecList &
3770TargetProperties::GetClangModuleSearchPaths ()
3771{
3772 const uint32_t idx = ePropertyClangModuleSearchPaths;
3773 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3774 assert(option_value);
3775 return option_value->GetCurrentValue();
3776}
3777
Greg Clayton67cc0632012-08-22 17:17:09 +00003778bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003779TargetProperties::GetEnableAutoImportClangModules() const
3780{
3781 const uint32_t idx = ePropertyAutoImportClangModules;
3782 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3783}
3784
3785bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003786TargetProperties::GetEnableSyntheticValue () const
3787{
3788 const uint32_t idx = ePropertyEnableSynthetic;
3789 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3790}
3791
3792uint32_t
3793TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3794{
3795 const uint32_t idx = ePropertyMaxChildrenCount;
3796 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3797}
3798
3799uint32_t
3800TargetProperties::GetMaximumSizeOfStringSummary() const
3801{
3802 const uint32_t idx = ePropertyMaxSummaryLength;
3803 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3804}
3805
Enrico Granatad325bf92013-06-04 22:54:16 +00003806uint32_t
3807TargetProperties::GetMaximumMemReadSize () const
3808{
3809 const uint32_t idx = ePropertyMaxMemReadSize;
3810 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3811}
3812
Greg Clayton67cc0632012-08-22 17:17:09 +00003813FileSpec
3814TargetProperties::GetStandardInputPath () const
3815{
3816 const uint32_t idx = ePropertyInputPath;
3817 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3818}
3819
3820void
3821TargetProperties::SetStandardInputPath (const char *p)
3822{
3823 const uint32_t idx = ePropertyInputPath;
3824 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3825}
3826
3827FileSpec
3828TargetProperties::GetStandardOutputPath () const
3829{
3830 const uint32_t idx = ePropertyOutputPath;
3831 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3832}
3833
3834void
3835TargetProperties::SetStandardOutputPath (const char *p)
3836{
3837 const uint32_t idx = ePropertyOutputPath;
3838 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3839}
3840
3841FileSpec
3842TargetProperties::GetStandardErrorPath () const
3843{
3844 const uint32_t idx = ePropertyErrorPath;
3845 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3846}
3847
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003848LanguageType
3849TargetProperties::GetLanguage () const
3850{
3851 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage (NULL, ePropertyLanguage);
3852 if (value)
3853 return value->GetCurrentValue();
3854 return LanguageType();
3855}
3856
Greg Clayton6920b522012-08-22 18:39:03 +00003857const char *
3858TargetProperties::GetExpressionPrefixContentsAsCString ()
3859{
3860 const uint32_t idx = ePropertyExprPrefix;
3861 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3862 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003863 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003864 const bool null_terminate = true;
3865 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003866 if (data_sp)
3867 return (const char *) data_sp->GetBytes();
3868 }
Greg Clayton6920b522012-08-22 18:39:03 +00003869 return NULL;
3870}
3871
Greg Clayton67cc0632012-08-22 17:17:09 +00003872void
3873TargetProperties::SetStandardErrorPath (const char *p)
3874{
3875 const uint32_t idx = ePropertyErrorPath;
3876 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3877}
3878
3879bool
3880TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3881{
3882 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3883 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3884}
3885
Jim Ingham17d023f2013-03-13 17:58:04 +00003886bool
Daniel Malead79ae052013-08-07 21:54:09 +00003887TargetProperties::GetUseHexImmediates () const
3888{
3889 const uint32_t idx = ePropertyUseHexImmediates;
3890 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3891}
3892
3893bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003894TargetProperties::GetUseFastStepping () const
3895{
3896 const uint32_t idx = ePropertyUseFastStepping;
3897 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3898}
3899
Greg Claytonfb6621e2013-12-06 21:59:52 +00003900bool
3901TargetProperties::GetDisplayExpressionsInCrashlogs () const
3902{
3903 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3904 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3905}
3906
Enrico Granata397ddd52013-05-21 20:13:34 +00003907LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003908TargetProperties::GetLoadScriptFromSymbolFile () const
3909{
3910 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003911 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003912}
3913
Daniel Malead79ae052013-08-07 21:54:09 +00003914Disassembler::HexImmediateStyle
3915TargetProperties::GetHexImmediateStyle () const
3916{
3917 const uint32_t idx = ePropertyHexImmediateStyle;
3918 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3919}
3920
Greg Claytonfd814c52013-08-13 01:42:25 +00003921MemoryModuleLoadLevel
3922TargetProperties::GetMemoryModuleLoadLevel() const
3923{
3924 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3925 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3926}
3927
Jason Molendaa4bea722014-02-14 05:06:49 +00003928bool
3929TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3930{
3931 const uint32_t idx = ePropertyTrapHandlerNames;
3932 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3933}
Greg Claytonfd814c52013-08-13 01:42:25 +00003934
Jason Molendaa4bea722014-02-14 05:06:49 +00003935void
3936TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3937{
3938 const uint32_t idx = ePropertyTrapHandlerNames;
3939 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3940}
Greg Clayton67cc0632012-08-22 17:17:09 +00003941
Enrico Granata560558e2015-02-11 02:35:39 +00003942bool
3943TargetProperties::GetDisplayRuntimeSupportValues () const
3944{
3945 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3946 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3947}
3948
3949void
3950TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3951{
3952 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3953 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3954}
3955
Ewan Crawford78baa192015-05-13 09:18:18 +00003956bool
3957TargetProperties::GetNonStopModeEnabled () const
3958{
3959 const uint32_t idx = ePropertyNonStopModeEnabled;
3960 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3961}
3962
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00003963void
3964TargetProperties::SetNonStopModeEnabled (bool b)
3965{
3966 const uint32_t idx = ePropertyNonStopModeEnabled;
3967 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3968}
3969
Ilia K8f37ca52015-02-13 14:31:06 +00003970const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003971TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003972{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003973 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003974 return m_launch_info;
3975}
3976
3977void
3978TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3979{
3980 m_launch_info = launch_info;
3981 SetArg0(launch_info.GetArg0());
3982 SetRunArguments(launch_info.GetArguments());
3983 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3984 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3985 if (input_file_action)
3986 {
3987 const char *input_path = input_file_action->GetPath();
3988 if (input_path)
3989 SetStandardInputPath(input_path);
3990 }
3991 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3992 if (output_file_action)
3993 {
3994 const char *output_path = output_file_action->GetPath();
3995 if (output_path)
3996 SetStandardOutputPath(output_path);
3997 }
3998 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
3999 if (error_file_action)
4000 {
4001 const char *error_path = error_file_action->GetPath();
4002 if (error_path)
4003 SetStandardErrorPath(error_path);
4004 }
4005 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
4006 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4007 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
4008}
4009
4010void
4011TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
4012{
4013 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4014 this_->m_launch_info.SetArg0(this_->GetArg0());
4015}
4016
4017void
4018TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
4019{
4020 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4021 Args args;
4022 if (this_->GetRunArguments(args))
4023 this_->m_launch_info.GetArguments() = args;
4024}
4025
4026void
4027TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
4028{
4029 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4030 Args args;
4031 if (this_->GetEnvironmentAsArgs(args))
4032 this_->m_launch_info.GetEnvironmentEntries() = args;
4033}
4034
4035void
4036TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4037{
4038 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004039 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004040}
4041
4042void
4043TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4044{
4045 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004046 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004047}
4048
4049void
4050TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4051{
4052 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004053 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004054}
4055
4056void
4057TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
4058{
4059 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4060 if (this_->GetDetachOnError())
4061 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4062 else
4063 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
4064}
4065
4066void
4067TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
4068{
4069 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4070 if (this_->GetDisableASLR())
4071 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4072 else
4073 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
4074}
4075
4076void
4077TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
4078{
4079 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4080 if (this_->GetDisableSTDIO())
4081 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4082 else
4083 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4084}
Ilia Keb2c19a2015-03-10 21:59:55 +00004085
4086//----------------------------------------------------------------------
4087// Target::TargetEventData
4088//----------------------------------------------------------------------
4089
4090Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
4091 EventData (),
4092 m_target_sp (target_sp),
4093 m_module_list ()
4094{
4095}
4096
4097Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
4098 EventData (),
4099 m_target_sp (target_sp),
4100 m_module_list (module_list)
4101{
4102}
4103
4104Target::TargetEventData::~TargetEventData()
4105{
4106}
4107
4108const ConstString &
4109Target::TargetEventData::GetFlavorString ()
4110{
4111 static ConstString g_flavor ("Target::TargetEventData");
4112 return g_flavor;
4113}
4114
4115void
4116Target::TargetEventData::Dump (Stream *s) const
4117{
4118}
4119
4120const Target::TargetEventData *
4121Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4122{
4123 if (event_ptr)
4124 {
4125 const EventData *event_data = event_ptr->GetData();
4126 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4127 return static_cast <const TargetEventData *> (event_ptr->GetData());
4128 }
4129 return NULL;
4130}
4131
4132TargetSP
4133Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4134{
4135 TargetSP target_sp;
4136 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4137 if (event_data)
4138 target_sp = event_data->m_target_sp;
4139 return target_sp;
4140}
4141
4142ModuleList
4143Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4144{
4145 ModuleList module_list;
4146 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4147 if (event_data)
4148 module_list = event_data->m_module_list;
4149 return module_list;
4150}