blob: acb84e5d17487fec91c32255af9b77c526154c2e [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 Ingham583bbb12016-03-07 21:50:25 +000073 Broadcaster (debugger.GetBroadcasterManager(), 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 &
Jim Ingham583bbb12016-03-07 21:50:25 +0000197Target::CreateProcess (ListenerSP listener_sp, const char *plugin_name, const FileSpec *crash_file)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198{
199 DeleteCurrentProcess ();
Jim Ingham583bbb12016-03-07 21:50:25 +0000200 m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name, listener_sp, 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{
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000351 FileSpec remapped_file;
352 ConstString remapped_path;
353 if (GetSourcePathMap().ReverseRemapPath(ConstString(file.GetPath().c_str()), remapped_path))
354 remapped_file.SetFile(remapped_path.AsCString(), true);
355 else
356 remapped_file = file;
357
Greg Clayton1f746072012-08-29 21:13:06 +0000358 if (check_inlines == eLazyBoolCalculate)
359 {
360 const InlineStrategy inline_strategy = GetInlineStrategy();
361 switch (inline_strategy)
362 {
363 case eInlineBreakpointsNever:
364 check_inlines = eLazyBoolNo;
365 break;
366
367 case eInlineBreakpointsHeaders:
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000368 if (remapped_file.IsSourceImplementationFile())
Greg Clayton1f746072012-08-29 21:13:06 +0000369 check_inlines = eLazyBoolNo;
370 else
371 check_inlines = eLazyBoolYes;
372 break;
373
374 case eInlineBreakpointsAlways:
375 check_inlines = eLazyBoolYes;
376 break;
377 }
378 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000379 SearchFilterSP filter_sp;
380 if (check_inlines == eLazyBoolNo)
381 {
382 // Not checking for inlines, we are looking only for matching compile units
383 FileSpecList compile_unit_list;
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000384 compile_unit_list.Append (remapped_file);
Greg Clayton93bfb292012-09-07 23:48:57 +0000385 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
386 }
387 else
388 {
389 filter_sp = GetSearchFilterForModuleList (containingModules);
390 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000391 if (skip_prologue == eLazyBoolCalculate)
392 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000393 if (move_to_nearest_code == eLazyBoolCalculate)
394 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000395
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000396 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine(nullptr,
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000397 remapped_file,
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000398 line_no,
399 check_inlines,
400 skip_prologue,
401 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000402 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000406Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000409
410 // Check for any reason we want to move this breakpoint to other address.
411 addr = GetBreakableLoadAddress(addr);
412
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 // Attempt to resolve our load address if possible, though it is ok if
414 // it doesn't resolve to section/offset.
415
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000416 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000417 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000418 if (!so_addr.IsValid())
419 {
420 // The address didn't resolve, so just set this as an absolute address
421 so_addr.SetOffset (addr);
422 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000423 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424 return bp_sp;
425}
426
427BreakpointSP
Jim Ingham055a08a2015-11-17 03:39:13 +0000428Target::CreateBreakpoint (const Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000430 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000431 BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(nullptr, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000432 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433}
434
Jim Ingham055a08a2015-11-17 03:39:13 +0000435lldb::BreakpointSP
436Target::CreateAddressInModuleBreakpoint (lldb::addr_t file_addr,
437 bool internal,
438 const FileSpec *file_spec,
439 bool request_hardware)
440{
441 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000442 BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(nullptr, file_addr, file_spec));
Jim Ingham055a08a2015-11-17 03:39:13 +0000443 return CreateBreakpoint (filter_sp, resolver_sp, internal, request_hardware, false);
444}
445
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000447Target::CreateBreakpoint (const FileSpecList *containingModules,
448 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000449 const char *func_name,
450 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000451 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000452 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000453 bool internal,
454 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000456 BreakpointSP bp_sp;
457 if (func_name)
458 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000459 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000460
461 if (skip_prologue == eLazyBoolCalculate)
462 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000463 if (language == lldb::eLanguageTypeUnknown)
464 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000465
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000466 BreakpointResolverSP resolver_sp(new BreakpointResolverName(nullptr,
467 func_name,
468 func_name_type_mask,
469 language,
470 Breakpoint::Exact,
471 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000472 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000473 }
474 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475}
476
Jim Inghamfab10e82012-03-06 00:37:27 +0000477lldb::BreakpointSP
478Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000479 const FileSpecList *containingSourceFiles,
480 const std::vector<std::string> &func_names,
481 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000482 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000483 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000484 bool internal,
485 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000486{
487 BreakpointSP bp_sp;
488 size_t num_names = func_names.size();
489 if (num_names > 0)
490 {
491 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000492
493 if (skip_prologue == eLazyBoolCalculate)
494 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000495 if (language == lldb::eLanguageTypeUnknown)
496 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000497
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000498 BreakpointResolverSP resolver_sp(new BreakpointResolverName(nullptr,
499 func_names,
500 func_name_type_mask,
501 language,
502 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000503 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000504 }
505 return bp_sp;
506}
507
Jim Ingham133e0fb2012-03-03 02:05:11 +0000508BreakpointSP
509Target::CreateBreakpoint (const FileSpecList *containingModules,
510 const FileSpecList *containingSourceFiles,
511 const char *func_names[],
512 size_t num_names,
513 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000514 LanguageType language,
Jim Inghama8558b62012-05-22 00:12:20 +0000515 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000516 bool internal,
517 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000518{
519 BreakpointSP bp_sp;
520 if (num_names > 0)
521 {
522 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
523
Greg Clayton03da4cc2013-04-19 21:31:16 +0000524 if (skip_prologue == eLazyBoolCalculate)
525 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000526 if (language == lldb::eLanguageTypeUnknown)
527 language = GetLanguage();
528
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000529 BreakpointResolverSP resolver_sp(new BreakpointResolverName(nullptr,
530 func_names,
531 num_names,
532 func_name_type_mask,
533 language,
534 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000535 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000536 }
537 return bp_sp;
538}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539
540SearchFilterSP
541Target::GetSearchFilterForModule (const FileSpec *containingModule)
542{
543 SearchFilterSP filter_sp;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000544 if (containingModule != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 {
546 // TODO: We should look into sharing module based search filters
547 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000548 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 }
550 else
551 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000552 if (!m_search_filter_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000553 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554 filter_sp = m_search_filter_sp;
555 }
556 return filter_sp;
557}
558
Jim Ingham969795f2011-09-21 01:17:13 +0000559SearchFilterSP
560Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
561{
562 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000563 if (containingModules && containingModules->GetSize() != 0)
564 {
565 // TODO: We should look into sharing module based search filters
566 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000567 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000568 }
569 else
570 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000571 if (!m_search_filter_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000572 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000573 filter_sp = m_search_filter_sp;
574 }
575 return filter_sp;
576}
577
Jim Ingham87df91b2011-09-23 00:54:11 +0000578SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000579Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
580 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000581{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000582 if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
Jim Ingham87df91b2011-09-23 00:54:11 +0000583 return GetSearchFilterForModuleList(containingModules);
584
585 SearchFilterSP filter_sp;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000586 if (containingModules == nullptr)
Jim Ingham87df91b2011-09-23 00:54:11 +0000587 {
588 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
589 // but that will take a little reworking.
590
Greg Claytone1cd1be2012-01-29 20:56:30 +0000591 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000592 }
593 else
594 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000595 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000596 }
597 return filter_sp;
598}
599
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000600BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000601Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000602 const FileSpecList *containingSourceFiles,
603 RegularExpression &func_regex,
Jim Ingham0fcdac32015-11-06 22:48:59 +0000604 lldb::LanguageType requested_language,
Jim Inghama8558b62012-05-22 00:12:20 +0000605 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000606 bool internal,
607 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000608{
Jim Ingham87df91b2011-09-23 00:54:11 +0000609 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000610 bool skip =
611 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
612 : static_cast<bool>(skip_prologue);
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000613 BreakpointResolverSP resolver_sp(new BreakpointResolverName(nullptr,
614 func_regex,
615 requested_language,
616 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617
Jim Ingham1460e4b2014-01-10 23:46:59 +0000618 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619}
620
Jim Ingham219ba192012-03-05 04:47:34 +0000621lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000622Target::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 +0000623{
Jim Inghama72b31c2015-04-22 19:42:18 +0000624 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
625 if (exc_bkpt_sp && additional_args)
626 {
627 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
628 if (precondition_sp && additional_args)
629 {
630 if (error)
631 *error = precondition_sp->ConfigurePrecondition(*additional_args);
632 else
633 precondition_sp->ConfigurePrecondition(*additional_args);
634 }
635 }
636 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000637}
Jim Inghama72b31c2015-04-22 19:42:18 +0000638
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000640Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641{
642 BreakpointSP bp_sp;
643 if (filter_sp && resolver_sp)
644 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000645 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000647 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000649 return bp_sp;
650}
651
652void
653Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
654{
655 if (!bp_sp)
656 return;
657 if (internal)
658 m_internal_breakpoint_list.Add (bp_sp, false);
659 else
660 m_breakpoint_list.Add (bp_sp, true);
661
662 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
663 if (log)
664 {
665 StreamString s;
666 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
667 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
668 }
669
670 bp_sp->ResolveBreakpoint();
671
672 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000673 {
674 m_last_created_breakpoint = bp_sp;
675 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676}
677
Johnny Chen86364b42011-09-20 23:28:55 +0000678bool
679Target::ProcessIsValid()
680{
681 return (m_process_sp && m_process_sp->IsAlive());
682}
683
Johnny Chenb90827e2012-06-04 23:19:54 +0000684static bool
685CheckIfWatchpointsExhausted(Target *target, Error &error)
686{
687 uint32_t num_supported_hardware_watchpoints;
688 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
689 if (rc.Success())
690 {
691 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
692 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
693 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
694 num_supported_hardware_watchpoints);
695 }
696 return false;
697}
698
Johnny Chen01a67862011-10-14 00:42:25 +0000699// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000700// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000701WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000702Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000703{
Greg Clayton5160ce52013-03-27 23:08:40 +0000704 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000705 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000706 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000707 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000708
Johnny Chen01a67862011-10-14 00:42:25 +0000709 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000710 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000711 {
712 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000713 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000714 }
Jim Inghamc6462312013-06-18 21:52:48 +0000715
Johnny Chen45e541f2011-09-14 22:20:15 +0000716 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000717 {
718 if (size == 0)
719 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
720 else
Daniel Malead01b2952012-11-29 21:49:15 +0000721 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000722 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000723 }
Jim Inghamc6462312013-06-18 21:52:48 +0000724
725 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
726 {
727 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
728 }
Johnny Chen7313a642011-09-13 01:15:36 +0000729
Johnny Chen01a67862011-10-14 00:42:25 +0000730 // Currently we only support one watchpoint per address, with total number
731 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000732
733 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000734 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 +0000735 Mutex::Locker locker;
736 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000737 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000738 if (matched_sp)
739 {
Johnny Chen0c406372011-09-14 20:23:45 +0000740 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000741 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000742 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
743 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000744 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000745 if (size == old_size && kind == old_type)
746 {
Johnny Chen01a67862011-10-14 00:42:25 +0000747 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000748 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000749 }
750 else
751 {
Johnny Chen01a67862011-10-14 00:42:25 +0000752 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000753 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
754 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000755 }
Johnny Chen3c532582011-09-13 23:29:31 +0000756 }
757
Jason Molenda727e3922012-12-05 23:07:34 +0000758 if (!wp_sp)
759 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000760 wp_sp.reset(new Watchpoint(*this, addr, size, type));
761 wp_sp->SetWatchpointType(kind, notify);
762 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000763 }
Johnny Chen0c406372011-09-14 20:23:45 +0000764
Jim Ingham1b5792e2012-12-18 02:03:49 +0000765 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000766 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000767 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
768 __FUNCTION__,
769 error.Success() ? "succeeded" : "failed",
770 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000771
Jason Molenda727e3922012-12-05 23:07:34 +0000772 if (error.Fail())
773 {
Johnny Chen41b77262012-03-26 22:00:10 +0000774 // Enabling the watchpoint on the device side failed.
775 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000776 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000777 // See if we could provide more helpful error message.
778 if (!CheckIfWatchpointsExhausted(this, error))
779 {
780 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000781 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000782 }
Johnny Chen01a67862011-10-14 00:42:25 +0000783 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000784 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000785 else
Johnny Chen01a67862011-10-14 00:42:25 +0000786 m_last_created_watchpoint = wp_sp;
787 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000788}
789
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000790void
791Target::RemoveAllBreakpoints (bool internal_also)
792{
Greg Clayton5160ce52013-03-27 23:08:40 +0000793 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794 if (log)
795 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
796
Greg Clayton9fed0d82010-07-23 23:33:17 +0000797 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000799 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000800
801 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802}
803
804void
805Target::DisableAllBreakpoints (bool internal_also)
806{
Greg Clayton5160ce52013-03-27 23:08:40 +0000807 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 if (log)
809 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
810
811 m_breakpoint_list.SetEnabledAll (false);
812 if (internal_also)
813 m_internal_breakpoint_list.SetEnabledAll (false);
814}
815
816void
817Target::EnableAllBreakpoints (bool internal_also)
818{
Greg Clayton5160ce52013-03-27 23:08:40 +0000819 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000820 if (log)
821 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
822
823 m_breakpoint_list.SetEnabledAll (true);
824 if (internal_also)
825 m_internal_breakpoint_list.SetEnabledAll (true);
826}
827
828bool
829Target::RemoveBreakpointByID (break_id_t break_id)
830{
Greg Clayton5160ce52013-03-27 23:08:40 +0000831 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832 if (log)
833 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
834
835 if (DisableBreakpointByID (break_id))
836 {
837 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000838 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000840 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000841 if (m_last_created_breakpoint)
842 {
843 if (m_last_created_breakpoint->GetID() == break_id)
844 m_last_created_breakpoint.reset();
845 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000846 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000847 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 return true;
849 }
850 return false;
851}
852
853bool
854Target::DisableBreakpointByID (break_id_t break_id)
855{
Greg Clayton5160ce52013-03-27 23:08:40 +0000856 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857 if (log)
858 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
859
860 BreakpointSP bp_sp;
861
862 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
863 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
864 else
865 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
866 if (bp_sp)
867 {
868 bp_sp->SetEnabled (false);
869 return true;
870 }
871 return false;
872}
873
874bool
875Target::EnableBreakpointByID (break_id_t break_id)
876{
Greg Clayton5160ce52013-03-27 23:08:40 +0000877 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 if (log)
879 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
880 __FUNCTION__,
881 break_id,
882 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
883
884 BreakpointSP bp_sp;
885
886 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
887 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
888 else
889 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
890
891 if (bp_sp)
892 {
893 bp_sp->SetEnabled (true);
894 return true;
895 }
896 return false;
897}
898
Johnny Chenedf50372011-09-23 21:21:43 +0000899// The flag 'end_to_end', default to true, signifies that the operation is
900// performed end to end, for both the debugger and the debuggee.
901
Johnny Chen01a67862011-10-14 00:42:25 +0000902// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
903// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000904bool
Johnny Chen01a67862011-10-14 00:42:25 +0000905Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000906{
Greg Clayton5160ce52013-03-27 23:08:40 +0000907 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000908 if (log)
909 log->Printf ("Target::%s\n", __FUNCTION__);
910
Johnny Chenedf50372011-09-23 21:21:43 +0000911 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000912 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000913 return true;
914 }
915
916 // Otherwise, it's an end to end operation.
917
Johnny Chen86364b42011-09-20 23:28:55 +0000918 if (!ProcessIsValid())
919 return false;
920
Johnny Chen01a67862011-10-14 00:42:25 +0000921 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000922 for (size_t i = 0; i < num_watchpoints; ++i)
923 {
Johnny Chen01a67862011-10-14 00:42:25 +0000924 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
925 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000926 return false;
927
Johnny Chen01a67862011-10-14 00:42:25 +0000928 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000929 if (rc.Fail())
930 return false;
931 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000932 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000933 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000934 return true; // Success!
935}
936
Johnny Chen01a67862011-10-14 00:42:25 +0000937// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
938// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000939bool
Johnny Chen01a67862011-10-14 00:42:25 +0000940Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000941{
Greg Clayton5160ce52013-03-27 23:08:40 +0000942 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000943 if (log)
944 log->Printf ("Target::%s\n", __FUNCTION__);
945
Johnny Chenedf50372011-09-23 21:21:43 +0000946 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000947 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000948 return true;
949 }
950
951 // Otherwise, it's an end to end operation.
952
Johnny Chen86364b42011-09-20 23:28:55 +0000953 if (!ProcessIsValid())
954 return false;
955
Johnny Chen01a67862011-10-14 00:42:25 +0000956 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000957 for (size_t i = 0; i < num_watchpoints; ++i)
958 {
Johnny Chen01a67862011-10-14 00:42:25 +0000959 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
960 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000961 return false;
962
Johnny Chen01a67862011-10-14 00:42:25 +0000963 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000964 if (rc.Fail())
965 return false;
966 }
Johnny Chen86364b42011-09-20 23:28:55 +0000967 return true; // Success!
968}
969
Johnny Chen01a67862011-10-14 00:42:25 +0000970// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
971// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000972bool
Johnny Chen01a67862011-10-14 00:42:25 +0000973Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000974{
Greg Clayton5160ce52013-03-27 23:08:40 +0000975 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000976 if (log)
977 log->Printf ("Target::%s\n", __FUNCTION__);
978
Johnny Chenedf50372011-09-23 21:21:43 +0000979 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000980 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000981 return true;
982 }
983
984 // Otherwise, it's an end to end operation.
985
Johnny Chen86364b42011-09-20 23:28:55 +0000986 if (!ProcessIsValid())
987 return false;
988
Johnny Chen01a67862011-10-14 00:42:25 +0000989 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000990 for (size_t i = 0; i < num_watchpoints; ++i)
991 {
Johnny Chen01a67862011-10-14 00:42:25 +0000992 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
993 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000994 return false;
995
Johnny Chen01a67862011-10-14 00:42:25 +0000996 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000997 if (rc.Fail())
998 return false;
999 }
Johnny Chen86364b42011-09-20 23:28:55 +00001000 return true; // Success!
1001}
1002
Johnny Chena4d6bc92012-02-25 06:44:30 +00001003// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1004bool
1005Target::ClearAllWatchpointHitCounts ()
1006{
Greg Clayton5160ce52013-03-27 23:08:40 +00001007 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +00001008 if (log)
1009 log->Printf ("Target::%s\n", __FUNCTION__);
1010
1011 size_t num_watchpoints = m_watchpoint_list.GetSize();
1012 for (size_t i = 0; i < num_watchpoints; ++i)
1013 {
1014 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1015 if (!wp_sp)
1016 return false;
1017
1018 wp_sp->ResetHitCount();
1019 }
1020 return true; // Success!
1021}
1022
Enrico Granata5e3fe042015-02-11 00:37:54 +00001023// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1024bool
1025Target::ClearAllWatchpointHistoricValues ()
1026{
1027 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
1028 if (log)
1029 log->Printf ("Target::%s\n", __FUNCTION__);
1030
1031 size_t num_watchpoints = m_watchpoint_list.GetSize();
1032 for (size_t i = 0; i < num_watchpoints; ++i)
1033 {
1034 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1035 if (!wp_sp)
1036 return false;
1037
1038 wp_sp->ResetHistoricValues();
1039 }
1040 return true; // Success!
1041}
1042
Johnny Chen01a67862011-10-14 00:42:25 +00001043// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +00001044// during these operations.
1045bool
Johnny Chen01a67862011-10-14 00:42:25 +00001046Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001047{
Greg Clayton5160ce52013-03-27 23:08:40 +00001048 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001049 if (log)
1050 log->Printf ("Target::%s\n", __FUNCTION__);
1051
1052 if (!ProcessIsValid())
1053 return false;
1054
Johnny Chen01a67862011-10-14 00:42:25 +00001055 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +00001056 for (size_t i = 0; i < num_watchpoints; ++i)
1057 {
Johnny Chen01a67862011-10-14 00:42:25 +00001058 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1059 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001060 return false;
1061
Johnny Chen01a67862011-10-14 00:42:25 +00001062 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001063 }
1064 return true; // Success!
1065}
1066
Johnny Chen01a67862011-10-14 00:42:25 +00001067// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001068bool
Johnny Chen01a67862011-10-14 00:42:25 +00001069Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001070{
Greg Clayton5160ce52013-03-27 23:08:40 +00001071 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001072 if (log)
1073 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1074
1075 if (!ProcessIsValid())
1076 return false;
1077
Johnny Chen01a67862011-10-14 00:42:25 +00001078 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1079 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001080 {
Johnny Chen01a67862011-10-14 00:42:25 +00001081 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001082 if (rc.Success())
1083 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001084
Johnny Chenf04ee932011-09-22 18:04:58 +00001085 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001086 }
1087 return false;
1088}
1089
Johnny Chen01a67862011-10-14 00:42:25 +00001090// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001091bool
Johnny Chen01a67862011-10-14 00:42:25 +00001092Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001093{
Greg Clayton5160ce52013-03-27 23:08:40 +00001094 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001095 if (log)
1096 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1097
1098 if (!ProcessIsValid())
1099 return false;
1100
Johnny Chen01a67862011-10-14 00:42:25 +00001101 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1102 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001103 {
Johnny Chen01a67862011-10-14 00:42:25 +00001104 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001105 if (rc.Success())
1106 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001107
Johnny Chenf04ee932011-09-22 18:04:58 +00001108 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001109 }
1110 return false;
1111}
1112
Johnny Chen01a67862011-10-14 00:42:25 +00001113// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001114bool
Johnny Chen01a67862011-10-14 00:42:25 +00001115Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001116{
Greg Clayton5160ce52013-03-27 23:08:40 +00001117 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001118 if (log)
1119 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1120
Jim Inghamb0b45132013-07-02 02:09:46 +00001121 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1122 if (watch_to_remove_sp == m_last_created_watchpoint)
1123 m_last_created_watchpoint.reset();
1124
Johnny Chen01a67862011-10-14 00:42:25 +00001125 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001126 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001127 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001128 return true;
1129 }
1130 return false;
1131}
1132
Johnny Chen01a67862011-10-14 00:42:25 +00001133// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001134bool
Johnny Chen01a67862011-10-14 00:42:25 +00001135Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001136{
Greg Clayton5160ce52013-03-27 23:08:40 +00001137 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001138 if (log)
1139 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1140
1141 if (!ProcessIsValid())
1142 return false;
1143
Johnny Chen01a67862011-10-14 00:42:25 +00001144 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1145 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001146 {
Johnny Chen01a67862011-10-14 00:42:25 +00001147 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001148 return true;
1149 }
1150 return false;
1151}
1152
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153ModuleSP
1154Target::GetExecutableModule ()
1155{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001156 // search for the first executable in the module list
1157 for (size_t i = 0; i < m_images.GetSize(); ++i)
1158 {
1159 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1160 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1161 if (obj == nullptr)
1162 continue;
1163 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1164 return module_sp;
1165 }
1166 // as fall back return the first module loaded
1167 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001168}
1169
1170Module*
1171Target::GetExecutableModulePointer ()
1172{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001173 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174}
1175
Enrico Granata17598482012-11-08 02:22:02 +00001176static void
1177LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1178{
1179 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001180 StreamString feedback_stream;
1181 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001182 {
Enrico Granata97303392013-05-21 00:00:30 +00001183 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001184 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001185 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1186 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001187 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001188 if (feedback_stream.GetSize())
1189 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1190 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001191}
1192
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193void
Greg Claytonb35db632013-11-09 00:03:31 +00001194Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195{
Greg Claytonb35db632013-11-09 00:03:31 +00001196 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001197 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 m_images.Clear();
Greg Clayton5beec212015-10-08 21:04:34 +00001199 m_scratch_type_system_map.Clear();
Sean Callanana3444ff2015-11-10 22:54:42 +00001200 m_ast_importer_sp.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001201}
1202
1203void
Greg Claytonb35db632013-11-09 00:03:31 +00001204Target::DidExec ()
1205{
1206 // When a process exec's we need to know about it so we can do some cleanup.
1207 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1208 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1209}
1210
1211void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001212Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1213{
1214 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001215 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001217 if (executable_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218 {
1219 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001220 "Target::SetExecutableModule (executable = '%s')",
1221 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001222
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001223 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224
Jim Ingham5aee1622010-08-09 23:31:02 +00001225 // 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 +00001226 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001227 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001228 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001229 if (log)
1230 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1231 }
1232
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001234 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001236 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237 {
1238 executable_objfile->GetDependentModules(dependent_files);
1239 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1240 {
Greg Claytonded470d2011-03-19 01:12:21 +00001241 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1242 FileSpec platform_dependent_file_spec;
1243 if (m_platform_sp)
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001244 m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001245 else
1246 platform_dependent_file_spec = dependent_file_spec;
1247
Greg Claytonb9a01b32012-02-26 05:51:37 +00001248 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1249 ModuleSP image_module_sp(GetSharedModule (module_spec));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001250 if (image_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252 ObjectFile *objfile = image_module_sp->GetObjectFile();
1253 if (objfile)
1254 objfile->GetDependentModules(dependent_files);
1255 }
1256 }
1257 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 }
1259}
1260
Jim Ingham5aee1622010-08-09 23:31:02 +00001261bool
1262Target::SetArchitecture (const ArchSpec &arch_spec)
1263{
Greg Clayton5160ce52013-03-27 23:08:40 +00001264 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001265 bool missing_local_arch = !m_arch.IsValid();
Jason Molendadad8af42015-11-10 04:11:37 +00001266 bool replace_local_arch = true;
1267 bool compatible_local_arch = false;
1268 ArchSpec other(arch_spec);
1269
1270 if (!missing_local_arch)
Jim Ingham5aee1622010-08-09 23:31:02 +00001271 {
Jason Molendadad8af42015-11-10 04:11:37 +00001272 if (m_arch.IsCompatibleMatch(arch_spec))
1273 {
1274 other.MergeFrom(m_arch);
1275
1276 if (m_arch.IsCompatibleMatch(other))
1277 {
1278 compatible_local_arch = true;
1279 bool arch_changed, vendor_changed, os_changed, os_ver_changed, env_changed;
1280
1281 m_arch.PiecewiseTripleCompare(other,
1282 arch_changed,
1283 vendor_changed,
1284 os_changed,
1285 os_ver_changed,
1286 env_changed);
1287
1288 if (!arch_changed && !vendor_changed && !os_changed)
1289 replace_local_arch = false;
1290 }
1291 }
1292 }
1293
1294 if (compatible_local_arch || missing_local_arch)
1295 {
1296 // If we haven't got a valid arch spec, or the architectures are compatible
1297 // update the architecture, unless the one we already have is more specified
1298 if (replace_local_arch)
1299 m_arch = other;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001300 if (log)
Jason Molendadad8af42015-11-10 04:11:37 +00001301 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 +00001302 return true;
1303 }
Jason Molendadad8af42015-11-10 04:11:37 +00001304
1305 // If we have an executable file, try to reset the executable to the desired architecture
1306 if (log)
1307 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1308 m_arch = other;
1309 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001310
Jason Molendadad8af42015-11-10 04:11:37 +00001311 ClearModules(true);
1312 // Need to do something about unsetting breakpoints.
1313
1314 if (executable_sp)
1315 {
1316 if (log)
1317 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1318 ModuleSpec module_spec (executable_sp->GetFileSpec(), other);
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001319 Error error = ModuleList::GetSharedModule(module_spec,
1320 executable_sp,
1321 &GetExecutableSearchPaths(),
1322 nullptr,
1323 nullptr);
Jason Molendadad8af42015-11-10 04:11:37 +00001324
1325 if (!error.Fail() && executable_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +00001326 {
Jason Molendadad8af42015-11-10 04:11:37 +00001327 SetExecutableModule (executable_sp, true);
1328 return true;
Jim Ingham5aee1622010-08-09 23:31:02 +00001329 }
1330 }
Greg Clayton70512312012-05-08 01:45:38 +00001331 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001332}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001334bool
1335Target::MergeArchitecture (const ArchSpec &arch_spec)
1336{
1337 if (arch_spec.IsValid())
1338 {
1339 if (m_arch.IsCompatibleMatch(arch_spec))
1340 {
1341 // The current target arch is compatible with "arch_spec", see if we
1342 // can improve our current architecture using bits from "arch_spec"
1343
1344 // Merge bits from arch_spec into "merged_arch" and set our architecture
1345 ArchSpec merged_arch (m_arch);
1346 merged_arch.MergeFrom (arch_spec);
1347 return SetArchitecture(merged_arch);
1348 }
1349 else
1350 {
1351 // The new architecture is different, we just need to replace it
1352 return SetArchitecture(arch_spec);
1353 }
1354 }
1355 return false;
1356}
1357
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001358void
Enrico Granataefe637d2012-11-08 19:16:03 +00001359Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001360{
1361}
1362
1363void
Enrico Granataefe637d2012-11-08 19:16:03 +00001364Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365{
1366 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001367 if (m_valid)
1368 {
1369 ModuleList my_module_list;
1370 my_module_list.Append(module_sp);
1371 LoadScriptingResourceForModule(module_sp, this);
1372 ModulesDidLoad (my_module_list);
1373 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374}
1375
1376void
Enrico Granataefe637d2012-11-08 19:16:03 +00001377Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001378{
Adrian McCarthy6ecdbc82015-10-15 22:39:55 +00001379 // A module is being removed from this target.
Greg Clayton23f8c952014-03-24 23:10:19 +00001380 if (m_valid)
1381 {
1382 ModuleList my_module_list;
1383 my_module_list.Append(module_sp);
1384 ModulesDidUnload (my_module_list, false);
1385 }
Enrico Granata17598482012-11-08 02:22:02 +00001386}
1387
1388void
Enrico Granataefe637d2012-11-08 19:16:03 +00001389Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390{
Jim Inghame716ae02011-08-03 01:00:06 +00001391 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001392 if (m_valid)
Jason Molendad91c3fd2015-11-10 04:18:12 +00001393 {
Greg Clayton23f8c952014-03-24 23:10:19 +00001394 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001395 m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
1396 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001397}
1398
1399void
1400Target::ModulesDidLoad (ModuleList &module_list)
1401{
Greg Clayton23f8c952014-03-24 23:10:19 +00001402 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001403 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001404 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001405 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001406 if (m_process_sp)
1407 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001408 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001409 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001410 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001411 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001412}
1413
1414void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001415Target::SymbolsDidLoad (ModuleList &module_list)
1416{
Greg Clayton23f8c952014-03-24 23:10:19 +00001417 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001418 {
Jim Ingham31caf982013-06-04 23:01:35 +00001419 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001420 {
Jim Ingham31caf982013-06-04 23:01:35 +00001421 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1422 if (runtime)
1423 {
1424 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1425 objc_runtime->SymbolsDidLoad(module_list);
1426 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001427 }
Jim Ingham31caf982013-06-04 23:01:35 +00001428
Greg Clayton095eeaa2013-11-05 23:28:00 +00001429 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001430 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001431 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001432 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001433}
1434
1435void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001436Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437{
Greg Clayton23f8c952014-03-24 23:10:19 +00001438 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001439 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001440 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001441 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001442 m_internal_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001443 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001444 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445}
1446
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001447bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001448Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001449{
Greg Clayton67cc0632012-08-22 17:17:09 +00001450 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001451 {
1452 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001453 ModuleSpec module_spec (module_file_spec);
1454 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001455
1456 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1457 // black list.
1458 if (num_modules > 0)
1459 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001460 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001461 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001462 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001463 return false;
1464 }
1465 return true;
1466 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001467 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001468 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001469}
1470
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001471bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001472Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001473{
Greg Clayton67cc0632012-08-22 17:17:09 +00001474 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001475 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001476 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001477 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001478 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001479 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001480}
1481
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001482size_t
Greg Claytondb598232011-01-07 01:57:07 +00001483Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1484{
Greg Claytone72dfb32012-02-24 01:59:29 +00001485 SectionSP section_sp (addr.GetSection());
1486 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001487 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001488 // 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 +00001489 if (section_sp->IsEncrypted())
1490 {
Greg Clayton57f06302012-05-25 17:05:55 +00001491 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001492 return 0;
1493 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001494 ModuleSP module_sp (section_sp->GetModule());
1495 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001496 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001497 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1498 if (objfile)
1499 {
1500 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1501 addr.GetOffset(),
1502 dst,
1503 dst_len);
1504 if (bytes_read > 0)
1505 return bytes_read;
1506 else
1507 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1508 }
Greg Claytondb598232011-01-07 01:57:07 +00001509 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001510 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001511 }
1512 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001513 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001514 }
1515 else
Greg Claytondb598232011-01-07 01:57:07 +00001516 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001517
Greg Claytondb598232011-01-07 01:57:07 +00001518 return 0;
1519}
1520
1521size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001522Target::ReadMemory (const Address& addr,
1523 bool prefer_file_cache,
1524 void *dst,
1525 size_t dst_len,
1526 Error &error,
1527 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001530
Enrico Granata9128ee22011-09-06 19:20:51 +00001531 // if we end up reading this from process memory, we will fill this
1532 // with the actual load address
1533 if (load_addr_ptr)
1534 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1535
Greg Claytondb598232011-01-07 01:57:07 +00001536 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001537
1538 addr_t load_addr = LLDB_INVALID_ADDRESS;
1539 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001540 Address resolved_addr;
1541 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001542 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001543 SectionLoadList &section_load_list = GetSectionLoadList();
1544 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001545 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001546 // No sections are loaded, so we must assume we are not running
1547 // yet and anything we are given is a file address.
1548 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1549 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001550 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001551 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001552 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001553 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001554 // we have manually loaded some sections with "target modules load ..."
1555 // or because we have have a live process that has sections loaded
1556 // through the dynamic loader
1557 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001558 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001559 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001560 }
Greg Clayton357132e2011-03-26 19:14:58 +00001561 if (!resolved_addr.IsValid())
1562 resolved_addr = addr;
Greg Claytonc749eb82011-07-11 05:12:02 +00001563
Greg Claytondb598232011-01-07 01:57:07 +00001564 if (prefer_file_cache)
1565 {
1566 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1567 if (bytes_read > 0)
1568 return bytes_read;
1569 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001570
Johnny Chen86364b42011-09-20 23:28:55 +00001571 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001572 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001573 if (load_addr == LLDB_INVALID_ADDRESS)
1574 load_addr = resolved_addr.GetLoadAddress (this);
1575
Greg Claytondda4f7b2010-06-30 23:03:03 +00001576 if (load_addr == LLDB_INVALID_ADDRESS)
1577 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001578 ModuleSP addr_module_sp (resolved_addr.GetModule());
1579 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001580 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001581 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001582 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001583 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001584 else
Daniel Malead01b2952012-11-29 21:49:15 +00001585 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001586 }
1587 else
1588 {
Greg Claytondb598232011-01-07 01:57:07 +00001589 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001590 if (bytes_read != dst_len)
1591 {
1592 if (error.Success())
1593 {
1594 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001595 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001596 else
Daniel Malead01b2952012-11-29 21:49:15 +00001597 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 +00001598 }
1599 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001600 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001601 {
1602 if (load_addr_ptr)
1603 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001604 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001605 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001606 // If the address is not section offset we have an address that
1607 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001608 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001609 // more we can do. If it is section offset, we might be able to
1610 // read cached memory from the object file.
1611 if (!resolved_addr.IsSectionOffset())
1612 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001614 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001615
Greg Claytonc749eb82011-07-11 05:12:02 +00001616 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001617 {
Greg Claytondb598232011-01-07 01:57:07 +00001618 // If we didn't already try and read from the object file cache, then
1619 // try it after failing to read from the process.
1620 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001621 }
1622 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001623}
1624
Greg Claytond16e1e52011-07-12 17:06:17 +00001625size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001626Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1627{
1628 char buf[256];
1629 out_str.clear();
1630 addr_t curr_addr = addr.GetLoadAddress(this);
1631 Address address(addr);
1632 while (1)
1633 {
1634 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1635 if (length == 0)
1636 break;
1637 out_str.append(buf, length);
1638 // If we got "length - 1" bytes, we didn't get the whole C string, we
1639 // need to read some more characters
1640 if (length == sizeof(buf) - 1)
1641 curr_addr += length;
1642 else
1643 break;
1644 address = Address(curr_addr);
1645 }
1646 return out_str.size();
1647}
1648
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001649size_t
1650Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1651{
1652 size_t total_cstr_len = 0;
1653 if (dst && dst_max_len)
1654 {
1655 result_error.Clear();
1656 // NULL out everything just to be safe
1657 memset (dst, 0, dst_max_len);
1658 Error error;
1659 addr_t curr_addr = addr.GetLoadAddress(this);
1660 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001661
1662 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1663 // think this really needs to be tied to the memory cache subsystem's
1664 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001665 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001666
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001667 size_t bytes_left = dst_max_len - 1;
1668 char *curr_dst = dst;
1669
1670 while (bytes_left > 0)
1671 {
1672 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1673 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1674 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1675
1676 if (bytes_read == 0)
1677 {
1678 result_error = error;
1679 dst[total_cstr_len] = '\0';
1680 break;
1681 }
1682 const size_t len = strlen(curr_dst);
1683
1684 total_cstr_len += len;
1685
1686 if (len < bytes_to_read)
1687 break;
1688
1689 curr_dst += bytes_read;
1690 curr_addr += bytes_read;
1691 bytes_left -= bytes_read;
1692 address = Address(curr_addr);
1693 }
1694 }
1695 else
1696 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001697 if (dst == nullptr)
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001698 result_error.SetErrorString("invalid arguments");
1699 else
1700 result_error.Clear();
1701 }
1702 return total_cstr_len;
1703}
1704
1705size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001706Target::ReadScalarIntegerFromMemory (const Address& addr,
1707 bool prefer_file_cache,
1708 uint32_t byte_size,
1709 bool is_signed,
1710 Scalar &scalar,
1711 Error &error)
1712{
1713 uint64_t uval;
1714
1715 if (byte_size <= sizeof(uval))
1716 {
1717 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1718 if (bytes_read == byte_size)
1719 {
1720 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001721 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001722 if (byte_size <= 4)
1723 scalar = data.GetMaxU32 (&offset, byte_size);
1724 else
1725 scalar = data.GetMaxU64 (&offset, byte_size);
1726
1727 if (is_signed)
1728 scalar.SignExtend(byte_size * 8);
1729 return bytes_read;
1730 }
1731 }
1732 else
1733 {
1734 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1735 }
1736 return 0;
1737}
1738
1739uint64_t
1740Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1741 bool prefer_file_cache,
1742 size_t integer_byte_size,
1743 uint64_t fail_value,
1744 Error &error)
1745{
1746 Scalar scalar;
1747 if (ReadScalarIntegerFromMemory (addr,
1748 prefer_file_cache,
1749 integer_byte_size,
1750 false,
1751 scalar,
1752 error))
1753 return scalar.ULongLong(fail_value);
1754 return fail_value;
1755}
1756
1757bool
1758Target::ReadPointerFromMemory (const Address& addr,
1759 bool prefer_file_cache,
1760 Error &error,
1761 Address &pointer_addr)
1762{
1763 Scalar scalar;
1764 if (ReadScalarIntegerFromMemory (addr,
1765 prefer_file_cache,
1766 m_arch.GetAddressByteSize(),
1767 false,
1768 scalar,
1769 error))
1770 {
1771 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1772 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1773 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001774 SectionLoadList &section_load_list = GetSectionLoadList();
1775 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001776 {
1777 // No sections are loaded, so we must assume we are not running
1778 // yet and anything we are given is a file address.
1779 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1780 }
1781 else
1782 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001783 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001784 // we have manually loaded some sections with "target modules load ..."
1785 // or because we have have a live process that has sections loaded
1786 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001787 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001788 }
1789 // We weren't able to resolve the pointer value, so just return
1790 // an address with no section
1791 if (!pointer_addr.IsValid())
1792 pointer_addr.SetOffset (pointer_vm_addr);
1793 return true;
1794
1795 }
1796 }
1797 return false;
1798}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001799
1800ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001801Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001802{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001803 ModuleSP module_sp;
1804
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001805 Error error;
1806
Jim Ingham4a94c912012-05-17 18:38:42 +00001807 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1808 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001809
Jim Ingham4a94c912012-05-17 18:38:42 +00001810 if (module_spec.GetUUID().IsValid())
1811 module_sp = m_images.FindFirstModule(module_spec);
1812
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001813 if (!module_sp)
1814 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001815 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1816 bool did_create_module = false;
1817
1818 // If there are image search path entries, try to use them first to acquire a suitable image.
1819 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001820 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001821 ModuleSpec transformed_spec (module_spec);
1822 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1823 {
1824 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1825 error = ModuleList::GetSharedModule (transformed_spec,
1826 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001827 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001828 &old_module_sp,
1829 &did_create_module);
1830 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001831 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001832
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001833 if (!module_sp)
1834 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001835 // If we have a UUID, we can check our global shared module list in case
1836 // we already have it. If we don't have a valid UUID, then we can't since
1837 // the path in "module_spec" will be a platform path, and we will need to
1838 // let the platform find that file. For example, we could be asking for
1839 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1840 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1841 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1842 // cache.
1843 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001844 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001845 // We have a UUID, it is OK to check the global module list...
1846 error = ModuleList::GetSharedModule (module_spec,
1847 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001848 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001849 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001850 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001851 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001852
1853 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001854 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001855 // The platform is responsible for finding and caching an appropriate
1856 // module in the shared module cache.
1857 if (m_platform_sp)
1858 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001859 error = m_platform_sp->GetSharedModule (module_spec,
1860 m_process_sp.get(),
1861 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001862 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001863 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001864 &did_create_module);
1865 }
1866 else
1867 {
1868 error.SetErrorString("no platform is currently set");
1869 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001870 }
1871 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001872
Jim Ingham4a94c912012-05-17 18:38:42 +00001873 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1874 // module in the list already, and if there was, let's remove it.
1875 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001876 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001877 ObjectFile *objfile = module_sp->GetObjectFile();
1878 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001879 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001880 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001881 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001882 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1883 case ObjectFile::eTypeExecutable: /// A normal executable
1884 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1885 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1886 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1887 break;
1888 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1889 if (error_ptr)
1890 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1891 return ModuleSP();
1892 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1893 if (error_ptr)
1894 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1895 return ModuleSP();
1896 default:
1897 if (error_ptr)
1898 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1899 return ModuleSP();
1900 }
1901 // GetSharedModule is not guaranteed to find the old shared module, for instance
1902 // in the common case where you pass in the UUID, it is only going to find the one
1903 // module matching the UUID. In fact, it has no good way to know what the "old module"
1904 // relevant to this target is, since there might be many copies of a module with this file spec
1905 // in various running debug sessions, but only one of them will belong to this target.
1906 // So let's remove the UUID from the module list, and look in the target's module list.
1907 // Only do this if there is SOMETHING else in the module spec...
1908 if (!old_module_sp)
1909 {
1910 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001911 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001912 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1913 module_spec_copy.GetUUID().Clear();
1914
1915 ModuleList found_modules;
1916 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1917 if (num_found == 1)
1918 {
1919 old_module_sp = found_modules.GetModuleAtIndex(0);
1920 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001921 }
1922 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001923
1924 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1925 {
1926 m_images.ReplaceModule(old_module_sp, module_sp);
1927 Module *old_module_ptr = old_module_sp.get();
1928 old_module_sp.reset();
1929 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1930 }
1931 else
1932 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001933 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001934 else
1935 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001936 }
1937 }
1938 if (error_ptr)
1939 *error_ptr = error;
1940 return module_sp;
1941}
1942
Greg Claytond9e416c2012-02-18 05:35:26 +00001943TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001944Target::CalculateTarget ()
1945{
Greg Claytond9e416c2012-02-18 05:35:26 +00001946 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947}
1948
Greg Claytond9e416c2012-02-18 05:35:26 +00001949ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001950Target::CalculateProcess ()
1951{
Sean Callanan92734d12016-02-12 19:45:31 +00001952 return m_process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953}
1954
Greg Claytond9e416c2012-02-18 05:35:26 +00001955ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001956Target::CalculateThread ()
1957{
Greg Claytond9e416c2012-02-18 05:35:26 +00001958 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001959}
1960
Jason Molendab57e4a12013-11-04 09:33:30 +00001961StackFrameSP
1962Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001963{
Jason Molendab57e4a12013-11-04 09:33:30 +00001964 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965}
1966
1967void
Greg Clayton0603aa92010-10-04 01:05:56 +00001968Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969{
Greg Claytonc14ee322011-09-22 04:58:26 +00001970 exe_ctx.Clear();
1971 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972}
1973
1974PathMappingList &
1975Target::GetImageSearchPathList ()
1976{
1977 return m_image_search_paths;
1978}
1979
1980void
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001981Target::ImageSearchPathsChanged(const PathMappingList &path_list,
1982 void *baton)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001983{
1984 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00001985 ModuleSP exe_module_sp (target->GetExecutableModule());
1986 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00001987 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988}
1989
Jim Ingham151c0322015-09-15 21:13:50 +00001990TypeSystem *
Sean Callanana994b0b2015-10-02 18:40:30 +00001991Target::GetScratchTypeSystemForLanguage (Error *error, lldb::LanguageType language, bool create_on_demand)
Jim Ingham151c0322015-09-15 21:13:50 +00001992{
Greg Clayton5beec212015-10-08 21:04:34 +00001993 if (!m_valid)
1994 return nullptr;
1995
Sean Callanana994b0b2015-10-02 18:40:30 +00001996 if (error)
1997 {
1998 error->Clear();
1999 }
2000
Sean Callananb92bd752015-10-01 16:28:02 +00002001 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Sean Callanana994b0b2015-10-02 18:40:30 +00002002 || language == eLanguageTypeUnknown)
2003 {
Sean Callananc7b054a2015-10-09 18:01:10 +00002004 std::set<lldb::LanguageType> languages_for_types;
2005 std::set<lldb::LanguageType> languages_for_expressions;
2006
2007 Language::GetLanguagesSupportingTypeSystems(languages_for_types, languages_for_expressions);
2008
2009 if (languages_for_expressions.count(eLanguageTypeC))
2010 {
2011 language = eLanguageTypeC; // LLDB's default. Override by setting the target language.
2012 }
2013 else
2014 {
2015 if (languages_for_expressions.empty())
2016 {
2017 return nullptr;
2018 }
2019 else
2020 {
2021 language = *languages_for_expressions.begin();
2022 }
2023 }
Sean Callananb92bd752015-10-01 16:28:02 +00002024 }
Sean Callananb92bd752015-10-01 16:28:02 +00002025
Greg Clayton5beec212015-10-08 21:04:34 +00002026 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this, create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00002027}
2028
2029PersistentExpressionState *
2030Target::GetPersistentExpressionStateForLanguage (lldb::LanguageType language)
2031{
Sean Callanana994b0b2015-10-02 18:40:30 +00002032 TypeSystem *type_system = GetScratchTypeSystemForLanguage(nullptr, language, true);
Sean Callananb92bd752015-10-01 16:28:02 +00002033
2034 if (type_system)
2035 {
2036 return type_system->GetPersistentExpressionState();
2037 }
Jim Ingham151c0322015-09-15 21:13:50 +00002038 else
Sean Callananb92bd752015-10-01 16:28:02 +00002039 {
2040 return nullptr;
2041 }
Jim Ingham151c0322015-09-15 21:13:50 +00002042}
2043
2044UserExpression *
2045Target::GetUserExpressionForLanguage(const char *expr,
Sean Callanana994b0b2015-10-02 18:40:30 +00002046 const char *expr_prefix,
2047 lldb::LanguageType language,
2048 Expression::ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +00002049 const EvaluateExpressionOptions &options,
Sean Callanana994b0b2015-10-02 18:40:30 +00002050 Error &error)
Jim Ingham151c0322015-09-15 21:13:50 +00002051{
Sean Callanana994b0b2015-10-02 18:40:30 +00002052 Error type_system_error;
2053
2054 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002055 UserExpression *user_expr = nullptr;
2056
2057 if (!type_system)
2058 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002059 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 +00002060 return nullptr;
2061 }
2062
Jim Ingham19a63fc2015-11-03 02:11:24 +00002063 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00002064 if (!user_expr)
2065 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2066
2067 return user_expr;
2068}
2069
2070FunctionCaller *
2071Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
2072 const CompilerType &return_type,
2073 const Address& function_address,
2074 const ValueList &arg_value_list,
2075 const char *name,
2076 Error &error)
2077{
Sean Callanana994b0b2015-10-02 18:40:30 +00002078 Error type_system_error;
2079 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002080 FunctionCaller *persistent_fn = nullptr;
2081
2082 if (!type_system)
2083 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002084 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 +00002085 return persistent_fn;
2086 }
2087
2088 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
2089 if (!persistent_fn)
2090 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2091
2092 return persistent_fn;
2093}
2094
2095UtilityFunction *
2096Target::GetUtilityFunctionForLanguage (const char *text,
2097 lldb::LanguageType language,
2098 const char *name,
2099 Error &error)
2100{
Sean Callanana994b0b2015-10-02 18:40:30 +00002101 Error type_system_error;
2102 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002103 UtilityFunction *utility_fn = nullptr;
2104
2105 if (!type_system)
2106 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002107 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 +00002108 return utility_fn;
2109 }
2110
2111 utility_fn = type_system->GetUtilityFunction (text, name);
2112 if (!utility_fn)
2113 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2114
2115 return utility_fn;
2116}
2117
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00002119Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002120{
Greg Clayton5beec212015-10-08 21:04:34 +00002121 if (m_valid)
Sean Callanan4bf80d52011-11-15 22:27:19 +00002122 {
Greg Clayton5beec212015-10-08 21:04:34 +00002123 if (TypeSystem* type_system = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC, create_on_demand))
2124 return llvm::dyn_cast<ClangASTContext>(type_system);
Sean Callanan4bf80d52011-11-15 22:27:19 +00002125 }
Greg Clayton5beec212015-10-08 21:04:34 +00002126 return nullptr;
Sean Callananb92bd752015-10-01 16:28:02 +00002127}
2128
Sean Callanana3444ff2015-11-10 22:54:42 +00002129ClangASTImporterSP
Sean Callanan686b2312011-11-16 18:20:47 +00002130Target::GetClangASTImporter()
2131{
Greg Clayton5beec212015-10-08 21:04:34 +00002132 if (m_valid)
Sean Callanan686b2312011-11-16 18:20:47 +00002133 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002134 if (!m_ast_importer_sp)
Greg Clayton5beec212015-10-08 21:04:34 +00002135 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002136 m_ast_importer_sp.reset(new ClangASTImporter());
Greg Clayton5beec212015-10-08 21:04:34 +00002137 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002138 return m_ast_importer_sp;
Sean Callanan686b2312011-11-16 18:20:47 +00002139 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002140 return ClangASTImporterSP();
Sean Callanan686b2312011-11-16 18:20:47 +00002141}
2142
Greg Clayton99d0faf2010-11-18 23:32:35 +00002143void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002144Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002145{
Greg Clayton6920b522012-08-22 18:39:03 +00002146 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002147}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002148
Greg Clayton99d0faf2010-11-18 23:32:35 +00002149void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002150Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002151{
Greg Clayton6920b522012-08-22 18:39:03 +00002152 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002153}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002154
Greg Claytonc859e2d2012-02-13 23:10:39 +00002155FileSpecList
2156Target::GetDefaultExecutableSearchPaths ()
2157{
Greg Clayton67cc0632012-08-22 17:17:09 +00002158 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2159 if (properties_sp)
2160 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002161 return FileSpecList();
2162}
2163
Michael Sartaina7499c92013-07-01 19:45:50 +00002164FileSpecList
2165Target::GetDefaultDebugFileSearchPaths ()
2166{
2167 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2168 if (properties_sp)
2169 return properties_sp->GetDebugFileSearchPaths();
2170 return FileSpecList();
2171}
2172
Sean Callanan85054342015-04-03 15:39:47 +00002173FileSpecList
2174Target::GetDefaultClangModuleSearchPaths ()
2175{
2176 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2177 if (properties_sp)
2178 return properties_sp->GetClangModuleSearchPaths();
2179 return FileSpecList();
2180}
2181
Caroline Ticedaccaa92010-09-20 20:44:43 +00002182ArchSpec
2183Target::GetDefaultArchitecture ()
2184{
Greg Clayton67cc0632012-08-22 17:17:09 +00002185 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2186 if (properties_sp)
2187 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002188 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002189}
2190
2191void
Greg Clayton67cc0632012-08-22 17:17:09 +00002192Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002193{
Greg Clayton67cc0632012-08-22 17:17:09 +00002194 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2195 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002196 {
2197 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 +00002198 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002199 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002200}
2201
Greg Clayton0603aa92010-10-04 01:05:56 +00002202Target *
2203Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2204{
2205 // The target can either exist in the "process" of ExecutionContext, or in
2206 // the "target_sp" member of SymbolContext. This accessor helper function
2207 // will get the target from one of these locations.
2208
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002209 Target *target = nullptr;
2210 if (sc_ptr != nullptr)
Greg Clayton0603aa92010-10-04 01:05:56 +00002211 target = sc_ptr->target_sp.get();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002212 if (target == nullptr && exe_ctx_ptr)
Greg Claytonc14ee322011-09-22 04:58:26 +00002213 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002214 return target;
2215}
2216
Jim Ingham1624a2d2014-05-05 02:26:40 +00002217ExpressionResults
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002218Target::EvaluateExpression(const char *expr_cstr,
Jim Inghama309efe2015-10-28 22:23:17 +00002219 ExecutionContextScope *exe_scope,
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002220 lldb::ValueObjectSP &result_valobj_sp,
2221 const EvaluateExpressionOptions& options)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002222{
Enrico Granata97fca502012-09-18 17:43:16 +00002223 result_valobj_sp.reset();
2224
Jim Ingham8646d3c2014-05-05 02:47:44 +00002225 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002226
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002227 if (expr_cstr == nullptr || expr_cstr[0] == '\0')
Greg Claytond1767f02011-12-08 02:13:16 +00002228 return execution_results;
2229
Jim Ingham6026ca32011-05-12 02:06:14 +00002230 // We shouldn't run stop hooks in expressions.
2231 // Be sure to reset this if you return anywhere within this function.
2232 bool old_suppress_value = m_suppress_stop_hooks;
2233 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002234
2235 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002236
Jim Inghama309efe2015-10-28 22:23:17 +00002237 if (exe_scope)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002238 {
Jim Inghama309efe2015-10-28 22:23:17 +00002239 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002240 }
2241 else if (m_process_sp)
2242 {
2243 m_process_sp->CalculateExecutionContext(exe_ctx);
2244 }
2245 else
2246 {
2247 CalculateExecutionContext(exe_ctx);
2248 }
2249
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002250 // Make sure we aren't just trying to see the value of a persistent
2251 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002252 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002253 // Only check for persistent variables the expression starts with a '$'
2254 if (expr_cstr[0] == '$')
Sean Callanana994b0b2015-10-02 18:40:30 +00002255 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002256
2257 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002258 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002259 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002260 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002261 }
2262 else
2263 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002264 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002265 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002266 execution_results = UserExpression::Evaluate (exe_ctx,
2267 options,
2268 expr_cstr,
2269 prefix,
2270 result_valobj_sp,
2271 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002272 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002273
2274 m_suppress_stop_hooks = old_suppress_value;
2275
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002276 return execution_results;
2277}
2278
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002279lldb::ExpressionVariableSP
2280Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002281{
Greg Clayton5beec212015-10-08 21:04:34 +00002282 lldb::ExpressionVariableSP variable_sp;
2283 m_scratch_type_system_map.ForEach([this, name, &variable_sp](TypeSystem *type_system) -> bool
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002284 {
Greg Clayton5beec212015-10-08 21:04:34 +00002285 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002286 {
Greg Clayton5beec212015-10-08 21:04:34 +00002287 variable_sp = persistent_state->GetVariable(name);
2288
2289 if (variable_sp)
2290 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002291 }
Greg Clayton5beec212015-10-08 21:04:34 +00002292 return true; // Keep iterating the ForEach
2293 });
2294 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002295}
2296
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002297lldb::addr_t
Sean Callananb92bd752015-10-01 16:28:02 +00002298Target::GetPersistentSymbol(const ConstString &name)
2299{
Greg Clayton5beec212015-10-08 21:04:34 +00002300 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Sean Callananb92bd752015-10-01 16:28:02 +00002301
Greg Clayton5beec212015-10-08 21:04:34 +00002302 m_scratch_type_system_map.ForEach([this, name, &address](TypeSystem *type_system) -> bool
Sean Callananb92bd752015-10-01 16:28:02 +00002303 {
Greg Clayton5beec212015-10-08 21:04:34 +00002304 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callananb92bd752015-10-01 16:28:02 +00002305 {
Greg Clayton5beec212015-10-08 21:04:34 +00002306 address = persistent_state->LookupSymbol(name);
2307 if (address != LLDB_INVALID_ADDRESS)
2308 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002309 }
Greg Clayton5beec212015-10-08 21:04:34 +00002310 return true; // Keep iterating the ForEach
2311 });
2312 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002313}
2314
2315lldb::addr_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002316Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2317{
2318 addr_t code_addr = load_addr;
2319 switch (m_arch.GetMachine())
2320 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002321 case llvm::Triple::mips:
2322 case llvm::Triple::mipsel:
2323 case llvm::Triple::mips64:
2324 case llvm::Triple::mips64el:
2325 switch (addr_class)
2326 {
2327 case eAddressClassData:
2328 case eAddressClassDebug:
2329 return LLDB_INVALID_ADDRESS;
2330
2331 case eAddressClassUnknown:
2332 case eAddressClassInvalid:
2333 case eAddressClassCode:
2334 case eAddressClassCodeAlternateISA:
2335 case eAddressClassRuntime:
2336 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2337 code_addr |= 1ull;
2338 break;
2339 }
2340 break;
2341
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002342 case llvm::Triple::arm:
2343 case llvm::Triple::thumb:
2344 switch (addr_class)
2345 {
2346 case eAddressClassData:
2347 case eAddressClassDebug:
2348 return LLDB_INVALID_ADDRESS;
2349
2350 case eAddressClassUnknown:
2351 case eAddressClassInvalid:
2352 case eAddressClassCode:
2353 case eAddressClassCodeAlternateISA:
2354 case eAddressClassRuntime:
2355 // Check if bit zero it no set?
2356 if ((code_addr & 1ull) == 0)
2357 {
2358 // Bit zero isn't set, check if the address is a multiple of 2?
2359 if (code_addr & 2ull)
2360 {
2361 // The address is a multiple of 2 so it must be thumb, set bit zero
2362 code_addr |= 1ull;
2363 }
2364 else if (addr_class == eAddressClassCodeAlternateISA)
2365 {
2366 // We checked the address and the address claims to be the alternate ISA
2367 // which means thumb, so set bit zero.
2368 code_addr |= 1ull;
2369 }
2370 }
2371 break;
2372 }
2373 break;
2374
2375 default:
2376 break;
2377 }
2378 return code_addr;
2379}
2380
2381lldb::addr_t
2382Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2383{
2384 addr_t opcode_addr = load_addr;
2385 switch (m_arch.GetMachine())
2386 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002387 case llvm::Triple::mips:
2388 case llvm::Triple::mipsel:
2389 case llvm::Triple::mips64:
2390 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002391 case llvm::Triple::arm:
2392 case llvm::Triple::thumb:
2393 switch (addr_class)
2394 {
2395 case eAddressClassData:
2396 case eAddressClassDebug:
2397 return LLDB_INVALID_ADDRESS;
2398
2399 case eAddressClassInvalid:
2400 case eAddressClassUnknown:
2401 case eAddressClassCode:
2402 case eAddressClassCodeAlternateISA:
2403 case eAddressClassRuntime:
2404 opcode_addr &= ~(1ull);
2405 break;
2406 }
2407 break;
2408
2409 default:
2410 break;
2411 }
2412 return opcode_addr;
2413}
2414
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002415lldb::addr_t
2416Target::GetBreakableLoadAddress (lldb::addr_t addr)
2417{
2418 addr_t breakable_addr = addr;
2419 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2420
2421 switch (m_arch.GetMachine())
2422 {
2423 default:
2424 break;
2425 case llvm::Triple::mips:
2426 case llvm::Triple::mipsel:
2427 case llvm::Triple::mips64:
2428 case llvm::Triple::mips64el:
2429 {
2430 addr_t function_start = 0;
2431 addr_t current_offset = 0;
2432 uint32_t loop_count = 0;
2433 Address resolved_addr;
2434 uint32_t arch_flags = m_arch.GetFlags ();
2435 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2436 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2437 SectionLoadList &section_load_list = GetSectionLoadList();
2438
2439 if (section_load_list.IsEmpty())
2440 // No sections are loaded, so we must assume we are not running yet
2441 // and need to operate only on file address.
2442 m_images.ResolveFileAddress (addr, resolved_addr);
2443 else
2444 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2445
2446 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2447 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2448 if (temp_addr_module_sp)
2449 {
2450 SymbolContext sc;
2451 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002452 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002453 Address sym_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002454 if (sc.function)
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002455 sym_addr = sc.function->GetAddressRange().GetBaseAddress();
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002456 else if (sc.symbol)
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002457 sym_addr = sc.symbol->GetAddress();
2458
2459 function_start = sym_addr.GetLoadAddress(this);
2460 if (function_start == LLDB_INVALID_ADDRESS)
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002461 function_start = sym_addr.GetFileAddress();
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002462
2463 if (function_start)
2464 current_offset = addr - function_start;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002465 }
2466
2467 // If breakpoint address is start of function then we dont have to do anything.
2468 if (current_offset == 0)
2469 return breakable_addr;
2470 else
2471 loop_count = current_offset / 2;
2472
2473 if (loop_count > 3)
2474 {
2475 // Scan previous 6 bytes
2476 if (IsMips16 | IsMicromips)
2477 loop_count = 3;
2478 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2479 else
2480 loop_count = 2;
2481 }
2482
2483 // Create Disassembler Instance
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002484 lldb::DisassemblerSP disasm_sp(Disassembler::FindPlugin(m_arch, nullptr, nullptr));
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002485
2486 ExecutionContext exe_ctx;
2487 CalculateExecutionContext(exe_ctx);
2488 InstructionList instruction_list;
2489 InstructionSP prev_insn;
2490 bool prefer_file_cache = true; // Read from file
2491 uint32_t inst_to_choose = 0;
2492
2493 for (uint32_t i = 1; i <= loop_count; i++)
2494 {
2495 // Adjust the address to read from.
2496 resolved_addr.Slide (-2);
2497 AddressRange range(resolved_addr, i*2);
2498 uint32_t insn_size = 0;
2499
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002500 disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002501
2502 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2503 if (num_insns)
2504 {
2505 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2506 insn_size = prev_insn->GetOpcode().GetByteSize();
2507 if (i == 1 && insn_size == 2)
2508 {
2509 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2510 instruction_list.Append(prev_insn);
2511 inst_to_choose = 1;
2512 }
2513 else if (i == 2)
2514 {
2515 // Here we may get one 4-byte instruction or two 2-byte instructions.
2516 if (num_insns == 2)
2517 {
2518 // Looks like there are two 2-byte instructions above our breakpoint target address.
2519 // 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.
2520 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2521 // and whatever i=1 iteration has found out is true.
2522 inst_to_choose = 1;
2523 break;
2524 }
2525 else if (insn_size == 4)
2526 {
2527 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2528 // Lets try scanning upper 2 bytes to verify this.
2529 instruction_list.Append(prev_insn);
2530 inst_to_choose = 2;
2531 }
2532 }
2533 else if (i == 3)
2534 {
2535 if (insn_size == 4)
2536 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2537 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2538 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2539 inst_to_choose = 0;
2540 else
2541 // This is straight-forward
2542 inst_to_choose = 2;
2543 break;
2544 }
2545 }
2546 else
2547 {
2548 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2549 if (i > 1)
2550 {
2551 inst_to_choose = i - 1;
2552 break;
2553 }
2554 }
2555 }
2556
2557 // Check if we are able to find any valid instruction.
2558 if (inst_to_choose)
2559 {
2560 if (inst_to_choose > instruction_list.GetSize())
2561 inst_to_choose--;
2562 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2563
2564 if (prev_insn->HasDelaySlot())
2565 {
2566 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2567 // Adjust the breakable address
2568 breakable_addr = addr - shift_size;
2569 if (log)
2570 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);
2571 }
2572 }
2573 break;
2574 }
2575 }
2576 return breakable_addr;
2577}
2578
Greg Clayton9585fbf2013-03-19 00:20:55 +00002579SourceManager &
2580Target::GetSourceManager ()
2581{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002582 if (!m_source_manager_ap)
Greg Clayton9585fbf2013-03-19 00:20:55 +00002583 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2584 return *m_source_manager_ap;
2585}
2586
Sean Callanan9998acd2014-12-05 01:21:59 +00002587ClangModulesDeclVendor *
2588Target::GetClangModulesDeclVendor ()
2589{
2590 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2591
2592 {
2593 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2594
2595 if (!m_clang_modules_decl_vendor_ap)
2596 {
2597 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2598 }
2599 }
2600
2601 return m_clang_modules_decl_vendor_ap.get();
2602}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002603
Greg Clayton44d93782014-01-27 23:43:24 +00002604Target::StopHookSP
2605Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002606{
2607 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002608 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2609 m_stop_hooks[new_uid] = stop_hook_sp;
2610 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002611}
2612
2613bool
2614Target::RemoveStopHookByID (lldb::user_id_t user_id)
2615{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002616 size_t num_removed = m_stop_hooks.erase(user_id);
2617 return (num_removed != 0);
Jim Ingham9575d842011-03-11 03:53:59 +00002618}
2619
2620void
2621Target::RemoveAllStopHooks ()
2622{
2623 m_stop_hooks.clear();
2624}
2625
2626Target::StopHookSP
2627Target::GetStopHookByID (lldb::user_id_t user_id)
2628{
2629 StopHookSP found_hook;
2630
2631 StopHookCollection::iterator specified_hook_iter;
2632 specified_hook_iter = m_stop_hooks.find (user_id);
2633 if (specified_hook_iter != m_stop_hooks.end())
2634 found_hook = (*specified_hook_iter).second;
2635 return found_hook;
2636}
2637
2638bool
2639Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2640{
2641 StopHookCollection::iterator specified_hook_iter;
2642 specified_hook_iter = m_stop_hooks.find (user_id);
2643 if (specified_hook_iter == m_stop_hooks.end())
2644 return false;
2645
2646 (*specified_hook_iter).second->SetIsActive (active_state);
2647 return true;
2648}
2649
2650void
2651Target::SetAllStopHooksActiveState (bool active_state)
2652{
2653 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2654 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2655 {
2656 (*pos).second->SetIsActive (active_state);
2657 }
2658}
2659
2660void
2661Target::RunStopHooks ()
2662{
Jim Ingham6026ca32011-05-12 02:06:14 +00002663 if (m_suppress_stop_hooks)
2664 return;
2665
Jim Ingham9575d842011-03-11 03:53:59 +00002666 if (!m_process_sp)
2667 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002668
2669 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2670 // since in that case we do not want to run the stop-hooks
2671 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2672 return;
2673
Jim Ingham9575d842011-03-11 03:53:59 +00002674 if (m_stop_hooks.empty())
2675 return;
2676
2677 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2678
2679 // If there aren't any active stop hooks, don't bother either:
2680 bool any_active_hooks = false;
2681 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2682 {
2683 if ((*pos).second->IsActive())
2684 {
2685 any_active_hooks = true;
2686 break;
2687 }
2688 }
2689 if (!any_active_hooks)
2690 return;
2691
2692 CommandReturnObject result;
2693
2694 std::vector<ExecutionContext> exc_ctx_with_reasons;
2695 std::vector<SymbolContext> sym_ctx_with_reasons;
2696
2697 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2698 size_t num_threads = cur_threadlist.GetSize();
2699 for (size_t i = 0; i < num_threads; i++)
2700 {
2701 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2702 if (cur_thread_sp->ThreadStoppedForAReason())
2703 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002704 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002705 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2706 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2707 }
2708 }
2709
2710 // If no threads stopped for a reason, don't run the stop-hooks.
2711 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2712 if (num_exe_ctx == 0)
2713 return;
2714
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002715 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2716 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002717
2718 bool keep_going = true;
2719 bool hooks_ran = false;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002720 bool print_hook_header = (m_stop_hooks.size() != 1);
2721 bool print_thread_header = (num_exe_ctx != 1);
Jim Ingham381e25b2011-03-22 01:47:27 +00002722
Jim Ingham9575d842011-03-11 03:53:59 +00002723 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2724 {
2725 // result.Clear();
2726 StopHookSP cur_hook_sp = (*pos).second;
2727 if (!cur_hook_sp->IsActive())
2728 continue;
2729
2730 bool any_thread_matched = false;
2731 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2732 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002733 if ((cur_hook_sp->GetSpecifier() == nullptr
Jim Ingham9575d842011-03-11 03:53:59 +00002734 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002735 && (cur_hook_sp->GetThreadSpecifier() == nullptr
Jim Ingham3d902922012-03-07 22:03:04 +00002736 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002737 {
2738 if (!hooks_ran)
2739 {
Jim Ingham9575d842011-03-11 03:53:59 +00002740 hooks_ran = true;
2741 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002742 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002743 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002744 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2745 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002746 nullptr);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002747 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002748 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002749 else
Daniel Malead01b2952012-11-29 21:49:15 +00002750 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002751 any_thread_matched = true;
2752 }
2753
Jim Ingham381e25b2011-03-22 01:47:27 +00002754 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002755 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002756
2757 CommandInterpreterRunOptions options;
2758 options.SetStopOnContinue (true);
2759 options.SetStopOnError (true);
2760 options.SetEchoCommands (false);
2761 options.SetPrintResults (true);
2762 options.SetAddToHistory (false);
2763
2764 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2765 &exc_ctx_with_reasons[i],
2766 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002767 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002768
2769 // If the command started the target going again, we should bag out of
2770 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002771 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2772 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002773 {
Daniel Malead01b2952012-11-29 21:49:15 +00002774 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002775 keep_going = false;
2776 }
2777 }
2778 }
2779 }
Jason Molenda879cf772011-09-23 00:42:55 +00002780
Caroline Tice969ed3d2011-05-02 20:41:46 +00002781 result.GetImmediateOutputStream()->Flush();
2782 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002783}
2784
Greg Claytonfbb76342013-11-20 21:07:01 +00002785const TargetPropertiesSP &
2786Target::GetGlobalProperties()
2787{
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002788 // NOTE: intentional leak so we don't crash if global destructor chain gets
2789 // called as other threads still use the result of this function
2790 static TargetPropertiesSP *g_settings_sp_ptr = nullptr;
Greg Clayton04df8ee2016-02-26 19:38:18 +00002791 static std::once_flag g_once_flag;
2792 std::call_once(g_once_flag, []() {
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002793 g_settings_sp_ptr = new TargetPropertiesSP(new TargetProperties(nullptr));
Greg Clayton04df8ee2016-02-26 19:38:18 +00002794 });
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002795 return *g_settings_sp_ptr;
Greg Claytonfbb76342013-11-20 21:07:01 +00002796}
2797
2798Error
2799Target::Install (ProcessLaunchInfo *launch_info)
2800{
2801 Error error;
2802 PlatformSP platform_sp (GetPlatform());
2803 if (platform_sp)
2804 {
2805 if (platform_sp->IsRemote())
2806 {
2807 if (platform_sp->IsConnected())
2808 {
2809 // Install all files that have an install path, and always install the
2810 // main executable when connected to a remote platform
2811 const ModuleList& modules = GetImages();
2812 const size_t num_images = modules.GetSize();
2813 for (size_t idx = 0; idx < num_images; ++idx)
2814 {
2815 const bool is_main_executable = idx == 0;
2816 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2817 if (module_sp)
2818 {
2819 FileSpec local_file (module_sp->GetFileSpec());
2820 if (local_file)
2821 {
2822 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2823 if (!remote_file)
2824 {
2825 if (is_main_executable) // TODO: add setting for always installing main executable???
2826 {
2827 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002828 remote_file = platform_sp->GetRemoteWorkingDirectory();
2829 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002830 }
2831 }
2832 if (remote_file)
2833 {
2834 error = platform_sp->Install(local_file, remote_file);
2835 if (error.Success())
2836 {
2837 module_sp->SetPlatformFileSpec(remote_file);
2838 if (is_main_executable)
2839 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002840 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002841 if (launch_info)
2842 launch_info->SetExecutableFile(remote_file, false);
2843 }
2844 }
2845 else
2846 break;
2847 }
2848 }
2849 }
2850 }
2851 }
2852 }
2853 }
2854 return error;
2855}
Greg Clayton7b242382011-07-08 00:48:09 +00002856
Greg Claytond5944cd2013-12-06 01:12:00 +00002857bool
2858Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2859{
2860 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2861}
2862
2863bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002864Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2865{
2866 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2867}
2868
2869bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002870Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2871{
2872 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2873 if (old_section_load_addr != new_section_load_addr)
2874 {
2875 uint32_t stop_id = 0;
2876 ProcessSP process_sp(GetProcessSP());
2877 if (process_sp)
2878 stop_id = process_sp->GetStopID();
2879 else
2880 stop_id = m_section_load_history.GetLastStopID();
2881 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2882 return true; // Return true if the section load address was changed...
2883 }
2884 return false; // Return false to indicate nothing changed
Greg Claytond5944cd2013-12-06 01:12:00 +00002885}
2886
Greg Clayton8012cad2014-11-17 19:39:20 +00002887size_t
2888Target::UnloadModuleSections (const ModuleList &module_list)
2889{
2890 size_t section_unload_count = 0;
2891 size_t num_modules = module_list.GetSize();
2892 for (size_t i=0; i<num_modules; ++i)
2893 {
2894 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2895 }
2896 return section_unload_count;
2897}
2898
2899size_t
2900Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2901{
2902 uint32_t stop_id = 0;
2903 ProcessSP process_sp(GetProcessSP());
2904 if (process_sp)
2905 stop_id = process_sp->GetStopID();
2906 else
2907 stop_id = m_section_load_history.GetLastStopID();
2908 SectionList *sections = module_sp->GetSectionList();
2909 size_t section_unload_count = 0;
2910 if (sections)
2911 {
2912 const uint32_t num_sections = sections->GetNumSections(0);
2913 for (uint32_t i = 0; i < num_sections; ++i)
2914 {
2915 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2916 }
2917 }
2918 return section_unload_count;
2919}
2920
Greg Claytond5944cd2013-12-06 01:12:00 +00002921bool
2922Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2923{
2924 uint32_t stop_id = 0;
2925 ProcessSP process_sp(GetProcessSP());
2926 if (process_sp)
2927 stop_id = process_sp->GetStopID();
2928 else
2929 stop_id = m_section_load_history.GetLastStopID();
2930 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2931}
2932
2933bool
2934Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2935{
2936 uint32_t stop_id = 0;
2937 ProcessSP process_sp(GetProcessSP());
2938 if (process_sp)
2939 stop_id = process_sp->GetStopID();
2940 else
2941 stop_id = m_section_load_history.GetLastStopID();
2942 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2943}
2944
2945void
2946Target::ClearAllLoadedSections ()
2947{
2948 m_section_load_history.Clear();
2949}
2950
Greg Claytonb09c5382013-12-13 17:20:18 +00002951Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002952Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002953{
2954 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002955 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2956
2957 if (log)
2958 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2959
Greg Claytonb09c5382013-12-13 17:20:18 +00002960 StateType state = eStateInvalid;
2961
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002962 // Scope to temporarily get the process state in case someone has manually
2963 // remotely connected already to a process and we can skip the platform
2964 // launching.
2965 {
2966 ProcessSP process_sp (GetProcessSP());
2967
2968 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002969 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002970 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002971 if (log)
2972 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2973 }
2974 else
2975 {
2976 if (log)
2977 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2978 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002979 }
2980
Greg Claytonb09c5382013-12-13 17:20:18 +00002981 launch_info.GetFlags().Set (eLaunchFlagDebug);
2982
2983 // Get the value of synchronous execution here. If you wait till after you have started to
2984 // run, then you could have hit a breakpoint, whose command might switch the value, and
2985 // then you'll pick up that incorrect value.
2986 Debugger &debugger = GetDebugger();
2987 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
2988
2989 PlatformSP platform_sp (GetPlatform());
2990
2991 // Finalize the file actions, and if none were given, default to opening
2992 // up a pseudo terminal
2993 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00002994 if (log)
2995 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
2996 __FUNCTION__,
2997 platform_sp ? "true" : "false",
2998 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
2999 default_to_use_pty ? "true" : "false");
3000
Greg Claytonb09c5382013-12-13 17:20:18 +00003001 launch_info.FinalizeFileActions (this, default_to_use_pty);
3002
3003 if (state == eStateConnected)
3004 {
3005 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
3006 {
3007 error.SetErrorString("can't launch in tty when launching through a remote connection");
3008 return error;
3009 }
3010 }
3011
3012 if (!launch_info.GetArchitecture().IsValid())
3013 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00003014
3015 // 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 +00003016 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
3017 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003018 if (log)
3019 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
3020
Greg Clayton5df78fa2015-05-23 03:54:53 +00003021 // Get a weak pointer to the previous process if we have one
3022 ProcessWP process_wp;
3023 if (m_process_sp)
3024 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00003025 m_process_sp = GetPlatform()->DebugProcess (launch_info,
3026 debugger,
3027 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00003028 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00003029
3030 // Cleanup the old process since someone might still have a strong
3031 // reference to this process and we would like to allow it to cleanup
3032 // as much as it can without the object being destroyed. We try to
3033 // lock the shared pointer and if that works, then someone else still
3034 // has a strong reference to the process.
3035
3036 ProcessSP old_process_sp(process_wp.lock());
3037 if (old_process_sp)
3038 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00003039 }
3040 else
3041 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003042 if (log)
3043 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
3044
Greg Claytonb09c5382013-12-13 17:20:18 +00003045 if (state == eStateConnected)
3046 {
3047 assert(m_process_sp);
3048 }
3049 else
3050 {
Todd Fiala015d8182014-07-22 23:41:36 +00003051 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00003052 const char *plugin_name = launch_info.GetProcessPluginName();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003053 CreateProcess(launch_info.GetListenerForProcess(debugger), plugin_name, nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00003054 }
Todd Fiala015d8182014-07-22 23:41:36 +00003055
3056 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00003057 if (m_process_sp)
3058 error = m_process_sp->Launch (launch_info);
3059 }
3060
3061 if (!m_process_sp)
3062 {
3063 if (error.Success())
3064 error.SetErrorString("failed to launch or debug process");
3065 return error;
3066 }
3067
3068 if (error.Success())
3069 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003070 if (synchronous_execution || !launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003071 {
Greg Clayton44d93782014-01-27 23:43:24 +00003072 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00003073 if (!hijack_listener_sp)
3074 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003075 hijack_listener_sp = Listener::MakeListener("lldb.Target.Launch.hijack");
Zachary Turnere6d213a2015-03-26 20:41:14 +00003076 launch_info.SetHijackListener(hijack_listener_sp);
Jim Ingham583bbb12016-03-07 21:50:25 +00003077 m_process_sp->HijackProcessEvents(hijack_listener_sp);
Zachary Turnere6d213a2015-03-26 20:41:14 +00003078 }
Todd Fialaac33cc92014-10-09 01:02:08 +00003079
Jim Ingham583bbb12016-03-07 21:50:25 +00003080 StateType state = m_process_sp->WaitForProcessToStop(nullptr, nullptr, false, hijack_listener_sp, nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00003081
3082 if (state == eStateStopped)
3083 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003084 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003085 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003086 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00003087 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003088 error = m_process_sp->PrivateResume();
3089 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00003090 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003091 state = m_process_sp->WaitForProcessToStop(nullptr, nullptr, true, hijack_listener_sp, stream);
Ilia K064e69f2015-03-23 21:16:25 +00003092 const bool must_be_alive = false; // eStateExited is ok, so this must be false
3093 if (!StateIsStoppedState(state, must_be_alive))
3094 {
3095 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
3096 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003097 }
3098 }
Ilia K064e69f2015-03-23 21:16:25 +00003099 else
3100 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003101 m_process_sp->RestoreProcessEvents();
3102 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00003103 }
3104 if (!error.Success())
3105 {
Ilia K064e69f2015-03-23 21:16:25 +00003106 Error error2;
3107 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
3108 error = error2;
3109 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003110 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003111 }
Greg Clayton40286e02014-04-30 20:29:09 +00003112 else if (state == eStateExited)
3113 {
Zachary Turner10687b02014-10-20 17:46:43 +00003114 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00003115 const int exit_status = m_process_sp->GetExitStatus();
3116 const char *exit_desc = m_process_sp->GetExitDescription();
3117#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'."
3118 if (exit_desc && exit_desc[0])
3119 {
3120 if (with_shell)
3121 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
3122 else
3123 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
3124 }
3125 else
3126 {
3127 if (with_shell)
3128 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
3129 else
3130 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
3131 }
3132 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003133 else
3134 {
3135 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
3136 }
3137 }
Greg Clayton44d93782014-01-27 23:43:24 +00003138 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00003139 }
3140 else
3141 {
Greg Clayton44d93782014-01-27 23:43:24 +00003142 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003143 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003144 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003145 }
3146 return error;
3147}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003148
3149Error
3150Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3151{
3152 auto state = eStateInvalid;
3153 auto process_sp = GetProcessSP ();
3154 if (process_sp)
3155 {
3156 state = process_sp->GetState ();
3157 if (process_sp->IsAlive () && state != eStateConnected)
3158 {
3159 if (state == eStateAttaching)
3160 return Error ("process attach is in progress");
3161 return Error ("a process is already being debugged");
3162 }
3163 }
3164
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003165 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3166
3167 // If no process info was specified, then use the target executable
3168 // name as the process to attach to by default
3169 if (!attach_info.ProcessInfoSpecified ())
3170 {
3171 if (old_exec_module_sp)
3172 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3173
3174 if (!attach_info.ProcessInfoSpecified ())
3175 {
3176 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3177 }
3178 }
3179
3180 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
Greg Claytonb3788ea2015-10-05 22:58:37 +00003181 ListenerSP hijack_listener_sp;
3182 const bool async = attach_info.GetAsync();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003183 if (!async)
Greg Claytonb3788ea2015-10-05 22:58:37 +00003184 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003185 hijack_listener_sp = Listener::MakeListener("lldb.Target.Attach.attach.hijack");
Greg Claytonb3788ea2015-10-05 22:58:37 +00003186 attach_info.SetHijackListener (hijack_listener_sp);
3187 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003188
3189 Error error;
3190 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3191 {
3192 SetPlatform (platform_sp);
3193 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3194 }
3195 else
3196 {
3197 if (state != eStateConnected)
3198 {
3199 const char *plugin_name = attach_info.GetProcessPluginName ();
3200 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3201 if (process_sp == nullptr)
3202 {
3203 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3204 return error;
3205 }
3206 }
Greg Claytonb3788ea2015-10-05 22:58:37 +00003207 if (hijack_listener_sp)
Jim Ingham583bbb12016-03-07 21:50:25 +00003208 process_sp->HijackProcessEvents (hijack_listener_sp);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003209 error = process_sp->Attach (attach_info);
3210 }
3211
Greg Claytone75e5d82015-10-20 00:14:20 +00003212 if (error.Success () && process_sp)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003213 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003214 if (async)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003215 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003216 process_sp->RestoreProcessEvents ();
3217 }
3218 else
3219 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003220 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener(), stream);
Greg Claytone75e5d82015-10-20 00:14:20 +00003221 process_sp->RestoreProcessEvents ();
3222
3223 if (state != eStateStopped)
3224 {
3225 const char *exit_desc = process_sp->GetExitDescription ();
3226 if (exit_desc)
3227 error.SetErrorStringWithFormat ("%s", exit_desc);
3228 else
3229 error.SetErrorString ("process did not stop (no such process or permission problem?)");
3230 process_sp->Destroy (false);
3231 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003232 }
3233 }
3234 return error;
3235}
3236
Jim Ingham9575d842011-03-11 03:53:59 +00003237//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003238// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003239//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003240Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3241 UserID (uid),
3242 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003243 m_commands (),
3244 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003245 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003246 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003247{
3248}
3249
3250Target::StopHook::StopHook (const StopHook &rhs) :
3251 UserID (rhs.GetID()),
3252 m_target_sp (rhs.m_target_sp),
3253 m_commands (rhs.m_commands),
3254 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003255 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003256 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003257{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003258 if (rhs.m_thread_spec_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00003259 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3260}
3261
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003262Target::StopHook::~StopHook() = default;
Jim Ingham9575d842011-03-11 03:53:59 +00003263
3264void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003265Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3266{
3267 m_specifier_sp.reset(specifier);
3268}
3269
3270void
Jim Ingham9575d842011-03-11 03:53:59 +00003271Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3272{
3273 m_thread_spec_ap.reset (specifier);
3274}
Jim Ingham9575d842011-03-11 03:53:59 +00003275
3276void
3277Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3278{
3279 int indent_level = s->GetIndentLevel();
3280
3281 s->SetIndentLevel(indent_level + 2);
3282
Daniel Malead01b2952012-11-29 21:49:15 +00003283 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003284 if (m_active)
3285 s->Indent ("State: enabled\n");
3286 else
3287 s->Indent ("State: disabled\n");
3288
3289 if (m_specifier_sp)
3290 {
3291 s->Indent();
3292 s->PutCString ("Specifier:\n");
3293 s->SetIndentLevel (indent_level + 4);
3294 m_specifier_sp->GetDescription (s, level);
3295 s->SetIndentLevel (indent_level + 2);
3296 }
3297
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003298 if (m_thread_spec_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00003299 {
3300 StreamString tmp;
3301 s->Indent("Thread:\n");
3302 m_thread_spec_ap->GetDescription (&tmp, level);
3303 s->SetIndentLevel (indent_level + 4);
3304 s->Indent (tmp.GetData());
3305 s->PutCString ("\n");
3306 s->SetIndentLevel (indent_level + 2);
3307 }
3308
3309 s->Indent ("Commands: \n");
3310 s->SetIndentLevel (indent_level + 4);
3311 uint32_t num_commands = m_commands.GetSize();
3312 for (uint32_t i = 0; i < num_commands; i++)
3313 {
3314 s->Indent(m_commands.GetStringAtIndex(i));
3315 s->PutCString ("\n");
3316 }
3317 s->SetIndentLevel (indent_level);
3318}
3319
Greg Clayton67cc0632012-08-22 17:17:09 +00003320//--------------------------------------------------------------
3321// class TargetProperties
3322//--------------------------------------------------------------
3323
3324OptionEnumValueElement
3325lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003326{
Greg Clayton67cc0632012-08-22 17:17:09 +00003327 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3328 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3329 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003330 { 0, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +00003331};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003332
Greg Clayton1f746072012-08-29 21:13:06 +00003333static OptionEnumValueElement
3334g_inline_breakpoint_enums[] =
3335{
3336 { 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."},
3337 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3338 { 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 +00003339 { 0, nullptr, nullptr }
Greg Clayton1f746072012-08-29 21:13:06 +00003340};
3341
Jim Ingham0f063ba2013-03-02 00:26:47 +00003342typedef enum x86DisassemblyFlavor
3343{
3344 eX86DisFlavorDefault,
3345 eX86DisFlavorIntel,
3346 eX86DisFlavorATT
3347} x86DisassemblyFlavor;
3348
3349static OptionEnumValueElement
3350g_x86_dis_flavor_value_types[] =
3351{
3352 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3353 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3354 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003355 { 0, nullptr, nullptr }
Jim Ingham0f063ba2013-03-02 00:26:47 +00003356};
3357
Enrico Granata397ddd52013-05-21 20:13:34 +00003358static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003359g_hex_immediate_style_values[] =
3360{
3361 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3362 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003363 { 0, nullptr, nullptr }
Daniel Malead79ae052013-08-07 21:54:09 +00003364};
3365
3366static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003367g_load_script_from_sym_file_values[] =
3368{
3369 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3370 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3371 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003372 { 0, nullptr, nullptr }
Enrico Granata397ddd52013-05-21 20:13:34 +00003373};
3374
Greg Claytonfd814c52013-08-13 01:42:25 +00003375static OptionEnumValueElement
Jason Molenda878ae012016-02-19 00:05:17 +00003376g_load_current_working_dir_lldbinit_values[] =
3377{
3378 { eLoadCWDlldbinitTrue, "true", "Load .lldbinit files from current directory"},
3379 { eLoadCWDlldbinitFalse, "false", "Do not load .lldbinit files from current directory"},
3380 { eLoadCWDlldbinitWarn, "warn", "Warn about loading .lldbinit files from current directory"},
3381 { 0, nullptr, nullptr }
3382};
3383
3384static OptionEnumValueElement
Greg Claytonfd814c52013-08-13 01:42:25 +00003385g_memory_module_load_level_values[] =
3386{
Greg Clayton86eac942013-08-13 21:32:34 +00003387 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003388 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3389 { 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 +00003390 { 0, nullptr, nullptr }
Greg Claytonfd814c52013-08-13 01:42:25 +00003391};
3392
Greg Clayton67cc0632012-08-22 17:17:09 +00003393static PropertyDefinition
3394g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003395{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003396 { "default-arch" , OptionValue::eTypeArch , true , 0 , nullptr, nullptr, "Default architecture to choose, when there's a choice." },
3397 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Move breakpoints to nearest code." },
3398 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , nullptr, nullptr, "The language to use when interpreting expressions entered in commands." },
3399 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "Path to a file containing expressions to be prepended to all expressions." },
3400 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eDynamicDontRunTarget , nullptr, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
3401 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Should synthetic values be used by default whenever available." },
3402 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Skip function prologues when setting breakpoints by name." },
3403 { "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 +00003404 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3405 "some part (starting at the root) of the path to the file when it was built, "
3406 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3407 "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 +00003408 { "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." },
3409 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , nullptr, nullptr, "List of directories to be searched when locating debug symbol files." },
3410 { "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 +00003411 { "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 +00003412 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , nullptr, nullptr, "Maximum number of children to expand in any level of depth." },
3413 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , nullptr, nullptr, "Maximum number of characters to show when using %s in summary strings." },
3414 { "max-memory-read-size" , OptionValue::eTypeSInt64 , false, 1024 , nullptr, nullptr, "Maximum number of bytes that 'memory read' will fetch before --force must be specified." },
3415 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Consult the platform module avoid list when setting non-module specific breakpoints." },
3416 { "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." },
3417 { "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." },
3418 { "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." },
3419 { "inherit-env" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Inherit the environment from the process that is running LLDB." },
3420 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for reading its standard input." },
3421 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for writing its standard output." },
3422 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for writing its standard error." },
3423 { "detach-on-error" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "debugserver will detach (rather than killing) a process if it loses connection with lldb." },
3424 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Disable Address Space Layout Randomization (ASLR)" },
3425 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , nullptr, nullptr, "Disable stdin/stdout for process (e.g. for a GUI application)" },
3426 { "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 +00003427 "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 +00003428 "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 +00003429 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003430 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3431 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3432 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003433 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003434 // 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 +00003435 { "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." },
3436 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, nullptr, nullptr, "Show immediates in disassembly as hexadecimal." },
3437 { "hex-immediate-style" , OptionValue::eTypeEnum , false, Disassembler::eHexStyleC, nullptr, g_hex_immediate_style_values, "Which style to use for printing hexadecimal disassembly values." },
3438 { "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." },
3439 { "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 +00003440 { "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 +00003441 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, nullptr, g_memory_module_load_level_values,
Greg Clayton86eac942013-08-13 21:32:34 +00003442 "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. "
3443 "This setting helps users control how much information gets loaded when loading modules from memory."
3444 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3445 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3446 "'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 +00003447 { "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." },
3448 { "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." },
3449 { "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." },
3450 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, nullptr, nullptr, "Disable lock-step debugging, instead control threads independently." },
3451 { nullptr , OptionValue::eTypeInvalid , false, 0 , nullptr, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +00003452};
Enrico Granata560558e2015-02-11 02:35:39 +00003453
Greg Clayton67cc0632012-08-22 17:17:09 +00003454enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003455{
Greg Clayton67cc0632012-08-22 17:17:09 +00003456 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003457 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003458 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003459 ePropertyExprPrefix,
3460 ePropertyPreferDynamic,
3461 ePropertyEnableSynthetic,
3462 ePropertySkipPrologue,
3463 ePropertySourceMap,
3464 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003465 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003466 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003467 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003468 ePropertyMaxChildrenCount,
3469 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003470 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003471 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003472 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003473 ePropertyRunArgs,
3474 ePropertyEnvVars,
3475 ePropertyInheritEnv,
3476 ePropertyInputPath,
3477 ePropertyOutputPath,
3478 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003479 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003480 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003481 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003482 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003483 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003484 ePropertyUseHexImmediates,
3485 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003486 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003487 ePropertyLoadScriptFromSymbolFile,
Jason Molenda878ae012016-02-19 00:05:17 +00003488 ePropertyLoadCWDlldbinitFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003489 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003490 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003491 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003492 ePropertyDisplayRuntimeSupportValues,
3493 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003494};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003495
Greg Clayton67cc0632012-08-22 17:17:09 +00003496class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003497{
Greg Clayton67cc0632012-08-22 17:17:09 +00003498public:
3499 TargetOptionValueProperties (const ConstString &name) :
3500 OptionValueProperties (name),
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003501 m_target(nullptr),
Greg Clayton67cc0632012-08-22 17:17:09 +00003502 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003503 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003504 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003505
Greg Clayton67cc0632012-08-22 17:17:09 +00003506 // This constructor is used when creating TargetOptionValueProperties when it
3507 // is part of a new lldb_private::Target instance. It will copy all current
3508 // global property values as needed
3509 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3510 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3511 m_target (target),
3512 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003513 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003514 }
3515
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003516 const Property *
3517 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +00003518 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003519 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003520 // try and grab the setting from the current target if there is one. Else we just
3521 // use the one from this instance.
3522 if (idx == ePropertyEnvVars)
3523 GetHostEnvironmentIfNeeded ();
3524
3525 if (exe_ctx)
3526 {
3527 Target *target = exe_ctx->GetTargetPtr();
3528 if (target)
3529 {
3530 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3531 if (this != target_properties)
3532 return target_properties->ProtectedGetPropertyAtIndex (idx);
3533 }
3534 }
3535 return ProtectedGetPropertyAtIndex (idx);
3536 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003537
3538 lldb::TargetSP
3539 GetTargetSP ()
3540 {
3541 return m_target->shared_from_this();
3542 }
3543
Greg Clayton67cc0632012-08-22 17:17:09 +00003544protected:
Greg Clayton67cc0632012-08-22 17:17:09 +00003545 void
3546 GetHostEnvironmentIfNeeded () const
3547 {
3548 if (!m_got_host_env)
3549 {
3550 if (m_target)
3551 {
3552 m_got_host_env = true;
3553 const uint32_t idx = ePropertyInheritEnv;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003554 if (GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0))
Greg Clayton67cc0632012-08-22 17:17:09 +00003555 {
3556 PlatformSP platform_sp (m_target->GetPlatform());
3557 if (platform_sp)
3558 {
3559 StringList env;
3560 if (platform_sp->GetEnvironment(env))
3561 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003562 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary(nullptr, ePropertyEnvVars);
Greg Clayton67cc0632012-08-22 17:17:09 +00003563 if (env_dict)
3564 {
3565 const bool can_replace = false;
3566 const size_t envc = env.GetSize();
3567 for (size_t idx=0; idx<envc; idx++)
3568 {
3569 const char *env_entry = env.GetStringAtIndex (idx);
3570 if (env_entry)
3571 {
3572 const char *equal_pos = ::strchr(env_entry, '=');
3573 ConstString key;
3574 // It is ok to have environment variables with no values
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003575 const char *value = nullptr;
Greg Clayton67cc0632012-08-22 17:17:09 +00003576 if (equal_pos)
3577 {
3578 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3579 if (equal_pos[1])
3580 value = equal_pos + 1;
3581 }
3582 else
3583 {
3584 key.SetCString(env_entry);
3585 }
3586 // Don't allow existing keys to be replaced with ones we get from the platform environment
3587 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3588 }
3589 }
3590 }
3591 }
3592 }
3593 }
3594 }
3595 }
3596 }
3597 Target *m_target;
3598 mutable bool m_got_host_env;
3599};
3600
Greg Claytonfbb76342013-11-20 21:07:01 +00003601//----------------------------------------------------------------------
3602// TargetProperties
3603//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003604TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003605 Properties (),
3606 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003607{
3608 if (target)
3609 {
3610 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003611
3612 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3613 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3614 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3615 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3616 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3617 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3618 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3619 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3620 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3621 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3622
3623 // Update m_launch_info once it was created
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003624 Arg0ValueChangedCallback(this, nullptr);
3625 RunArgsValueChangedCallback(this, nullptr);
3626 //EnvVarsValueChangedCallback(this, nullptr); // FIXME: cause segfault in Target::GetPlatform()
3627 InputPathValueChangedCallback(this, nullptr);
3628 OutputPathValueChangedCallback(this, nullptr);
3629 ErrorPathValueChangedCallback(this, nullptr);
3630 DetachOnErrorValueChangedCallback(this, nullptr);
3631 DisableASLRValueChangedCallback(this, nullptr);
3632 DisableSTDIOValueChangedCallback(this, nullptr);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003633 }
3634 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003635 {
3636 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3637 m_collection_sp->Initialize(g_properties);
3638 m_collection_sp->AppendProperty(ConstString("process"),
3639 ConstString("Settings specify to processes."),
3640 true,
3641 Process::GetGlobalProperties()->GetValueProperties());
3642 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003643}
3644
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003645TargetProperties::~TargetProperties() = default;
3646
Greg Clayton67cc0632012-08-22 17:17:09 +00003647ArchSpec
3648TargetProperties::GetDefaultArchitecture () const
3649{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003650 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(nullptr, ePropertyDefaultArch);
Greg Clayton67cc0632012-08-22 17:17:09 +00003651 if (value)
3652 return value->GetCurrentValue();
3653 return ArchSpec();
3654}
3655
3656void
3657TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3658{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003659 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(nullptr, ePropertyDefaultArch);
Greg Clayton67cc0632012-08-22 17:17:09 +00003660 if (value)
3661 return value->SetCurrentValue(arch, true);
3662}
3663
Ilia K055ad9b2015-05-18 13:41:01 +00003664bool
3665TargetProperties::GetMoveToNearestCode() const
3666{
3667 const uint32_t idx = ePropertyMoveToNearestCode;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003668 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Ilia K055ad9b2015-05-18 13:41:01 +00003669}
3670
Greg Clayton67cc0632012-08-22 17:17:09 +00003671lldb::DynamicValueType
3672TargetProperties::GetPreferDynamicValue() const
3673{
3674 const uint32_t idx = ePropertyPreferDynamic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003675 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003676}
3677
3678bool
Greg Clayton15484402015-05-15 18:40:24 +00003679TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3680{
3681 const uint32_t idx = ePropertyPreferDynamic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003682 return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, d);
Greg Clayton15484402015-05-15 18:40:24 +00003683}
3684
Greg Clayton15484402015-05-15 18:40:24 +00003685bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003686TargetProperties::GetDisableASLR () const
3687{
3688 const uint32_t idx = ePropertyDisableASLR;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003689 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003690}
3691
3692void
3693TargetProperties::SetDisableASLR (bool b)
3694{
3695 const uint32_t idx = ePropertyDisableASLR;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003696 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003697}
3698
3699bool
Jim Ingham106d0282014-06-25 02:32:56 +00003700TargetProperties::GetDetachOnError () const
3701{
3702 const uint32_t idx = ePropertyDetachOnError;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003703 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham106d0282014-06-25 02:32:56 +00003704}
3705
3706void
3707TargetProperties::SetDetachOnError (bool b)
3708{
3709 const uint32_t idx = ePropertyDetachOnError;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003710 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Jim Ingham106d0282014-06-25 02:32:56 +00003711}
3712
3713bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003714TargetProperties::GetDisableSTDIO () const
3715{
3716 const uint32_t idx = ePropertyDisableSTDIO;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003717 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003718}
3719
3720void
3721TargetProperties::SetDisableSTDIO (bool b)
3722{
3723 const uint32_t idx = ePropertyDisableSTDIO;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003724 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003725}
3726
Jim Ingham0f063ba2013-03-02 00:26:47 +00003727const char *
3728TargetProperties::GetDisassemblyFlavor () const
3729{
3730 const uint32_t idx = ePropertyDisassemblyFlavor;
3731 const char *return_value;
3732
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003733 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Jim Ingham0f063ba2013-03-02 00:26:47 +00003734 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3735 return return_value;
3736}
3737
Greg Clayton1f746072012-08-29 21:13:06 +00003738InlineStrategy
3739TargetProperties::GetInlineStrategy () const
3740{
3741 const uint32_t idx = ePropertyInlineStrategy;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003742 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton1f746072012-08-29 21:13:06 +00003743}
3744
Greg Clayton45392552012-10-17 22:57:12 +00003745const char *
3746TargetProperties::GetArg0 () const
3747{
3748 const uint32_t idx = ePropertyArg0;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003749 return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, nullptr);
Greg Clayton45392552012-10-17 22:57:12 +00003750}
3751
3752void
3753TargetProperties::SetArg0 (const char *arg)
3754{
3755 const uint32_t idx = ePropertyArg0;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003756 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003757 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003758}
3759
Greg Clayton67cc0632012-08-22 17:17:09 +00003760bool
3761TargetProperties::GetRunArguments (Args &args) const
3762{
3763 const uint32_t idx = ePropertyRunArgs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003764 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Greg Clayton67cc0632012-08-22 17:17:09 +00003765}
3766
3767void
3768TargetProperties::SetRunArguments (const Args &args)
3769{
3770 const uint32_t idx = ePropertyRunArgs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003771 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003772 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003773}
3774
3775size_t
3776TargetProperties::GetEnvironmentAsArgs (Args &env) const
3777{
3778 const uint32_t idx = ePropertyEnvVars;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003779 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, env);
Greg Clayton67cc0632012-08-22 17:17:09 +00003780}
3781
Ilia K8f37ca52015-02-13 14:31:06 +00003782void
3783TargetProperties::SetEnvironmentFromArgs (const Args &env)
3784{
3785 const uint32_t idx = ePropertyEnvVars;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003786 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003787 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003788}
3789
Greg Clayton67cc0632012-08-22 17:17:09 +00003790bool
3791TargetProperties::GetSkipPrologue() const
3792{
3793 const uint32_t idx = ePropertySkipPrologue;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003794 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003795}
3796
3797PathMappingList &
3798TargetProperties::GetSourcePathMap () const
3799{
3800 const uint32_t idx = ePropertySourceMap;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003801 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(nullptr, false, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003802 assert(option_value);
3803 return option_value->GetCurrentValue();
3804}
3805
3806FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003807TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003808{
3809 const uint32_t idx = ePropertyExecutableSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003810 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003811 assert(option_value);
3812 return option_value->GetCurrentValue();
3813}
3814
Michael Sartaina7499c92013-07-01 19:45:50 +00003815FileSpecList &
3816TargetProperties::GetDebugFileSearchPaths ()
3817{
3818 const uint32_t idx = ePropertyDebugFileSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003819 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Michael Sartaina7499c92013-07-01 19:45:50 +00003820 assert(option_value);
3821 return option_value->GetCurrentValue();
3822}
3823
Sean Callanan85054342015-04-03 15:39:47 +00003824FileSpecList &
3825TargetProperties::GetClangModuleSearchPaths ()
3826{
3827 const uint32_t idx = ePropertyClangModuleSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003828 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Sean Callanan85054342015-04-03 15:39:47 +00003829 assert(option_value);
3830 return option_value->GetCurrentValue();
3831}
3832
Greg Clayton67cc0632012-08-22 17:17:09 +00003833bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003834TargetProperties::GetEnableAutoImportClangModules() const
3835{
3836 const uint32_t idx = ePropertyAutoImportClangModules;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003837 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003838}
3839
3840bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003841TargetProperties::GetEnableSyntheticValue () const
3842{
3843 const uint32_t idx = ePropertyEnableSynthetic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003844 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003845}
3846
3847uint32_t
3848TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3849{
3850 const uint32_t idx = ePropertyMaxChildrenCount;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003851 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003852}
3853
3854uint32_t
3855TargetProperties::GetMaximumSizeOfStringSummary() const
3856{
3857 const uint32_t idx = ePropertyMaxSummaryLength;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003858 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003859}
3860
Enrico Granatad325bf92013-06-04 22:54:16 +00003861uint32_t
3862TargetProperties::GetMaximumMemReadSize () const
3863{
3864 const uint32_t idx = ePropertyMaxMemReadSize;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003865 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granatad325bf92013-06-04 22:54:16 +00003866}
3867
Greg Clayton67cc0632012-08-22 17:17:09 +00003868FileSpec
3869TargetProperties::GetStandardInputPath () const
3870{
3871 const uint32_t idx = ePropertyInputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003872 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003873}
3874
3875void
3876TargetProperties::SetStandardInputPath (const char *p)
3877{
3878 const uint32_t idx = ePropertyInputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003879 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003880}
3881
3882FileSpec
3883TargetProperties::GetStandardOutputPath () const
3884{
3885 const uint32_t idx = ePropertyOutputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003886 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003887}
3888
3889void
3890TargetProperties::SetStandardOutputPath (const char *p)
3891{
3892 const uint32_t idx = ePropertyOutputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003893 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003894}
3895
3896FileSpec
3897TargetProperties::GetStandardErrorPath () const
3898{
3899 const uint32_t idx = ePropertyErrorPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003900 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003901}
3902
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003903LanguageType
3904TargetProperties::GetLanguage () const
3905{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003906 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage(nullptr, ePropertyLanguage);
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003907 if (value)
3908 return value->GetCurrentValue();
3909 return LanguageType();
3910}
3911
Greg Clayton6920b522012-08-22 18:39:03 +00003912const char *
3913TargetProperties::GetExpressionPrefixContentsAsCString ()
3914{
3915 const uint32_t idx = ePropertyExprPrefix;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003916 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false, idx);
Greg Clayton6920b522012-08-22 18:39:03 +00003917 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003918 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003919 const bool null_terminate = true;
3920 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003921 if (data_sp)
3922 return (const char *) data_sp->GetBytes();
3923 }
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003924 return nullptr;
Greg Clayton6920b522012-08-22 18:39:03 +00003925}
3926
Greg Clayton67cc0632012-08-22 17:17:09 +00003927void
3928TargetProperties::SetStandardErrorPath (const char *p)
3929{
3930 const uint32_t idx = ePropertyErrorPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003931 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003932}
3933
3934bool
3935TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3936{
3937 const uint32_t idx = ePropertyBreakpointUseAvoidList;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003938 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003939}
3940
Jim Ingham17d023f2013-03-13 17:58:04 +00003941bool
Daniel Malead79ae052013-08-07 21:54:09 +00003942TargetProperties::GetUseHexImmediates () const
3943{
3944 const uint32_t idx = ePropertyUseHexImmediates;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003945 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Daniel Malead79ae052013-08-07 21:54:09 +00003946}
3947
3948bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003949TargetProperties::GetUseFastStepping () const
3950{
3951 const uint32_t idx = ePropertyUseFastStepping;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003952 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham17d023f2013-03-13 17:58:04 +00003953}
3954
Greg Claytonfb6621e2013-12-06 21:59:52 +00003955bool
3956TargetProperties::GetDisplayExpressionsInCrashlogs () const
3957{
3958 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003959 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Claytonfb6621e2013-12-06 21:59:52 +00003960}
3961
Enrico Granata397ddd52013-05-21 20:13:34 +00003962LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003963TargetProperties::GetLoadScriptFromSymbolFile () const
3964{
3965 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003966 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003967}
3968
Jason Molenda878ae012016-02-19 00:05:17 +00003969LoadCWDlldbinitFile
3970TargetProperties::GetLoadCWDlldbinitFile () const
3971{
3972 const uint32_t idx = ePropertyLoadCWDlldbinitFile;
3973 return (LoadCWDlldbinitFile) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
3974}
3975
Daniel Malead79ae052013-08-07 21:54:09 +00003976Disassembler::HexImmediateStyle
3977TargetProperties::GetHexImmediateStyle () const
3978{
3979 const uint32_t idx = ePropertyHexImmediateStyle;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003980 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Daniel Malead79ae052013-08-07 21:54:09 +00003981}
3982
Greg Claytonfd814c52013-08-13 01:42:25 +00003983MemoryModuleLoadLevel
3984TargetProperties::GetMemoryModuleLoadLevel() const
3985{
3986 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003987 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Claytonfd814c52013-08-13 01:42:25 +00003988}
3989
Jason Molendaa4bea722014-02-14 05:06:49 +00003990bool
3991TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
3992{
3993 const uint32_t idx = ePropertyTrapHandlerNames;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003994 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00003995}
Greg Claytonfd814c52013-08-13 01:42:25 +00003996
Jason Molendaa4bea722014-02-14 05:06:49 +00003997void
3998TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
3999{
4000 const uint32_t idx = ePropertyTrapHandlerNames;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004001 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00004002}
Greg Clayton67cc0632012-08-22 17:17:09 +00004003
Enrico Granata560558e2015-02-11 02:35:39 +00004004bool
4005TargetProperties::GetDisplayRuntimeSupportValues () const
4006{
4007 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004008 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Enrico Granata560558e2015-02-11 02:35:39 +00004009}
4010
4011void
4012TargetProperties::SetDisplayRuntimeSupportValues (bool b)
4013{
4014 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004015 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Enrico Granata560558e2015-02-11 02:35:39 +00004016}
4017
Ewan Crawford78baa192015-05-13 09:18:18 +00004018bool
4019TargetProperties::GetNonStopModeEnabled () const
4020{
4021 const uint32_t idx = ePropertyNonStopModeEnabled;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004022 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Ewan Crawford78baa192015-05-13 09:18:18 +00004023}
4024
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004025void
4026TargetProperties::SetNonStopModeEnabled (bool b)
4027{
4028 const uint32_t idx = ePropertyNonStopModeEnabled;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004029 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004030}
4031
Ilia K8f37ca52015-02-13 14:31:06 +00004032const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00004033TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00004034{
Ilia Kcc39d3f2015-02-13 17:07:55 +00004035 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00004036 return m_launch_info;
4037}
4038
4039void
4040TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
4041{
4042 m_launch_info = launch_info;
4043 SetArg0(launch_info.GetArg0());
4044 SetRunArguments(launch_info.GetArguments());
4045 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
4046 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
4047 if (input_file_action)
4048 {
4049 const char *input_path = input_file_action->GetPath();
4050 if (input_path)
4051 SetStandardInputPath(input_path);
4052 }
4053 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
4054 if (output_file_action)
4055 {
4056 const char *output_path = output_file_action->GetPath();
4057 if (output_path)
4058 SetStandardOutputPath(output_path);
4059 }
4060 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
4061 if (error_file_action)
4062 {
4063 const char *error_path = error_file_action->GetPath();
4064 if (error_path)
4065 SetStandardErrorPath(error_path);
4066 }
4067 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
4068 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4069 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
4070}
4071
4072void
4073TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
4074{
4075 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4076 this_->m_launch_info.SetArg0(this_->GetArg0());
4077}
4078
4079void
4080TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
4081{
4082 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4083 Args args;
4084 if (this_->GetRunArguments(args))
4085 this_->m_launch_info.GetArguments() = args;
4086}
4087
4088void
4089TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
4090{
4091 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4092 Args args;
4093 if (this_->GetEnvironmentAsArgs(args))
4094 this_->m_launch_info.GetEnvironmentEntries() = args;
4095}
4096
4097void
4098TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4099{
4100 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004101 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004102}
4103
4104void
4105TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4106{
4107 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004108 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004109}
4110
4111void
4112TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4113{
4114 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004115 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004116}
4117
4118void
4119TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
4120{
4121 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4122 if (this_->GetDetachOnError())
4123 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4124 else
4125 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
4126}
4127
4128void
4129TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
4130{
4131 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4132 if (this_->GetDisableASLR())
4133 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4134 else
4135 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
4136}
4137
4138void
4139TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
4140{
4141 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4142 if (this_->GetDisableSTDIO())
4143 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4144 else
4145 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4146}
Ilia Keb2c19a2015-03-10 21:59:55 +00004147
4148//----------------------------------------------------------------------
4149// Target::TargetEventData
4150//----------------------------------------------------------------------
4151
4152Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
4153 EventData (),
4154 m_target_sp (target_sp),
4155 m_module_list ()
4156{
4157}
4158
4159Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
4160 EventData (),
4161 m_target_sp (target_sp),
4162 m_module_list (module_list)
4163{
4164}
4165
Eugene Zelenko8f30a652015-10-23 18:39:37 +00004166Target::TargetEventData::~TargetEventData() = default;
Ilia Keb2c19a2015-03-10 21:59:55 +00004167
4168const ConstString &
4169Target::TargetEventData::GetFlavorString ()
4170{
4171 static ConstString g_flavor ("Target::TargetEventData");
4172 return g_flavor;
4173}
4174
4175void
4176Target::TargetEventData::Dump (Stream *s) const
4177{
Oleksiy Vyalov8b85b032016-01-19 17:54:47 +00004178 for (size_t i = 0; i < m_module_list.GetSize(); ++i)
4179 {
4180 if (i != 0)
4181 *s << ", ";
4182 m_module_list.GetModuleAtIndex(i)->GetDescription(s, lldb::eDescriptionLevelBrief);
4183 }
Ilia Keb2c19a2015-03-10 21:59:55 +00004184}
4185
4186const Target::TargetEventData *
4187Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4188{
4189 if (event_ptr)
4190 {
4191 const EventData *event_data = event_ptr->GetData();
4192 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4193 return static_cast <const TargetEventData *> (event_ptr->GetData());
4194 }
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004195 return nullptr;
Ilia Keb2c19a2015-03-10 21:59:55 +00004196}
4197
4198TargetSP
4199Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4200{
4201 TargetSP target_sp;
4202 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4203 if (event_data)
4204 target_sp = event_data->m_target_sp;
4205 return target_sp;
4206}
4207
4208ModuleList
4209Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4210{
4211 ModuleList module_list;
4212 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4213 if (event_data)
4214 module_list = event_data->m_module_list;
4215 return module_list;
4216}