blob: a96e4c98361e5d2bf636c09a6cccd85afea46b94 [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,
Jim Ingham24111672016-03-09 18:59:13 +0000345 lldb::addr_t offset,
Greg Clayton1f746072012-08-29 21:13:06 +0000346 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000347 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000348 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000349 bool hardware,
350 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000351{
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000352 FileSpec remapped_file;
353 ConstString remapped_path;
354 if (GetSourcePathMap().ReverseRemapPath(ConstString(file.GetPath().c_str()), remapped_path))
355 remapped_file.SetFile(remapped_path.AsCString(), true);
356 else
357 remapped_file = file;
358
Greg Clayton1f746072012-08-29 21:13:06 +0000359 if (check_inlines == eLazyBoolCalculate)
360 {
361 const InlineStrategy inline_strategy = GetInlineStrategy();
362 switch (inline_strategy)
363 {
364 case eInlineBreakpointsNever:
365 check_inlines = eLazyBoolNo;
366 break;
367
368 case eInlineBreakpointsHeaders:
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000369 if (remapped_file.IsSourceImplementationFile())
Greg Clayton1f746072012-08-29 21:13:06 +0000370 check_inlines = eLazyBoolNo;
371 else
372 check_inlines = eLazyBoolYes;
373 break;
374
375 case eInlineBreakpointsAlways:
376 check_inlines = eLazyBoolYes;
377 break;
378 }
379 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000380 SearchFilterSP filter_sp;
381 if (check_inlines == eLazyBoolNo)
382 {
383 // Not checking for inlines, we are looking only for matching compile units
384 FileSpecList compile_unit_list;
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000385 compile_unit_list.Append (remapped_file);
Greg Clayton93bfb292012-09-07 23:48:57 +0000386 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
387 }
388 else
389 {
390 filter_sp = GetSearchFilterForModuleList (containingModules);
391 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000392 if (skip_prologue == eLazyBoolCalculate)
393 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000394 if (move_to_nearest_code == eLazyBoolCalculate)
395 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000396
Jim Ingham24111672016-03-09 18:59:13 +0000397 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (nullptr,
398 remapped_file,
399 line_no,
400 offset,
401 check_inlines,
402 skip_prologue,
403 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000404 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405}
406
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000408Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000411
412 // Check for any reason we want to move this breakpoint to other address.
413 addr = GetBreakableLoadAddress(addr);
414
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 // Attempt to resolve our load address if possible, though it is ok if
416 // it doesn't resolve to section/offset.
417
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000418 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000419 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000420 if (!so_addr.IsValid())
421 {
422 // The address didn't resolve, so just set this as an absolute address
423 so_addr.SetOffset (addr);
424 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000425 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 return bp_sp;
427}
428
429BreakpointSP
Jim Ingham055a08a2015-11-17 03:39:13 +0000430Target::CreateBreakpoint (const Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000432 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000433 BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(nullptr, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000434 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435}
436
Jim Ingham055a08a2015-11-17 03:39:13 +0000437lldb::BreakpointSP
438Target::CreateAddressInModuleBreakpoint (lldb::addr_t file_addr,
439 bool internal,
440 const FileSpec *file_spec,
441 bool request_hardware)
442{
443 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000444 BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(nullptr, file_addr, file_spec));
Jim Ingham055a08a2015-11-17 03:39:13 +0000445 return CreateBreakpoint (filter_sp, resolver_sp, internal, request_hardware, false);
446}
447
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000449Target::CreateBreakpoint (const FileSpecList *containingModules,
450 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000451 const char *func_name,
Jim Ingham24111672016-03-09 18:59:13 +0000452 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000453 LanguageType language,
Jim Ingham24111672016-03-09 18:59:13 +0000454 lldb::addr_t offset,
Jim Inghama8558b62012-05-22 00:12:20 +0000455 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000456 bool internal,
457 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000459 BreakpointSP bp_sp;
460 if (func_name)
461 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000462 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000463
464 if (skip_prologue == eLazyBoolCalculate)
465 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000466 if (language == lldb::eLanguageTypeUnknown)
467 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000468
Jim Ingham24111672016-03-09 18:59:13 +0000469 BreakpointResolverSP resolver_sp (new BreakpointResolverName (nullptr,
470 func_name,
471 func_name_type_mask,
472 language,
473 Breakpoint::Exact,
474 offset,
475 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000476 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000477 }
478 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
Jim Inghamfab10e82012-03-06 00:37:27 +0000481lldb::BreakpointSP
482Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000483 const FileSpecList *containingSourceFiles,
484 const std::vector<std::string> &func_names,
485 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000486 LanguageType language,
Jim Ingham24111672016-03-09 18:59:13 +0000487 lldb::addr_t offset,
Jim Inghama8558b62012-05-22 00:12:20 +0000488 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000489 bool internal,
490 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000491{
492 BreakpointSP bp_sp;
493 size_t num_names = func_names.size();
494 if (num_names > 0)
495 {
496 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000497
498 if (skip_prologue == eLazyBoolCalculate)
499 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000500 if (language == lldb::eLanguageTypeUnknown)
501 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000502
Jim Ingham24111672016-03-09 18:59:13 +0000503
504 BreakpointResolverSP resolver_sp (new BreakpointResolverName (nullptr,
505 func_names,
506 func_name_type_mask,
507 language,
508 offset,
509 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000510 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000511 }
512 return bp_sp;
513}
514
Jim Ingham133e0fb2012-03-03 02:05:11 +0000515BreakpointSP
516Target::CreateBreakpoint (const FileSpecList *containingModules,
517 const FileSpecList *containingSourceFiles,
518 const char *func_names[],
519 size_t num_names,
Jim Ingham24111672016-03-09 18:59:13 +0000520 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000521 LanguageType language,
Jim Ingham24111672016-03-09 18:59:13 +0000522 lldb::addr_t offset,
Jim Inghama8558b62012-05-22 00:12:20 +0000523 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000524 bool internal,
525 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000526{
527 BreakpointSP bp_sp;
528 if (num_names > 0)
529 {
530 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
531
Greg Clayton03da4cc2013-04-19 21:31:16 +0000532 if (skip_prologue == eLazyBoolCalculate)
Jim Ingham24111672016-03-09 18:59:13 +0000533 {
534 if (offset == 0)
535 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
536 else
537 skip_prologue = eLazyBoolNo;
538 }
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000539 if (language == lldb::eLanguageTypeUnknown)
540 language = GetLanguage();
541
Jim Ingham24111672016-03-09 18:59:13 +0000542 BreakpointResolverSP resolver_sp (new BreakpointResolverName (nullptr,
543 func_names,
544 num_names,
545 func_name_type_mask,
546 language,
547 offset,
548 skip_prologue));
549 resolver_sp->SetOffset(offset);
Jim Ingham1460e4b2014-01-10 23:46:59 +0000550 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000551 }
552 return bp_sp;
553}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000554
555SearchFilterSP
556Target::GetSearchFilterForModule (const FileSpec *containingModule)
557{
558 SearchFilterSP filter_sp;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000559 if (containingModule != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 {
561 // TODO: We should look into sharing module based search filters
562 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000563 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564 }
565 else
566 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000567 if (!m_search_filter_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000568 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569 filter_sp = m_search_filter_sp;
570 }
571 return filter_sp;
572}
573
Jim Ingham969795f2011-09-21 01:17:13 +0000574SearchFilterSP
575Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
576{
577 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000578 if (containingModules && containingModules->GetSize() != 0)
579 {
580 // TODO: We should look into sharing module based search filters
581 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000582 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000583 }
584 else
585 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000586 if (!m_search_filter_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000587 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000588 filter_sp = m_search_filter_sp;
589 }
590 return filter_sp;
591}
592
Jim Ingham87df91b2011-09-23 00:54:11 +0000593SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000594Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
595 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000596{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000597 if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
Jim Ingham87df91b2011-09-23 00:54:11 +0000598 return GetSearchFilterForModuleList(containingModules);
599
600 SearchFilterSP filter_sp;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000601 if (containingModules == nullptr)
Jim Ingham87df91b2011-09-23 00:54:11 +0000602 {
603 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
604 // but that will take a little reworking.
605
Greg Claytone1cd1be2012-01-29 20:56:30 +0000606 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000607 }
608 else
609 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000610 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000611 }
612 return filter_sp;
613}
614
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000616Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000617 const FileSpecList *containingSourceFiles,
618 RegularExpression &func_regex,
Jim Ingham0fcdac32015-11-06 22:48:59 +0000619 lldb::LanguageType requested_language,
Jim Inghama8558b62012-05-22 00:12:20 +0000620 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000621 bool internal,
622 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623{
Jim Ingham87df91b2011-09-23 00:54:11 +0000624 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000625 bool skip =
626 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
627 : static_cast<bool>(skip_prologue);
Jim Ingham24111672016-03-09 18:59:13 +0000628 BreakpointResolverSP resolver_sp(new BreakpointResolverName (nullptr,
629 func_regex,
630 requested_language,
631 0,
632 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633
Jim Ingham1460e4b2014-01-10 23:46:59 +0000634 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635}
636
Jim Ingham219ba192012-03-05 04:47:34 +0000637lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000638Target::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 +0000639{
Jim Inghama72b31c2015-04-22 19:42:18 +0000640 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
641 if (exc_bkpt_sp && additional_args)
642 {
643 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
644 if (precondition_sp && additional_args)
645 {
646 if (error)
647 *error = precondition_sp->ConfigurePrecondition(*additional_args);
648 else
649 precondition_sp->ConfigurePrecondition(*additional_args);
650 }
651 }
652 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000653}
Jim Inghama72b31c2015-04-22 19:42:18 +0000654
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000656Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657{
658 BreakpointSP bp_sp;
659 if (filter_sp && resolver_sp)
660 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000661 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000663 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000665 return bp_sp;
666}
667
668void
669Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
670{
671 if (!bp_sp)
672 return;
673 if (internal)
674 m_internal_breakpoint_list.Add (bp_sp, false);
675 else
676 m_breakpoint_list.Add (bp_sp, true);
677
678 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
679 if (log)
680 {
681 StreamString s;
682 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
683 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
684 }
685
686 bp_sp->ResolveBreakpoint();
687
688 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000689 {
690 m_last_created_breakpoint = bp_sp;
691 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692}
693
Johnny Chen86364b42011-09-20 23:28:55 +0000694bool
695Target::ProcessIsValid()
696{
697 return (m_process_sp && m_process_sp->IsAlive());
698}
699
Johnny Chenb90827e2012-06-04 23:19:54 +0000700static bool
701CheckIfWatchpointsExhausted(Target *target, Error &error)
702{
703 uint32_t num_supported_hardware_watchpoints;
704 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
705 if (rc.Success())
706 {
707 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
708 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
709 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
710 num_supported_hardware_watchpoints);
711 }
712 return false;
713}
714
Johnny Chen01a67862011-10-14 00:42:25 +0000715// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000716// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000717WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000718Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000719{
Greg Clayton5160ce52013-03-27 23:08:40 +0000720 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000721 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000722 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000723 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000724
Johnny Chen01a67862011-10-14 00:42:25 +0000725 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000726 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000727 {
728 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000729 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000730 }
Jim Inghamc6462312013-06-18 21:52:48 +0000731
Johnny Chen45e541f2011-09-14 22:20:15 +0000732 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000733 {
734 if (size == 0)
735 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
736 else
Daniel Malead01b2952012-11-29 21:49:15 +0000737 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000738 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000739 }
Jim Inghamc6462312013-06-18 21:52:48 +0000740
741 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
742 {
743 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
744 }
Johnny Chen7313a642011-09-13 01:15:36 +0000745
Johnny Chen01a67862011-10-14 00:42:25 +0000746 // Currently we only support one watchpoint per address, with total number
747 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000748
749 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000750 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 +0000751 Mutex::Locker locker;
752 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000753 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000754 if (matched_sp)
755 {
Johnny Chen0c406372011-09-14 20:23:45 +0000756 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000757 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000758 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
759 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000760 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000761 if (size == old_size && kind == old_type)
762 {
Johnny Chen01a67862011-10-14 00:42:25 +0000763 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000764 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000765 }
766 else
767 {
Johnny Chen01a67862011-10-14 00:42:25 +0000768 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000769 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
770 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000771 }
Johnny Chen3c532582011-09-13 23:29:31 +0000772 }
773
Jason Molenda727e3922012-12-05 23:07:34 +0000774 if (!wp_sp)
775 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000776 wp_sp.reset(new Watchpoint(*this, addr, size, type));
777 wp_sp->SetWatchpointType(kind, notify);
778 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000779 }
Johnny Chen0c406372011-09-14 20:23:45 +0000780
Jim Ingham1b5792e2012-12-18 02:03:49 +0000781 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000782 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000783 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
784 __FUNCTION__,
785 error.Success() ? "succeeded" : "failed",
786 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000787
Jason Molenda727e3922012-12-05 23:07:34 +0000788 if (error.Fail())
789 {
Johnny Chen41b77262012-03-26 22:00:10 +0000790 // Enabling the watchpoint on the device side failed.
791 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000792 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000793 // See if we could provide more helpful error message.
794 if (!CheckIfWatchpointsExhausted(this, error))
795 {
796 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000797 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000798 }
Johnny Chen01a67862011-10-14 00:42:25 +0000799 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000800 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000801 else
Johnny Chen01a67862011-10-14 00:42:25 +0000802 m_last_created_watchpoint = wp_sp;
803 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000804}
805
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806void
807Target::RemoveAllBreakpoints (bool internal_also)
808{
Greg Clayton5160ce52013-03-27 23:08:40 +0000809 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 if (log)
811 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
812
Greg Clayton9fed0d82010-07-23 23:33:17 +0000813 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000815 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000816
817 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818}
819
820void
821Target::DisableAllBreakpoints (bool internal_also)
822{
Greg Clayton5160ce52013-03-27 23:08:40 +0000823 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000824 if (log)
825 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
826
827 m_breakpoint_list.SetEnabledAll (false);
828 if (internal_also)
829 m_internal_breakpoint_list.SetEnabledAll (false);
830}
831
832void
833Target::EnableAllBreakpoints (bool internal_also)
834{
Greg Clayton5160ce52013-03-27 23:08:40 +0000835 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000836 if (log)
837 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
838
839 m_breakpoint_list.SetEnabledAll (true);
840 if (internal_also)
841 m_internal_breakpoint_list.SetEnabledAll (true);
842}
843
844bool
845Target::RemoveBreakpointByID (break_id_t break_id)
846{
Greg Clayton5160ce52013-03-27 23:08:40 +0000847 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 if (log)
849 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
850
851 if (DisableBreakpointByID (break_id))
852 {
853 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000854 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000856 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000857 if (m_last_created_breakpoint)
858 {
859 if (m_last_created_breakpoint->GetID() == break_id)
860 m_last_created_breakpoint.reset();
861 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000862 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000863 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864 return true;
865 }
866 return false;
867}
868
869bool
870Target::DisableBreakpointByID (break_id_t break_id)
871{
Greg Clayton5160ce52013-03-27 23:08:40 +0000872 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873 if (log)
874 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
875
876 BreakpointSP bp_sp;
877
878 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
879 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
880 else
881 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
882 if (bp_sp)
883 {
884 bp_sp->SetEnabled (false);
885 return true;
886 }
887 return false;
888}
889
890bool
891Target::EnableBreakpointByID (break_id_t break_id)
892{
Greg Clayton5160ce52013-03-27 23:08:40 +0000893 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000894 if (log)
895 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
896 __FUNCTION__,
897 break_id,
898 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
899
900 BreakpointSP bp_sp;
901
902 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
903 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
904 else
905 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
906
907 if (bp_sp)
908 {
909 bp_sp->SetEnabled (true);
910 return true;
911 }
912 return false;
913}
914
Johnny Chenedf50372011-09-23 21:21:43 +0000915// The flag 'end_to_end', default to true, signifies that the operation is
916// performed end to end, for both the debugger and the debuggee.
917
Johnny Chen01a67862011-10-14 00:42:25 +0000918// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
919// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000920bool
Johnny Chen01a67862011-10-14 00:42:25 +0000921Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000922{
Greg Clayton5160ce52013-03-27 23:08:40 +0000923 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000924 if (log)
925 log->Printf ("Target::%s\n", __FUNCTION__);
926
Johnny Chenedf50372011-09-23 21:21:43 +0000927 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000928 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000929 return true;
930 }
931
932 // Otherwise, it's an end to end operation.
933
Johnny Chen86364b42011-09-20 23:28:55 +0000934 if (!ProcessIsValid())
935 return false;
936
Johnny Chen01a67862011-10-14 00:42:25 +0000937 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000938 for (size_t i = 0; i < num_watchpoints; ++i)
939 {
Johnny Chen01a67862011-10-14 00:42:25 +0000940 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
941 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000942 return false;
943
Johnny Chen01a67862011-10-14 00:42:25 +0000944 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000945 if (rc.Fail())
946 return false;
947 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000948 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000949 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000950 return true; // Success!
951}
952
Johnny Chen01a67862011-10-14 00:42:25 +0000953// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
954// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000955bool
Johnny Chen01a67862011-10-14 00:42:25 +0000956Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000957{
Greg Clayton5160ce52013-03-27 23:08:40 +0000958 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000959 if (log)
960 log->Printf ("Target::%s\n", __FUNCTION__);
961
Johnny Chenedf50372011-09-23 21:21:43 +0000962 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000963 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000964 return true;
965 }
966
967 // Otherwise, it's an end to end operation.
968
Johnny Chen86364b42011-09-20 23:28:55 +0000969 if (!ProcessIsValid())
970 return false;
971
Johnny Chen01a67862011-10-14 00:42:25 +0000972 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000973 for (size_t i = 0; i < num_watchpoints; ++i)
974 {
Johnny Chen01a67862011-10-14 00:42:25 +0000975 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
976 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000977 return false;
978
Johnny Chen01a67862011-10-14 00:42:25 +0000979 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000980 if (rc.Fail())
981 return false;
982 }
Johnny Chen86364b42011-09-20 23:28:55 +0000983 return true; // Success!
984}
985
Johnny Chen01a67862011-10-14 00:42:25 +0000986// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
987// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000988bool
Johnny Chen01a67862011-10-14 00:42:25 +0000989Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000990{
Greg Clayton5160ce52013-03-27 23:08:40 +0000991 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000992 if (log)
993 log->Printf ("Target::%s\n", __FUNCTION__);
994
Johnny Chenedf50372011-09-23 21:21:43 +0000995 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000996 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000997 return true;
998 }
999
1000 // Otherwise, it's an end to end operation.
1001
Johnny Chen86364b42011-09-20 23:28:55 +00001002 if (!ProcessIsValid())
1003 return false;
1004
Johnny Chen01a67862011-10-14 00:42:25 +00001005 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +00001006 for (size_t i = 0; i < num_watchpoints; ++i)
1007 {
Johnny Chen01a67862011-10-14 00:42:25 +00001008 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1009 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001010 return false;
1011
Johnny Chen01a67862011-10-14 00:42:25 +00001012 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +00001013 if (rc.Fail())
1014 return false;
1015 }
Johnny Chen86364b42011-09-20 23:28:55 +00001016 return true; // Success!
1017}
1018
Johnny Chena4d6bc92012-02-25 06:44:30 +00001019// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1020bool
1021Target::ClearAllWatchpointHitCounts ()
1022{
Greg Clayton5160ce52013-03-27 23:08:40 +00001023 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +00001024 if (log)
1025 log->Printf ("Target::%s\n", __FUNCTION__);
1026
1027 size_t num_watchpoints = m_watchpoint_list.GetSize();
1028 for (size_t i = 0; i < num_watchpoints; ++i)
1029 {
1030 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1031 if (!wp_sp)
1032 return false;
1033
1034 wp_sp->ResetHitCount();
1035 }
1036 return true; // Success!
1037}
1038
Enrico Granata5e3fe042015-02-11 00:37:54 +00001039// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1040bool
1041Target::ClearAllWatchpointHistoricValues ()
1042{
1043 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
1044 if (log)
1045 log->Printf ("Target::%s\n", __FUNCTION__);
1046
1047 size_t num_watchpoints = m_watchpoint_list.GetSize();
1048 for (size_t i = 0; i < num_watchpoints; ++i)
1049 {
1050 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1051 if (!wp_sp)
1052 return false;
1053
1054 wp_sp->ResetHistoricValues();
1055 }
1056 return true; // Success!
1057}
1058
Johnny Chen01a67862011-10-14 00:42:25 +00001059// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +00001060// during these operations.
1061bool
Johnny Chen01a67862011-10-14 00:42:25 +00001062Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001063{
Greg Clayton5160ce52013-03-27 23:08:40 +00001064 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001065 if (log)
1066 log->Printf ("Target::%s\n", __FUNCTION__);
1067
1068 if (!ProcessIsValid())
1069 return false;
1070
Johnny Chen01a67862011-10-14 00:42:25 +00001071 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +00001072 for (size_t i = 0; i < num_watchpoints; ++i)
1073 {
Johnny Chen01a67862011-10-14 00:42:25 +00001074 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1075 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001076 return false;
1077
Johnny Chen01a67862011-10-14 00:42:25 +00001078 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001079 }
1080 return true; // Success!
1081}
1082
Johnny Chen01a67862011-10-14 00:42:25 +00001083// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001084bool
Johnny Chen01a67862011-10-14 00:42:25 +00001085Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001086{
Greg Clayton5160ce52013-03-27 23:08:40 +00001087 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001088 if (log)
1089 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1090
1091 if (!ProcessIsValid())
1092 return false;
1093
Johnny Chen01a67862011-10-14 00:42:25 +00001094 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1095 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001096 {
Johnny Chen01a67862011-10-14 00:42:25 +00001097 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001098 if (rc.Success())
1099 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001100
Johnny Chenf04ee932011-09-22 18:04:58 +00001101 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001102 }
1103 return false;
1104}
1105
Johnny Chen01a67862011-10-14 00:42:25 +00001106// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001107bool
Johnny Chen01a67862011-10-14 00:42:25 +00001108Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001109{
Greg Clayton5160ce52013-03-27 23:08:40 +00001110 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001111 if (log)
1112 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1113
1114 if (!ProcessIsValid())
1115 return false;
1116
Johnny Chen01a67862011-10-14 00:42:25 +00001117 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1118 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001119 {
Johnny Chen01a67862011-10-14 00:42:25 +00001120 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001121 if (rc.Success())
1122 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001123
Johnny Chenf04ee932011-09-22 18:04:58 +00001124 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001125 }
1126 return false;
1127}
1128
Johnny Chen01a67862011-10-14 00:42:25 +00001129// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001130bool
Johnny Chen01a67862011-10-14 00:42:25 +00001131Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001132{
Greg Clayton5160ce52013-03-27 23:08:40 +00001133 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001134 if (log)
1135 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1136
Jim Inghamb0b45132013-07-02 02:09:46 +00001137 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1138 if (watch_to_remove_sp == m_last_created_watchpoint)
1139 m_last_created_watchpoint.reset();
1140
Johnny Chen01a67862011-10-14 00:42:25 +00001141 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001142 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001143 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001144 return true;
1145 }
1146 return false;
1147}
1148
Johnny Chen01a67862011-10-14 00:42:25 +00001149// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001150bool
Johnny Chen01a67862011-10-14 00:42:25 +00001151Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001152{
Greg Clayton5160ce52013-03-27 23:08:40 +00001153 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001154 if (log)
1155 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1156
1157 if (!ProcessIsValid())
1158 return false;
1159
Johnny Chen01a67862011-10-14 00:42:25 +00001160 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1161 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001162 {
Johnny Chen01a67862011-10-14 00:42:25 +00001163 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001164 return true;
1165 }
1166 return false;
1167}
1168
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169ModuleSP
1170Target::GetExecutableModule ()
1171{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001172 // search for the first executable in the module list
1173 for (size_t i = 0; i < m_images.GetSize(); ++i)
1174 {
1175 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1176 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1177 if (obj == nullptr)
1178 continue;
1179 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1180 return module_sp;
1181 }
1182 // as fall back return the first module loaded
1183 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001184}
1185
1186Module*
1187Target::GetExecutableModulePointer ()
1188{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001189 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190}
1191
Enrico Granata17598482012-11-08 02:22:02 +00001192static void
1193LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1194{
1195 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001196 StreamString feedback_stream;
1197 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001198 {
Enrico Granata97303392013-05-21 00:00:30 +00001199 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001200 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001201 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1202 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001203 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001204 if (feedback_stream.GetSize())
1205 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1206 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001207}
1208
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209void
Greg Claytonb35db632013-11-09 00:03:31 +00001210Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211{
Greg Claytonb35db632013-11-09 00:03:31 +00001212 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001213 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214 m_images.Clear();
Greg Clayton5beec212015-10-08 21:04:34 +00001215 m_scratch_type_system_map.Clear();
Sean Callanana3444ff2015-11-10 22:54:42 +00001216 m_ast_importer_sp.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001217}
1218
1219void
Greg Claytonb35db632013-11-09 00:03:31 +00001220Target::DidExec ()
1221{
1222 // When a process exec's we need to know about it so we can do some cleanup.
1223 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1224 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1225}
1226
1227void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001228Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1229{
1230 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001231 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001232
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001233 if (executable_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234 {
1235 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001236 "Target::SetExecutableModule (executable = '%s')",
1237 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001238
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001239 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240
Jim Ingham5aee1622010-08-09 23:31:02 +00001241 // 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 +00001242 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001243 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001244 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001245 if (log)
1246 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1247 }
1248
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001249 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001250 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001251
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001252 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253 {
1254 executable_objfile->GetDependentModules(dependent_files);
1255 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1256 {
Greg Claytonded470d2011-03-19 01:12:21 +00001257 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1258 FileSpec platform_dependent_file_spec;
1259 if (m_platform_sp)
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001260 m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001261 else
1262 platform_dependent_file_spec = dependent_file_spec;
1263
Greg Claytonb9a01b32012-02-26 05:51:37 +00001264 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1265 ModuleSP image_module_sp(GetSharedModule (module_spec));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001266 if (image_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001267 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001268 ObjectFile *objfile = image_module_sp->GetObjectFile();
1269 if (objfile)
1270 objfile->GetDependentModules(dependent_files);
1271 }
1272 }
1273 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001274 }
1275}
1276
Jim Ingham5aee1622010-08-09 23:31:02 +00001277bool
1278Target::SetArchitecture (const ArchSpec &arch_spec)
1279{
Greg Clayton5160ce52013-03-27 23:08:40 +00001280 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001281 bool missing_local_arch = !m_arch.IsValid();
Jason Molendadad8af42015-11-10 04:11:37 +00001282 bool replace_local_arch = true;
1283 bool compatible_local_arch = false;
1284 ArchSpec other(arch_spec);
1285
1286 if (!missing_local_arch)
Jim Ingham5aee1622010-08-09 23:31:02 +00001287 {
Jason Molendadad8af42015-11-10 04:11:37 +00001288 if (m_arch.IsCompatibleMatch(arch_spec))
1289 {
1290 other.MergeFrom(m_arch);
1291
1292 if (m_arch.IsCompatibleMatch(other))
1293 {
1294 compatible_local_arch = true;
1295 bool arch_changed, vendor_changed, os_changed, os_ver_changed, env_changed;
1296
1297 m_arch.PiecewiseTripleCompare(other,
1298 arch_changed,
1299 vendor_changed,
1300 os_changed,
1301 os_ver_changed,
1302 env_changed);
1303
1304 if (!arch_changed && !vendor_changed && !os_changed)
1305 replace_local_arch = false;
1306 }
1307 }
1308 }
1309
1310 if (compatible_local_arch || missing_local_arch)
1311 {
1312 // If we haven't got a valid arch spec, or the architectures are compatible
1313 // update the architecture, unless the one we already have is more specified
1314 if (replace_local_arch)
1315 m_arch = other;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001316 if (log)
Jason Molendadad8af42015-11-10 04:11:37 +00001317 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 +00001318 return true;
1319 }
Jason Molendadad8af42015-11-10 04:11:37 +00001320
1321 // If we have an executable file, try to reset the executable to the desired architecture
1322 if (log)
1323 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1324 m_arch = other;
1325 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001326
Jason Molendadad8af42015-11-10 04:11:37 +00001327 ClearModules(true);
1328 // Need to do something about unsetting breakpoints.
1329
1330 if (executable_sp)
1331 {
1332 if (log)
1333 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1334 ModuleSpec module_spec (executable_sp->GetFileSpec(), other);
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001335 Error error = ModuleList::GetSharedModule(module_spec,
1336 executable_sp,
1337 &GetExecutableSearchPaths(),
1338 nullptr,
1339 nullptr);
Jason Molendadad8af42015-11-10 04:11:37 +00001340
1341 if (!error.Fail() && executable_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +00001342 {
Jason Molendadad8af42015-11-10 04:11:37 +00001343 SetExecutableModule (executable_sp, true);
1344 return true;
Jim Ingham5aee1622010-08-09 23:31:02 +00001345 }
1346 }
Greg Clayton70512312012-05-08 01:45:38 +00001347 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001348}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001350bool
1351Target::MergeArchitecture (const ArchSpec &arch_spec)
1352{
1353 if (arch_spec.IsValid())
1354 {
1355 if (m_arch.IsCompatibleMatch(arch_spec))
1356 {
1357 // The current target arch is compatible with "arch_spec", see if we
1358 // can improve our current architecture using bits from "arch_spec"
1359
1360 // Merge bits from arch_spec into "merged_arch" and set our architecture
1361 ArchSpec merged_arch (m_arch);
1362 merged_arch.MergeFrom (arch_spec);
1363 return SetArchitecture(merged_arch);
1364 }
1365 else
1366 {
1367 // The new architecture is different, we just need to replace it
1368 return SetArchitecture(arch_spec);
1369 }
1370 }
1371 return false;
1372}
1373
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001374void
Enrico Granataefe637d2012-11-08 19:16:03 +00001375Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001376{
1377}
1378
1379void
Enrico Granataefe637d2012-11-08 19:16:03 +00001380Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001381{
1382 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001383 if (m_valid)
1384 {
1385 ModuleList my_module_list;
1386 my_module_list.Append(module_sp);
1387 LoadScriptingResourceForModule(module_sp, this);
1388 ModulesDidLoad (my_module_list);
1389 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001390}
1391
1392void
Enrico Granataefe637d2012-11-08 19:16:03 +00001393Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001394{
Adrian McCarthy6ecdbc82015-10-15 22:39:55 +00001395 // A module is being removed from this target.
Greg Clayton23f8c952014-03-24 23:10:19 +00001396 if (m_valid)
1397 {
1398 ModuleList my_module_list;
1399 my_module_list.Append(module_sp);
1400 ModulesDidUnload (my_module_list, false);
1401 }
Enrico Granata17598482012-11-08 02:22:02 +00001402}
1403
1404void
Enrico Granataefe637d2012-11-08 19:16:03 +00001405Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001406{
Jim Inghame716ae02011-08-03 01:00:06 +00001407 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001408 if (m_valid)
Jason Molendad91c3fd2015-11-10 04:18:12 +00001409 {
Greg Clayton23f8c952014-03-24 23:10:19 +00001410 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001411 m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
1412 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001413}
1414
1415void
1416Target::ModulesDidLoad (ModuleList &module_list)
1417{
Greg Clayton23f8c952014-03-24 23:10:19 +00001418 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001419 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001420 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001421 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001422 if (m_process_sp)
1423 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001424 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001425 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001426 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001427 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428}
1429
1430void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001431Target::SymbolsDidLoad (ModuleList &module_list)
1432{
Greg Clayton23f8c952014-03-24 23:10:19 +00001433 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001434 {
Jim Ingham31caf982013-06-04 23:01:35 +00001435 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001436 {
Jim Ingham31caf982013-06-04 23:01:35 +00001437 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1438 if (runtime)
1439 {
1440 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1441 objc_runtime->SymbolsDidLoad(module_list);
1442 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001443 }
Jim Ingham31caf982013-06-04 23:01:35 +00001444
Greg Clayton095eeaa2013-11-05 23:28:00 +00001445 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001446 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001447 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001448 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001449}
1450
1451void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001452Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001453{
Greg Clayton23f8c952014-03-24 23:10:19 +00001454 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001455 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001456 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001457 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001458 m_internal_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001459 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001460 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461}
1462
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001463bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001464Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001465{
Greg Clayton67cc0632012-08-22 17:17:09 +00001466 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001467 {
1468 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001469 ModuleSpec module_spec (module_file_spec);
1470 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001471
1472 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1473 // black list.
1474 if (num_modules > 0)
1475 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001476 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001477 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001478 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001479 return false;
1480 }
1481 return true;
1482 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001483 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001484 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001485}
1486
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001487bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001488Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001489{
Greg Clayton67cc0632012-08-22 17:17:09 +00001490 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001491 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001492 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001493 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001494 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001495 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001496}
1497
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001498size_t
Greg Claytondb598232011-01-07 01:57:07 +00001499Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1500{
Greg Claytone72dfb32012-02-24 01:59:29 +00001501 SectionSP section_sp (addr.GetSection());
1502 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001503 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001504 // 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 +00001505 if (section_sp->IsEncrypted())
1506 {
Greg Clayton57f06302012-05-25 17:05:55 +00001507 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001508 return 0;
1509 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001510 ModuleSP module_sp (section_sp->GetModule());
1511 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001512 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001513 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1514 if (objfile)
1515 {
1516 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1517 addr.GetOffset(),
1518 dst,
1519 dst_len);
1520 if (bytes_read > 0)
1521 return bytes_read;
1522 else
1523 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1524 }
Greg Claytondb598232011-01-07 01:57:07 +00001525 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001526 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001527 }
1528 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001529 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001530 }
1531 else
Greg Claytondb598232011-01-07 01:57:07 +00001532 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001533
Greg Claytondb598232011-01-07 01:57:07 +00001534 return 0;
1535}
1536
1537size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001538Target::ReadMemory (const Address& addr,
1539 bool prefer_file_cache,
1540 void *dst,
1541 size_t dst_len,
1542 Error &error,
1543 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001544{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001545 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001546
Enrico Granata9128ee22011-09-06 19:20:51 +00001547 // if we end up reading this from process memory, we will fill this
1548 // with the actual load address
1549 if (load_addr_ptr)
1550 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1551
Greg Claytondb598232011-01-07 01:57:07 +00001552 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001553
1554 addr_t load_addr = LLDB_INVALID_ADDRESS;
1555 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001556 Address resolved_addr;
1557 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001558 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001559 SectionLoadList &section_load_list = GetSectionLoadList();
1560 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001561 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001562 // No sections are loaded, so we must assume we are not running
1563 // yet and anything we are given is a file address.
1564 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1565 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001566 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001567 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001568 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001569 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001570 // we have manually loaded some sections with "target modules load ..."
1571 // or because we have have a live process that has sections loaded
1572 // through the dynamic loader
1573 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001574 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001575 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001576 }
Greg Clayton357132e2011-03-26 19:14:58 +00001577 if (!resolved_addr.IsValid())
1578 resolved_addr = addr;
Greg Claytonc749eb82011-07-11 05:12:02 +00001579
Greg Claytondb598232011-01-07 01:57:07 +00001580 if (prefer_file_cache)
1581 {
1582 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1583 if (bytes_read > 0)
1584 return bytes_read;
1585 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001586
Johnny Chen86364b42011-09-20 23:28:55 +00001587 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001588 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001589 if (load_addr == LLDB_INVALID_ADDRESS)
1590 load_addr = resolved_addr.GetLoadAddress (this);
1591
Greg Claytondda4f7b2010-06-30 23:03:03 +00001592 if (load_addr == LLDB_INVALID_ADDRESS)
1593 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001594 ModuleSP addr_module_sp (resolved_addr.GetModule());
1595 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001596 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001597 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001598 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001599 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001600 else
Daniel Malead01b2952012-11-29 21:49:15 +00001601 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001602 }
1603 else
1604 {
Greg Claytondb598232011-01-07 01:57:07 +00001605 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001606 if (bytes_read != dst_len)
1607 {
1608 if (error.Success())
1609 {
1610 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001611 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001612 else
Daniel Malead01b2952012-11-29 21:49:15 +00001613 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 +00001614 }
1615 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001616 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001617 {
1618 if (load_addr_ptr)
1619 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001620 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001621 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001622 // If the address is not section offset we have an address that
1623 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001624 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001625 // more we can do. If it is section offset, we might be able to
1626 // read cached memory from the object file.
1627 if (!resolved_addr.IsSectionOffset())
1628 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001629 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001631
Greg Claytonc749eb82011-07-11 05:12:02 +00001632 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001633 {
Greg Claytondb598232011-01-07 01:57:07 +00001634 // If we didn't already try and read from the object file cache, then
1635 // try it after failing to read from the process.
1636 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001637 }
1638 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001639}
1640
Greg Claytond16e1e52011-07-12 17:06:17 +00001641size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001642Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1643{
1644 char buf[256];
1645 out_str.clear();
1646 addr_t curr_addr = addr.GetLoadAddress(this);
1647 Address address(addr);
1648 while (1)
1649 {
1650 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1651 if (length == 0)
1652 break;
1653 out_str.append(buf, length);
1654 // If we got "length - 1" bytes, we didn't get the whole C string, we
1655 // need to read some more characters
1656 if (length == sizeof(buf) - 1)
1657 curr_addr += length;
1658 else
1659 break;
1660 address = Address(curr_addr);
1661 }
1662 return out_str.size();
1663}
1664
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001665size_t
1666Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1667{
1668 size_t total_cstr_len = 0;
1669 if (dst && dst_max_len)
1670 {
1671 result_error.Clear();
1672 // NULL out everything just to be safe
1673 memset (dst, 0, dst_max_len);
1674 Error error;
1675 addr_t curr_addr = addr.GetLoadAddress(this);
1676 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001677
1678 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1679 // think this really needs to be tied to the memory cache subsystem's
1680 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001681 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001682
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001683 size_t bytes_left = dst_max_len - 1;
1684 char *curr_dst = dst;
1685
1686 while (bytes_left > 0)
1687 {
1688 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1689 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1690 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1691
1692 if (bytes_read == 0)
1693 {
1694 result_error = error;
1695 dst[total_cstr_len] = '\0';
1696 break;
1697 }
1698 const size_t len = strlen(curr_dst);
1699
1700 total_cstr_len += len;
1701
1702 if (len < bytes_to_read)
1703 break;
1704
1705 curr_dst += bytes_read;
1706 curr_addr += bytes_read;
1707 bytes_left -= bytes_read;
1708 address = Address(curr_addr);
1709 }
1710 }
1711 else
1712 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001713 if (dst == nullptr)
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001714 result_error.SetErrorString("invalid arguments");
1715 else
1716 result_error.Clear();
1717 }
1718 return total_cstr_len;
1719}
1720
1721size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001722Target::ReadScalarIntegerFromMemory (const Address& addr,
1723 bool prefer_file_cache,
1724 uint32_t byte_size,
1725 bool is_signed,
1726 Scalar &scalar,
1727 Error &error)
1728{
1729 uint64_t uval;
1730
1731 if (byte_size <= sizeof(uval))
1732 {
1733 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1734 if (bytes_read == byte_size)
1735 {
1736 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001737 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001738 if (byte_size <= 4)
1739 scalar = data.GetMaxU32 (&offset, byte_size);
1740 else
1741 scalar = data.GetMaxU64 (&offset, byte_size);
1742
1743 if (is_signed)
1744 scalar.SignExtend(byte_size * 8);
1745 return bytes_read;
1746 }
1747 }
1748 else
1749 {
1750 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1751 }
1752 return 0;
1753}
1754
1755uint64_t
1756Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1757 bool prefer_file_cache,
1758 size_t integer_byte_size,
1759 uint64_t fail_value,
1760 Error &error)
1761{
1762 Scalar scalar;
1763 if (ReadScalarIntegerFromMemory (addr,
1764 prefer_file_cache,
1765 integer_byte_size,
1766 false,
1767 scalar,
1768 error))
1769 return scalar.ULongLong(fail_value);
1770 return fail_value;
1771}
1772
1773bool
1774Target::ReadPointerFromMemory (const Address& addr,
1775 bool prefer_file_cache,
1776 Error &error,
1777 Address &pointer_addr)
1778{
1779 Scalar scalar;
1780 if (ReadScalarIntegerFromMemory (addr,
1781 prefer_file_cache,
1782 m_arch.GetAddressByteSize(),
1783 false,
1784 scalar,
1785 error))
1786 {
1787 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1788 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1789 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001790 SectionLoadList &section_load_list = GetSectionLoadList();
1791 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001792 {
1793 // No sections are loaded, so we must assume we are not running
1794 // yet and anything we are given is a file address.
1795 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1796 }
1797 else
1798 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001799 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001800 // we have manually loaded some sections with "target modules load ..."
1801 // or because we have have a live process that has sections loaded
1802 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001803 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001804 }
1805 // We weren't able to resolve the pointer value, so just return
1806 // an address with no section
1807 if (!pointer_addr.IsValid())
1808 pointer_addr.SetOffset (pointer_vm_addr);
1809 return true;
1810
1811 }
1812 }
1813 return false;
1814}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001815
1816ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001817Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001818{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819 ModuleSP module_sp;
1820
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001821 Error error;
1822
Jim Ingham4a94c912012-05-17 18:38:42 +00001823 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1824 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001825
Jim Ingham4a94c912012-05-17 18:38:42 +00001826 if (module_spec.GetUUID().IsValid())
1827 module_sp = m_images.FindFirstModule(module_spec);
1828
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001829 if (!module_sp)
1830 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001831 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1832 bool did_create_module = false;
1833
1834 // If there are image search path entries, try to use them first to acquire a suitable image.
1835 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001836 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001837 ModuleSpec transformed_spec (module_spec);
1838 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1839 {
1840 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1841 error = ModuleList::GetSharedModule (transformed_spec,
1842 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001843 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001844 &old_module_sp,
1845 &did_create_module);
1846 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001847 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001848
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001849 if (!module_sp)
1850 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001851 // If we have a UUID, we can check our global shared module list in case
1852 // we already have it. If we don't have a valid UUID, then we can't since
1853 // the path in "module_spec" will be a platform path, and we will need to
1854 // let the platform find that file. For example, we could be asking for
1855 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1856 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1857 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1858 // cache.
1859 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001860 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001861 // We have a UUID, it is OK to check the global module list...
1862 error = ModuleList::GetSharedModule (module_spec,
1863 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001864 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001865 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001866 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001867 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001868
1869 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001870 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001871 // The platform is responsible for finding and caching an appropriate
1872 // module in the shared module cache.
1873 if (m_platform_sp)
1874 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001875 error = m_platform_sp->GetSharedModule (module_spec,
1876 m_process_sp.get(),
1877 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001878 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001879 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001880 &did_create_module);
1881 }
1882 else
1883 {
1884 error.SetErrorString("no platform is currently set");
1885 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001886 }
1887 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001888
Jim Ingham4a94c912012-05-17 18:38:42 +00001889 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1890 // module in the list already, and if there was, let's remove it.
1891 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001892 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001893 ObjectFile *objfile = module_sp->GetObjectFile();
1894 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001895 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001896 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001897 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001898 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1899 case ObjectFile::eTypeExecutable: /// A normal executable
1900 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1901 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1902 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1903 break;
1904 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1905 if (error_ptr)
1906 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1907 return ModuleSP();
1908 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1909 if (error_ptr)
1910 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1911 return ModuleSP();
1912 default:
1913 if (error_ptr)
1914 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1915 return ModuleSP();
1916 }
1917 // GetSharedModule is not guaranteed to find the old shared module, for instance
1918 // in the common case where you pass in the UUID, it is only going to find the one
1919 // module matching the UUID. In fact, it has no good way to know what the "old module"
1920 // relevant to this target is, since there might be many copies of a module with this file spec
1921 // in various running debug sessions, but only one of them will belong to this target.
1922 // So let's remove the UUID from the module list, and look in the target's module list.
1923 // Only do this if there is SOMETHING else in the module spec...
1924 if (!old_module_sp)
1925 {
1926 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001927 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001928 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1929 module_spec_copy.GetUUID().Clear();
1930
1931 ModuleList found_modules;
1932 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1933 if (num_found == 1)
1934 {
1935 old_module_sp = found_modules.GetModuleAtIndex(0);
1936 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001937 }
1938 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001939
1940 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1941 {
1942 m_images.ReplaceModule(old_module_sp, module_sp);
1943 Module *old_module_ptr = old_module_sp.get();
1944 old_module_sp.reset();
1945 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1946 }
1947 else
1948 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001949 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001950 else
1951 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952 }
1953 }
1954 if (error_ptr)
1955 *error_ptr = error;
1956 return module_sp;
1957}
1958
Greg Claytond9e416c2012-02-18 05:35:26 +00001959TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001960Target::CalculateTarget ()
1961{
Greg Claytond9e416c2012-02-18 05:35:26 +00001962 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001963}
1964
Greg Claytond9e416c2012-02-18 05:35:26 +00001965ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001966Target::CalculateProcess ()
1967{
Sean Callanan92734d12016-02-12 19:45:31 +00001968 return m_process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969}
1970
Greg Claytond9e416c2012-02-18 05:35:26 +00001971ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001972Target::CalculateThread ()
1973{
Greg Claytond9e416c2012-02-18 05:35:26 +00001974 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975}
1976
Jason Molendab57e4a12013-11-04 09:33:30 +00001977StackFrameSP
1978Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979{
Jason Molendab57e4a12013-11-04 09:33:30 +00001980 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981}
1982
1983void
Greg Clayton0603aa92010-10-04 01:05:56 +00001984Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985{
Greg Claytonc14ee322011-09-22 04:58:26 +00001986 exe_ctx.Clear();
1987 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001988}
1989
1990PathMappingList &
1991Target::GetImageSearchPathList ()
1992{
1993 return m_image_search_paths;
1994}
1995
1996void
Eugene Zelenko8f30a652015-10-23 18:39:37 +00001997Target::ImageSearchPathsChanged(const PathMappingList &path_list,
1998 void *baton)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001999{
2000 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00002001 ModuleSP exe_module_sp (target->GetExecutableModule());
2002 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00002003 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004}
2005
Jim Ingham151c0322015-09-15 21:13:50 +00002006TypeSystem *
Sean Callanana994b0b2015-10-02 18:40:30 +00002007Target::GetScratchTypeSystemForLanguage (Error *error, lldb::LanguageType language, bool create_on_demand)
Jim Ingham151c0322015-09-15 21:13:50 +00002008{
Greg Clayton5beec212015-10-08 21:04:34 +00002009 if (!m_valid)
2010 return nullptr;
2011
Sean Callanana994b0b2015-10-02 18:40:30 +00002012 if (error)
2013 {
2014 error->Clear();
2015 }
2016
Sean Callananb92bd752015-10-01 16:28:02 +00002017 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Sean Callanana994b0b2015-10-02 18:40:30 +00002018 || language == eLanguageTypeUnknown)
2019 {
Sean Callananc7b054a2015-10-09 18:01:10 +00002020 std::set<lldb::LanguageType> languages_for_types;
2021 std::set<lldb::LanguageType> languages_for_expressions;
2022
2023 Language::GetLanguagesSupportingTypeSystems(languages_for_types, languages_for_expressions);
2024
2025 if (languages_for_expressions.count(eLanguageTypeC))
2026 {
2027 language = eLanguageTypeC; // LLDB's default. Override by setting the target language.
2028 }
2029 else
2030 {
2031 if (languages_for_expressions.empty())
2032 {
2033 return nullptr;
2034 }
2035 else
2036 {
2037 language = *languages_for_expressions.begin();
2038 }
2039 }
Sean Callananb92bd752015-10-01 16:28:02 +00002040 }
Sean Callananb92bd752015-10-01 16:28:02 +00002041
Greg Clayton5beec212015-10-08 21:04:34 +00002042 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this, create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00002043}
2044
2045PersistentExpressionState *
2046Target::GetPersistentExpressionStateForLanguage (lldb::LanguageType language)
2047{
Sean Callanana994b0b2015-10-02 18:40:30 +00002048 TypeSystem *type_system = GetScratchTypeSystemForLanguage(nullptr, language, true);
Sean Callananb92bd752015-10-01 16:28:02 +00002049
2050 if (type_system)
2051 {
2052 return type_system->GetPersistentExpressionState();
2053 }
Jim Ingham151c0322015-09-15 21:13:50 +00002054 else
Sean Callananb92bd752015-10-01 16:28:02 +00002055 {
2056 return nullptr;
2057 }
Jim Ingham151c0322015-09-15 21:13:50 +00002058}
2059
2060UserExpression *
2061Target::GetUserExpressionForLanguage(const char *expr,
Sean Callanana994b0b2015-10-02 18:40:30 +00002062 const char *expr_prefix,
2063 lldb::LanguageType language,
2064 Expression::ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +00002065 const EvaluateExpressionOptions &options,
Sean Callanana994b0b2015-10-02 18:40:30 +00002066 Error &error)
Jim Ingham151c0322015-09-15 21:13:50 +00002067{
Sean Callanana994b0b2015-10-02 18:40:30 +00002068 Error type_system_error;
2069
2070 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002071 UserExpression *user_expr = nullptr;
2072
2073 if (!type_system)
2074 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002075 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 +00002076 return nullptr;
2077 }
2078
Jim Ingham19a63fc2015-11-03 02:11:24 +00002079 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00002080 if (!user_expr)
2081 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2082
2083 return user_expr;
2084}
2085
2086FunctionCaller *
2087Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
2088 const CompilerType &return_type,
2089 const Address& function_address,
2090 const ValueList &arg_value_list,
2091 const char *name,
2092 Error &error)
2093{
Sean Callanana994b0b2015-10-02 18:40:30 +00002094 Error type_system_error;
2095 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002096 FunctionCaller *persistent_fn = nullptr;
2097
2098 if (!type_system)
2099 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002100 error.SetErrorStringWithFormat("Could not find type system for language %s: %s", Language::GetNameForLanguageType(language), type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00002101 return persistent_fn;
2102 }
2103
2104 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
2105 if (!persistent_fn)
2106 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2107
2108 return persistent_fn;
2109}
2110
2111UtilityFunction *
2112Target::GetUtilityFunctionForLanguage (const char *text,
2113 lldb::LanguageType language,
2114 const char *name,
2115 Error &error)
2116{
Sean Callanana994b0b2015-10-02 18:40:30 +00002117 Error type_system_error;
2118 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002119 UtilityFunction *utility_fn = nullptr;
2120
2121 if (!type_system)
2122 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002123 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 +00002124 return utility_fn;
2125 }
2126
2127 utility_fn = type_system->GetUtilityFunction (text, name);
2128 if (!utility_fn)
2129 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2130
2131 return utility_fn;
2132}
2133
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002134ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00002135Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002136{
Greg Clayton5beec212015-10-08 21:04:34 +00002137 if (m_valid)
Sean Callanan4bf80d52011-11-15 22:27:19 +00002138 {
Greg Clayton5beec212015-10-08 21:04:34 +00002139 if (TypeSystem* type_system = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC, create_on_demand))
2140 return llvm::dyn_cast<ClangASTContext>(type_system);
Sean Callanan4bf80d52011-11-15 22:27:19 +00002141 }
Greg Clayton5beec212015-10-08 21:04:34 +00002142 return nullptr;
Sean Callananb92bd752015-10-01 16:28:02 +00002143}
2144
Sean Callanana3444ff2015-11-10 22:54:42 +00002145ClangASTImporterSP
Sean Callanan686b2312011-11-16 18:20:47 +00002146Target::GetClangASTImporter()
2147{
Greg Clayton5beec212015-10-08 21:04:34 +00002148 if (m_valid)
Sean Callanan686b2312011-11-16 18:20:47 +00002149 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002150 if (!m_ast_importer_sp)
Greg Clayton5beec212015-10-08 21:04:34 +00002151 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002152 m_ast_importer_sp.reset(new ClangASTImporter());
Greg Clayton5beec212015-10-08 21:04:34 +00002153 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002154 return m_ast_importer_sp;
Sean Callanan686b2312011-11-16 18:20:47 +00002155 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002156 return ClangASTImporterSP();
Sean Callanan686b2312011-11-16 18:20:47 +00002157}
2158
Greg Clayton99d0faf2010-11-18 23:32:35 +00002159void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002160Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002161{
Greg Clayton6920b522012-08-22 18:39:03 +00002162 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002163}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002164
Greg Clayton99d0faf2010-11-18 23:32:35 +00002165void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002166Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002167{
Greg Clayton6920b522012-08-22 18:39:03 +00002168 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002169}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002170
Greg Claytonc859e2d2012-02-13 23:10:39 +00002171FileSpecList
2172Target::GetDefaultExecutableSearchPaths ()
2173{
Greg Clayton67cc0632012-08-22 17:17:09 +00002174 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2175 if (properties_sp)
2176 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002177 return FileSpecList();
2178}
2179
Michael Sartaina7499c92013-07-01 19:45:50 +00002180FileSpecList
2181Target::GetDefaultDebugFileSearchPaths ()
2182{
2183 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2184 if (properties_sp)
2185 return properties_sp->GetDebugFileSearchPaths();
2186 return FileSpecList();
2187}
2188
Sean Callanan85054342015-04-03 15:39:47 +00002189FileSpecList
2190Target::GetDefaultClangModuleSearchPaths ()
2191{
2192 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2193 if (properties_sp)
2194 return properties_sp->GetClangModuleSearchPaths();
2195 return FileSpecList();
2196}
2197
Caroline Ticedaccaa92010-09-20 20:44:43 +00002198ArchSpec
2199Target::GetDefaultArchitecture ()
2200{
Greg Clayton67cc0632012-08-22 17:17:09 +00002201 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2202 if (properties_sp)
2203 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002204 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002205}
2206
2207void
Greg Clayton67cc0632012-08-22 17:17:09 +00002208Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002209{
Greg Clayton67cc0632012-08-22 17:17:09 +00002210 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2211 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002212 {
2213 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 +00002214 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002215 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002216}
2217
Greg Clayton0603aa92010-10-04 01:05:56 +00002218Target *
2219Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2220{
2221 // The target can either exist in the "process" of ExecutionContext, or in
2222 // the "target_sp" member of SymbolContext. This accessor helper function
2223 // will get the target from one of these locations.
2224
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002225 Target *target = nullptr;
2226 if (sc_ptr != nullptr)
Greg Clayton0603aa92010-10-04 01:05:56 +00002227 target = sc_ptr->target_sp.get();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002228 if (target == nullptr && exe_ctx_ptr)
Greg Claytonc14ee322011-09-22 04:58:26 +00002229 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002230 return target;
2231}
2232
Jim Ingham1624a2d2014-05-05 02:26:40 +00002233ExpressionResults
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002234Target::EvaluateExpression(const char *expr_cstr,
Jim Inghama309efe2015-10-28 22:23:17 +00002235 ExecutionContextScope *exe_scope,
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002236 lldb::ValueObjectSP &result_valobj_sp,
2237 const EvaluateExpressionOptions& options)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002238{
Enrico Granata97fca502012-09-18 17:43:16 +00002239 result_valobj_sp.reset();
2240
Jim Ingham8646d3c2014-05-05 02:47:44 +00002241 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002242
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002243 if (expr_cstr == nullptr || expr_cstr[0] == '\0')
Greg Claytond1767f02011-12-08 02:13:16 +00002244 return execution_results;
2245
Jim Ingham6026ca32011-05-12 02:06:14 +00002246 // We shouldn't run stop hooks in expressions.
2247 // Be sure to reset this if you return anywhere within this function.
2248 bool old_suppress_value = m_suppress_stop_hooks;
2249 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002250
2251 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002252
Jim Inghama309efe2015-10-28 22:23:17 +00002253 if (exe_scope)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002254 {
Jim Inghama309efe2015-10-28 22:23:17 +00002255 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002256 }
2257 else if (m_process_sp)
2258 {
2259 m_process_sp->CalculateExecutionContext(exe_ctx);
2260 }
2261 else
2262 {
2263 CalculateExecutionContext(exe_ctx);
2264 }
2265
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002266 // Make sure we aren't just trying to see the value of a persistent
2267 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002268 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002269 // Only check for persistent variables the expression starts with a '$'
2270 if (expr_cstr[0] == '$')
Sean Callanana994b0b2015-10-02 18:40:30 +00002271 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002272
2273 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002274 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002275 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002276 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002277 }
2278 else
2279 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002280 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002281 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002282 execution_results = UserExpression::Evaluate (exe_ctx,
2283 options,
2284 expr_cstr,
2285 prefix,
2286 result_valobj_sp,
2287 error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002288 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002289
2290 m_suppress_stop_hooks = old_suppress_value;
2291
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002292 return execution_results;
2293}
2294
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002295lldb::ExpressionVariableSP
2296Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002297{
Greg Clayton5beec212015-10-08 21:04:34 +00002298 lldb::ExpressionVariableSP variable_sp;
2299 m_scratch_type_system_map.ForEach([this, name, &variable_sp](TypeSystem *type_system) -> bool
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002300 {
Greg Clayton5beec212015-10-08 21:04:34 +00002301 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002302 {
Greg Clayton5beec212015-10-08 21:04:34 +00002303 variable_sp = persistent_state->GetVariable(name);
2304
2305 if (variable_sp)
2306 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002307 }
Greg Clayton5beec212015-10-08 21:04:34 +00002308 return true; // Keep iterating the ForEach
2309 });
2310 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002311}
2312
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002313lldb::addr_t
Sean Callananb92bd752015-10-01 16:28:02 +00002314Target::GetPersistentSymbol(const ConstString &name)
2315{
Greg Clayton5beec212015-10-08 21:04:34 +00002316 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Sean Callananb92bd752015-10-01 16:28:02 +00002317
Greg Clayton5beec212015-10-08 21:04:34 +00002318 m_scratch_type_system_map.ForEach([this, name, &address](TypeSystem *type_system) -> bool
Sean Callananb92bd752015-10-01 16:28:02 +00002319 {
Greg Clayton5beec212015-10-08 21:04:34 +00002320 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callananb92bd752015-10-01 16:28:02 +00002321 {
Greg Clayton5beec212015-10-08 21:04:34 +00002322 address = persistent_state->LookupSymbol(name);
2323 if (address != LLDB_INVALID_ADDRESS)
2324 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002325 }
Greg Clayton5beec212015-10-08 21:04:34 +00002326 return true; // Keep iterating the ForEach
2327 });
2328 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002329}
2330
2331lldb::addr_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002332Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2333{
2334 addr_t code_addr = load_addr;
2335 switch (m_arch.GetMachine())
2336 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002337 case llvm::Triple::mips:
2338 case llvm::Triple::mipsel:
2339 case llvm::Triple::mips64:
2340 case llvm::Triple::mips64el:
2341 switch (addr_class)
2342 {
2343 case eAddressClassData:
2344 case eAddressClassDebug:
2345 return LLDB_INVALID_ADDRESS;
2346
2347 case eAddressClassUnknown:
2348 case eAddressClassInvalid:
2349 case eAddressClassCode:
2350 case eAddressClassCodeAlternateISA:
2351 case eAddressClassRuntime:
2352 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2353 code_addr |= 1ull;
2354 break;
2355 }
2356 break;
2357
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002358 case llvm::Triple::arm:
2359 case llvm::Triple::thumb:
2360 switch (addr_class)
2361 {
2362 case eAddressClassData:
2363 case eAddressClassDebug:
2364 return LLDB_INVALID_ADDRESS;
2365
2366 case eAddressClassUnknown:
2367 case eAddressClassInvalid:
2368 case eAddressClassCode:
2369 case eAddressClassCodeAlternateISA:
2370 case eAddressClassRuntime:
2371 // Check if bit zero it no set?
2372 if ((code_addr & 1ull) == 0)
2373 {
2374 // Bit zero isn't set, check if the address is a multiple of 2?
2375 if (code_addr & 2ull)
2376 {
2377 // The address is a multiple of 2 so it must be thumb, set bit zero
2378 code_addr |= 1ull;
2379 }
2380 else if (addr_class == eAddressClassCodeAlternateISA)
2381 {
2382 // We checked the address and the address claims to be the alternate ISA
2383 // which means thumb, so set bit zero.
2384 code_addr |= 1ull;
2385 }
2386 }
2387 break;
2388 }
2389 break;
2390
2391 default:
2392 break;
2393 }
2394 return code_addr;
2395}
2396
2397lldb::addr_t
2398Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2399{
2400 addr_t opcode_addr = load_addr;
2401 switch (m_arch.GetMachine())
2402 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002403 case llvm::Triple::mips:
2404 case llvm::Triple::mipsel:
2405 case llvm::Triple::mips64:
2406 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002407 case llvm::Triple::arm:
2408 case llvm::Triple::thumb:
2409 switch (addr_class)
2410 {
2411 case eAddressClassData:
2412 case eAddressClassDebug:
2413 return LLDB_INVALID_ADDRESS;
2414
2415 case eAddressClassInvalid:
2416 case eAddressClassUnknown:
2417 case eAddressClassCode:
2418 case eAddressClassCodeAlternateISA:
2419 case eAddressClassRuntime:
2420 opcode_addr &= ~(1ull);
2421 break;
2422 }
2423 break;
2424
2425 default:
2426 break;
2427 }
2428 return opcode_addr;
2429}
2430
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002431lldb::addr_t
2432Target::GetBreakableLoadAddress (lldb::addr_t addr)
2433{
2434 addr_t breakable_addr = addr;
2435 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2436
2437 switch (m_arch.GetMachine())
2438 {
2439 default:
2440 break;
2441 case llvm::Triple::mips:
2442 case llvm::Triple::mipsel:
2443 case llvm::Triple::mips64:
2444 case llvm::Triple::mips64el:
2445 {
2446 addr_t function_start = 0;
2447 addr_t current_offset = 0;
2448 uint32_t loop_count = 0;
2449 Address resolved_addr;
2450 uint32_t arch_flags = m_arch.GetFlags ();
2451 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2452 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2453 SectionLoadList &section_load_list = GetSectionLoadList();
2454
2455 if (section_load_list.IsEmpty())
2456 // No sections are loaded, so we must assume we are not running yet
2457 // and need to operate only on file address.
2458 m_images.ResolveFileAddress (addr, resolved_addr);
2459 else
2460 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2461
2462 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2463 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2464 if (temp_addr_module_sp)
2465 {
2466 SymbolContext sc;
2467 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002468 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002469 Address sym_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002470 if (sc.function)
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002471 sym_addr = sc.function->GetAddressRange().GetBaseAddress();
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002472 else if (sc.symbol)
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002473 sym_addr = sc.symbol->GetAddress();
2474
2475 function_start = sym_addr.GetLoadAddress(this);
2476 if (function_start == LLDB_INVALID_ADDRESS)
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002477 function_start = sym_addr.GetFileAddress();
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002478
2479 if (function_start)
2480 current_offset = addr - function_start;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002481 }
2482
2483 // If breakpoint address is start of function then we dont have to do anything.
2484 if (current_offset == 0)
2485 return breakable_addr;
2486 else
2487 loop_count = current_offset / 2;
2488
2489 if (loop_count > 3)
2490 {
2491 // Scan previous 6 bytes
2492 if (IsMips16 | IsMicromips)
2493 loop_count = 3;
2494 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2495 else
2496 loop_count = 2;
2497 }
2498
2499 // Create Disassembler Instance
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002500 lldb::DisassemblerSP disasm_sp(Disassembler::FindPlugin(m_arch, nullptr, nullptr));
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002501
2502 ExecutionContext exe_ctx;
2503 CalculateExecutionContext(exe_ctx);
2504 InstructionList instruction_list;
2505 InstructionSP prev_insn;
2506 bool prefer_file_cache = true; // Read from file
2507 uint32_t inst_to_choose = 0;
2508
2509 for (uint32_t i = 1; i <= loop_count; i++)
2510 {
2511 // Adjust the address to read from.
2512 resolved_addr.Slide (-2);
2513 AddressRange range(resolved_addr, i*2);
2514 uint32_t insn_size = 0;
2515
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002516 disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002517
2518 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2519 if (num_insns)
2520 {
2521 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2522 insn_size = prev_insn->GetOpcode().GetByteSize();
2523 if (i == 1 && insn_size == 2)
2524 {
2525 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2526 instruction_list.Append(prev_insn);
2527 inst_to_choose = 1;
2528 }
2529 else if (i == 2)
2530 {
2531 // Here we may get one 4-byte instruction or two 2-byte instructions.
2532 if (num_insns == 2)
2533 {
2534 // Looks like there are two 2-byte instructions above our breakpoint target address.
2535 // 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.
2536 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2537 // and whatever i=1 iteration has found out is true.
2538 inst_to_choose = 1;
2539 break;
2540 }
2541 else if (insn_size == 4)
2542 {
2543 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2544 // Lets try scanning upper 2 bytes to verify this.
2545 instruction_list.Append(prev_insn);
2546 inst_to_choose = 2;
2547 }
2548 }
2549 else if (i == 3)
2550 {
2551 if (insn_size == 4)
2552 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2553 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2554 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2555 inst_to_choose = 0;
2556 else
2557 // This is straight-forward
2558 inst_to_choose = 2;
2559 break;
2560 }
2561 }
2562 else
2563 {
2564 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2565 if (i > 1)
2566 {
2567 inst_to_choose = i - 1;
2568 break;
2569 }
2570 }
2571 }
2572
2573 // Check if we are able to find any valid instruction.
2574 if (inst_to_choose)
2575 {
2576 if (inst_to_choose > instruction_list.GetSize())
2577 inst_to_choose--;
2578 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2579
2580 if (prev_insn->HasDelaySlot())
2581 {
2582 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2583 // Adjust the breakable address
2584 breakable_addr = addr - shift_size;
2585 if (log)
2586 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);
2587 }
2588 }
2589 break;
2590 }
2591 }
2592 return breakable_addr;
2593}
2594
Greg Clayton9585fbf2013-03-19 00:20:55 +00002595SourceManager &
2596Target::GetSourceManager ()
2597{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002598 if (!m_source_manager_ap)
Greg Clayton9585fbf2013-03-19 00:20:55 +00002599 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2600 return *m_source_manager_ap;
2601}
2602
Sean Callanan9998acd2014-12-05 01:21:59 +00002603ClangModulesDeclVendor *
2604Target::GetClangModulesDeclVendor ()
2605{
2606 static Mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2607
2608 {
2609 Mutex::Locker clang_modules_decl_vendor_locker(s_clang_modules_decl_vendor_mutex);
2610
2611 if (!m_clang_modules_decl_vendor_ap)
2612 {
2613 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2614 }
2615 }
2616
2617 return m_clang_modules_decl_vendor_ap.get();
2618}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002619
Greg Clayton44d93782014-01-27 23:43:24 +00002620Target::StopHookSP
2621Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002622{
2623 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002624 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2625 m_stop_hooks[new_uid] = stop_hook_sp;
2626 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002627}
2628
2629bool
2630Target::RemoveStopHookByID (lldb::user_id_t user_id)
2631{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002632 size_t num_removed = m_stop_hooks.erase(user_id);
2633 return (num_removed != 0);
Jim Ingham9575d842011-03-11 03:53:59 +00002634}
2635
2636void
2637Target::RemoveAllStopHooks ()
2638{
2639 m_stop_hooks.clear();
2640}
2641
2642Target::StopHookSP
2643Target::GetStopHookByID (lldb::user_id_t user_id)
2644{
2645 StopHookSP found_hook;
2646
2647 StopHookCollection::iterator specified_hook_iter;
2648 specified_hook_iter = m_stop_hooks.find (user_id);
2649 if (specified_hook_iter != m_stop_hooks.end())
2650 found_hook = (*specified_hook_iter).second;
2651 return found_hook;
2652}
2653
2654bool
2655Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2656{
2657 StopHookCollection::iterator specified_hook_iter;
2658 specified_hook_iter = m_stop_hooks.find (user_id);
2659 if (specified_hook_iter == m_stop_hooks.end())
2660 return false;
2661
2662 (*specified_hook_iter).second->SetIsActive (active_state);
2663 return true;
2664}
2665
2666void
2667Target::SetAllStopHooksActiveState (bool active_state)
2668{
2669 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2670 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2671 {
2672 (*pos).second->SetIsActive (active_state);
2673 }
2674}
2675
2676void
2677Target::RunStopHooks ()
2678{
Jim Ingham6026ca32011-05-12 02:06:14 +00002679 if (m_suppress_stop_hooks)
2680 return;
2681
Jim Ingham9575d842011-03-11 03:53:59 +00002682 if (!m_process_sp)
2683 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002684
2685 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2686 // since in that case we do not want to run the stop-hooks
2687 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2688 return;
2689
Jim Ingham9575d842011-03-11 03:53:59 +00002690 if (m_stop_hooks.empty())
2691 return;
2692
2693 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2694
2695 // If there aren't any active stop hooks, don't bother either:
2696 bool any_active_hooks = false;
2697 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2698 {
2699 if ((*pos).second->IsActive())
2700 {
2701 any_active_hooks = true;
2702 break;
2703 }
2704 }
2705 if (!any_active_hooks)
2706 return;
2707
2708 CommandReturnObject result;
2709
2710 std::vector<ExecutionContext> exc_ctx_with_reasons;
2711 std::vector<SymbolContext> sym_ctx_with_reasons;
2712
2713 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2714 size_t num_threads = cur_threadlist.GetSize();
2715 for (size_t i = 0; i < num_threads; i++)
2716 {
2717 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2718 if (cur_thread_sp->ThreadStoppedForAReason())
2719 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002720 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002721 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2722 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2723 }
2724 }
2725
2726 // If no threads stopped for a reason, don't run the stop-hooks.
2727 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2728 if (num_exe_ctx == 0)
2729 return;
2730
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002731 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2732 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002733
2734 bool keep_going = true;
2735 bool hooks_ran = false;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002736 bool print_hook_header = (m_stop_hooks.size() != 1);
2737 bool print_thread_header = (num_exe_ctx != 1);
Jim Ingham381e25b2011-03-22 01:47:27 +00002738
Jim Ingham9575d842011-03-11 03:53:59 +00002739 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2740 {
2741 // result.Clear();
2742 StopHookSP cur_hook_sp = (*pos).second;
2743 if (!cur_hook_sp->IsActive())
2744 continue;
2745
2746 bool any_thread_matched = false;
2747 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2748 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002749 if ((cur_hook_sp->GetSpecifier() == nullptr
Jim Ingham9575d842011-03-11 03:53:59 +00002750 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002751 && (cur_hook_sp->GetThreadSpecifier() == nullptr
Jim Ingham3d902922012-03-07 22:03:04 +00002752 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002753 {
2754 if (!hooks_ran)
2755 {
Jim Ingham9575d842011-03-11 03:53:59 +00002756 hooks_ran = true;
2757 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002758 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002759 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002760 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2761 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002762 nullptr);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002763 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002764 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002765 else
Daniel Malead01b2952012-11-29 21:49:15 +00002766 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002767 any_thread_matched = true;
2768 }
2769
Jim Ingham381e25b2011-03-22 01:47:27 +00002770 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002771 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002772
2773 CommandInterpreterRunOptions options;
2774 options.SetStopOnContinue (true);
2775 options.SetStopOnError (true);
2776 options.SetEchoCommands (false);
2777 options.SetPrintResults (true);
2778 options.SetAddToHistory (false);
2779
2780 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2781 &exc_ctx_with_reasons[i],
2782 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002783 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002784
2785 // If the command started the target going again, we should bag out of
2786 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002787 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2788 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002789 {
Daniel Malead01b2952012-11-29 21:49:15 +00002790 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002791 keep_going = false;
2792 }
2793 }
2794 }
2795 }
Jason Molenda879cf772011-09-23 00:42:55 +00002796
Caroline Tice969ed3d2011-05-02 20:41:46 +00002797 result.GetImmediateOutputStream()->Flush();
2798 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002799}
2800
Greg Claytonfbb76342013-11-20 21:07:01 +00002801const TargetPropertiesSP &
2802Target::GetGlobalProperties()
2803{
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002804 // NOTE: intentional leak so we don't crash if global destructor chain gets
2805 // called as other threads still use the result of this function
2806 static TargetPropertiesSP *g_settings_sp_ptr = nullptr;
Greg Clayton04df8ee2016-02-26 19:38:18 +00002807 static std::once_flag g_once_flag;
2808 std::call_once(g_once_flag, []() {
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002809 g_settings_sp_ptr = new TargetPropertiesSP(new TargetProperties(nullptr));
Greg Clayton04df8ee2016-02-26 19:38:18 +00002810 });
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002811 return *g_settings_sp_ptr;
Greg Claytonfbb76342013-11-20 21:07:01 +00002812}
2813
2814Error
2815Target::Install (ProcessLaunchInfo *launch_info)
2816{
2817 Error error;
2818 PlatformSP platform_sp (GetPlatform());
2819 if (platform_sp)
2820 {
2821 if (platform_sp->IsRemote())
2822 {
2823 if (platform_sp->IsConnected())
2824 {
2825 // Install all files that have an install path, and always install the
2826 // main executable when connected to a remote platform
2827 const ModuleList& modules = GetImages();
2828 const size_t num_images = modules.GetSize();
2829 for (size_t idx = 0; idx < num_images; ++idx)
2830 {
2831 const bool is_main_executable = idx == 0;
2832 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2833 if (module_sp)
2834 {
2835 FileSpec local_file (module_sp->GetFileSpec());
2836 if (local_file)
2837 {
2838 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2839 if (!remote_file)
2840 {
2841 if (is_main_executable) // TODO: add setting for always installing main executable???
2842 {
2843 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002844 remote_file = platform_sp->GetRemoteWorkingDirectory();
2845 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002846 }
2847 }
2848 if (remote_file)
2849 {
2850 error = platform_sp->Install(local_file, remote_file);
2851 if (error.Success())
2852 {
2853 module_sp->SetPlatformFileSpec(remote_file);
2854 if (is_main_executable)
2855 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002856 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002857 if (launch_info)
2858 launch_info->SetExecutableFile(remote_file, false);
2859 }
2860 }
2861 else
2862 break;
2863 }
2864 }
2865 }
2866 }
2867 }
2868 }
2869 }
2870 return error;
2871}
Greg Clayton7b242382011-07-08 00:48:09 +00002872
Greg Claytond5944cd2013-12-06 01:12:00 +00002873bool
2874Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2875{
2876 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2877}
2878
2879bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002880Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2881{
2882 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2883}
2884
2885bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002886Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2887{
2888 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2889 if (old_section_load_addr != new_section_load_addr)
2890 {
2891 uint32_t stop_id = 0;
2892 ProcessSP process_sp(GetProcessSP());
2893 if (process_sp)
2894 stop_id = process_sp->GetStopID();
2895 else
2896 stop_id = m_section_load_history.GetLastStopID();
2897 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2898 return true; // Return true if the section load address was changed...
2899 }
2900 return false; // Return false to indicate nothing changed
Greg Claytond5944cd2013-12-06 01:12:00 +00002901}
2902
Greg Clayton8012cad2014-11-17 19:39:20 +00002903size_t
2904Target::UnloadModuleSections (const ModuleList &module_list)
2905{
2906 size_t section_unload_count = 0;
2907 size_t num_modules = module_list.GetSize();
2908 for (size_t i=0; i<num_modules; ++i)
2909 {
2910 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2911 }
2912 return section_unload_count;
2913}
2914
2915size_t
2916Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2917{
2918 uint32_t stop_id = 0;
2919 ProcessSP process_sp(GetProcessSP());
2920 if (process_sp)
2921 stop_id = process_sp->GetStopID();
2922 else
2923 stop_id = m_section_load_history.GetLastStopID();
2924 SectionList *sections = module_sp->GetSectionList();
2925 size_t section_unload_count = 0;
2926 if (sections)
2927 {
2928 const uint32_t num_sections = sections->GetNumSections(0);
2929 for (uint32_t i = 0; i < num_sections; ++i)
2930 {
2931 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2932 }
2933 }
2934 return section_unload_count;
2935}
2936
Greg Claytond5944cd2013-12-06 01:12:00 +00002937bool
2938Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2939{
2940 uint32_t stop_id = 0;
2941 ProcessSP process_sp(GetProcessSP());
2942 if (process_sp)
2943 stop_id = process_sp->GetStopID();
2944 else
2945 stop_id = m_section_load_history.GetLastStopID();
2946 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2947}
2948
2949bool
2950Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2951{
2952 uint32_t stop_id = 0;
2953 ProcessSP process_sp(GetProcessSP());
2954 if (process_sp)
2955 stop_id = process_sp->GetStopID();
2956 else
2957 stop_id = m_section_load_history.GetLastStopID();
2958 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2959}
2960
2961void
2962Target::ClearAllLoadedSections ()
2963{
2964 m_section_load_history.Clear();
2965}
2966
Greg Claytonb09c5382013-12-13 17:20:18 +00002967Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002968Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002969{
2970 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002971 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2972
2973 if (log)
2974 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2975
Greg Claytonb09c5382013-12-13 17:20:18 +00002976 StateType state = eStateInvalid;
2977
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002978 // Scope to temporarily get the process state in case someone has manually
2979 // remotely connected already to a process and we can skip the platform
2980 // launching.
2981 {
2982 ProcessSP process_sp (GetProcessSP());
2983
2984 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002985 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002986 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002987 if (log)
2988 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2989 }
2990 else
2991 {
2992 if (log)
2993 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
2994 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002995 }
2996
Greg Claytonb09c5382013-12-13 17:20:18 +00002997 launch_info.GetFlags().Set (eLaunchFlagDebug);
2998
2999 // Get the value of synchronous execution here. If you wait till after you have started to
3000 // run, then you could have hit a breakpoint, whose command might switch the value, and
3001 // then you'll pick up that incorrect value.
3002 Debugger &debugger = GetDebugger();
3003 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
3004
3005 PlatformSP platform_sp (GetPlatform());
3006
3007 // Finalize the file actions, and if none were given, default to opening
3008 // up a pseudo terminal
3009 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00003010 if (log)
3011 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
3012 __FUNCTION__,
3013 platform_sp ? "true" : "false",
3014 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
3015 default_to_use_pty ? "true" : "false");
3016
Greg Claytonb09c5382013-12-13 17:20:18 +00003017 launch_info.FinalizeFileActions (this, default_to_use_pty);
3018
3019 if (state == eStateConnected)
3020 {
3021 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
3022 {
3023 error.SetErrorString("can't launch in tty when launching through a remote connection");
3024 return error;
3025 }
3026 }
3027
3028 if (!launch_info.GetArchitecture().IsValid())
3029 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00003030
3031 // 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 +00003032 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
3033 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003034 if (log)
3035 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
3036
Greg Clayton5df78fa2015-05-23 03:54:53 +00003037 // Get a weak pointer to the previous process if we have one
3038 ProcessWP process_wp;
3039 if (m_process_sp)
3040 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00003041 m_process_sp = GetPlatform()->DebugProcess (launch_info,
3042 debugger,
3043 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00003044 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00003045
3046 // Cleanup the old process since someone might still have a strong
3047 // reference to this process and we would like to allow it to cleanup
3048 // as much as it can without the object being destroyed. We try to
3049 // lock the shared pointer and if that works, then someone else still
3050 // has a strong reference to the process.
3051
3052 ProcessSP old_process_sp(process_wp.lock());
3053 if (old_process_sp)
3054 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00003055 }
3056 else
3057 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003058 if (log)
3059 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
3060
Greg Claytonb09c5382013-12-13 17:20:18 +00003061 if (state == eStateConnected)
3062 {
3063 assert(m_process_sp);
3064 }
3065 else
3066 {
Todd Fiala015d8182014-07-22 23:41:36 +00003067 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00003068 const char *plugin_name = launch_info.GetProcessPluginName();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003069 CreateProcess(launch_info.GetListenerForProcess(debugger), plugin_name, nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00003070 }
Todd Fiala015d8182014-07-22 23:41:36 +00003071
3072 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00003073 if (m_process_sp)
3074 error = m_process_sp->Launch (launch_info);
3075 }
3076
3077 if (!m_process_sp)
3078 {
3079 if (error.Success())
3080 error.SetErrorString("failed to launch or debug process");
3081 return error;
3082 }
3083
3084 if (error.Success())
3085 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003086 if (synchronous_execution || !launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003087 {
Greg Clayton44d93782014-01-27 23:43:24 +00003088 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00003089 if (!hijack_listener_sp)
3090 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003091 hijack_listener_sp = Listener::MakeListener("lldb.Target.Launch.hijack");
Zachary Turnere6d213a2015-03-26 20:41:14 +00003092 launch_info.SetHijackListener(hijack_listener_sp);
Jim Ingham583bbb12016-03-07 21:50:25 +00003093 m_process_sp->HijackProcessEvents(hijack_listener_sp);
Zachary Turnere6d213a2015-03-26 20:41:14 +00003094 }
Todd Fialaac33cc92014-10-09 01:02:08 +00003095
Jim Ingham583bbb12016-03-07 21:50:25 +00003096 StateType state = m_process_sp->WaitForProcessToStop(nullptr, nullptr, false, hijack_listener_sp, nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00003097
3098 if (state == eStateStopped)
3099 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003100 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003101 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003102 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00003103 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003104 error = m_process_sp->PrivateResume();
3105 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00003106 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003107 state = m_process_sp->WaitForProcessToStop(nullptr, nullptr, true, hijack_listener_sp, stream);
Ilia K064e69f2015-03-23 21:16:25 +00003108 const bool must_be_alive = false; // eStateExited is ok, so this must be false
3109 if (!StateIsStoppedState(state, must_be_alive))
3110 {
3111 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
3112 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003113 }
3114 }
Ilia K064e69f2015-03-23 21:16:25 +00003115 else
3116 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003117 m_process_sp->RestoreProcessEvents();
3118 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00003119 }
3120 if (!error.Success())
3121 {
Ilia K064e69f2015-03-23 21:16:25 +00003122 Error error2;
3123 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
3124 error = error2;
3125 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003126 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003127 }
Greg Clayton40286e02014-04-30 20:29:09 +00003128 else if (state == eStateExited)
3129 {
Zachary Turner10687b02014-10-20 17:46:43 +00003130 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00003131 const int exit_status = m_process_sp->GetExitStatus();
3132 const char *exit_desc = m_process_sp->GetExitDescription();
3133#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'."
3134 if (exit_desc && exit_desc[0])
3135 {
3136 if (with_shell)
3137 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
3138 else
3139 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
3140 }
3141 else
3142 {
3143 if (with_shell)
3144 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
3145 else
3146 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
3147 }
3148 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003149 else
3150 {
3151 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
3152 }
3153 }
Greg Clayton44d93782014-01-27 23:43:24 +00003154 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00003155 }
3156 else
3157 {
Greg Clayton44d93782014-01-27 23:43:24 +00003158 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003159 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003160 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003161 }
3162 return error;
3163}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003164
3165Error
3166Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3167{
3168 auto state = eStateInvalid;
3169 auto process_sp = GetProcessSP ();
3170 if (process_sp)
3171 {
3172 state = process_sp->GetState ();
3173 if (process_sp->IsAlive () && state != eStateConnected)
3174 {
3175 if (state == eStateAttaching)
3176 return Error ("process attach is in progress");
3177 return Error ("a process is already being debugged");
3178 }
3179 }
3180
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003181 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3182
3183 // If no process info was specified, then use the target executable
3184 // name as the process to attach to by default
3185 if (!attach_info.ProcessInfoSpecified ())
3186 {
3187 if (old_exec_module_sp)
3188 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3189
3190 if (!attach_info.ProcessInfoSpecified ())
3191 {
3192 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3193 }
3194 }
3195
3196 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
Greg Claytonb3788ea2015-10-05 22:58:37 +00003197 ListenerSP hijack_listener_sp;
3198 const bool async = attach_info.GetAsync();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003199 if (!async)
Greg Claytonb3788ea2015-10-05 22:58:37 +00003200 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003201 hijack_listener_sp = Listener::MakeListener("lldb.Target.Attach.attach.hijack");
Greg Claytonb3788ea2015-10-05 22:58:37 +00003202 attach_info.SetHijackListener (hijack_listener_sp);
3203 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003204
3205 Error error;
3206 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3207 {
3208 SetPlatform (platform_sp);
3209 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3210 }
3211 else
3212 {
3213 if (state != eStateConnected)
3214 {
3215 const char *plugin_name = attach_info.GetProcessPluginName ();
3216 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3217 if (process_sp == nullptr)
3218 {
3219 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3220 return error;
3221 }
3222 }
Greg Claytonb3788ea2015-10-05 22:58:37 +00003223 if (hijack_listener_sp)
Jim Ingham583bbb12016-03-07 21:50:25 +00003224 process_sp->HijackProcessEvents (hijack_listener_sp);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003225 error = process_sp->Attach (attach_info);
3226 }
3227
Greg Claytone75e5d82015-10-20 00:14:20 +00003228 if (error.Success () && process_sp)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003229 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003230 if (async)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003231 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003232 process_sp->RestoreProcessEvents ();
3233 }
3234 else
3235 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003236 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener(), stream);
Greg Claytone75e5d82015-10-20 00:14:20 +00003237 process_sp->RestoreProcessEvents ();
3238
3239 if (state != eStateStopped)
3240 {
3241 const char *exit_desc = process_sp->GetExitDescription ();
3242 if (exit_desc)
3243 error.SetErrorStringWithFormat ("%s", exit_desc);
3244 else
3245 error.SetErrorString ("process did not stop (no such process or permission problem?)");
3246 process_sp->Destroy (false);
3247 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003248 }
3249 }
3250 return error;
3251}
3252
Jim Ingham9575d842011-03-11 03:53:59 +00003253//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003254// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003255//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003256Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3257 UserID (uid),
3258 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003259 m_commands (),
3260 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003261 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003262 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003263{
3264}
3265
3266Target::StopHook::StopHook (const StopHook &rhs) :
3267 UserID (rhs.GetID()),
3268 m_target_sp (rhs.m_target_sp),
3269 m_commands (rhs.m_commands),
3270 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003271 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003272 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003273{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003274 if (rhs.m_thread_spec_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00003275 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3276}
3277
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003278Target::StopHook::~StopHook() = default;
Jim Ingham9575d842011-03-11 03:53:59 +00003279
3280void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003281Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3282{
3283 m_specifier_sp.reset(specifier);
3284}
3285
3286void
Jim Ingham9575d842011-03-11 03:53:59 +00003287Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3288{
3289 m_thread_spec_ap.reset (specifier);
3290}
Jim Ingham9575d842011-03-11 03:53:59 +00003291
3292void
3293Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3294{
3295 int indent_level = s->GetIndentLevel();
3296
3297 s->SetIndentLevel(indent_level + 2);
3298
Daniel Malead01b2952012-11-29 21:49:15 +00003299 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003300 if (m_active)
3301 s->Indent ("State: enabled\n");
3302 else
3303 s->Indent ("State: disabled\n");
3304
3305 if (m_specifier_sp)
3306 {
3307 s->Indent();
3308 s->PutCString ("Specifier:\n");
3309 s->SetIndentLevel (indent_level + 4);
3310 m_specifier_sp->GetDescription (s, level);
3311 s->SetIndentLevel (indent_level + 2);
3312 }
3313
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003314 if (m_thread_spec_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00003315 {
3316 StreamString tmp;
3317 s->Indent("Thread:\n");
3318 m_thread_spec_ap->GetDescription (&tmp, level);
3319 s->SetIndentLevel (indent_level + 4);
3320 s->Indent (tmp.GetData());
3321 s->PutCString ("\n");
3322 s->SetIndentLevel (indent_level + 2);
3323 }
3324
3325 s->Indent ("Commands: \n");
3326 s->SetIndentLevel (indent_level + 4);
3327 uint32_t num_commands = m_commands.GetSize();
3328 for (uint32_t i = 0; i < num_commands; i++)
3329 {
3330 s->Indent(m_commands.GetStringAtIndex(i));
3331 s->PutCString ("\n");
3332 }
3333 s->SetIndentLevel (indent_level);
3334}
3335
Greg Clayton67cc0632012-08-22 17:17:09 +00003336//--------------------------------------------------------------
3337// class TargetProperties
3338//--------------------------------------------------------------
3339
3340OptionEnumValueElement
3341lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003342{
Greg Clayton67cc0632012-08-22 17:17:09 +00003343 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3344 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3345 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003346 { 0, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +00003347};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003348
Greg Clayton1f746072012-08-29 21:13:06 +00003349static OptionEnumValueElement
3350g_inline_breakpoint_enums[] =
3351{
3352 { 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."},
3353 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3354 { 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 +00003355 { 0, nullptr, nullptr }
Greg Clayton1f746072012-08-29 21:13:06 +00003356};
3357
Jim Ingham0f063ba2013-03-02 00:26:47 +00003358typedef enum x86DisassemblyFlavor
3359{
3360 eX86DisFlavorDefault,
3361 eX86DisFlavorIntel,
3362 eX86DisFlavorATT
3363} x86DisassemblyFlavor;
3364
3365static OptionEnumValueElement
3366g_x86_dis_flavor_value_types[] =
3367{
3368 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3369 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3370 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003371 { 0, nullptr, nullptr }
Jim Ingham0f063ba2013-03-02 00:26:47 +00003372};
3373
Enrico Granata397ddd52013-05-21 20:13:34 +00003374static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003375g_hex_immediate_style_values[] =
3376{
3377 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3378 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003379 { 0, nullptr, nullptr }
Daniel Malead79ae052013-08-07 21:54:09 +00003380};
3381
3382static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003383g_load_script_from_sym_file_values[] =
3384{
3385 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3386 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3387 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003388 { 0, nullptr, nullptr }
Enrico Granata397ddd52013-05-21 20:13:34 +00003389};
3390
Greg Claytonfd814c52013-08-13 01:42:25 +00003391static OptionEnumValueElement
Jason Molenda878ae012016-02-19 00:05:17 +00003392g_load_current_working_dir_lldbinit_values[] =
3393{
3394 { eLoadCWDlldbinitTrue, "true", "Load .lldbinit files from current directory"},
3395 { eLoadCWDlldbinitFalse, "false", "Do not load .lldbinit files from current directory"},
3396 { eLoadCWDlldbinitWarn, "warn", "Warn about loading .lldbinit files from current directory"},
3397 { 0, nullptr, nullptr }
3398};
3399
3400static OptionEnumValueElement
Greg Claytonfd814c52013-08-13 01:42:25 +00003401g_memory_module_load_level_values[] =
3402{
Greg Clayton86eac942013-08-13 21:32:34 +00003403 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003404 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3405 { 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 +00003406 { 0, nullptr, nullptr }
Greg Claytonfd814c52013-08-13 01:42:25 +00003407};
3408
Greg Clayton67cc0632012-08-22 17:17:09 +00003409static PropertyDefinition
3410g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003411{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003412 { "default-arch" , OptionValue::eTypeArch , true , 0 , nullptr, nullptr, "Default architecture to choose, when there's a choice." },
3413 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Move breakpoints to nearest code." },
3414 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , nullptr, nullptr, "The language to use when interpreting expressions entered in commands." },
3415 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "Path to a file containing expressions to be prepended to all expressions." },
3416 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eDynamicDontRunTarget , nullptr, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
3417 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Should synthetic values be used by default whenever available." },
3418 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Skip function prologues when setting breakpoints by name." },
3419 { "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 +00003420 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3421 "some part (starting at the root) of the path to the file when it was built, "
3422 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3423 "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 +00003424 { "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." },
3425 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , nullptr, nullptr, "List of directories to be searched when locating debug symbol files." },
3426 { "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 +00003427 { "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 +00003428 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , nullptr, nullptr, "Maximum number of children to expand in any level of depth." },
3429 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , nullptr, nullptr, "Maximum number of characters to show when using %s in summary strings." },
3430 { "max-memory-read-size" , OptionValue::eTypeSInt64 , false, 1024 , nullptr, nullptr, "Maximum number of bytes that 'memory read' will fetch before --force must be specified." },
3431 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Consult the platform module avoid list when setting non-module specific breakpoints." },
3432 { "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." },
3433 { "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." },
3434 { "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." },
3435 { "inherit-env" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Inherit the environment from the process that is running LLDB." },
3436 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for reading its standard input." },
3437 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for writing its standard output." },
3438 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for writing its standard error." },
3439 { "detach-on-error" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "debugserver will detach (rather than killing) a process if it loses connection with lldb." },
3440 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Disable Address Space Layout Randomization (ASLR)" },
3441 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , nullptr, nullptr, "Disable stdin/stdout for process (e.g. for a GUI application)" },
3442 { "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 +00003443 "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 +00003444 "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 +00003445 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003446 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3447 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3448 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003449 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003450 // 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 +00003451 { "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." },
3452 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, nullptr, nullptr, "Show immediates in disassembly as hexadecimal." },
3453 { "hex-immediate-style" , OptionValue::eTypeEnum , false, Disassembler::eHexStyleC, nullptr, g_hex_immediate_style_values, "Which style to use for printing hexadecimal disassembly values." },
3454 { "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." },
3455 { "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 +00003456 { "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 +00003457 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, nullptr, g_memory_module_load_level_values,
Greg Clayton86eac942013-08-13 21:32:34 +00003458 "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. "
3459 "This setting helps users control how much information gets loaded when loading modules from memory."
3460 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3461 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3462 "'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 +00003463 { "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." },
3464 { "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." },
3465 { "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." },
3466 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, nullptr, nullptr, "Disable lock-step debugging, instead control threads independently." },
3467 { nullptr , OptionValue::eTypeInvalid , false, 0 , nullptr, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +00003468};
Enrico Granata560558e2015-02-11 02:35:39 +00003469
Greg Clayton67cc0632012-08-22 17:17:09 +00003470enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003471{
Greg Clayton67cc0632012-08-22 17:17:09 +00003472 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003473 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003474 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003475 ePropertyExprPrefix,
3476 ePropertyPreferDynamic,
3477 ePropertyEnableSynthetic,
3478 ePropertySkipPrologue,
3479 ePropertySourceMap,
3480 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003481 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003482 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003483 ePropertyAutoImportClangModules,
Greg Clayton67cc0632012-08-22 17:17:09 +00003484 ePropertyMaxChildrenCount,
3485 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003486 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003487 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003488 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003489 ePropertyRunArgs,
3490 ePropertyEnvVars,
3491 ePropertyInheritEnv,
3492 ePropertyInputPath,
3493 ePropertyOutputPath,
3494 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003495 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003496 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003497 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003498 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003499 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003500 ePropertyUseHexImmediates,
3501 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003502 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003503 ePropertyLoadScriptFromSymbolFile,
Jason Molenda878ae012016-02-19 00:05:17 +00003504 ePropertyLoadCWDlldbinitFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003505 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003506 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003507 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003508 ePropertyDisplayRuntimeSupportValues,
3509 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003510};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003511
Greg Clayton67cc0632012-08-22 17:17:09 +00003512class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003513{
Greg Clayton67cc0632012-08-22 17:17:09 +00003514public:
3515 TargetOptionValueProperties (const ConstString &name) :
3516 OptionValueProperties (name),
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003517 m_target(nullptr),
Greg Clayton67cc0632012-08-22 17:17:09 +00003518 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003519 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003520 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003521
Greg Clayton67cc0632012-08-22 17:17:09 +00003522 // This constructor is used when creating TargetOptionValueProperties when it
3523 // is part of a new lldb_private::Target instance. It will copy all current
3524 // global property values as needed
3525 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3526 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3527 m_target (target),
3528 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003529 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003530 }
3531
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003532 const Property *
3533 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +00003534 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003535 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003536 // try and grab the setting from the current target if there is one. Else we just
3537 // use the one from this instance.
3538 if (idx == ePropertyEnvVars)
3539 GetHostEnvironmentIfNeeded ();
3540
3541 if (exe_ctx)
3542 {
3543 Target *target = exe_ctx->GetTargetPtr();
3544 if (target)
3545 {
3546 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3547 if (this != target_properties)
3548 return target_properties->ProtectedGetPropertyAtIndex (idx);
3549 }
3550 }
3551 return ProtectedGetPropertyAtIndex (idx);
3552 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003553
3554 lldb::TargetSP
3555 GetTargetSP ()
3556 {
3557 return m_target->shared_from_this();
3558 }
3559
Greg Clayton67cc0632012-08-22 17:17:09 +00003560protected:
Greg Clayton67cc0632012-08-22 17:17:09 +00003561 void
3562 GetHostEnvironmentIfNeeded () const
3563 {
3564 if (!m_got_host_env)
3565 {
3566 if (m_target)
3567 {
3568 m_got_host_env = true;
3569 const uint32_t idx = ePropertyInheritEnv;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003570 if (GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0))
Greg Clayton67cc0632012-08-22 17:17:09 +00003571 {
3572 PlatformSP platform_sp (m_target->GetPlatform());
3573 if (platform_sp)
3574 {
3575 StringList env;
3576 if (platform_sp->GetEnvironment(env))
3577 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003578 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary(nullptr, ePropertyEnvVars);
Greg Clayton67cc0632012-08-22 17:17:09 +00003579 if (env_dict)
3580 {
3581 const bool can_replace = false;
3582 const size_t envc = env.GetSize();
3583 for (size_t idx=0; idx<envc; idx++)
3584 {
3585 const char *env_entry = env.GetStringAtIndex (idx);
3586 if (env_entry)
3587 {
3588 const char *equal_pos = ::strchr(env_entry, '=');
3589 ConstString key;
3590 // It is ok to have environment variables with no values
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003591 const char *value = nullptr;
Greg Clayton67cc0632012-08-22 17:17:09 +00003592 if (equal_pos)
3593 {
3594 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3595 if (equal_pos[1])
3596 value = equal_pos + 1;
3597 }
3598 else
3599 {
3600 key.SetCString(env_entry);
3601 }
3602 // Don't allow existing keys to be replaced with ones we get from the platform environment
3603 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3604 }
3605 }
3606 }
3607 }
3608 }
3609 }
3610 }
3611 }
3612 }
3613 Target *m_target;
3614 mutable bool m_got_host_env;
3615};
3616
Greg Claytonfbb76342013-11-20 21:07:01 +00003617//----------------------------------------------------------------------
3618// TargetProperties
3619//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003620TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003621 Properties (),
3622 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003623{
3624 if (target)
3625 {
3626 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003627
3628 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3629 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3630 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3631 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3632 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3633 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3634 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3635 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3636 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3637 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3638
3639 // Update m_launch_info once it was created
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003640 Arg0ValueChangedCallback(this, nullptr);
3641 RunArgsValueChangedCallback(this, nullptr);
3642 //EnvVarsValueChangedCallback(this, nullptr); // FIXME: cause segfault in Target::GetPlatform()
3643 InputPathValueChangedCallback(this, nullptr);
3644 OutputPathValueChangedCallback(this, nullptr);
3645 ErrorPathValueChangedCallback(this, nullptr);
3646 DetachOnErrorValueChangedCallback(this, nullptr);
3647 DisableASLRValueChangedCallback(this, nullptr);
3648 DisableSTDIOValueChangedCallback(this, nullptr);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003649 }
3650 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003651 {
3652 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3653 m_collection_sp->Initialize(g_properties);
3654 m_collection_sp->AppendProperty(ConstString("process"),
3655 ConstString("Settings specify to processes."),
3656 true,
3657 Process::GetGlobalProperties()->GetValueProperties());
3658 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003659}
3660
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003661TargetProperties::~TargetProperties() = default;
3662
Greg Clayton67cc0632012-08-22 17:17:09 +00003663ArchSpec
3664TargetProperties::GetDefaultArchitecture () const
3665{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003666 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(nullptr, ePropertyDefaultArch);
Greg Clayton67cc0632012-08-22 17:17:09 +00003667 if (value)
3668 return value->GetCurrentValue();
3669 return ArchSpec();
3670}
3671
3672void
3673TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3674{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003675 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(nullptr, ePropertyDefaultArch);
Greg Clayton67cc0632012-08-22 17:17:09 +00003676 if (value)
3677 return value->SetCurrentValue(arch, true);
3678}
3679
Ilia K055ad9b2015-05-18 13:41:01 +00003680bool
3681TargetProperties::GetMoveToNearestCode() const
3682{
3683 const uint32_t idx = ePropertyMoveToNearestCode;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003684 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Ilia K055ad9b2015-05-18 13:41:01 +00003685}
3686
Greg Clayton67cc0632012-08-22 17:17:09 +00003687lldb::DynamicValueType
3688TargetProperties::GetPreferDynamicValue() const
3689{
3690 const uint32_t idx = ePropertyPreferDynamic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003691 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003692}
3693
3694bool
Greg Clayton15484402015-05-15 18:40:24 +00003695TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3696{
3697 const uint32_t idx = ePropertyPreferDynamic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003698 return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, d);
Greg Clayton15484402015-05-15 18:40:24 +00003699}
3700
Greg Clayton15484402015-05-15 18:40:24 +00003701bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003702TargetProperties::GetDisableASLR () const
3703{
3704 const uint32_t idx = ePropertyDisableASLR;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003705 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003706}
3707
3708void
3709TargetProperties::SetDisableASLR (bool b)
3710{
3711 const uint32_t idx = ePropertyDisableASLR;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003712 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003713}
3714
3715bool
Jim Ingham106d0282014-06-25 02:32:56 +00003716TargetProperties::GetDetachOnError () const
3717{
3718 const uint32_t idx = ePropertyDetachOnError;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003719 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham106d0282014-06-25 02:32:56 +00003720}
3721
3722void
3723TargetProperties::SetDetachOnError (bool b)
3724{
3725 const uint32_t idx = ePropertyDetachOnError;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003726 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Jim Ingham106d0282014-06-25 02:32:56 +00003727}
3728
3729bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003730TargetProperties::GetDisableSTDIO () const
3731{
3732 const uint32_t idx = ePropertyDisableSTDIO;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003733 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003734}
3735
3736void
3737TargetProperties::SetDisableSTDIO (bool b)
3738{
3739 const uint32_t idx = ePropertyDisableSTDIO;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003740 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003741}
3742
Jim Ingham0f063ba2013-03-02 00:26:47 +00003743const char *
3744TargetProperties::GetDisassemblyFlavor () const
3745{
3746 const uint32_t idx = ePropertyDisassemblyFlavor;
3747 const char *return_value;
3748
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003749 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Jim Ingham0f063ba2013-03-02 00:26:47 +00003750 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3751 return return_value;
3752}
3753
Greg Clayton1f746072012-08-29 21:13:06 +00003754InlineStrategy
3755TargetProperties::GetInlineStrategy () const
3756{
3757 const uint32_t idx = ePropertyInlineStrategy;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003758 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton1f746072012-08-29 21:13:06 +00003759}
3760
Greg Clayton45392552012-10-17 22:57:12 +00003761const char *
3762TargetProperties::GetArg0 () const
3763{
3764 const uint32_t idx = ePropertyArg0;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003765 return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, nullptr);
Greg Clayton45392552012-10-17 22:57:12 +00003766}
3767
3768void
3769TargetProperties::SetArg0 (const char *arg)
3770{
3771 const uint32_t idx = ePropertyArg0;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003772 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003773 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003774}
3775
Greg Clayton67cc0632012-08-22 17:17:09 +00003776bool
3777TargetProperties::GetRunArguments (Args &args) const
3778{
3779 const uint32_t idx = ePropertyRunArgs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003780 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Greg Clayton67cc0632012-08-22 17:17:09 +00003781}
3782
3783void
3784TargetProperties::SetRunArguments (const Args &args)
3785{
3786 const uint32_t idx = ePropertyRunArgs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003787 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003788 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003789}
3790
3791size_t
3792TargetProperties::GetEnvironmentAsArgs (Args &env) const
3793{
3794 const uint32_t idx = ePropertyEnvVars;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003795 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, env);
Greg Clayton67cc0632012-08-22 17:17:09 +00003796}
3797
Ilia K8f37ca52015-02-13 14:31:06 +00003798void
3799TargetProperties::SetEnvironmentFromArgs (const Args &env)
3800{
3801 const uint32_t idx = ePropertyEnvVars;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003802 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003803 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003804}
3805
Greg Clayton67cc0632012-08-22 17:17:09 +00003806bool
3807TargetProperties::GetSkipPrologue() const
3808{
3809 const uint32_t idx = ePropertySkipPrologue;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003810 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003811}
3812
3813PathMappingList &
3814TargetProperties::GetSourcePathMap () const
3815{
3816 const uint32_t idx = ePropertySourceMap;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003817 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(nullptr, false, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003818 assert(option_value);
3819 return option_value->GetCurrentValue();
3820}
3821
3822FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003823TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003824{
3825 const uint32_t idx = ePropertyExecutableSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003826 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003827 assert(option_value);
3828 return option_value->GetCurrentValue();
3829}
3830
Michael Sartaina7499c92013-07-01 19:45:50 +00003831FileSpecList &
3832TargetProperties::GetDebugFileSearchPaths ()
3833{
3834 const uint32_t idx = ePropertyDebugFileSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003835 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Michael Sartaina7499c92013-07-01 19:45:50 +00003836 assert(option_value);
3837 return option_value->GetCurrentValue();
3838}
3839
Sean Callanan85054342015-04-03 15:39:47 +00003840FileSpecList &
3841TargetProperties::GetClangModuleSearchPaths ()
3842{
3843 const uint32_t idx = ePropertyClangModuleSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003844 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Sean Callanan85054342015-04-03 15:39:47 +00003845 assert(option_value);
3846 return option_value->GetCurrentValue();
3847}
3848
Greg Clayton67cc0632012-08-22 17:17:09 +00003849bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003850TargetProperties::GetEnableAutoImportClangModules() const
3851{
3852 const uint32_t idx = ePropertyAutoImportClangModules;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003853 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003854}
3855
3856bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003857TargetProperties::GetEnableSyntheticValue () const
3858{
3859 const uint32_t idx = ePropertyEnableSynthetic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003860 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003861}
3862
3863uint32_t
3864TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3865{
3866 const uint32_t idx = ePropertyMaxChildrenCount;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003867 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003868}
3869
3870uint32_t
3871TargetProperties::GetMaximumSizeOfStringSummary() const
3872{
3873 const uint32_t idx = ePropertyMaxSummaryLength;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003874 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003875}
3876
Enrico Granatad325bf92013-06-04 22:54:16 +00003877uint32_t
3878TargetProperties::GetMaximumMemReadSize () const
3879{
3880 const uint32_t idx = ePropertyMaxMemReadSize;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003881 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granatad325bf92013-06-04 22:54:16 +00003882}
3883
Greg Clayton67cc0632012-08-22 17:17:09 +00003884FileSpec
3885TargetProperties::GetStandardInputPath () const
3886{
3887 const uint32_t idx = ePropertyInputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003888 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003889}
3890
3891void
3892TargetProperties::SetStandardInputPath (const char *p)
3893{
3894 const uint32_t idx = ePropertyInputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003895 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003896}
3897
3898FileSpec
3899TargetProperties::GetStandardOutputPath () const
3900{
3901 const uint32_t idx = ePropertyOutputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003902 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003903}
3904
3905void
3906TargetProperties::SetStandardOutputPath (const char *p)
3907{
3908 const uint32_t idx = ePropertyOutputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003909 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003910}
3911
3912FileSpec
3913TargetProperties::GetStandardErrorPath () const
3914{
3915 const uint32_t idx = ePropertyErrorPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003916 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003917}
3918
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003919LanguageType
3920TargetProperties::GetLanguage () const
3921{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003922 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage(nullptr, ePropertyLanguage);
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003923 if (value)
3924 return value->GetCurrentValue();
3925 return LanguageType();
3926}
3927
Greg Clayton6920b522012-08-22 18:39:03 +00003928const char *
3929TargetProperties::GetExpressionPrefixContentsAsCString ()
3930{
3931 const uint32_t idx = ePropertyExprPrefix;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003932 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false, idx);
Greg Clayton6920b522012-08-22 18:39:03 +00003933 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003934 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003935 const bool null_terminate = true;
3936 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003937 if (data_sp)
3938 return (const char *) data_sp->GetBytes();
3939 }
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003940 return nullptr;
Greg Clayton6920b522012-08-22 18:39:03 +00003941}
3942
Greg Clayton67cc0632012-08-22 17:17:09 +00003943void
3944TargetProperties::SetStandardErrorPath (const char *p)
3945{
3946 const uint32_t idx = ePropertyErrorPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003947 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003948}
3949
3950bool
3951TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3952{
3953 const uint32_t idx = ePropertyBreakpointUseAvoidList;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003954 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003955}
3956
Jim Ingham17d023f2013-03-13 17:58:04 +00003957bool
Daniel Malead79ae052013-08-07 21:54:09 +00003958TargetProperties::GetUseHexImmediates () const
3959{
3960 const uint32_t idx = ePropertyUseHexImmediates;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003961 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Daniel Malead79ae052013-08-07 21:54:09 +00003962}
3963
3964bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003965TargetProperties::GetUseFastStepping () const
3966{
3967 const uint32_t idx = ePropertyUseFastStepping;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003968 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham17d023f2013-03-13 17:58:04 +00003969}
3970
Greg Claytonfb6621e2013-12-06 21:59:52 +00003971bool
3972TargetProperties::GetDisplayExpressionsInCrashlogs () const
3973{
3974 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003975 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Claytonfb6621e2013-12-06 21:59:52 +00003976}
3977
Enrico Granata397ddd52013-05-21 20:13:34 +00003978LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003979TargetProperties::GetLoadScriptFromSymbolFile () const
3980{
3981 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003982 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003983}
3984
Jason Molenda878ae012016-02-19 00:05:17 +00003985LoadCWDlldbinitFile
3986TargetProperties::GetLoadCWDlldbinitFile () const
3987{
3988 const uint32_t idx = ePropertyLoadCWDlldbinitFile;
3989 return (LoadCWDlldbinitFile) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
3990}
3991
Daniel Malead79ae052013-08-07 21:54:09 +00003992Disassembler::HexImmediateStyle
3993TargetProperties::GetHexImmediateStyle () const
3994{
3995 const uint32_t idx = ePropertyHexImmediateStyle;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003996 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Daniel Malead79ae052013-08-07 21:54:09 +00003997}
3998
Greg Claytonfd814c52013-08-13 01:42:25 +00003999MemoryModuleLoadLevel
4000TargetProperties::GetMemoryModuleLoadLevel() const
4001{
4002 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004003 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Claytonfd814c52013-08-13 01:42:25 +00004004}
4005
Jason Molendaa4bea722014-02-14 05:06:49 +00004006bool
4007TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
4008{
4009 const uint32_t idx = ePropertyTrapHandlerNames;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004010 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00004011}
Greg Claytonfd814c52013-08-13 01:42:25 +00004012
Jason Molendaa4bea722014-02-14 05:06:49 +00004013void
4014TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
4015{
4016 const uint32_t idx = ePropertyTrapHandlerNames;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004017 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00004018}
Greg Clayton67cc0632012-08-22 17:17:09 +00004019
Enrico Granata560558e2015-02-11 02:35:39 +00004020bool
4021TargetProperties::GetDisplayRuntimeSupportValues () const
4022{
4023 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004024 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Enrico Granata560558e2015-02-11 02:35:39 +00004025}
4026
4027void
4028TargetProperties::SetDisplayRuntimeSupportValues (bool b)
4029{
4030 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004031 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Enrico Granata560558e2015-02-11 02:35:39 +00004032}
4033
Ewan Crawford78baa192015-05-13 09:18:18 +00004034bool
4035TargetProperties::GetNonStopModeEnabled () const
4036{
4037 const uint32_t idx = ePropertyNonStopModeEnabled;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004038 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Ewan Crawford78baa192015-05-13 09:18:18 +00004039}
4040
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004041void
4042TargetProperties::SetNonStopModeEnabled (bool b)
4043{
4044 const uint32_t idx = ePropertyNonStopModeEnabled;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004045 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004046}
4047
Ilia K8f37ca52015-02-13 14:31:06 +00004048const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00004049TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00004050{
Ilia Kcc39d3f2015-02-13 17:07:55 +00004051 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00004052 return m_launch_info;
4053}
4054
4055void
4056TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
4057{
4058 m_launch_info = launch_info;
4059 SetArg0(launch_info.GetArg0());
4060 SetRunArguments(launch_info.GetArguments());
4061 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
4062 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
4063 if (input_file_action)
4064 {
4065 const char *input_path = input_file_action->GetPath();
4066 if (input_path)
4067 SetStandardInputPath(input_path);
4068 }
4069 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
4070 if (output_file_action)
4071 {
4072 const char *output_path = output_file_action->GetPath();
4073 if (output_path)
4074 SetStandardOutputPath(output_path);
4075 }
4076 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
4077 if (error_file_action)
4078 {
4079 const char *error_path = error_file_action->GetPath();
4080 if (error_path)
4081 SetStandardErrorPath(error_path);
4082 }
4083 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
4084 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4085 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
4086}
4087
4088void
4089TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
4090{
4091 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4092 this_->m_launch_info.SetArg0(this_->GetArg0());
4093}
4094
4095void
4096TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
4097{
4098 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4099 Args args;
4100 if (this_->GetRunArguments(args))
4101 this_->m_launch_info.GetArguments() = args;
4102}
4103
4104void
4105TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
4106{
4107 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4108 Args args;
4109 if (this_->GetEnvironmentAsArgs(args))
4110 this_->m_launch_info.GetEnvironmentEntries() = args;
4111}
4112
4113void
4114TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4115{
4116 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004117 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004118}
4119
4120void
4121TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4122{
4123 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004124 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004125}
4126
4127void
4128TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4129{
4130 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004131 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004132}
4133
4134void
4135TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
4136{
4137 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4138 if (this_->GetDetachOnError())
4139 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4140 else
4141 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
4142}
4143
4144void
4145TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
4146{
4147 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4148 if (this_->GetDisableASLR())
4149 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4150 else
4151 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
4152}
4153
4154void
4155TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
4156{
4157 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4158 if (this_->GetDisableSTDIO())
4159 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4160 else
4161 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4162}
Ilia Keb2c19a2015-03-10 21:59:55 +00004163
4164//----------------------------------------------------------------------
4165// Target::TargetEventData
4166//----------------------------------------------------------------------
4167
4168Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
4169 EventData (),
4170 m_target_sp (target_sp),
4171 m_module_list ()
4172{
4173}
4174
4175Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
4176 EventData (),
4177 m_target_sp (target_sp),
4178 m_module_list (module_list)
4179{
4180}
4181
Eugene Zelenko8f30a652015-10-23 18:39:37 +00004182Target::TargetEventData::~TargetEventData() = default;
Ilia Keb2c19a2015-03-10 21:59:55 +00004183
4184const ConstString &
4185Target::TargetEventData::GetFlavorString ()
4186{
4187 static ConstString g_flavor ("Target::TargetEventData");
4188 return g_flavor;
4189}
4190
4191void
4192Target::TargetEventData::Dump (Stream *s) const
4193{
Oleksiy Vyalov8b85b032016-01-19 17:54:47 +00004194 for (size_t i = 0; i < m_module_list.GetSize(); ++i)
4195 {
4196 if (i != 0)
4197 *s << ", ";
4198 m_module_list.GetModuleAtIndex(i)->GetDescription(s, lldb::eDescriptionLevelBrief);
4199 }
Ilia Keb2c19a2015-03-10 21:59:55 +00004200}
4201
4202const Target::TargetEventData *
4203Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4204{
4205 if (event_ptr)
4206 {
4207 const EventData *event_data = event_ptr->GetData();
4208 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4209 return static_cast <const TargetEventData *> (event_ptr->GetData());
4210 }
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004211 return nullptr;
Ilia Keb2c19a2015-03-10 21:59:55 +00004212}
4213
4214TargetSP
4215Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4216{
4217 TargetSP target_sp;
4218 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4219 if (event_data)
4220 target_sp = event_data->m_target_sp;
4221 return target_sp;
4222}
4223
4224ModuleList
4225Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4226{
4227 ModuleList module_list;
4228 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4229 if (event_data)
4230 module_list = event_data->m_module_list;
4231 return module_list;
4232}