blob: 9719457ff41babcb329ecc4bc22c016045457b18 [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
Greg Clayton04df8ee2016-02-26 19:38:18 +000012#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000013// Other libraries and framework includes
14// Project includes
Eugene Zelenko8f30a652015-10-23 18:39:37 +000015#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000016#include "lldb/Breakpoint/BreakpointResolver.h"
17#include "lldb/Breakpoint/BreakpointResolverAddress.h"
18#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000019#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020#include "lldb/Breakpoint/BreakpointResolverName.h"
Johnny Chen01a67862011-10-14 00:42:25 +000021#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000022#include "lldb/Core/Debugger.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/Event.h"
24#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000025#include "lldb/Core/Module.h"
26#include "lldb/Core/ModuleSpec.h"
27#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000028#include "lldb/Core/SourceManager.h"
Greg Claytonb09c5382013-12-13 17:20:18 +000029#include "lldb/Core/State.h"
Greg Clayton44d93782014-01-27 23:43:24 +000030#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/StreamString.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000032#include "lldb/Core/Timer.h"
33#include "lldb/Core/ValueObject.h"
Sean Callanan66810412015-10-19 23:11:07 +000034#include "lldb/Expression/REPL.h"
Jim Ingham151c0322015-09-15 21:13:50 +000035#include "lldb/Expression/UserExpression.h"
Sean Callanan4dbb2712015-09-25 20:35:58 +000036#include "Plugins/ExpressionParser/Clang/ClangASTSource.h"
37#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
38#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
Zachary Turner10687b02014-10-20 17:46:43 +000039#include "lldb/Host/FileSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Host/Host.h"
Jim Ingham9575d842011-03-11 03:53:59 +000041#include "lldb/Interpreter/CommandInterpreter.h"
42#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000043#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000044#include "lldb/Interpreter/OptionValues.h"
45#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000046#include "lldb/Symbol/ClangASTContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Symbol/ObjectFile.h"
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000048#include "lldb/Symbol/Function.h"
49#include "lldb/Symbol/Symbol.h"
Jim Ingham151c0322015-09-15 21:13:50 +000050#include "lldb/Target/Language.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000051#include "lldb/Target/LanguageRuntime.h"
52#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000054#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000055#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000056#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000057#include "lldb/Target/Thread.h"
58#include "lldb/Target/ThreadSpec.h"
Sean Callanan9a6940c2015-10-21 00:36:34 +000059#include "lldb/Utility/LLDBAssert.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
61using namespace lldb;
62using namespace lldb_private;
63
Jim Ingham4bddaeb2012-02-16 06:50:00 +000064ConstString &
65Target::GetStaticBroadcasterClass ()
66{
67 static ConstString class_name ("lldb.target");
68 return class_name;
69}
70
Jim Ingham893c9322014-11-22 01:42:44 +000071Target::Target(Debugger &debugger, const ArchSpec &target_arch, const lldb::PlatformSP &platform_sp, bool is_dummy_target) :
Greg Clayton67cc0632012-08-22 17:17:09 +000072 TargetProperties (this),
Jim Ingham4f465cf2012-10-10 18:32:14 +000073 Broadcaster (&debugger, Target::GetStaticBroadcasterClass().AsCString()),
Greg Clayton32e0a752011-03-30 18:16:51 +000074 ExecutionContextScope (),
Greg Clayton66111032010-06-23 01:19:29 +000075 m_debugger (debugger),
Greg Clayton32e0a752011-03-30 18:16:51 +000076 m_platform_sp (platform_sp),
Greg Claytonaf67cec2010-12-20 20:49:23 +000077 m_mutex (Mutex::eMutexTypeRecursive),
Greg Clayton32e0a752011-03-30 18:16:51 +000078 m_arch (target_arch),
Enrico Granata17598482012-11-08 02:22:02 +000079 m_images (this),
Greg Claytond5944cd2013-12-06 01:12:00 +000080 m_section_load_history (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081 m_breakpoint_list (false),
82 m_internal_breakpoint_list (true),
Johnny Chen01a67862011-10-14 00:42:25 +000083 m_watchpoint_list (),
Greg Clayton32e0a752011-03-30 18:16:51 +000084 m_process_sp (),
85 m_search_filter_sp (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000086 m_image_search_paths (ImageSearchPathsChanged, this),
Sean Callanana3444ff2015-11-10 22:54:42 +000087 m_ast_importer_sp (),
Greg Clayton9585fbf2013-03-19 00:20:55 +000088 m_source_manager_ap(),
Greg Clayton32e0a752011-03-30 18:16:51 +000089 m_stop_hooks (),
Jim Ingham6026ca32011-05-12 02:06:14 +000090 m_stop_hook_next_id (0),
Greg Claytond5944cd2013-12-06 01:12:00 +000091 m_valid (true),
Jim Ingham893c9322014-11-22 01:42:44 +000092 m_suppress_stop_hooks (false),
93 m_is_dummy_target(is_dummy_target)
94
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095{
Greg Claytoncfd1ace2010-10-31 03:01:06 +000096 SetEventName (eBroadcastBitBreakpointChanged, "breakpoint-changed");
97 SetEventName (eBroadcastBitModulesLoaded, "modules-loaded");
98 SetEventName (eBroadcastBitModulesUnloaded, "modules-unloaded");
Jim Ingham1b5792e2012-12-18 02:03:49 +000099 SetEventName (eBroadcastBitWatchpointChanged, "watchpoint-changed");
Enrico Granataf15ee4e2013-04-05 18:49:06 +0000100 SetEventName (eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000101
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000102 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000103
Greg Clayton5160ce52013-03-27 23:08:40 +0000104 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105 if (log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000106 log->Printf ("%p Target::Target()", static_cast<void*>(this));
Jason Molendae1b68ad2012-12-05 00:25:49 +0000107 if (m_arch.IsValid())
108 {
Jason Molendaa3f24b32012-12-12 02:23:56 +0000109 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 +0000110 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111}
112
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000113Target::~Target()
114{
115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
116 if (log)
117 log->Printf ("%p Target::~Target()", static_cast<void*>(this));
118 DeleteCurrentProcess ();
119}
120
Jim Ingham893c9322014-11-22 01:42:44 +0000121void
122Target::PrimeFromDummyTarget(Target *target)
123{
124 if (!target)
125 return;
126
127 m_stop_hooks = target->m_stop_hooks;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000128
129 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints())
130 {
131 if (breakpoint_sp->IsInternal())
132 continue;
133
134 BreakpointSP new_bp (new Breakpoint (*this, *breakpoint_sp.get()));
135 AddBreakpoint (new_bp, false);
136 }
Jim Ingham893c9322014-11-22 01:42:44 +0000137}
138
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139void
Caroline Ticeceb6b132010-10-26 03:11:13 +0000140Target::Dump (Stream *s, lldb::DescriptionLevel description_level)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141{
Greg Clayton89411422010-10-08 00:21:05 +0000142// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000143 if (description_level != lldb::eDescriptionLevelBrief)
144 {
145 s->Indent();
146 s->PutCString("Target\n");
147 s->IndentMore();
Greg Clayton93aa84e2010-10-29 04:59:35 +0000148 m_images.Dump(s);
149 m_breakpoint_list.Dump(s);
150 m_internal_breakpoint_list.Dump(s);
151 s->IndentLess();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000152 }
153 else
154 {
Greg Claytonaa149cb2011-08-11 02:48:45 +0000155 Module *exe_module = GetExecutableModulePointer();
156 if (exe_module)
157 s->PutCString (exe_module->GetFileSpec().GetFilename().GetCString());
Jim Ingham48028b62011-05-12 01:12:28 +0000158 else
159 s->PutCString ("No executable module.");
Caroline Ticeceb6b132010-10-26 03:11:13 +0000160 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161}
162
163void
Greg Clayton90ba8112012-12-05 00:16:59 +0000164Target::CleanupProcess ()
165{
166 // Do any cleanup of the target we need to do between process instances.
167 // NB It is better to do this before destroying the process in case the
168 // clean up needs some help from the process.
169 m_breakpoint_list.ClearAllBreakpointSites();
170 m_internal_breakpoint_list.ClearAllBreakpointSites();
171 // Disable watchpoints just on the debugger side.
172 Mutex::Locker locker;
173 this->GetWatchpointList().GetListMutex(locker);
174 DisableAllWatchpoints(false);
175 ClearAllWatchpointHitCounts();
Enrico Granata5e3fe042015-02-11 00:37:54 +0000176 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000177}
178
179void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000180Target::DeleteCurrentProcess ()
181{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000182 if (m_process_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 {
Greg Claytond5944cd2013-12-06 01:12:00 +0000184 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 if (m_process_sp->IsAlive())
Jason Molendaede31932015-04-17 05:01:58 +0000186 m_process_sp->Destroy(false);
Jim Inghamd0a3e122011-02-16 17:54:55 +0000187
188 m_process_sp->Finalize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189
Greg Clayton90ba8112012-12-05 00:16:59 +0000190 CleanupProcess ();
191
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 m_process_sp.reset();
193 }
194}
195
196const lldb::ProcessSP &
Greg Claytonc3776bf2012-02-09 06:16:32 +0000197Target::CreateProcess (Listener &listener, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198{
199 DeleteCurrentProcess ();
Zachary Turner7529df92015-09-01 20:02:29 +0000200 m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name, listener, crash_file);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201 return m_process_sp;
202}
203
204const lldb::ProcessSP &
205Target::GetProcessSP () const
206{
207 return m_process_sp;
208}
209
Sean Callanan66810412015-10-19 23:11:07 +0000210lldb::REPLSP
Sean Callanan3e7e9152015-10-20 00:23:46 +0000211Target::GetREPL (Error &err, lldb::LanguageType language, const char *repl_options, bool can_create)
Sean Callanan66810412015-10-19 23:11:07 +0000212{
213 if (language == eLanguageTypeUnknown)
214 {
Sean Callanan93c0b002015-10-21 19:14:33 +0000215 std::set<LanguageType> repl_languages;
216
217 Language::GetLanguagesSupportingREPLs(repl_languages);
218
219 if (repl_languages.size() == 1)
220 {
221 language = *repl_languages.begin();
222 }
Sean Callanan97f84e82015-10-21 19:31:17 +0000223 else if (repl_languages.size() == 0)
224 {
225 err.SetErrorStringWithFormat("LLDB isn't configured with support support for any REPLs.");
226 return REPLSP();
227 }
Sean Callanan93c0b002015-10-21 19:14:33 +0000228 else
229 {
230 err.SetErrorStringWithFormat("Multiple possible REPL languages. Please specify a language.");
Sean Callanan97f84e82015-10-21 19:31:17 +0000231 return REPLSP();
Sean Callanan93c0b002015-10-21 19:14:33 +0000232 }
Sean Callanan66810412015-10-19 23:11:07 +0000233 }
234
235 REPLMap::iterator pos = m_repl_map.find(language);
236
237 if (pos != m_repl_map.end())
238 {
239 return pos->second;
240 }
241
242 if (!can_create)
243 {
Sean Callanan3e7e9152015-10-20 00:23:46 +0000244 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 +0000245 return lldb::REPLSP();
246 }
247
Sean Callanan3b682de2015-10-21 00:28:44 +0000248 Debugger *const debugger = nullptr;
249 lldb::REPLSP ret = REPL::Create(err, language, debugger, this, repl_options);
Sean Callanan66810412015-10-19 23:11:07 +0000250
251 if (ret)
252 {
253 m_repl_map[language] = ret;
254 return m_repl_map[language];
255 }
256
Sean Callanan3e7e9152015-10-20 00:23:46 +0000257 if (err.Success())
258 {
259 err.SetErrorStringWithFormat("Couldn't create a REPL for %s", Language::GetNameForLanguageType(language));
260 }
261
262 return lldb::REPLSP();
Sean Callanan66810412015-10-19 23:11:07 +0000263}
264
Greg Clayton3418c852011-08-10 02:10:13 +0000265void
Sean Callanan9a6940c2015-10-21 00:36:34 +0000266Target::SetREPL (lldb::LanguageType language, lldb::REPLSP repl_sp)
267{
268 lldbassert(!m_repl_map.count(language));
269
270 m_repl_map[language] = repl_sp;
271}
272
273void
Greg Clayton3418c852011-08-10 02:10:13 +0000274Target::Destroy()
275{
276 Mutex::Locker locker (m_mutex);
Filipe Cabecinhas721ba3f2012-05-19 09:59:08 +0000277 m_valid = false;
Greg Clayton3418c852011-08-10 02:10:13 +0000278 DeleteCurrentProcess ();
279 m_platform_sp.reset();
280 m_arch.Clear();
Greg Claytonb35db632013-11-09 00:03:31 +0000281 ClearModules(true);
Greg Claytond5944cd2013-12-06 01:12:00 +0000282 m_section_load_history.Clear();
Greg Clayton3418c852011-08-10 02:10:13 +0000283 const bool notify = false;
284 m_breakpoint_list.RemoveAll(notify);
285 m_internal_breakpoint_list.RemoveAll(notify);
286 m_last_created_breakpoint.reset();
Johnny Chen01a67862011-10-14 00:42:25 +0000287 m_last_created_watchpoint.reset();
Greg Clayton3418c852011-08-10 02:10:13 +0000288 m_search_filter_sp.reset();
289 m_image_search_paths.Clear(notify);
Greg Clayton3418c852011-08-10 02:10:13 +0000290 m_stop_hooks.clear();
291 m_stop_hook_next_id = 0;
292 m_suppress_stop_hooks = false;
293}
294
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295BreakpointList &
296Target::GetBreakpointList(bool internal)
297{
298 if (internal)
299 return m_internal_breakpoint_list;
300 else
301 return m_breakpoint_list;
302}
303
304const BreakpointList &
305Target::GetBreakpointList(bool internal) const
306{
307 if (internal)
308 return m_internal_breakpoint_list;
309 else
310 return m_breakpoint_list;
311}
312
313BreakpointSP
314Target::GetBreakpointByID (break_id_t break_id)
315{
316 BreakpointSP bp_sp;
317
318 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
319 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
320 else
321 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
322
323 return bp_sp;
324}
325
326BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000327Target::CreateSourceRegexBreakpoint (const FileSpecList *containingModules,
Greg Clayton1f746072012-08-29 21:13:06 +0000328 const FileSpecList *source_file_spec_list,
329 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000330 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000331 bool hardware,
332 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333{
Jim Ingham87df91b2011-09-23 00:54:11 +0000334 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000335 if (move_to_nearest_code == eLazyBoolCalculate)
336 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000337 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex(nullptr, source_regex, !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000338 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000339}
340
Jim Ingham969795f2011-09-21 01:17:13 +0000341BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000342Target::CreateBreakpoint (const FileSpecList *containingModules,
343 const FileSpec &file,
344 uint32_t line_no,
Greg Clayton1f746072012-08-29 21:13:06 +0000345 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000346 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000347 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000348 bool hardware,
349 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000350{
Greg Clayton1f746072012-08-29 21:13:06 +0000351 if (check_inlines == eLazyBoolCalculate)
352 {
353 const InlineStrategy inline_strategy = GetInlineStrategy();
354 switch (inline_strategy)
355 {
356 case eInlineBreakpointsNever:
357 check_inlines = eLazyBoolNo;
358 break;
359
360 case eInlineBreakpointsHeaders:
361 if (file.IsSourceImplementationFile())
362 check_inlines = eLazyBoolNo;
363 else
364 check_inlines = eLazyBoolYes;
365 break;
366
367 case eInlineBreakpointsAlways:
368 check_inlines = eLazyBoolYes;
369 break;
370 }
371 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000372 SearchFilterSP filter_sp;
373 if (check_inlines == eLazyBoolNo)
374 {
375 // Not checking for inlines, we are looking only for matching compile units
376 FileSpecList compile_unit_list;
377 compile_unit_list.Append (file);
378 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
379 }
380 else
381 {
382 filter_sp = GetSearchFilterForModuleList (containingModules);
383 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000384 if (skip_prologue == eLazyBoolCalculate)
385 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000386 if (move_to_nearest_code == eLazyBoolCalculate)
387 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000388
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000389 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine(nullptr,
390 file,
391 line_no,
392 check_inlines,
393 skip_prologue,
394 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000395 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396}
397
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000399Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000402
403 // Check for any reason we want to move this breakpoint to other address.
404 addr = GetBreakableLoadAddress(addr);
405
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 // Attempt to resolve our load address if possible, though it is ok if
407 // it doesn't resolve to section/offset.
408
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000409 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000410 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000411 if (!so_addr.IsValid())
412 {
413 // The address didn't resolve, so just set this as an absolute address
414 so_addr.SetOffset (addr);
415 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000416 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 return bp_sp;
418}
419
420BreakpointSP
Jim Ingham055a08a2015-11-17 03:39:13 +0000421Target::CreateBreakpoint (const Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000422{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000423 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000424 BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(nullptr, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000425 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426}
427
Jim Ingham055a08a2015-11-17 03:39:13 +0000428lldb::BreakpointSP
429Target::CreateAddressInModuleBreakpoint (lldb::addr_t file_addr,
430 bool internal,
431 const FileSpec *file_spec,
432 bool request_hardware)
433{
434 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000435 BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(nullptr, file_addr, file_spec));
Jim Ingham055a08a2015-11-17 03:39:13 +0000436 return CreateBreakpoint (filter_sp, resolver_sp, internal, request_hardware, false);
437}
438
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000440Target::CreateBreakpoint (const FileSpecList *containingModules,
441 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000442 const char *func_name,
443 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000444 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000445 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000446 bool internal,
447 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000449 BreakpointSP bp_sp;
450 if (func_name)
451 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000452 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000453
454 if (skip_prologue == eLazyBoolCalculate)
455 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000456 if (language == lldb::eLanguageTypeUnknown)
457 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000458
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000459 BreakpointResolverSP resolver_sp(new BreakpointResolverName(nullptr,
460 func_name,
461 func_name_type_mask,
462 language,
463 Breakpoint::Exact,
464 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000465 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000466 }
467 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468}
469
Jim Inghamfab10e82012-03-06 00:37:27 +0000470lldb::BreakpointSP
471Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000472 const FileSpecList *containingSourceFiles,
473 const std::vector<std::string> &func_names,
474 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000475 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000476 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000477 bool internal,
478 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000479{
480 BreakpointSP bp_sp;
481 size_t num_names = func_names.size();
482 if (num_names > 0)
483 {
484 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000485
486 if (skip_prologue == eLazyBoolCalculate)
487 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000488 if (language == lldb::eLanguageTypeUnknown)
489 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000490
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000491 BreakpointResolverSP resolver_sp(new BreakpointResolverName(nullptr,
492 func_names,
493 func_name_type_mask,
494 language,
495 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000496 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000497 }
498 return bp_sp;
499}
500
Jim Ingham133e0fb2012-03-03 02:05:11 +0000501BreakpointSP
502Target::CreateBreakpoint (const FileSpecList *containingModules,
503 const FileSpecList *containingSourceFiles,
504 const char *func_names[],
505 size_t num_names,
506 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000507 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000508 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000509 bool internal,
510 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000511{
512 BreakpointSP bp_sp;
513 if (num_names > 0)
514 {
515 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
516
Greg Clayton03da4cc2013-04-19 21:31:16 +0000517 if (skip_prologue == eLazyBoolCalculate)
518 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000519 if (language == lldb::eLanguageTypeUnknown)
520 language = GetLanguage();
521
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000522 BreakpointResolverSP resolver_sp(new BreakpointResolverName(nullptr,
523 func_names,
524 num_names,
525 func_name_type_mask,
526 language,
527 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000528 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000529 }
530 return bp_sp;
531}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532
533SearchFilterSP
534Target::GetSearchFilterForModule (const FileSpec *containingModule)
535{
536 SearchFilterSP filter_sp;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000537 if (containingModule != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000538 {
539 // TODO: We should look into sharing module based search filters
540 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000541 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 }
543 else
544 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000545 if (!m_search_filter_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000546 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547 filter_sp = m_search_filter_sp;
548 }
549 return filter_sp;
550}
551
Jim Ingham969795f2011-09-21 01:17:13 +0000552SearchFilterSP
553Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
554{
555 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000556 if (containingModules && containingModules->GetSize() != 0)
557 {
558 // TODO: We should look into sharing module based search filters
559 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000560 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000561 }
562 else
563 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000564 if (!m_search_filter_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000565 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000566 filter_sp = m_search_filter_sp;
567 }
568 return filter_sp;
569}
570
Jim Ingham87df91b2011-09-23 00:54:11 +0000571SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000572Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
573 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000574{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000575 if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
Jim Ingham87df91b2011-09-23 00:54:11 +0000576 return GetSearchFilterForModuleList(containingModules);
577
578 SearchFilterSP filter_sp;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000579 if (containingModules == nullptr)
Jim Ingham87df91b2011-09-23 00:54:11 +0000580 {
581 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
582 // but that will take a little reworking.
583
Greg Claytone1cd1be2012-01-29 20:56:30 +0000584 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000585 }
586 else
587 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000588 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000589 }
590 return filter_sp;
591}
592
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000594Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000595 const FileSpecList *containingSourceFiles,
596 RegularExpression &func_regex,
Jim Ingham0fcdac32015-11-06 22:48:59 +0000597 lldb::LanguageType requested_language,
Jim Inghama8558b62012-05-22 00:12:20 +0000598 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000599 bool internal,
600 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601{
Jim Ingham87df91b2011-09-23 00:54:11 +0000602 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000603 bool skip =
604 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
605 : static_cast<bool>(skip_prologue);
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000606 BreakpointResolverSP resolver_sp(new BreakpointResolverName(nullptr,
607 func_regex,
608 requested_language,
609 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610
Jim Ingham1460e4b2014-01-10 23:46:59 +0000611 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612}
613
Jim Ingham219ba192012-03-05 04:47:34 +0000614lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000615Target::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 +0000616{
Jim Inghama72b31c2015-04-22 19:42:18 +0000617 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
618 if (exc_bkpt_sp && additional_args)
619 {
620 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
621 if (precondition_sp && additional_args)
622 {
623 if (error)
624 *error = precondition_sp->ConfigurePrecondition(*additional_args);
625 else
626 precondition_sp->ConfigurePrecondition(*additional_args);
627 }
628 }
629 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000630}
Jim Inghama72b31c2015-04-22 19:42:18 +0000631
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000633Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634{
635 BreakpointSP bp_sp;
636 if (filter_sp && resolver_sp)
637 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000638 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000640 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000642 return bp_sp;
643}
644
645void
646Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
647{
648 if (!bp_sp)
649 return;
650 if (internal)
651 m_internal_breakpoint_list.Add (bp_sp, false);
652 else
653 m_breakpoint_list.Add (bp_sp, true);
654
655 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
656 if (log)
657 {
658 StreamString s;
659 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
660 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
661 }
662
663 bp_sp->ResolveBreakpoint();
664
665 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000666 {
667 m_last_created_breakpoint = bp_sp;
668 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669}
670
Johnny Chen86364b42011-09-20 23:28:55 +0000671bool
672Target::ProcessIsValid()
673{
674 return (m_process_sp && m_process_sp->IsAlive());
675}
676
Johnny Chenb90827e2012-06-04 23:19:54 +0000677static bool
678CheckIfWatchpointsExhausted(Target *target, Error &error)
679{
680 uint32_t num_supported_hardware_watchpoints;
681 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
682 if (rc.Success())
683 {
684 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
685 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
686 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
687 num_supported_hardware_watchpoints);
688 }
689 return false;
690}
691
Johnny Chen01a67862011-10-14 00:42:25 +0000692// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000693// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000694WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000695Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000696{
Greg Clayton5160ce52013-03-27 23:08:40 +0000697 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000698 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000699 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000700 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000701
Johnny Chen01a67862011-10-14 00:42:25 +0000702 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000703 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000704 {
705 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000706 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000707 }
Jim Inghamc6462312013-06-18 21:52:48 +0000708
Johnny Chen45e541f2011-09-14 22:20:15 +0000709 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000710 {
711 if (size == 0)
712 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
713 else
Daniel Malead01b2952012-11-29 21:49:15 +0000714 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000715 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000716 }
Jim Inghamc6462312013-06-18 21:52:48 +0000717
718 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
719 {
720 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
721 }
Johnny Chen7313a642011-09-13 01:15:36 +0000722
Johnny Chen01a67862011-10-14 00:42:25 +0000723 // Currently we only support one watchpoint per address, with total number
724 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000725
726 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000727 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 +0000728 Mutex::Locker locker;
729 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000730 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000731 if (matched_sp)
732 {
Johnny Chen0c406372011-09-14 20:23:45 +0000733 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000734 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000735 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
736 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000737 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000738 if (size == old_size && kind == old_type)
739 {
Johnny Chen01a67862011-10-14 00:42:25 +0000740 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000741 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000742 }
743 else
744 {
Johnny Chen01a67862011-10-14 00:42:25 +0000745 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000746 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
747 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000748 }
Johnny Chen3c532582011-09-13 23:29:31 +0000749 }
750
Jason Molenda727e3922012-12-05 23:07:34 +0000751 if (!wp_sp)
752 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000753 wp_sp.reset(new Watchpoint(*this, addr, size, type));
754 wp_sp->SetWatchpointType(kind, notify);
755 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000756 }
Johnny Chen0c406372011-09-14 20:23:45 +0000757
Jim Ingham1b5792e2012-12-18 02:03:49 +0000758 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000759 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000760 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
761 __FUNCTION__,
762 error.Success() ? "succeeded" : "failed",
763 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000764
Jason Molenda727e3922012-12-05 23:07:34 +0000765 if (error.Fail())
766 {
Johnny Chen41b77262012-03-26 22:00:10 +0000767 // Enabling the watchpoint on the device side failed.
768 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000769 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000770 // See if we could provide more helpful error message.
771 if (!CheckIfWatchpointsExhausted(this, error))
772 {
773 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000774 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000775 }
Johnny Chen01a67862011-10-14 00:42:25 +0000776 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000777 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000778 else
Johnny Chen01a67862011-10-14 00:42:25 +0000779 m_last_created_watchpoint = wp_sp;
780 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000781}
782
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783void
784Target::RemoveAllBreakpoints (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
Greg Clayton9fed0d82010-07-23 23:33:17 +0000790 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000792 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000793
794 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795}
796
797void
798Target::DisableAllBreakpoints (bool internal_also)
799{
Greg Clayton5160ce52013-03-27 23:08:40 +0000800 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 if (log)
802 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
803
804 m_breakpoint_list.SetEnabledAll (false);
805 if (internal_also)
806 m_internal_breakpoint_list.SetEnabledAll (false);
807}
808
809void
810Target::EnableAllBreakpoints (bool internal_also)
811{
Greg Clayton5160ce52013-03-27 23:08:40 +0000812 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 if (log)
814 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
815
816 m_breakpoint_list.SetEnabledAll (true);
817 if (internal_also)
818 m_internal_breakpoint_list.SetEnabledAll (true);
819}
820
821bool
822Target::RemoveBreakpointByID (break_id_t break_id)
823{
Greg Clayton5160ce52013-03-27 23:08:40 +0000824 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000825 if (log)
826 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
827
828 if (DisableBreakpointByID (break_id))
829 {
830 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000831 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000833 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000834 if (m_last_created_breakpoint)
835 {
836 if (m_last_created_breakpoint->GetID() == break_id)
837 m_last_created_breakpoint.reset();
838 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000839 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000840 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 return true;
842 }
843 return false;
844}
845
846bool
847Target::DisableBreakpointByID (break_id_t break_id)
848{
Greg Clayton5160ce52013-03-27 23:08:40 +0000849 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000850 if (log)
851 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
852
853 BreakpointSP bp_sp;
854
855 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
856 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
857 else
858 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
859 if (bp_sp)
860 {
861 bp_sp->SetEnabled (false);
862 return true;
863 }
864 return false;
865}
866
867bool
868Target::EnableBreakpointByID (break_id_t break_id)
869{
Greg Clayton5160ce52013-03-27 23:08:40 +0000870 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 if (log)
872 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
873 __FUNCTION__,
874 break_id,
875 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
876
877 BreakpointSP bp_sp;
878
879 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
880 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
881 else
882 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
883
884 if (bp_sp)
885 {
886 bp_sp->SetEnabled (true);
887 return true;
888 }
889 return false;
890}
891
Johnny Chenedf50372011-09-23 21:21:43 +0000892// The flag 'end_to_end', default to true, signifies that the operation is
893// performed end to end, for both the debugger and the debuggee.
894
Johnny Chen01a67862011-10-14 00:42:25 +0000895// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
896// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000897bool
Johnny Chen01a67862011-10-14 00:42:25 +0000898Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000899{
Greg Clayton5160ce52013-03-27 23:08:40 +0000900 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000901 if (log)
902 log->Printf ("Target::%s\n", __FUNCTION__);
903
Johnny Chenedf50372011-09-23 21:21:43 +0000904 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000905 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000906 return true;
907 }
908
909 // Otherwise, it's an end to end operation.
910
Johnny Chen86364b42011-09-20 23:28:55 +0000911 if (!ProcessIsValid())
912 return false;
913
Johnny Chen01a67862011-10-14 00:42:25 +0000914 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000915 for (size_t i = 0; i < num_watchpoints; ++i)
916 {
Johnny Chen01a67862011-10-14 00:42:25 +0000917 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
918 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000919 return false;
920
Johnny Chen01a67862011-10-14 00:42:25 +0000921 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000922 if (rc.Fail())
923 return false;
924 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000925 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000926 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000927 return true; // Success!
928}
929
Johnny Chen01a67862011-10-14 00:42:25 +0000930// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
931// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000932bool
Johnny Chen01a67862011-10-14 00:42:25 +0000933Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000934{
Greg Clayton5160ce52013-03-27 23:08:40 +0000935 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000936 if (log)
937 log->Printf ("Target::%s\n", __FUNCTION__);
938
Johnny Chenedf50372011-09-23 21:21:43 +0000939 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000940 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000941 return true;
942 }
943
944 // Otherwise, it's an end to end operation.
945
Johnny Chen86364b42011-09-20 23:28:55 +0000946 if (!ProcessIsValid())
947 return false;
948
Johnny Chen01a67862011-10-14 00:42:25 +0000949 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000950 for (size_t i = 0; i < num_watchpoints; ++i)
951 {
Johnny Chen01a67862011-10-14 00:42:25 +0000952 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
953 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000954 return false;
955
Johnny Chen01a67862011-10-14 00:42:25 +0000956 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000957 if (rc.Fail())
958 return false;
959 }
Johnny Chen86364b42011-09-20 23:28:55 +0000960 return true; // Success!
961}
962
Johnny Chen01a67862011-10-14 00:42:25 +0000963// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
964// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000965bool
Johnny Chen01a67862011-10-14 00:42:25 +0000966Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000967{
Greg Clayton5160ce52013-03-27 23:08:40 +0000968 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000969 if (log)
970 log->Printf ("Target::%s\n", __FUNCTION__);
971
Johnny Chenedf50372011-09-23 21:21:43 +0000972 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000973 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000974 return true;
975 }
976
977 // Otherwise, it's an end to end operation.
978
Johnny Chen86364b42011-09-20 23:28:55 +0000979 if (!ProcessIsValid())
980 return false;
981
Johnny Chen01a67862011-10-14 00:42:25 +0000982 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000983 for (size_t i = 0; i < num_watchpoints; ++i)
984 {
Johnny Chen01a67862011-10-14 00:42:25 +0000985 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
986 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000987 return false;
988
Johnny Chen01a67862011-10-14 00:42:25 +0000989 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000990 if (rc.Fail())
991 return false;
992 }
Johnny Chen86364b42011-09-20 23:28:55 +0000993 return true; // Success!
994}
995
Johnny Chena4d6bc92012-02-25 06:44:30 +0000996// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
997bool
998Target::ClearAllWatchpointHitCounts ()
999{
Greg Clayton5160ce52013-03-27 23:08:40 +00001000 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +00001001 if (log)
1002 log->Printf ("Target::%s\n", __FUNCTION__);
1003
1004 size_t num_watchpoints = m_watchpoint_list.GetSize();
1005 for (size_t i = 0; i < num_watchpoints; ++i)
1006 {
1007 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1008 if (!wp_sp)
1009 return false;
1010
1011 wp_sp->ResetHitCount();
1012 }
1013 return true; // Success!
1014}
1015
Enrico Granata5e3fe042015-02-11 00:37:54 +00001016// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1017bool
1018Target::ClearAllWatchpointHistoricValues ()
1019{
1020 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
1021 if (log)
1022 log->Printf ("Target::%s\n", __FUNCTION__);
1023
1024 size_t num_watchpoints = m_watchpoint_list.GetSize();
1025 for (size_t i = 0; i < num_watchpoints; ++i)
1026 {
1027 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1028 if (!wp_sp)
1029 return false;
1030
1031 wp_sp->ResetHistoricValues();
1032 }
1033 return true; // Success!
1034}
1035
Johnny Chen01a67862011-10-14 00:42:25 +00001036// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +00001037// during these operations.
1038bool
Johnny Chen01a67862011-10-14 00:42:25 +00001039Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001040{
Greg Clayton5160ce52013-03-27 23:08:40 +00001041 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001042 if (log)
1043 log->Printf ("Target::%s\n", __FUNCTION__);
1044
1045 if (!ProcessIsValid())
1046 return false;
1047
Johnny Chen01a67862011-10-14 00:42:25 +00001048 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +00001049 for (size_t i = 0; i < num_watchpoints; ++i)
1050 {
Johnny Chen01a67862011-10-14 00:42:25 +00001051 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1052 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001053 return false;
1054
Johnny Chen01a67862011-10-14 00:42:25 +00001055 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001056 }
1057 return true; // Success!
1058}
1059
Johnny Chen01a67862011-10-14 00:42:25 +00001060// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001061bool
Johnny Chen01a67862011-10-14 00:42:25 +00001062Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001063{
Greg Clayton5160ce52013-03-27 23:08:40 +00001064 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001065 if (log)
1066 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1067
1068 if (!ProcessIsValid())
1069 return false;
1070
Johnny Chen01a67862011-10-14 00:42:25 +00001071 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1072 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001073 {
Johnny Chen01a67862011-10-14 00:42:25 +00001074 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001075 if (rc.Success())
1076 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001077
Johnny Chenf04ee932011-09-22 18:04:58 +00001078 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001079 }
1080 return false;
1081}
1082
Johnny Chen01a67862011-10-14 00:42:25 +00001083// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001084bool
Johnny Chen01a67862011-10-14 00:42:25 +00001085Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001086{
Greg Clayton5160ce52013-03-27 23:08:40 +00001087 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001088 if (log)
1089 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1090
1091 if (!ProcessIsValid())
1092 return false;
1093
Johnny Chen01a67862011-10-14 00:42:25 +00001094 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1095 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001096 {
Johnny Chen01a67862011-10-14 00:42:25 +00001097 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001098 if (rc.Success())
1099 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001100
Johnny Chenf04ee932011-09-22 18:04:58 +00001101 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001102 }
1103 return false;
1104}
1105
Johnny Chen01a67862011-10-14 00:42:25 +00001106// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001107bool
Johnny Chen01a67862011-10-14 00:42:25 +00001108Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001109{
Greg Clayton5160ce52013-03-27 23:08:40 +00001110 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001111 if (log)
1112 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1113
Jim Inghamb0b45132013-07-02 02:09:46 +00001114 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1115 if (watch_to_remove_sp == m_last_created_watchpoint)
1116 m_last_created_watchpoint.reset();
1117
Johnny Chen01a67862011-10-14 00:42:25 +00001118 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001119 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001120 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001121 return true;
1122 }
1123 return false;
1124}
1125
Johnny Chen01a67862011-10-14 00:42:25 +00001126// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001127bool
Johnny Chen01a67862011-10-14 00:42:25 +00001128Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001129{
Greg Clayton5160ce52013-03-27 23:08:40 +00001130 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001131 if (log)
1132 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1133
1134 if (!ProcessIsValid())
1135 return false;
1136
Johnny Chen01a67862011-10-14 00:42:25 +00001137 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1138 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001139 {
Johnny Chen01a67862011-10-14 00:42:25 +00001140 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001141 return true;
1142 }
1143 return false;
1144}
1145
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001146ModuleSP
1147Target::GetExecutableModule ()
1148{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001149 // search for the first executable in the module list
1150 for (size_t i = 0; i < m_images.GetSize(); ++i)
1151 {
1152 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1153 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1154 if (obj == nullptr)
1155 continue;
1156 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1157 return module_sp;
1158 }
1159 // as fall back return the first module loaded
1160 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001161}
1162
1163Module*
1164Target::GetExecutableModulePointer ()
1165{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001166 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167}
1168
Enrico Granata17598482012-11-08 02:22:02 +00001169static void
1170LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1171{
1172 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001173 StreamString feedback_stream;
1174 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001175 {
Enrico Granata97303392013-05-21 00:00:30 +00001176 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001177 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001178 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1179 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001180 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001181 if (feedback_stream.GetSize())
1182 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1183 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001184}
1185
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186void
Greg Claytonb35db632013-11-09 00:03:31 +00001187Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188{
Greg Claytonb35db632013-11-09 00:03:31 +00001189 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001190 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 m_images.Clear();
Greg Clayton5beec212015-10-08 21:04:34 +00001192 m_scratch_type_system_map.Clear();
Sean Callanana3444ff2015-11-10 22:54:42 +00001193 m_ast_importer_sp.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001194}
1195
1196void
Greg Claytonb35db632013-11-09 00:03:31 +00001197Target::DidExec ()
1198{
1199 // When a process exec's we need to know about it so we can do some cleanup.
1200 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1201 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1202}
1203
1204void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001205Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1206{
1207 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001208 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001210 if (executable_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211 {
1212 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001213 "Target::SetExecutableModule (executable = '%s')",
1214 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001216 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217
Jim Ingham5aee1622010-08-09 23:31:02 +00001218 // 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 +00001219 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001220 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001221 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001222 if (log)
1223 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1224 }
1225
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001227 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001228
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001229 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001230 {
1231 executable_objfile->GetDependentModules(dependent_files);
1232 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1233 {
Greg Claytonded470d2011-03-19 01:12:21 +00001234 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1235 FileSpec platform_dependent_file_spec;
1236 if (m_platform_sp)
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001237 m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001238 else
1239 platform_dependent_file_spec = dependent_file_spec;
1240
Greg Claytonb9a01b32012-02-26 05:51:37 +00001241 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1242 ModuleSP image_module_sp(GetSharedModule (module_spec));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001243 if (image_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245 ObjectFile *objfile = image_module_sp->GetObjectFile();
1246 if (objfile)
1247 objfile->GetDependentModules(dependent_files);
1248 }
1249 }
1250 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 }
1252}
1253
Jim Ingham5aee1622010-08-09 23:31:02 +00001254bool
1255Target::SetArchitecture (const ArchSpec &arch_spec)
1256{
Greg Clayton5160ce52013-03-27 23:08:40 +00001257 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001258 bool missing_local_arch = !m_arch.IsValid();
Jason Molendadad8af42015-11-10 04:11:37 +00001259 bool replace_local_arch = true;
1260 bool compatible_local_arch = false;
1261 ArchSpec other(arch_spec);
1262
1263 if (!missing_local_arch)
Jim Ingham5aee1622010-08-09 23:31:02 +00001264 {
Jason Molendadad8af42015-11-10 04:11:37 +00001265 if (m_arch.IsCompatibleMatch(arch_spec))
1266 {
1267 other.MergeFrom(m_arch);
1268
1269 if (m_arch.IsCompatibleMatch(other))
1270 {
1271 compatible_local_arch = true;
1272 bool arch_changed, vendor_changed, os_changed, os_ver_changed, env_changed;
1273
1274 m_arch.PiecewiseTripleCompare(other,
1275 arch_changed,
1276 vendor_changed,
1277 os_changed,
1278 os_ver_changed,
1279 env_changed);
1280
1281 if (!arch_changed && !vendor_changed && !os_changed)
1282 replace_local_arch = false;
1283 }
1284 }
1285 }
1286
1287 if (compatible_local_arch || missing_local_arch)
1288 {
1289 // If we haven't got a valid arch spec, or the architectures are compatible
1290 // update the architecture, unless the one we already have is more specified
1291 if (replace_local_arch)
1292 m_arch = other;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001293 if (log)
Jason Molendadad8af42015-11-10 04:11:37 +00001294 log->Printf ("Target::SetArchitecture set architecture to %s (%s)", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
Jim Ingham5aee1622010-08-09 23:31:02 +00001295 return true;
1296 }
Jason Molendadad8af42015-11-10 04:11:37 +00001297
1298 // If we have an executable file, try to reset the executable to the desired architecture
1299 if (log)
1300 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1301 m_arch = other;
1302 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001303
Jason Molendadad8af42015-11-10 04:11:37 +00001304 ClearModules(true);
1305 // Need to do something about unsetting breakpoints.
1306
1307 if (executable_sp)
1308 {
1309 if (log)
1310 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1311 ModuleSpec module_spec (executable_sp->GetFileSpec(), other);
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001312 Error error = ModuleList::GetSharedModule(module_spec,
1313 executable_sp,
1314 &GetExecutableSearchPaths(),
1315 nullptr,
1316 nullptr);
Jason Molendadad8af42015-11-10 04:11:37 +00001317
1318 if (!error.Fail() && executable_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +00001319 {
Jason Molendadad8af42015-11-10 04:11:37 +00001320 SetExecutableModule (executable_sp, true);
1321 return true;
Jim Ingham5aee1622010-08-09 23:31:02 +00001322 }
1323 }
Greg Clayton70512312012-05-08 01:45:38 +00001324 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001325}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001327bool
1328Target::MergeArchitecture (const ArchSpec &arch_spec)
1329{
1330 if (arch_spec.IsValid())
1331 {
1332 if (m_arch.IsCompatibleMatch(arch_spec))
1333 {
1334 // The current target arch is compatible with "arch_spec", see if we
1335 // can improve our current architecture using bits from "arch_spec"
1336
1337 // Merge bits from arch_spec into "merged_arch" and set our architecture
1338 ArchSpec merged_arch (m_arch);
1339 merged_arch.MergeFrom (arch_spec);
1340 return SetArchitecture(merged_arch);
1341 }
1342 else
1343 {
1344 // The new architecture is different, we just need to replace it
1345 return SetArchitecture(arch_spec);
1346 }
1347 }
1348 return false;
1349}
1350
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001351void
Enrico Granataefe637d2012-11-08 19:16:03 +00001352Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001353{
1354}
1355
1356void
Enrico Granataefe637d2012-11-08 19:16:03 +00001357Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358{
1359 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001360 if (m_valid)
1361 {
1362 ModuleList my_module_list;
1363 my_module_list.Append(module_sp);
1364 LoadScriptingResourceForModule(module_sp, this);
1365 ModulesDidLoad (my_module_list);
1366 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001367}
1368
1369void
Enrico Granataefe637d2012-11-08 19:16:03 +00001370Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001371{
Adrian McCarthy6ecdbc82015-10-15 22:39:55 +00001372 // A module is being removed from this target.
Greg Clayton23f8c952014-03-24 23:10:19 +00001373 if (m_valid)
1374 {
1375 ModuleList my_module_list;
1376 my_module_list.Append(module_sp);
1377 ModulesDidUnload (my_module_list, false);
1378 }
Enrico Granata17598482012-11-08 02:22:02 +00001379}
1380
1381void
Enrico Granataefe637d2012-11-08 19:16:03 +00001382Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001383{
Jim Inghame716ae02011-08-03 01:00:06 +00001384 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001385 if (m_valid)
Jason Molendad91c3fd2015-11-10 04:18:12 +00001386 {
Greg Clayton23f8c952014-03-24 23:10:19 +00001387 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001388 m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
1389 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390}
1391
1392void
1393Target::ModulesDidLoad (ModuleList &module_list)
1394{
Greg Clayton23f8c952014-03-24 23:10:19 +00001395 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001396 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001397 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001398 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001399 if (m_process_sp)
1400 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001401 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001402 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001403 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001404 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405}
1406
1407void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001408Target::SymbolsDidLoad (ModuleList &module_list)
1409{
Greg Clayton23f8c952014-03-24 23:10:19 +00001410 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001411 {
Jim Ingham31caf982013-06-04 23:01:35 +00001412 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001413 {
Jim Ingham31caf982013-06-04 23:01:35 +00001414 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1415 if (runtime)
1416 {
1417 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1418 objc_runtime->SymbolsDidLoad(module_list);
1419 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001420 }
Jim Ingham31caf982013-06-04 23:01:35 +00001421
Greg Clayton095eeaa2013-11-05 23:28:00 +00001422 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001423 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001424 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001425 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001426}
1427
1428void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001429Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430{
Greg Clayton23f8c952014-03-24 23:10:19 +00001431 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001432 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001433 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001434 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001435 m_internal_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001436 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001437 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438}
1439
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001440bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001441Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001442{
Greg Clayton67cc0632012-08-22 17:17:09 +00001443 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001444 {
1445 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001446 ModuleSpec module_spec (module_file_spec);
1447 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001448
1449 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1450 // black list.
1451 if (num_modules > 0)
1452 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001453 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001454 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001455 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001456 return false;
1457 }
1458 return true;
1459 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001460 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001461 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001462}
1463
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001464bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001465Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001466{
Greg Clayton67cc0632012-08-22 17:17:09 +00001467 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001468 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001469 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001470 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001471 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001472 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001473}
1474
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475size_t
Greg Claytondb598232011-01-07 01:57:07 +00001476Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1477{
Greg Claytone72dfb32012-02-24 01:59:29 +00001478 SectionSP section_sp (addr.GetSection());
1479 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001480 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001481 // 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 +00001482 if (section_sp->IsEncrypted())
1483 {
Greg Clayton57f06302012-05-25 17:05:55 +00001484 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001485 return 0;
1486 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001487 ModuleSP module_sp (section_sp->GetModule());
1488 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001489 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001490 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1491 if (objfile)
1492 {
1493 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1494 addr.GetOffset(),
1495 dst,
1496 dst_len);
1497 if (bytes_read > 0)
1498 return bytes_read;
1499 else
1500 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1501 }
Greg Claytondb598232011-01-07 01:57:07 +00001502 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001503 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001504 }
1505 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001506 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001507 }
1508 else
Greg Claytondb598232011-01-07 01:57:07 +00001509 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001510
Greg Claytondb598232011-01-07 01:57:07 +00001511 return 0;
1512}
1513
1514size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001515Target::ReadMemory (const Address& addr,
1516 bool prefer_file_cache,
1517 void *dst,
1518 size_t dst_len,
1519 Error &error,
1520 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001521{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001522 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001523
Enrico Granata9128ee22011-09-06 19:20:51 +00001524 // if we end up reading this from process memory, we will fill this
1525 // with the actual load address
1526 if (load_addr_ptr)
1527 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1528
Greg Claytondb598232011-01-07 01:57:07 +00001529 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001530
1531 addr_t load_addr = LLDB_INVALID_ADDRESS;
1532 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001533 Address resolved_addr;
1534 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001535 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001536 SectionLoadList &section_load_list = GetSectionLoadList();
1537 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001538 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001539 // No sections are loaded, so we must assume we are not running
1540 // yet and anything we are given is a file address.
1541 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1542 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001543 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001544 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001545 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001546 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001547 // we have manually loaded some sections with "target modules load ..."
1548 // or because we have have a live process that has sections loaded
1549 // through the dynamic loader
1550 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001551 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001552 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001553 }
Greg Clayton357132e2011-03-26 19:14:58 +00001554 if (!resolved_addr.IsValid())
1555 resolved_addr = addr;
Greg Claytonc749eb82011-07-11 05:12:02 +00001556
Greg Claytondb598232011-01-07 01:57:07 +00001557 if (prefer_file_cache)
1558 {
1559 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1560 if (bytes_read > 0)
1561 return bytes_read;
1562 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001563
Johnny Chen86364b42011-09-20 23:28:55 +00001564 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001565 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001566 if (load_addr == LLDB_INVALID_ADDRESS)
1567 load_addr = resolved_addr.GetLoadAddress (this);
1568
Greg Claytondda4f7b2010-06-30 23:03:03 +00001569 if (load_addr == LLDB_INVALID_ADDRESS)
1570 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001571 ModuleSP addr_module_sp (resolved_addr.GetModule());
1572 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001573 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001574 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001575 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001576 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001577 else
Daniel Malead01b2952012-11-29 21:49:15 +00001578 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001579 }
1580 else
1581 {
Greg Claytondb598232011-01-07 01:57:07 +00001582 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583 if (bytes_read != dst_len)
1584 {
1585 if (error.Success())
1586 {
1587 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001588 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589 else
Daniel Malead01b2952012-11-29 21:49:15 +00001590 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 +00001591 }
1592 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001593 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001594 {
1595 if (load_addr_ptr)
1596 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001597 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001598 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001599 // If the address is not section offset we have an address that
1600 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001601 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001602 // more we can do. If it is section offset, we might be able to
1603 // read cached memory from the object file.
1604 if (!resolved_addr.IsSectionOffset())
1605 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001606 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001607 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001608
Greg Claytonc749eb82011-07-11 05:12:02 +00001609 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001610 {
Greg Claytondb598232011-01-07 01:57:07 +00001611 // If we didn't already try and read from the object file cache, then
1612 // try it after failing to read from the process.
1613 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001614 }
1615 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001616}
1617
Greg Claytond16e1e52011-07-12 17:06:17 +00001618size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001619Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1620{
1621 char buf[256];
1622 out_str.clear();
1623 addr_t curr_addr = addr.GetLoadAddress(this);
1624 Address address(addr);
1625 while (1)
1626 {
1627 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1628 if (length == 0)
1629 break;
1630 out_str.append(buf, length);
1631 // If we got "length - 1" bytes, we didn't get the whole C string, we
1632 // need to read some more characters
1633 if (length == sizeof(buf) - 1)
1634 curr_addr += length;
1635 else
1636 break;
1637 address = Address(curr_addr);
1638 }
1639 return out_str.size();
1640}
1641
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001642size_t
1643Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1644{
1645 size_t total_cstr_len = 0;
1646 if (dst && dst_max_len)
1647 {
1648 result_error.Clear();
1649 // NULL out everything just to be safe
1650 memset (dst, 0, dst_max_len);
1651 Error error;
1652 addr_t curr_addr = addr.GetLoadAddress(this);
1653 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001654
1655 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1656 // think this really needs to be tied to the memory cache subsystem's
1657 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001658 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001659
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001660 size_t bytes_left = dst_max_len - 1;
1661 char *curr_dst = dst;
1662
1663 while (bytes_left > 0)
1664 {
1665 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1666 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1667 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1668
1669 if (bytes_read == 0)
1670 {
1671 result_error = error;
1672 dst[total_cstr_len] = '\0';
1673 break;
1674 }
1675 const size_t len = strlen(curr_dst);
1676
1677 total_cstr_len += len;
1678
1679 if (len < bytes_to_read)
1680 break;
1681
1682 curr_dst += bytes_read;
1683 curr_addr += bytes_read;
1684 bytes_left -= bytes_read;
1685 address = Address(curr_addr);
1686 }
1687 }
1688 else
1689 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001690 if (dst == nullptr)
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001691 result_error.SetErrorString("invalid arguments");
1692 else
1693 result_error.Clear();
1694 }
1695 return total_cstr_len;
1696}
1697
1698size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001699Target::ReadScalarIntegerFromMemory (const Address& addr,
1700 bool prefer_file_cache,
1701 uint32_t byte_size,
1702 bool is_signed,
1703 Scalar &scalar,
1704 Error &error)
1705{
1706 uint64_t uval;
1707
1708 if (byte_size <= sizeof(uval))
1709 {
1710 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1711 if (bytes_read == byte_size)
1712 {
1713 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001714 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001715 if (byte_size <= 4)
1716 scalar = data.GetMaxU32 (&offset, byte_size);
1717 else
1718 scalar = data.GetMaxU64 (&offset, byte_size);
1719
1720 if (is_signed)
1721 scalar.SignExtend(byte_size * 8);
1722 return bytes_read;
1723 }
1724 }
1725 else
1726 {
1727 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1728 }
1729 return 0;
1730}
1731
1732uint64_t
1733Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1734 bool prefer_file_cache,
1735 size_t integer_byte_size,
1736 uint64_t fail_value,
1737 Error &error)
1738{
1739 Scalar scalar;
1740 if (ReadScalarIntegerFromMemory (addr,
1741 prefer_file_cache,
1742 integer_byte_size,
1743 false,
1744 scalar,
1745 error))
1746 return scalar.ULongLong(fail_value);
1747 return fail_value;
1748}
1749
1750bool
1751Target::ReadPointerFromMemory (const Address& addr,
1752 bool prefer_file_cache,
1753 Error &error,
1754 Address &pointer_addr)
1755{
1756 Scalar scalar;
1757 if (ReadScalarIntegerFromMemory (addr,
1758 prefer_file_cache,
1759 m_arch.GetAddressByteSize(),
1760 false,
1761 scalar,
1762 error))
1763 {
1764 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1765 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1766 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001767 SectionLoadList &section_load_list = GetSectionLoadList();
1768 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001769 {
1770 // No sections are loaded, so we must assume we are not running
1771 // yet and anything we are given is a file address.
1772 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1773 }
1774 else
1775 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001776 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001777 // we have manually loaded some sections with "target modules load ..."
1778 // or because we have have a live process that has sections loaded
1779 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001780 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001781 }
1782 // We weren't able to resolve the pointer value, so just return
1783 // an address with no section
1784 if (!pointer_addr.IsValid())
1785 pointer_addr.SetOffset (pointer_vm_addr);
1786 return true;
1787
1788 }
1789 }
1790 return false;
1791}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001792
1793ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001794Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001795{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001796 ModuleSP module_sp;
1797
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001798 Error error;
1799
Jim Ingham4a94c912012-05-17 18:38:42 +00001800 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1801 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001802
Jim Ingham4a94c912012-05-17 18:38:42 +00001803 if (module_spec.GetUUID().IsValid())
1804 module_sp = m_images.FindFirstModule(module_spec);
1805
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001806 if (!module_sp)
1807 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001808 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1809 bool did_create_module = false;
1810
1811 // If there are image search path entries, try to use them first to acquire a suitable image.
1812 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001813 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001814 ModuleSpec transformed_spec (module_spec);
1815 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1816 {
1817 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1818 error = ModuleList::GetSharedModule (transformed_spec,
1819 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001820 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001821 &old_module_sp,
1822 &did_create_module);
1823 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001824 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001825
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001826 if (!module_sp)
1827 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001828 // If we have a UUID, we can check our global shared module list in case
1829 // we already have it. If we don't have a valid UUID, then we can't since
1830 // the path in "module_spec" will be a platform path, and we will need to
1831 // let the platform find that file. For example, we could be asking for
1832 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1833 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1834 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1835 // cache.
1836 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001837 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001838 // We have a UUID, it is OK to check the global module list...
1839 error = ModuleList::GetSharedModule (module_spec,
1840 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001841 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001842 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001843 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001844 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001845
1846 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001847 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001848 // The platform is responsible for finding and caching an appropriate
1849 // module in the shared module cache.
1850 if (m_platform_sp)
1851 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001852 error = m_platform_sp->GetSharedModule (module_spec,
1853 m_process_sp.get(),
1854 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001855 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001856 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001857 &did_create_module);
1858 }
1859 else
1860 {
1861 error.SetErrorString("no platform is currently set");
1862 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001863 }
1864 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001865
Jim Ingham4a94c912012-05-17 18:38:42 +00001866 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1867 // module in the list already, and if there was, let's remove it.
1868 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001869 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001870 ObjectFile *objfile = module_sp->GetObjectFile();
1871 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001872 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001873 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001874 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001875 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1876 case ObjectFile::eTypeExecutable: /// A normal executable
1877 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1878 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1879 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1880 break;
1881 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1882 if (error_ptr)
1883 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1884 return ModuleSP();
1885 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1886 if (error_ptr)
1887 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1888 return ModuleSP();
1889 default:
1890 if (error_ptr)
1891 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1892 return ModuleSP();
1893 }
1894 // GetSharedModule is not guaranteed to find the old shared module, for instance
1895 // in the common case where you pass in the UUID, it is only going to find the one
1896 // module matching the UUID. In fact, it has no good way to know what the "old module"
1897 // relevant to this target is, since there might be many copies of a module with this file spec
1898 // in various running debug sessions, but only one of them will belong to this target.
1899 // So let's remove the UUID from the module list, and look in the target's module list.
1900 // Only do this if there is SOMETHING else in the module spec...
1901 if (!old_module_sp)
1902 {
1903 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001904 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001905 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1906 module_spec_copy.GetUUID().Clear();
1907
1908 ModuleList found_modules;
1909 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1910 if (num_found == 1)
1911 {
1912 old_module_sp = found_modules.GetModuleAtIndex(0);
1913 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001914 }
1915 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001916
1917 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1918 {
1919 m_images.ReplaceModule(old_module_sp, module_sp);
1920 Module *old_module_ptr = old_module_sp.get();
1921 old_module_sp.reset();
1922 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1923 }
1924 else
1925 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001926 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001927 else
1928 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001929 }
1930 }
1931 if (error_ptr)
1932 *error_ptr = error;
1933 return module_sp;
1934}
1935
Greg Claytond9e416c2012-02-18 05:35:26 +00001936TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937Target::CalculateTarget ()
1938{
Greg Claytond9e416c2012-02-18 05:35:26 +00001939 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001940}
1941
Greg Claytond9e416c2012-02-18 05:35:26 +00001942ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001943Target::CalculateProcess ()
1944{
Sean Callanan92734d12016-02-12 19:45:31 +00001945 return m_process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946}
1947
Greg Claytond9e416c2012-02-18 05:35:26 +00001948ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001949Target::CalculateThread ()
1950{
Greg Claytond9e416c2012-02-18 05:35:26 +00001951 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952}
1953
Jason Molendab57e4a12013-11-04 09:33:30 +00001954StackFrameSP
1955Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001956{
Jason Molendab57e4a12013-11-04 09:33:30 +00001957 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001958}
1959
1960void
Greg Clayton0603aa92010-10-04 01:05:56 +00001961Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001962{
Greg Claytonc14ee322011-09-22 04:58:26 +00001963 exe_ctx.Clear();
1964 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965}
1966
1967PathMappingList &
1968Target::GetImageSearchPathList ()
1969{
1970 return m_image_search_paths;
1971}
1972
1973void
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001974Target::ImageSearchPathsChanged(const PathMappingList &path_list,
1975 void *baton)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976{
1977 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001978 ModuleSP exe_module_sp (target->GetExecutableModule());
1979 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001980 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981}
1982
Jim Ingham151c0322015-09-15 21:13:50 +00001983TypeSystem *
Sean Callanana994b0b2015-10-02 18:40:30 +00001984Target::GetScratchTypeSystemForLanguage (Error *error, lldb::LanguageType language, bool create_on_demand)
Jim Ingham151c0322015-09-15 21:13:50 +00001985{
Greg Clayton5beec212015-10-08 21:04:34 +00001986 if (!m_valid)
1987 return nullptr;
1988
Sean Callanana994b0b2015-10-02 18:40:30 +00001989 if (error)
1990 {
1991 error->Clear();
1992 }
1993
Sean Callananb92bd752015-10-01 16:28:02 +00001994 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Sean Callanana994b0b2015-10-02 18:40:30 +00001995 || language == eLanguageTypeUnknown)
1996 {
Sean Callananc7b054a2015-10-09 18:01:10 +00001997 std::set<lldb::LanguageType> languages_for_types;
1998 std::set<lldb::LanguageType> languages_for_expressions;
1999
2000 Language::GetLanguagesSupportingTypeSystems(languages_for_types, languages_for_expressions);
2001
2002 if (languages_for_expressions.count(eLanguageTypeC))
2003 {
2004 language = eLanguageTypeC; // LLDB's default. Override by setting the target language.
2005 }
2006 else
2007 {
2008 if (languages_for_expressions.empty())
2009 {
2010 return nullptr;
2011 }
2012 else
2013 {
2014 language = *languages_for_expressions.begin();
2015 }
2016 }
Sean Callananb92bd752015-10-01 16:28:02 +00002017 }
Sean Callananb92bd752015-10-01 16:28:02 +00002018
Greg Clayton5beec212015-10-08 21:04:34 +00002019 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this, create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00002020}
2021
2022PersistentExpressionState *
2023Target::GetPersistentExpressionStateForLanguage (lldb::LanguageType language)
2024{
Sean Callanana994b0b2015-10-02 18:40:30 +00002025 TypeSystem *type_system = GetScratchTypeSystemForLanguage(nullptr, language, true);
Sean Callananb92bd752015-10-01 16:28:02 +00002026
2027 if (type_system)
2028 {
2029 return type_system->GetPersistentExpressionState();
2030 }
Jim Ingham151c0322015-09-15 21:13:50 +00002031 else
Sean Callananb92bd752015-10-01 16:28:02 +00002032 {
2033 return nullptr;
2034 }
Jim Ingham151c0322015-09-15 21:13:50 +00002035}
2036
2037UserExpression *
2038Target::GetUserExpressionForLanguage(const char *expr,
Sean Callanana994b0b2015-10-02 18:40:30 +00002039 const char *expr_prefix,
2040 lldb::LanguageType language,
2041 Expression::ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +00002042 const EvaluateExpressionOptions &options,
Sean Callanana994b0b2015-10-02 18:40:30 +00002043 Error &error)
Jim Ingham151c0322015-09-15 21:13:50 +00002044{
Sean Callanana994b0b2015-10-02 18:40:30 +00002045 Error type_system_error;
2046
2047 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002048 UserExpression *user_expr = nullptr;
2049
2050 if (!type_system)
2051 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002052 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 +00002053 return nullptr;
2054 }
2055
Jim Ingham19a63fc2015-11-03 02:11:24 +00002056 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00002057 if (!user_expr)
2058 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2059
2060 return user_expr;
2061}
2062
2063FunctionCaller *
2064Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
2065 const CompilerType &return_type,
2066 const Address& function_address,
2067 const ValueList &arg_value_list,
2068 const char *name,
2069 Error &error)
2070{
Sean Callanana994b0b2015-10-02 18:40:30 +00002071 Error type_system_error;
2072 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002073 FunctionCaller *persistent_fn = nullptr;
2074
2075 if (!type_system)
2076 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002077 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 +00002078 return persistent_fn;
2079 }
2080
2081 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
2082 if (!persistent_fn)
2083 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2084
2085 return persistent_fn;
2086}
2087
2088UtilityFunction *
2089Target::GetUtilityFunctionForLanguage (const char *text,
2090 lldb::LanguageType language,
2091 const char *name,
2092 Error &error)
2093{
Sean Callanana994b0b2015-10-02 18:40:30 +00002094 Error type_system_error;
2095 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002096 UtilityFunction *utility_fn = nullptr;
2097
2098 if (!type_system)
2099 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002100 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 +00002101 return utility_fn;
2102 }
2103
2104 utility_fn = type_system->GetUtilityFunction (text, name);
2105 if (!utility_fn)
2106 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2107
2108 return utility_fn;
2109}
2110
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002111ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00002112Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002113{
Greg Clayton5beec212015-10-08 21:04:34 +00002114 if (m_valid)
Sean Callanan4bf80d52011-11-15 22:27:19 +00002115 {
Greg Clayton5beec212015-10-08 21:04:34 +00002116 if (TypeSystem* type_system = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC, create_on_demand))
2117 return llvm::dyn_cast<ClangASTContext>(type_system);
Sean Callanan4bf80d52011-11-15 22:27:19 +00002118 }
Greg Clayton5beec212015-10-08 21:04:34 +00002119 return nullptr;
Sean Callananb92bd752015-10-01 16:28:02 +00002120}
2121
Sean Callanana3444ff2015-11-10 22:54:42 +00002122ClangASTImporterSP
Sean Callanan686b2312011-11-16 18:20:47 +00002123Target::GetClangASTImporter()
2124{
Greg Clayton5beec212015-10-08 21:04:34 +00002125 if (m_valid)
Sean Callanan686b2312011-11-16 18:20:47 +00002126 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002127 if (!m_ast_importer_sp)
Greg Clayton5beec212015-10-08 21:04:34 +00002128 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002129 m_ast_importer_sp.reset(new ClangASTImporter());
Greg Clayton5beec212015-10-08 21:04:34 +00002130 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002131 return m_ast_importer_sp;
Sean Callanan686b2312011-11-16 18:20:47 +00002132 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002133 return ClangASTImporterSP();
Sean Callanan686b2312011-11-16 18:20:47 +00002134}
2135
Greg Clayton99d0faf2010-11-18 23:32:35 +00002136void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002137Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002138{
Greg Clayton6920b522012-08-22 18:39:03 +00002139 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002140}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002141
Greg Clayton99d0faf2010-11-18 23:32:35 +00002142void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002143Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002144{
Greg Clayton6920b522012-08-22 18:39:03 +00002145 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002146}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002147
Greg Claytonc859e2d2012-02-13 23:10:39 +00002148FileSpecList
2149Target::GetDefaultExecutableSearchPaths ()
2150{
Greg Clayton67cc0632012-08-22 17:17:09 +00002151 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2152 if (properties_sp)
2153 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002154 return FileSpecList();
2155}
2156
Michael Sartaina7499c92013-07-01 19:45:50 +00002157FileSpecList
2158Target::GetDefaultDebugFileSearchPaths ()
2159{
2160 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2161 if (properties_sp)
2162 return properties_sp->GetDebugFileSearchPaths();
2163 return FileSpecList();
2164}
2165
Sean Callanan85054342015-04-03 15:39:47 +00002166FileSpecList
2167Target::GetDefaultClangModuleSearchPaths ()
2168{
2169 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2170 if (properties_sp)
2171 return properties_sp->GetClangModuleSearchPaths();
2172 return FileSpecList();
2173}
2174
Caroline Ticedaccaa92010-09-20 20:44:43 +00002175ArchSpec
2176Target::GetDefaultArchitecture ()
2177{
Greg Clayton67cc0632012-08-22 17:17:09 +00002178 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2179 if (properties_sp)
2180 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002181 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002182}
2183
2184void
Greg Clayton67cc0632012-08-22 17:17:09 +00002185Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002186{
Greg Clayton67cc0632012-08-22 17:17:09 +00002187 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2188 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002189 {
2190 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 +00002191 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002192 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002193}
2194
Greg Clayton0603aa92010-10-04 01:05:56 +00002195Target *
2196Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2197{
2198 // The target can either exist in the "process" of ExecutionContext, or in
2199 // the "target_sp" member of SymbolContext. This accessor helper function
2200 // will get the target from one of these locations.
2201
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002202 Target *target = nullptr;
2203 if (sc_ptr != nullptr)
Greg Clayton0603aa92010-10-04 01:05:56 +00002204 target = sc_ptr->target_sp.get();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002205 if (target == nullptr && exe_ctx_ptr)
Greg Claytonc14ee322011-09-22 04:58:26 +00002206 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002207 return target;
2208}
2209
Jim Ingham1624a2d2014-05-05 02:26:40 +00002210ExpressionResults
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002211Target::EvaluateExpression(const char *expr_cstr,
Jim Inghama309efe2015-10-28 22:23:17 +00002212 ExecutionContextScope *exe_scope,
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002213 lldb::ValueObjectSP &result_valobj_sp,
2214 const EvaluateExpressionOptions& options)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002215{
Enrico Granata97fca502012-09-18 17:43:16 +00002216 result_valobj_sp.reset();
2217
Jim Ingham8646d3c2014-05-05 02:47:44 +00002218 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002219
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002220 if (expr_cstr == nullptr || expr_cstr[0] == '\0')
Greg Claytond1767f02011-12-08 02:13:16 +00002221 return execution_results;
2222
Jim Ingham6026ca32011-05-12 02:06:14 +00002223 // We shouldn't run stop hooks in expressions.
2224 // Be sure to reset this if you return anywhere within this function.
2225 bool old_suppress_value = m_suppress_stop_hooks;
2226 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002227
2228 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002229
Jim Inghama309efe2015-10-28 22:23:17 +00002230 if (exe_scope)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002231 {
Jim Inghama309efe2015-10-28 22:23:17 +00002232 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002233 }
2234 else if (m_process_sp)
2235 {
2236 m_process_sp->CalculateExecutionContext(exe_ctx);
2237 }
2238 else
2239 {
2240 CalculateExecutionContext(exe_ctx);
2241 }
2242
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002243 // Make sure we aren't just trying to see the value of a persistent
2244 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002245 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002246 // Only check for persistent variables the expression starts with a '$'
2247 if (expr_cstr[0] == '$')
Sean Callanana994b0b2015-10-02 18:40:30 +00002248 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002249
2250 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002251 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002252 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002253 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002254 }
2255 else
2256 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002257 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002258 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002259 execution_results = UserExpression::Evaluate (exe_ctx,
2260 options,
2261 expr_cstr,
2262 prefix,
2263 result_valobj_sp,
2264 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002265 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002266
2267 m_suppress_stop_hooks = old_suppress_value;
2268
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002269 return execution_results;
2270}
2271
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002272lldb::ExpressionVariableSP
2273Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002274{
Greg Clayton5beec212015-10-08 21:04:34 +00002275 lldb::ExpressionVariableSP variable_sp;
2276 m_scratch_type_system_map.ForEach([this, name, &variable_sp](TypeSystem *type_system) -> bool
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002277 {
Greg Clayton5beec212015-10-08 21:04:34 +00002278 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002279 {
Greg Clayton5beec212015-10-08 21:04:34 +00002280 variable_sp = persistent_state->GetVariable(name);
2281
2282 if (variable_sp)
2283 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002284 }
Greg Clayton5beec212015-10-08 21:04:34 +00002285 return true; // Keep iterating the ForEach
2286 });
2287 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002288}
2289
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002290lldb::addr_t
Sean Callananb92bd752015-10-01 16:28:02 +00002291Target::GetPersistentSymbol(const ConstString &name)
2292{
Greg Clayton5beec212015-10-08 21:04:34 +00002293 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Sean Callananb92bd752015-10-01 16:28:02 +00002294
Greg Clayton5beec212015-10-08 21:04:34 +00002295 m_scratch_type_system_map.ForEach([this, name, &address](TypeSystem *type_system) -> bool
Sean Callananb92bd752015-10-01 16:28:02 +00002296 {
Greg Clayton5beec212015-10-08 21:04:34 +00002297 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callananb92bd752015-10-01 16:28:02 +00002298 {
Greg Clayton5beec212015-10-08 21:04:34 +00002299 address = persistent_state->LookupSymbol(name);
2300 if (address != LLDB_INVALID_ADDRESS)
2301 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002302 }
Greg Clayton5beec212015-10-08 21:04:34 +00002303 return true; // Keep iterating the ForEach
2304 });
2305 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002306}
2307
2308lldb::addr_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002309Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2310{
2311 addr_t code_addr = load_addr;
2312 switch (m_arch.GetMachine())
2313 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002314 case llvm::Triple::mips:
2315 case llvm::Triple::mipsel:
2316 case llvm::Triple::mips64:
2317 case llvm::Triple::mips64el:
2318 switch (addr_class)
2319 {
2320 case eAddressClassData:
2321 case eAddressClassDebug:
2322 return LLDB_INVALID_ADDRESS;
2323
2324 case eAddressClassUnknown:
2325 case eAddressClassInvalid:
2326 case eAddressClassCode:
2327 case eAddressClassCodeAlternateISA:
2328 case eAddressClassRuntime:
2329 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2330 code_addr |= 1ull;
2331 break;
2332 }
2333 break;
2334
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002335 case llvm::Triple::arm:
2336 case llvm::Triple::thumb:
2337 switch (addr_class)
2338 {
2339 case eAddressClassData:
2340 case eAddressClassDebug:
2341 return LLDB_INVALID_ADDRESS;
2342
2343 case eAddressClassUnknown:
2344 case eAddressClassInvalid:
2345 case eAddressClassCode:
2346 case eAddressClassCodeAlternateISA:
2347 case eAddressClassRuntime:
2348 // Check if bit zero it no set?
2349 if ((code_addr & 1ull) == 0)
2350 {
2351 // Bit zero isn't set, check if the address is a multiple of 2?
2352 if (code_addr & 2ull)
2353 {
2354 // The address is a multiple of 2 so it must be thumb, set bit zero
2355 code_addr |= 1ull;
2356 }
2357 else if (addr_class == eAddressClassCodeAlternateISA)
2358 {
2359 // We checked the address and the address claims to be the alternate ISA
2360 // which means thumb, so set bit zero.
2361 code_addr |= 1ull;
2362 }
2363 }
2364 break;
2365 }
2366 break;
2367
2368 default:
2369 break;
2370 }
2371 return code_addr;
2372}
2373
2374lldb::addr_t
2375Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2376{
2377 addr_t opcode_addr = load_addr;
2378 switch (m_arch.GetMachine())
2379 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002380 case llvm::Triple::mips:
2381 case llvm::Triple::mipsel:
2382 case llvm::Triple::mips64:
2383 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002384 case llvm::Triple::arm:
2385 case llvm::Triple::thumb:
2386 switch (addr_class)
2387 {
2388 case eAddressClassData:
2389 case eAddressClassDebug:
2390 return LLDB_INVALID_ADDRESS;
2391
2392 case eAddressClassInvalid:
2393 case eAddressClassUnknown:
2394 case eAddressClassCode:
2395 case eAddressClassCodeAlternateISA:
2396 case eAddressClassRuntime:
2397 opcode_addr &= ~(1ull);
2398 break;
2399 }
2400 break;
2401
2402 default:
2403 break;
2404 }
2405 return opcode_addr;
2406}
2407
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002408lldb::addr_t
2409Target::GetBreakableLoadAddress (lldb::addr_t addr)
2410{
2411 addr_t breakable_addr = addr;
2412 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2413
2414 switch (m_arch.GetMachine())
2415 {
2416 default:
2417 break;
2418 case llvm::Triple::mips:
2419 case llvm::Triple::mipsel:
2420 case llvm::Triple::mips64:
2421 case llvm::Triple::mips64el:
2422 {
2423 addr_t function_start = 0;
2424 addr_t current_offset = 0;
2425 uint32_t loop_count = 0;
2426 Address resolved_addr;
2427 uint32_t arch_flags = m_arch.GetFlags ();
2428 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2429 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2430 SectionLoadList &section_load_list = GetSectionLoadList();
2431
2432 if (section_load_list.IsEmpty())
2433 // No sections are loaded, so we must assume we are not running yet
2434 // and need to operate only on file address.
2435 m_images.ResolveFileAddress (addr, resolved_addr);
2436 else
2437 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2438
2439 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2440 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2441 if (temp_addr_module_sp)
2442 {
2443 SymbolContext sc;
2444 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002445 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002446 Address sym_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002447 if (sc.function)
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002448 sym_addr = sc.function->GetAddressRange().GetBaseAddress();
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002449 else if (sc.symbol)
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002450 sym_addr = sc.symbol->GetAddress();
2451
2452 function_start = sym_addr.GetLoadAddress(this);
2453 if (function_start == LLDB_INVALID_ADDRESS)
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002454 function_start = sym_addr.GetFileAddress();
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002455
2456 if (function_start)
2457 current_offset = addr - function_start;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002458 }
2459
2460 // If breakpoint address is start of function then we dont have to do anything.
2461 if (current_offset == 0)
2462 return breakable_addr;
2463 else
2464 loop_count = current_offset / 2;
2465
2466 if (loop_count > 3)
2467 {
2468 // Scan previous 6 bytes
2469 if (IsMips16 | IsMicromips)
2470 loop_count = 3;
2471 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2472 else
2473 loop_count = 2;
2474 }
2475
2476 // Create Disassembler Instance
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002477 lldb::DisassemblerSP disasm_sp(Disassembler::FindPlugin(m_arch, nullptr, nullptr));
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002478
2479 ExecutionContext exe_ctx;
2480 CalculateExecutionContext(exe_ctx);
2481 InstructionList instruction_list;
2482 InstructionSP prev_insn;
2483 bool prefer_file_cache = true; // Read from file
2484 uint32_t inst_to_choose = 0;
2485
2486 for (uint32_t i = 1; i <= loop_count; i++)
2487 {
2488 // Adjust the address to read from.
2489 resolved_addr.Slide (-2);
2490 AddressRange range(resolved_addr, i*2);
2491 uint32_t insn_size = 0;
2492
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002493 disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002494
2495 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2496 if (num_insns)
2497 {
2498 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2499 insn_size = prev_insn->GetOpcode().GetByteSize();
2500 if (i == 1 && insn_size == 2)
2501 {
2502 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2503 instruction_list.Append(prev_insn);
2504 inst_to_choose = 1;
2505 }
2506 else if (i == 2)
2507 {
2508 // Here we may get one 4-byte instruction or two 2-byte instructions.
2509 if (num_insns == 2)
2510 {
2511 // Looks like there are two 2-byte instructions above our breakpoint target address.
2512 // 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.
2513 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2514 // and whatever i=1 iteration has found out is true.
2515 inst_to_choose = 1;
2516 break;
2517 }
2518 else if (insn_size == 4)
2519 {
2520 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2521 // Lets try scanning upper 2 bytes to verify this.
2522 instruction_list.Append(prev_insn);
2523 inst_to_choose = 2;
2524 }
2525 }
2526 else if (i == 3)
2527 {
2528 if (insn_size == 4)
2529 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2530 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2531 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2532 inst_to_choose = 0;
2533 else
2534 // This is straight-forward
2535 inst_to_choose = 2;
2536 break;
2537 }
2538 }
2539 else
2540 {
2541 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2542 if (i > 1)
2543 {
2544 inst_to_choose = i - 1;
2545 break;
2546 }
2547 }
2548 }
2549
2550 // Check if we are able to find any valid instruction.
2551 if (inst_to_choose)
2552 {
2553 if (inst_to_choose > instruction_list.GetSize())
2554 inst_to_choose--;
2555 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2556
2557 if (prev_insn->HasDelaySlot())
2558 {
2559 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2560 // Adjust the breakable address
2561 breakable_addr = addr - shift_size;
2562 if (log)
2563 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);
2564 }
2565 }
2566 break;
2567 }
2568 }
2569 return breakable_addr;
2570}
2571
Greg Clayton9585fbf2013-03-19 00:20:55 +00002572SourceManager &
2573Target::GetSourceManager ()
2574{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002575 if (!m_source_manager_ap)
Greg Clayton9585fbf2013-03-19 00:20:55 +00002576 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2577 return *m_source_manager_ap;
2578}
2579
Sean Callanan9998acd2014-12-05 01:21:59 +00002580ClangModulesDeclVendor *
2581Target::GetClangModulesDeclVendor ()
2582{
2583 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2584
2585 {
2586 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2587
2588 if (!m_clang_modules_decl_vendor_ap)
2589 {
2590 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2591 }
2592 }
2593
2594 return m_clang_modules_decl_vendor_ap.get();
2595}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002596
Greg Clayton44d93782014-01-27 23:43:24 +00002597Target::StopHookSP
2598Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002599{
2600 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002601 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2602 m_stop_hooks[new_uid] = stop_hook_sp;
2603 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002604}
2605
2606bool
2607Target::RemoveStopHookByID (lldb::user_id_t user_id)
2608{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002609 size_t num_removed = m_stop_hooks.erase(user_id);
2610 return (num_removed != 0);
Jim Ingham9575d842011-03-11 03:53:59 +00002611}
2612
2613void
2614Target::RemoveAllStopHooks ()
2615{
2616 m_stop_hooks.clear();
2617}
2618
2619Target::StopHookSP
2620Target::GetStopHookByID (lldb::user_id_t user_id)
2621{
2622 StopHookSP found_hook;
2623
2624 StopHookCollection::iterator specified_hook_iter;
2625 specified_hook_iter = m_stop_hooks.find (user_id);
2626 if (specified_hook_iter != m_stop_hooks.end())
2627 found_hook = (*specified_hook_iter).second;
2628 return found_hook;
2629}
2630
2631bool
2632Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2633{
2634 StopHookCollection::iterator specified_hook_iter;
2635 specified_hook_iter = m_stop_hooks.find (user_id);
2636 if (specified_hook_iter == m_stop_hooks.end())
2637 return false;
2638
2639 (*specified_hook_iter).second->SetIsActive (active_state);
2640 return true;
2641}
2642
2643void
2644Target::SetAllStopHooksActiveState (bool active_state)
2645{
2646 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2647 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2648 {
2649 (*pos).second->SetIsActive (active_state);
2650 }
2651}
2652
2653void
2654Target::RunStopHooks ()
2655{
Jim Ingham6026ca32011-05-12 02:06:14 +00002656 if (m_suppress_stop_hooks)
2657 return;
2658
Jim Ingham9575d842011-03-11 03:53:59 +00002659 if (!m_process_sp)
2660 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002661
2662 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2663 // since in that case we do not want to run the stop-hooks
2664 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2665 return;
2666
Jim Ingham9575d842011-03-11 03:53:59 +00002667 if (m_stop_hooks.empty())
2668 return;
2669
2670 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2671
2672 // If there aren't any active stop hooks, don't bother either:
2673 bool any_active_hooks = false;
2674 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2675 {
2676 if ((*pos).second->IsActive())
2677 {
2678 any_active_hooks = true;
2679 break;
2680 }
2681 }
2682 if (!any_active_hooks)
2683 return;
2684
2685 CommandReturnObject result;
2686
2687 std::vector<ExecutionContext> exc_ctx_with_reasons;
2688 std::vector<SymbolContext> sym_ctx_with_reasons;
2689
2690 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2691 size_t num_threads = cur_threadlist.GetSize();
2692 for (size_t i = 0; i < num_threads; i++)
2693 {
2694 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2695 if (cur_thread_sp->ThreadStoppedForAReason())
2696 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002697 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002698 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2699 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2700 }
2701 }
2702
2703 // If no threads stopped for a reason, don't run the stop-hooks.
2704 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2705 if (num_exe_ctx == 0)
2706 return;
2707
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002708 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2709 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002710
2711 bool keep_going = true;
2712 bool hooks_ran = false;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002713 bool print_hook_header = (m_stop_hooks.size() != 1);
2714 bool print_thread_header = (num_exe_ctx != 1);
Jim Ingham381e25b2011-03-22 01:47:27 +00002715
Jim Ingham9575d842011-03-11 03:53:59 +00002716 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2717 {
2718 // result.Clear();
2719 StopHookSP cur_hook_sp = (*pos).second;
2720 if (!cur_hook_sp->IsActive())
2721 continue;
2722
2723 bool any_thread_matched = false;
2724 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2725 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002726 if ((cur_hook_sp->GetSpecifier() == nullptr
Jim Ingham9575d842011-03-11 03:53:59 +00002727 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002728 && (cur_hook_sp->GetThreadSpecifier() == nullptr
Jim Ingham3d902922012-03-07 22:03:04 +00002729 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002730 {
2731 if (!hooks_ran)
2732 {
Jim Ingham9575d842011-03-11 03:53:59 +00002733 hooks_ran = true;
2734 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002735 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002736 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002737 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2738 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002739 nullptr);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002740 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002741 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002742 else
Daniel Malead01b2952012-11-29 21:49:15 +00002743 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002744 any_thread_matched = true;
2745 }
2746
Jim Ingham381e25b2011-03-22 01:47:27 +00002747 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002748 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002749
2750 CommandInterpreterRunOptions options;
2751 options.SetStopOnContinue (true);
2752 options.SetStopOnError (true);
2753 options.SetEchoCommands (false);
2754 options.SetPrintResults (true);
2755 options.SetAddToHistory (false);
2756
2757 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2758 &exc_ctx_with_reasons[i],
2759 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002760 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002761
2762 // If the command started the target going again, we should bag out of
2763 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002764 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2765 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002766 {
Daniel Malead01b2952012-11-29 21:49:15 +00002767 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002768 keep_going = false;
2769 }
2770 }
2771 }
2772 }
Jason Molenda879cf772011-09-23 00:42:55 +00002773
Caroline Tice969ed3d2011-05-02 20:41:46 +00002774 result.GetImmediateOutputStream()->Flush();
2775 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002776}
2777
Greg Claytonfbb76342013-11-20 21:07:01 +00002778const TargetPropertiesSP &
2779Target::GetGlobalProperties()
2780{
2781 static TargetPropertiesSP g_settings_sp;
Greg Clayton04df8ee2016-02-26 19:38:18 +00002782 static std::once_flag g_once_flag;
2783 std::call_once(g_once_flag, []() {
2784 if (!g_settings_sp)
2785 g_settings_sp.reset(new TargetProperties(nullptr));
2786 });
Greg Claytonfbb76342013-11-20 21:07:01 +00002787 return g_settings_sp;
2788}
2789
2790Error
2791Target::Install (ProcessLaunchInfo *launch_info)
2792{
2793 Error error;
2794 PlatformSP platform_sp (GetPlatform());
2795 if (platform_sp)
2796 {
2797 if (platform_sp->IsRemote())
2798 {
2799 if (platform_sp->IsConnected())
2800 {
2801 // Install all files that have an install path, and always install the
2802 // main executable when connected to a remote platform
2803 const ModuleList& modules = GetImages();
2804 const size_t num_images = modules.GetSize();
2805 for (size_t idx = 0; idx < num_images; ++idx)
2806 {
2807 const bool is_main_executable = idx == 0;
2808 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2809 if (module_sp)
2810 {
2811 FileSpec local_file (module_sp->GetFileSpec());
2812 if (local_file)
2813 {
2814 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2815 if (!remote_file)
2816 {
2817 if (is_main_executable) // TODO: add setting for always installing main executable???
2818 {
2819 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002820 remote_file = platform_sp->GetRemoteWorkingDirectory();
2821 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002822 }
2823 }
2824 if (remote_file)
2825 {
2826 error = platform_sp->Install(local_file, remote_file);
2827 if (error.Success())
2828 {
2829 module_sp->SetPlatformFileSpec(remote_file);
2830 if (is_main_executable)
2831 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002832 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002833 if (launch_info)
2834 launch_info->SetExecutableFile(remote_file, false);
2835 }
2836 }
2837 else
2838 break;
2839 }
2840 }
2841 }
2842 }
2843 }
2844 }
2845 }
2846 return error;
2847}
Greg Clayton7b242382011-07-08 00:48:09 +00002848
Greg Claytond5944cd2013-12-06 01:12:00 +00002849bool
2850Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2851{
2852 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2853}
2854
2855bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002856Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2857{
2858 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2859}
2860
2861bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002862Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2863{
2864 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2865 if (old_section_load_addr != new_section_load_addr)
2866 {
2867 uint32_t stop_id = 0;
2868 ProcessSP process_sp(GetProcessSP());
2869 if (process_sp)
2870 stop_id = process_sp->GetStopID();
2871 else
2872 stop_id = m_section_load_history.GetLastStopID();
2873 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2874 return true; // Return true if the section load address was changed...
2875 }
2876 return false; // Return false to indicate nothing changed
Greg Claytond5944cd2013-12-06 01:12:00 +00002877}
2878
Greg Clayton8012cad2014-11-17 19:39:20 +00002879size_t
2880Target::UnloadModuleSections (const ModuleList &module_list)
2881{
2882 size_t section_unload_count = 0;
2883 size_t num_modules = module_list.GetSize();
2884 for (size_t i=0; i<num_modules; ++i)
2885 {
2886 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2887 }
2888 return section_unload_count;
2889}
2890
2891size_t
2892Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2893{
2894 uint32_t stop_id = 0;
2895 ProcessSP process_sp(GetProcessSP());
2896 if (process_sp)
2897 stop_id = process_sp->GetStopID();
2898 else
2899 stop_id = m_section_load_history.GetLastStopID();
2900 SectionList *sections = module_sp->GetSectionList();
2901 size_t section_unload_count = 0;
2902 if (sections)
2903 {
2904 const uint32_t num_sections = sections->GetNumSections(0);
2905 for (uint32_t i = 0; i < num_sections; ++i)
2906 {
2907 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2908 }
2909 }
2910 return section_unload_count;
2911}
2912
Greg Claytond5944cd2013-12-06 01:12:00 +00002913bool
2914Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2915{
2916 uint32_t stop_id = 0;
2917 ProcessSP process_sp(GetProcessSP());
2918 if (process_sp)
2919 stop_id = process_sp->GetStopID();
2920 else
2921 stop_id = m_section_load_history.GetLastStopID();
2922 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2923}
2924
2925bool
2926Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2927{
2928 uint32_t stop_id = 0;
2929 ProcessSP process_sp(GetProcessSP());
2930 if (process_sp)
2931 stop_id = process_sp->GetStopID();
2932 else
2933 stop_id = m_section_load_history.GetLastStopID();
2934 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2935}
2936
2937void
2938Target::ClearAllLoadedSections ()
2939{
2940 m_section_load_history.Clear();
2941}
2942
Greg Claytonb09c5382013-12-13 17:20:18 +00002943Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002944Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002945{
2946 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002947 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2948
2949 if (log)
2950 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2951
Greg Claytonb09c5382013-12-13 17:20:18 +00002952 StateType state = eStateInvalid;
2953
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002954 // Scope to temporarily get the process state in case someone has manually
2955 // remotely connected already to a process and we can skip the platform
2956 // launching.
2957 {
2958 ProcessSP process_sp (GetProcessSP());
2959
2960 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002961 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002962 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002963 if (log)
2964 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2965 }
2966 else
2967 {
2968 if (log)
2969 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2970 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002971 }
2972
Greg Claytonb09c5382013-12-13 17:20:18 +00002973 launch_info.GetFlags().Set (eLaunchFlagDebug);
2974
2975 // Get the value of synchronous execution here. If you wait till after you have started to
2976 // run, then you could have hit a breakpoint, whose command might switch the value, and
2977 // then you'll pick up that incorrect value.
2978 Debugger &debugger = GetDebugger();
2979 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2980
2981 PlatformSP platform_sp (GetPlatform());
2982
2983 // Finalize the file actions, and if none were given, default to opening
2984 // up a pseudo terminal
2985 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002986 if (log)
2987 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2988 __FUNCTION__,
2989 platform_sp ? "true" : "false",
2990 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2991 default_to_use_pty ? "true" : "false");
2992
Greg Claytonb09c5382013-12-13 17:20:18 +00002993 launch_info.FinalizeFileActions (this, default_to_use_pty);
2994
2995 if (state == eStateConnected)
2996 {
2997 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
2998 {
2999 error.SetErrorString("can't launch in tty when launching through a remote connection");
3000 return error;
3001 }
3002 }
3003
3004 if (!launch_info.GetArchitecture().IsValid())
3005 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00003006
3007 // 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 +00003008 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
3009 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003010 if (log)
3011 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
3012
Greg Clayton5df78fa2015-05-23 03:54:53 +00003013 // Get a weak pointer to the previous process if we have one
3014 ProcessWP process_wp;
3015 if (m_process_sp)
3016 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00003017 m_process_sp = GetPlatform()->DebugProcess (launch_info,
3018 debugger,
3019 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00003020 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00003021
3022 // Cleanup the old process since someone might still have a strong
3023 // reference to this process and we would like to allow it to cleanup
3024 // as much as it can without the object being destroyed. We try to
3025 // lock the shared pointer and if that works, then someone else still
3026 // has a strong reference to the process.
3027
3028 ProcessSP old_process_sp(process_wp.lock());
3029 if (old_process_sp)
3030 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00003031 }
3032 else
3033 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003034 if (log)
3035 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
3036
Greg Claytonb09c5382013-12-13 17:20:18 +00003037 if (state == eStateConnected)
3038 {
3039 assert(m_process_sp);
3040 }
3041 else
3042 {
Todd Fiala015d8182014-07-22 23:41:36 +00003043 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00003044 const char *plugin_name = launch_info.GetProcessPluginName();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003045 CreateProcess(launch_info.GetListenerForProcess(debugger), plugin_name, nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00003046 }
Todd Fiala015d8182014-07-22 23:41:36 +00003047
3048 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00003049 if (m_process_sp)
3050 error = m_process_sp->Launch (launch_info);
3051 }
3052
3053 if (!m_process_sp)
3054 {
3055 if (error.Success())
3056 error.SetErrorString("failed to launch or debug process");
3057 return error;
3058 }
3059
3060 if (error.Success())
3061 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003062 if (synchronous_execution || !launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003063 {
Greg Clayton44d93782014-01-27 23:43:24 +00003064 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00003065 if (!hijack_listener_sp)
3066 {
3067 hijack_listener_sp.reset(new Listener("lldb.Target.Launch.hijack"));
3068 launch_info.SetHijackListener(hijack_listener_sp);
3069 m_process_sp->HijackProcessEvents(hijack_listener_sp.get());
3070 }
Todd Fialaac33cc92014-10-09 01:02:08 +00003071
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003072 StateType state = m_process_sp->WaitForProcessToStop(nullptr, nullptr, false, hijack_listener_sp.get(), nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00003073
3074 if (state == eStateStopped)
3075 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003076 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003077 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003078 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00003079 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003080 error = m_process_sp->PrivateResume();
3081 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00003082 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003083 state = m_process_sp->WaitForProcessToStop(nullptr, nullptr, true, hijack_listener_sp.get(), stream);
Ilia K064e69f2015-03-23 21:16:25 +00003084 const bool must_be_alive = false; // eStateExited is ok, so this must be false
3085 if (!StateIsStoppedState(state, must_be_alive))
3086 {
3087 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
3088 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003089 }
3090 }
Ilia K064e69f2015-03-23 21:16:25 +00003091 else
3092 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003093 m_process_sp->RestoreProcessEvents();
3094 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00003095 }
3096 if (!error.Success())
3097 {
Ilia K064e69f2015-03-23 21:16:25 +00003098 Error error2;
3099 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
3100 error = error2;
3101 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003102 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003103 }
Greg Clayton40286e02014-04-30 20:29:09 +00003104 else if (state == eStateExited)
3105 {
Zachary Turner10687b02014-10-20 17:46:43 +00003106 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00003107 const int exit_status = m_process_sp->GetExitStatus();
3108 const char *exit_desc = m_process_sp->GetExitDescription();
3109#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'."
3110 if (exit_desc && exit_desc[0])
3111 {
3112 if (with_shell)
3113 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
3114 else
3115 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
3116 }
3117 else
3118 {
3119 if (with_shell)
3120 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
3121 else
3122 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
3123 }
3124 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003125 else
3126 {
3127 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
3128 }
3129 }
Greg Clayton44d93782014-01-27 23:43:24 +00003130 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00003131 }
3132 else
3133 {
Greg Clayton44d93782014-01-27 23:43:24 +00003134 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003135 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003136 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003137 }
3138 return error;
3139}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003140
3141Error
3142Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3143{
3144 auto state = eStateInvalid;
3145 auto process_sp = GetProcessSP ();
3146 if (process_sp)
3147 {
3148 state = process_sp->GetState ();
3149 if (process_sp->IsAlive () && state != eStateConnected)
3150 {
3151 if (state == eStateAttaching)
3152 return Error ("process attach is in progress");
3153 return Error ("a process is already being debugged");
3154 }
3155 }
3156
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003157 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3158
3159 // If no process info was specified, then use the target executable
3160 // name as the process to attach to by default
3161 if (!attach_info.ProcessInfoSpecified ())
3162 {
3163 if (old_exec_module_sp)
3164 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3165
3166 if (!attach_info.ProcessInfoSpecified ())
3167 {
3168 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3169 }
3170 }
3171
3172 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
Greg Claytonb3788ea2015-10-05 22:58:37 +00003173 ListenerSP hijack_listener_sp;
3174 const bool async = attach_info.GetAsync();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003175 if (!async)
Greg Claytonb3788ea2015-10-05 22:58:37 +00003176 {
3177 hijack_listener_sp.reset (new Listener ("lldb.Target.Attach.attach.hijack"));
3178 attach_info.SetHijackListener (hijack_listener_sp);
3179 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003180
3181 Error error;
3182 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3183 {
3184 SetPlatform (platform_sp);
3185 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3186 }
3187 else
3188 {
3189 if (state != eStateConnected)
3190 {
3191 const char *plugin_name = attach_info.GetProcessPluginName ();
3192 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3193 if (process_sp == nullptr)
3194 {
3195 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3196 return error;
3197 }
3198 }
Greg Claytonb3788ea2015-10-05 22:58:37 +00003199 if (hijack_listener_sp)
3200 process_sp->HijackProcessEvents (hijack_listener_sp.get ());
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003201 error = process_sp->Attach (attach_info);
3202 }
3203
Greg Claytone75e5d82015-10-20 00:14:20 +00003204 if (error.Success () && process_sp)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003205 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003206 if (async)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003207 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003208 process_sp->RestoreProcessEvents ();
3209 }
3210 else
3211 {
3212 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener ().get (), stream);
3213 process_sp->RestoreProcessEvents ();
3214
3215 if (state != eStateStopped)
3216 {
3217 const char *exit_desc = process_sp->GetExitDescription ();
3218 if (exit_desc)
3219 error.SetErrorStringWithFormat ("%s", exit_desc);
3220 else
3221 error.SetErrorString ("process did not stop (no such process or permission problem?)");
3222 process_sp->Destroy (false);
3223 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003224 }
3225 }
3226 return error;
3227}
3228
Jim Ingham9575d842011-03-11 03:53:59 +00003229//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003230// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003231//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003232Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3233 UserID (uid),
3234 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003235 m_commands (),
3236 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003237 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003238 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003239{
3240}
3241
3242Target::StopHook::StopHook (const StopHook &rhs) :
3243 UserID (rhs.GetID()),
3244 m_target_sp (rhs.m_target_sp),
3245 m_commands (rhs.m_commands),
3246 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003247 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003248 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003249{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003250 if (rhs.m_thread_spec_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00003251 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3252}
3253
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003254Target::StopHook::~StopHook() = default;
Jim Ingham9575d842011-03-11 03:53:59 +00003255
3256void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003257Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3258{
3259 m_specifier_sp.reset(specifier);
3260}
3261
3262void
Jim Ingham9575d842011-03-11 03:53:59 +00003263Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3264{
3265 m_thread_spec_ap.reset (specifier);
3266}
Jim Ingham9575d842011-03-11 03:53:59 +00003267
3268void
3269Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3270{
3271 int indent_level = s->GetIndentLevel();
3272
3273 s->SetIndentLevel(indent_level + 2);
3274
Daniel Malead01b2952012-11-29 21:49:15 +00003275 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003276 if (m_active)
3277 s->Indent ("State: enabled\n");
3278 else
3279 s->Indent ("State: disabled\n");
3280
3281 if (m_specifier_sp)
3282 {
3283 s->Indent();
3284 s->PutCString ("Specifier:\n");
3285 s->SetIndentLevel (indent_level + 4);
3286 m_specifier_sp->GetDescription (s, level);
3287 s->SetIndentLevel (indent_level + 2);
3288 }
3289
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003290 if (m_thread_spec_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00003291 {
3292 StreamString tmp;
3293 s->Indent("Thread:\n");
3294 m_thread_spec_ap->GetDescription (&tmp, level);
3295 s->SetIndentLevel (indent_level + 4);
3296 s->Indent (tmp.GetData());
3297 s->PutCString ("\n");
3298 s->SetIndentLevel (indent_level + 2);
3299 }
3300
3301 s->Indent ("Commands: \n");
3302 s->SetIndentLevel (indent_level + 4);
3303 uint32_t num_commands = m_commands.GetSize();
3304 for (uint32_t i = 0; i < num_commands; i++)
3305 {
3306 s->Indent(m_commands.GetStringAtIndex(i));
3307 s->PutCString ("\n");
3308 }
3309 s->SetIndentLevel (indent_level);
3310}
3311
Greg Clayton67cc0632012-08-22 17:17:09 +00003312//--------------------------------------------------------------
3313// class TargetProperties
3314//--------------------------------------------------------------
3315
3316OptionEnumValueElement
3317lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003318{
Greg Clayton67cc0632012-08-22 17:17:09 +00003319 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3320 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3321 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003322 { 0, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +00003323};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003324
Greg Clayton1f746072012-08-29 21:13:06 +00003325static OptionEnumValueElement
3326g_inline_breakpoint_enums[] =
3327{
3328 { 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."},
3329 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3330 { eInlineBreakpointsAlways, "always", "Always look for inline breakpoint locations when setting file and line breakpoints (slower but most accurate)."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003331 { 0, nullptr, nullptr }
Greg Clayton1f746072012-08-29 21:13:06 +00003332};
3333
Jim Ingham0f063ba2013-03-02 00:26:47 +00003334typedef enum x86DisassemblyFlavor
3335{
3336 eX86DisFlavorDefault,
3337 eX86DisFlavorIntel,
3338 eX86DisFlavorATT
3339} x86DisassemblyFlavor;
3340
3341static OptionEnumValueElement
3342g_x86_dis_flavor_value_types[] =
3343{
3344 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3345 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3346 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003347 { 0, nullptr, nullptr }
Jim Ingham0f063ba2013-03-02 00:26:47 +00003348};
3349
Enrico Granata397ddd52013-05-21 20:13:34 +00003350static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003351g_hex_immediate_style_values[] =
3352{
3353 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3354 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003355 { 0, nullptr, nullptr }
Daniel Malead79ae052013-08-07 21:54:09 +00003356};
3357
3358static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003359g_load_script_from_sym_file_values[] =
3360{
3361 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3362 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3363 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003364 { 0, nullptr, nullptr }
Enrico Granata397ddd52013-05-21 20:13:34 +00003365};
3366
Greg Claytonfd814c52013-08-13 01:42:25 +00003367static OptionEnumValueElement
Jason Molenda878ae012016-02-19 00:05:17 +00003368g_load_current_working_dir_lldbinit_values[] =
3369{
3370 { eLoadCWDlldbinitTrue, "true", "Load .lldbinit files from current directory"},
3371 { eLoadCWDlldbinitFalse, "false", "Do not load .lldbinit files from current directory"},
3372 { eLoadCWDlldbinitWarn, "warn", "Warn about loading .lldbinit files from current directory"},
3373 { 0, nullptr, nullptr }
3374};
3375
3376static OptionEnumValueElement
Greg Claytonfd814c52013-08-13 01:42:25 +00003377g_memory_module_load_level_values[] =
3378{
Greg Clayton86eac942013-08-13 21:32:34 +00003379 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003380 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3381 { eMemoryModuleLoadLevelComplete, "complete", "Load complete information when loading modules from memory. Currently this setting loads sections and all symbols."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003382 { 0, nullptr, nullptr }
Greg Claytonfd814c52013-08-13 01:42:25 +00003383};
3384
Greg Clayton67cc0632012-08-22 17:17:09 +00003385static PropertyDefinition
3386g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003387{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003388 { "default-arch" , OptionValue::eTypeArch , true , 0 , nullptr, nullptr, "Default architecture to choose, when there's a choice." },
3389 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Move breakpoints to nearest code." },
3390 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , nullptr, nullptr, "The language to use when interpreting expressions entered in commands." },
3391 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "Path to a file containing expressions to be prepended to all expressions." },
3392 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eDynamicDontRunTarget , nullptr, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
3393 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Should synthetic values be used by default whenever available." },
3394 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Skip function prologues when setting breakpoints by name." },
3395 { "source-map" , OptionValue::eTypePathMap , false, 0 , nullptr, nullptr, "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 +00003396 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3397 "some part (starting at the root) of the path to the file when it was built, "
3398 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3399 "Each element of the array is checked in order and the first one that results in a match wins." },
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003400 { "exec-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , nullptr, nullptr, "Executable search paths to use when locating executable files whose paths don't match the local file system." },
3401 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , nullptr, nullptr, "List of directories to be searched when locating debug symbol files." },
3402 { "clang-module-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , nullptr, nullptr, "List of directories to be searched when locating modules for Clang." },
Sean Callananb0300a42016-01-14 21:46:09 +00003403 { "auto-import-clang-modules" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Automatically load Clang modules referred to by the program." },
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003404 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , nullptr, nullptr, "Maximum number of children to expand in any level of depth." },
3405 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , nullptr, nullptr, "Maximum number of characters to show when using %s in summary strings." },
3406 { "max-memory-read-size" , OptionValue::eTypeSInt64 , false, 1024 , nullptr, nullptr, "Maximum number of bytes that 'memory read' will fetch before --force must be specified." },
3407 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Consult the platform module avoid list when setting non-module specific breakpoints." },
3408 { "arg0" , OptionValue::eTypeString , false, 0 , nullptr, nullptr, "The first argument passed to the program in the argument array which can be different from the executable itself." },
3409 { "run-args" , OptionValue::eTypeArgs , false, 0 , nullptr, nullptr, "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." },
3410 { "env-vars" , OptionValue::eTypeDictionary, false, OptionValue::eTypeString , nullptr, nullptr, "A list of all the environment variables to be passed to the executable's environment, and their values." },
3411 { "inherit-env" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Inherit the environment from the process that is running LLDB." },
3412 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for reading its standard input." },
3413 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for writing its standard output." },
3414 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for writing its standard error." },
3415 { "detach-on-error" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "debugserver will detach (rather than killing) a process if it loses connection with lldb." },
3416 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Disable Address Space Layout Randomization (ASLR)" },
3417 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , nullptr, nullptr, "Disable stdin/stdout for process (e.g. for a GUI application)" },
3418 { "inline-breakpoint-strategy" , OptionValue::eTypeEnum , false, eInlineBreakpointsAlways , nullptr, g_inline_breakpoint_enums, "The strategy to use when settings breakpoints by file and line. "
Greg Clayton1f746072012-08-29 21:13:06 +00003419 "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 +00003420 "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 +00003421 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003422 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3423 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3424 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003425 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003426 // 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.
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003427 { "x86-disassembly-flavor" , OptionValue::eTypeEnum , false, eX86DisFlavorDefault, nullptr, g_x86_dis_flavor_value_types, "The default disassembly flavor to use for x86 or x86-64 targets." },
3428 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, nullptr, nullptr, "Show immediates in disassembly as hexadecimal." },
3429 { "hex-immediate-style" , OptionValue::eTypeEnum , false, Disassembler::eHexStyleC, nullptr, g_hex_immediate_style_values, "Which style to use for printing hexadecimal disassembly values." },
3430 { "use-fast-stepping" , OptionValue::eTypeBoolean , false, true, nullptr, nullptr, "Use a fast stepping algorithm based on running from branch to branch rather than instruction single-stepping." },
3431 { "load-script-from-symbol-file" , OptionValue::eTypeEnum , false, eLoadScriptFromSymFileWarn, nullptr, g_load_script_from_sym_file_values, "Allow LLDB to load scripting resources embedded in symbol files when available." },
Jason Molenda878ae012016-02-19 00:05:17 +00003432 { "load-cwd-lldbinit" , OptionValue::eTypeEnum , false, eLoadCWDlldbinitWarn, nullptr, g_load_current_working_dir_lldbinit_values, "Allow LLDB to .lldbinit files from the current directory automatically." },
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003433 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, nullptr, g_memory_module_load_level_values,
Greg Clayton86eac942013-08-13 21:32:34 +00003434 "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. "
3435 "This setting helps users control how much information gets loaded when loading modules from memory."
3436 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3437 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3438 "'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). " },
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003439 { "display-expression-in-crashlogs" , OptionValue::eTypeBoolean , false, false, nullptr, nullptr, "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." },
3440 { "trap-handler-names" , OptionValue::eTypeArray , true, OptionValue::eTypeString, nullptr, nullptr, "A list of trap handler function names, e.g. a common Unix user process one is _sigtramp." },
3441 { "display-runtime-support-values" , OptionValue::eTypeBoolean , false, false, nullptr, nullptr, "If true, LLDB will show variables that are meant to support the operation of a language's runtime support." },
3442 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, nullptr, nullptr, "Disable lock-step debugging, instead control threads independently." },
3443 { nullptr , OptionValue::eTypeInvalid , false, 0 , nullptr, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +00003444};
Enrico Granata560558e2015-02-11 02:35:39 +00003445
Greg Clayton67cc0632012-08-22 17:17:09 +00003446enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003447{
Greg Clayton67cc0632012-08-22 17:17:09 +00003448 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003449 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003450 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003451 ePropertyExprPrefix,
3452 ePropertyPreferDynamic,
3453 ePropertyEnableSynthetic,
3454 ePropertySkipPrologue,
3455 ePropertySourceMap,
3456 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003457 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003458 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003459 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003460 ePropertyMaxChildrenCount,
3461 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003462 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003463 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003464 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003465 ePropertyRunArgs,
3466 ePropertyEnvVars,
3467 ePropertyInheritEnv,
3468 ePropertyInputPath,
3469 ePropertyOutputPath,
3470 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003471 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003472 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003473 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003474 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003475 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003476 ePropertyUseHexImmediates,
3477 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003478 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003479 ePropertyLoadScriptFromSymbolFile,
Jason Molenda878ae012016-02-19 00:05:17 +00003480 ePropertyLoadCWDlldbinitFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003481 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003482 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003483 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003484 ePropertyDisplayRuntimeSupportValues,
3485 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003486};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003487
Greg Clayton67cc0632012-08-22 17:17:09 +00003488class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003489{
Greg Clayton67cc0632012-08-22 17:17:09 +00003490public:
3491 TargetOptionValueProperties (const ConstString &name) :
3492 OptionValueProperties (name),
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003493 m_target(nullptr),
Greg Clayton67cc0632012-08-22 17:17:09 +00003494 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003495 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003496 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003497
Greg Clayton67cc0632012-08-22 17:17:09 +00003498 // This constructor is used when creating TargetOptionValueProperties when it
3499 // is part of a new lldb_private::Target instance. It will copy all current
3500 // global property values as needed
3501 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3502 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3503 m_target (target),
3504 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003505 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003506 }
3507
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003508 const Property *
3509 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +00003510 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003511 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003512 // try and grab the setting from the current target if there is one. Else we just
3513 // use the one from this instance.
3514 if (idx == ePropertyEnvVars)
3515 GetHostEnvironmentIfNeeded ();
3516
3517 if (exe_ctx)
3518 {
3519 Target *target = exe_ctx->GetTargetPtr();
3520 if (target)
3521 {
3522 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3523 if (this != target_properties)
3524 return target_properties->ProtectedGetPropertyAtIndex (idx);
3525 }
3526 }
3527 return ProtectedGetPropertyAtIndex (idx);
3528 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003529
3530 lldb::TargetSP
3531 GetTargetSP ()
3532 {
3533 return m_target->shared_from_this();
3534 }
3535
Greg Clayton67cc0632012-08-22 17:17:09 +00003536protected:
Greg Clayton67cc0632012-08-22 17:17:09 +00003537 void
3538 GetHostEnvironmentIfNeeded () const
3539 {
3540 if (!m_got_host_env)
3541 {
3542 if (m_target)
3543 {
3544 m_got_host_env = true;
3545 const uint32_t idx = ePropertyInheritEnv;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003546 if (GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0))
Greg Clayton67cc0632012-08-22 17:17:09 +00003547 {
3548 PlatformSP platform_sp (m_target->GetPlatform());
3549 if (platform_sp)
3550 {
3551 StringList env;
3552 if (platform_sp->GetEnvironment(env))
3553 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003554 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary(nullptr, ePropertyEnvVars);
Greg Clayton67cc0632012-08-22 17:17:09 +00003555 if (env_dict)
3556 {
3557 const bool can_replace = false;
3558 const size_t envc = env.GetSize();
3559 for (size_t idx=0; idx<envc; idx++)
3560 {
3561 const char *env_entry = env.GetStringAtIndex (idx);
3562 if (env_entry)
3563 {
3564 const char *equal_pos = ::strchr(env_entry, '=');
3565 ConstString key;
3566 // It is ok to have environment variables with no values
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003567 const char *value = nullptr;
Greg Clayton67cc0632012-08-22 17:17:09 +00003568 if (equal_pos)
3569 {
3570 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3571 if (equal_pos[1])
3572 value = equal_pos + 1;
3573 }
3574 else
3575 {
3576 key.SetCString(env_entry);
3577 }
3578 // Don't allow existing keys to be replaced with ones we get from the platform environment
3579 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3580 }
3581 }
3582 }
3583 }
3584 }
3585 }
3586 }
3587 }
3588 }
3589 Target *m_target;
3590 mutable bool m_got_host_env;
3591};
3592
Greg Claytonfbb76342013-11-20 21:07:01 +00003593//----------------------------------------------------------------------
3594// TargetProperties
3595//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003596TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003597 Properties (),
3598 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003599{
3600 if (target)
3601 {
3602 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003603
3604 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3605 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3606 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3607 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3608 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3609 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3610 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3611 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3612 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3613 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3614
3615 // Update m_launch_info once it was created
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003616 Arg0ValueChangedCallback(this, nullptr);
3617 RunArgsValueChangedCallback(this, nullptr);
3618 //EnvVarsValueChangedCallback(this, nullptr); // FIXME: cause segfault in Target::GetPlatform()
3619 InputPathValueChangedCallback(this, nullptr);
3620 OutputPathValueChangedCallback(this, nullptr);
3621 ErrorPathValueChangedCallback(this, nullptr);
3622 DetachOnErrorValueChangedCallback(this, nullptr);
3623 DisableASLRValueChangedCallback(this, nullptr);
3624 DisableSTDIOValueChangedCallback(this, nullptr);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003625 }
3626 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003627 {
3628 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3629 m_collection_sp->Initialize(g_properties);
3630 m_collection_sp->AppendProperty(ConstString("process"),
3631 ConstString("Settings specify to processes."),
3632 true,
3633 Process::GetGlobalProperties()->GetValueProperties());
3634 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003635}
3636
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003637TargetProperties::~TargetProperties() = default;
3638
Greg Clayton67cc0632012-08-22 17:17:09 +00003639ArchSpec
3640TargetProperties::GetDefaultArchitecture () const
3641{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003642 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(nullptr, ePropertyDefaultArch);
Greg Clayton67cc0632012-08-22 17:17:09 +00003643 if (value)
3644 return value->GetCurrentValue();
3645 return ArchSpec();
3646}
3647
3648void
3649TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3650{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003651 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(nullptr, ePropertyDefaultArch);
Greg Clayton67cc0632012-08-22 17:17:09 +00003652 if (value)
3653 return value->SetCurrentValue(arch, true);
3654}
3655
Ilia K055ad9b2015-05-18 13:41:01 +00003656bool
3657TargetProperties::GetMoveToNearestCode() const
3658{
3659 const uint32_t idx = ePropertyMoveToNearestCode;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003660 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Ilia K055ad9b2015-05-18 13:41:01 +00003661}
3662
Greg Clayton67cc0632012-08-22 17:17:09 +00003663lldb::DynamicValueType
3664TargetProperties::GetPreferDynamicValue() const
3665{
3666 const uint32_t idx = ePropertyPreferDynamic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003667 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003668}
3669
3670bool
Greg Clayton15484402015-05-15 18:40:24 +00003671TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3672{
3673 const uint32_t idx = ePropertyPreferDynamic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003674 return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, d);
Greg Clayton15484402015-05-15 18:40:24 +00003675}
3676
Greg Clayton15484402015-05-15 18:40:24 +00003677bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003678TargetProperties::GetDisableASLR () const
3679{
3680 const uint32_t idx = ePropertyDisableASLR;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003681 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003682}
3683
3684void
3685TargetProperties::SetDisableASLR (bool b)
3686{
3687 const uint32_t idx = ePropertyDisableASLR;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003688 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003689}
3690
3691bool
Jim Ingham106d0282014-06-25 02:32:56 +00003692TargetProperties::GetDetachOnError () const
3693{
3694 const uint32_t idx = ePropertyDetachOnError;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003695 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham106d0282014-06-25 02:32:56 +00003696}
3697
3698void
3699TargetProperties::SetDetachOnError (bool b)
3700{
3701 const uint32_t idx = ePropertyDetachOnError;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003702 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Jim Ingham106d0282014-06-25 02:32:56 +00003703}
3704
3705bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003706TargetProperties::GetDisableSTDIO () const
3707{
3708 const uint32_t idx = ePropertyDisableSTDIO;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003709 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003710}
3711
3712void
3713TargetProperties::SetDisableSTDIO (bool b)
3714{
3715 const uint32_t idx = ePropertyDisableSTDIO;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003716 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003717}
3718
Jim Ingham0f063ba2013-03-02 00:26:47 +00003719const char *
3720TargetProperties::GetDisassemblyFlavor () const
3721{
3722 const uint32_t idx = ePropertyDisassemblyFlavor;
3723 const char *return_value;
3724
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003725 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Jim Ingham0f063ba2013-03-02 00:26:47 +00003726 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3727 return return_value;
3728}
3729
Greg Clayton1f746072012-08-29 21:13:06 +00003730InlineStrategy
3731TargetProperties::GetInlineStrategy () const
3732{
3733 const uint32_t idx = ePropertyInlineStrategy;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003734 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton1f746072012-08-29 21:13:06 +00003735}
3736
Greg Clayton45392552012-10-17 22:57:12 +00003737const char *
3738TargetProperties::GetArg0 () const
3739{
3740 const uint32_t idx = ePropertyArg0;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003741 return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, nullptr);
Greg Clayton45392552012-10-17 22:57:12 +00003742}
3743
3744void
3745TargetProperties::SetArg0 (const char *arg)
3746{
3747 const uint32_t idx = ePropertyArg0;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003748 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003749 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003750}
3751
Greg Clayton67cc0632012-08-22 17:17:09 +00003752bool
3753TargetProperties::GetRunArguments (Args &args) const
3754{
3755 const uint32_t idx = ePropertyRunArgs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003756 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Greg Clayton67cc0632012-08-22 17:17:09 +00003757}
3758
3759void
3760TargetProperties::SetRunArguments (const Args &args)
3761{
3762 const uint32_t idx = ePropertyRunArgs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003763 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003764 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003765}
3766
3767size_t
3768TargetProperties::GetEnvironmentAsArgs (Args &env) const
3769{
3770 const uint32_t idx = ePropertyEnvVars;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003771 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, env);
Greg Clayton67cc0632012-08-22 17:17:09 +00003772}
3773
Ilia K8f37ca52015-02-13 14:31:06 +00003774void
3775TargetProperties::SetEnvironmentFromArgs (const Args &env)
3776{
3777 const uint32_t idx = ePropertyEnvVars;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003778 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003779 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003780}
3781
Greg Clayton67cc0632012-08-22 17:17:09 +00003782bool
3783TargetProperties::GetSkipPrologue() const
3784{
3785 const uint32_t idx = ePropertySkipPrologue;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003786 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003787}
3788
3789PathMappingList &
3790TargetProperties::GetSourcePathMap () const
3791{
3792 const uint32_t idx = ePropertySourceMap;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003793 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(nullptr, false, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003794 assert(option_value);
3795 return option_value->GetCurrentValue();
3796}
3797
3798FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003799TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003800{
3801 const uint32_t idx = ePropertyExecutableSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003802 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003803 assert(option_value);
3804 return option_value->GetCurrentValue();
3805}
3806
Michael Sartaina7499c92013-07-01 19:45:50 +00003807FileSpecList &
3808TargetProperties::GetDebugFileSearchPaths ()
3809{
3810 const uint32_t idx = ePropertyDebugFileSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003811 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Michael Sartaina7499c92013-07-01 19:45:50 +00003812 assert(option_value);
3813 return option_value->GetCurrentValue();
3814}
3815
Sean Callanan85054342015-04-03 15:39:47 +00003816FileSpecList &
3817TargetProperties::GetClangModuleSearchPaths ()
3818{
3819 const uint32_t idx = ePropertyClangModuleSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003820 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Sean Callanan85054342015-04-03 15:39:47 +00003821 assert(option_value);
3822 return option_value->GetCurrentValue();
3823}
3824
Greg Clayton67cc0632012-08-22 17:17:09 +00003825bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003826TargetProperties::GetEnableAutoImportClangModules() const
3827{
3828 const uint32_t idx = ePropertyAutoImportClangModules;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003829 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003830}
3831
3832bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003833TargetProperties::GetEnableSyntheticValue () const
3834{
3835 const uint32_t idx = ePropertyEnableSynthetic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003836 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003837}
3838
3839uint32_t
3840TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3841{
3842 const uint32_t idx = ePropertyMaxChildrenCount;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003843 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003844}
3845
3846uint32_t
3847TargetProperties::GetMaximumSizeOfStringSummary() const
3848{
3849 const uint32_t idx = ePropertyMaxSummaryLength;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003850 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003851}
3852
Enrico Granatad325bf92013-06-04 22:54:16 +00003853uint32_t
3854TargetProperties::GetMaximumMemReadSize () const
3855{
3856 const uint32_t idx = ePropertyMaxMemReadSize;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003857 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granatad325bf92013-06-04 22:54:16 +00003858}
3859
Greg Clayton67cc0632012-08-22 17:17:09 +00003860FileSpec
3861TargetProperties::GetStandardInputPath () const
3862{
3863 const uint32_t idx = ePropertyInputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003864 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003865}
3866
3867void
3868TargetProperties::SetStandardInputPath (const char *p)
3869{
3870 const uint32_t idx = ePropertyInputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003871 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003872}
3873
3874FileSpec
3875TargetProperties::GetStandardOutputPath () const
3876{
3877 const uint32_t idx = ePropertyOutputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003878 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003879}
3880
3881void
3882TargetProperties::SetStandardOutputPath (const char *p)
3883{
3884 const uint32_t idx = ePropertyOutputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003885 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003886}
3887
3888FileSpec
3889TargetProperties::GetStandardErrorPath () const
3890{
3891 const uint32_t idx = ePropertyErrorPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003892 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003893}
3894
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003895LanguageType
3896TargetProperties::GetLanguage () const
3897{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003898 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage(nullptr, ePropertyLanguage);
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003899 if (value)
3900 return value->GetCurrentValue();
3901 return LanguageType();
3902}
3903
Greg Clayton6920b522012-08-22 18:39:03 +00003904const char *
3905TargetProperties::GetExpressionPrefixContentsAsCString ()
3906{
3907 const uint32_t idx = ePropertyExprPrefix;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003908 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false, idx);
Greg Clayton6920b522012-08-22 18:39:03 +00003909 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003910 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003911 const bool null_terminate = true;
3912 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003913 if (data_sp)
3914 return (const char *) data_sp->GetBytes();
3915 }
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003916 return nullptr;
Greg Clayton6920b522012-08-22 18:39:03 +00003917}
3918
Greg Clayton67cc0632012-08-22 17:17:09 +00003919void
3920TargetProperties::SetStandardErrorPath (const char *p)
3921{
3922 const uint32_t idx = ePropertyErrorPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003923 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003924}
3925
3926bool
3927TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3928{
3929 const uint32_t idx = ePropertyBreakpointUseAvoidList;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003930 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003931}
3932
Jim Ingham17d023f2013-03-13 17:58:04 +00003933bool
Daniel Malead79ae052013-08-07 21:54:09 +00003934TargetProperties::GetUseHexImmediates () const
3935{
3936 const uint32_t idx = ePropertyUseHexImmediates;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003937 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Daniel Malead79ae052013-08-07 21:54:09 +00003938}
3939
3940bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003941TargetProperties::GetUseFastStepping () const
3942{
3943 const uint32_t idx = ePropertyUseFastStepping;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003944 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham17d023f2013-03-13 17:58:04 +00003945}
3946
Greg Claytonfb6621e2013-12-06 21:59:52 +00003947bool
3948TargetProperties::GetDisplayExpressionsInCrashlogs () const
3949{
3950 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003951 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Claytonfb6621e2013-12-06 21:59:52 +00003952}
3953
Enrico Granata397ddd52013-05-21 20:13:34 +00003954LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003955TargetProperties::GetLoadScriptFromSymbolFile () const
3956{
3957 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003958 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003959}
3960
Jason Molenda878ae012016-02-19 00:05:17 +00003961LoadCWDlldbinitFile
3962TargetProperties::GetLoadCWDlldbinitFile () const
3963{
3964 const uint32_t idx = ePropertyLoadCWDlldbinitFile;
3965 return (LoadCWDlldbinitFile) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
3966}
3967
Daniel Malead79ae052013-08-07 21:54:09 +00003968Disassembler::HexImmediateStyle
3969TargetProperties::GetHexImmediateStyle () const
3970{
3971 const uint32_t idx = ePropertyHexImmediateStyle;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003972 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Daniel Malead79ae052013-08-07 21:54:09 +00003973}
3974
Greg Claytonfd814c52013-08-13 01:42:25 +00003975MemoryModuleLoadLevel
3976TargetProperties::GetMemoryModuleLoadLevel() const
3977{
3978 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003979 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Claytonfd814c52013-08-13 01:42:25 +00003980}
3981
Jason Molendaa4bea722014-02-14 05:06:49 +00003982bool
3983TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3984{
3985 const uint32_t idx = ePropertyTrapHandlerNames;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003986 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00003987}
Greg Claytonfd814c52013-08-13 01:42:25 +00003988
Jason Molendaa4bea722014-02-14 05:06:49 +00003989void
3990TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3991{
3992 const uint32_t idx = ePropertyTrapHandlerNames;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003993 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00003994}
Greg Clayton67cc0632012-08-22 17:17:09 +00003995
Enrico Granata560558e2015-02-11 02:35:39 +00003996bool
3997TargetProperties::GetDisplayRuntimeSupportValues () const
3998{
3999 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004000 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Enrico Granata560558e2015-02-11 02:35:39 +00004001}
4002
4003void
4004TargetProperties::SetDisplayRuntimeSupportValues (bool b)
4005{
4006 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004007 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Enrico Granata560558e2015-02-11 02:35:39 +00004008}
4009
Ewan Crawford78baa192015-05-13 09:18:18 +00004010bool
4011TargetProperties::GetNonStopModeEnabled () const
4012{
4013 const uint32_t idx = ePropertyNonStopModeEnabled;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004014 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Ewan Crawford78baa192015-05-13 09:18:18 +00004015}
4016
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004017void
4018TargetProperties::SetNonStopModeEnabled (bool b)
4019{
4020 const uint32_t idx = ePropertyNonStopModeEnabled;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004021 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004022}
4023
Ilia K8f37ca52015-02-13 14:31:06 +00004024const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00004025TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00004026{
Ilia Kcc39d3f2015-02-13 17:07:55 +00004027 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00004028 return m_launch_info;
4029}
4030
4031void
4032TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
4033{
4034 m_launch_info = launch_info;
4035 SetArg0(launch_info.GetArg0());
4036 SetRunArguments(launch_info.GetArguments());
4037 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
4038 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
4039 if (input_file_action)
4040 {
4041 const char *input_path = input_file_action->GetPath();
4042 if (input_path)
4043 SetStandardInputPath(input_path);
4044 }
4045 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
4046 if (output_file_action)
4047 {
4048 const char *output_path = output_file_action->GetPath();
4049 if (output_path)
4050 SetStandardOutputPath(output_path);
4051 }
4052 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
4053 if (error_file_action)
4054 {
4055 const char *error_path = error_file_action->GetPath();
4056 if (error_path)
4057 SetStandardErrorPath(error_path);
4058 }
4059 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
4060 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4061 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
4062}
4063
4064void
4065TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
4066{
4067 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4068 this_->m_launch_info.SetArg0(this_->GetArg0());
4069}
4070
4071void
4072TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
4073{
4074 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4075 Args args;
4076 if (this_->GetRunArguments(args))
4077 this_->m_launch_info.GetArguments() = args;
4078}
4079
4080void
4081TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
4082{
4083 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4084 Args args;
4085 if (this_->GetEnvironmentAsArgs(args))
4086 this_->m_launch_info.GetEnvironmentEntries() = args;
4087}
4088
4089void
4090TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4091{
4092 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004093 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004094}
4095
4096void
4097TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4098{
4099 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004100 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004101}
4102
4103void
4104TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4105{
4106 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004107 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004108}
4109
4110void
4111TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
4112{
4113 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4114 if (this_->GetDetachOnError())
4115 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4116 else
4117 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
4118}
4119
4120void
4121TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
4122{
4123 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4124 if (this_->GetDisableASLR())
4125 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4126 else
4127 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
4128}
4129
4130void
4131TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
4132{
4133 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4134 if (this_->GetDisableSTDIO())
4135 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4136 else
4137 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4138}
Ilia Keb2c19a2015-03-10 21:59:55 +00004139
4140//----------------------------------------------------------------------
4141// Target::TargetEventData
4142//----------------------------------------------------------------------
4143
4144Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
4145 EventData (),
4146 m_target_sp (target_sp),
4147 m_module_list ()
4148{
4149}
4150
4151Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
4152 EventData (),
4153 m_target_sp (target_sp),
4154 m_module_list (module_list)
4155{
4156}
4157
Eugene Zelenko8f30a652015-10-23 18:39:37 +00004158Target::TargetEventData::~TargetEventData() = default;
Ilia Keb2c19a2015-03-10 21:59:55 +00004159
4160const ConstString &
4161Target::TargetEventData::GetFlavorString ()
4162{
4163 static ConstString g_flavor ("Target::TargetEventData");
4164 return g_flavor;
4165}
4166
4167void
4168Target::TargetEventData::Dump (Stream *s) const
4169{
Oleksiy Vyalov8b85b032016-01-19 17:54:47 +00004170 for (size_t i = 0; i < m_module_list.GetSize(); ++i)
4171 {
4172 if (i != 0)
4173 *s << ", ";
4174 m_module_list.GetModuleAtIndex(i)->GetDescription(s, lldb::eDescriptionLevelBrief);
4175 }
Ilia Keb2c19a2015-03-10 21:59:55 +00004176}
4177
4178const Target::TargetEventData *
4179Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4180{
4181 if (event_ptr)
4182 {
4183 const EventData *event_data = event_ptr->GetData();
4184 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4185 return static_cast <const TargetEventData *> (event_ptr->GetData());
4186 }
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004187 return nullptr;
Ilia Keb2c19a2015-03-10 21:59:55 +00004188}
4189
4190TargetSP
4191Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4192{
4193 TargetSP target_sp;
4194 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4195 if (event_data)
4196 target_sp = event_data->m_target_sp;
4197 return target_sp;
4198}
4199
4200ModuleList
4201Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4202{
4203 ModuleList module_list;
4204 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4205 if (event_data)
4206 module_list = event_data->m_module_list;
4207 return module_list;
4208}