blob: 86b6b99b0e79decdee1e1a6f90ecd21d03d93f56 [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010// C Includes
11// C++ Includes
12// Other libraries and framework includes
13// Project includes
Eugene Zelenko8f30a652015-10-23 18:39:37 +000014#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Breakpoint/BreakpointResolver.h"
16#include "lldb/Breakpoint/BreakpointResolverAddress.h"
17#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000018#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000020#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000021#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Core/Event.h"
23#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000024#include "lldb/Core/Module.h"
25#include "lldb/Core/ModuleSpec.h"
26#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000027#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000028#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000029#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000031#include "lldb/Core/Timer.h"
32#include "lldb/Core/ValueObject.h"
Sean Callanan66810412015-10-19 23:11:07 +000033#include "lldb/Expression/REPL.h"
Jim Ingham151c0322015-09-15 21:13:50 +000034#include "lldb/Expression/UserExpression.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000035#include "Plugins/ExpressionParser/Clang/ClangASTSource.h"
36#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
37#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000038#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000040#include "lldb/Interpreter/CommandInterpreter.h"
41#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000042#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000043#include "lldb/Interpreter/OptionValues.h"
44#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000045#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ObjectFile.h"
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000047#include "lldb/Symbol/Function.h"
48#include "lldb/Symbol/Symbol.h"
Jim Ingham151c0322015-09-15 21:13:50 +000049#include "lldb/Target/Language.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000050#include "lldb/Target/LanguageRuntime.h"
51#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000053#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000054#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000055#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000056#include "lldb/Target/Thread.h"
57#include "lldb/Target/ThreadSpec.h"
Sean Callanan9a6940c2015-10-21 00:36:34 +000058#include "lldb/Utility/LLDBAssert.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
Jim Ingham893c9322014-11-22 01:42:44 +000070Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000071 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000072 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000073 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000074 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000075 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000076 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000077 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000078 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000079 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080 m_breakpoint_list (false),
81 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000082 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000083 m_process_sp (),
84 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085 m_image_search_paths (ImageSearchPathsChanged, this),
Sean Callanana3444ff2015-11-10 22:54:42 +000086 m_ast_importer_sp (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000087 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000088 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000089 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000090 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000091 m_suppress_stop_hooks (false),
92 m_is_dummy_target(is_dummy_target)
93
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000095 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
96 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
97 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000098 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +000099 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000100
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000101 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000102
Greg Clayton5160ce52013-03-27 23:08:40 +0000103 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000105 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000106 if (m_arch.IsValid())
107 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000108 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 +0000109 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000112Target::~Target()
113{
114 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
115 if (log)
116 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
117 DeleteCurrentProcess ();
118}
119
Jim Ingham893c9322014-11-22 01:42:44 +0000120void
121Target::PrimeFromDummyTarget(Target *target)
122{
123 if (!target)
124 return;
125
126 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000127
128 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
129 {
130 if (breakpoint_sp->IsInternal())
131 continue;
132
133 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
134 AddBreakpoint (new_bp, false);
135 }
Jim Ingham893c9322014-11-22 01:42:44 +0000136}
137
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000138void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000139Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140{
Greg Clayton89411422010-10-08 00:21:05 +0000141// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000142 if (description_level != lldb::eDescriptionLevelBrief)
143 {
144 s->Indent();
145 s->PutCString("Target\n");
146 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000147 m_images.Dump(s);
148 m_breakpoint_list.Dump(s);
149 m_internal_breakpoint_list.Dump(s);
150 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000151 }
152 else
153 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000154 Module *exe_module = GetExecutableModulePointer();
155 if (exe_module)
156 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000157 else
158 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000159 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160}
161
162void
Greg Clayton90ba8112012-12-05 00:16:59 +0000163Target::CleanupProcess ()
164{
165 // Do any cleanup of the target we need to do between process instances.
166 // NB It is better to do this before destroying the process in case the
167 // clean up needs some help from the process.
168 m_breakpoint_list.ClearAllBreakpointSites();
169 m_internal_breakpoint_list.ClearAllBreakpointSites();
170 // Disable watchpoints just on the debugger side.
171 Mutex::Locker locker;
172 this->GetWatchpointList().GetListMutex(locker);
173 DisableAllWatchpoints(false);
174 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000175 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000176}
177
178void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000179Target::DeleteCurrentProcess ()
180{
181 if (m_process_sp.get())
182 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000183 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 if (m_process_sp->IsAlive())
Jason Molendaede31932015-04-17 05:01:58 +0000185 m_process_sp->Destroy(false);
Jim Inghamd0a3e122011-02-16 17:54:55 +0000186
187 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188
Greg Clayton90ba8112012-12-05 00:16:59 +0000189 CleanupProcess ();
190
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191 m_process_sp.reset();
192 }
193}
194
195const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000196Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000197{
198 DeleteCurrentProcess ();
Zachary Turner7529df92015-09-01 20:02:29 +0000199 m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000200 return m_process_sp;
201}
202
203const lldb::ProcessSP &
204Target::GetProcessSP () const
205{
206 return m_process_sp;
207}
208
Sean Callanan66810412015-10-19 23:11:07 +0000209lldb::REPLSP
Sean Callanan3e7e9152015-10-20 00:23:46 +0000210Target::GetREPL (Error &err, lldb::LanguageType language, const char *repl_options, bool can_create)
Sean Callanan66810412015-10-19 23:11:07 +0000211{
212 if (language == eLanguageTypeUnknown)
213 {
Sean Callanan93c0b002015-10-21 19:14:33 +0000214 std::set<LanguageType> repl_languages;
215
216 Language::GetLanguagesSupportingREPLs(repl_languages);
217
218 if (repl_languages.size() == 1)
219 {
220 language = *repl_languages.begin();
221 }
Sean Callanan97f84e82015-10-21 19:31:17 +0000222 else if (repl_languages.size() == 0)
223 {
224 err.SetErrorStringWithFormat("LLDB isn't configured with support support for any REPLs.");
225 return REPLSP();
226 }
Sean Callanan93c0b002015-10-21 19:14:33 +0000227 else
228 {
229 err.SetErrorStringWithFormat("Multiple possible REPL languages. Please specify a language.");
Sean Callanan97f84e82015-10-21 19:31:17 +0000230 return REPLSP();
Sean Callanan93c0b002015-10-21 19:14:33 +0000231 }
Sean Callanan66810412015-10-19 23:11:07 +0000232 }
233
234 REPLMap::iterator pos = m_repl_map.find(language);
235
236 if (pos != m_repl_map.end())
237 {
238 return pos->second;
239 }
240
241 if (!can_create)
242 {
Sean Callanan3e7e9152015-10-20 00:23:46 +0000243 err.SetErrorStringWithFormat("Couldn't find an existing REPL for %s, and can't create a new one", Language::GetNameForLanguageType(language));
Sean Callanan66810412015-10-19 23:11:07 +0000244 return lldb::REPLSP();
245 }
246
Sean Callanan3b682de2015-10-21 00:28:44 +0000247 Debugger *const debugger = nullptr;
248 lldb::REPLSP ret = REPL::Create(err, language, debugger, this, repl_options);
Sean Callanan66810412015-10-19 23:11:07 +0000249
250 if (ret)
251 {
252 m_repl_map[language] = ret;
253 return m_repl_map[language];
254 }
255
Sean Callanan3e7e9152015-10-20 00:23:46 +0000256 if (err.Success())
257 {
258 err.SetErrorStringWithFormat("Couldn't create a REPL for %s", Language::GetNameForLanguageType(language));
259 }
260
261 return lldb::REPLSP();
Sean Callanan66810412015-10-19 23:11:07 +0000262}
263
Greg Clayton3418c852011-08-10 02:10:13 +0000264void
Sean Callanan9a6940c2015-10-21 00:36:34 +0000265Target::SetREPL (lldb::LanguageType language, lldb::REPLSP repl_sp)
266{
267 lldbassert(!m_repl_map.count(language));
268
269 m_repl_map[language] = repl_sp;
270}
271
272void
Greg Clayton3418c852011-08-10 02:10:13 +0000273Target::Destroy()
274{
275 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000276 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000277 DeleteCurrentProcess ();
278 m_platform_sp.reset();
279 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000280 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000281 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000282 const bool notify = false;
283 m_breakpoint_list.RemoveAll(notify);
284 m_internal_breakpoint_list.RemoveAll(notify);
285 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000286 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000287 m_search_filter_sp.reset();
288 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000289 m_stop_hooks.clear();
290 m_stop_hook_next_id = 0;
291 m_suppress_stop_hooks = false;
292}
293
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294BreakpointList &
295Target::GetBreakpointList(bool internal)
296{
297 if (internal)
298 return m_internal_breakpoint_list;
299 else
300 return m_breakpoint_list;
301}
302
303const BreakpointList &
304Target::GetBreakpointList(bool internal) const
305{
306 if (internal)
307 return m_internal_breakpoint_list;
308 else
309 return m_breakpoint_list;
310}
311
312BreakpointSP
313Target::GetBreakpointByID (break_id_t break_id)
314{
315 BreakpointSP bp_sp;
316
317 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
318 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
319 else
320 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
321
322 return bp_sp;
323}
324
325BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000326Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000327 const FileSpecList *source_file_spec_list,
328 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000329 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000330 bool hardware,
331 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332{
Jim Ingham87df91b2011-09-23 00:54:11 +0000333 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000334 if (move_to_nearest_code == eLazyBoolCalculate)
335 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
336 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex (NULL, source_regex, !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000337 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000338}
339
Jim Ingham969795f2011-09-21 01:17:13 +0000340BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000341Target::CreateBreakpoint (const FileSpecList *containingModules,
342 const FileSpec &file,
343 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000344 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000345 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000346 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000347 bool hardware,
348 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000349{
Greg Clayton1f746072012-08-29 21:13:06 +0000350 if (check_inlines == eLazyBoolCalculate)
351 {
352 const InlineStrategy inline_strategy = GetInlineStrategy();
353 switch (inline_strategy)
354 {
355 case eInlineBreakpointsNever:
356 check_inlines = eLazyBoolNo;
357 break;
358
359 case eInlineBreakpointsHeaders:
360 if (file.IsSourceImplementationFile())
361 check_inlines = eLazyBoolNo;
362 else
363 check_inlines = eLazyBoolYes;
364 break;
365
366 case eInlineBreakpointsAlways:
367 check_inlines = eLazyBoolYes;
368 break;
369 }
370 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000371 SearchFilterSP filter_sp;
372 if (check_inlines == eLazyBoolNo)
373 {
374 // Not checking for inlines, we are looking only for matching compile units
375 FileSpecList compile_unit_list;
376 compile_unit_list.Append (file);
377 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
378 }
379 else
380 {
381 filter_sp = GetSearchFilterForModuleList (containingModules);
382 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000383 if (skip_prologue == eLazyBoolCalculate)
384 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000385 if (move_to_nearest_code == eLazyBoolCalculate)
386 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000387
Greg Clayton1f746072012-08-29 21:13:06 +0000388 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (NULL,
389 file,
390 line_no,
391 check_inlines,
Ilia K055ad9b2015-05-18 13:41:01 +0000392 skip_prologue,
393 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000394 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395}
396
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000398Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000401
402 // Check for any reason we want to move this breakpoint to other address.
403 addr = GetBreakableLoadAddress(addr);
404
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 // Attempt to resolve our load address if possible, though it is ok if
406 // it doesn't resolve to section/offset.
407
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000408 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000409 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000410 if (!so_addr.IsValid())
411 {
412 // The address didn't resolve, so just set this as an absolute address
413 so_addr.SetOffset (addr);
414 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000415 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000416 return bp_sp;
417}
418
419BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000420Target::CreateBreakpoint (Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000422 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 BreakpointResolverSP resolver_sp (new BreakpointResolverAddress (NULL, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000424 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425}
426
427BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000428Target::CreateBreakpoint (const FileSpecList *containingModules,
429 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000430 const char *func_name,
431 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000432 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000433 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000434 bool internal,
435 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000437 BreakpointSP bp_sp;
438 if (func_name)
439 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000440 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000441
442 if (skip_prologue == eLazyBoolCalculate)
443 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000444 if (language == lldb::eLanguageTypeUnknown)
445 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000446
Greg Claytond16e1e52011-07-12 17:06:17 +0000447 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
448 func_name,
449 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000450 language,
Greg Claytond16e1e52011-07-12 17:06:17 +0000451 Breakpoint::Exact,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000452 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000453 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000454 }
455 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000456}
457
Jim Inghamfab10e82012-03-06 00:37:27 +0000458lldb::BreakpointSP
459Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000460 const FileSpecList *containingSourceFiles,
461 const std::vector<std::string> &func_names,
462 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000463 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000464 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000465 bool internal,
466 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000467{
468 BreakpointSP bp_sp;
469 size_t num_names = func_names.size();
470 if (num_names > 0)
471 {
472 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000473
474 if (skip_prologue == eLazyBoolCalculate)
475 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000476 if (language == lldb::eLanguageTypeUnknown)
477 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000478
479 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Inghamfab10e82012-03-06 00:37:27 +0000480 func_names,
481 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000482 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000483 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000484 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000485 }
486 return bp_sp;
487}
488
Jim Ingham133e0fb2012-03-03 02:05:11 +0000489BreakpointSP
490Target::CreateBreakpoint (const FileSpecList *containingModules,
491 const FileSpecList *containingSourceFiles,
492 const char *func_names[],
493 size_t num_names,
494 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000495 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000496 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000497 bool internal,
498 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000499{
500 BreakpointSP bp_sp;
501 if (num_names > 0)
502 {
503 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
504
Greg Clayton03da4cc2013-04-19 21:31:16 +0000505 if (skip_prologue == eLazyBoolCalculate)
506 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000507 if (language == lldb::eLanguageTypeUnknown)
508 language = GetLanguage();
509
Greg Clayton03da4cc2013-04-19 21:31:16 +0000510 BreakpointResolverSP resolver_sp (new BreakpointResolverName (NULL,
Jim Ingham133e0fb2012-03-03 02:05:11 +0000511 func_names,
512 num_names,
Jim Inghamfab10e82012-03-06 00:37:27 +0000513 func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000514 language,
Greg Clayton03da4cc2013-04-19 21:31:16 +0000515 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000516 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000517 }
518 return bp_sp;
519}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000520
521SearchFilterSP
522Target::GetSearchFilterForModule (const FileSpec *containingModule)
523{
524 SearchFilterSP filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 if (containingModule != NULL)
526 {
527 // TODO: We should look into sharing module based search filters
528 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000529 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 }
531 else
532 {
533 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000534 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535 filter_sp = m_search_filter_sp;
536 }
537 return filter_sp;
538}
539
Jim Ingham969795f2011-09-21 01:17:13 +0000540SearchFilterSP
541Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
542{
543 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000544 if (containingModules && containingModules->GetSize() != 0)
545 {
546 // TODO: We should look into sharing module based search filters
547 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000548 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000549 }
550 else
551 {
552 if (m_search_filter_sp.get() == NULL)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000553 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000554 filter_sp = m_search_filter_sp;
555 }
556 return filter_sp;
557}
558
Jim Ingham87df91b2011-09-23 00:54:11 +0000559SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000560Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
561 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000562{
563 if (containingSourceFiles == NULL || containingSourceFiles->GetSize() == 0)
564 return GetSearchFilterForModuleList(containingModules);
565
566 SearchFilterSP filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000567 if (containingModules == NULL)
568 {
569 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
570 // but that will take a little reworking.
571
Greg Claytone1cd1be2012-01-29 20:56:30 +0000572 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000573 }
574 else
575 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000576 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000577 }
578 return filter_sp;
579}
580
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000582Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000583 const FileSpecList *containingSourceFiles,
584 RegularExpression &func_regex,
Jim Ingham0fcdac32015-11-06 22:48:59 +0000585 lldb::LanguageType requested_language,
Jim Inghama8558b62012-05-22 00:12:20 +0000586 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000587 bool internal,
588 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589{
Jim Ingham87df91b2011-09-23 00:54:11 +0000590 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000591 bool skip =
592 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
593 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000594 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
Jim Ingham0fcdac32015-11-06 22:48:59 +0000595 func_regex,
596 requested_language,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000597 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598
Jim Ingham1460e4b2014-01-10 23:46:59 +0000599 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600}
601
Jim Ingham219ba192012-03-05 04:47:34 +0000602lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000603Target::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 +0000604{
Jim Inghama72b31c2015-04-22 19:42:18 +0000605 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
606 if (exc_bkpt_sp && additional_args)
607 {
608 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
609 if (precondition_sp && additional_args)
610 {
611 if (error)
612 *error = precondition_sp->ConfigurePrecondition(*additional_args);
613 else
614 precondition_sp->ConfigurePrecondition(*additional_args);
615 }
616 }
617 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000618}
Jim Inghama72b31c2015-04-22 19:42:18 +0000619
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000621Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000622{
623 BreakpointSP bp_sp;
624 if (filter_sp && resolver_sp)
625 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000626 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000628 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000630 return bp_sp;
631}
632
633void
634Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
635{
636 if (!bp_sp)
637 return;
638 if (internal)
639 m_internal_breakpoint_list.Add (bp_sp, false);
640 else
641 m_breakpoint_list.Add (bp_sp, true);
642
643 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
644 if (log)
645 {
646 StreamString s;
647 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
648 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
649 }
650
651 bp_sp->ResolveBreakpoint();
652
653 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000654 {
655 m_last_created_breakpoint = bp_sp;
656 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657}
658
Johnny Chen86364b42011-09-20 23:28:55 +0000659bool
660Target::ProcessIsValid()
661{
662 return (m_process_sp && m_process_sp->IsAlive());
663}
664
Johnny Chenb90827e2012-06-04 23:19:54 +0000665static bool
666CheckIfWatchpointsExhausted(Target *target, Error &error)
667{
668 uint32_t num_supported_hardware_watchpoints;
669 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
670 if (rc.Success())
671 {
672 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
673 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
674 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
675 num_supported_hardware_watchpoints);
676 }
677 return false;
678}
679
Johnny Chen01a67862011-10-14 00:42:25 +0000680// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000681// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000682WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000683Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000684{
Greg Clayton5160ce52013-03-27 23:08:40 +0000685 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000686 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000687 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000688 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000689
Johnny Chen01a67862011-10-14 00:42:25 +0000690 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000691 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000692 {
693 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000694 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000695 }
Jim Inghamc6462312013-06-18 21:52:48 +0000696
Johnny Chen45e541f2011-09-14 22:20:15 +0000697 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000698 {
699 if (size == 0)
700 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
701 else
Daniel Malead01b2952012-11-29 21:49:15 +0000702 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000703 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000704 }
Jim Inghamc6462312013-06-18 21:52:48 +0000705
706 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
707 {
708 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
709 }
Johnny Chen7313a642011-09-13 01:15:36 +0000710
Johnny Chen01a67862011-10-14 00:42:25 +0000711 // Currently we only support one watchpoint per address, with total number
712 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000713
714 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000715 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 +0000716 Mutex::Locker locker;
717 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000718 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000719 if (matched_sp)
720 {
Johnny Chen0c406372011-09-14 20:23:45 +0000721 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000722 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000723 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
724 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000725 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000726 if (size == old_size && kind == old_type)
727 {
Johnny Chen01a67862011-10-14 00:42:25 +0000728 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000729 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000730 }
731 else
732 {
Johnny Chen01a67862011-10-14 00:42:25 +0000733 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000734 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
735 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000736 }
Johnny Chen3c532582011-09-13 23:29:31 +0000737 }
738
Jason Molenda727e3922012-12-05 23:07:34 +0000739 if (!wp_sp)
740 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000741 wp_sp.reset(new Watchpoint(*this, addr, size, type));
742 wp_sp->SetWatchpointType(kind, notify);
743 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000744 }
Johnny Chen0c406372011-09-14 20:23:45 +0000745
Jim Ingham1b5792e2012-12-18 02:03:49 +0000746 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000747 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000748 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
749 __FUNCTION__,
750 error.Success() ? "succeeded" : "failed",
751 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000752
Jason Molenda727e3922012-12-05 23:07:34 +0000753 if (error.Fail())
754 {
Johnny Chen41b77262012-03-26 22:00:10 +0000755 // Enabling the watchpoint on the device side failed.
756 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000757 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000758 // See if we could provide more helpful error message.
759 if (!CheckIfWatchpointsExhausted(this, error))
760 {
761 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000762 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000763 }
Johnny Chen01a67862011-10-14 00:42:25 +0000764 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000765 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000766 else
Johnny Chen01a67862011-10-14 00:42:25 +0000767 m_last_created_watchpoint = wp_sp;
768 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000769}
770
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771void
772Target::RemoveAllBreakpoints (bool internal_also)
773{
Greg Clayton5160ce52013-03-27 23:08:40 +0000774 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775 if (log)
776 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
777
Greg Clayton9fed0d82010-07-23 23:33:17 +0000778 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000780 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000781
782 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783}
784
785void
786Target::DisableAllBreakpoints (bool internal_also)
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 (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
791
792 m_breakpoint_list.SetEnabledAll (false);
793 if (internal_also)
794 m_internal_breakpoint_list.SetEnabledAll (false);
795}
796
797void
798Target::EnableAllBreakpoints (bool internal_also)
799{
Greg Clayton5160ce52013-03-27 23:08:40 +0000800 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 if (log)
802 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
803
804 m_breakpoint_list.SetEnabledAll (true);
805 if (internal_also)
806 m_internal_breakpoint_list.SetEnabledAll (true);
807}
808
809bool
810Target::RemoveBreakpointByID (break_id_t break_id)
811{
Greg Clayton5160ce52013-03-27 23:08:40 +0000812 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 if (log)
814 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
815
816 if (DisableBreakpointByID (break_id))
817 {
818 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000819 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000821 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000822 if (m_last_created_breakpoint)
823 {
824 if (m_last_created_breakpoint->GetID() == break_id)
825 m_last_created_breakpoint.reset();
826 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000827 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000828 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 return true;
830 }
831 return false;
832}
833
834bool
835Target::DisableBreakpointByID (break_id_t break_id)
836{
Greg Clayton5160ce52013-03-27 23:08:40 +0000837 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 if (log)
839 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, 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 if (bp_sp)
848 {
849 bp_sp->SetEnabled (false);
850 return true;
851 }
852 return false;
853}
854
855bool
856Target::EnableBreakpointByID (break_id_t break_id)
857{
Greg Clayton5160ce52013-03-27 23:08:40 +0000858 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 if (log)
860 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
861 __FUNCTION__,
862 break_id,
863 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
864
865 BreakpointSP bp_sp;
866
867 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
868 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
869 else
870 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
871
872 if (bp_sp)
873 {
874 bp_sp->SetEnabled (true);
875 return true;
876 }
877 return false;
878}
879
Johnny Chenedf50372011-09-23 21:21:43 +0000880// The flag 'end_to_end', default to true, signifies that the operation is
881// performed end to end, for both the debugger and the debuggee.
882
Johnny Chen01a67862011-10-14 00:42:25 +0000883// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
884// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000885bool
Johnny Chen01a67862011-10-14 00:42:25 +0000886Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000887{
Greg Clayton5160ce52013-03-27 23:08:40 +0000888 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000889 if (log)
890 log->Printf ("Target::%s\n", __FUNCTION__);
891
Johnny Chenedf50372011-09-23 21:21:43 +0000892 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000893 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000894 return true;
895 }
896
897 // Otherwise, it's an end to end operation.
898
Johnny Chen86364b42011-09-20 23:28:55 +0000899 if (!ProcessIsValid())
900 return false;
901
Johnny Chen01a67862011-10-14 00:42:25 +0000902 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000903 for (size_t i = 0; i < num_watchpoints; ++i)
904 {
Johnny Chen01a67862011-10-14 00:42:25 +0000905 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
906 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000907 return false;
908
Johnny Chen01a67862011-10-14 00:42:25 +0000909 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000910 if (rc.Fail())
911 return false;
912 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000913 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000914 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000915 return true; // Success!
916}
917
Johnny Chen01a67862011-10-14 00:42:25 +0000918// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
919// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000920bool
Johnny Chen01a67862011-10-14 00:42:25 +0000921Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000922{
Greg Clayton5160ce52013-03-27 23:08:40 +0000923 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000924 if (log)
925 log->Printf ("Target::%s\n", __FUNCTION__);
926
Johnny Chenedf50372011-09-23 21:21:43 +0000927 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000928 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000929 return true;
930 }
931
932 // Otherwise, it's an end to end operation.
933
Johnny Chen86364b42011-09-20 23:28:55 +0000934 if (!ProcessIsValid())
935 return false;
936
Johnny Chen01a67862011-10-14 00:42:25 +0000937 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000938 for (size_t i = 0; i < num_watchpoints; ++i)
939 {
Johnny Chen01a67862011-10-14 00:42:25 +0000940 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
941 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000942 return false;
943
Johnny Chen01a67862011-10-14 00:42:25 +0000944 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000945 if (rc.Fail())
946 return false;
947 }
Johnny Chen86364b42011-09-20 23:28:55 +0000948 return true; // Success!
949}
950
Johnny Chen01a67862011-10-14 00:42:25 +0000951// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
952// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000953bool
Johnny Chen01a67862011-10-14 00:42:25 +0000954Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000955{
Greg Clayton5160ce52013-03-27 23:08:40 +0000956 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000957 if (log)
958 log->Printf ("Target::%s\n", __FUNCTION__);
959
Johnny Chenedf50372011-09-23 21:21:43 +0000960 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000961 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000962 return true;
963 }
964
965 // Otherwise, it's an end to end operation.
966
Johnny Chen86364b42011-09-20 23:28:55 +0000967 if (!ProcessIsValid())
968 return false;
969
Johnny Chen01a67862011-10-14 00:42:25 +0000970 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000971 for (size_t i = 0; i < num_watchpoints; ++i)
972 {
Johnny Chen01a67862011-10-14 00:42:25 +0000973 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
974 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000975 return false;
976
Johnny Chen01a67862011-10-14 00:42:25 +0000977 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000978 if (rc.Fail())
979 return false;
980 }
Johnny Chen86364b42011-09-20 23:28:55 +0000981 return true; // Success!
982}
983
Johnny Chena4d6bc92012-02-25 06:44:30 +0000984// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
985bool
986Target::ClearAllWatchpointHitCounts ()
987{
Greg Clayton5160ce52013-03-27 23:08:40 +0000988 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000989 if (log)
990 log->Printf ("Target::%s\n", __FUNCTION__);
991
992 size_t num_watchpoints = m_watchpoint_list.GetSize();
993 for (size_t i = 0; i < num_watchpoints; ++i)
994 {
995 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
996 if (!wp_sp)
997 return false;
998
999 wp_sp->ResetHitCount();
1000 }
1001 return true; // Success!
1002}
1003
Enrico Granata5e3fe042015-02-11 00:37:54 +00001004// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1005bool
1006Target::ClearAllWatchpointHistoricValues ()
1007{
1008 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
1009 if (log)
1010 log->Printf ("Target::%s\n", __FUNCTION__);
1011
1012 size_t num_watchpoints = m_watchpoint_list.GetSize();
1013 for (size_t i = 0; i < num_watchpoints; ++i)
1014 {
1015 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1016 if (!wp_sp)
1017 return false;
1018
1019 wp_sp->ResetHistoricValues();
1020 }
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 Chen6cc60e82011-10-05 21:35:46 +00001025// during these operations.
1026bool
Johnny Chen01a67862011-10-14 00:42:25 +00001027Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001028{
Greg Clayton5160ce52013-03-27 23:08:40 +00001029 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001030 if (log)
1031 log->Printf ("Target::%s\n", __FUNCTION__);
1032
1033 if (!ProcessIsValid())
1034 return false;
1035
Johnny Chen01a67862011-10-14 00:42:25 +00001036 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +00001037 for (size_t i = 0; i < num_watchpoints; ++i)
1038 {
Johnny Chen01a67862011-10-14 00:42:25 +00001039 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1040 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001041 return false;
1042
Johnny Chen01a67862011-10-14 00:42:25 +00001043 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001044 }
1045 return true; // Success!
1046}
1047
Johnny Chen01a67862011-10-14 00:42:25 +00001048// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001049bool
Johnny Chen01a67862011-10-14 00:42:25 +00001050Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001051{
Greg Clayton5160ce52013-03-27 23:08:40 +00001052 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001053 if (log)
1054 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1055
1056 if (!ProcessIsValid())
1057 return false;
1058
Johnny Chen01a67862011-10-14 00:42:25 +00001059 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1060 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001061 {
Johnny Chen01a67862011-10-14 00:42:25 +00001062 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001063 if (rc.Success())
1064 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001065
Johnny Chenf04ee932011-09-22 18:04:58 +00001066 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001067 }
1068 return false;
1069}
1070
Johnny Chen01a67862011-10-14 00:42:25 +00001071// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001072bool
Johnny Chen01a67862011-10-14 00:42:25 +00001073Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001074{
Greg Clayton5160ce52013-03-27 23:08:40 +00001075 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001076 if (log)
1077 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1078
1079 if (!ProcessIsValid())
1080 return false;
1081
Johnny Chen01a67862011-10-14 00:42:25 +00001082 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1083 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001084 {
Johnny Chen01a67862011-10-14 00:42:25 +00001085 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001086 if (rc.Success())
1087 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001088
Johnny Chenf04ee932011-09-22 18:04:58 +00001089 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001090 }
1091 return false;
1092}
1093
Johnny Chen01a67862011-10-14 00:42:25 +00001094// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001095bool
Johnny Chen01a67862011-10-14 00:42:25 +00001096Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001097{
Greg Clayton5160ce52013-03-27 23:08:40 +00001098 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001099 if (log)
1100 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1101
Jim Inghamb0b45132013-07-02 02:09:46 +00001102 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1103 if (watch_to_remove_sp == m_last_created_watchpoint)
1104 m_last_created_watchpoint.reset();
1105
Johnny Chen01a67862011-10-14 00:42:25 +00001106 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001107 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001108 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001109 return true;
1110 }
1111 return false;
1112}
1113
Johnny Chen01a67862011-10-14 00:42:25 +00001114// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001115bool
Johnny Chen01a67862011-10-14 00:42:25 +00001116Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001117{
Greg Clayton5160ce52013-03-27 23:08:40 +00001118 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001119 if (log)
1120 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1121
1122 if (!ProcessIsValid())
1123 return false;
1124
Johnny Chen01a67862011-10-14 00:42:25 +00001125 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1126 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001127 {
Johnny Chen01a67862011-10-14 00:42:25 +00001128 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001129 return true;
1130 }
1131 return false;
1132}
1133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134ModuleSP
1135Target::GetExecutableModule ()
1136{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001137 // search for the first executable in the module list
1138 for (size_t i = 0; i < m_images.GetSize(); ++i)
1139 {
1140 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1141 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1142 if (obj == nullptr)
1143 continue;
1144 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1145 return module_sp;
1146 }
1147 // as fall back return the first module loaded
1148 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001149}
1150
1151Module*
1152Target::GetExecutableModulePointer ()
1153{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001154 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155}
1156
Enrico Granata17598482012-11-08 02:22:02 +00001157static void
1158LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1159{
1160 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001161 StreamString feedback_stream;
1162 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001163 {
Enrico Granata97303392013-05-21 00:00:30 +00001164 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001165 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001166 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1167 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001168 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001169 if (feedback_stream.GetSize())
1170 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1171 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001172}
1173
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174void
Greg Claytonb35db632013-11-09 00:03:31 +00001175Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001176{
Greg Claytonb35db632013-11-09 00:03:31 +00001177 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001178 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001179 m_images.Clear();
Greg Clayton5beec212015-10-08 21:04:34 +00001180 m_scratch_type_system_map.Clear();
Sean Callanana3444ff2015-11-10 22:54:42 +00001181 m_ast_importer_sp.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001182}
1183
1184void
Greg Claytonb35db632013-11-09 00:03:31 +00001185Target::DidExec ()
1186{
1187 // When a process exec's we need to know about it so we can do some cleanup.
1188 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1189 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1190}
1191
1192void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001193Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1194{
1195 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001196 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197
1198 if (executable_sp.get())
1199 {
1200 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001201 "Target::SetExecutableModule (executable = '%s')",
1202 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001204 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205
Jim Ingham5aee1622010-08-09 23:31:02 +00001206 // 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 +00001207 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001208 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001209 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001210 if (log)
1211 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1212 }
1213
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001215 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001217 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218 {
1219 executable_objfile->GetDependentModules(dependent_files);
1220 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1221 {
Greg Claytonded470d2011-03-19 01:12:21 +00001222 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1223 FileSpec platform_dependent_file_spec;
1224 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001225 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001226 else
1227 platform_dependent_file_spec = dependent_file_spec;
1228
Greg Claytonb9a01b32012-02-26 05:51:37 +00001229 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1230 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231 if (image_module_sp.get())
1232 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233 ObjectFile *objfile = image_module_sp->GetObjectFile();
1234 if (objfile)
1235 objfile->GetDependentModules(dependent_files);
1236 }
1237 }
1238 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 }
1240}
1241
Jim Ingham5aee1622010-08-09 23:31:02 +00001242bool
1243Target::SetArchitecture (const ArchSpec &arch_spec)
1244{
Greg Clayton5160ce52013-03-27 23:08:40 +00001245 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Jason Molendadad8af42015-11-10 04:11:37 +00001246 bool missing_local_arch = (false == m_arch.IsValid());
1247 bool replace_local_arch = true;
1248 bool compatible_local_arch = false;
1249 ArchSpec other(arch_spec);
1250
1251 if (!missing_local_arch)
Jim Ingham5aee1622010-08-09 23:31:02 +00001252 {
Jason Molendadad8af42015-11-10 04:11:37 +00001253 if (m_arch.IsCompatibleMatch(arch_spec))
1254 {
1255 other.MergeFrom(m_arch);
1256
1257 if (m_arch.IsCompatibleMatch(other))
1258 {
1259 compatible_local_arch = true;
1260 bool arch_changed, vendor_changed, os_changed, os_ver_changed, env_changed;
1261
1262 m_arch.PiecewiseTripleCompare(other,
1263 arch_changed,
1264 vendor_changed,
1265 os_changed,
1266 os_ver_changed,
1267 env_changed);
1268
1269 if (!arch_changed && !vendor_changed && !os_changed)
1270 replace_local_arch = false;
1271 }
1272 }
1273 }
1274
1275 if (compatible_local_arch || missing_local_arch)
1276 {
1277 // If we haven't got a valid arch spec, or the architectures are compatible
1278 // update the architecture, unless the one we already have is more specified
1279 if (replace_local_arch)
1280 m_arch = other;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001281 if (log)
Jason Molendadad8af42015-11-10 04:11:37 +00001282 log->Printf ("Target::SetArchitecture set architecture to %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jim Ingham5aee1622010-08-09 23:31:02 +00001283 return true;
1284 }
Jason Molendadad8af42015-11-10 04:11:37 +00001285
1286 // If we have an executable file, try to reset the executable to the desired architecture
1287 if (log)
1288 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1289 m_arch = other;
1290 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001291
Jason Molendadad8af42015-11-10 04:11:37 +00001292 ClearModules(true);
1293 // Need to do something about unsetting breakpoints.
1294
1295 if (executable_sp)
1296 {
1297 if (log)
1298 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1299 ModuleSpec module_spec (executable_sp->GetFileSpec(), other);
1300 Error error = ModuleList::GetSharedModule (module_spec,
1301 executable_sp,
1302 &GetExecutableSearchPaths(),
1303 NULL,
1304 NULL);
1305
1306 if (!error.Fail() && executable_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +00001307 {
Jason Molendadad8af42015-11-10 04:11:37 +00001308 SetExecutableModule (executable_sp, true);
1309 return true;
Jim Ingham5aee1622010-08-09 23:31:02 +00001310 }
1311 }
Greg Clayton70512312012-05-08 01:45:38 +00001312 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001313}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001314
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001315bool
1316Target::MergeArchitecture (const ArchSpec &arch_spec)
1317{
1318 if (arch_spec.IsValid())
1319 {
1320 if (m_arch.IsCompatibleMatch(arch_spec))
1321 {
1322 // The current target arch is compatible with "arch_spec", see if we
1323 // can improve our current architecture using bits from "arch_spec"
1324
1325 // Merge bits from arch_spec into "merged_arch" and set our architecture
1326 ArchSpec merged_arch (m_arch);
1327 merged_arch.MergeFrom (arch_spec);
1328 return SetArchitecture(merged_arch);
1329 }
1330 else
1331 {
1332 // The new architecture is different, we just need to replace it
1333 return SetArchitecture(arch_spec);
1334 }
1335 }
1336 return false;
1337}
1338
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001339void
Enrico Granataefe637d2012-11-08 19:16:03 +00001340Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001341{
1342}
1343
1344void
Enrico Granataefe637d2012-11-08 19:16:03 +00001345Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001346{
1347 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001348 if (m_valid)
1349 {
1350 ModuleList my_module_list;
1351 my_module_list.Append(module_sp);
1352 LoadScriptingResourceForModule(module_sp, this);
1353 ModulesDidLoad (my_module_list);
1354 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355}
1356
1357void
Enrico Granataefe637d2012-11-08 19:16:03 +00001358Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001359{
Adrian McCarthy6ecdbc82015-10-15 22:39:55 +00001360 // A module is being removed from this target.
Greg Clayton23f8c952014-03-24 23:10:19 +00001361 if (m_valid)
1362 {
1363 ModuleList my_module_list;
1364 my_module_list.Append(module_sp);
1365 ModulesDidUnload (my_module_list, false);
1366 }
Enrico Granata17598482012-11-08 02:22:02 +00001367}
1368
1369void
Enrico Granataefe637d2012-11-08 19:16:03 +00001370Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001371{
Jim Inghame716ae02011-08-03 01:00:06 +00001372 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001373 if (m_valid)
Jason Molendad91c3fd2015-11-10 04:18:12 +00001374 {
Greg Clayton23f8c952014-03-24 23:10:19 +00001375 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001376 m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
1377 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001378}
1379
1380void
1381Target::ModulesDidLoad (ModuleList &module_list)
1382{
Greg Clayton23f8c952014-03-24 23:10:19 +00001383 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001384 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001385 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001386 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001387 if (m_process_sp)
1388 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001389 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001390 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001391 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001392 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001393}
1394
1395void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001396Target::SymbolsDidLoad (ModuleList &module_list)
1397{
Greg Clayton23f8c952014-03-24 23:10:19 +00001398 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001399 {
Jim Ingham31caf982013-06-04 23:01:35 +00001400 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001401 {
Jim Ingham31caf982013-06-04 23:01:35 +00001402 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1403 if (runtime)
1404 {
1405 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1406 objc_runtime->SymbolsDidLoad(module_list);
1407 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001408 }
Jim Ingham31caf982013-06-04 23:01:35 +00001409
Greg Clayton095eeaa2013-11-05 23:28:00 +00001410 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001411 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001412 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001413 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001414}
1415
1416void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001417Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001418{
Greg Clayton23f8c952014-03-24 23:10:19 +00001419 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001420 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001421 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001422 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001423 m_internal_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001424 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001425 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426}
1427
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001428bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001429Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001430{
Greg Clayton67cc0632012-08-22 17:17:09 +00001431 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001432 {
1433 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001434 ModuleSpec module_spec (module_file_spec);
1435 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001436
1437 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1438 // black list.
1439 if (num_modules > 0)
1440 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001441 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001442 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001443 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001444 return false;
1445 }
1446 return true;
1447 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001448 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001449 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001450}
1451
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001452bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001453Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001454{
Greg Clayton67cc0632012-08-22 17:17:09 +00001455 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001456 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001457 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001458 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001459 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001460 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001461}
1462
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001463size_t
Greg Claytondb598232011-01-07 01:57:07 +00001464Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1465{
Greg Claytone72dfb32012-02-24 01:59:29 +00001466 SectionSP section_sp (addr.GetSection());
1467 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001468 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001469 // 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 +00001470 if (section_sp->IsEncrypted())
1471 {
Greg Clayton57f06302012-05-25 17:05:55 +00001472 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001473 return 0;
1474 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001475 ModuleSP module_sp (section_sp->GetModule());
1476 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001477 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001478 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1479 if (objfile)
1480 {
1481 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1482 addr.GetOffset(),
1483 dst,
1484 dst_len);
1485 if (bytes_read > 0)
1486 return bytes_read;
1487 else
1488 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1489 }
Greg Claytondb598232011-01-07 01:57:07 +00001490 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001491 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001492 }
1493 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001494 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001495 }
1496 else
Greg Claytondb598232011-01-07 01:57:07 +00001497 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001498
Greg Claytondb598232011-01-07 01:57:07 +00001499 return 0;
1500}
1501
1502size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001503Target::ReadMemory (const Address& addr,
1504 bool prefer_file_cache,
1505 void *dst,
1506 size_t dst_len,
1507 Error &error,
1508 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001509{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001510 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001511
Enrico Granata9128ee22011-09-06 19:20:51 +00001512 // if we end up reading this from process memory, we will fill this
1513 // with the actual load address
1514 if (load_addr_ptr)
1515 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1516
Greg Claytondb598232011-01-07 01:57:07 +00001517 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001518
1519 addr_t load_addr = LLDB_INVALID_ADDRESS;
1520 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001521 Address resolved_addr;
1522 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001523 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001524 SectionLoadList &section_load_list = GetSectionLoadList();
1525 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001526 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001527 // No sections are loaded, so we must assume we are not running
1528 // yet and anything we are given is a file address.
1529 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1530 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001531 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001532 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001533 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001534 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001535 // we have manually loaded some sections with "target modules load ..."
1536 // or because we have have a live process that has sections loaded
1537 // through the dynamic loader
1538 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001539 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001540 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001541 }
Greg Clayton357132e2011-03-26 19:14:58 +00001542 if (!resolved_addr.IsValid())
1543 resolved_addr = addr;
Greg Claytonc749eb82011-07-11 05:12:02 +00001544
Greg Claytondb598232011-01-07 01:57:07 +00001545 if (prefer_file_cache)
1546 {
1547 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1548 if (bytes_read > 0)
1549 return bytes_read;
1550 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001551
Johnny Chen86364b42011-09-20 23:28:55 +00001552 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001553 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001554 if (load_addr == LLDB_INVALID_ADDRESS)
1555 load_addr = resolved_addr.GetLoadAddress (this);
1556
Greg Claytondda4f7b2010-06-30 23:03:03 +00001557 if (load_addr == LLDB_INVALID_ADDRESS)
1558 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001559 ModuleSP addr_module_sp (resolved_addr.GetModule());
1560 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001561 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001562 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001563 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001564 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001565 else
Daniel Malead01b2952012-11-29 21:49:15 +00001566 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001567 }
1568 else
1569 {
Greg Claytondb598232011-01-07 01:57:07 +00001570 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571 if (bytes_read != dst_len)
1572 {
1573 if (error.Success())
1574 {
1575 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001576 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577 else
Daniel Malead01b2952012-11-29 21:49:15 +00001578 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 +00001579 }
1580 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001581 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001582 {
1583 if (load_addr_ptr)
1584 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001585 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001586 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001587 // If the address is not section offset we have an address that
1588 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001589 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001590 // more we can do. If it is section offset, we might be able to
1591 // read cached memory from the object file.
1592 if (!resolved_addr.IsSectionOffset())
1593 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001594 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001595 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001596
Greg Claytonc749eb82011-07-11 05:12:02 +00001597 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001598 {
Greg Claytondb598232011-01-07 01:57:07 +00001599 // If we didn't already try and read from the object file cache, then
1600 // try it after failing to read from the process.
1601 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001602 }
1603 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001604}
1605
Greg Claytond16e1e52011-07-12 17:06:17 +00001606size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001607Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1608{
1609 char buf[256];
1610 out_str.clear();
1611 addr_t curr_addr = addr.GetLoadAddress(this);
1612 Address address(addr);
1613 while (1)
1614 {
1615 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1616 if (length == 0)
1617 break;
1618 out_str.append(buf, length);
1619 // If we got "length - 1" bytes, we didn't get the whole C string, we
1620 // need to read some more characters
1621 if (length == sizeof(buf) - 1)
1622 curr_addr += length;
1623 else
1624 break;
1625 address = Address(curr_addr);
1626 }
1627 return out_str.size();
1628}
1629
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001630size_t
1631Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1632{
1633 size_t total_cstr_len = 0;
1634 if (dst && dst_max_len)
1635 {
1636 result_error.Clear();
1637 // NULL out everything just to be safe
1638 memset (dst, 0, dst_max_len);
1639 Error error;
1640 addr_t curr_addr = addr.GetLoadAddress(this);
1641 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001642
1643 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1644 // think this really needs to be tied to the memory cache subsystem's
1645 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001646 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001647
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001648 size_t bytes_left = dst_max_len - 1;
1649 char *curr_dst = dst;
1650
1651 while (bytes_left > 0)
1652 {
1653 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1654 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1655 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1656
1657 if (bytes_read == 0)
1658 {
1659 result_error = error;
1660 dst[total_cstr_len] = '\0';
1661 break;
1662 }
1663 const size_t len = strlen(curr_dst);
1664
1665 total_cstr_len += len;
1666
1667 if (len < bytes_to_read)
1668 break;
1669
1670 curr_dst += bytes_read;
1671 curr_addr += bytes_read;
1672 bytes_left -= bytes_read;
1673 address = Address(curr_addr);
1674 }
1675 }
1676 else
1677 {
1678 if (dst == NULL)
1679 result_error.SetErrorString("invalid arguments");
1680 else
1681 result_error.Clear();
1682 }
1683 return total_cstr_len;
1684}
1685
1686size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001687Target::ReadScalarIntegerFromMemory (const Address& addr,
1688 bool prefer_file_cache,
1689 uint32_t byte_size,
1690 bool is_signed,
1691 Scalar &scalar,
1692 Error &error)
1693{
1694 uint64_t uval;
1695
1696 if (byte_size <= sizeof(uval))
1697 {
1698 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1699 if (bytes_read == byte_size)
1700 {
1701 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001702 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001703 if (byte_size <= 4)
1704 scalar = data.GetMaxU32 (&offset, byte_size);
1705 else
1706 scalar = data.GetMaxU64 (&offset, byte_size);
1707
1708 if (is_signed)
1709 scalar.SignExtend(byte_size * 8);
1710 return bytes_read;
1711 }
1712 }
1713 else
1714 {
1715 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1716 }
1717 return 0;
1718}
1719
1720uint64_t
1721Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1722 bool prefer_file_cache,
1723 size_t integer_byte_size,
1724 uint64_t fail_value,
1725 Error &error)
1726{
1727 Scalar scalar;
1728 if (ReadScalarIntegerFromMemory (addr,
1729 prefer_file_cache,
1730 integer_byte_size,
1731 false,
1732 scalar,
1733 error))
1734 return scalar.ULongLong(fail_value);
1735 return fail_value;
1736}
1737
1738bool
1739Target::ReadPointerFromMemory (const Address& addr,
1740 bool prefer_file_cache,
1741 Error &error,
1742 Address &pointer_addr)
1743{
1744 Scalar scalar;
1745 if (ReadScalarIntegerFromMemory (addr,
1746 prefer_file_cache,
1747 m_arch.GetAddressByteSize(),
1748 false,
1749 scalar,
1750 error))
1751 {
1752 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1753 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1754 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001755 SectionLoadList &section_load_list = GetSectionLoadList();
1756 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001757 {
1758 // No sections are loaded, so we must assume we are not running
1759 // yet and anything we are given is a file address.
1760 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1761 }
1762 else
1763 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001764 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001765 // we have manually loaded some sections with "target modules load ..."
1766 // or because we have have a live process that has sections loaded
1767 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001768 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001769 }
1770 // We weren't able to resolve the pointer value, so just return
1771 // an address with no section
1772 if (!pointer_addr.IsValid())
1773 pointer_addr.SetOffset (pointer_vm_addr);
1774 return true;
1775
1776 }
1777 }
1778 return false;
1779}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001780
1781ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001782Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001783{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001784 ModuleSP module_sp;
1785
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786 Error error;
1787
Jim Ingham4a94c912012-05-17 18:38:42 +00001788 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1789 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001790
Jim Ingham4a94c912012-05-17 18:38:42 +00001791 if (module_spec.GetUUID().IsValid())
1792 module_sp = m_images.FindFirstModule(module_spec);
1793
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001794 if (!module_sp)
1795 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001796 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1797 bool did_create_module = false;
1798
1799 // If there are image search path entries, try to use them first to acquire a suitable image.
1800 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001801 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001802 ModuleSpec transformed_spec (module_spec);
1803 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1804 {
1805 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1806 error = ModuleList::GetSharedModule (transformed_spec,
1807 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001808 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001809 &old_module_sp,
1810 &did_create_module);
1811 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001812 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001813
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001814 if (!module_sp)
1815 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001816 // If we have a UUID, we can check our global shared module list in case
1817 // we already have it. If we don't have a valid UUID, then we can't since
1818 // the path in "module_spec" will be a platform path, and we will need to
1819 // let the platform find that file. For example, we could be asking for
1820 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1821 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1822 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1823 // cache.
1824 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001825 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001826 // We have a UUID, it is OK to check the global module list...
1827 error = ModuleList::GetSharedModule (module_spec,
1828 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001829 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001830 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001831 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001832 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001833
1834 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001835 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001836 // The platform is responsible for finding and caching an appropriate
1837 // module in the shared module cache.
1838 if (m_platform_sp)
1839 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001840 error = m_platform_sp->GetSharedModule (module_spec,
1841 m_process_sp.get(),
1842 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001843 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001844 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001845 &did_create_module);
1846 }
1847 else
1848 {
1849 error.SetErrorString("no platform is currently set");
1850 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001851 }
1852 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853
Jim Ingham4a94c912012-05-17 18:38:42 +00001854 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1855 // module in the list already, and if there was, let's remove it.
1856 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001857 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001858 ObjectFile *objfile = module_sp->GetObjectFile();
1859 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001860 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001861 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001862 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001863 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1864 case ObjectFile::eTypeExecutable: /// A normal executable
1865 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1866 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1867 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1868 break;
1869 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1870 if (error_ptr)
1871 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1872 return ModuleSP();
1873 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1874 if (error_ptr)
1875 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1876 return ModuleSP();
1877 default:
1878 if (error_ptr)
1879 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1880 return ModuleSP();
1881 }
1882 // GetSharedModule is not guaranteed to find the old shared module, for instance
1883 // in the common case where you pass in the UUID, it is only going to find the one
1884 // module matching the UUID. In fact, it has no good way to know what the "old module"
1885 // relevant to this target is, since there might be many copies of a module with this file spec
1886 // in various running debug sessions, but only one of them will belong to this target.
1887 // So let's remove the UUID from the module list, and look in the target's module list.
1888 // Only do this if there is SOMETHING else in the module spec...
1889 if (!old_module_sp)
1890 {
1891 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001892 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001893 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1894 module_spec_copy.GetUUID().Clear();
1895
1896 ModuleList found_modules;
1897 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1898 if (num_found == 1)
1899 {
1900 old_module_sp = found_modules.GetModuleAtIndex(0);
1901 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001902 }
1903 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001904
1905 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1906 {
1907 m_images.ReplaceModule(old_module_sp, module_sp);
1908 Module *old_module_ptr = old_module_sp.get();
1909 old_module_sp.reset();
1910 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1911 }
1912 else
1913 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001914 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001915 else
1916 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917 }
1918 }
1919 if (error_ptr)
1920 *error_ptr = error;
1921 return module_sp;
1922}
1923
Greg Claytond9e416c2012-02-18 05:35:26 +00001924TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001925Target::CalculateTarget ()
1926{
Greg Claytond9e416c2012-02-18 05:35:26 +00001927 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001928}
1929
Greg Claytond9e416c2012-02-18 05:35:26 +00001930ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931Target::CalculateProcess ()
1932{
Greg Claytond9e416c2012-02-18 05:35:26 +00001933 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001934}
1935
Greg Claytond9e416c2012-02-18 05:35:26 +00001936ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937Target::CalculateThread ()
1938{
Greg Claytond9e416c2012-02-18 05:35:26 +00001939 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001940}
1941
Jason Molendab57e4a12013-11-04 09:33:30 +00001942StackFrameSP
1943Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001944{
Jason Molendab57e4a12013-11-04 09:33:30 +00001945 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946}
1947
1948void
Greg Clayton0603aa92010-10-04 01:05:56 +00001949Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001950{
Greg Claytonc14ee322011-09-22 04:58:26 +00001951 exe_ctx.Clear();
1952 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953}
1954
1955PathMappingList &
1956Target::GetImageSearchPathList ()
1957{
1958 return m_image_search_paths;
1959}
1960
1961void
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001962Target::ImageSearchPathsChanged(const PathMappingList &path_list,
1963 void *baton)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001964{
1965 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001966 ModuleSP exe_module_sp (target->GetExecutableModule());
1967 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001968 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969}
1970
Jim Ingham151c0322015-09-15 21:13:50 +00001971TypeSystem *
Sean Callanana994b0b2015-10-02 18:40:30 +00001972Target::GetScratchTypeSystemForLanguage (Error *error, lldb::LanguageType language, bool create_on_demand)
Jim Ingham151c0322015-09-15 21:13:50 +00001973{
Greg Clayton5beec212015-10-08 21:04:34 +00001974 if (!m_valid)
1975 return nullptr;
1976
Sean Callanana994b0b2015-10-02 18:40:30 +00001977 if (error)
1978 {
1979 error->Clear();
1980 }
1981
Sean Callananb92bd752015-10-01 16:28:02 +00001982 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Sean Callanana994b0b2015-10-02 18:40:30 +00001983 || language == eLanguageTypeUnknown)
1984 {
Sean Callananc7b054a2015-10-09 18:01:10 +00001985 std::set<lldb::LanguageType> languages_for_types;
1986 std::set<lldb::LanguageType> languages_for_expressions;
1987
1988 Language::GetLanguagesSupportingTypeSystems(languages_for_types, languages_for_expressions);
1989
1990 if (languages_for_expressions.count(eLanguageTypeC))
1991 {
1992 language = eLanguageTypeC; // LLDB's default. Override by setting the target language.
1993 }
1994 else
1995 {
1996 if (languages_for_expressions.empty())
1997 {
1998 return nullptr;
1999 }
2000 else
2001 {
2002 language = *languages_for_expressions.begin();
2003 }
2004 }
Sean Callananb92bd752015-10-01 16:28:02 +00002005 }
Sean Callananb92bd752015-10-01 16:28:02 +00002006
Greg Clayton5beec212015-10-08 21:04:34 +00002007 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this, create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00002008}
2009
2010PersistentExpressionState *
2011Target::GetPersistentExpressionStateForLanguage (lldb::LanguageType language)
2012{
Sean Callanana994b0b2015-10-02 18:40:30 +00002013 TypeSystem *type_system = GetScratchTypeSystemForLanguage(nullptr, language, true);
Sean Callananb92bd752015-10-01 16:28:02 +00002014
2015 if (type_system)
2016 {
2017 return type_system->GetPersistentExpressionState();
2018 }
Jim Ingham151c0322015-09-15 21:13:50 +00002019 else
Sean Callananb92bd752015-10-01 16:28:02 +00002020 {
2021 return nullptr;
2022 }
Jim Ingham151c0322015-09-15 21:13:50 +00002023}
2024
2025UserExpression *
2026Target::GetUserExpressionForLanguage(const char *expr,
Sean Callanana994b0b2015-10-02 18:40:30 +00002027 const char *expr_prefix,
2028 lldb::LanguageType language,
2029 Expression::ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +00002030 const EvaluateExpressionOptions &options,
Sean Callanana994b0b2015-10-02 18:40:30 +00002031 Error &error)
Jim Ingham151c0322015-09-15 21:13:50 +00002032{
Sean Callanana994b0b2015-10-02 18:40:30 +00002033 Error type_system_error;
2034
2035 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002036 UserExpression *user_expr = nullptr;
2037
2038 if (!type_system)
2039 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002040 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 +00002041 return nullptr;
2042 }
2043
Jim Ingham19a63fc2015-11-03 02:11:24 +00002044 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00002045 if (!user_expr)
2046 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2047
2048 return user_expr;
2049}
2050
2051FunctionCaller *
2052Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
2053 const CompilerType &return_type,
2054 const Address& function_address,
2055 const ValueList &arg_value_list,
2056 const char *name,
2057 Error &error)
2058{
Sean Callanana994b0b2015-10-02 18:40:30 +00002059 Error type_system_error;
2060 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002061 FunctionCaller *persistent_fn = nullptr;
2062
2063 if (!type_system)
2064 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002065 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 +00002066 return persistent_fn;
2067 }
2068
2069 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
2070 if (!persistent_fn)
2071 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2072
2073 return persistent_fn;
2074}
2075
2076UtilityFunction *
2077Target::GetUtilityFunctionForLanguage (const char *text,
2078 lldb::LanguageType language,
2079 const char *name,
2080 Error &error)
2081{
Sean Callanana994b0b2015-10-02 18:40:30 +00002082 Error type_system_error;
2083 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002084 UtilityFunction *utility_fn = nullptr;
2085
2086 if (!type_system)
2087 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002088 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 +00002089 return utility_fn;
2090 }
2091
2092 utility_fn = type_system->GetUtilityFunction (text, name);
2093 if (!utility_fn)
2094 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2095
2096 return utility_fn;
2097}
2098
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002099ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00002100Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002101{
Greg Clayton5beec212015-10-08 21:04:34 +00002102 if (m_valid)
Sean Callanan4bf80d52011-11-15 22:27:19 +00002103 {
Greg Clayton5beec212015-10-08 21:04:34 +00002104 if (TypeSystem* type_system = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC, create_on_demand))
2105 return llvm::dyn_cast<ClangASTContext>(type_system);
Sean Callanan4bf80d52011-11-15 22:27:19 +00002106 }
Greg Clayton5beec212015-10-08 21:04:34 +00002107 return nullptr;
Sean Callananb92bd752015-10-01 16:28:02 +00002108}
2109
Sean Callanana3444ff2015-11-10 22:54:42 +00002110ClangASTImporterSP
Sean Callanan686b2312011-11-16 18:20:47 +00002111Target::GetClangASTImporter()
2112{
Greg Clayton5beec212015-10-08 21:04:34 +00002113 if (m_valid)
Sean Callanan686b2312011-11-16 18:20:47 +00002114 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002115 if (!m_ast_importer_sp)
Greg Clayton5beec212015-10-08 21:04:34 +00002116 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002117 m_ast_importer_sp.reset(new ClangASTImporter());
Greg Clayton5beec212015-10-08 21:04:34 +00002118 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002119 return m_ast_importer_sp;
Sean Callanan686b2312011-11-16 18:20:47 +00002120 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002121 return ClangASTImporterSP();
Sean Callanan686b2312011-11-16 18:20:47 +00002122}
2123
Greg Clayton99d0faf2010-11-18 23:32:35 +00002124void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002125Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002126{
Greg Clayton6920b522012-08-22 18:39:03 +00002127 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002128}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002129
Greg Clayton99d0faf2010-11-18 23:32:35 +00002130void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002131Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002132{
Greg Clayton6920b522012-08-22 18:39:03 +00002133 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002134}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002135
Greg Claytonc859e2d2012-02-13 23:10:39 +00002136FileSpecList
2137Target::GetDefaultExecutableSearchPaths ()
2138{
Greg Clayton67cc0632012-08-22 17:17:09 +00002139 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2140 if (properties_sp)
2141 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002142 return FileSpecList();
2143}
2144
Michael Sartaina7499c92013-07-01 19:45:50 +00002145FileSpecList
2146Target::GetDefaultDebugFileSearchPaths ()
2147{
2148 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2149 if (properties_sp)
2150 return properties_sp->GetDebugFileSearchPaths();
2151 return FileSpecList();
2152}
2153
Sean Callanan85054342015-04-03 15:39:47 +00002154FileSpecList
2155Target::GetDefaultClangModuleSearchPaths ()
2156{
2157 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2158 if (properties_sp)
2159 return properties_sp->GetClangModuleSearchPaths();
2160 return FileSpecList();
2161}
2162
Caroline Ticedaccaa92010-09-20 20:44:43 +00002163ArchSpec
2164Target::GetDefaultArchitecture ()
2165{
Greg Clayton67cc0632012-08-22 17:17:09 +00002166 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2167 if (properties_sp)
2168 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002169 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002170}
2171
2172void
Greg Clayton67cc0632012-08-22 17:17:09 +00002173Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002174{
Greg Clayton67cc0632012-08-22 17:17:09 +00002175 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2176 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002177 {
2178 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 +00002179 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002180 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002181}
2182
Greg Clayton0603aa92010-10-04 01:05:56 +00002183Target *
2184Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2185{
2186 // The target can either exist in the "process" of ExecutionContext, or in
2187 // the "target_sp" member of SymbolContext. This accessor helper function
2188 // will get the target from one of these locations.
2189
2190 Target *target = NULL;
2191 if (sc_ptr != NULL)
2192 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00002193 if (target == NULL && exe_ctx_ptr)
2194 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002195 return target;
2196}
2197
Jim Ingham1624a2d2014-05-05 02:26:40 +00002198ExpressionResults
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002199Target::EvaluateExpression(const char *expr_cstr,
Jim Inghama309efe2015-10-28 22:23:17 +00002200 ExecutionContextScope *exe_scope,
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002201 lldb::ValueObjectSP &result_valobj_sp,
2202 const EvaluateExpressionOptions& options)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002203{
Enrico Granata97fca502012-09-18 17:43:16 +00002204 result_valobj_sp.reset();
2205
Jim Ingham8646d3c2014-05-05 02:47:44 +00002206 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002207
Greg Claytond1767f02011-12-08 02:13:16 +00002208 if (expr_cstr == NULL || expr_cstr[0] == '\0')
2209 return execution_results;
2210
Jim Ingham6026ca32011-05-12 02:06:14 +00002211 // We shouldn't run stop hooks in expressions.
2212 // Be sure to reset this if you return anywhere within this function.
2213 bool old_suppress_value = m_suppress_stop_hooks;
2214 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002215
2216 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002217
Jim Inghama309efe2015-10-28 22:23:17 +00002218 if (exe_scope)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002219 {
Jim Inghama309efe2015-10-28 22:23:17 +00002220 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002221 }
2222 else if (m_process_sp)
2223 {
2224 m_process_sp->CalculateExecutionContext(exe_ctx);
2225 }
2226 else
2227 {
2228 CalculateExecutionContext(exe_ctx);
2229 }
2230
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002231 // Make sure we aren't just trying to see the value of a persistent
2232 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002233 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002234 // Only check for persistent variables the expression starts with a '$'
2235 if (expr_cstr[0] == '$')
Sean Callanana994b0b2015-10-02 18:40:30 +00002236 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002237
2238 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002239 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002240 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002241 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002242 }
2243 else
2244 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002245 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002246 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002247 execution_results = UserExpression::Evaluate (exe_ctx,
2248 options,
2249 expr_cstr,
2250 prefix,
2251 result_valobj_sp,
2252 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002253 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002254
2255 m_suppress_stop_hooks = old_suppress_value;
2256
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002257 return execution_results;
2258}
2259
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002260lldb::ExpressionVariableSP
2261Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002262{
Greg Clayton5beec212015-10-08 21:04:34 +00002263 lldb::ExpressionVariableSP variable_sp;
2264 m_scratch_type_system_map.ForEach([this, name, &variable_sp](TypeSystem *type_system) -> bool
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002265 {
Greg Clayton5beec212015-10-08 21:04:34 +00002266 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002267 {
Greg Clayton5beec212015-10-08 21:04:34 +00002268 variable_sp = persistent_state->GetVariable(name);
2269
2270 if (variable_sp)
2271 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002272 }
Greg Clayton5beec212015-10-08 21:04:34 +00002273 return true; // Keep iterating the ForEach
2274 });
2275 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002276}
2277
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002278lldb::addr_t
Sean Callananb92bd752015-10-01 16:28:02 +00002279Target::GetPersistentSymbol(const ConstString &name)
2280{
Greg Clayton5beec212015-10-08 21:04:34 +00002281 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Sean Callananb92bd752015-10-01 16:28:02 +00002282
Greg Clayton5beec212015-10-08 21:04:34 +00002283 m_scratch_type_system_map.ForEach([this, name, &address](TypeSystem *type_system) -> bool
Sean Callananb92bd752015-10-01 16:28:02 +00002284 {
Greg Clayton5beec212015-10-08 21:04:34 +00002285 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callananb92bd752015-10-01 16:28:02 +00002286 {
Greg Clayton5beec212015-10-08 21:04:34 +00002287 address = persistent_state->LookupSymbol(name);
2288 if (address != LLDB_INVALID_ADDRESS)
2289 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002290 }
Greg Clayton5beec212015-10-08 21:04:34 +00002291 return true; // Keep iterating the ForEach
2292 });
2293 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002294}
2295
2296lldb::addr_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002297Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2298{
2299 addr_t code_addr = load_addr;
2300 switch (m_arch.GetMachine())
2301 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002302 case llvm::Triple::mips:
2303 case llvm::Triple::mipsel:
2304 case llvm::Triple::mips64:
2305 case llvm::Triple::mips64el:
2306 switch (addr_class)
2307 {
2308 case eAddressClassData:
2309 case eAddressClassDebug:
2310 return LLDB_INVALID_ADDRESS;
2311
2312 case eAddressClassUnknown:
2313 case eAddressClassInvalid:
2314 case eAddressClassCode:
2315 case eAddressClassCodeAlternateISA:
2316 case eAddressClassRuntime:
2317 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2318 code_addr |= 1ull;
2319 break;
2320 }
2321 break;
2322
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002323 case llvm::Triple::arm:
2324 case llvm::Triple::thumb:
2325 switch (addr_class)
2326 {
2327 case eAddressClassData:
2328 case eAddressClassDebug:
2329 return LLDB_INVALID_ADDRESS;
2330
2331 case eAddressClassUnknown:
2332 case eAddressClassInvalid:
2333 case eAddressClassCode:
2334 case eAddressClassCodeAlternateISA:
2335 case eAddressClassRuntime:
2336 // Check if bit zero it no set?
2337 if ((code_addr & 1ull) == 0)
2338 {
2339 // Bit zero isn't set, check if the address is a multiple of 2?
2340 if (code_addr & 2ull)
2341 {
2342 // The address is a multiple of 2 so it must be thumb, set bit zero
2343 code_addr |= 1ull;
2344 }
2345 else if (addr_class == eAddressClassCodeAlternateISA)
2346 {
2347 // We checked the address and the address claims to be the alternate ISA
2348 // which means thumb, so set bit zero.
2349 code_addr |= 1ull;
2350 }
2351 }
2352 break;
2353 }
2354 break;
2355
2356 default:
2357 break;
2358 }
2359 return code_addr;
2360}
2361
2362lldb::addr_t
2363Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2364{
2365 addr_t opcode_addr = load_addr;
2366 switch (m_arch.GetMachine())
2367 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002368 case llvm::Triple::mips:
2369 case llvm::Triple::mipsel:
2370 case llvm::Triple::mips64:
2371 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002372 case llvm::Triple::arm:
2373 case llvm::Triple::thumb:
2374 switch (addr_class)
2375 {
2376 case eAddressClassData:
2377 case eAddressClassDebug:
2378 return LLDB_INVALID_ADDRESS;
2379
2380 case eAddressClassInvalid:
2381 case eAddressClassUnknown:
2382 case eAddressClassCode:
2383 case eAddressClassCodeAlternateISA:
2384 case eAddressClassRuntime:
2385 opcode_addr &= ~(1ull);
2386 break;
2387 }
2388 break;
2389
2390 default:
2391 break;
2392 }
2393 return opcode_addr;
2394}
2395
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002396lldb::addr_t
2397Target::GetBreakableLoadAddress (lldb::addr_t addr)
2398{
2399 addr_t breakable_addr = addr;
2400 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2401
2402 switch (m_arch.GetMachine())
2403 {
2404 default:
2405 break;
2406 case llvm::Triple::mips:
2407 case llvm::Triple::mipsel:
2408 case llvm::Triple::mips64:
2409 case llvm::Triple::mips64el:
2410 {
2411 addr_t function_start = 0;
2412 addr_t current_offset = 0;
2413 uint32_t loop_count = 0;
2414 Address resolved_addr;
2415 uint32_t arch_flags = m_arch.GetFlags ();
2416 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2417 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2418 SectionLoadList &section_load_list = GetSectionLoadList();
2419
2420 if (section_load_list.IsEmpty())
2421 // No sections are loaded, so we must assume we are not running yet
2422 // and need to operate only on file address.
2423 m_images.ResolveFileAddress (addr, resolved_addr);
2424 else
2425 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2426
2427 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2428 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2429 if (temp_addr_module_sp)
2430 {
2431 SymbolContext sc;
2432 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002433 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002434 if (sc.function)
2435 {
2436 function_start = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(this);
2437 if (function_start == LLDB_INVALID_ADDRESS)
2438 function_start = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
2439 }
2440 else if (sc.symbol)
2441 {
2442 Address sym_addr = sc.symbol->GetAddress();
2443 function_start = sym_addr.GetFileAddress();
2444 }
2445 current_offset = addr - function_start;
2446 }
2447
2448 // If breakpoint address is start of function then we dont have to do anything.
2449 if (current_offset == 0)
2450 return breakable_addr;
2451 else
2452 loop_count = current_offset / 2;
2453
2454 if (loop_count > 3)
2455 {
2456 // Scan previous 6 bytes
2457 if (IsMips16 | IsMicromips)
2458 loop_count = 3;
2459 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2460 else
2461 loop_count = 2;
2462 }
2463
2464 // Create Disassembler Instance
2465 lldb::DisassemblerSP disasm_sp (Disassembler::FindPlugin(m_arch, NULL, NULL));
2466
2467 ExecutionContext exe_ctx;
2468 CalculateExecutionContext(exe_ctx);
2469 InstructionList instruction_list;
2470 InstructionSP prev_insn;
2471 bool prefer_file_cache = true; // Read from file
2472 uint32_t inst_to_choose = 0;
2473
2474 for (uint32_t i = 1; i <= loop_count; i++)
2475 {
2476 // Adjust the address to read from.
2477 resolved_addr.Slide (-2);
2478 AddressRange range(resolved_addr, i*2);
2479 uint32_t insn_size = 0;
2480
Greg Clayton6071e6f2015-08-26 22:57:51 +00002481 disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002482
2483 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2484 if (num_insns)
2485 {
2486 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2487 insn_size = prev_insn->GetOpcode().GetByteSize();
2488 if (i == 1 && insn_size == 2)
2489 {
2490 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2491 instruction_list.Append(prev_insn);
2492 inst_to_choose = 1;
2493 }
2494 else if (i == 2)
2495 {
2496 // Here we may get one 4-byte instruction or two 2-byte instructions.
2497 if (num_insns == 2)
2498 {
2499 // Looks like there are two 2-byte instructions above our breakpoint target address.
2500 // 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.
2501 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2502 // and whatever i=1 iteration has found out is true.
2503 inst_to_choose = 1;
2504 break;
2505 }
2506 else if (insn_size == 4)
2507 {
2508 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2509 // Lets try scanning upper 2 bytes to verify this.
2510 instruction_list.Append(prev_insn);
2511 inst_to_choose = 2;
2512 }
2513 }
2514 else if (i == 3)
2515 {
2516 if (insn_size == 4)
2517 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2518 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2519 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2520 inst_to_choose = 0;
2521 else
2522 // This is straight-forward
2523 inst_to_choose = 2;
2524 break;
2525 }
2526 }
2527 else
2528 {
2529 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2530 if (i > 1)
2531 {
2532 inst_to_choose = i - 1;
2533 break;
2534 }
2535 }
2536 }
2537
2538 // Check if we are able to find any valid instruction.
2539 if (inst_to_choose)
2540 {
2541 if (inst_to_choose > instruction_list.GetSize())
2542 inst_to_choose--;
2543 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2544
2545 if (prev_insn->HasDelaySlot())
2546 {
2547 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2548 // Adjust the breakable address
2549 breakable_addr = addr - shift_size;
2550 if (log)
2551 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);
2552 }
2553 }
2554 break;
2555 }
2556 }
2557 return breakable_addr;
2558}
2559
Greg Clayton9585fbf2013-03-19 00:20:55 +00002560SourceManager &
2561Target::GetSourceManager ()
2562{
2563 if (m_source_manager_ap.get() == NULL)
2564 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2565 return *m_source_manager_ap;
2566}
2567
Sean Callanan9998acd2014-12-05 01:21:59 +00002568ClangModulesDeclVendor *
2569Target::GetClangModulesDeclVendor ()
2570{
2571 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2572
2573 {
2574 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2575
2576 if (!m_clang_modules_decl_vendor_ap)
2577 {
2578 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2579 }
2580 }
2581
2582 return m_clang_modules_decl_vendor_ap.get();
2583}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002584
Greg Clayton44d93782014-01-27 23:43:24 +00002585Target::StopHookSP
2586Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002587{
2588 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002589 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2590 m_stop_hooks[new_uid] = stop_hook_sp;
2591 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002592}
2593
2594bool
2595Target::RemoveStopHookByID (lldb::user_id_t user_id)
2596{
2597 size_t num_removed;
2598 num_removed = m_stop_hooks.erase (user_id);
2599 if (num_removed == 0)
2600 return false;
2601 else
2602 return true;
2603}
2604
2605void
2606Target::RemoveAllStopHooks ()
2607{
2608 m_stop_hooks.clear();
2609}
2610
2611Target::StopHookSP
2612Target::GetStopHookByID (lldb::user_id_t user_id)
2613{
2614 StopHookSP found_hook;
2615
2616 StopHookCollection::iterator specified_hook_iter;
2617 specified_hook_iter = m_stop_hooks.find (user_id);
2618 if (specified_hook_iter != m_stop_hooks.end())
2619 found_hook = (*specified_hook_iter).second;
2620 return found_hook;
2621}
2622
2623bool
2624Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2625{
2626 StopHookCollection::iterator specified_hook_iter;
2627 specified_hook_iter = m_stop_hooks.find (user_id);
2628 if (specified_hook_iter == m_stop_hooks.end())
2629 return false;
2630
2631 (*specified_hook_iter).second->SetIsActive (active_state);
2632 return true;
2633}
2634
2635void
2636Target::SetAllStopHooksActiveState (bool active_state)
2637{
2638 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2639 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2640 {
2641 (*pos).second->SetIsActive (active_state);
2642 }
2643}
2644
2645void
2646Target::RunStopHooks ()
2647{
Jim Ingham6026ca32011-05-12 02:06:14 +00002648 if (m_suppress_stop_hooks)
2649 return;
2650
Jim Ingham9575d842011-03-11 03:53:59 +00002651 if (!m_process_sp)
2652 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002653
2654 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2655 // since in that case we do not want to run the stop-hooks
2656 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2657 return;
2658
Jim Ingham9575d842011-03-11 03:53:59 +00002659 if (m_stop_hooks.empty())
2660 return;
2661
2662 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2663
2664 // If there aren't any active stop hooks, don't bother either:
2665 bool any_active_hooks = false;
2666 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2667 {
2668 if ((*pos).second->IsActive())
2669 {
2670 any_active_hooks = true;
2671 break;
2672 }
2673 }
2674 if (!any_active_hooks)
2675 return;
2676
2677 CommandReturnObject result;
2678
2679 std::vector<ExecutionContext> exc_ctx_with_reasons;
2680 std::vector<SymbolContext> sym_ctx_with_reasons;
2681
2682 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2683 size_t num_threads = cur_threadlist.GetSize();
2684 for (size_t i = 0; i < num_threads; i++)
2685 {
2686 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2687 if (cur_thread_sp->ThreadStoppedForAReason())
2688 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002689 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002690 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2691 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2692 }
2693 }
2694
2695 // If no threads stopped for a reason, don't run the stop-hooks.
2696 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2697 if (num_exe_ctx == 0)
2698 return;
2699
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002700 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2701 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002702
2703 bool keep_going = true;
2704 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002705 bool print_hook_header;
2706 bool print_thread_header;
2707
2708 if (num_exe_ctx == 1)
2709 print_thread_header = false;
2710 else
2711 print_thread_header = true;
2712
2713 if (m_stop_hooks.size() == 1)
2714 print_hook_header = false;
2715 else
2716 print_hook_header = true;
2717
Jim Ingham9575d842011-03-11 03:53:59 +00002718 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2719 {
2720 // result.Clear();
2721 StopHookSP cur_hook_sp = (*pos).second;
2722 if (!cur_hook_sp->IsActive())
2723 continue;
2724
2725 bool any_thread_matched = false;
2726 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2727 {
2728 if ((cur_hook_sp->GetSpecifier () == NULL
2729 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2730 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002731 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002732 {
2733 if (!hooks_ran)
2734 {
Jim Ingham9575d842011-03-11 03:53:59 +00002735 hooks_ran = true;
2736 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002737 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002738 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002739 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2740 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2741 NULL);
2742 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002743 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002744 else
Daniel Malead01b2952012-11-29 21:49:15 +00002745 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002746 any_thread_matched = true;
2747 }
2748
Jim Ingham381e25b2011-03-22 01:47:27 +00002749 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002750 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002751
2752 CommandInterpreterRunOptions options;
2753 options.SetStopOnContinue (true);
2754 options.SetStopOnError (true);
2755 options.SetEchoCommands (false);
2756 options.SetPrintResults (true);
2757 options.SetAddToHistory (false);
2758
2759 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2760 &exc_ctx_with_reasons[i],
2761 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002762 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002763
2764 // If the command started the target going again, we should bag out of
2765 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002766 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2767 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002768 {
Daniel Malead01b2952012-11-29 21:49:15 +00002769 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002770 keep_going = false;
2771 }
2772 }
2773 }
2774 }
Jason Molenda879cf772011-09-23 00:42:55 +00002775
Caroline Tice969ed3d2011-05-02 20:41:46 +00002776 result.GetImmediateOutputStream()->Flush();
2777 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002778}
2779
Greg Claytonfbb76342013-11-20 21:07:01 +00002780const TargetPropertiesSP &
2781Target::GetGlobalProperties()
2782{
2783 static TargetPropertiesSP g_settings_sp;
2784 if (!g_settings_sp)
2785 {
2786 g_settings_sp.reset (new TargetProperties (NULL));
2787 }
2788 return g_settings_sp;
2789}
2790
2791Error
2792Target::Install (ProcessLaunchInfo *launch_info)
2793{
2794 Error error;
2795 PlatformSP platform_sp (GetPlatform());
2796 if (platform_sp)
2797 {
2798 if (platform_sp->IsRemote())
2799 {
2800 if (platform_sp->IsConnected())
2801 {
2802 // Install all files that have an install path, and always install the
2803 // main executable when connected to a remote platform
2804 const ModuleList& modules = GetImages();
2805 const size_t num_images = modules.GetSize();
2806 for (size_t idx = 0; idx < num_images; ++idx)
2807 {
2808 const bool is_main_executable = idx == 0;
2809 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2810 if (module_sp)
2811 {
2812 FileSpec local_file (module_sp->GetFileSpec());
2813 if (local_file)
2814 {
2815 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2816 if (!remote_file)
2817 {
2818 if (is_main_executable) // TODO: add setting for always installing main executable???
2819 {
2820 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002821 remote_file = platform_sp->GetRemoteWorkingDirectory();
2822 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002823 }
2824 }
2825 if (remote_file)
2826 {
2827 error = platform_sp->Install(local_file, remote_file);
2828 if (error.Success())
2829 {
2830 module_sp->SetPlatformFileSpec(remote_file);
2831 if (is_main_executable)
2832 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002833 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002834 if (launch_info)
2835 launch_info->SetExecutableFile(remote_file, false);
2836 }
2837 }
2838 else
2839 break;
2840 }
2841 }
2842 }
2843 }
2844 }
2845 }
2846 }
2847 return error;
2848}
Greg Clayton7b242382011-07-08 00:48:09 +00002849
Greg Claytond5944cd2013-12-06 01:12:00 +00002850bool
2851Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2852{
2853 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2854}
2855
2856bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002857Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2858{
2859 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2860}
2861
2862bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002863Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2864{
2865 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2866 if (old_section_load_addr != new_section_load_addr)
2867 {
2868 uint32_t stop_id = 0;
2869 ProcessSP process_sp(GetProcessSP());
2870 if (process_sp)
2871 stop_id = process_sp->GetStopID();
2872 else
2873 stop_id = m_section_load_history.GetLastStopID();
2874 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2875 return true; // Return true if the section load address was changed...
2876 }
2877 return false; // Return false to indicate nothing changed
Greg Claytond5944cd2013-12-06 01:12:00 +00002878}
2879
Greg Clayton8012cad2014-11-17 19:39:20 +00002880size_t
2881Target::UnloadModuleSections (const ModuleList &module_list)
2882{
2883 size_t section_unload_count = 0;
2884 size_t num_modules = module_list.GetSize();
2885 for (size_t i=0; i<num_modules; ++i)
2886 {
2887 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2888 }
2889 return section_unload_count;
2890}
2891
2892size_t
2893Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2894{
2895 uint32_t stop_id = 0;
2896 ProcessSP process_sp(GetProcessSP());
2897 if (process_sp)
2898 stop_id = process_sp->GetStopID();
2899 else
2900 stop_id = m_section_load_history.GetLastStopID();
2901 SectionList *sections = module_sp->GetSectionList();
2902 size_t section_unload_count = 0;
2903 if (sections)
2904 {
2905 const uint32_t num_sections = sections->GetNumSections(0);
2906 for (uint32_t i = 0; i < num_sections; ++i)
2907 {
2908 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2909 }
2910 }
2911 return section_unload_count;
2912}
2913
Greg Claytond5944cd2013-12-06 01:12:00 +00002914bool
2915Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2916{
2917 uint32_t stop_id = 0;
2918 ProcessSP process_sp(GetProcessSP());
2919 if (process_sp)
2920 stop_id = process_sp->GetStopID();
2921 else
2922 stop_id = m_section_load_history.GetLastStopID();
2923 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2924}
2925
2926bool
2927Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2928{
2929 uint32_t stop_id = 0;
2930 ProcessSP process_sp(GetProcessSP());
2931 if (process_sp)
2932 stop_id = process_sp->GetStopID();
2933 else
2934 stop_id = m_section_load_history.GetLastStopID();
2935 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2936}
2937
2938void
2939Target::ClearAllLoadedSections ()
2940{
2941 m_section_load_history.Clear();
2942}
2943
Greg Claytonb09c5382013-12-13 17:20:18 +00002944Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002945Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002946{
2947 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002948 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2949
2950 if (log)
2951 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2952
Greg Claytonb09c5382013-12-13 17:20:18 +00002953 StateType state = eStateInvalid;
2954
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002955 // Scope to temporarily get the process state in case someone has manually
2956 // remotely connected already to a process and we can skip the platform
2957 // launching.
2958 {
2959 ProcessSP process_sp (GetProcessSP());
2960
2961 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002962 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002963 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002964 if (log)
2965 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2966 }
2967 else
2968 {
2969 if (log)
2970 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2971 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002972 }
2973
Greg Claytonb09c5382013-12-13 17:20:18 +00002974 launch_info.GetFlags().Set (eLaunchFlagDebug);
2975
2976 // Get the value of synchronous execution here. If you wait till after you have started to
2977 // run, then you could have hit a breakpoint, whose command might switch the value, and
2978 // then you'll pick up that incorrect value.
2979 Debugger &debugger = GetDebugger();
2980 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2981
2982 PlatformSP platform_sp (GetPlatform());
2983
2984 // Finalize the file actions, and if none were given, default to opening
2985 // up a pseudo terminal
2986 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002987 if (log)
2988 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2989 __FUNCTION__,
2990 platform_sp ? "true" : "false",
2991 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2992 default_to_use_pty ? "true" : "false");
2993
Greg Claytonb09c5382013-12-13 17:20:18 +00002994 launch_info.FinalizeFileActions (this, default_to_use_pty);
2995
2996 if (state == eStateConnected)
2997 {
2998 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2999 {
3000 error.SetErrorString("can't launch in tty when launching through a remote connection");
3001 return error;
3002 }
3003 }
3004
3005 if (!launch_info.GetArchitecture().IsValid())
3006 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00003007
3008 // 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 +00003009 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
3010 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003011 if (log)
3012 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
3013
Greg Clayton5df78fa2015-05-23 03:54:53 +00003014 // Get a weak pointer to the previous process if we have one
3015 ProcessWP process_wp;
3016 if (m_process_sp)
3017 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00003018 m_process_sp = GetPlatform()->DebugProcess (launch_info,
3019 debugger,
3020 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00003021 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00003022
3023 // Cleanup the old process since someone might still have a strong
3024 // reference to this process and we would like to allow it to cleanup
3025 // as much as it can without the object being destroyed. We try to
3026 // lock the shared pointer and if that works, then someone else still
3027 // has a strong reference to the process.
3028
3029 ProcessSP old_process_sp(process_wp.lock());
3030 if (old_process_sp)
3031 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00003032 }
3033 else
3034 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003035 if (log)
3036 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
3037
Greg Claytonb09c5382013-12-13 17:20:18 +00003038 if (state == eStateConnected)
3039 {
3040 assert(m_process_sp);
3041 }
3042 else
3043 {
Todd Fiala015d8182014-07-22 23:41:36 +00003044 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00003045 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00003046 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00003047 }
Todd Fiala015d8182014-07-22 23:41:36 +00003048
3049 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00003050 if (m_process_sp)
3051 error = m_process_sp->Launch (launch_info);
3052 }
3053
3054 if (!m_process_sp)
3055 {
3056 if (error.Success())
3057 error.SetErrorString("failed to launch or debug process");
3058 return error;
3059 }
3060
3061 if (error.Success())
3062 {
Ilia K064e69f2015-03-23 21:16:25 +00003063 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00003064 {
Greg Clayton44d93782014-01-27 23:43:24 +00003065 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00003066 if (!hijack_listener_sp)
3067 {
3068 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
3069 launch_info.SetHijackListener(hijack_listener_sp);
3070 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
3071 }
Todd Fialaac33cc92014-10-09 01:02:08 +00003072
Ilia K38810f42015-05-20 10:15:47 +00003073 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00003074
3075 if (state == eStateStopped)
3076 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003077 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003078 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003079 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00003080 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003081 error = m_process_sp->PrivateResume();
3082 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00003083 {
Ilia K064e69f2015-03-23 21:16:25 +00003084 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
3085 const bool must_be_alive = false; // eStateExited is ok, so this must be false
3086 if (!StateIsStoppedState(state, must_be_alive))
3087 {
3088 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
3089 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003090 }
3091 }
Ilia K064e69f2015-03-23 21:16:25 +00003092 else
3093 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003094 m_process_sp->RestoreProcessEvents();
3095 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00003096 }
3097 if (!error.Success())
3098 {
Ilia K064e69f2015-03-23 21:16:25 +00003099 Error error2;
3100 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
3101 error = error2;
3102 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003103 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003104 }
Greg Clayton40286e02014-04-30 20:29:09 +00003105 else if (state == eStateExited)
3106 {
Zachary Turner10687b02014-10-20 17:46:43 +00003107 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00003108 const int exit_status = m_process_sp->GetExitStatus();
3109 const char *exit_desc = m_process_sp->GetExitDescription();
3110#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'."
3111 if (exit_desc && exit_desc[0])
3112 {
3113 if (with_shell)
3114 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
3115 else
3116 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
3117 }
3118 else
3119 {
3120 if (with_shell)
3121 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
3122 else
3123 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
3124 }
3125 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003126 else
3127 {
3128 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
3129 }
3130 }
Greg Clayton44d93782014-01-27 23:43:24 +00003131 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00003132 }
3133 else
3134 {
Greg Clayton44d93782014-01-27 23:43:24 +00003135 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003136 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003137 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003138 }
3139 return error;
3140}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003141
3142Error
3143Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3144{
3145 auto state = eStateInvalid;
3146 auto process_sp = GetProcessSP ();
3147 if (process_sp)
3148 {
3149 state = process_sp->GetState ();
3150 if (process_sp->IsAlive () && state != eStateConnected)
3151 {
3152 if (state == eStateAttaching)
3153 return Error ("process attach is in progress");
3154 return Error ("a process is already being debugged");
3155 }
3156 }
3157
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003158 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3159
3160 // If no process info was specified, then use the target executable
3161 // name as the process to attach to by default
3162 if (!attach_info.ProcessInfoSpecified ())
3163 {
3164 if (old_exec_module_sp)
3165 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3166
3167 if (!attach_info.ProcessInfoSpecified ())
3168 {
3169 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3170 }
3171 }
3172
3173 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
Greg Claytonb3788ea2015-10-05 22:58:37 +00003174 ListenerSP hijack_listener_sp;
3175 const bool async = attach_info.GetAsync();
3176 if (async == false)
3177 {
3178 hijack_listener_sp.reset (new Listener ("lldb.Target.Attach.attach.hijack"));
3179 attach_info.SetHijackListener (hijack_listener_sp);
3180 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003181
3182 Error error;
3183 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3184 {
3185 SetPlatform (platform_sp);
3186 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3187 }
3188 else
3189 {
3190 if (state != eStateConnected)
3191 {
3192 const char *plugin_name = attach_info.GetProcessPluginName ();
3193 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3194 if (process_sp == nullptr)
3195 {
3196 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3197 return error;
3198 }
3199 }
Greg Claytonb3788ea2015-10-05 22:58:37 +00003200 if (hijack_listener_sp)
3201 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003202 error = process_sp->Attach (attach_info);
3203 }
3204
Greg Claytone75e5d82015-10-20 00:14:20 +00003205 if (error.Success () && process_sp)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003206 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003207 if (async)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003208 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003209 process_sp->RestoreProcessEvents ();
3210 }
3211 else
3212 {
3213 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
3214 process_sp->RestoreProcessEvents ();
3215
3216 if (state != eStateStopped)
3217 {
3218 const char *exit_desc = process_sp->GetExitDescription ();
3219 if (exit_desc)
3220 error.SetErrorStringWithFormat ("%s", exit_desc);
3221 else
3222 error.SetErrorString ("process did not stop (no such process or permission problem?)");
3223 process_sp->Destroy (false);
3224 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003225 }
3226 }
3227 return error;
3228}
3229
Jim Ingham9575d842011-03-11 03:53:59 +00003230//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003231// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003232//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003233Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3234 UserID (uid),
3235 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003236 m_commands (),
3237 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003238 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003239 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003240{
3241}
3242
3243Target::StopHook::StopHook (const StopHook &rhs) :
3244 UserID (rhs.GetID()),
3245 m_target_sp (rhs.m_target_sp),
3246 m_commands (rhs.m_commands),
3247 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003248 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003249 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003250{
3251 if (rhs.m_thread_spec_ap.get() != NULL)
3252 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3253}
3254
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003255Target::StopHook::~StopHook() = default;
Jim Ingham9575d842011-03-11 03:53:59 +00003256
3257void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003258Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3259{
3260 m_specifier_sp.reset(specifier);
3261}
3262
3263void
Jim Ingham9575d842011-03-11 03:53:59 +00003264Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3265{
3266 m_thread_spec_ap.reset (specifier);
3267}
Jim Ingham9575d842011-03-11 03:53:59 +00003268
3269void
3270Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3271{
3272 int indent_level = s->GetIndentLevel();
3273
3274 s->SetIndentLevel(indent_level + 2);
3275
Daniel Malead01b2952012-11-29 21:49:15 +00003276 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003277 if (m_active)
3278 s->Indent ("State: enabled\n");
3279 else
3280 s->Indent ("State: disabled\n");
3281
3282 if (m_specifier_sp)
3283 {
3284 s->Indent();
3285 s->PutCString ("Specifier:\n");
3286 s->SetIndentLevel (indent_level + 4);
3287 m_specifier_sp->GetDescription (s, level);
3288 s->SetIndentLevel (indent_level + 2);
3289 }
3290
3291 if (m_thread_spec_ap.get() != NULL)
3292 {
3293 StreamString tmp;
3294 s->Indent("Thread:\n");
3295 m_thread_spec_ap->GetDescription (&tmp, level);
3296 s->SetIndentLevel (indent_level + 4);
3297 s->Indent (tmp.GetData());
3298 s->PutCString ("\n");
3299 s->SetIndentLevel (indent_level + 2);
3300 }
3301
3302 s->Indent ("Commands: \n");
3303 s->SetIndentLevel (indent_level + 4);
3304 uint32_t num_commands = m_commands.GetSize();
3305 for (uint32_t i = 0; i < num_commands; i++)
3306 {
3307 s->Indent(m_commands.GetStringAtIndex(i));
3308 s->PutCString ("\n");
3309 }
3310 s->SetIndentLevel (indent_level);
3311}
3312
Greg Clayton67cc0632012-08-22 17:17:09 +00003313//--------------------------------------------------------------
3314// class TargetProperties
3315//--------------------------------------------------------------
3316
3317OptionEnumValueElement
3318lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003319{
Greg Clayton67cc0632012-08-22 17:17:09 +00003320 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3321 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3322 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
3323 { 0, NULL, NULL }
3324};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003325
Greg Clayton1f746072012-08-29 21:13:06 +00003326static OptionEnumValueElement
3327g_inline_breakpoint_enums[] =
3328{
3329 { 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."},
3330 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3331 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
3332 { 0, NULL, NULL }
3333};
3334
Jim Ingham0f063ba2013-03-02 00:26:47 +00003335typedef enum x86DisassemblyFlavor
3336{
3337 eX86DisFlavorDefault,
3338 eX86DisFlavorIntel,
3339 eX86DisFlavorATT
3340} x86DisassemblyFlavor;
3341
3342static OptionEnumValueElement
3343g_x86_dis_flavor_value_types[] =
3344{
3345 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3346 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3347 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
3348 { 0, NULL, NULL }
3349};
3350
Enrico Granata397ddd52013-05-21 20:13:34 +00003351static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003352g_hex_immediate_style_values[] =
3353{
3354 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3355 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
3356 { 0, NULL, NULL }
3357};
3358
3359static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003360g_load_script_from_sym_file_values[] =
3361{
3362 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3363 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3364 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
3365 { 0, NULL, NULL }
3366};
3367
Greg Claytonfd814c52013-08-13 01:42:25 +00003368static OptionEnumValueElement
3369g_memory_module_load_level_values[] =
3370{
Greg Clayton86eac942013-08-13 21:32:34 +00003371 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003372 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3373 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
3374 { 0, NULL, NULL }
3375};
3376
Greg Clayton67cc0632012-08-22 17:17:09 +00003377static PropertyDefinition
3378g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003379{
Greg Clayton67cc0632012-08-22 17:17:09 +00003380 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
Ilia K055ad9b2015-05-18 13:41:01 +00003381 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Move breakpoints to nearest code." },
Dawn Perchike0c96ac2015-07-30 22:10:08 +00003382 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , NULL, NULL, "The language to use when interpreting expressions entered in commands." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003383 { "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 +00003384 { "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 +00003385 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
3386 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
Jim Ingham0ca778e2015-09-15 18:03:00 +00003387 { "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 +00003388 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3389 "some part (starting at the root) of the path to the file when it was built, "
3390 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3391 "Each element of the array is checked in order and the first one that results in a match wins." },
3392 { "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 +00003393 { "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 +00003394 { "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 +00003395 { "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 +00003396 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
3397 { "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 +00003398 { "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 +00003399 { "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 +00003400 { "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." },
3401 { "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 +00003402 { "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." },
3403 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
3404 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
3405 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
3406 { "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 +00003407 { "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 +00003408 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
3409 { "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 +00003410 { "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 +00003411 "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 +00003412 "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 +00003413 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003414 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3415 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3416 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003417 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003418 // 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.
3419 { "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 +00003420 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
3421 { "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 +00003422 { "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 +00003423 { "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 +00003424 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
3425 "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. "
3426 "This setting helps users control how much information gets loaded when loading modules from memory."
3427 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3428 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3429 "'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 +00003430 { "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 +00003431 { "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 +00003432 { "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 +00003433 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, NULL, NULL, "Disable lock-step debugging, instead control threads independently." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003434 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
3435};
Enrico Granata560558e2015-02-11 02:35:39 +00003436
Greg Clayton67cc0632012-08-22 17:17:09 +00003437enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003438{
Greg Clayton67cc0632012-08-22 17:17:09 +00003439 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003440 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003441 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003442 ePropertyExprPrefix,
3443 ePropertyPreferDynamic,
3444 ePropertyEnableSynthetic,
3445 ePropertySkipPrologue,
3446 ePropertySourceMap,
3447 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003448 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003449 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003450 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003451 ePropertyMaxChildrenCount,
3452 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003453 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003454 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003455 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003456 ePropertyRunArgs,
3457 ePropertyEnvVars,
3458 ePropertyInheritEnv,
3459 ePropertyInputPath,
3460 ePropertyOutputPath,
3461 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003462 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003463 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003464 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003465 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003466 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003467 ePropertyUseHexImmediates,
3468 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003469 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003470 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003471 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003472 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003473 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003474 ePropertyDisplayRuntimeSupportValues,
3475 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003476};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003477
Greg Clayton67cc0632012-08-22 17:17:09 +00003478class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003479{
Greg Clayton67cc0632012-08-22 17:17:09 +00003480public:
3481 TargetOptionValueProperties (const ConstString &name) :
3482 OptionValueProperties (name),
3483 m_target (NULL),
3484 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003485 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003486 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003487
Greg Clayton67cc0632012-08-22 17:17:09 +00003488 // This constructor is used when creating TargetOptionValueProperties when it
3489 // is part of a new lldb_private::Target instance. It will copy all current
3490 // global property values as needed
3491 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3492 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3493 m_target (target),
3494 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003495 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003496 }
3497
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003498 const Property *
3499 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +00003500 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003501 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003502 // try and grab the setting from the current target if there is one. Else we just
3503 // use the one from this instance.
3504 if (idx == ePropertyEnvVars)
3505 GetHostEnvironmentIfNeeded ();
3506
3507 if (exe_ctx)
3508 {
3509 Target *target = exe_ctx->GetTargetPtr();
3510 if (target)
3511 {
3512 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3513 if (this != target_properties)
3514 return target_properties->ProtectedGetPropertyAtIndex (idx);
3515 }
3516 }
3517 return ProtectedGetPropertyAtIndex (idx);
3518 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003519
3520 lldb::TargetSP
3521 GetTargetSP ()
3522 {
3523 return m_target->shared_from_this();
3524 }
3525
Greg Clayton67cc0632012-08-22 17:17:09 +00003526protected:
Greg Clayton67cc0632012-08-22 17:17:09 +00003527 void
3528 GetHostEnvironmentIfNeeded () const
3529 {
3530 if (!m_got_host_env)
3531 {
3532 if (m_target)
3533 {
3534 m_got_host_env = true;
3535 const uint32_t idx = ePropertyInheritEnv;
3536 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3537 {
3538 PlatformSP platform_sp (m_target->GetPlatform());
3539 if (platform_sp)
3540 {
3541 StringList env;
3542 if (platform_sp->GetEnvironment(env))
3543 {
3544 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3545 if (env_dict)
3546 {
3547 const bool can_replace = false;
3548 const size_t envc = env.GetSize();
3549 for (size_t idx=0; idx<envc; idx++)
3550 {
3551 const char *env_entry = env.GetStringAtIndex (idx);
3552 if (env_entry)
3553 {
3554 const char *equal_pos = ::strchr(env_entry, '=');
3555 ConstString key;
3556 // It is ok to have environment variables with no values
3557 const char *value = NULL;
3558 if (equal_pos)
3559 {
3560 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3561 if (equal_pos[1])
3562 value = equal_pos + 1;
3563 }
3564 else
3565 {
3566 key.SetCString(env_entry);
3567 }
3568 // Don't allow existing keys to be replaced with ones we get from the platform environment
3569 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3570 }
3571 }
3572 }
3573 }
3574 }
3575 }
3576 }
3577 }
3578 }
3579 Target *m_target;
3580 mutable bool m_got_host_env;
3581};
3582
Greg Claytonfbb76342013-11-20 21:07:01 +00003583//----------------------------------------------------------------------
3584// TargetProperties
3585//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003586TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003587 Properties (),
3588 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003589{
3590 if (target)
3591 {
3592 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003593
3594 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3595 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3596 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3597 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3598 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3599 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3600 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3601 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3602 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3603 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3604
3605 // Update m_launch_info once it was created
3606 Arg0ValueChangedCallback(this, NULL);
3607 RunArgsValueChangedCallback(this, NULL);
3608 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3609 InputPathValueChangedCallback(this, NULL);
3610 OutputPathValueChangedCallback(this, NULL);
3611 ErrorPathValueChangedCallback(this, NULL);
3612 DetachOnErrorValueChangedCallback(this, NULL);
3613 DisableASLRValueChangedCallback(this, NULL);
3614 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003615 }
3616 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003617 {
3618 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3619 m_collection_sp->Initialize(g_properties);
3620 m_collection_sp->AppendProperty(ConstString("process"),
3621 ConstString("Settings specify to processes."),
3622 true,
3623 Process::GetGlobalProperties()->GetValueProperties());
3624 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003625}
3626
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003627TargetProperties::~TargetProperties() = default;
3628
Greg Clayton67cc0632012-08-22 17:17:09 +00003629ArchSpec
3630TargetProperties::GetDefaultArchitecture () const
3631{
3632 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3633 if (value)
3634 return value->GetCurrentValue();
3635 return ArchSpec();
3636}
3637
3638void
3639TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3640{
3641 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3642 if (value)
3643 return value->SetCurrentValue(arch, true);
3644}
3645
Ilia K055ad9b2015-05-18 13:41:01 +00003646bool
3647TargetProperties::GetMoveToNearestCode() const
3648{
3649 const uint32_t idx = ePropertyMoveToNearestCode;
3650 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3651}
3652
Greg Clayton67cc0632012-08-22 17:17:09 +00003653lldb::DynamicValueType
3654TargetProperties::GetPreferDynamicValue() const
3655{
3656 const uint32_t idx = ePropertyPreferDynamic;
3657 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3658}
3659
3660bool
Greg Clayton15484402015-05-15 18:40:24 +00003661TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3662{
3663 const uint32_t idx = ePropertyPreferDynamic;
3664 return m_collection_sp->SetPropertyAtIndexAsEnumeration(NULL, idx, d);
3665}
3666
Greg Clayton15484402015-05-15 18:40:24 +00003667bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003668TargetProperties::GetDisableASLR () const
3669{
3670 const uint32_t idx = ePropertyDisableASLR;
3671 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3672}
3673
3674void
3675TargetProperties::SetDisableASLR (bool b)
3676{
3677 const uint32_t idx = ePropertyDisableASLR;
3678 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3679}
3680
3681bool
Jim Ingham106d0282014-06-25 02:32:56 +00003682TargetProperties::GetDetachOnError () const
3683{
3684 const uint32_t idx = ePropertyDetachOnError;
3685 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3686}
3687
3688void
3689TargetProperties::SetDetachOnError (bool b)
3690{
3691 const uint32_t idx = ePropertyDetachOnError;
3692 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3693}
3694
3695bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003696TargetProperties::GetDisableSTDIO () const
3697{
3698 const uint32_t idx = ePropertyDisableSTDIO;
3699 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3700}
3701
3702void
3703TargetProperties::SetDisableSTDIO (bool b)
3704{
3705 const uint32_t idx = ePropertyDisableSTDIO;
3706 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3707}
3708
Jim Ingham0f063ba2013-03-02 00:26:47 +00003709const char *
3710TargetProperties::GetDisassemblyFlavor () const
3711{
3712 const uint32_t idx = ePropertyDisassemblyFlavor;
3713 const char *return_value;
3714
3715 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3716 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3717 return return_value;
3718}
3719
Greg Clayton1f746072012-08-29 21:13:06 +00003720InlineStrategy
3721TargetProperties::GetInlineStrategy () const
3722{
3723 const uint32_t idx = ePropertyInlineStrategy;
3724 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3725}
3726
Greg Clayton45392552012-10-17 22:57:12 +00003727const char *
3728TargetProperties::GetArg0 () const
3729{
3730 const uint32_t idx = ePropertyArg0;
3731 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3732}
3733
3734void
3735TargetProperties::SetArg0 (const char *arg)
3736{
3737 const uint32_t idx = ePropertyArg0;
3738 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003739 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003740}
3741
Greg Clayton67cc0632012-08-22 17:17:09 +00003742bool
3743TargetProperties::GetRunArguments (Args &args) const
3744{
3745 const uint32_t idx = ePropertyRunArgs;
3746 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3747}
3748
3749void
3750TargetProperties::SetRunArguments (const Args &args)
3751{
3752 const uint32_t idx = ePropertyRunArgs;
3753 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003754 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003755}
3756
3757size_t
3758TargetProperties::GetEnvironmentAsArgs (Args &env) const
3759{
3760 const uint32_t idx = ePropertyEnvVars;
3761 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3762}
3763
Ilia K8f37ca52015-02-13 14:31:06 +00003764void
3765TargetProperties::SetEnvironmentFromArgs (const Args &env)
3766{
3767 const uint32_t idx = ePropertyEnvVars;
3768 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003769 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003770}
3771
Greg Clayton67cc0632012-08-22 17:17:09 +00003772bool
3773TargetProperties::GetSkipPrologue() const
3774{
3775 const uint32_t idx = ePropertySkipPrologue;
3776 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3777}
3778
3779PathMappingList &
3780TargetProperties::GetSourcePathMap () const
3781{
3782 const uint32_t idx = ePropertySourceMap;
3783 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3784 assert(option_value);
3785 return option_value->GetCurrentValue();
3786}
3787
3788FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003789TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003790{
3791 const uint32_t idx = ePropertyExecutableSearchPaths;
3792 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3793 assert(option_value);
3794 return option_value->GetCurrentValue();
3795}
3796
Michael Sartaina7499c92013-07-01 19:45:50 +00003797FileSpecList &
3798TargetProperties::GetDebugFileSearchPaths ()
3799{
3800 const uint32_t idx = ePropertyDebugFileSearchPaths;
3801 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3802 assert(option_value);
3803 return option_value->GetCurrentValue();
3804}
3805
Sean Callanan85054342015-04-03 15:39:47 +00003806FileSpecList &
3807TargetProperties::GetClangModuleSearchPaths ()
3808{
3809 const uint32_t idx = ePropertyClangModuleSearchPaths;
3810 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3811 assert(option_value);
3812 return option_value->GetCurrentValue();
3813}
3814
Greg Clayton67cc0632012-08-22 17:17:09 +00003815bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003816TargetProperties::GetEnableAutoImportClangModules() const
3817{
3818 const uint32_t idx = ePropertyAutoImportClangModules;
3819 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3820}
3821
3822bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003823TargetProperties::GetEnableSyntheticValue () const
3824{
3825 const uint32_t idx = ePropertyEnableSynthetic;
3826 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3827}
3828
3829uint32_t
3830TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3831{
3832 const uint32_t idx = ePropertyMaxChildrenCount;
3833 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3834}
3835
3836uint32_t
3837TargetProperties::GetMaximumSizeOfStringSummary() const
3838{
3839 const uint32_t idx = ePropertyMaxSummaryLength;
3840 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3841}
3842
Enrico Granatad325bf92013-06-04 22:54:16 +00003843uint32_t
3844TargetProperties::GetMaximumMemReadSize () const
3845{
3846 const uint32_t idx = ePropertyMaxMemReadSize;
3847 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3848}
3849
Greg Clayton67cc0632012-08-22 17:17:09 +00003850FileSpec
3851TargetProperties::GetStandardInputPath () const
3852{
3853 const uint32_t idx = ePropertyInputPath;
3854 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3855}
3856
3857void
3858TargetProperties::SetStandardInputPath (const char *p)
3859{
3860 const uint32_t idx = ePropertyInputPath;
3861 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3862}
3863
3864FileSpec
3865TargetProperties::GetStandardOutputPath () const
3866{
3867 const uint32_t idx = ePropertyOutputPath;
3868 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3869}
3870
3871void
3872TargetProperties::SetStandardOutputPath (const char *p)
3873{
3874 const uint32_t idx = ePropertyOutputPath;
3875 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3876}
3877
3878FileSpec
3879TargetProperties::GetStandardErrorPath () const
3880{
3881 const uint32_t idx = ePropertyErrorPath;
3882 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3883}
3884
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003885LanguageType
3886TargetProperties::GetLanguage () const
3887{
3888 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage (NULL, ePropertyLanguage);
3889 if (value)
3890 return value->GetCurrentValue();
3891 return LanguageType();
3892}
3893
Greg Clayton6920b522012-08-22 18:39:03 +00003894const char *
3895TargetProperties::GetExpressionPrefixContentsAsCString ()
3896{
3897 const uint32_t idx = ePropertyExprPrefix;
3898 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3899 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003900 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003901 const bool null_terminate = true;
3902 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003903 if (data_sp)
3904 return (const char *) data_sp->GetBytes();
3905 }
Greg Clayton6920b522012-08-22 18:39:03 +00003906 return NULL;
3907}
3908
Greg Clayton67cc0632012-08-22 17:17:09 +00003909void
3910TargetProperties::SetStandardErrorPath (const char *p)
3911{
3912 const uint32_t idx = ePropertyErrorPath;
3913 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3914}
3915
3916bool
3917TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3918{
3919 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3920 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3921}
3922
Jim Ingham17d023f2013-03-13 17:58:04 +00003923bool
Daniel Malead79ae052013-08-07 21:54:09 +00003924TargetProperties::GetUseHexImmediates () const
3925{
3926 const uint32_t idx = ePropertyUseHexImmediates;
3927 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3928}
3929
3930bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003931TargetProperties::GetUseFastStepping () const
3932{
3933 const uint32_t idx = ePropertyUseFastStepping;
3934 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3935}
3936
Greg Claytonfb6621e2013-12-06 21:59:52 +00003937bool
3938TargetProperties::GetDisplayExpressionsInCrashlogs () const
3939{
3940 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3941 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3942}
3943
Enrico Granata397ddd52013-05-21 20:13:34 +00003944LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003945TargetProperties::GetLoadScriptFromSymbolFile () const
3946{
3947 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003948 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003949}
3950
Daniel Malead79ae052013-08-07 21:54:09 +00003951Disassembler::HexImmediateStyle
3952TargetProperties::GetHexImmediateStyle () const
3953{
3954 const uint32_t idx = ePropertyHexImmediateStyle;
3955 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3956}
3957
Greg Claytonfd814c52013-08-13 01:42:25 +00003958MemoryModuleLoadLevel
3959TargetProperties::GetMemoryModuleLoadLevel() const
3960{
3961 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3962 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3963}
3964
Jason Molendaa4bea722014-02-14 05:06:49 +00003965bool
3966TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3967{
3968 const uint32_t idx = ePropertyTrapHandlerNames;
3969 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3970}
Greg Claytonfd814c52013-08-13 01:42:25 +00003971
Jason Molendaa4bea722014-02-14 05:06:49 +00003972void
3973TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3974{
3975 const uint32_t idx = ePropertyTrapHandlerNames;
3976 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3977}
Greg Clayton67cc0632012-08-22 17:17:09 +00003978
Enrico Granata560558e2015-02-11 02:35:39 +00003979bool
3980TargetProperties::GetDisplayRuntimeSupportValues () const
3981{
3982 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3983 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3984}
3985
3986void
3987TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3988{
3989 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3990 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3991}
3992
Ewan Crawford78baa192015-05-13 09:18:18 +00003993bool
3994TargetProperties::GetNonStopModeEnabled () const
3995{
3996 const uint32_t idx = ePropertyNonStopModeEnabled;
3997 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3998}
3999
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004000void
4001TargetProperties::SetNonStopModeEnabled (bool b)
4002{
4003 const uint32_t idx = ePropertyNonStopModeEnabled;
4004 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
4005}
4006
Ilia K8f37ca52015-02-13 14:31:06 +00004007const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00004008TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00004009{
Ilia Kcc39d3f2015-02-13 17:07:55 +00004010 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00004011 return m_launch_info;
4012}
4013
4014void
4015TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
4016{
4017 m_launch_info = launch_info;
4018 SetArg0(launch_info.GetArg0());
4019 SetRunArguments(launch_info.GetArguments());
4020 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
4021 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
4022 if (input_file_action)
4023 {
4024 const char *input_path = input_file_action->GetPath();
4025 if (input_path)
4026 SetStandardInputPath(input_path);
4027 }
4028 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
4029 if (output_file_action)
4030 {
4031 const char *output_path = output_file_action->GetPath();
4032 if (output_path)
4033 SetStandardOutputPath(output_path);
4034 }
4035 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
4036 if (error_file_action)
4037 {
4038 const char *error_path = error_file_action->GetPath();
4039 if (error_path)
4040 SetStandardErrorPath(error_path);
4041 }
4042 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
4043 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4044 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
4045}
4046
4047void
4048TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
4049{
4050 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4051 this_->m_launch_info.SetArg0(this_->GetArg0());
4052}
4053
4054void
4055TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
4056{
4057 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4058 Args args;
4059 if (this_->GetRunArguments(args))
4060 this_->m_launch_info.GetArguments() = args;
4061}
4062
4063void
4064TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
4065{
4066 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4067 Args args;
4068 if (this_->GetEnvironmentAsArgs(args))
4069 this_->m_launch_info.GetEnvironmentEntries() = args;
4070}
4071
4072void
4073TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4074{
4075 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004076 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004077}
4078
4079void
4080TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4081{
4082 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004083 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004084}
4085
4086void
4087TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4088{
4089 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004090 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004091}
4092
4093void
4094TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
4095{
4096 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4097 if (this_->GetDetachOnError())
4098 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4099 else
4100 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
4101}
4102
4103void
4104TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
4105{
4106 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4107 if (this_->GetDisableASLR())
4108 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4109 else
4110 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
4111}
4112
4113void
4114TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
4115{
4116 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4117 if (this_->GetDisableSTDIO())
4118 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4119 else
4120 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4121}
Ilia Keb2c19a2015-03-10 21:59:55 +00004122
4123//----------------------------------------------------------------------
4124// Target::TargetEventData
4125//----------------------------------------------------------------------
4126
4127Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
4128 EventData (),
4129 m_target_sp (target_sp),
4130 m_module_list ()
4131{
4132}
4133
4134Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
4135 EventData (),
4136 m_target_sp (target_sp),
4137 m_module_list (module_list)
4138{
4139}
4140
Eugene Zelenko8f30a652015-10-23 18:39:37 +00004141Target::TargetEventData::~TargetEventData() = default;
Ilia Keb2c19a2015-03-10 21:59:55 +00004142
4143const ConstString &
4144Target::TargetEventData::GetFlavorString ()
4145{
4146 static ConstString g_flavor ("Target::TargetEventData");
4147 return g_flavor;
4148}
4149
4150void
4151Target::TargetEventData::Dump (Stream *s) const
4152{
4153}
4154
4155const Target::TargetEventData *
4156Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4157{
4158 if (event_ptr)
4159 {
4160 const EventData *event_data = event_ptr->GetData();
4161 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4162 return static_cast <const TargetEventData *> (event_ptr->GetData());
4163 }
4164 return NULL;
4165}
4166
4167TargetSP
4168Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4169{
4170 TargetSP target_sp;
4171 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4172 if (event_data)
4173 target_sp = event_data->m_target_sp;
4174 return target_sp;
4175}
4176
4177ModuleList
4178Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4179{
4180 ModuleList module_list;
4181 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4182 if (event_data)
4183 module_list = event_data->m_module_list;
4184 return module_list;
4185}