blob: 1f6bacca2b4305270326bdcd8a755b84c65a40f8 [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),
Greg Claytone01e07b2013-04-18 18:10:51 +000086 m_ast_importer_ap (),
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,
585 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000586 bool internal,
587 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588{
Jim Ingham87df91b2011-09-23 00:54:11 +0000589 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000590 bool skip =
591 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
592 : static_cast<bool>(skip_prologue);
Greg Claytond16e1e52011-07-12 17:06:17 +0000593 BreakpointResolverSP resolver_sp(new BreakpointResolverName (NULL,
594 func_regex,
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000595 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596
Jim Ingham1460e4b2014-01-10 23:46:59 +0000597 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598}
599
Jim Ingham219ba192012-03-05 04:47:34 +0000600lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000601Target::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 +0000602{
Jim Inghama72b31c2015-04-22 19:42:18 +0000603 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
604 if (exc_bkpt_sp && additional_args)
605 {
606 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
607 if (precondition_sp && additional_args)
608 {
609 if (error)
610 *error = precondition_sp->ConfigurePrecondition(*additional_args);
611 else
612 precondition_sp->ConfigurePrecondition(*additional_args);
613 }
614 }
615 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000616}
Jim Inghama72b31c2015-04-22 19:42:18 +0000617
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000618BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000619Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620{
621 BreakpointSP bp_sp;
622 if (filter_sp && resolver_sp)
623 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000624 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000626 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000628 return bp_sp;
629}
630
631void
632Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
633{
634 if (!bp_sp)
635 return;
636 if (internal)
637 m_internal_breakpoint_list.Add (bp_sp, false);
638 else
639 m_breakpoint_list.Add (bp_sp, true);
640
641 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
642 if (log)
643 {
644 StreamString s;
645 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
646 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
647 }
648
649 bp_sp->ResolveBreakpoint();
650
651 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000652 {
653 m_last_created_breakpoint = bp_sp;
654 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655}
656
Johnny Chen86364b42011-09-20 23:28:55 +0000657bool
658Target::ProcessIsValid()
659{
660 return (m_process_sp && m_process_sp->IsAlive());
661}
662
Johnny Chenb90827e2012-06-04 23:19:54 +0000663static bool
664CheckIfWatchpointsExhausted(Target *target, Error &error)
665{
666 uint32_t num_supported_hardware_watchpoints;
667 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
668 if (rc.Success())
669 {
670 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
671 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
672 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
673 num_supported_hardware_watchpoints);
674 }
675 return false;
676}
677
Johnny Chen01a67862011-10-14 00:42:25 +0000678// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000679// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000680WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000681Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000682{
Greg Clayton5160ce52013-03-27 23:08:40 +0000683 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000684 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000685 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000686 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000687
Johnny Chen01a67862011-10-14 00:42:25 +0000688 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000689 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000690 {
691 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000692 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000693 }
Jim Inghamc6462312013-06-18 21:52:48 +0000694
Johnny Chen45e541f2011-09-14 22:20:15 +0000695 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000696 {
697 if (size == 0)
698 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
699 else
Daniel Malead01b2952012-11-29 21:49:15 +0000700 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000701 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000702 }
Jim Inghamc6462312013-06-18 21:52:48 +0000703
704 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
705 {
706 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
707 }
Johnny Chen7313a642011-09-13 01:15:36 +0000708
Johnny Chen01a67862011-10-14 00:42:25 +0000709 // Currently we only support one watchpoint per address, with total number
710 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000711
712 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000713 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 +0000714 Mutex::Locker locker;
715 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000716 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000717 if (matched_sp)
718 {
Johnny Chen0c406372011-09-14 20:23:45 +0000719 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000720 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000721 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
722 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000723 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000724 if (size == old_size && kind == old_type)
725 {
Johnny Chen01a67862011-10-14 00:42:25 +0000726 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000727 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000728 }
729 else
730 {
Johnny Chen01a67862011-10-14 00:42:25 +0000731 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000732 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
733 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000734 }
Johnny Chen3c532582011-09-13 23:29:31 +0000735 }
736
Jason Molenda727e3922012-12-05 23:07:34 +0000737 if (!wp_sp)
738 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000739 wp_sp.reset(new Watchpoint(*this, addr, size, type));
740 wp_sp->SetWatchpointType(kind, notify);
741 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000742 }
Johnny Chen0c406372011-09-14 20:23:45 +0000743
Jim Ingham1b5792e2012-12-18 02:03:49 +0000744 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000745 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000746 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
747 __FUNCTION__,
748 error.Success() ? "succeeded" : "failed",
749 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000750
Jason Molenda727e3922012-12-05 23:07:34 +0000751 if (error.Fail())
752 {
Johnny Chen41b77262012-03-26 22:00:10 +0000753 // Enabling the watchpoint on the device side failed.
754 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000755 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000756 // See if we could provide more helpful error message.
757 if (!CheckIfWatchpointsExhausted(this, error))
758 {
759 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000760 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000761 }
Johnny Chen01a67862011-10-14 00:42:25 +0000762 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000763 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000764 else
Johnny Chen01a67862011-10-14 00:42:25 +0000765 m_last_created_watchpoint = wp_sp;
766 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000767}
768
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769void
770Target::RemoveAllBreakpoints (bool internal_also)
771{
Greg Clayton5160ce52013-03-27 23:08:40 +0000772 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 if (log)
774 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
775
Greg Clayton9fed0d82010-07-23 23:33:17 +0000776 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000778 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000779
780 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781}
782
783void
784Target::DisableAllBreakpoints (bool internal_also)
785{
Greg Clayton5160ce52013-03-27 23:08:40 +0000786 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000787 if (log)
788 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
789
790 m_breakpoint_list.SetEnabledAll (false);
791 if (internal_also)
792 m_internal_breakpoint_list.SetEnabledAll (false);
793}
794
795void
796Target::EnableAllBreakpoints (bool internal_also)
797{
Greg Clayton5160ce52013-03-27 23:08:40 +0000798 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 if (log)
800 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
801
802 m_breakpoint_list.SetEnabledAll (true);
803 if (internal_also)
804 m_internal_breakpoint_list.SetEnabledAll (true);
805}
806
807bool
808Target::RemoveBreakpointByID (break_id_t break_id)
809{
Greg Clayton5160ce52013-03-27 23:08:40 +0000810 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 if (log)
812 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
813
814 if (DisableBreakpointByID (break_id))
815 {
816 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000817 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000819 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000820 if (m_last_created_breakpoint)
821 {
822 if (m_last_created_breakpoint->GetID() == break_id)
823 m_last_created_breakpoint.reset();
824 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000825 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000826 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000827 return true;
828 }
829 return false;
830}
831
832bool
833Target::DisableBreakpointByID (break_id_t break_id)
834{
Greg Clayton5160ce52013-03-27 23:08:40 +0000835 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 if (log)
837 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
838
839 BreakpointSP bp_sp;
840
841 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
842 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
843 else
844 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
845 if (bp_sp)
846 {
847 bp_sp->SetEnabled (false);
848 return true;
849 }
850 return false;
851}
852
853bool
854Target::EnableBreakpointByID (break_id_t break_id)
855{
Greg Clayton5160ce52013-03-27 23:08:40 +0000856 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857 if (log)
858 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
859 __FUNCTION__,
860 break_id,
861 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
862
863 BreakpointSP bp_sp;
864
865 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
866 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
867 else
868 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
869
870 if (bp_sp)
871 {
872 bp_sp->SetEnabled (true);
873 return true;
874 }
875 return false;
876}
877
Johnny Chenedf50372011-09-23 21:21:43 +0000878// The flag 'end_to_end', default to true, signifies that the operation is
879// performed end to end, for both the debugger and the debuggee.
880
Johnny Chen01a67862011-10-14 00:42:25 +0000881// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
882// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000883bool
Johnny Chen01a67862011-10-14 00:42:25 +0000884Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000885{
Greg Clayton5160ce52013-03-27 23:08:40 +0000886 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000887 if (log)
888 log->Printf ("Target::%s\n", __FUNCTION__);
889
Johnny Chenedf50372011-09-23 21:21:43 +0000890 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000891 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000892 return true;
893 }
894
895 // Otherwise, it's an end to end operation.
896
Johnny Chen86364b42011-09-20 23:28:55 +0000897 if (!ProcessIsValid())
898 return false;
899
Johnny Chen01a67862011-10-14 00:42:25 +0000900 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000901 for (size_t i = 0; i < num_watchpoints; ++i)
902 {
Johnny Chen01a67862011-10-14 00:42:25 +0000903 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
904 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000905 return false;
906
Johnny Chen01a67862011-10-14 00:42:25 +0000907 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000908 if (rc.Fail())
909 return false;
910 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000911 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000912 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000913 return true; // Success!
914}
915
Johnny Chen01a67862011-10-14 00:42:25 +0000916// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
917// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000918bool
Johnny Chen01a67862011-10-14 00:42:25 +0000919Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000920{
Greg Clayton5160ce52013-03-27 23:08:40 +0000921 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000922 if (log)
923 log->Printf ("Target::%s\n", __FUNCTION__);
924
Johnny Chenedf50372011-09-23 21:21:43 +0000925 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000926 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000927 return true;
928 }
929
930 // Otherwise, it's an end to end operation.
931
Johnny Chen86364b42011-09-20 23:28:55 +0000932 if (!ProcessIsValid())
933 return false;
934
Johnny Chen01a67862011-10-14 00:42:25 +0000935 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000936 for (size_t i = 0; i < num_watchpoints; ++i)
937 {
Johnny Chen01a67862011-10-14 00:42:25 +0000938 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
939 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000940 return false;
941
Johnny Chen01a67862011-10-14 00:42:25 +0000942 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000943 if (rc.Fail())
944 return false;
945 }
Johnny Chen86364b42011-09-20 23:28:55 +0000946 return true; // Success!
947}
948
Johnny Chen01a67862011-10-14 00:42:25 +0000949// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
950// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000951bool
Johnny Chen01a67862011-10-14 00:42:25 +0000952Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000953{
Greg Clayton5160ce52013-03-27 23:08:40 +0000954 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000955 if (log)
956 log->Printf ("Target::%s\n", __FUNCTION__);
957
Johnny Chenedf50372011-09-23 21:21:43 +0000958 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000959 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000960 return true;
961 }
962
963 // Otherwise, it's an end to end operation.
964
Johnny Chen86364b42011-09-20 23:28:55 +0000965 if (!ProcessIsValid())
966 return false;
967
Johnny Chen01a67862011-10-14 00:42:25 +0000968 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000969 for (size_t i = 0; i < num_watchpoints; ++i)
970 {
Johnny Chen01a67862011-10-14 00:42:25 +0000971 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
972 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000973 return false;
974
Johnny Chen01a67862011-10-14 00:42:25 +0000975 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000976 if (rc.Fail())
977 return false;
978 }
Johnny Chen86364b42011-09-20 23:28:55 +0000979 return true; // Success!
980}
981
Johnny Chena4d6bc92012-02-25 06:44:30 +0000982// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
983bool
984Target::ClearAllWatchpointHitCounts ()
985{
Greg Clayton5160ce52013-03-27 23:08:40 +0000986 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +0000987 if (log)
988 log->Printf ("Target::%s\n", __FUNCTION__);
989
990 size_t num_watchpoints = m_watchpoint_list.GetSize();
991 for (size_t i = 0; i < num_watchpoints; ++i)
992 {
993 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
994 if (!wp_sp)
995 return false;
996
997 wp_sp->ResetHitCount();
998 }
999 return true; // Success!
1000}
1001
Enrico Granata5e3fe042015-02-11 00:37:54 +00001002// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1003bool
1004Target::ClearAllWatchpointHistoricValues ()
1005{
1006 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
1007 if (log)
1008 log->Printf ("Target::%s\n", __FUNCTION__);
1009
1010 size_t num_watchpoints = m_watchpoint_list.GetSize();
1011 for (size_t i = 0; i < num_watchpoints; ++i)
1012 {
1013 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1014 if (!wp_sp)
1015 return false;
1016
1017 wp_sp->ResetHistoricValues();
1018 }
1019 return true; // Success!
1020}
1021
Johnny Chen01a67862011-10-14 00:42:25 +00001022// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +00001023// during these operations.
1024bool
Johnny Chen01a67862011-10-14 00:42:25 +00001025Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001026{
Greg Clayton5160ce52013-03-27 23:08:40 +00001027 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001028 if (log)
1029 log->Printf ("Target::%s\n", __FUNCTION__);
1030
1031 if (!ProcessIsValid())
1032 return false;
1033
Johnny Chen01a67862011-10-14 00:42:25 +00001034 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +00001035 for (size_t i = 0; i < num_watchpoints; ++i)
1036 {
Johnny Chen01a67862011-10-14 00:42:25 +00001037 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1038 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001039 return false;
1040
Johnny Chen01a67862011-10-14 00:42:25 +00001041 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001042 }
1043 return true; // Success!
1044}
1045
Johnny Chen01a67862011-10-14 00:42:25 +00001046// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001047bool
Johnny Chen01a67862011-10-14 00:42:25 +00001048Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001049{
Greg Clayton5160ce52013-03-27 23:08:40 +00001050 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001051 if (log)
1052 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1053
1054 if (!ProcessIsValid())
1055 return false;
1056
Johnny Chen01a67862011-10-14 00:42:25 +00001057 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1058 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001059 {
Johnny Chen01a67862011-10-14 00:42:25 +00001060 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001061 if (rc.Success())
1062 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001063
Johnny Chenf04ee932011-09-22 18:04:58 +00001064 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001065 }
1066 return false;
1067}
1068
Johnny Chen01a67862011-10-14 00:42:25 +00001069// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001070bool
Johnny Chen01a67862011-10-14 00:42:25 +00001071Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001072{
Greg Clayton5160ce52013-03-27 23:08:40 +00001073 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001074 if (log)
1075 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1076
1077 if (!ProcessIsValid())
1078 return false;
1079
Johnny Chen01a67862011-10-14 00:42:25 +00001080 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1081 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001082 {
Johnny Chen01a67862011-10-14 00:42:25 +00001083 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001084 if (rc.Success())
1085 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001086
Johnny Chenf04ee932011-09-22 18:04:58 +00001087 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001088 }
1089 return false;
1090}
1091
Johnny Chen01a67862011-10-14 00:42:25 +00001092// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001093bool
Johnny Chen01a67862011-10-14 00:42:25 +00001094Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001095{
Greg Clayton5160ce52013-03-27 23:08:40 +00001096 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001097 if (log)
1098 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1099
Jim Inghamb0b45132013-07-02 02:09:46 +00001100 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1101 if (watch_to_remove_sp == m_last_created_watchpoint)
1102 m_last_created_watchpoint.reset();
1103
Johnny Chen01a67862011-10-14 00:42:25 +00001104 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001105 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001106 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001107 return true;
1108 }
1109 return false;
1110}
1111
Johnny Chen01a67862011-10-14 00:42:25 +00001112// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001113bool
Johnny Chen01a67862011-10-14 00:42:25 +00001114Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001115{
Greg Clayton5160ce52013-03-27 23:08:40 +00001116 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001117 if (log)
1118 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1119
1120 if (!ProcessIsValid())
1121 return false;
1122
Johnny Chen01a67862011-10-14 00:42:25 +00001123 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1124 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001125 {
Johnny Chen01a67862011-10-14 00:42:25 +00001126 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001127 return true;
1128 }
1129 return false;
1130}
1131
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001132ModuleSP
1133Target::GetExecutableModule ()
1134{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001135 // search for the first executable in the module list
1136 for (size_t i = 0; i < m_images.GetSize(); ++i)
1137 {
1138 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1139 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1140 if (obj == nullptr)
1141 continue;
1142 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1143 return module_sp;
1144 }
1145 // as fall back return the first module loaded
1146 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001147}
1148
1149Module*
1150Target::GetExecutableModulePointer ()
1151{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001152 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153}
1154
Enrico Granata17598482012-11-08 02:22:02 +00001155static void
1156LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1157{
1158 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001159 StreamString feedback_stream;
1160 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001161 {
Enrico Granata97303392013-05-21 00:00:30 +00001162 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001163 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001164 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1165 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001166 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001167 if (feedback_stream.GetSize())
1168 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1169 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001170}
1171
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001172void
Greg Claytonb35db632013-11-09 00:03:31 +00001173Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174{
Greg Claytonb35db632013-11-09 00:03:31 +00001175 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001176 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001177 m_images.Clear();
Greg Clayton5beec212015-10-08 21:04:34 +00001178 m_scratch_type_system_map.Clear();
Sean Callanan686b2312011-11-16 18:20:47 +00001179 m_ast_importer_ap.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001180}
1181
1182void
Greg Claytonb35db632013-11-09 00:03:31 +00001183Target::DidExec ()
1184{
1185 // When a process exec's we need to know about it so we can do some cleanup.
1186 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1187 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1188}
1189
1190void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001191Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1192{
1193 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001194 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195
1196 if (executable_sp.get())
1197 {
1198 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001199 "Target::SetExecutableModule (executable = '%s')",
1200 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001201
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001202 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203
Jim Ingham5aee1622010-08-09 23:31:02 +00001204 // 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 +00001205 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001206 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001207 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001208 if (log)
1209 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1210 }
1211
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001212 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001213 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001215 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216 {
1217 executable_objfile->GetDependentModules(dependent_files);
1218 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1219 {
Greg Claytonded470d2011-03-19 01:12:21 +00001220 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1221 FileSpec platform_dependent_file_spec;
1222 if (m_platform_sp)
Steve Puccifc995722014-01-17 18:18:31 +00001223 m_platform_sp->GetFileWithUUID (dependent_file_spec, NULL, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001224 else
1225 platform_dependent_file_spec = dependent_file_spec;
1226
Greg Claytonb9a01b32012-02-26 05:51:37 +00001227 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1228 ModuleSP image_module_sp(GetSharedModule (module_spec));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229 if (image_module_sp.get())
1230 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001231 ObjectFile *objfile = image_module_sp->GetObjectFile();
1232 if (objfile)
1233 objfile->GetDependentModules(dependent_files);
1234 }
1235 }
1236 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237 }
1238}
1239
Jim Ingham5aee1622010-08-09 23:31:02 +00001240bool
1241Target::SetArchitecture (const ArchSpec &arch_spec)
1242{
Greg Clayton5160ce52013-03-27 23:08:40 +00001243 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Sean Callananbf4b7be2012-12-13 22:07:14 +00001244 if (m_arch.IsCompatibleMatch(arch_spec) || !m_arch.IsValid())
Jim Ingham5aee1622010-08-09 23:31:02 +00001245 {
Greg Clayton70512312012-05-08 01:45:38 +00001246 // If we haven't got a valid arch spec, or the architectures are
1247 // compatible, so just update the architecture. Architectures can be
1248 // equal, yet the triple OS and vendor might change, so we need to do
1249 // the assignment here just in case.
Greg Clayton32e0a752011-03-30 18:16:51 +00001250 m_arch = arch_spec;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001251 if (log)
1252 log->Printf ("Target::SetArchitecture setting architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Jim Ingham5aee1622010-08-09 23:31:02 +00001253 return true;
1254 }
1255 else
1256 {
1257 // If we have an executable file, try to reset the executable to the desired architecture
Jason Molendae1b68ad2012-12-05 00:25:49 +00001258 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +00001259 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Greg Clayton32e0a752011-03-30 18:16:51 +00001260 m_arch = arch_spec;
Jim Ingham5aee1622010-08-09 23:31:02 +00001261 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001262
Greg Claytonb35db632013-11-09 00:03:31 +00001263 ClearModules(true);
Jim Ingham5aee1622010-08-09 23:31:02 +00001264 // Need to do something about unsetting breakpoints.
1265
1266 if (executable_sp)
1267 {
Jason Molendae1b68ad2012-12-05 00:25:49 +00001268 if (log)
1269 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
Greg Claytonb9a01b32012-02-26 05:51:37 +00001270 ModuleSpec module_spec (executable_sp->GetFileSpec(), arch_spec);
1271 Error error = ModuleList::GetSharedModule (module_spec,
1272 executable_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001273 &GetExecutableSearchPaths(),
Greg Claytonb9a01b32012-02-26 05:51:37 +00001274 NULL,
1275 NULL);
Jim Ingham5aee1622010-08-09 23:31:02 +00001276
1277 if (!error.Fail() && executable_sp)
1278 {
1279 SetExecutableModule (executable_sp, true);
1280 return true;
1281 }
Jim Ingham5aee1622010-08-09 23:31:02 +00001282 }
1283 }
Greg Clayton70512312012-05-08 01:45:38 +00001284 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001285}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001286
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001287bool
1288Target::MergeArchitecture (const ArchSpec &arch_spec)
1289{
1290 if (arch_spec.IsValid())
1291 {
1292 if (m_arch.IsCompatibleMatch(arch_spec))
1293 {
1294 // The current target arch is compatible with "arch_spec", see if we
1295 // can improve our current architecture using bits from "arch_spec"
1296
1297 // Merge bits from arch_spec into "merged_arch" and set our architecture
1298 ArchSpec merged_arch (m_arch);
1299 merged_arch.MergeFrom (arch_spec);
1300 return SetArchitecture(merged_arch);
1301 }
1302 else
1303 {
1304 // The new architecture is different, we just need to replace it
1305 return SetArchitecture(arch_spec);
1306 }
1307 }
1308 return false;
1309}
1310
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001311void
Enrico Granataefe637d2012-11-08 19:16:03 +00001312Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001313{
1314}
1315
1316void
Enrico Granataefe637d2012-11-08 19:16:03 +00001317Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001318{
1319 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001320 if (m_valid)
1321 {
1322 ModuleList my_module_list;
1323 my_module_list.Append(module_sp);
1324 LoadScriptingResourceForModule(module_sp, this);
1325 ModulesDidLoad (my_module_list);
1326 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001327}
1328
1329void
Enrico Granataefe637d2012-11-08 19:16:03 +00001330Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001331{
Adrian McCarthy6ecdbc82015-10-15 22:39:55 +00001332 // A module is being removed from this target.
Greg Clayton23f8c952014-03-24 23:10:19 +00001333 if (m_valid)
1334 {
1335 ModuleList my_module_list;
1336 my_module_list.Append(module_sp);
1337 ModulesDidUnload (my_module_list, false);
1338 }
Enrico Granata17598482012-11-08 02:22:02 +00001339}
1340
1341void
Enrico Granataefe637d2012-11-08 19:16:03 +00001342Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001343{
Jim Inghame716ae02011-08-03 01:00:06 +00001344 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001345 if (m_valid)
1346 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001347}
1348
1349void
1350Target::ModulesDidLoad (ModuleList &module_list)
1351{
Greg Clayton23f8c952014-03-24 23:10:19 +00001352 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001353 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001354 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001355 if (m_process_sp)
1356 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001357 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001358 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001359 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001360 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001361}
1362
1363void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001364Target::SymbolsDidLoad (ModuleList &module_list)
1365{
Greg Clayton23f8c952014-03-24 23:10:19 +00001366 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001367 {
Jim Ingham31caf982013-06-04 23:01:35 +00001368 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001369 {
Jim Ingham31caf982013-06-04 23:01:35 +00001370 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1371 if (runtime)
1372 {
1373 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1374 objc_runtime->SymbolsDidLoad(module_list);
1375 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001376 }
Jim Ingham31caf982013-06-04 23:01:35 +00001377
Greg Clayton095eeaa2013-11-05 23:28:00 +00001378 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001379 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001380 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001381}
1382
1383void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001384Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001385{
Greg Clayton23f8c952014-03-24 23:10:19 +00001386 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001387 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001388 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001389 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001390 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001391 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001392}
1393
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001394bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001395Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001396{
Greg Clayton67cc0632012-08-22 17:17:09 +00001397 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001398 {
1399 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001400 ModuleSpec module_spec (module_file_spec);
1401 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001402
1403 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1404 // black list.
1405 if (num_modules > 0)
1406 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001407 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001408 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001409 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001410 return false;
1411 }
1412 return true;
1413 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001414 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001415 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001416}
1417
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001418bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001419Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001420{
Greg Clayton67cc0632012-08-22 17:17:09 +00001421 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001422 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001423 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001424 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001425 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001426 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001427}
1428
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001429size_t
Greg Claytondb598232011-01-07 01:57:07 +00001430Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1431{
Greg Claytone72dfb32012-02-24 01:59:29 +00001432 SectionSP section_sp (addr.GetSection());
1433 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001434 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001435 // 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 +00001436 if (section_sp->IsEncrypted())
1437 {
Greg Clayton57f06302012-05-25 17:05:55 +00001438 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001439 return 0;
1440 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001441 ModuleSP module_sp (section_sp->GetModule());
1442 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001443 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001444 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1445 if (objfile)
1446 {
1447 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1448 addr.GetOffset(),
1449 dst,
1450 dst_len);
1451 if (bytes_read > 0)
1452 return bytes_read;
1453 else
1454 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1455 }
Greg Claytondb598232011-01-07 01:57:07 +00001456 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001457 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001458 }
1459 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001460 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001461 }
1462 else
Greg Claytondb598232011-01-07 01:57:07 +00001463 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001464
Greg Claytondb598232011-01-07 01:57:07 +00001465 return 0;
1466}
1467
1468size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001469Target::ReadMemory (const Address& addr,
1470 bool prefer_file_cache,
1471 void *dst,
1472 size_t dst_len,
1473 Error &error,
1474 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001477
Enrico Granata9128ee22011-09-06 19:20:51 +00001478 // if we end up reading this from process memory, we will fill this
1479 // with the actual load address
1480 if (load_addr_ptr)
1481 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1482
Greg Claytondb598232011-01-07 01:57:07 +00001483 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001484
1485 addr_t load_addr = LLDB_INVALID_ADDRESS;
1486 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001487 Address resolved_addr;
1488 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001489 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001490 SectionLoadList &section_load_list = GetSectionLoadList();
1491 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001492 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001493 // No sections are loaded, so we must assume we are not running
1494 // yet and anything we are given is a file address.
1495 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1496 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001497 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001498 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001499 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001500 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001501 // we have manually loaded some sections with "target modules load ..."
1502 // or because we have have a live process that has sections loaded
1503 // through the dynamic loader
1504 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001505 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001506 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001507 }
Greg Clayton357132e2011-03-26 19:14:58 +00001508 if (!resolved_addr.IsValid())
1509 resolved_addr = addr;
Greg Claytonc749eb82011-07-11 05:12:02 +00001510
Greg Claytondb598232011-01-07 01:57:07 +00001511 if (prefer_file_cache)
1512 {
1513 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1514 if (bytes_read > 0)
1515 return bytes_read;
1516 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001517
Johnny Chen86364b42011-09-20 23:28:55 +00001518 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001519 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001520 if (load_addr == LLDB_INVALID_ADDRESS)
1521 load_addr = resolved_addr.GetLoadAddress (this);
1522
Greg Claytondda4f7b2010-06-30 23:03:03 +00001523 if (load_addr == LLDB_INVALID_ADDRESS)
1524 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001525 ModuleSP addr_module_sp (resolved_addr.GetModule());
1526 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001527 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001528 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001529 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001530 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001531 else
Daniel Malead01b2952012-11-29 21:49:15 +00001532 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001533 }
1534 else
1535 {
Greg Claytondb598232011-01-07 01:57:07 +00001536 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001537 if (bytes_read != dst_len)
1538 {
1539 if (error.Success())
1540 {
1541 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001542 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001543 else
Daniel Malead01b2952012-11-29 21:49:15 +00001544 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 +00001545 }
1546 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001547 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001548 {
1549 if (load_addr_ptr)
1550 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001551 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001552 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001553 // If the address is not section offset we have an address that
1554 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001555 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001556 // more we can do. If it is section offset, we might be able to
1557 // read cached memory from the object file.
1558 if (!resolved_addr.IsSectionOffset())
1559 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001560 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001562
Greg Claytonc749eb82011-07-11 05:12:02 +00001563 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001564 {
Greg Claytondb598232011-01-07 01:57:07 +00001565 // If we didn't already try and read from the object file cache, then
1566 // try it after failing to read from the process.
1567 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001568 }
1569 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001570}
1571
Greg Claytond16e1e52011-07-12 17:06:17 +00001572size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001573Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1574{
1575 char buf[256];
1576 out_str.clear();
1577 addr_t curr_addr = addr.GetLoadAddress(this);
1578 Address address(addr);
1579 while (1)
1580 {
1581 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1582 if (length == 0)
1583 break;
1584 out_str.append(buf, length);
1585 // If we got "length - 1" bytes, we didn't get the whole C string, we
1586 // need to read some more characters
1587 if (length == sizeof(buf) - 1)
1588 curr_addr += length;
1589 else
1590 break;
1591 address = Address(curr_addr);
1592 }
1593 return out_str.size();
1594}
1595
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001596size_t
1597Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1598{
1599 size_t total_cstr_len = 0;
1600 if (dst && dst_max_len)
1601 {
1602 result_error.Clear();
1603 // NULL out everything just to be safe
1604 memset (dst, 0, dst_max_len);
1605 Error error;
1606 addr_t curr_addr = addr.GetLoadAddress(this);
1607 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001608
1609 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1610 // think this really needs to be tied to the memory cache subsystem's
1611 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001612 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001613
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001614 size_t bytes_left = dst_max_len - 1;
1615 char *curr_dst = dst;
1616
1617 while (bytes_left > 0)
1618 {
1619 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1620 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1621 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1622
1623 if (bytes_read == 0)
1624 {
1625 result_error = error;
1626 dst[total_cstr_len] = '\0';
1627 break;
1628 }
1629 const size_t len = strlen(curr_dst);
1630
1631 total_cstr_len += len;
1632
1633 if (len < bytes_to_read)
1634 break;
1635
1636 curr_dst += bytes_read;
1637 curr_addr += bytes_read;
1638 bytes_left -= bytes_read;
1639 address = Address(curr_addr);
1640 }
1641 }
1642 else
1643 {
1644 if (dst == NULL)
1645 result_error.SetErrorString("invalid arguments");
1646 else
1647 result_error.Clear();
1648 }
1649 return total_cstr_len;
1650}
1651
1652size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001653Target::ReadScalarIntegerFromMemory (const Address& addr,
1654 bool prefer_file_cache,
1655 uint32_t byte_size,
1656 bool is_signed,
1657 Scalar &scalar,
1658 Error &error)
1659{
1660 uint64_t uval;
1661
1662 if (byte_size <= sizeof(uval))
1663 {
1664 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1665 if (bytes_read == byte_size)
1666 {
1667 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001668 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001669 if (byte_size <= 4)
1670 scalar = data.GetMaxU32 (&offset, byte_size);
1671 else
1672 scalar = data.GetMaxU64 (&offset, byte_size);
1673
1674 if (is_signed)
1675 scalar.SignExtend(byte_size * 8);
1676 return bytes_read;
1677 }
1678 }
1679 else
1680 {
1681 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1682 }
1683 return 0;
1684}
1685
1686uint64_t
1687Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1688 bool prefer_file_cache,
1689 size_t integer_byte_size,
1690 uint64_t fail_value,
1691 Error &error)
1692{
1693 Scalar scalar;
1694 if (ReadScalarIntegerFromMemory (addr,
1695 prefer_file_cache,
1696 integer_byte_size,
1697 false,
1698 scalar,
1699 error))
1700 return scalar.ULongLong(fail_value);
1701 return fail_value;
1702}
1703
1704bool
1705Target::ReadPointerFromMemory (const Address& addr,
1706 bool prefer_file_cache,
1707 Error &error,
1708 Address &pointer_addr)
1709{
1710 Scalar scalar;
1711 if (ReadScalarIntegerFromMemory (addr,
1712 prefer_file_cache,
1713 m_arch.GetAddressByteSize(),
1714 false,
1715 scalar,
1716 error))
1717 {
1718 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1719 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1720 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001721 SectionLoadList &section_load_list = GetSectionLoadList();
1722 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001723 {
1724 // No sections are loaded, so we must assume we are not running
1725 // yet and anything we are given is a file address.
1726 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1727 }
1728 else
1729 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001730 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001731 // we have manually loaded some sections with "target modules load ..."
1732 // or because we have have a live process that has sections loaded
1733 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001734 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001735 }
1736 // We weren't able to resolve the pointer value, so just return
1737 // an address with no section
1738 if (!pointer_addr.IsValid())
1739 pointer_addr.SetOffset (pointer_vm_addr);
1740 return true;
1741
1742 }
1743 }
1744 return false;
1745}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001746
1747ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001748Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750 ModuleSP module_sp;
1751
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752 Error error;
1753
Jim Ingham4a94c912012-05-17 18:38:42 +00001754 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1755 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001756
Jim Ingham4a94c912012-05-17 18:38:42 +00001757 if (module_spec.GetUUID().IsValid())
1758 module_sp = m_images.FindFirstModule(module_spec);
1759
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001760 if (!module_sp)
1761 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001762 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1763 bool did_create_module = false;
1764
1765 // If there are image search path entries, try to use them first to acquire a suitable image.
1766 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001767 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001768 ModuleSpec transformed_spec (module_spec);
1769 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1770 {
1771 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1772 error = ModuleList::GetSharedModule (transformed_spec,
1773 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001774 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001775 &old_module_sp,
1776 &did_create_module);
1777 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001778 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001779
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001780 if (!module_sp)
1781 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001782 // If we have a UUID, we can check our global shared module list in case
1783 // we already have it. If we don't have a valid UUID, then we can't since
1784 // the path in "module_spec" will be a platform path, and we will need to
1785 // let the platform find that file. For example, we could be asking for
1786 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1787 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1788 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1789 // cache.
1790 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001791 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001792 // We have a UUID, it is OK to check the global module list...
1793 error = ModuleList::GetSharedModule (module_spec,
1794 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001795 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001796 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001797 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001798 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001799
1800 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001801 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001802 // The platform is responsible for finding and caching an appropriate
1803 // module in the shared module cache.
1804 if (m_platform_sp)
1805 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001806 error = m_platform_sp->GetSharedModule (module_spec,
1807 m_process_sp.get(),
1808 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001809 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001810 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001811 &did_create_module);
1812 }
1813 else
1814 {
1815 error.SetErrorString("no platform is currently set");
1816 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001817 }
1818 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819
Jim Ingham4a94c912012-05-17 18:38:42 +00001820 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1821 // module in the list already, and if there was, let's remove it.
1822 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001823 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001824 ObjectFile *objfile = module_sp->GetObjectFile();
1825 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001826 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001827 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001828 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001829 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1830 case ObjectFile::eTypeExecutable: /// A normal executable
1831 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1832 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1833 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1834 break;
1835 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1836 if (error_ptr)
1837 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1838 return ModuleSP();
1839 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1840 if (error_ptr)
1841 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1842 return ModuleSP();
1843 default:
1844 if (error_ptr)
1845 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1846 return ModuleSP();
1847 }
1848 // GetSharedModule is not guaranteed to find the old shared module, for instance
1849 // in the common case where you pass in the UUID, it is only going to find the one
1850 // module matching the UUID. In fact, it has no good way to know what the "old module"
1851 // relevant to this target is, since there might be many copies of a module with this file spec
1852 // in various running debug sessions, but only one of them will belong to this target.
1853 // So let's remove the UUID from the module list, and look in the target's module list.
1854 // Only do this if there is SOMETHING else in the module spec...
1855 if (!old_module_sp)
1856 {
1857 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001858 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001859 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1860 module_spec_copy.GetUUID().Clear();
1861
1862 ModuleList found_modules;
1863 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1864 if (num_found == 1)
1865 {
1866 old_module_sp = found_modules.GetModuleAtIndex(0);
1867 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001868 }
1869 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001870
1871 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1872 {
1873 m_images.ReplaceModule(old_module_sp, module_sp);
1874 Module *old_module_ptr = old_module_sp.get();
1875 old_module_sp.reset();
1876 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1877 }
1878 else
1879 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001880 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001881 else
1882 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001883 }
1884 }
1885 if (error_ptr)
1886 *error_ptr = error;
1887 return module_sp;
1888}
1889
Greg Claytond9e416c2012-02-18 05:35:26 +00001890TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001891Target::CalculateTarget ()
1892{
Greg Claytond9e416c2012-02-18 05:35:26 +00001893 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894}
1895
Greg Claytond9e416c2012-02-18 05:35:26 +00001896ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897Target::CalculateProcess ()
1898{
Greg Claytond9e416c2012-02-18 05:35:26 +00001899 return ProcessSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001900}
1901
Greg Claytond9e416c2012-02-18 05:35:26 +00001902ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001903Target::CalculateThread ()
1904{
Greg Claytond9e416c2012-02-18 05:35:26 +00001905 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001906}
1907
Jason Molendab57e4a12013-11-04 09:33:30 +00001908StackFrameSP
1909Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001910{
Jason Molendab57e4a12013-11-04 09:33:30 +00001911 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001912}
1913
1914void
Greg Clayton0603aa92010-10-04 01:05:56 +00001915Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001916{
Greg Claytonc14ee322011-09-22 04:58:26 +00001917 exe_ctx.Clear();
1918 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001919}
1920
1921PathMappingList &
1922Target::GetImageSearchPathList ()
1923{
1924 return m_image_search_paths;
1925}
1926
1927void
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001928Target::ImageSearchPathsChanged(const PathMappingList &path_list,
1929 void *baton)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001930{
1931 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001932 ModuleSP exe_module_sp (target->GetExecutableModule());
1933 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001934 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001935}
1936
Jim Ingham151c0322015-09-15 21:13:50 +00001937TypeSystem *
Sean Callanana994b0b2015-10-02 18:40:30 +00001938Target::GetScratchTypeSystemForLanguage (Error *error, lldb::LanguageType language, bool create_on_demand)
Jim Ingham151c0322015-09-15 21:13:50 +00001939{
Greg Clayton5beec212015-10-08 21:04:34 +00001940 if (!m_valid)
1941 return nullptr;
1942
Sean Callanana994b0b2015-10-02 18:40:30 +00001943 if (error)
1944 {
1945 error->Clear();
1946 }
1947
Sean Callananb92bd752015-10-01 16:28:02 +00001948 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Sean Callanana994b0b2015-10-02 18:40:30 +00001949 || language == eLanguageTypeUnknown)
1950 {
Sean Callananc7b054a2015-10-09 18:01:10 +00001951 std::set<lldb::LanguageType> languages_for_types;
1952 std::set<lldb::LanguageType> languages_for_expressions;
1953
1954 Language::GetLanguagesSupportingTypeSystems(languages_for_types, languages_for_expressions);
1955
1956 if (languages_for_expressions.count(eLanguageTypeC))
1957 {
1958 language = eLanguageTypeC; // LLDB's default. Override by setting the target language.
1959 }
1960 else
1961 {
1962 if (languages_for_expressions.empty())
1963 {
1964 return nullptr;
1965 }
1966 else
1967 {
1968 language = *languages_for_expressions.begin();
1969 }
1970 }
Sean Callananb92bd752015-10-01 16:28:02 +00001971 }
Sean Callananb92bd752015-10-01 16:28:02 +00001972
Greg Clayton5beec212015-10-08 21:04:34 +00001973 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this, create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00001974}
1975
1976PersistentExpressionState *
1977Target::GetPersistentExpressionStateForLanguage (lldb::LanguageType language)
1978{
Sean Callanana994b0b2015-10-02 18:40:30 +00001979 TypeSystem *type_system = GetScratchTypeSystemForLanguage(nullptr, language, true);
Sean Callananb92bd752015-10-01 16:28:02 +00001980
1981 if (type_system)
1982 {
1983 return type_system->GetPersistentExpressionState();
1984 }
Jim Ingham151c0322015-09-15 21:13:50 +00001985 else
Sean Callananb92bd752015-10-01 16:28:02 +00001986 {
1987 return nullptr;
1988 }
Jim Ingham151c0322015-09-15 21:13:50 +00001989}
1990
1991UserExpression *
1992Target::GetUserExpressionForLanguage(const char *expr,
Sean Callanana994b0b2015-10-02 18:40:30 +00001993 const char *expr_prefix,
1994 lldb::LanguageType language,
1995 Expression::ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +00001996 const EvaluateExpressionOptions &options,
Sean Callanana994b0b2015-10-02 18:40:30 +00001997 Error &error)
Jim Ingham151c0322015-09-15 21:13:50 +00001998{
Sean Callanana994b0b2015-10-02 18:40:30 +00001999 Error type_system_error;
2000
2001 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002002 UserExpression *user_expr = nullptr;
2003
2004 if (!type_system)
2005 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002006 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 +00002007 return nullptr;
2008 }
2009
Jim Ingham19a63fc2015-11-03 02:11:24 +00002010 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00002011 if (!user_expr)
2012 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2013
2014 return user_expr;
2015}
2016
2017FunctionCaller *
2018Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
2019 const CompilerType &return_type,
2020 const Address& function_address,
2021 const ValueList &arg_value_list,
2022 const char *name,
2023 Error &error)
2024{
Sean Callanana994b0b2015-10-02 18:40:30 +00002025 Error type_system_error;
2026 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002027 FunctionCaller *persistent_fn = nullptr;
2028
2029 if (!type_system)
2030 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002031 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 +00002032 return persistent_fn;
2033 }
2034
2035 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
2036 if (!persistent_fn)
2037 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2038
2039 return persistent_fn;
2040}
2041
2042UtilityFunction *
2043Target::GetUtilityFunctionForLanguage (const char *text,
2044 lldb::LanguageType language,
2045 const char *name,
2046 Error &error)
2047{
Sean Callanana994b0b2015-10-02 18:40:30 +00002048 Error type_system_error;
2049 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002050 UtilityFunction *utility_fn = nullptr;
2051
2052 if (!type_system)
2053 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002054 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 +00002055 return utility_fn;
2056 }
2057
2058 utility_fn = type_system->GetUtilityFunction (text, name);
2059 if (!utility_fn)
2060 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2061
2062 return utility_fn;
2063}
2064
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002065ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00002066Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002067{
Greg Clayton5beec212015-10-08 21:04:34 +00002068 if (m_valid)
Sean Callanan4bf80d52011-11-15 22:27:19 +00002069 {
Greg Clayton5beec212015-10-08 21:04:34 +00002070 if (TypeSystem* type_system = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC, create_on_demand))
2071 return llvm::dyn_cast<ClangASTContext>(type_system);
Sean Callanan4bf80d52011-11-15 22:27:19 +00002072 }
Greg Clayton5beec212015-10-08 21:04:34 +00002073 return nullptr;
Sean Callananb92bd752015-10-01 16:28:02 +00002074}
2075
Sean Callanan686b2312011-11-16 18:20:47 +00002076ClangASTImporter *
2077Target::GetClangASTImporter()
2078{
Greg Clayton5beec212015-10-08 21:04:34 +00002079 if (m_valid)
Sean Callanan686b2312011-11-16 18:20:47 +00002080 {
Greg Clayton5beec212015-10-08 21:04:34 +00002081 ClangASTImporter *ast_importer = m_ast_importer_ap.get();
2082
2083 if (!ast_importer)
2084 {
2085 ast_importer = new ClangASTImporter();
2086 m_ast_importer_ap.reset(ast_importer);
2087 }
2088 return ast_importer;
Sean Callanan686b2312011-11-16 18:20:47 +00002089 }
Greg Clayton5beec212015-10-08 21:04:34 +00002090 return nullptr;
Sean Callanan686b2312011-11-16 18:20:47 +00002091}
2092
Greg Clayton99d0faf2010-11-18 23:32:35 +00002093void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002094Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002095{
Greg Clayton6920b522012-08-22 18:39:03 +00002096 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002097}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002098
Greg Clayton99d0faf2010-11-18 23:32:35 +00002099void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002100Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002101{
Greg Clayton6920b522012-08-22 18:39:03 +00002102 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002103}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002104
Greg Claytonc859e2d2012-02-13 23:10:39 +00002105FileSpecList
2106Target::GetDefaultExecutableSearchPaths ()
2107{
Greg Clayton67cc0632012-08-22 17:17:09 +00002108 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2109 if (properties_sp)
2110 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002111 return FileSpecList();
2112}
2113
Michael Sartaina7499c92013-07-01 19:45:50 +00002114FileSpecList
2115Target::GetDefaultDebugFileSearchPaths ()
2116{
2117 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2118 if (properties_sp)
2119 return properties_sp->GetDebugFileSearchPaths();
2120 return FileSpecList();
2121}
2122
Sean Callanan85054342015-04-03 15:39:47 +00002123FileSpecList
2124Target::GetDefaultClangModuleSearchPaths ()
2125{
2126 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2127 if (properties_sp)
2128 return properties_sp->GetClangModuleSearchPaths();
2129 return FileSpecList();
2130}
2131
Caroline Ticedaccaa92010-09-20 20:44:43 +00002132ArchSpec
2133Target::GetDefaultArchitecture ()
2134{
Greg Clayton67cc0632012-08-22 17:17:09 +00002135 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2136 if (properties_sp)
2137 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002138 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002139}
2140
2141void
Greg Clayton67cc0632012-08-22 17:17:09 +00002142Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002143{
Greg Clayton67cc0632012-08-22 17:17:09 +00002144 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2145 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002146 {
2147 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 +00002148 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002149 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002150}
2151
Greg Clayton0603aa92010-10-04 01:05:56 +00002152Target *
2153Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2154{
2155 // The target can either exist in the "process" of ExecutionContext, or in
2156 // the "target_sp" member of SymbolContext. This accessor helper function
2157 // will get the target from one of these locations.
2158
2159 Target *target = NULL;
2160 if (sc_ptr != NULL)
2161 target = sc_ptr->target_sp.get();
Greg Claytonc14ee322011-09-22 04:58:26 +00002162 if (target == NULL && exe_ctx_ptr)
2163 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002164 return target;
2165}
2166
Jim Ingham1624a2d2014-05-05 02:26:40 +00002167ExpressionResults
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002168Target::EvaluateExpression(const char *expr_cstr,
Jim Inghama309efe2015-10-28 22:23:17 +00002169 ExecutionContextScope *exe_scope,
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002170 lldb::ValueObjectSP &result_valobj_sp,
2171 const EvaluateExpressionOptions& options)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002172{
Enrico Granata97fca502012-09-18 17:43:16 +00002173 result_valobj_sp.reset();
2174
Jim Ingham8646d3c2014-05-05 02:47:44 +00002175 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002176
Greg Claytond1767f02011-12-08 02:13:16 +00002177 if (expr_cstr == NULL || expr_cstr[0] == '\0')
2178 return execution_results;
2179
Jim Ingham6026ca32011-05-12 02:06:14 +00002180 // We shouldn't run stop hooks in expressions.
2181 // Be sure to reset this if you return anywhere within this function.
2182 bool old_suppress_value = m_suppress_stop_hooks;
2183 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002184
2185 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002186
Jim Inghama309efe2015-10-28 22:23:17 +00002187 if (exe_scope)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002188 {
Jim Inghama309efe2015-10-28 22:23:17 +00002189 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002190 }
2191 else if (m_process_sp)
2192 {
2193 m_process_sp->CalculateExecutionContext(exe_ctx);
2194 }
2195 else
2196 {
2197 CalculateExecutionContext(exe_ctx);
2198 }
2199
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002200 // Make sure we aren't just trying to see the value of a persistent
2201 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002202 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002203 // Only check for persistent variables the expression starts with a '$'
2204 if (expr_cstr[0] == '$')
Sean Callanana994b0b2015-10-02 18:40:30 +00002205 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002206
2207 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002208 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002209 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002210 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002211 }
2212 else
2213 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002214 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002215 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002216 execution_results = UserExpression::Evaluate (exe_ctx,
2217 options,
2218 expr_cstr,
2219 prefix,
2220 result_valobj_sp,
2221 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002222 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002223
2224 m_suppress_stop_hooks = old_suppress_value;
2225
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002226 return execution_results;
2227}
2228
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002229lldb::ExpressionVariableSP
2230Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002231{
Greg Clayton5beec212015-10-08 21:04:34 +00002232 lldb::ExpressionVariableSP variable_sp;
2233 m_scratch_type_system_map.ForEach([this, name, &variable_sp](TypeSystem *type_system) -> bool
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002234 {
Greg Clayton5beec212015-10-08 21:04:34 +00002235 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002236 {
Greg Clayton5beec212015-10-08 21:04:34 +00002237 variable_sp = persistent_state->GetVariable(name);
2238
2239 if (variable_sp)
2240 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002241 }
Greg Clayton5beec212015-10-08 21:04:34 +00002242 return true; // Keep iterating the ForEach
2243 });
2244 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002245}
2246
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002247lldb::addr_t
Sean Callananb92bd752015-10-01 16:28:02 +00002248Target::GetPersistentSymbol(const ConstString &name)
2249{
Greg Clayton5beec212015-10-08 21:04:34 +00002250 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Sean Callananb92bd752015-10-01 16:28:02 +00002251
Greg Clayton5beec212015-10-08 21:04:34 +00002252 m_scratch_type_system_map.ForEach([this, name, &address](TypeSystem *type_system) -> bool
Sean Callananb92bd752015-10-01 16:28:02 +00002253 {
Greg Clayton5beec212015-10-08 21:04:34 +00002254 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callananb92bd752015-10-01 16:28:02 +00002255 {
Greg Clayton5beec212015-10-08 21:04:34 +00002256 address = persistent_state->LookupSymbol(name);
2257 if (address != LLDB_INVALID_ADDRESS)
2258 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002259 }
Greg Clayton5beec212015-10-08 21:04:34 +00002260 return true; // Keep iterating the ForEach
2261 });
2262 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002263}
2264
2265lldb::addr_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002266Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2267{
2268 addr_t code_addr = load_addr;
2269 switch (m_arch.GetMachine())
2270 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002271 case llvm::Triple::mips:
2272 case llvm::Triple::mipsel:
2273 case llvm::Triple::mips64:
2274 case llvm::Triple::mips64el:
2275 switch (addr_class)
2276 {
2277 case eAddressClassData:
2278 case eAddressClassDebug:
2279 return LLDB_INVALID_ADDRESS;
2280
2281 case eAddressClassUnknown:
2282 case eAddressClassInvalid:
2283 case eAddressClassCode:
2284 case eAddressClassCodeAlternateISA:
2285 case eAddressClassRuntime:
2286 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2287 code_addr |= 1ull;
2288 break;
2289 }
2290 break;
2291
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002292 case llvm::Triple::arm:
2293 case llvm::Triple::thumb:
2294 switch (addr_class)
2295 {
2296 case eAddressClassData:
2297 case eAddressClassDebug:
2298 return LLDB_INVALID_ADDRESS;
2299
2300 case eAddressClassUnknown:
2301 case eAddressClassInvalid:
2302 case eAddressClassCode:
2303 case eAddressClassCodeAlternateISA:
2304 case eAddressClassRuntime:
2305 // Check if bit zero it no set?
2306 if ((code_addr & 1ull) == 0)
2307 {
2308 // Bit zero isn't set, check if the address is a multiple of 2?
2309 if (code_addr & 2ull)
2310 {
2311 // The address is a multiple of 2 so it must be thumb, set bit zero
2312 code_addr |= 1ull;
2313 }
2314 else if (addr_class == eAddressClassCodeAlternateISA)
2315 {
2316 // We checked the address and the address claims to be the alternate ISA
2317 // which means thumb, so set bit zero.
2318 code_addr |= 1ull;
2319 }
2320 }
2321 break;
2322 }
2323 break;
2324
2325 default:
2326 break;
2327 }
2328 return code_addr;
2329}
2330
2331lldb::addr_t
2332Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2333{
2334 addr_t opcode_addr = load_addr;
2335 switch (m_arch.GetMachine())
2336 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002337 case llvm::Triple::mips:
2338 case llvm::Triple::mipsel:
2339 case llvm::Triple::mips64:
2340 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002341 case llvm::Triple::arm:
2342 case llvm::Triple::thumb:
2343 switch (addr_class)
2344 {
2345 case eAddressClassData:
2346 case eAddressClassDebug:
2347 return LLDB_INVALID_ADDRESS;
2348
2349 case eAddressClassInvalid:
2350 case eAddressClassUnknown:
2351 case eAddressClassCode:
2352 case eAddressClassCodeAlternateISA:
2353 case eAddressClassRuntime:
2354 opcode_addr &= ~(1ull);
2355 break;
2356 }
2357 break;
2358
2359 default:
2360 break;
2361 }
2362 return opcode_addr;
2363}
2364
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002365lldb::addr_t
2366Target::GetBreakableLoadAddress (lldb::addr_t addr)
2367{
2368 addr_t breakable_addr = addr;
2369 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2370
2371 switch (m_arch.GetMachine())
2372 {
2373 default:
2374 break;
2375 case llvm::Triple::mips:
2376 case llvm::Triple::mipsel:
2377 case llvm::Triple::mips64:
2378 case llvm::Triple::mips64el:
2379 {
2380 addr_t function_start = 0;
2381 addr_t current_offset = 0;
2382 uint32_t loop_count = 0;
2383 Address resolved_addr;
2384 uint32_t arch_flags = m_arch.GetFlags ();
2385 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2386 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2387 SectionLoadList &section_load_list = GetSectionLoadList();
2388
2389 if (section_load_list.IsEmpty())
2390 // No sections are loaded, so we must assume we are not running yet
2391 // and need to operate only on file address.
2392 m_images.ResolveFileAddress (addr, resolved_addr);
2393 else
2394 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2395
2396 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2397 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2398 if (temp_addr_module_sp)
2399 {
2400 SymbolContext sc;
2401 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002402 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002403 if (sc.function)
2404 {
2405 function_start = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(this);
2406 if (function_start == LLDB_INVALID_ADDRESS)
2407 function_start = sc.function->GetAddressRange().GetBaseAddress().GetFileAddress();
2408 }
2409 else if (sc.symbol)
2410 {
2411 Address sym_addr = sc.symbol->GetAddress();
2412 function_start = sym_addr.GetFileAddress();
2413 }
2414 current_offset = addr - function_start;
2415 }
2416
2417 // If breakpoint address is start of function then we dont have to do anything.
2418 if (current_offset == 0)
2419 return breakable_addr;
2420 else
2421 loop_count = current_offset / 2;
2422
2423 if (loop_count > 3)
2424 {
2425 // Scan previous 6 bytes
2426 if (IsMips16 | IsMicromips)
2427 loop_count = 3;
2428 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2429 else
2430 loop_count = 2;
2431 }
2432
2433 // Create Disassembler Instance
2434 lldb::DisassemblerSP disasm_sp (Disassembler::FindPlugin(m_arch, NULL, NULL));
2435
2436 ExecutionContext exe_ctx;
2437 CalculateExecutionContext(exe_ctx);
2438 InstructionList instruction_list;
2439 InstructionSP prev_insn;
2440 bool prefer_file_cache = true; // Read from file
2441 uint32_t inst_to_choose = 0;
2442
2443 for (uint32_t i = 1; i <= loop_count; i++)
2444 {
2445 // Adjust the address to read from.
2446 resolved_addr.Slide (-2);
2447 AddressRange range(resolved_addr, i*2);
2448 uint32_t insn_size = 0;
2449
Greg Clayton6071e6f2015-08-26 22:57:51 +00002450 disasm_sp->ParseInstructions (&exe_ctx, range, NULL, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002451
2452 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2453 if (num_insns)
2454 {
2455 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2456 insn_size = prev_insn->GetOpcode().GetByteSize();
2457 if (i == 1 && insn_size == 2)
2458 {
2459 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2460 instruction_list.Append(prev_insn);
2461 inst_to_choose = 1;
2462 }
2463 else if (i == 2)
2464 {
2465 // Here we may get one 4-byte instruction or two 2-byte instructions.
2466 if (num_insns == 2)
2467 {
2468 // Looks like there are two 2-byte instructions above our breakpoint target address.
2469 // 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.
2470 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2471 // and whatever i=1 iteration has found out is true.
2472 inst_to_choose = 1;
2473 break;
2474 }
2475 else if (insn_size == 4)
2476 {
2477 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2478 // Lets try scanning upper 2 bytes to verify this.
2479 instruction_list.Append(prev_insn);
2480 inst_to_choose = 2;
2481 }
2482 }
2483 else if (i == 3)
2484 {
2485 if (insn_size == 4)
2486 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2487 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2488 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2489 inst_to_choose = 0;
2490 else
2491 // This is straight-forward
2492 inst_to_choose = 2;
2493 break;
2494 }
2495 }
2496 else
2497 {
2498 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2499 if (i > 1)
2500 {
2501 inst_to_choose = i - 1;
2502 break;
2503 }
2504 }
2505 }
2506
2507 // Check if we are able to find any valid instruction.
2508 if (inst_to_choose)
2509 {
2510 if (inst_to_choose > instruction_list.GetSize())
2511 inst_to_choose--;
2512 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2513
2514 if (prev_insn->HasDelaySlot())
2515 {
2516 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2517 // Adjust the breakable address
2518 breakable_addr = addr - shift_size;
2519 if (log)
2520 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);
2521 }
2522 }
2523 break;
2524 }
2525 }
2526 return breakable_addr;
2527}
2528
Greg Clayton9585fbf2013-03-19 00:20:55 +00002529SourceManager &
2530Target::GetSourceManager ()
2531{
2532 if (m_source_manager_ap.get() == NULL)
2533 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2534 return *m_source_manager_ap;
2535}
2536
Sean Callanan9998acd2014-12-05 01:21:59 +00002537ClangModulesDeclVendor *
2538Target::GetClangModulesDeclVendor ()
2539{
2540 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2541
2542 {
2543 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2544
2545 if (!m_clang_modules_decl_vendor_ap)
2546 {
2547 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2548 }
2549 }
2550
2551 return m_clang_modules_decl_vendor_ap.get();
2552}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002553
Greg Clayton44d93782014-01-27 23:43:24 +00002554Target::StopHookSP
2555Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002556{
2557 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002558 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2559 m_stop_hooks[new_uid] = stop_hook_sp;
2560 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002561}
2562
2563bool
2564Target::RemoveStopHookByID (lldb::user_id_t user_id)
2565{
2566 size_t num_removed;
2567 num_removed = m_stop_hooks.erase (user_id);
2568 if (num_removed == 0)
2569 return false;
2570 else
2571 return true;
2572}
2573
2574void
2575Target::RemoveAllStopHooks ()
2576{
2577 m_stop_hooks.clear();
2578}
2579
2580Target::StopHookSP
2581Target::GetStopHookByID (lldb::user_id_t user_id)
2582{
2583 StopHookSP found_hook;
2584
2585 StopHookCollection::iterator specified_hook_iter;
2586 specified_hook_iter = m_stop_hooks.find (user_id);
2587 if (specified_hook_iter != m_stop_hooks.end())
2588 found_hook = (*specified_hook_iter).second;
2589 return found_hook;
2590}
2591
2592bool
2593Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2594{
2595 StopHookCollection::iterator specified_hook_iter;
2596 specified_hook_iter = m_stop_hooks.find (user_id);
2597 if (specified_hook_iter == m_stop_hooks.end())
2598 return false;
2599
2600 (*specified_hook_iter).second->SetIsActive (active_state);
2601 return true;
2602}
2603
2604void
2605Target::SetAllStopHooksActiveState (bool active_state)
2606{
2607 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2608 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2609 {
2610 (*pos).second->SetIsActive (active_state);
2611 }
2612}
2613
2614void
2615Target::RunStopHooks ()
2616{
Jim Ingham6026ca32011-05-12 02:06:14 +00002617 if (m_suppress_stop_hooks)
2618 return;
2619
Jim Ingham9575d842011-03-11 03:53:59 +00002620 if (!m_process_sp)
2621 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002622
2623 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2624 // since in that case we do not want to run the stop-hooks
2625 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2626 return;
2627
Jim Ingham9575d842011-03-11 03:53:59 +00002628 if (m_stop_hooks.empty())
2629 return;
2630
2631 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2632
2633 // If there aren't any active stop hooks, don't bother either:
2634 bool any_active_hooks = false;
2635 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2636 {
2637 if ((*pos).second->IsActive())
2638 {
2639 any_active_hooks = true;
2640 break;
2641 }
2642 }
2643 if (!any_active_hooks)
2644 return;
2645
2646 CommandReturnObject result;
2647
2648 std::vector<ExecutionContext> exc_ctx_with_reasons;
2649 std::vector<SymbolContext> sym_ctx_with_reasons;
2650
2651 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2652 size_t num_threads = cur_threadlist.GetSize();
2653 for (size_t i = 0; i < num_threads; i++)
2654 {
2655 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2656 if (cur_thread_sp->ThreadStoppedForAReason())
2657 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002658 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002659 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2660 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2661 }
2662 }
2663
2664 // If no threads stopped for a reason, don't run the stop-hooks.
2665 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2666 if (num_exe_ctx == 0)
2667 return;
2668
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002669 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2670 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002671
2672 bool keep_going = true;
2673 bool hooks_ran = false;
Jim Ingham381e25b2011-03-22 01:47:27 +00002674 bool print_hook_header;
2675 bool print_thread_header;
2676
2677 if (num_exe_ctx == 1)
2678 print_thread_header = false;
2679 else
2680 print_thread_header = true;
2681
2682 if (m_stop_hooks.size() == 1)
2683 print_hook_header = false;
2684 else
2685 print_hook_header = true;
2686
Jim Ingham9575d842011-03-11 03:53:59 +00002687 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2688 {
2689 // result.Clear();
2690 StopHookSP cur_hook_sp = (*pos).second;
2691 if (!cur_hook_sp->IsActive())
2692 continue;
2693
2694 bool any_thread_matched = false;
2695 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2696 {
2697 if ((cur_hook_sp->GetSpecifier () == NULL
2698 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
2699 && (cur_hook_sp->GetThreadSpecifier() == NULL
Jim Ingham3d902922012-03-07 22:03:04 +00002700 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002701 {
2702 if (!hooks_ran)
2703 {
Jim Ingham9575d842011-03-11 03:53:59 +00002704 hooks_ran = true;
2705 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002706 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002707 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002708 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2709 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
2710 NULL);
2711 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002712 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002713 else
Daniel Malead01b2952012-11-29 21:49:15 +00002714 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002715 any_thread_matched = true;
2716 }
2717
Jim Ingham381e25b2011-03-22 01:47:27 +00002718 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002719 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002720
2721 CommandInterpreterRunOptions options;
2722 options.SetStopOnContinue (true);
2723 options.SetStopOnError (true);
2724 options.SetEchoCommands (false);
2725 options.SetPrintResults (true);
2726 options.SetAddToHistory (false);
2727
2728 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2729 &exc_ctx_with_reasons[i],
2730 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002731 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002732
2733 // If the command started the target going again, we should bag out of
2734 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002735 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2736 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002737 {
Daniel Malead01b2952012-11-29 21:49:15 +00002738 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002739 keep_going = false;
2740 }
2741 }
2742 }
2743 }
Jason Molenda879cf772011-09-23 00:42:55 +00002744
Caroline Tice969ed3d2011-05-02 20:41:46 +00002745 result.GetImmediateOutputStream()->Flush();
2746 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002747}
2748
Greg Claytonfbb76342013-11-20 21:07:01 +00002749const TargetPropertiesSP &
2750Target::GetGlobalProperties()
2751{
2752 static TargetPropertiesSP g_settings_sp;
2753 if (!g_settings_sp)
2754 {
2755 g_settings_sp.reset (new TargetProperties (NULL));
2756 }
2757 return g_settings_sp;
2758}
2759
2760Error
2761Target::Install (ProcessLaunchInfo *launch_info)
2762{
2763 Error error;
2764 PlatformSP platform_sp (GetPlatform());
2765 if (platform_sp)
2766 {
2767 if (platform_sp->IsRemote())
2768 {
2769 if (platform_sp->IsConnected())
2770 {
2771 // Install all files that have an install path, and always install the
2772 // main executable when connected to a remote platform
2773 const ModuleList& modules = GetImages();
2774 const size_t num_images = modules.GetSize();
2775 for (size_t idx = 0; idx < num_images; ++idx)
2776 {
2777 const bool is_main_executable = idx == 0;
2778 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2779 if (module_sp)
2780 {
2781 FileSpec local_file (module_sp->GetFileSpec());
2782 if (local_file)
2783 {
2784 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2785 if (!remote_file)
2786 {
2787 if (is_main_executable) // TODO: add setting for always installing main executable???
2788 {
2789 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002790 remote_file = platform_sp->GetRemoteWorkingDirectory();
2791 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002792 }
2793 }
2794 if (remote_file)
2795 {
2796 error = platform_sp->Install(local_file, remote_file);
2797 if (error.Success())
2798 {
2799 module_sp->SetPlatformFileSpec(remote_file);
2800 if (is_main_executable)
2801 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002802 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002803 if (launch_info)
2804 launch_info->SetExecutableFile(remote_file, false);
2805 }
2806 }
2807 else
2808 break;
2809 }
2810 }
2811 }
2812 }
2813 }
2814 }
2815 }
2816 return error;
2817}
Greg Clayton7b242382011-07-08 00:48:09 +00002818
Greg Claytond5944cd2013-12-06 01:12:00 +00002819bool
2820Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2821{
2822 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2823}
2824
2825bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002826Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2827{
2828 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2829}
2830
2831bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002832Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2833{
2834 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2835 if (old_section_load_addr != new_section_load_addr)
2836 {
2837 uint32_t stop_id = 0;
2838 ProcessSP process_sp(GetProcessSP());
2839 if (process_sp)
2840 stop_id = process_sp->GetStopID();
2841 else
2842 stop_id = m_section_load_history.GetLastStopID();
2843 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2844 return true; // Return true if the section load address was changed...
2845 }
2846 return false; // Return false to indicate nothing changed
Greg Claytond5944cd2013-12-06 01:12:00 +00002847}
2848
Greg Clayton8012cad2014-11-17 19:39:20 +00002849size_t
2850Target::UnloadModuleSections (const ModuleList &module_list)
2851{
2852 size_t section_unload_count = 0;
2853 size_t num_modules = module_list.GetSize();
2854 for (size_t i=0; i<num_modules; ++i)
2855 {
2856 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2857 }
2858 return section_unload_count;
2859}
2860
2861size_t
2862Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2863{
2864 uint32_t stop_id = 0;
2865 ProcessSP process_sp(GetProcessSP());
2866 if (process_sp)
2867 stop_id = process_sp->GetStopID();
2868 else
2869 stop_id = m_section_load_history.GetLastStopID();
2870 SectionList *sections = module_sp->GetSectionList();
2871 size_t section_unload_count = 0;
2872 if (sections)
2873 {
2874 const uint32_t num_sections = sections->GetNumSections(0);
2875 for (uint32_t i = 0; i < num_sections; ++i)
2876 {
2877 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2878 }
2879 }
2880 return section_unload_count;
2881}
2882
Greg Claytond5944cd2013-12-06 01:12:00 +00002883bool
2884Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2885{
2886 uint32_t stop_id = 0;
2887 ProcessSP process_sp(GetProcessSP());
2888 if (process_sp)
2889 stop_id = process_sp->GetStopID();
2890 else
2891 stop_id = m_section_load_history.GetLastStopID();
2892 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2893}
2894
2895bool
2896Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2897{
2898 uint32_t stop_id = 0;
2899 ProcessSP process_sp(GetProcessSP());
2900 if (process_sp)
2901 stop_id = process_sp->GetStopID();
2902 else
2903 stop_id = m_section_load_history.GetLastStopID();
2904 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2905}
2906
2907void
2908Target::ClearAllLoadedSections ()
2909{
2910 m_section_load_history.Clear();
2911}
2912
Greg Claytonb09c5382013-12-13 17:20:18 +00002913Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002914Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002915{
2916 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002917 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2918
2919 if (log)
2920 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2921
Greg Claytonb09c5382013-12-13 17:20:18 +00002922 StateType state = eStateInvalid;
2923
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002924 // Scope to temporarily get the process state in case someone has manually
2925 // remotely connected already to a process and we can skip the platform
2926 // launching.
2927 {
2928 ProcessSP process_sp (GetProcessSP());
2929
2930 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002931 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002932 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002933 if (log)
2934 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2935 }
2936 else
2937 {
2938 if (log)
2939 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2940 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002941 }
2942
Greg Claytonb09c5382013-12-13 17:20:18 +00002943 launch_info.GetFlags().Set (eLaunchFlagDebug);
2944
2945 // Get the value of synchronous execution here. If you wait till after you have started to
2946 // run, then you could have hit a breakpoint, whose command might switch the value, and
2947 // then you'll pick up that incorrect value.
2948 Debugger &debugger = GetDebugger();
2949 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2950
2951 PlatformSP platform_sp (GetPlatform());
2952
2953 // Finalize the file actions, and if none were given, default to opening
2954 // up a pseudo terminal
2955 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002956 if (log)
2957 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2958 __FUNCTION__,
2959 platform_sp ? "true" : "false",
2960 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2961 default_to_use_pty ? "true" : "false");
2962
Greg Claytonb09c5382013-12-13 17:20:18 +00002963 launch_info.FinalizeFileActions (this, default_to_use_pty);
2964
2965 if (state == eStateConnected)
2966 {
2967 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2968 {
2969 error.SetErrorString("can't launch in tty when launching through a remote connection");
2970 return error;
2971 }
2972 }
2973
2974 if (!launch_info.GetArchitecture().IsValid())
2975 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00002976
2977 // 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 +00002978 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
2979 {
Todd Fialaac33cc92014-10-09 01:02:08 +00002980 if (log)
2981 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
2982
Greg Clayton5df78fa2015-05-23 03:54:53 +00002983 // Get a weak pointer to the previous process if we have one
2984 ProcessWP process_wp;
2985 if (m_process_sp)
2986 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00002987 m_process_sp = GetPlatform()->DebugProcess (launch_info,
2988 debugger,
2989 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00002990 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00002991
2992 // Cleanup the old process since someone might still have a strong
2993 // reference to this process and we would like to allow it to cleanup
2994 // as much as it can without the object being destroyed. We try to
2995 // lock the shared pointer and if that works, then someone else still
2996 // has a strong reference to the process.
2997
2998 ProcessSP old_process_sp(process_wp.lock());
2999 if (old_process_sp)
3000 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00003001 }
3002 else
3003 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003004 if (log)
3005 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
3006
Greg Claytonb09c5382013-12-13 17:20:18 +00003007 if (state == eStateConnected)
3008 {
3009 assert(m_process_sp);
3010 }
3011 else
3012 {
Todd Fiala015d8182014-07-22 23:41:36 +00003013 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00003014 const char *plugin_name = launch_info.GetProcessPluginName();
Greg Clayton8012cad2014-11-17 19:39:20 +00003015 CreateProcess (launch_info.GetListenerForProcess(debugger), plugin_name, NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00003016 }
Todd Fiala015d8182014-07-22 23:41:36 +00003017
3018 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00003019 if (m_process_sp)
3020 error = m_process_sp->Launch (launch_info);
3021 }
3022
3023 if (!m_process_sp)
3024 {
3025 if (error.Success())
3026 error.SetErrorString("failed to launch or debug process");
3027 return error;
3028 }
3029
3030 if (error.Success())
3031 {
Ilia K064e69f2015-03-23 21:16:25 +00003032 if (synchronous_execution || launch_info.GetFlags().Test(eLaunchFlagStopAtEntry) == false)
Greg Claytonb09c5382013-12-13 17:20:18 +00003033 {
Greg Clayton44d93782014-01-27 23:43:24 +00003034 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00003035 if (!hijack_listener_sp)
3036 {
3037 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
3038 launch_info.SetHijackListener(hijack_listener_sp);
3039 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
3040 }
Todd Fialaac33cc92014-10-09 01:02:08 +00003041
Ilia K38810f42015-05-20 10:15:47 +00003042 StateType state = m_process_sp->WaitForProcessToStop (NULL, NULL, false, hijack_listener_sp.get(), NULL);
Greg Claytonb09c5382013-12-13 17:20:18 +00003043
3044 if (state == eStateStopped)
3045 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003046 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003047 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003048 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00003049 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003050 error = m_process_sp->PrivateResume();
3051 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00003052 {
Ilia K064e69f2015-03-23 21:16:25 +00003053 state = m_process_sp->WaitForProcessToStop (NULL, NULL, true, hijack_listener_sp.get(), stream);
3054 const bool must_be_alive = false; // eStateExited is ok, so this must be false
3055 if (!StateIsStoppedState(state, must_be_alive))
3056 {
3057 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
3058 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003059 }
3060 }
Ilia K064e69f2015-03-23 21:16:25 +00003061 else
3062 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003063 m_process_sp->RestoreProcessEvents();
3064 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00003065 }
3066 if (!error.Success())
3067 {
Ilia K064e69f2015-03-23 21:16:25 +00003068 Error error2;
3069 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
3070 error = error2;
3071 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003072 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003073 }
Greg Clayton40286e02014-04-30 20:29:09 +00003074 else if (state == eStateExited)
3075 {
Zachary Turner10687b02014-10-20 17:46:43 +00003076 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00003077 const int exit_status = m_process_sp->GetExitStatus();
3078 const char *exit_desc = m_process_sp->GetExitDescription();
3079#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'."
3080 if (exit_desc && exit_desc[0])
3081 {
3082 if (with_shell)
3083 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
3084 else
3085 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
3086 }
3087 else
3088 {
3089 if (with_shell)
3090 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
3091 else
3092 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
3093 }
3094 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003095 else
3096 {
3097 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
3098 }
3099 }
Greg Clayton44d93782014-01-27 23:43:24 +00003100 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00003101 }
3102 else
3103 {
Greg Clayton44d93782014-01-27 23:43:24 +00003104 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003105 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003106 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003107 }
3108 return error;
3109}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003110
3111Error
3112Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3113{
3114 auto state = eStateInvalid;
3115 auto process_sp = GetProcessSP ();
3116 if (process_sp)
3117 {
3118 state = process_sp->GetState ();
3119 if (process_sp->IsAlive () && state != eStateConnected)
3120 {
3121 if (state == eStateAttaching)
3122 return Error ("process attach is in progress");
3123 return Error ("a process is already being debugged");
3124 }
3125 }
3126
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003127 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3128
3129 // If no process info was specified, then use the target executable
3130 // name as the process to attach to by default
3131 if (!attach_info.ProcessInfoSpecified ())
3132 {
3133 if (old_exec_module_sp)
3134 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3135
3136 if (!attach_info.ProcessInfoSpecified ())
3137 {
3138 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3139 }
3140 }
3141
3142 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
Greg Claytonb3788ea2015-10-05 22:58:37 +00003143 ListenerSP hijack_listener_sp;
3144 const bool async = attach_info.GetAsync();
3145 if (async == false)
3146 {
3147 hijack_listener_sp.reset (new Listener ("lldb.Target.Attach.attach.hijack"));
3148 attach_info.SetHijackListener (hijack_listener_sp);
3149 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003150
3151 Error error;
3152 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3153 {
3154 SetPlatform (platform_sp);
3155 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3156 }
3157 else
3158 {
3159 if (state != eStateConnected)
3160 {
3161 const char *plugin_name = attach_info.GetProcessPluginName ();
3162 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3163 if (process_sp == nullptr)
3164 {
3165 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3166 return error;
3167 }
3168 }
Greg Claytonb3788ea2015-10-05 22:58:37 +00003169 if (hijack_listener_sp)
3170 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003171 error = process_sp->Attach (attach_info);
3172 }
3173
Greg Claytone75e5d82015-10-20 00:14:20 +00003174 if (error.Success () && process_sp)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003175 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003176 if (async)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003177 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003178 process_sp->RestoreProcessEvents ();
3179 }
3180 else
3181 {
3182 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
3183 process_sp->RestoreProcessEvents ();
3184
3185 if (state != eStateStopped)
3186 {
3187 const char *exit_desc = process_sp->GetExitDescription ();
3188 if (exit_desc)
3189 error.SetErrorStringWithFormat ("%s", exit_desc);
3190 else
3191 error.SetErrorString ("process did not stop (no such process or permission problem?)");
3192 process_sp->Destroy (false);
3193 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003194 }
3195 }
3196 return error;
3197}
3198
Jim Ingham9575d842011-03-11 03:53:59 +00003199//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003200// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003201//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003202Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3203 UserID (uid),
3204 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003205 m_commands (),
3206 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003207 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003208 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003209{
3210}
3211
3212Target::StopHook::StopHook (const StopHook &rhs) :
3213 UserID (rhs.GetID()),
3214 m_target_sp (rhs.m_target_sp),
3215 m_commands (rhs.m_commands),
3216 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003217 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003218 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003219{
3220 if (rhs.m_thread_spec_ap.get() != NULL)
3221 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3222}
3223
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003224Target::StopHook::~StopHook() = default;
Jim Ingham9575d842011-03-11 03:53:59 +00003225
3226void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003227Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3228{
3229 m_specifier_sp.reset(specifier);
3230}
3231
3232void
Jim Ingham9575d842011-03-11 03:53:59 +00003233Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3234{
3235 m_thread_spec_ap.reset (specifier);
3236}
Jim Ingham9575d842011-03-11 03:53:59 +00003237
3238void
3239Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3240{
3241 int indent_level = s->GetIndentLevel();
3242
3243 s->SetIndentLevel(indent_level + 2);
3244
Daniel Malead01b2952012-11-29 21:49:15 +00003245 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003246 if (m_active)
3247 s->Indent ("State: enabled\n");
3248 else
3249 s->Indent ("State: disabled\n");
3250
3251 if (m_specifier_sp)
3252 {
3253 s->Indent();
3254 s->PutCString ("Specifier:\n");
3255 s->SetIndentLevel (indent_level + 4);
3256 m_specifier_sp->GetDescription (s, level);
3257 s->SetIndentLevel (indent_level + 2);
3258 }
3259
3260 if (m_thread_spec_ap.get() != NULL)
3261 {
3262 StreamString tmp;
3263 s->Indent("Thread:\n");
3264 m_thread_spec_ap->GetDescription (&tmp, level);
3265 s->SetIndentLevel (indent_level + 4);
3266 s->Indent (tmp.GetData());
3267 s->PutCString ("\n");
3268 s->SetIndentLevel (indent_level + 2);
3269 }
3270
3271 s->Indent ("Commands: \n");
3272 s->SetIndentLevel (indent_level + 4);
3273 uint32_t num_commands = m_commands.GetSize();
3274 for (uint32_t i = 0; i < num_commands; i++)
3275 {
3276 s->Indent(m_commands.GetStringAtIndex(i));
3277 s->PutCString ("\n");
3278 }
3279 s->SetIndentLevel (indent_level);
3280}
3281
Greg Clayton67cc0632012-08-22 17:17:09 +00003282//--------------------------------------------------------------
3283// class TargetProperties
3284//--------------------------------------------------------------
3285
3286OptionEnumValueElement
3287lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003288{
Greg Clayton67cc0632012-08-22 17:17:09 +00003289 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3290 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3291 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
3292 { 0, NULL, NULL }
3293};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003294
Greg Clayton1f746072012-08-29 21:13:06 +00003295static OptionEnumValueElement
3296g_inline_breakpoint_enums[] =
3297{
3298 { 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."},
3299 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3300 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
3301 { 0, NULL, NULL }
3302};
3303
Jim Ingham0f063ba2013-03-02 00:26:47 +00003304typedef enum x86DisassemblyFlavor
3305{
3306 eX86DisFlavorDefault,
3307 eX86DisFlavorIntel,
3308 eX86DisFlavorATT
3309} x86DisassemblyFlavor;
3310
3311static OptionEnumValueElement
3312g_x86_dis_flavor_value_types[] =
3313{
3314 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3315 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3316 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
3317 { 0, NULL, NULL }
3318};
3319
Enrico Granata397ddd52013-05-21 20:13:34 +00003320static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003321g_hex_immediate_style_values[] =
3322{
3323 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3324 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
3325 { 0, NULL, NULL }
3326};
3327
3328static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003329g_load_script_from_sym_file_values[] =
3330{
3331 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3332 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3333 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
3334 { 0, NULL, NULL }
3335};
3336
Greg Claytonfd814c52013-08-13 01:42:25 +00003337static OptionEnumValueElement
3338g_memory_module_load_level_values[] =
3339{
Greg Clayton86eac942013-08-13 21:32:34 +00003340 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003341 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3342 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
3343 { 0, NULL, NULL }
3344};
3345
Greg Clayton67cc0632012-08-22 17:17:09 +00003346static PropertyDefinition
3347g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003348{
Greg Clayton67cc0632012-08-22 17:17:09 +00003349 { "default-arch" , OptionValue::eTypeArch , true , 0 , NULL, NULL, "Default architecture to choose, when there's a choice." },
Ilia K055ad9b2015-05-18 13:41:01 +00003350 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Move breakpoints to nearest code." },
Dawn Perchike0c96ac2015-07-30 22:10:08 +00003351 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , NULL, NULL, "The language to use when interpreting expressions entered in commands." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003352 { "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 +00003353 { "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 +00003354 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Should synthetic values be used by default whenever available." },
3355 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Skip function prologues when setting breakpoints by name." },
Jim Ingham0ca778e2015-09-15 18:03:00 +00003356 { "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 +00003357 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3358 "some part (starting at the root) of the path to the file when it was built, "
3359 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3360 "Each element of the array is checked in order and the first one that results in a match wins." },
3361 { "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 +00003362 { "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 +00003363 { "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 +00003364 { "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 +00003365 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , NULL, NULL, "Maximum number of children to expand in any level of depth." },
3366 { "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 +00003367 { "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 +00003368 { "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 +00003369 { "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." },
3370 { "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 +00003371 { "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." },
3372 { "inherit-env" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Inherit the environment from the process that is running LLDB." },
3373 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for reading its standard input." },
3374 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , NULL, NULL, "The file/path to be used by the executable program for writing its standard output." },
3375 { "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 +00003376 { "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 +00003377 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , NULL, NULL, "Disable Address Space Layout Randomization (ASLR)" },
3378 { "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 +00003379 { "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 +00003380 "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 +00003381 "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 +00003382 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003383 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3384 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3385 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003386 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003387 // 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.
3388 { "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 +00003389 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, NULL, NULL, "Show immediates in disassembly as hexadecimal." },
3390 { "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 +00003391 { "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 +00003392 { "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 +00003393 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, NULL, g_memory_module_load_level_values,
3394 "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. "
3395 "This setting helps users control how much information gets loaded when loading modules from memory."
3396 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3397 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3398 "'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 +00003399 { "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 +00003400 { "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 +00003401 { "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 +00003402 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, NULL, NULL, "Disable lock-step debugging, instead control threads independently." },
Greg Clayton67cc0632012-08-22 17:17:09 +00003403 { NULL , OptionValue::eTypeInvalid , false, 0 , NULL, NULL, NULL }
3404};
Enrico Granata560558e2015-02-11 02:35:39 +00003405
Greg Clayton67cc0632012-08-22 17:17:09 +00003406enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003407{
Greg Clayton67cc0632012-08-22 17:17:09 +00003408 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003409 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003410 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003411 ePropertyExprPrefix,
3412 ePropertyPreferDynamic,
3413 ePropertyEnableSynthetic,
3414 ePropertySkipPrologue,
3415 ePropertySourceMap,
3416 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003417 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003418 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003419 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003420 ePropertyMaxChildrenCount,
3421 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003422 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003423 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003424 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003425 ePropertyRunArgs,
3426 ePropertyEnvVars,
3427 ePropertyInheritEnv,
3428 ePropertyInputPath,
3429 ePropertyOutputPath,
3430 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003431 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003432 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003433 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003434 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003435 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003436 ePropertyUseHexImmediates,
3437 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003438 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003439 ePropertyLoadScriptFromSymbolFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003440 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003441 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003442 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003443 ePropertyDisplayRuntimeSupportValues,
3444 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003445};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003446
Greg Clayton67cc0632012-08-22 17:17:09 +00003447class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003448{
Greg Clayton67cc0632012-08-22 17:17:09 +00003449public:
3450 TargetOptionValueProperties (const ConstString &name) :
3451 OptionValueProperties (name),
3452 m_target (NULL),
3453 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003454 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003455 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003456
Greg Clayton67cc0632012-08-22 17:17:09 +00003457 // This constructor is used when creating TargetOptionValueProperties when it
3458 // is part of a new lldb_private::Target instance. It will copy all current
3459 // global property values as needed
3460 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3461 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3462 m_target (target),
3463 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003464 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003465 }
3466
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003467 const Property *
3468 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +00003469 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003470 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003471 // try and grab the setting from the current target if there is one. Else we just
3472 // use the one from this instance.
3473 if (idx == ePropertyEnvVars)
3474 GetHostEnvironmentIfNeeded ();
3475
3476 if (exe_ctx)
3477 {
3478 Target *target = exe_ctx->GetTargetPtr();
3479 if (target)
3480 {
3481 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3482 if (this != target_properties)
3483 return target_properties->ProtectedGetPropertyAtIndex (idx);
3484 }
3485 }
3486 return ProtectedGetPropertyAtIndex (idx);
3487 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003488
3489 lldb::TargetSP
3490 GetTargetSP ()
3491 {
3492 return m_target->shared_from_this();
3493 }
3494
Greg Clayton67cc0632012-08-22 17:17:09 +00003495protected:
Greg Clayton67cc0632012-08-22 17:17:09 +00003496 void
3497 GetHostEnvironmentIfNeeded () const
3498 {
3499 if (!m_got_host_env)
3500 {
3501 if (m_target)
3502 {
3503 m_got_host_env = true;
3504 const uint32_t idx = ePropertyInheritEnv;
3505 if (GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0))
3506 {
3507 PlatformSP platform_sp (m_target->GetPlatform());
3508 if (platform_sp)
3509 {
3510 StringList env;
3511 if (platform_sp->GetEnvironment(env))
3512 {
3513 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary (NULL, ePropertyEnvVars);
3514 if (env_dict)
3515 {
3516 const bool can_replace = false;
3517 const size_t envc = env.GetSize();
3518 for (size_t idx=0; idx<envc; idx++)
3519 {
3520 const char *env_entry = env.GetStringAtIndex (idx);
3521 if (env_entry)
3522 {
3523 const char *equal_pos = ::strchr(env_entry, '=');
3524 ConstString key;
3525 // It is ok to have environment variables with no values
3526 const char *value = NULL;
3527 if (equal_pos)
3528 {
3529 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3530 if (equal_pos[1])
3531 value = equal_pos + 1;
3532 }
3533 else
3534 {
3535 key.SetCString(env_entry);
3536 }
3537 // Don't allow existing keys to be replaced with ones we get from the platform environment
3538 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3539 }
3540 }
3541 }
3542 }
3543 }
3544 }
3545 }
3546 }
3547 }
3548 Target *m_target;
3549 mutable bool m_got_host_env;
3550};
3551
Greg Claytonfbb76342013-11-20 21:07:01 +00003552//----------------------------------------------------------------------
3553// TargetProperties
3554//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003555TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003556 Properties (),
3557 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003558{
3559 if (target)
3560 {
3561 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003562
3563 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3564 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3565 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3566 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3567 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3568 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3569 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3570 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3571 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3572 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3573
3574 // Update m_launch_info once it was created
3575 Arg0ValueChangedCallback(this, NULL);
3576 RunArgsValueChangedCallback(this, NULL);
3577 //EnvVarsValueChangedCallback(this, NULL); // FIXME: cause segfault in Target::GetPlatform()
3578 InputPathValueChangedCallback(this, NULL);
3579 OutputPathValueChangedCallback(this, NULL);
3580 ErrorPathValueChangedCallback(this, NULL);
3581 DetachOnErrorValueChangedCallback(this, NULL);
3582 DisableASLRValueChangedCallback(this, NULL);
3583 DisableSTDIOValueChangedCallback(this, NULL);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003584 }
3585 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003586 {
3587 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3588 m_collection_sp->Initialize(g_properties);
3589 m_collection_sp->AppendProperty(ConstString("process"),
3590 ConstString("Settings specify to processes."),
3591 true,
3592 Process::GetGlobalProperties()->GetValueProperties());
3593 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003594}
3595
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003596TargetProperties::~TargetProperties() = default;
3597
Greg Clayton67cc0632012-08-22 17:17:09 +00003598ArchSpec
3599TargetProperties::GetDefaultArchitecture () const
3600{
3601 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3602 if (value)
3603 return value->GetCurrentValue();
3604 return ArchSpec();
3605}
3606
3607void
3608TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3609{
3610 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch (NULL, ePropertyDefaultArch);
3611 if (value)
3612 return value->SetCurrentValue(arch, true);
3613}
3614
Ilia K055ad9b2015-05-18 13:41:01 +00003615bool
3616TargetProperties::GetMoveToNearestCode() const
3617{
3618 const uint32_t idx = ePropertyMoveToNearestCode;
3619 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3620}
3621
Greg Clayton67cc0632012-08-22 17:17:09 +00003622lldb::DynamicValueType
3623TargetProperties::GetPreferDynamicValue() const
3624{
3625 const uint32_t idx = ePropertyPreferDynamic;
3626 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3627}
3628
3629bool
Greg Clayton15484402015-05-15 18:40:24 +00003630TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3631{
3632 const uint32_t idx = ePropertyPreferDynamic;
3633 return m_collection_sp->SetPropertyAtIndexAsEnumeration(NULL, idx, d);
3634}
3635
Greg Clayton15484402015-05-15 18:40:24 +00003636bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003637TargetProperties::GetDisableASLR () const
3638{
3639 const uint32_t idx = ePropertyDisableASLR;
3640 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3641}
3642
3643void
3644TargetProperties::SetDisableASLR (bool b)
3645{
3646 const uint32_t idx = ePropertyDisableASLR;
3647 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3648}
3649
3650bool
Jim Ingham106d0282014-06-25 02:32:56 +00003651TargetProperties::GetDetachOnError () const
3652{
3653 const uint32_t idx = ePropertyDetachOnError;
3654 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3655}
3656
3657void
3658TargetProperties::SetDetachOnError (bool b)
3659{
3660 const uint32_t idx = ePropertyDetachOnError;
3661 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3662}
3663
3664bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003665TargetProperties::GetDisableSTDIO () const
3666{
3667 const uint32_t idx = ePropertyDisableSTDIO;
3668 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3669}
3670
3671void
3672TargetProperties::SetDisableSTDIO (bool b)
3673{
3674 const uint32_t idx = ePropertyDisableSTDIO;
3675 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3676}
3677
Jim Ingham0f063ba2013-03-02 00:26:47 +00003678const char *
3679TargetProperties::GetDisassemblyFlavor () const
3680{
3681 const uint32_t idx = ePropertyDisassemblyFlavor;
3682 const char *return_value;
3683
3684 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3685 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3686 return return_value;
3687}
3688
Greg Clayton1f746072012-08-29 21:13:06 +00003689InlineStrategy
3690TargetProperties::GetInlineStrategy () const
3691{
3692 const uint32_t idx = ePropertyInlineStrategy;
3693 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration (NULL, idx, g_properties[idx].default_uint_value);
3694}
3695
Greg Clayton45392552012-10-17 22:57:12 +00003696const char *
3697TargetProperties::GetArg0 () const
3698{
3699 const uint32_t idx = ePropertyArg0;
3700 return m_collection_sp->GetPropertyAtIndexAsString (NULL, idx, NULL);
3701}
3702
3703void
3704TargetProperties::SetArg0 (const char *arg)
3705{
3706 const uint32_t idx = ePropertyArg0;
3707 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003708 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003709}
3710
Greg Clayton67cc0632012-08-22 17:17:09 +00003711bool
3712TargetProperties::GetRunArguments (Args &args) const
3713{
3714 const uint32_t idx = ePropertyRunArgs;
3715 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3716}
3717
3718void
3719TargetProperties::SetRunArguments (const Args &args)
3720{
3721 const uint32_t idx = ePropertyRunArgs;
3722 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003723 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003724}
3725
3726size_t
3727TargetProperties::GetEnvironmentAsArgs (Args &env) const
3728{
3729 const uint32_t idx = ePropertyEnvVars;
3730 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, env);
3731}
3732
Ilia K8f37ca52015-02-13 14:31:06 +00003733void
3734TargetProperties::SetEnvironmentFromArgs (const Args &env)
3735{
3736 const uint32_t idx = ePropertyEnvVars;
3737 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003738 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003739}
3740
Greg Clayton67cc0632012-08-22 17:17:09 +00003741bool
3742TargetProperties::GetSkipPrologue() const
3743{
3744 const uint32_t idx = ePropertySkipPrologue;
3745 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3746}
3747
3748PathMappingList &
3749TargetProperties::GetSourcePathMap () const
3750{
3751 const uint32_t idx = ePropertySourceMap;
3752 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings (NULL, false, idx);
3753 assert(option_value);
3754 return option_value->GetCurrentValue();
3755}
3756
3757FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003758TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003759{
3760 const uint32_t idx = ePropertyExecutableSearchPaths;
3761 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3762 assert(option_value);
3763 return option_value->GetCurrentValue();
3764}
3765
Michael Sartaina7499c92013-07-01 19:45:50 +00003766FileSpecList &
3767TargetProperties::GetDebugFileSearchPaths ()
3768{
3769 const uint32_t idx = ePropertyDebugFileSearchPaths;
3770 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3771 assert(option_value);
3772 return option_value->GetCurrentValue();
3773}
3774
Sean Callanan85054342015-04-03 15:39:47 +00003775FileSpecList &
3776TargetProperties::GetClangModuleSearchPaths ()
3777{
3778 const uint32_t idx = ePropertyClangModuleSearchPaths;
3779 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList (NULL, false, idx);
3780 assert(option_value);
3781 return option_value->GetCurrentValue();
3782}
3783
Greg Clayton67cc0632012-08-22 17:17:09 +00003784bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003785TargetProperties::GetEnableAutoImportClangModules() const
3786{
3787 const uint32_t idx = ePropertyAutoImportClangModules;
3788 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3789}
3790
3791bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003792TargetProperties::GetEnableSyntheticValue () const
3793{
3794 const uint32_t idx = ePropertyEnableSynthetic;
3795 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3796}
3797
3798uint32_t
3799TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3800{
3801 const uint32_t idx = ePropertyMaxChildrenCount;
3802 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3803}
3804
3805uint32_t
3806TargetProperties::GetMaximumSizeOfStringSummary() const
3807{
3808 const uint32_t idx = ePropertyMaxSummaryLength;
3809 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3810}
3811
Enrico Granatad325bf92013-06-04 22:54:16 +00003812uint32_t
3813TargetProperties::GetMaximumMemReadSize () const
3814{
3815 const uint32_t idx = ePropertyMaxMemReadSize;
3816 return m_collection_sp->GetPropertyAtIndexAsSInt64 (NULL, idx, g_properties[idx].default_uint_value);
3817}
3818
Greg Clayton67cc0632012-08-22 17:17:09 +00003819FileSpec
3820TargetProperties::GetStandardInputPath () const
3821{
3822 const uint32_t idx = ePropertyInputPath;
3823 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3824}
3825
3826void
3827TargetProperties::SetStandardInputPath (const char *p)
3828{
3829 const uint32_t idx = ePropertyInputPath;
3830 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3831}
3832
3833FileSpec
3834TargetProperties::GetStandardOutputPath () const
3835{
3836 const uint32_t idx = ePropertyOutputPath;
3837 return m_collection_sp->GetPropertyAtIndexAsFileSpec (NULL, idx);
3838}
3839
3840void
3841TargetProperties::SetStandardOutputPath (const char *p)
3842{
3843 const uint32_t idx = ePropertyOutputPath;
3844 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3845}
3846
3847FileSpec
3848TargetProperties::GetStandardErrorPath () const
3849{
3850 const uint32_t idx = ePropertyErrorPath;
3851 return m_collection_sp->GetPropertyAtIndexAsFileSpec(NULL, idx);
3852}
3853
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003854LanguageType
3855TargetProperties::GetLanguage () const
3856{
3857 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage (NULL, ePropertyLanguage);
3858 if (value)
3859 return value->GetCurrentValue();
3860 return LanguageType();
3861}
3862
Greg Clayton6920b522012-08-22 18:39:03 +00003863const char *
3864TargetProperties::GetExpressionPrefixContentsAsCString ()
3865{
3866 const uint32_t idx = ePropertyExprPrefix;
3867 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec (NULL, false, idx);
3868 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003869 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003870 const bool null_terminate = true;
3871 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003872 if (data_sp)
3873 return (const char *) data_sp->GetBytes();
3874 }
Greg Clayton6920b522012-08-22 18:39:03 +00003875 return NULL;
3876}
3877
Greg Clayton67cc0632012-08-22 17:17:09 +00003878void
3879TargetProperties::SetStandardErrorPath (const char *p)
3880{
3881 const uint32_t idx = ePropertyErrorPath;
3882 m_collection_sp->SetPropertyAtIndexAsString (NULL, idx, p);
3883}
3884
3885bool
3886TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3887{
3888 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3889 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3890}
3891
Jim Ingham17d023f2013-03-13 17:58:04 +00003892bool
Daniel Malead79ae052013-08-07 21:54:09 +00003893TargetProperties::GetUseHexImmediates () const
3894{
3895 const uint32_t idx = ePropertyUseHexImmediates;
3896 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3897}
3898
3899bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003900TargetProperties::GetUseFastStepping () const
3901{
3902 const uint32_t idx = ePropertyUseFastStepping;
3903 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3904}
3905
Greg Claytonfb6621e2013-12-06 21:59:52 +00003906bool
3907TargetProperties::GetDisplayExpressionsInCrashlogs () const
3908{
3909 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3910 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
3911}
3912
Enrico Granata397ddd52013-05-21 20:13:34 +00003913LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003914TargetProperties::GetLoadScriptFromSymbolFile () const
3915{
3916 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Enrico Granata397ddd52013-05-21 20:13:34 +00003917 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003918}
3919
Daniel Malead79ae052013-08-07 21:54:09 +00003920Disassembler::HexImmediateStyle
3921TargetProperties::GetHexImmediateStyle () const
3922{
3923 const uint32_t idx = ePropertyHexImmediateStyle;
3924 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3925}
3926
Greg Claytonfd814c52013-08-13 01:42:25 +00003927MemoryModuleLoadLevel
3928TargetProperties::GetMemoryModuleLoadLevel() const
3929{
3930 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
3931 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(NULL, idx, g_properties[idx].default_uint_value);
3932}
3933
Jason Molendaa4bea722014-02-14 05:06:49 +00003934bool
3935TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3936{
3937 const uint32_t idx = ePropertyTrapHandlerNames;
3938 return m_collection_sp->GetPropertyAtIndexAsArgs (NULL, idx, args);
3939}
Greg Claytonfd814c52013-08-13 01:42:25 +00003940
Jason Molendaa4bea722014-02-14 05:06:49 +00003941void
3942TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3943{
3944 const uint32_t idx = ePropertyTrapHandlerNames;
3945 m_collection_sp->SetPropertyAtIndexFromArgs (NULL, idx, args);
3946}
Greg Clayton67cc0632012-08-22 17:17:09 +00003947
Enrico Granata560558e2015-02-11 02:35:39 +00003948bool
3949TargetProperties::GetDisplayRuntimeSupportValues () const
3950{
3951 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3952 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3953}
3954
3955void
3956TargetProperties::SetDisplayRuntimeSupportValues (bool b)
3957{
3958 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
3959 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3960}
3961
Ewan Crawford78baa192015-05-13 09:18:18 +00003962bool
3963TargetProperties::GetNonStopModeEnabled () const
3964{
3965 const uint32_t idx = ePropertyNonStopModeEnabled;
3966 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, false);
3967}
3968
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00003969void
3970TargetProperties::SetNonStopModeEnabled (bool b)
3971{
3972 const uint32_t idx = ePropertyNonStopModeEnabled;
3973 m_collection_sp->SetPropertyAtIndexAsBoolean (NULL, idx, b);
3974}
3975
Ilia K8f37ca52015-02-13 14:31:06 +00003976const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00003977TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00003978{
Ilia Kcc39d3f2015-02-13 17:07:55 +00003979 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00003980 return m_launch_info;
3981}
3982
3983void
3984TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
3985{
3986 m_launch_info = launch_info;
3987 SetArg0(launch_info.GetArg0());
3988 SetRunArguments(launch_info.GetArguments());
3989 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
3990 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
3991 if (input_file_action)
3992 {
3993 const char *input_path = input_file_action->GetPath();
3994 if (input_path)
3995 SetStandardInputPath(input_path);
3996 }
3997 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
3998 if (output_file_action)
3999 {
4000 const char *output_path = output_file_action->GetPath();
4001 if (output_path)
4002 SetStandardOutputPath(output_path);
4003 }
4004 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
4005 if (error_file_action)
4006 {
4007 const char *error_path = error_file_action->GetPath();
4008 if (error_path)
4009 SetStandardErrorPath(error_path);
4010 }
4011 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
4012 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4013 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
4014}
4015
4016void
4017TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
4018{
4019 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4020 this_->m_launch_info.SetArg0(this_->GetArg0());
4021}
4022
4023void
4024TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
4025{
4026 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4027 Args args;
4028 if (this_->GetRunArguments(args))
4029 this_->m_launch_info.GetArguments() = args;
4030}
4031
4032void
4033TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
4034{
4035 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4036 Args args;
4037 if (this_->GetEnvironmentAsArgs(args))
4038 this_->m_launch_info.GetEnvironmentEntries() = args;
4039}
4040
4041void
4042TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4043{
4044 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004045 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004046}
4047
4048void
4049TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4050{
4051 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004052 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004053}
4054
4055void
4056TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4057{
4058 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004059 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004060}
4061
4062void
4063TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
4064{
4065 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4066 if (this_->GetDetachOnError())
4067 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4068 else
4069 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
4070}
4071
4072void
4073TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
4074{
4075 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4076 if (this_->GetDisableASLR())
4077 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4078 else
4079 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
4080}
4081
4082void
4083TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
4084{
4085 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4086 if (this_->GetDisableSTDIO())
4087 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4088 else
4089 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4090}
Ilia Keb2c19a2015-03-10 21:59:55 +00004091
4092//----------------------------------------------------------------------
4093// Target::TargetEventData
4094//----------------------------------------------------------------------
4095
4096Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
4097 EventData (),
4098 m_target_sp (target_sp),
4099 m_module_list ()
4100{
4101}
4102
4103Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
4104 EventData (),
4105 m_target_sp (target_sp),
4106 m_module_list (module_list)
4107{
4108}
4109
Eugene Zelenko8f30a652015-10-23 18:39:37 +00004110Target::TargetEventData::~TargetEventData() = default;
Ilia Keb2c19a2015-03-10 21:59:55 +00004111
4112const ConstString &
4113Target::TargetEventData::GetFlavorString ()
4114{
4115 static ConstString g_flavor ("Target::TargetEventData");
4116 return g_flavor;
4117}
4118
4119void
4120Target::TargetEventData::Dump (Stream *s) const
4121{
4122}
4123
4124const Target::TargetEventData *
4125Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4126{
4127 if (event_ptr)
4128 {
4129 const EventData *event_data = event_ptr->GetData();
4130 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4131 return static_cast <const TargetEventData *> (event_ptr->GetData());
4132 }
4133 return NULL;
4134}
4135
4136TargetSP
4137Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4138{
4139 TargetSP target_sp;
4140 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4141 if (event_data)
4142 target_sp = event_data->m_target_sp;
4143 return target_sp;
4144}
4145
4146ModuleList
4147Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4148{
4149 ModuleList module_list;
4150 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4151 if (event_data)
4152 module_list = event_data->m_module_list;
4153 return module_list;
4154}