blob: fc6b1d782e0c50ff5a8799f6a63925d73210ccce [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 {
Kate Stone07612022016-04-21 00:56:08 +0000225 err.SetErrorStringWithFormat("LLDB isn't configured with REPL support for any languages.");
Sean Callanan97f84e82015-10-21 19:31:17 +0000226 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,
Jim Ingham76bb8d62016-04-28 01:40:57 +0000329 const std::unordered_set<std::string> &function_names,
Greg Clayton1f746072012-08-29 21:13:06 +0000330 RegularExpression &source_regex,
Greg Claytoneb023e72013-10-11 19:48:25 +0000331 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000332 bool hardware,
333 LazyBool move_to_nearest_code)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334{
Jim Ingham87df91b2011-09-23 00:54:11 +0000335 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, source_file_spec_list));
Ilia K055ad9b2015-05-18 13:41:01 +0000336 if (move_to_nearest_code == eLazyBoolCalculate)
337 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Jim Ingham76bb8d62016-04-28 01:40:57 +0000338 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex(nullptr,
339 source_regex,
340 function_names,
341 !static_cast<bool>(move_to_nearest_code)));
342
Jim Ingham1460e4b2014-01-10 23:46:59 +0000343 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham969795f2011-09-21 01:17:13 +0000344}
345
Jim Ingham969795f2011-09-21 01:17:13 +0000346BreakpointSP
Jim Inghama8558b62012-05-22 00:12:20 +0000347Target::CreateBreakpoint (const FileSpecList *containingModules,
348 const FileSpec &file,
349 uint32_t line_no,
Jim Ingham24111672016-03-09 18:59:13 +0000350 lldb::addr_t offset,
Greg Clayton1f746072012-08-29 21:13:06 +0000351 LazyBool check_inlines,
Jim Inghama8558b62012-05-22 00:12:20 +0000352 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000353 bool internal,
Ilia K055ad9b2015-05-18 13:41:01 +0000354 bool hardware,
355 LazyBool move_to_nearest_code)
Jim Ingham969795f2011-09-21 01:17:13 +0000356{
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000357 FileSpec remapped_file;
358 ConstString remapped_path;
359 if (GetSourcePathMap().ReverseRemapPath(ConstString(file.GetPath().c_str()), remapped_path))
360 remapped_file.SetFile(remapped_path.AsCString(), true);
361 else
362 remapped_file = file;
363
Greg Clayton1f746072012-08-29 21:13:06 +0000364 if (check_inlines == eLazyBoolCalculate)
365 {
366 const InlineStrategy inline_strategy = GetInlineStrategy();
367 switch (inline_strategy)
368 {
369 case eInlineBreakpointsNever:
370 check_inlines = eLazyBoolNo;
371 break;
372
373 case eInlineBreakpointsHeaders:
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000374 if (remapped_file.IsSourceImplementationFile())
Greg Clayton1f746072012-08-29 21:13:06 +0000375 check_inlines = eLazyBoolNo;
376 else
377 check_inlines = eLazyBoolYes;
378 break;
379
380 case eInlineBreakpointsAlways:
381 check_inlines = eLazyBoolYes;
382 break;
383 }
384 }
Greg Clayton93bfb292012-09-07 23:48:57 +0000385 SearchFilterSP filter_sp;
386 if (check_inlines == eLazyBoolNo)
387 {
388 // Not checking for inlines, we are looking only for matching compile units
389 FileSpecList compile_unit_list;
Tamas Berghammerb0b1ea32016-03-04 11:26:44 +0000390 compile_unit_list.Append (remapped_file);
Greg Clayton93bfb292012-09-07 23:48:57 +0000391 filter_sp = GetSearchFilterForModuleAndCUList (containingModules, &compile_unit_list);
392 }
393 else
394 {
395 filter_sp = GetSearchFilterForModuleList (containingModules);
396 }
Greg Clayton03da4cc2013-04-19 21:31:16 +0000397 if (skip_prologue == eLazyBoolCalculate)
398 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Ilia K055ad9b2015-05-18 13:41:01 +0000399 if (move_to_nearest_code == eLazyBoolCalculate)
400 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
Greg Clayton03da4cc2013-04-19 21:31:16 +0000401
Jim Ingham24111672016-03-09 18:59:13 +0000402 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine (nullptr,
403 remapped_file,
404 line_no,
405 offset,
406 check_inlines,
407 skip_prologue,
408 !static_cast<bool>(move_to_nearest_code)));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000409 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410}
411
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412BreakpointSP
Greg Claytoneb023e72013-10-11 19:48:25 +0000413Target::CreateBreakpoint (lldb::addr_t addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 Address so_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +0000416
417 // Check for any reason we want to move this breakpoint to other address.
418 addr = GetBreakableLoadAddress(addr);
419
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 // Attempt to resolve our load address if possible, though it is ok if
421 // it doesn't resolve to section/offset.
422
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000423 // Try and resolve as a load address if possible
Greg Claytond5944cd2013-12-06 01:12:00 +0000424 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000425 if (!so_addr.IsValid())
426 {
427 // The address didn't resolve, so just set this as an absolute address
428 so_addr.SetOffset (addr);
429 }
Greg Claytoneb023e72013-10-11 19:48:25 +0000430 BreakpointSP bp_sp (CreateBreakpoint(so_addr, internal, hardware));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 return bp_sp;
432}
433
434BreakpointSP
Jim Ingham055a08a2015-11-17 03:39:13 +0000435Target::CreateBreakpoint (const Address &addr, bool internal, bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436{
Jim Ingham33df7cd2014-12-06 01:28:03 +0000437 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000438 BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(nullptr, addr));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000439 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440}
441
Jim Ingham055a08a2015-11-17 03:39:13 +0000442lldb::BreakpointSP
443Target::CreateAddressInModuleBreakpoint (lldb::addr_t file_addr,
444 bool internal,
445 const FileSpec *file_spec,
446 bool request_hardware)
447{
448 SearchFilterSP filter_sp(new SearchFilterForUnconstrainedSearches (shared_from_this()));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000449 BreakpointResolverSP resolver_sp(new BreakpointResolverAddress(nullptr, file_addr, file_spec));
Jim Ingham055a08a2015-11-17 03:39:13 +0000450 return CreateBreakpoint (filter_sp, resolver_sp, internal, request_hardware, false);
451}
452
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000454Target::CreateBreakpoint (const FileSpecList *containingModules,
455 const FileSpecList *containingSourceFiles,
Greg Claytond16e1e52011-07-12 17:06:17 +0000456 const char *func_name,
Jim Ingham24111672016-03-09 18:59:13 +0000457 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000458 LanguageType language,
Jim Ingham24111672016-03-09 18:59:13 +0000459 lldb::addr_t offset,
Jim Inghama8558b62012-05-22 00:12:20 +0000460 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000461 bool internal,
462 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463{
Greg Clayton0c5cd902010-06-28 21:30:43 +0000464 BreakpointSP bp_sp;
465 if (func_name)
466 {
Jim Ingham87df91b2011-09-23 00:54:11 +0000467 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000468
469 if (skip_prologue == eLazyBoolCalculate)
470 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000471 if (language == lldb::eLanguageTypeUnknown)
472 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000473
Jim Ingham24111672016-03-09 18:59:13 +0000474 BreakpointResolverSP resolver_sp (new BreakpointResolverName (nullptr,
475 func_name,
476 func_name_type_mask,
477 language,
478 Breakpoint::Exact,
479 offset,
480 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000481 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Greg Clayton0c5cd902010-06-28 21:30:43 +0000482 }
483 return bp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484}
485
Jim Inghamfab10e82012-03-06 00:37:27 +0000486lldb::BreakpointSP
487Target::CreateBreakpoint (const FileSpecList *containingModules,
Greg Clayton4116e932012-05-15 02:33:01 +0000488 const FileSpecList *containingSourceFiles,
489 const std::vector<std::string> &func_names,
490 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000491 LanguageType language,
Jim Ingham24111672016-03-09 18:59:13 +0000492 lldb::addr_t offset,
Jim Inghama8558b62012-05-22 00:12:20 +0000493 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000494 bool internal,
495 bool hardware)
Jim Inghamfab10e82012-03-06 00:37:27 +0000496{
497 BreakpointSP bp_sp;
498 size_t num_names = func_names.size();
499 if (num_names > 0)
500 {
501 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Greg Clayton03da4cc2013-04-19 21:31:16 +0000502
503 if (skip_prologue == eLazyBoolCalculate)
504 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000505 if (language == lldb::eLanguageTypeUnknown)
506 language = GetLanguage();
Greg Clayton03da4cc2013-04-19 21:31:16 +0000507
Jim Ingham24111672016-03-09 18:59:13 +0000508
509 BreakpointResolverSP resolver_sp (new BreakpointResolverName (nullptr,
510 func_names,
511 func_name_type_mask,
512 language,
513 offset,
514 skip_prologue));
Jim Ingham1460e4b2014-01-10 23:46:59 +0000515 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Inghamfab10e82012-03-06 00:37:27 +0000516 }
517 return bp_sp;
518}
519
Jim Ingham133e0fb2012-03-03 02:05:11 +0000520BreakpointSP
521Target::CreateBreakpoint (const FileSpecList *containingModules,
522 const FileSpecList *containingSourceFiles,
523 const char *func_names[],
524 size_t num_names,
Jim Ingham24111672016-03-09 18:59:13 +0000525 uint32_t func_name_type_mask,
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000526 LanguageType language,
Jim Ingham24111672016-03-09 18:59:13 +0000527 lldb::addr_t offset,
Jim Inghama8558b62012-05-22 00:12:20 +0000528 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000529 bool internal,
530 bool hardware)
Jim Ingham133e0fb2012-03-03 02:05:11 +0000531{
532 BreakpointSP bp_sp;
533 if (num_names > 0)
534 {
535 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
536
Greg Clayton03da4cc2013-04-19 21:31:16 +0000537 if (skip_prologue == eLazyBoolCalculate)
Jim Ingham24111672016-03-09 18:59:13 +0000538 {
539 if (offset == 0)
540 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
541 else
542 skip_prologue = eLazyBoolNo;
543 }
Dawn Perchik23b1dec2015-07-21 22:05:07 +0000544 if (language == lldb::eLanguageTypeUnknown)
545 language = GetLanguage();
546
Jim Ingham24111672016-03-09 18:59:13 +0000547 BreakpointResolverSP resolver_sp (new BreakpointResolverName (nullptr,
548 func_names,
549 num_names,
550 func_name_type_mask,
551 language,
552 offset,
553 skip_prologue));
554 resolver_sp->SetOffset(offset);
Jim Ingham1460e4b2014-01-10 23:46:59 +0000555 bp_sp = CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Jim Ingham133e0fb2012-03-03 02:05:11 +0000556 }
557 return bp_sp;
558}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559
560SearchFilterSP
561Target::GetSearchFilterForModule (const FileSpec *containingModule)
562{
563 SearchFilterSP filter_sp;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000564 if (containingModule != nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 {
566 // TODO: We should look into sharing module based search filters
567 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000568 filter_sp.reset (new SearchFilterByModule (shared_from_this(), *containingModule));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569 }
570 else
571 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000572 if (!m_search_filter_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000573 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574 filter_sp = m_search_filter_sp;
575 }
576 return filter_sp;
577}
578
Jim Ingham969795f2011-09-21 01:17:13 +0000579SearchFilterSP
580Target::GetSearchFilterForModuleList (const FileSpecList *containingModules)
581{
582 SearchFilterSP filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000583 if (containingModules && containingModules->GetSize() != 0)
584 {
585 // TODO: We should look into sharing module based search filters
586 // across many breakpoints like we do for the simple target based one
Greg Claytone1cd1be2012-01-29 20:56:30 +0000587 filter_sp.reset (new SearchFilterByModuleList (shared_from_this(), *containingModules));
Jim Ingham969795f2011-09-21 01:17:13 +0000588 }
589 else
590 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000591 if (!m_search_filter_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +0000592 m_search_filter_sp.reset (new SearchFilterForUnconstrainedSearches (shared_from_this()));
Jim Ingham969795f2011-09-21 01:17:13 +0000593 filter_sp = m_search_filter_sp;
594 }
595 return filter_sp;
596}
597
Jim Ingham87df91b2011-09-23 00:54:11 +0000598SearchFilterSP
Jim Inghama8558b62012-05-22 00:12:20 +0000599Target::GetSearchFilterForModuleAndCUList (const FileSpecList *containingModules,
600 const FileSpecList *containingSourceFiles)
Jim Ingham87df91b2011-09-23 00:54:11 +0000601{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000602 if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
Jim Ingham87df91b2011-09-23 00:54:11 +0000603 return GetSearchFilterForModuleList(containingModules);
604
605 SearchFilterSP filter_sp;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +0000606 if (containingModules == nullptr)
Jim Ingham87df91b2011-09-23 00:54:11 +0000607 {
608 // We could make a special "CU List only SearchFilter". Better yet was if these could be composable,
609 // but that will take a little reworking.
610
Greg Claytone1cd1be2012-01-29 20:56:30 +0000611 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), FileSpecList(), *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000612 }
613 else
614 {
Greg Claytone1cd1be2012-01-29 20:56:30 +0000615 filter_sp.reset (new SearchFilterByModuleListAndCU (shared_from_this(), *containingModules, *containingSourceFiles));
Jim Ingham87df91b2011-09-23 00:54:11 +0000616 }
617 return filter_sp;
618}
619
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620BreakpointSP
Jim Ingham87df91b2011-09-23 00:54:11 +0000621Target::CreateFuncRegexBreakpoint (const FileSpecList *containingModules,
Jim Inghama8558b62012-05-22 00:12:20 +0000622 const FileSpecList *containingSourceFiles,
623 RegularExpression &func_regex,
Jim Ingham0fcdac32015-11-06 22:48:59 +0000624 lldb::LanguageType requested_language,
Jim Inghama8558b62012-05-22 00:12:20 +0000625 LazyBool skip_prologue,
Greg Claytoneb023e72013-10-11 19:48:25 +0000626 bool internal,
627 bool hardware)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628{
Jim Ingham87df91b2011-09-23 00:54:11 +0000629 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList (containingModules, containingSourceFiles));
Saleem Abdulrasoolb5c128b2014-07-23 01:53:52 +0000630 bool skip =
631 (skip_prologue == eLazyBoolCalculate) ? GetSkipPrologue()
632 : static_cast<bool>(skip_prologue);
Jim Ingham24111672016-03-09 18:59:13 +0000633 BreakpointResolverSP resolver_sp(new BreakpointResolverName (nullptr,
634 func_regex,
635 requested_language,
636 0,
637 skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638
Jim Ingham1460e4b2014-01-10 23:46:59 +0000639 return CreateBreakpoint (filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640}
641
Jim Ingham219ba192012-03-05 04:47:34 +0000642lldb::BreakpointSP
Jim Inghama72b31c2015-04-22 19:42:18 +0000643Target::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 +0000644{
Jim Inghama72b31c2015-04-22 19:42:18 +0000645 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint (*this, language, catch_bp, throw_bp, internal);
646 if (exc_bkpt_sp && additional_args)
647 {
648 Breakpoint::BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition();
649 if (precondition_sp && additional_args)
650 {
651 if (error)
652 *error = precondition_sp->ConfigurePrecondition(*additional_args);
653 else
654 precondition_sp->ConfigurePrecondition(*additional_args);
655 }
656 }
657 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000658}
Jim Inghama72b31c2015-04-22 19:42:18 +0000659
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660BreakpointSP
Jim Ingham1460e4b2014-01-10 23:46:59 +0000661Target::CreateBreakpoint (SearchFilterSP &filter_sp, BreakpointResolverSP &resolver_sp, bool internal, bool request_hardware, bool resolve_indirect_symbols)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662{
663 BreakpointSP bp_sp;
664 if (filter_sp && resolver_sp)
665 {
Jim Ingham1460e4b2014-01-10 23:46:59 +0000666 bp_sp.reset(new Breakpoint (*this, filter_sp, resolver_sp, request_hardware, resolve_indirect_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 resolver_sp->SetBreakpoint (bp_sp.get());
Jim Ingham33df7cd2014-12-06 01:28:03 +0000668 AddBreakpoint (bp_sp, internal);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000670 return bp_sp;
671}
672
673void
674Target::AddBreakpoint (lldb::BreakpointSP bp_sp, bool internal)
675{
676 if (!bp_sp)
677 return;
678 if (internal)
679 m_internal_breakpoint_list.Add (bp_sp, false);
680 else
681 m_breakpoint_list.Add (bp_sp, true);
682
683 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
684 if (log)
685 {
686 StreamString s;
687 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
688 log->Printf ("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no", s.GetData());
689 }
690
691 bp_sp->ResolveBreakpoint();
692
693 if (!internal)
Jim Ingham36f3b362010-10-14 23:45:03 +0000694 {
695 m_last_created_breakpoint = bp_sp;
696 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697}
698
Johnny Chen86364b42011-09-20 23:28:55 +0000699bool
700Target::ProcessIsValid()
701{
702 return (m_process_sp && m_process_sp->IsAlive());
703}
704
Johnny Chenb90827e2012-06-04 23:19:54 +0000705static bool
706CheckIfWatchpointsExhausted(Target *target, Error &error)
707{
708 uint32_t num_supported_hardware_watchpoints;
709 Error rc = target->GetProcessSP()->GetWatchpointSupportInfo(num_supported_hardware_watchpoints);
710 if (rc.Success())
711 {
712 uint32_t num_current_watchpoints = target->GetWatchpointList().GetSize();
713 if (num_current_watchpoints >= num_supported_hardware_watchpoints)
714 error.SetErrorStringWithFormat("number of supported hardware watchpoints (%u) has been reached",
715 num_supported_hardware_watchpoints);
716 }
717 return false;
718}
719
Johnny Chen01a67862011-10-14 00:42:25 +0000720// See also Watchpoint::SetWatchpointType(uint32_t type) and
Johnny Chenab9ee762011-09-14 00:26:03 +0000721// the OptionGroupWatchpoint::WatchType enum type.
Johnny Chen01a67862011-10-14 00:42:25 +0000722WatchpointSP
Greg Claytona1e5dc82015-08-11 22:53:00 +0000723Target::CreateWatchpoint(lldb::addr_t addr, size_t size, const CompilerType *type, uint32_t kind, Error &error)
Johnny Chen887062a2011-09-12 23:38:44 +0000724{
Greg Clayton5160ce52013-03-27 23:08:40 +0000725 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen0c406372011-09-14 20:23:45 +0000726 if (log)
Daniel Malead01b2952012-11-29 21:49:15 +0000727 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 " type = %u)\n",
Jim Inghama7dfb662012-10-23 07:20:06 +0000728 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000729
Johnny Chen01a67862011-10-14 00:42:25 +0000730 WatchpointSP wp_sp;
Johnny Chen86364b42011-09-20 23:28:55 +0000731 if (!ProcessIsValid())
Johnny Chenb90827e2012-06-04 23:19:54 +0000732 {
733 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000734 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000735 }
Jim Inghamc6462312013-06-18 21:52:48 +0000736
Johnny Chen45e541f2011-09-14 22:20:15 +0000737 if (addr == LLDB_INVALID_ADDRESS || size == 0)
Johnny Chenb90827e2012-06-04 23:19:54 +0000738 {
739 if (size == 0)
740 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
741 else
Daniel Malead01b2952012-11-29 21:49:15 +0000742 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
Johnny Chen01a67862011-10-14 00:42:25 +0000743 return wp_sp;
Johnny Chenb90827e2012-06-04 23:19:54 +0000744 }
Jim Inghamc6462312013-06-18 21:52:48 +0000745
746 if (!LLDB_WATCH_TYPE_IS_VALID(kind))
747 {
748 error.SetErrorStringWithFormat ("invalid watchpoint type: %d", kind);
749 }
Johnny Chen7313a642011-09-13 01:15:36 +0000750
Johnny Chen01a67862011-10-14 00:42:25 +0000751 // Currently we only support one watchpoint per address, with total number
752 // of watchpoints limited by the hardware which the inferior is running on.
Johnny Chen7385a5a2012-05-31 22:56:36 +0000753
754 // Grab the list mutex while doing operations.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000755 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 +0000756 Mutex::Locker locker;
757 this->GetWatchpointList().GetListMutex(locker);
Johnny Chen01a67862011-10-14 00:42:25 +0000758 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
Johnny Chen3c532582011-09-13 23:29:31 +0000759 if (matched_sp)
760 {
Johnny Chen0c406372011-09-14 20:23:45 +0000761 size_t old_size = matched_sp->GetByteSize();
Johnny Chen3c532582011-09-13 23:29:31 +0000762 uint32_t old_type =
Johnny Chen0c406372011-09-14 20:23:45 +0000763 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
764 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
Johnny Chen01a67862011-10-14 00:42:25 +0000765 // Return the existing watchpoint if both size and type match.
Jim Inghamc6462312013-06-18 21:52:48 +0000766 if (size == old_size && kind == old_type)
767 {
Johnny Chen01a67862011-10-14 00:42:25 +0000768 wp_sp = matched_sp;
Jim Ingham1b5792e2012-12-18 02:03:49 +0000769 wp_sp->SetEnabled(false, notify);
Jim Inghamc6462312013-06-18 21:52:48 +0000770 }
771 else
772 {
Johnny Chen01a67862011-10-14 00:42:25 +0000773 // Nil the matched watchpoint; we will be creating a new one.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000774 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
775 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000776 }
Johnny Chen3c532582011-09-13 23:29:31 +0000777 }
778
Jason Molenda727e3922012-12-05 23:07:34 +0000779 if (!wp_sp)
780 {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000781 wp_sp.reset(new Watchpoint(*this, addr, size, type));
782 wp_sp->SetWatchpointType(kind, notify);
783 m_watchpoint_list.Add (wp_sp, true);
Johnny Chen45e541f2011-09-14 22:20:15 +0000784 }
Johnny Chen0c406372011-09-14 20:23:45 +0000785
Jim Ingham1b5792e2012-12-18 02:03:49 +0000786 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
Johnny Chen0c406372011-09-14 20:23:45 +0000787 if (log)
Jason Molenda727e3922012-12-05 23:07:34 +0000788 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
789 __FUNCTION__,
790 error.Success() ? "succeeded" : "failed",
791 wp_sp->GetID());
Johnny Chen0c406372011-09-14 20:23:45 +0000792
Jason Molenda727e3922012-12-05 23:07:34 +0000793 if (error.Fail())
794 {
Johnny Chen41b77262012-03-26 22:00:10 +0000795 // Enabling the watchpoint on the device side failed.
796 // Remove the said watchpoint from the list maintained by the target instance.
Jim Ingham1b5792e2012-12-18 02:03:49 +0000797 m_watchpoint_list.Remove (wp_sp->GetID(), true);
Johnny Chenb90827e2012-06-04 23:19:54 +0000798 // See if we could provide more helpful error message.
799 if (!CheckIfWatchpointsExhausted(this, error))
800 {
801 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
Greg Clayton6fea17e2014-03-03 19:15:20 +0000802 error.SetErrorStringWithFormat("watch size of %" PRIu64 " is not supported", (uint64_t)size);
Johnny Chenb90827e2012-06-04 23:19:54 +0000803 }
Johnny Chen01a67862011-10-14 00:42:25 +0000804 wp_sp.reset();
Johnny Chen41b77262012-03-26 22:00:10 +0000805 }
Johnny Chen9d954d82011-09-27 20:29:45 +0000806 else
Johnny Chen01a67862011-10-14 00:42:25 +0000807 m_last_created_watchpoint = wp_sp;
808 return wp_sp;
Johnny Chen887062a2011-09-12 23:38:44 +0000809}
810
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811void
812Target::RemoveAllBreakpoints (bool internal_also)
813{
Greg Clayton5160ce52013-03-27 23:08:40 +0000814 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 if (log)
816 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
817
Greg Clayton9fed0d82010-07-23 23:33:17 +0000818 m_breakpoint_list.RemoveAll (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819 if (internal_also)
Greg Clayton9fed0d82010-07-23 23:33:17 +0000820 m_internal_breakpoint_list.RemoveAll (false);
Jim Ingham36f3b362010-10-14 23:45:03 +0000821
822 m_last_created_breakpoint.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823}
824
825void
826Target::DisableAllBreakpoints (bool internal_also)
827{
Greg Clayton5160ce52013-03-27 23:08:40 +0000828 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 if (log)
830 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
831
832 m_breakpoint_list.SetEnabledAll (false);
833 if (internal_also)
834 m_internal_breakpoint_list.SetEnabledAll (false);
835}
836
837void
838Target::EnableAllBreakpoints (bool internal_also)
839{
Greg Clayton5160ce52013-03-27 23:08:40 +0000840 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 if (log)
842 log->Printf ("Target::%s (internal_also = %s)\n", __FUNCTION__, internal_also ? "yes" : "no");
843
844 m_breakpoint_list.SetEnabledAll (true);
845 if (internal_also)
846 m_internal_breakpoint_list.SetEnabledAll (true);
847}
848
849bool
850Target::RemoveBreakpointByID (break_id_t break_id)
851{
Greg Clayton5160ce52013-03-27 23:08:40 +0000852 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000853 if (log)
854 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
855
856 if (DisableBreakpointByID (break_id))
857 {
858 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
Greg Clayton9fed0d82010-07-23 23:33:17 +0000859 m_internal_breakpoint_list.Remove(break_id, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 else
Jim Ingham36f3b362010-10-14 23:45:03 +0000861 {
Greg Claytonaa1c5872011-01-24 23:35:47 +0000862 if (m_last_created_breakpoint)
863 {
864 if (m_last_created_breakpoint->GetID() == break_id)
865 m_last_created_breakpoint.reset();
866 }
Greg Clayton9fed0d82010-07-23 23:33:17 +0000867 m_breakpoint_list.Remove(break_id, true);
Jim Ingham36f3b362010-10-14 23:45:03 +0000868 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869 return true;
870 }
871 return false;
872}
873
874bool
875Target::DisableBreakpointByID (break_id_t break_id)
876{
Greg Clayton5160ce52013-03-27 23:08:40 +0000877 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 if (log)
879 log->Printf ("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__, break_id, LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
880
881 BreakpointSP bp_sp;
882
883 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
884 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
885 else
886 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
887 if (bp_sp)
888 {
889 bp_sp->SetEnabled (false);
890 return true;
891 }
892 return false;
893}
894
895bool
896Target::EnableBreakpointByID (break_id_t break_id)
897{
Greg Clayton5160ce52013-03-27 23:08:40 +0000898 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899 if (log)
900 log->Printf ("Target::%s (break_id = %i, internal = %s)\n",
901 __FUNCTION__,
902 break_id,
903 LLDB_BREAK_ID_IS_INTERNAL (break_id) ? "yes" : "no");
904
905 BreakpointSP bp_sp;
906
907 if (LLDB_BREAK_ID_IS_INTERNAL (break_id))
908 bp_sp = m_internal_breakpoint_list.FindBreakpointByID (break_id);
909 else
910 bp_sp = m_breakpoint_list.FindBreakpointByID (break_id);
911
912 if (bp_sp)
913 {
914 bp_sp->SetEnabled (true);
915 return true;
916 }
917 return false;
918}
919
Johnny Chenedf50372011-09-23 21:21:43 +0000920// The flag 'end_to_end', default to true, signifies that the operation is
921// performed end to end, for both the debugger and the debuggee.
922
Johnny Chen01a67862011-10-14 00:42:25 +0000923// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
924// to end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000925bool
Johnny Chen01a67862011-10-14 00:42:25 +0000926Target::RemoveAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000927{
Greg Clayton5160ce52013-03-27 23:08:40 +0000928 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000929 if (log)
930 log->Printf ("Target::%s\n", __FUNCTION__);
931
Johnny Chenedf50372011-09-23 21:21:43 +0000932 if (!end_to_end) {
Jim Ingham1b5792e2012-12-18 02:03:49 +0000933 m_watchpoint_list.RemoveAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +0000934 return true;
935 }
936
937 // Otherwise, it's an end to end operation.
938
Johnny Chen86364b42011-09-20 23:28:55 +0000939 if (!ProcessIsValid())
940 return false;
941
Johnny Chen01a67862011-10-14 00:42:25 +0000942 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000943 for (size_t i = 0; i < num_watchpoints; ++i)
944 {
Johnny Chen01a67862011-10-14 00:42:25 +0000945 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
946 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000947 return false;
948
Johnny Chen01a67862011-10-14 00:42:25 +0000949 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000950 if (rc.Fail())
951 return false;
952 }
Jim Ingham1b5792e2012-12-18 02:03:49 +0000953 m_watchpoint_list.RemoveAll (true);
Jim Inghamb0b45132013-07-02 02:09:46 +0000954 m_last_created_watchpoint.reset();
Johnny Chen86364b42011-09-20 23:28:55 +0000955 return true; // Success!
956}
957
Johnny Chen01a67862011-10-14 00:42:25 +0000958// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
959// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000960bool
Johnny Chen01a67862011-10-14 00:42:25 +0000961Target::DisableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000962{
Greg Clayton5160ce52013-03-27 23:08:40 +0000963 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000964 if (log)
965 log->Printf ("Target::%s\n", __FUNCTION__);
966
Johnny Chenedf50372011-09-23 21:21:43 +0000967 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +0000968 m_watchpoint_list.SetEnabledAll(false);
Johnny Chenedf50372011-09-23 21:21:43 +0000969 return true;
970 }
971
972 // Otherwise, it's an end to end operation.
973
Johnny Chen86364b42011-09-20 23:28:55 +0000974 if (!ProcessIsValid())
975 return false;
976
Johnny Chen01a67862011-10-14 00:42:25 +0000977 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +0000978 for (size_t i = 0; i < num_watchpoints; ++i)
979 {
Johnny Chen01a67862011-10-14 00:42:25 +0000980 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
981 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +0000982 return false;
983
Johnny Chen01a67862011-10-14 00:42:25 +0000984 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +0000985 if (rc.Fail())
986 return false;
987 }
Johnny Chen86364b42011-09-20 23:28:55 +0000988 return true; // Success!
989}
990
Johnny Chen01a67862011-10-14 00:42:25 +0000991// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end to
992// end operations.
Johnny Chen86364b42011-09-20 23:28:55 +0000993bool
Johnny Chen01a67862011-10-14 00:42:25 +0000994Target::EnableAllWatchpoints (bool end_to_end)
Johnny Chen86364b42011-09-20 23:28:55 +0000995{
Greg Clayton5160ce52013-03-27 23:08:40 +0000996 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +0000997 if (log)
998 log->Printf ("Target::%s\n", __FUNCTION__);
999
Johnny Chenedf50372011-09-23 21:21:43 +00001000 if (!end_to_end) {
Johnny Chen01a67862011-10-14 00:42:25 +00001001 m_watchpoint_list.SetEnabledAll(true);
Johnny Chenedf50372011-09-23 21:21:43 +00001002 return true;
1003 }
1004
1005 // Otherwise, it's an end to end operation.
1006
Johnny Chen86364b42011-09-20 23:28:55 +00001007 if (!ProcessIsValid())
1008 return false;
1009
Johnny Chen01a67862011-10-14 00:42:25 +00001010 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen86364b42011-09-20 23:28:55 +00001011 for (size_t i = 0; i < num_watchpoints; ++i)
1012 {
Johnny Chen01a67862011-10-14 00:42:25 +00001013 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1014 if (!wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001015 return false;
1016
Johnny Chen01a67862011-10-14 00:42:25 +00001017 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chen86364b42011-09-20 23:28:55 +00001018 if (rc.Fail())
1019 return false;
1020 }
Johnny Chen86364b42011-09-20 23:28:55 +00001021 return true; // Success!
1022}
1023
Johnny Chena4d6bc92012-02-25 06:44:30 +00001024// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1025bool
1026Target::ClearAllWatchpointHitCounts ()
1027{
Greg Clayton5160ce52013-03-27 23:08:40 +00001028 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chena4d6bc92012-02-25 06:44:30 +00001029 if (log)
1030 log->Printf ("Target::%s\n", __FUNCTION__);
1031
1032 size_t num_watchpoints = m_watchpoint_list.GetSize();
1033 for (size_t i = 0; i < num_watchpoints; ++i)
1034 {
1035 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1036 if (!wp_sp)
1037 return false;
1038
1039 wp_sp->ResetHitCount();
1040 }
1041 return true; // Success!
1042}
1043
Enrico Granata5e3fe042015-02-11 00:37:54 +00001044// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
1045bool
1046Target::ClearAllWatchpointHistoricValues ()
1047{
1048 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
1049 if (log)
1050 log->Printf ("Target::%s\n", __FUNCTION__);
1051
1052 size_t num_watchpoints = m_watchpoint_list.GetSize();
1053 for (size_t i = 0; i < num_watchpoints; ++i)
1054 {
1055 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1056 if (!wp_sp)
1057 return false;
1058
1059 wp_sp->ResetHistoricValues();
1060 }
1061 return true; // Success!
1062}
1063
Johnny Chen01a67862011-10-14 00:42:25 +00001064// Assumption: Caller holds the list mutex lock for m_watchpoint_list
Johnny Chen6cc60e82011-10-05 21:35:46 +00001065// during these operations.
1066bool
Johnny Chen01a67862011-10-14 00:42:25 +00001067Target::IgnoreAllWatchpoints (uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001068{
Greg Clayton5160ce52013-03-27 23:08:40 +00001069 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001070 if (log)
1071 log->Printf ("Target::%s\n", __FUNCTION__);
1072
1073 if (!ProcessIsValid())
1074 return false;
1075
Johnny Chen01a67862011-10-14 00:42:25 +00001076 size_t num_watchpoints = m_watchpoint_list.GetSize();
Johnny Chen6cc60e82011-10-05 21:35:46 +00001077 for (size_t i = 0; i < num_watchpoints; ++i)
1078 {
Johnny Chen01a67862011-10-14 00:42:25 +00001079 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1080 if (!wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001081 return false;
1082
Johnny Chen01a67862011-10-14 00:42:25 +00001083 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001084 }
1085 return true; // Success!
1086}
1087
Johnny Chen01a67862011-10-14 00:42:25 +00001088// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001089bool
Johnny Chen01a67862011-10-14 00:42:25 +00001090Target::DisableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001091{
Greg Clayton5160ce52013-03-27 23:08:40 +00001092 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001093 if (log)
1094 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1095
1096 if (!ProcessIsValid())
1097 return false;
1098
Johnny Chen01a67862011-10-14 00:42:25 +00001099 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1100 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001101 {
Johnny Chen01a67862011-10-14 00:42:25 +00001102 Error rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001103 if (rc.Success())
1104 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001105
Johnny Chenf04ee932011-09-22 18:04:58 +00001106 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001107 }
1108 return false;
1109}
1110
Johnny Chen01a67862011-10-14 00:42:25 +00001111// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001112bool
Johnny Chen01a67862011-10-14 00:42:25 +00001113Target::EnableWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001114{
Greg Clayton5160ce52013-03-27 23:08:40 +00001115 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001116 if (log)
1117 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1118
1119 if (!ProcessIsValid())
1120 return false;
1121
Johnny Chen01a67862011-10-14 00:42:25 +00001122 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1123 if (wp_sp)
Johnny Chen86364b42011-09-20 23:28:55 +00001124 {
Johnny Chen01a67862011-10-14 00:42:25 +00001125 Error rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Johnny Chenf04ee932011-09-22 18:04:58 +00001126 if (rc.Success())
1127 return true;
Johnny Chen86364b42011-09-20 23:28:55 +00001128
Johnny Chenf04ee932011-09-22 18:04:58 +00001129 // Else, fallthrough.
Johnny Chen86364b42011-09-20 23:28:55 +00001130 }
1131 return false;
1132}
1133
Johnny Chen01a67862011-10-14 00:42:25 +00001134// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen86364b42011-09-20 23:28:55 +00001135bool
Johnny Chen01a67862011-10-14 00:42:25 +00001136Target::RemoveWatchpointByID (lldb::watch_id_t watch_id)
Johnny Chen86364b42011-09-20 23:28:55 +00001137{
Greg Clayton5160ce52013-03-27 23:08:40 +00001138 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen86364b42011-09-20 23:28:55 +00001139 if (log)
1140 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1141
Jim Inghamb0b45132013-07-02 02:09:46 +00001142 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1143 if (watch_to_remove_sp == m_last_created_watchpoint)
1144 m_last_created_watchpoint.reset();
1145
Johnny Chen01a67862011-10-14 00:42:25 +00001146 if (DisableWatchpointByID (watch_id))
Johnny Chen86364b42011-09-20 23:28:55 +00001147 {
Jim Ingham1b5792e2012-12-18 02:03:49 +00001148 m_watchpoint_list.Remove(watch_id, true);
Johnny Chen86364b42011-09-20 23:28:55 +00001149 return true;
1150 }
1151 return false;
1152}
1153
Johnny Chen01a67862011-10-14 00:42:25 +00001154// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Johnny Chen6cc60e82011-10-05 21:35:46 +00001155bool
Johnny Chen01a67862011-10-14 00:42:25 +00001156Target::IgnoreWatchpointByID (lldb::watch_id_t watch_id, uint32_t ignore_count)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001157{
Greg Clayton5160ce52013-03-27 23:08:40 +00001158 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_WATCHPOINTS));
Johnny Chen6cc60e82011-10-05 21:35:46 +00001159 if (log)
1160 log->Printf ("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
1161
1162 if (!ProcessIsValid())
1163 return false;
1164
Johnny Chen01a67862011-10-14 00:42:25 +00001165 WatchpointSP wp_sp = m_watchpoint_list.FindByID (watch_id);
1166 if (wp_sp)
Johnny Chen6cc60e82011-10-05 21:35:46 +00001167 {
Johnny Chen01a67862011-10-14 00:42:25 +00001168 wp_sp->SetIgnoreCount(ignore_count);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001169 return true;
1170 }
1171 return false;
1172}
1173
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174ModuleSP
1175Target::GetExecutableModule ()
1176{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001177 // search for the first executable in the module list
1178 for (size_t i = 0; i < m_images.GetSize(); ++i)
1179 {
1180 ModuleSP module_sp = m_images.GetModuleAtIndex (i);
1181 lldb_private::ObjectFile * obj = module_sp->GetObjectFile();
1182 if (obj == nullptr)
1183 continue;
1184 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1185 return module_sp;
1186 }
1187 // as fall back return the first module loaded
1188 return m_images.GetModuleAtIndex (0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001189}
1190
1191Module*
1192Target::GetExecutableModulePointer ()
1193{
Aidan Doddsc0c83852015-05-08 09:36:31 +00001194 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195}
1196
Enrico Granata17598482012-11-08 02:22:02 +00001197static void
1198LoadScriptingResourceForModule (const ModuleSP &module_sp, Target *target)
1199{
1200 Error error;
Enrico Granata97303392013-05-21 00:00:30 +00001201 StreamString feedback_stream;
1202 if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, &feedback_stream))
Enrico Granata17598482012-11-08 02:22:02 +00001203 {
Enrico Granata97303392013-05-21 00:00:30 +00001204 if (error.AsCString())
Greg Clayton44d93782014-01-27 23:43:24 +00001205 target->GetDebugger().GetErrorFile()->Printf("unable to load scripting data for module %s - error reported was %s\n",
Enrico Granata97303392013-05-21 00:00:30 +00001206 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1207 error.AsCString());
Enrico Granata17598482012-11-08 02:22:02 +00001208 }
Enrico Granatafe7295d2014-08-16 00:32:58 +00001209 if (feedback_stream.GetSize())
1210 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1211 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001212}
1213
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214void
Greg Claytonb35db632013-11-09 00:03:31 +00001215Target::ClearModules(bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216{
Greg Claytonb35db632013-11-09 00:03:31 +00001217 ModulesDidUnload (m_images, delete_locations);
Greg Claytond5944cd2013-12-06 01:12:00 +00001218 m_section_load_history.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001219 m_images.Clear();
Greg Clayton5beec212015-10-08 21:04:34 +00001220 m_scratch_type_system_map.Clear();
Sean Callanana3444ff2015-11-10 22:54:42 +00001221 m_ast_importer_sp.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001222}
1223
1224void
Greg Claytonb35db632013-11-09 00:03:31 +00001225Target::DidExec ()
1226{
1227 // When a process exec's we need to know about it so we can do some cleanup.
1228 m_breakpoint_list.RemoveInvalidLocations(m_arch);
1229 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch);
1230}
1231
1232void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001233Target::SetExecutableModule (ModuleSP& executable_sp, bool get_dependent_files)
1234{
1235 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Greg Claytonb35db632013-11-09 00:03:31 +00001236 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001237
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001238 if (executable_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239 {
1240 Timer scoped_timer (__PRETTY_FUNCTION__,
Greg Claytonb5ad4ec2013-04-29 17:25:54 +00001241 "Target::SetExecutableModule (executable = '%s')",
1242 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001244 m_images.Append(executable_sp); // The first image is our executable file
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245
Jim Ingham5aee1622010-08-09 23:31:02 +00001246 // 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 +00001247 if (!m_arch.IsValid())
Jason Molendae1b68ad2012-12-05 00:25:49 +00001248 {
Greg Clayton32e0a752011-03-30 18:16:51 +00001249 m_arch = executable_sp->GetArchitecture();
Jason Molendae1b68ad2012-12-05 00:25:49 +00001250 if (log)
1251 log->Printf ("Target::SetExecutableModule setting architecture to %s (%s) based on executable file", m_arch.GetArchitectureName(), m_arch.GetTriple().getTriple().c_str());
1252 }
1253
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 FileSpecList dependent_files;
Greg Claytone996fd32011-03-08 22:40:15 +00001255 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256
Greg Claytoncac9c5f2011-09-24 00:52:29 +00001257 if (executable_objfile && get_dependent_files)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001258 {
1259 executable_objfile->GetDependentModules(dependent_files);
1260 for (uint32_t i=0; i<dependent_files.GetSize(); i++)
1261 {
Greg Claytonded470d2011-03-19 01:12:21 +00001262 FileSpec dependent_file_spec (dependent_files.GetFileSpecPointerAtIndex(i));
1263 FileSpec platform_dependent_file_spec;
1264 if (m_platform_sp)
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001265 m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr, platform_dependent_file_spec);
Greg Claytonded470d2011-03-19 01:12:21 +00001266 else
1267 platform_dependent_file_spec = dependent_file_spec;
1268
Greg Claytonb9a01b32012-02-26 05:51:37 +00001269 ModuleSpec module_spec (platform_dependent_file_spec, m_arch);
1270 ModuleSP image_module_sp(GetSharedModule (module_spec));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001271 if (image_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273 ObjectFile *objfile = image_module_sp->GetObjectFile();
1274 if (objfile)
1275 objfile->GetDependentModules(dependent_files);
1276 }
1277 }
1278 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279 }
1280}
1281
Jim Ingham5aee1622010-08-09 23:31:02 +00001282bool
1283Target::SetArchitecture (const ArchSpec &arch_spec)
1284{
Greg Clayton5160ce52013-03-27 23:08:40 +00001285 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001286 bool missing_local_arch = !m_arch.IsValid();
Jason Molendadad8af42015-11-10 04:11:37 +00001287 bool replace_local_arch = true;
1288 bool compatible_local_arch = false;
1289 ArchSpec other(arch_spec);
1290
1291 if (!missing_local_arch)
Jim Ingham5aee1622010-08-09 23:31:02 +00001292 {
Jason Molendadad8af42015-11-10 04:11:37 +00001293 if (m_arch.IsCompatibleMatch(arch_spec))
1294 {
1295 other.MergeFrom(m_arch);
1296
1297 if (m_arch.IsCompatibleMatch(other))
1298 {
1299 compatible_local_arch = true;
1300 bool arch_changed, vendor_changed, os_changed, os_ver_changed, env_changed;
1301
1302 m_arch.PiecewiseTripleCompare(other,
1303 arch_changed,
1304 vendor_changed,
1305 os_changed,
1306 os_ver_changed,
1307 env_changed);
1308
Stephane Sezer03f650f2016-04-05 17:27:52 +00001309 if (!arch_changed && !vendor_changed && !os_changed && !env_changed)
Jason Molendadad8af42015-11-10 04:11:37 +00001310 replace_local_arch = false;
1311 }
1312 }
1313 }
1314
1315 if (compatible_local_arch || missing_local_arch)
1316 {
1317 // If we haven't got a valid arch spec, or the architectures are compatible
1318 // update the architecture, unless the one we already have is more specified
1319 if (replace_local_arch)
1320 m_arch = other;
Jason Molendae1b68ad2012-12-05 00:25:49 +00001321 if (log)
Jason Molendadad8af42015-11-10 04:11:37 +00001322 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 +00001323 return true;
1324 }
Jason Molendadad8af42015-11-10 04:11:37 +00001325
1326 // If we have an executable file, try to reset the executable to the desired architecture
1327 if (log)
1328 log->Printf ("Target::SetArchitecture changing architecture to %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1329 m_arch = other;
1330 ModuleSP executable_sp = GetExecutableModule ();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001331
Jason Molendadad8af42015-11-10 04:11:37 +00001332 ClearModules(true);
1333 // Need to do something about unsetting breakpoints.
1334
1335 if (executable_sp)
1336 {
1337 if (log)
1338 log->Printf("Target::SetArchitecture Trying to select executable file architecture %s (%s)", arch_spec.GetArchitectureName(), arch_spec.GetTriple().getTriple().c_str());
1339 ModuleSpec module_spec (executable_sp->GetFileSpec(), other);
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001340 Error error = ModuleList::GetSharedModule(module_spec,
1341 executable_sp,
1342 &GetExecutableSearchPaths(),
1343 nullptr,
1344 nullptr);
Jason Molendadad8af42015-11-10 04:11:37 +00001345
1346 if (!error.Fail() && executable_sp)
Jim Ingham5aee1622010-08-09 23:31:02 +00001347 {
Jason Molendadad8af42015-11-10 04:11:37 +00001348 SetExecutableModule (executable_sp, true);
1349 return true;
Jim Ingham5aee1622010-08-09 23:31:02 +00001350 }
1351 }
Greg Clayton70512312012-05-08 01:45:38 +00001352 return false;
Jim Ingham5aee1622010-08-09 23:31:02 +00001353}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001354
Tamas Berghammere9f4dfe2015-03-13 10:32:42 +00001355bool
1356Target::MergeArchitecture (const ArchSpec &arch_spec)
1357{
1358 if (arch_spec.IsValid())
1359 {
1360 if (m_arch.IsCompatibleMatch(arch_spec))
1361 {
1362 // The current target arch is compatible with "arch_spec", see if we
1363 // can improve our current architecture using bits from "arch_spec"
1364
1365 // Merge bits from arch_spec into "merged_arch" and set our architecture
1366 ArchSpec merged_arch (m_arch);
1367 merged_arch.MergeFrom (arch_spec);
1368 return SetArchitecture(merged_arch);
1369 }
1370 else
1371 {
1372 // The new architecture is different, we just need to replace it
1373 return SetArchitecture(arch_spec);
1374 }
1375 }
1376 return false;
1377}
1378
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379void
Enrico Granataefe637d2012-11-08 19:16:03 +00001380Target::WillClearList (const ModuleList& module_list)
Enrico Granata17598482012-11-08 02:22:02 +00001381{
1382}
1383
1384void
Enrico Granataefe637d2012-11-08 19:16:03 +00001385Target::ModuleAdded (const ModuleList& module_list, const ModuleSP &module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001386{
1387 // A module is being added to this target for the first time
Greg Clayton23f8c952014-03-24 23:10:19 +00001388 if (m_valid)
1389 {
1390 ModuleList my_module_list;
1391 my_module_list.Append(module_sp);
1392 LoadScriptingResourceForModule(module_sp, this);
1393 ModulesDidLoad (my_module_list);
1394 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001395}
1396
1397void
Enrico Granataefe637d2012-11-08 19:16:03 +00001398Target::ModuleRemoved (const ModuleList& module_list, const ModuleSP &module_sp)
Enrico Granata17598482012-11-08 02:22:02 +00001399{
Adrian McCarthy6ecdbc82015-10-15 22:39:55 +00001400 // A module is being removed from this target.
Greg Clayton23f8c952014-03-24 23:10:19 +00001401 if (m_valid)
1402 {
1403 ModuleList my_module_list;
1404 my_module_list.Append(module_sp);
1405 ModulesDidUnload (my_module_list, false);
1406 }
Enrico Granata17598482012-11-08 02:22:02 +00001407}
1408
1409void
Enrico Granataefe637d2012-11-08 19:16:03 +00001410Target::ModuleUpdated (const ModuleList& module_list, const ModuleSP &old_module_sp, const ModuleSP &new_module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001411{
Jim Inghame716ae02011-08-03 01:00:06 +00001412 // A module is replacing an already added module
Greg Clayton23f8c952014-03-24 23:10:19 +00001413 if (m_valid)
Jason Molendad91c3fd2015-11-10 04:18:12 +00001414 {
Greg Clayton23f8c952014-03-24 23:10:19 +00001415 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001416 m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, new_module_sp);
1417 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001418}
1419
1420void
1421Target::ModulesDidLoad (ModuleList &module_list)
1422{
Greg Clayton23f8c952014-03-24 23:10:19 +00001423 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001424 {
Greg Clayton095eeaa2013-11-05 23:28:00 +00001425 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001426 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendaeef51062013-11-05 03:57:19 +00001427 if (m_process_sp)
1428 {
Andrew MacPhersoneb4d0602014-03-13 09:37:02 +00001429 m_process_sp->ModulesDidLoad (module_list);
Jason Molendaeef51062013-11-05 03:57:19 +00001430 }
Ilia Keb2c19a2015-03-10 21:59:55 +00001431 BroadcastEvent (eBroadcastBitModulesLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001432 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433}
1434
1435void
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001436Target::SymbolsDidLoad (ModuleList &module_list)
1437{
Greg Clayton23f8c952014-03-24 23:10:19 +00001438 if (m_valid && module_list.GetSize())
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001439 {
Jim Ingham31caf982013-06-04 23:01:35 +00001440 if (m_process_sp)
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001441 {
Jim Ingham31caf982013-06-04 23:01:35 +00001442 LanguageRuntime* runtime = m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1443 if (runtime)
1444 {
1445 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime*)runtime;
1446 objc_runtime->SymbolsDidLoad(module_list);
1447 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001448 }
Jim Ingham31caf982013-06-04 23:01:35 +00001449
Greg Clayton095eeaa2013-11-05 23:28:00 +00001450 m_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001451 m_internal_breakpoint_list.UpdateBreakpoints (module_list, true, false);
Ilia Keb2c19a2015-03-10 21:59:55 +00001452 BroadcastEvent (eBroadcastBitSymbolsLoaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001453 }
Enrico Granataf15ee4e2013-04-05 18:49:06 +00001454}
1455
1456void
Greg Clayton095eeaa2013-11-05 23:28:00 +00001457Target::ModulesDidUnload (ModuleList &module_list, bool delete_locations)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458{
Greg Clayton23f8c952014-03-24 23:10:19 +00001459 if (m_valid && module_list.GetSize())
Enrico Granata17598482012-11-08 02:22:02 +00001460 {
Greg Clayton8012cad2014-11-17 19:39:20 +00001461 UnloadModuleSections (module_list);
Greg Clayton095eeaa2013-11-05 23:28:00 +00001462 m_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Jason Molendad91c3fd2015-11-10 04:18:12 +00001463 m_internal_breakpoint_list.UpdateBreakpoints (module_list, false, delete_locations);
Ilia Keb2c19a2015-03-10 21:59:55 +00001464 BroadcastEvent (eBroadcastBitModulesUnloaded, new TargetEventData (this->shared_from_this(), module_list));
Enrico Granata17598482012-11-08 02:22:02 +00001465 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466}
1467
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001468bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001469Target::ModuleIsExcludedForUnconstrainedSearches (const FileSpec &module_file_spec)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001470{
Greg Clayton67cc0632012-08-22 17:17:09 +00001471 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001472 {
1473 ModuleList matchingModules;
Greg Claytonb9a01b32012-02-26 05:51:37 +00001474 ModuleSpec module_spec (module_file_spec);
1475 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001476
1477 // If there is more than one module for this file spec, only return true if ALL the modules are on the
1478 // black list.
1479 if (num_modules > 0)
1480 {
Andy Gibbsa297a972013-06-19 19:04:53 +00001481 for (size_t i = 0; i < num_modules; i++)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001482 {
Jim Ingham33df7cd2014-12-06 01:28:03 +00001483 if (!ModuleIsExcludedForUnconstrainedSearches (matchingModules.GetModuleAtIndex(i)))
Jim Inghamc6674fd2011-10-28 23:14:11 +00001484 return false;
1485 }
1486 return true;
1487 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001488 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001489 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001490}
1491
Daniel Dunbarf9f70322011-10-31 22:50:37 +00001492bool
Jim Ingham33df7cd2014-12-06 01:28:03 +00001493Target::ModuleIsExcludedForUnconstrainedSearches (const lldb::ModuleSP &module_sp)
Jim Inghamc6674fd2011-10-28 23:14:11 +00001494{
Greg Clayton67cc0632012-08-22 17:17:09 +00001495 if (GetBreakpointsConsultPlatformAvoidList())
Jim Inghamc6674fd2011-10-28 23:14:11 +00001496 {
Greg Clayton67cc0632012-08-22 17:17:09 +00001497 if (m_platform_sp)
Jim Ingham33df7cd2014-12-06 01:28:03 +00001498 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches (*this, module_sp);
Jim Inghamc6674fd2011-10-28 23:14:11 +00001499 }
Greg Clayton67cc0632012-08-22 17:17:09 +00001500 return false;
Jim Inghamc6674fd2011-10-28 23:14:11 +00001501}
1502
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001503size_t
Greg Claytondb598232011-01-07 01:57:07 +00001504Target::ReadMemoryFromFileCache (const Address& addr, void *dst, size_t dst_len, Error &error)
1505{
Greg Claytone72dfb32012-02-24 01:59:29 +00001506 SectionSP section_sp (addr.GetSection());
1507 if (section_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001508 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001509 // 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 +00001510 if (section_sp->IsEncrypted())
1511 {
Greg Clayton57f06302012-05-25 17:05:55 +00001512 error.SetErrorString("section is encrypted");
Jason Molenda216d91f2012-04-25 00:06:56 +00001513 return 0;
1514 }
Greg Claytone72dfb32012-02-24 01:59:29 +00001515 ModuleSP module_sp (section_sp->GetModule());
1516 if (module_sp)
Greg Claytondb598232011-01-07 01:57:07 +00001517 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001518 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1519 if (objfile)
1520 {
1521 size_t bytes_read = objfile->ReadSectionData (section_sp.get(),
1522 addr.GetOffset(),
1523 dst,
1524 dst_len);
1525 if (bytes_read > 0)
1526 return bytes_read;
1527 else
1528 error.SetErrorStringWithFormat("error reading data from section %s", section_sp->GetName().GetCString());
1529 }
Greg Claytondb598232011-01-07 01:57:07 +00001530 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001531 error.SetErrorString("address isn't from a object file");
Greg Claytondb598232011-01-07 01:57:07 +00001532 }
1533 else
Greg Claytone72dfb32012-02-24 01:59:29 +00001534 error.SetErrorString("address isn't in a module");
Greg Claytondb598232011-01-07 01:57:07 +00001535 }
1536 else
Greg Claytondb598232011-01-07 01:57:07 +00001537 error.SetErrorString("address doesn't contain a section that points to a section in a object file");
Greg Claytone72dfb32012-02-24 01:59:29 +00001538
Greg Claytondb598232011-01-07 01:57:07 +00001539 return 0;
1540}
1541
1542size_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001543Target::ReadMemory (const Address& addr,
1544 bool prefer_file_cache,
1545 void *dst,
1546 size_t dst_len,
1547 Error &error,
1548 lldb::addr_t *load_addr_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001549{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001550 error.Clear();
Greg Claytondb598232011-01-07 01:57:07 +00001551
Enrico Granata9128ee22011-09-06 19:20:51 +00001552 // if we end up reading this from process memory, we will fill this
1553 // with the actual load address
1554 if (load_addr_ptr)
1555 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1556
Greg Claytondb598232011-01-07 01:57:07 +00001557 size_t bytes_read = 0;
Greg Claytonc749eb82011-07-11 05:12:02 +00001558
1559 addr_t load_addr = LLDB_INVALID_ADDRESS;
1560 addr_t file_addr = LLDB_INVALID_ADDRESS;
Greg Clayton357132e2011-03-26 19:14:58 +00001561 Address resolved_addr;
1562 if (!addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001563 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001564 SectionLoadList &section_load_list = GetSectionLoadList();
1565 if (section_load_list.IsEmpty())
Greg Claytonc749eb82011-07-11 05:12:02 +00001566 {
Greg Claytond16e1e52011-07-12 17:06:17 +00001567 // No sections are loaded, so we must assume we are not running
1568 // yet and anything we are given is a file address.
1569 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the file address
1570 m_images.ResolveFileAddress (file_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001571 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001572 else
Greg Claytonc749eb82011-07-11 05:12:02 +00001573 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001574 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001575 // we have manually loaded some sections with "target modules load ..."
1576 // or because we have have a live process that has sections loaded
1577 // through the dynamic loader
1578 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its offset is the load address
Greg Claytond5944cd2013-12-06 01:12:00 +00001579 section_load_list.ResolveLoadAddress (load_addr, resolved_addr);
Greg Claytonc749eb82011-07-11 05:12:02 +00001580 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001581 }
Greg Clayton357132e2011-03-26 19:14:58 +00001582 if (!resolved_addr.IsValid())
1583 resolved_addr = addr;
Greg Claytonc749eb82011-07-11 05:12:02 +00001584
Greg Claytondb598232011-01-07 01:57:07 +00001585 if (prefer_file_cache)
1586 {
1587 bytes_read = ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
1588 if (bytes_read > 0)
1589 return bytes_read;
1590 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001591
Johnny Chen86364b42011-09-20 23:28:55 +00001592 if (ProcessIsValid())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001593 {
Greg Claytonc749eb82011-07-11 05:12:02 +00001594 if (load_addr == LLDB_INVALID_ADDRESS)
1595 load_addr = resolved_addr.GetLoadAddress (this);
1596
Greg Claytondda4f7b2010-06-30 23:03:03 +00001597 if (load_addr == LLDB_INVALID_ADDRESS)
1598 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001599 ModuleSP addr_module_sp (resolved_addr.GetModule());
1600 if (addr_module_sp && addr_module_sp->GetFileSpec())
Daniel Malead01b2952012-11-29 21:49:15 +00001601 error.SetErrorStringWithFormat("%s[0x%" PRIx64 "] can't be resolved, %s in not currently loaded",
Jim Ingham4af59612014-12-19 19:20:44 +00001602 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknown>"),
Jason Molenda7e589a62011-09-20 00:26:08 +00001603 resolved_addr.GetFileAddress(),
Jim Ingham4af59612014-12-19 19:20:44 +00001604 addr_module_sp->GetFileSpec().GetFilename().AsCString("<Unknonw>"));
Greg Claytondda4f7b2010-06-30 23:03:03 +00001605 else
Daniel Malead01b2952012-11-29 21:49:15 +00001606 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved", resolved_addr.GetFileAddress());
Greg Claytondda4f7b2010-06-30 23:03:03 +00001607 }
1608 else
1609 {
Greg Claytondb598232011-01-07 01:57:07 +00001610 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001611 if (bytes_read != dst_len)
1612 {
1613 if (error.Success())
1614 {
1615 if (bytes_read == 0)
Daniel Malead01b2952012-11-29 21:49:15 +00001616 error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", load_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001617 else
Daniel Malead01b2952012-11-29 21:49:15 +00001618 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 +00001619 }
1620 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001621 if (bytes_read)
Enrico Granata9128ee22011-09-06 19:20:51 +00001622 {
1623 if (load_addr_ptr)
1624 *load_addr_ptr = load_addr;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001625 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001626 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001627 // If the address is not section offset we have an address that
1628 // doesn't resolve to any address in any currently loaded shared
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001629 // libraries and we failed to read memory so there isn't anything
Greg Claytondda4f7b2010-06-30 23:03:03 +00001630 // more we can do. If it is section offset, we might be able to
1631 // read cached memory from the object file.
1632 if (!resolved_addr.IsSectionOffset())
1633 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001635 }
Greg Claytondda4f7b2010-06-30 23:03:03 +00001636
Greg Claytonc749eb82011-07-11 05:12:02 +00001637 if (!prefer_file_cache && resolved_addr.IsSectionOffset())
Greg Claytondda4f7b2010-06-30 23:03:03 +00001638 {
Greg Claytondb598232011-01-07 01:57:07 +00001639 // If we didn't already try and read from the object file cache, then
1640 // try it after failing to read from the process.
1641 return ReadMemoryFromFileCache (resolved_addr, dst, dst_len, error);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001642 }
1643 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644}
1645
Greg Claytond16e1e52011-07-12 17:06:17 +00001646size_t
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001647Target::ReadCStringFromMemory (const Address& addr, std::string &out_str, Error &error)
1648{
1649 char buf[256];
1650 out_str.clear();
1651 addr_t curr_addr = addr.GetLoadAddress(this);
1652 Address address(addr);
1653 while (1)
1654 {
1655 size_t length = ReadCStringFromMemory (address, buf, sizeof(buf), error);
1656 if (length == 0)
1657 break;
1658 out_str.append(buf, length);
1659 // If we got "length - 1" bytes, we didn't get the whole C string, we
1660 // need to read some more characters
1661 if (length == sizeof(buf) - 1)
1662 curr_addr += length;
1663 else
1664 break;
1665 address = Address(curr_addr);
1666 }
1667 return out_str.size();
1668}
1669
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001670size_t
1671Target::ReadCStringFromMemory (const Address& addr, char *dst, size_t dst_max_len, Error &result_error)
1672{
1673 size_t total_cstr_len = 0;
1674 if (dst && dst_max_len)
1675 {
1676 result_error.Clear();
1677 // NULL out everything just to be safe
1678 memset (dst, 0, dst_max_len);
1679 Error error;
1680 addr_t curr_addr = addr.GetLoadAddress(this);
1681 Address address(addr);
Jason Molendaf0340c92014-09-03 22:30:54 +00001682
1683 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't
1684 // think this really needs to be tied to the memory cache subsystem's
1685 // cache line size, so leave this as a fixed constant.
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001686 const size_t cache_line_size = 512;
Jason Molendaf0340c92014-09-03 22:30:54 +00001687
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001688 size_t bytes_left = dst_max_len - 1;
1689 char *curr_dst = dst;
1690
1691 while (bytes_left > 0)
1692 {
1693 addr_t cache_line_bytes_left = cache_line_size - (curr_addr % cache_line_size);
1694 addr_t bytes_to_read = std::min<addr_t>(bytes_left, cache_line_bytes_left);
1695 size_t bytes_read = ReadMemory (address, false, curr_dst, bytes_to_read, error);
1696
1697 if (bytes_read == 0)
1698 {
1699 result_error = error;
1700 dst[total_cstr_len] = '\0';
1701 break;
1702 }
1703 const size_t len = strlen(curr_dst);
1704
1705 total_cstr_len += len;
1706
1707 if (len < bytes_to_read)
1708 break;
1709
1710 curr_dst += bytes_read;
1711 curr_addr += bytes_read;
1712 bytes_left -= bytes_read;
1713 address = Address(curr_addr);
1714 }
1715 }
1716 else
1717 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00001718 if (dst == nullptr)
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001719 result_error.SetErrorString("invalid arguments");
1720 else
1721 result_error.Clear();
1722 }
1723 return total_cstr_len;
1724}
1725
1726size_t
Greg Claytond16e1e52011-07-12 17:06:17 +00001727Target::ReadScalarIntegerFromMemory (const Address& addr,
1728 bool prefer_file_cache,
1729 uint32_t byte_size,
1730 bool is_signed,
1731 Scalar &scalar,
1732 Error &error)
1733{
1734 uint64_t uval;
1735
1736 if (byte_size <= sizeof(uval))
1737 {
1738 size_t bytes_read = ReadMemory (addr, prefer_file_cache, &uval, byte_size, error);
1739 if (bytes_read == byte_size)
1740 {
1741 DataExtractor data (&uval, sizeof(uval), m_arch.GetByteOrder(), m_arch.GetAddressByteSize());
Greg Claytonc7bece562013-01-25 18:06:21 +00001742 lldb::offset_t offset = 0;
Greg Claytond16e1e52011-07-12 17:06:17 +00001743 if (byte_size <= 4)
1744 scalar = data.GetMaxU32 (&offset, byte_size);
1745 else
1746 scalar = data.GetMaxU64 (&offset, byte_size);
1747
1748 if (is_signed)
1749 scalar.SignExtend(byte_size * 8);
1750 return bytes_read;
1751 }
1752 }
1753 else
1754 {
1755 error.SetErrorStringWithFormat ("byte size of %u is too large for integer scalar type", byte_size);
1756 }
1757 return 0;
1758}
1759
1760uint64_t
1761Target::ReadUnsignedIntegerFromMemory (const Address& addr,
1762 bool prefer_file_cache,
1763 size_t integer_byte_size,
1764 uint64_t fail_value,
1765 Error &error)
1766{
1767 Scalar scalar;
1768 if (ReadScalarIntegerFromMemory (addr,
1769 prefer_file_cache,
1770 integer_byte_size,
1771 false,
1772 scalar,
1773 error))
1774 return scalar.ULongLong(fail_value);
1775 return fail_value;
1776}
1777
1778bool
1779Target::ReadPointerFromMemory (const Address& addr,
1780 bool prefer_file_cache,
1781 Error &error,
1782 Address &pointer_addr)
1783{
1784 Scalar scalar;
1785 if (ReadScalarIntegerFromMemory (addr,
1786 prefer_file_cache,
1787 m_arch.GetAddressByteSize(),
1788 false,
1789 scalar,
1790 error))
1791 {
1792 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1793 if (pointer_vm_addr != LLDB_INVALID_ADDRESS)
1794 {
Greg Claytond5944cd2013-12-06 01:12:00 +00001795 SectionLoadList &section_load_list = GetSectionLoadList();
1796 if (section_load_list.IsEmpty())
Greg Claytond16e1e52011-07-12 17:06:17 +00001797 {
1798 // No sections are loaded, so we must assume we are not running
1799 // yet and anything we are given is a file address.
1800 m_images.ResolveFileAddress (pointer_vm_addr, pointer_addr);
1801 }
1802 else
1803 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00001804 // We have at least one section loaded. This can be because
Greg Claytond16e1e52011-07-12 17:06:17 +00001805 // we have manually loaded some sections with "target modules load ..."
1806 // or because we have have a live process that has sections loaded
1807 // through the dynamic loader
Greg Claytond5944cd2013-12-06 01:12:00 +00001808 section_load_list.ResolveLoadAddress (pointer_vm_addr, pointer_addr);
Greg Claytond16e1e52011-07-12 17:06:17 +00001809 }
1810 // We weren't able to resolve the pointer value, so just return
1811 // an address with no section
1812 if (!pointer_addr.IsValid())
1813 pointer_addr.SetOffset (pointer_vm_addr);
1814 return true;
1815
1816 }
1817 }
1818 return false;
1819}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001820
1821ModuleSP
Greg Claytonb9a01b32012-02-26 05:51:37 +00001822Target::GetSharedModule (const ModuleSpec &module_spec, Error *error_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001823{
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001824 ModuleSP module_sp;
1825
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001826 Error error;
1827
Jim Ingham4a94c912012-05-17 18:38:42 +00001828 // First see if we already have this module in our module list. If we do, then we're done, we don't need
1829 // to consult the shared modules list. But only do this if we are passed a UUID.
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001830
Jim Ingham4a94c912012-05-17 18:38:42 +00001831 if (module_spec.GetUUID().IsValid())
1832 module_sp = m_images.FindFirstModule(module_spec);
1833
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001834 if (!module_sp)
1835 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001836 ModuleSP old_module_sp; // This will get filled in if we have a new version of the library
1837 bool did_create_module = false;
1838
1839 // If there are image search path entries, try to use them first to acquire a suitable image.
1840 if (m_image_search_paths.GetSize())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001841 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001842 ModuleSpec transformed_spec (module_spec);
1843 if (m_image_search_paths.RemapPath (module_spec.GetFileSpec().GetDirectory(), transformed_spec.GetFileSpec().GetDirectory()))
1844 {
1845 transformed_spec.GetFileSpec().GetFilename() = module_spec.GetFileSpec().GetFilename();
1846 error = ModuleList::GetSharedModule (transformed_spec,
1847 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001848 &GetExecutableSearchPaths(),
Jim Ingham4a94c912012-05-17 18:38:42 +00001849 &old_module_sp,
1850 &did_create_module);
1851 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001852 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001853
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001854 if (!module_sp)
1855 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001856 // If we have a UUID, we can check our global shared module list in case
1857 // we already have it. If we don't have a valid UUID, then we can't since
1858 // the path in "module_spec" will be a platform path, and we will need to
1859 // let the platform find that file. For example, we could be asking for
1860 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
1861 // the local copy of "/usr/lib/dyld" since our platform could be a remote
1862 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
1863 // cache.
1864 if (module_spec.GetUUID().IsValid())
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001865 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001866 // We have a UUID, it is OK to check the global module list...
1867 error = ModuleList::GetSharedModule (module_spec,
1868 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001869 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001870 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001871 &did_create_module);
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001872 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001873
1874 if (!module_sp)
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001875 {
Jim Ingham4a94c912012-05-17 18:38:42 +00001876 // The platform is responsible for finding and caching an appropriate
1877 // module in the shared module cache.
1878 if (m_platform_sp)
1879 {
Tamas Berghammer7cb18bf2015-03-24 11:15:23 +00001880 error = m_platform_sp->GetSharedModule (module_spec,
1881 m_process_sp.get(),
1882 module_sp,
Greg Clayton6920b522012-08-22 18:39:03 +00001883 &GetExecutableSearchPaths(),
Greg Clayton67cc0632012-08-22 17:17:09 +00001884 &old_module_sp,
Jim Ingham4a94c912012-05-17 18:38:42 +00001885 &did_create_module);
1886 }
1887 else
1888 {
1889 error.SetErrorString("no platform is currently set");
1890 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00001891 }
1892 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001893
Jim Ingham4a94c912012-05-17 18:38:42 +00001894 // We found a module that wasn't in our target list. Let's make sure that there wasn't an equivalent
1895 // module in the list already, and if there was, let's remove it.
1896 if (module_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001898 ObjectFile *objfile = module_sp->GetObjectFile();
1899 if (objfile)
Jim Ingham4a94c912012-05-17 18:38:42 +00001900 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001901 switch (objfile->GetType())
Jim Ingham4a94c912012-05-17 18:38:42 +00001902 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001903 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of a program's execution state
1904 case ObjectFile::eTypeExecutable: /// A normal executable
1905 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker executable
1906 case ObjectFile::eTypeObjectFile: /// An intermediate object file
1907 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be used during execution
1908 break;
1909 case ObjectFile::eTypeDebugInfo: /// An object file that contains only debug information
1910 if (error_ptr)
1911 error_ptr->SetErrorString("debug info files aren't valid target modules, please specify an executable");
1912 return ModuleSP();
1913 case ObjectFile::eTypeStubLibrary: /// A library that can be linked against but not used for execution
1914 if (error_ptr)
1915 error_ptr->SetErrorString("stub libraries aren't valid target modules, please specify an executable");
1916 return ModuleSP();
1917 default:
1918 if (error_ptr)
1919 error_ptr->SetErrorString("unsupported file type, please specify an executable");
1920 return ModuleSP();
1921 }
1922 // GetSharedModule is not guaranteed to find the old shared module, for instance
1923 // in the common case where you pass in the UUID, it is only going to find the one
1924 // module matching the UUID. In fact, it has no good way to know what the "old module"
1925 // relevant to this target is, since there might be many copies of a module with this file spec
1926 // in various running debug sessions, but only one of them will belong to this target.
1927 // So let's remove the UUID from the module list, and look in the target's module list.
1928 // Only do this if there is SOMETHING else in the module spec...
1929 if (!old_module_sp)
1930 {
1931 if (module_spec.GetUUID().IsValid() && !module_spec.GetFileSpec().GetFilename().IsEmpty() && !module_spec.GetFileSpec().GetDirectory().IsEmpty())
Jim Ingham4a94c912012-05-17 18:38:42 +00001932 {
Greg Clayton50a24bd2012-11-29 22:16:27 +00001933 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
1934 module_spec_copy.GetUUID().Clear();
1935
1936 ModuleList found_modules;
1937 size_t num_found = m_images.FindModules (module_spec_copy, found_modules);
1938 if (num_found == 1)
1939 {
1940 old_module_sp = found_modules.GetModuleAtIndex(0);
1941 }
Jim Ingham4a94c912012-05-17 18:38:42 +00001942 }
1943 }
Greg Clayton50a24bd2012-11-29 22:16:27 +00001944
1945 if (old_module_sp && m_images.GetIndexForModule (old_module_sp.get()) != LLDB_INVALID_INDEX32)
1946 {
1947 m_images.ReplaceModule(old_module_sp, module_sp);
1948 Module *old_module_ptr = old_module_sp.get();
1949 old_module_sp.reset();
1950 ModuleList::RemoveSharedModuleIfOrphaned (old_module_ptr);
1951 }
1952 else
1953 m_images.Append(module_sp);
Jim Ingham4a94c912012-05-17 18:38:42 +00001954 }
Greg Clayton86e70cb2014-04-07 23:50:17 +00001955 else
1956 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001957 }
1958 }
1959 if (error_ptr)
1960 *error_ptr = error;
1961 return module_sp;
1962}
1963
Greg Claytond9e416c2012-02-18 05:35:26 +00001964TargetSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965Target::CalculateTarget ()
1966{
Greg Claytond9e416c2012-02-18 05:35:26 +00001967 return shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001968}
1969
Greg Claytond9e416c2012-02-18 05:35:26 +00001970ProcessSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001971Target::CalculateProcess ()
1972{
Sean Callanan92734d12016-02-12 19:45:31 +00001973 return m_process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001974}
1975
Greg Claytond9e416c2012-02-18 05:35:26 +00001976ThreadSP
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001977Target::CalculateThread ()
1978{
Greg Claytond9e416c2012-02-18 05:35:26 +00001979 return ThreadSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001980}
1981
Jason Molendab57e4a12013-11-04 09:33:30 +00001982StackFrameSP
1983Target::CalculateStackFrame ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001984{
Jason Molendab57e4a12013-11-04 09:33:30 +00001985 return StackFrameSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001986}
1987
1988void
Greg Clayton0603aa92010-10-04 01:05:56 +00001989Target::CalculateExecutionContext (ExecutionContext &exe_ctx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990{
Greg Claytonc14ee322011-09-22 04:58:26 +00001991 exe_ctx.Clear();
1992 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993}
1994
1995PathMappingList &
1996Target::GetImageSearchPathList ()
1997{
1998 return m_image_search_paths;
1999}
2000
2001void
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002002Target::ImageSearchPathsChanged(const PathMappingList &path_list,
2003 void *baton)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002004{
2005 Target *target = (Target *)baton;
Greg Claytonaa149cb2011-08-11 02:48:45 +00002006 ModuleSP exe_module_sp (target->GetExecutableModule());
2007 if (exe_module_sp)
Greg Claytonaa149cb2011-08-11 02:48:45 +00002008 target->SetExecutableModule (exe_module_sp, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002009}
2010
Jim Ingham151c0322015-09-15 21:13:50 +00002011TypeSystem *
Sean Callanana994b0b2015-10-02 18:40:30 +00002012Target::GetScratchTypeSystemForLanguage (Error *error, lldb::LanguageType language, bool create_on_demand)
Jim Ingham151c0322015-09-15 21:13:50 +00002013{
Greg Clayton5beec212015-10-08 21:04:34 +00002014 if (!m_valid)
2015 return nullptr;
2016
Sean Callanana994b0b2015-10-02 18:40:30 +00002017 if (error)
2018 {
2019 error->Clear();
2020 }
2021
Sean Callananb92bd752015-10-01 16:28:02 +00002022 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all assembly code
Sean Callanana994b0b2015-10-02 18:40:30 +00002023 || language == eLanguageTypeUnknown)
2024 {
Sean Callananc7b054a2015-10-09 18:01:10 +00002025 std::set<lldb::LanguageType> languages_for_types;
2026 std::set<lldb::LanguageType> languages_for_expressions;
2027
2028 Language::GetLanguagesSupportingTypeSystems(languages_for_types, languages_for_expressions);
2029
2030 if (languages_for_expressions.count(eLanguageTypeC))
2031 {
2032 language = eLanguageTypeC; // LLDB's default. Override by setting the target language.
2033 }
2034 else
2035 {
2036 if (languages_for_expressions.empty())
2037 {
2038 return nullptr;
2039 }
2040 else
2041 {
2042 language = *languages_for_expressions.begin();
2043 }
2044 }
Sean Callananb92bd752015-10-01 16:28:02 +00002045 }
Sean Callananb92bd752015-10-01 16:28:02 +00002046
Greg Clayton5beec212015-10-08 21:04:34 +00002047 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this, create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00002048}
2049
2050PersistentExpressionState *
2051Target::GetPersistentExpressionStateForLanguage (lldb::LanguageType language)
2052{
Sean Callanana994b0b2015-10-02 18:40:30 +00002053 TypeSystem *type_system = GetScratchTypeSystemForLanguage(nullptr, language, true);
Sean Callananb92bd752015-10-01 16:28:02 +00002054
2055 if (type_system)
2056 {
2057 return type_system->GetPersistentExpressionState();
2058 }
Jim Ingham151c0322015-09-15 21:13:50 +00002059 else
Sean Callananb92bd752015-10-01 16:28:02 +00002060 {
2061 return nullptr;
2062 }
Jim Ingham151c0322015-09-15 21:13:50 +00002063}
2064
2065UserExpression *
2066Target::GetUserExpressionForLanguage(const char *expr,
Sean Callanana994b0b2015-10-02 18:40:30 +00002067 const char *expr_prefix,
2068 lldb::LanguageType language,
2069 Expression::ResultType desired_type,
Jim Ingham19a63fc2015-11-03 02:11:24 +00002070 const EvaluateExpressionOptions &options,
Sean Callanana994b0b2015-10-02 18:40:30 +00002071 Error &error)
Jim Ingham151c0322015-09-15 21:13:50 +00002072{
Sean Callanana994b0b2015-10-02 18:40:30 +00002073 Error type_system_error;
2074
2075 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002076 UserExpression *user_expr = nullptr;
2077
2078 if (!type_system)
2079 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002080 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 +00002081 return nullptr;
2082 }
2083
Jim Ingham19a63fc2015-11-03 02:11:24 +00002084 user_expr = type_system->GetUserExpression(expr, expr_prefix, language, desired_type, options);
Jim Ingham151c0322015-09-15 21:13:50 +00002085 if (!user_expr)
2086 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2087
2088 return user_expr;
2089}
2090
2091FunctionCaller *
2092Target::GetFunctionCallerForLanguage (lldb::LanguageType language,
2093 const CompilerType &return_type,
2094 const Address& function_address,
2095 const ValueList &arg_value_list,
2096 const char *name,
2097 Error &error)
2098{
Sean Callanana994b0b2015-10-02 18:40:30 +00002099 Error type_system_error;
2100 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002101 FunctionCaller *persistent_fn = nullptr;
2102
2103 if (!type_system)
2104 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002105 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 +00002106 return persistent_fn;
2107 }
2108
2109 persistent_fn = type_system->GetFunctionCaller (return_type, function_address, arg_value_list, name);
2110 if (!persistent_fn)
2111 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2112
2113 return persistent_fn;
2114}
2115
2116UtilityFunction *
2117Target::GetUtilityFunctionForLanguage (const char *text,
2118 lldb::LanguageType language,
2119 const char *name,
2120 Error &error)
2121{
Sean Callanana994b0b2015-10-02 18:40:30 +00002122 Error type_system_error;
2123 TypeSystem *type_system = GetScratchTypeSystemForLanguage (&type_system_error, language);
Jim Ingham151c0322015-09-15 21:13:50 +00002124 UtilityFunction *utility_fn = nullptr;
2125
2126 if (!type_system)
2127 {
Sean Callanana994b0b2015-10-02 18:40:30 +00002128 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 +00002129 return utility_fn;
2130 }
2131
2132 utility_fn = type_system->GetUtilityFunction (text, name);
2133 if (!utility_fn)
2134 error.SetErrorStringWithFormat("Could not create an expression for language %s", Language::GetNameForLanguageType(language));
2135
2136 return utility_fn;
2137}
2138
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002139ClangASTContext *
Johnny Chen60e2c6a2011-11-30 23:18:53 +00002140Target::GetScratchClangASTContext(bool create_on_demand)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002141{
Greg Clayton5beec212015-10-08 21:04:34 +00002142 if (m_valid)
Sean Callanan4bf80d52011-11-15 22:27:19 +00002143 {
Greg Clayton5beec212015-10-08 21:04:34 +00002144 if (TypeSystem* type_system = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC, create_on_demand))
2145 return llvm::dyn_cast<ClangASTContext>(type_system);
Sean Callanan4bf80d52011-11-15 22:27:19 +00002146 }
Greg Clayton5beec212015-10-08 21:04:34 +00002147 return nullptr;
Sean Callananb92bd752015-10-01 16:28:02 +00002148}
2149
Sean Callanana3444ff2015-11-10 22:54:42 +00002150ClangASTImporterSP
Sean Callanan686b2312011-11-16 18:20:47 +00002151Target::GetClangASTImporter()
2152{
Greg Clayton5beec212015-10-08 21:04:34 +00002153 if (m_valid)
Sean Callanan686b2312011-11-16 18:20:47 +00002154 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002155 if (!m_ast_importer_sp)
Greg Clayton5beec212015-10-08 21:04:34 +00002156 {
Sean Callanana3444ff2015-11-10 22:54:42 +00002157 m_ast_importer_sp.reset(new ClangASTImporter());
Greg Clayton5beec212015-10-08 21:04:34 +00002158 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002159 return m_ast_importer_sp;
Sean Callanan686b2312011-11-16 18:20:47 +00002160 }
Sean Callanana3444ff2015-11-10 22:54:42 +00002161 return ClangASTImporterSP();
Sean Callanan686b2312011-11-16 18:20:47 +00002162}
2163
Greg Clayton99d0faf2010-11-18 23:32:35 +00002164void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002165Target::SettingsInitialize ()
Caroline Ticedaccaa92010-09-20 20:44:43 +00002166{
Greg Clayton6920b522012-08-22 18:39:03 +00002167 Process::SettingsInitialize ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002168}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002169
Greg Clayton99d0faf2010-11-18 23:32:35 +00002170void
Caroline Tice20bd37f2011-03-10 22:14:10 +00002171Target::SettingsTerminate ()
Greg Clayton99d0faf2010-11-18 23:32:35 +00002172{
Greg Clayton6920b522012-08-22 18:39:03 +00002173 Process::SettingsTerminate ();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002174}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002175
Greg Claytonc859e2d2012-02-13 23:10:39 +00002176FileSpecList
2177Target::GetDefaultExecutableSearchPaths ()
2178{
Greg Clayton67cc0632012-08-22 17:17:09 +00002179 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2180 if (properties_sp)
2181 return properties_sp->GetExecutableSearchPaths();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002182 return FileSpecList();
2183}
2184
Michael Sartaina7499c92013-07-01 19:45:50 +00002185FileSpecList
2186Target::GetDefaultDebugFileSearchPaths ()
2187{
2188 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2189 if (properties_sp)
2190 return properties_sp->GetDebugFileSearchPaths();
2191 return FileSpecList();
2192}
2193
Sean Callanan85054342015-04-03 15:39:47 +00002194FileSpecList
2195Target::GetDefaultClangModuleSearchPaths ()
2196{
2197 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2198 if (properties_sp)
2199 return properties_sp->GetClangModuleSearchPaths();
2200 return FileSpecList();
2201}
2202
Caroline Ticedaccaa92010-09-20 20:44:43 +00002203ArchSpec
2204Target::GetDefaultArchitecture ()
2205{
Greg Clayton67cc0632012-08-22 17:17:09 +00002206 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2207 if (properties_sp)
2208 return properties_sp->GetDefaultArchitecture();
Greg Clayton8910c902012-05-15 02:44:13 +00002209 return ArchSpec();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002210}
2211
2212void
Greg Clayton67cc0632012-08-22 17:17:09 +00002213Target::SetDefaultArchitecture (const ArchSpec &arch)
Caroline Ticedaccaa92010-09-20 20:44:43 +00002214{
Greg Clayton67cc0632012-08-22 17:17:09 +00002215 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2216 if (properties_sp)
Jason Molendaa3f24b32012-12-12 02:23:56 +00002217 {
2218 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 +00002219 return properties_sp->SetDefaultArchitecture(arch);
Jason Molendaa3f24b32012-12-12 02:23:56 +00002220 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00002221}
2222
Greg Clayton0603aa92010-10-04 01:05:56 +00002223Target *
2224Target::GetTargetFromContexts (const ExecutionContext *exe_ctx_ptr, const SymbolContext *sc_ptr)
2225{
2226 // The target can either exist in the "process" of ExecutionContext, or in
2227 // the "target_sp" member of SymbolContext. This accessor helper function
2228 // will get the target from one of these locations.
2229
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002230 Target *target = nullptr;
2231 if (sc_ptr != nullptr)
Greg Clayton0603aa92010-10-04 01:05:56 +00002232 target = sc_ptr->target_sp.get();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002233 if (target == nullptr && exe_ctx_ptr)
Greg Claytonc14ee322011-09-22 04:58:26 +00002234 target = exe_ctx_ptr->GetTargetPtr();
Greg Clayton0603aa92010-10-04 01:05:56 +00002235 return target;
2236}
2237
Jim Ingham1624a2d2014-05-05 02:26:40 +00002238ExpressionResults
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002239Target::EvaluateExpression(const char *expr_cstr,
Jim Inghama309efe2015-10-28 22:23:17 +00002240 ExecutionContextScope *exe_scope,
Eugene Zelenko8f30a652015-10-23 18:39:37 +00002241 lldb::ValueObjectSP &result_valobj_sp,
Jim Inghame5ee6f02016-03-29 22:00:08 +00002242 const EvaluateExpressionOptions& options,
2243 std::string *fixed_expression)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002244{
Enrico Granata97fca502012-09-18 17:43:16 +00002245 result_valobj_sp.reset();
2246
Jim Ingham8646d3c2014-05-05 02:47:44 +00002247 ExpressionResults execution_results = eExpressionSetupError;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002248
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002249 if (expr_cstr == nullptr || expr_cstr[0] == '\0')
Greg Claytond1767f02011-12-08 02:13:16 +00002250 return execution_results;
2251
Jim Ingham6026ca32011-05-12 02:06:14 +00002252 // We shouldn't run stop hooks in expressions.
2253 // Be sure to reset this if you return anywhere within this function.
2254 bool old_suppress_value = m_suppress_stop_hooks;
2255 m_suppress_stop_hooks = true;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002256
2257 ExecutionContext exe_ctx;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002258
Jim Inghama309efe2015-10-28 22:23:17 +00002259 if (exe_scope)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002260 {
Jim Inghama309efe2015-10-28 22:23:17 +00002261 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002262 }
2263 else if (m_process_sp)
2264 {
2265 m_process_sp->CalculateExecutionContext(exe_ctx);
2266 }
2267 else
2268 {
2269 CalculateExecutionContext(exe_ctx);
2270 }
2271
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002272 // Make sure we aren't just trying to see the value of a persistent
2273 // variable (something like "$0")
Sean Callananbc8ac342015-09-04 20:49:51 +00002274 lldb::ExpressionVariableSP persistent_var_sp;
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002275 // Only check for persistent variables the expression starts with a '$'
2276 if (expr_cstr[0] == '$')
Sean Callanana994b0b2015-10-02 18:40:30 +00002277 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)->GetPersistentExpressionState()->GetVariable (expr_cstr);
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002278
2279 if (persistent_var_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002280 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002281 result_valobj_sp = persistent_var_sp->GetValueObject ();
Jim Ingham8646d3c2014-05-05 02:47:44 +00002282 execution_results = eExpressionCompleted;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002283 }
2284 else
2285 {
Sean Callanan0bf0baf2013-01-12 02:04:23 +00002286 const char *prefix = GetExpressionPrefixContentsAsCString();
Greg Clayton62afb9f2013-11-04 19:35:17 +00002287 Error error;
Jim Ingham151c0322015-09-15 21:13:50 +00002288 execution_results = UserExpression::Evaluate (exe_ctx,
2289 options,
2290 expr_cstr,
2291 prefix,
2292 result_valobj_sp,
Jim Inghame5ee6f02016-03-29 22:00:08 +00002293 error,
2294 0, // Line Number
2295 fixed_expression);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002296 }
Jim Ingham6026ca32011-05-12 02:06:14 +00002297
2298 m_suppress_stop_hooks = old_suppress_value;
2299
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002300 return execution_results;
2301}
2302
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002303lldb::ExpressionVariableSP
2304Target::GetPersistentVariable(const ConstString &name)
Zachary Turner32abc6e2015-03-03 19:23:09 +00002305{
Greg Clayton5beec212015-10-08 21:04:34 +00002306 lldb::ExpressionVariableSP variable_sp;
2307 m_scratch_type_system_map.ForEach([this, name, &variable_sp](TypeSystem *type_system) -> bool
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002308 {
Greg Clayton5beec212015-10-08 21:04:34 +00002309 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002310 {
Greg Clayton5beec212015-10-08 21:04:34 +00002311 variable_sp = persistent_state->GetVariable(name);
2312
2313 if (variable_sp)
2314 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002315 }
Greg Clayton5beec212015-10-08 21:04:34 +00002316 return true; // Keep iterating the ForEach
2317 });
2318 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002319}
2320
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002321lldb::addr_t
Sean Callananb92bd752015-10-01 16:28:02 +00002322Target::GetPersistentSymbol(const ConstString &name)
2323{
Greg Clayton5beec212015-10-08 21:04:34 +00002324 lldb::addr_t address = LLDB_INVALID_ADDRESS;
Sean Callananb92bd752015-10-01 16:28:02 +00002325
Greg Clayton5beec212015-10-08 21:04:34 +00002326 m_scratch_type_system_map.ForEach([this, name, &address](TypeSystem *type_system) -> bool
Sean Callananb92bd752015-10-01 16:28:02 +00002327 {
Greg Clayton5beec212015-10-08 21:04:34 +00002328 if (PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState())
Sean Callananb92bd752015-10-01 16:28:02 +00002329 {
Greg Clayton5beec212015-10-08 21:04:34 +00002330 address = persistent_state->LookupSymbol(name);
2331 if (address != LLDB_INVALID_ADDRESS)
2332 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002333 }
Greg Clayton5beec212015-10-08 21:04:34 +00002334 return true; // Keep iterating the ForEach
2335 });
2336 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002337}
2338
2339lldb::addr_t
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002340Target::GetCallableLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2341{
2342 addr_t code_addr = load_addr;
2343 switch (m_arch.GetMachine())
2344 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002345 case llvm::Triple::mips:
2346 case llvm::Triple::mipsel:
2347 case llvm::Triple::mips64:
2348 case llvm::Triple::mips64el:
2349 switch (addr_class)
2350 {
2351 case eAddressClassData:
2352 case eAddressClassDebug:
2353 return LLDB_INVALID_ADDRESS;
2354
2355 case eAddressClassUnknown:
2356 case eAddressClassInvalid:
2357 case eAddressClassCode:
2358 case eAddressClassCodeAlternateISA:
2359 case eAddressClassRuntime:
2360 if ((code_addr & 2ull) || (addr_class == eAddressClassCodeAlternateISA))
2361 code_addr |= 1ull;
2362 break;
2363 }
2364 break;
2365
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002366 case llvm::Triple::arm:
2367 case llvm::Triple::thumb:
2368 switch (addr_class)
2369 {
2370 case eAddressClassData:
2371 case eAddressClassDebug:
2372 return LLDB_INVALID_ADDRESS;
2373
2374 case eAddressClassUnknown:
2375 case eAddressClassInvalid:
2376 case eAddressClassCode:
2377 case eAddressClassCodeAlternateISA:
2378 case eAddressClassRuntime:
2379 // Check if bit zero it no set?
2380 if ((code_addr & 1ull) == 0)
2381 {
2382 // Bit zero isn't set, check if the address is a multiple of 2?
2383 if (code_addr & 2ull)
2384 {
2385 // The address is a multiple of 2 so it must be thumb, set bit zero
2386 code_addr |= 1ull;
2387 }
2388 else if (addr_class == eAddressClassCodeAlternateISA)
2389 {
2390 // We checked the address and the address claims to be the alternate ISA
2391 // which means thumb, so set bit zero.
2392 code_addr |= 1ull;
2393 }
2394 }
2395 break;
2396 }
2397 break;
2398
2399 default:
2400 break;
2401 }
2402 return code_addr;
2403}
2404
2405lldb::addr_t
2406Target::GetOpcodeLoadAddress (lldb::addr_t load_addr, AddressClass addr_class) const
2407{
2408 addr_t opcode_addr = load_addr;
2409 switch (m_arch.GetMachine())
2410 {
Jaydeep Patil44d07fc2015-09-22 06:36:56 +00002411 case llvm::Triple::mips:
2412 case llvm::Triple::mipsel:
2413 case llvm::Triple::mips64:
2414 case llvm::Triple::mips64el:
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002415 case llvm::Triple::arm:
2416 case llvm::Triple::thumb:
2417 switch (addr_class)
2418 {
2419 case eAddressClassData:
2420 case eAddressClassDebug:
2421 return LLDB_INVALID_ADDRESS;
2422
2423 case eAddressClassInvalid:
2424 case eAddressClassUnknown:
2425 case eAddressClassCode:
2426 case eAddressClassCodeAlternateISA:
2427 case eAddressClassRuntime:
2428 opcode_addr &= ~(1ull);
2429 break;
2430 }
2431 break;
2432
2433 default:
2434 break;
2435 }
2436 return opcode_addr;
2437}
2438
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002439lldb::addr_t
2440Target::GetBreakableLoadAddress (lldb::addr_t addr)
2441{
2442 addr_t breakable_addr = addr;
2443 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_BREAKPOINTS));
2444
2445 switch (m_arch.GetMachine())
2446 {
2447 default:
2448 break;
2449 case llvm::Triple::mips:
2450 case llvm::Triple::mipsel:
2451 case llvm::Triple::mips64:
2452 case llvm::Triple::mips64el:
2453 {
2454 addr_t function_start = 0;
2455 addr_t current_offset = 0;
2456 uint32_t loop_count = 0;
2457 Address resolved_addr;
2458 uint32_t arch_flags = m_arch.GetFlags ();
2459 bool IsMips16 = arch_flags & ArchSpec::eMIPSAse_mips16;
2460 bool IsMicromips = arch_flags & ArchSpec::eMIPSAse_micromips;
2461 SectionLoadList &section_load_list = GetSectionLoadList();
2462
2463 if (section_load_list.IsEmpty())
2464 // No sections are loaded, so we must assume we are not running yet
2465 // and need to operate only on file address.
2466 m_images.ResolveFileAddress (addr, resolved_addr);
2467 else
2468 section_load_list.ResolveLoadAddress(addr, resolved_addr);
2469
2470 // Get the function boundaries to make sure we don't scan back before the beginning of the current function.
2471 ModuleSP temp_addr_module_sp (resolved_addr.GetModule());
2472 if (temp_addr_module_sp)
2473 {
2474 SymbolContext sc;
2475 uint32_t resolve_scope = eSymbolContextFunction | eSymbolContextSymbol;
Greg Clayton6071e6f2015-08-26 22:57:51 +00002476 temp_addr_module_sp->ResolveSymbolContextForAddress(resolved_addr, resolve_scope, sc);
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002477 Address sym_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002478 if (sc.function)
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002479 sym_addr = sc.function->GetAddressRange().GetBaseAddress();
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002480 else if (sc.symbol)
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002481 sym_addr = sc.symbol->GetAddress();
2482
2483 function_start = sym_addr.GetLoadAddress(this);
2484 if (function_start == LLDB_INVALID_ADDRESS)
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002485 function_start = sym_addr.GetFileAddress();
Bhushan D. Attardedf5f0b42016-01-27 10:16:30 +00002486
2487 if (function_start)
2488 current_offset = addr - function_start;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002489 }
2490
2491 // If breakpoint address is start of function then we dont have to do anything.
2492 if (current_offset == 0)
2493 return breakable_addr;
2494 else
2495 loop_count = current_offset / 2;
2496
2497 if (loop_count > 3)
2498 {
2499 // Scan previous 6 bytes
2500 if (IsMips16 | IsMicromips)
2501 loop_count = 3;
2502 // For mips-only, instructions are always 4 bytes, so scan previous 4 bytes only.
2503 else
2504 loop_count = 2;
2505 }
2506
2507 // Create Disassembler Instance
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002508 lldb::DisassemblerSP disasm_sp(Disassembler::FindPlugin(m_arch, nullptr, nullptr));
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002509
2510 ExecutionContext exe_ctx;
2511 CalculateExecutionContext(exe_ctx);
2512 InstructionList instruction_list;
2513 InstructionSP prev_insn;
2514 bool prefer_file_cache = true; // Read from file
2515 uint32_t inst_to_choose = 0;
2516
2517 for (uint32_t i = 1; i <= loop_count; i++)
2518 {
2519 // Adjust the address to read from.
2520 resolved_addr.Slide (-2);
2521 AddressRange range(resolved_addr, i*2);
2522 uint32_t insn_size = 0;
2523
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002524 disasm_sp->ParseInstructions(&exe_ctx, range, nullptr, prefer_file_cache);
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002525
2526 uint32_t num_insns = disasm_sp->GetInstructionList().GetSize();
2527 if (num_insns)
2528 {
2529 prev_insn = disasm_sp->GetInstructionList().GetInstructionAtIndex(0);
2530 insn_size = prev_insn->GetOpcode().GetByteSize();
2531 if (i == 1 && insn_size == 2)
2532 {
2533 // This looks like a valid 2-byte instruction (but it could be a part of upper 4 byte instruction).
2534 instruction_list.Append(prev_insn);
2535 inst_to_choose = 1;
2536 }
2537 else if (i == 2)
2538 {
2539 // Here we may get one 4-byte instruction or two 2-byte instructions.
2540 if (num_insns == 2)
2541 {
2542 // Looks like there are two 2-byte instructions above our breakpoint target address.
2543 // 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.
2544 // In both cases we don't care because in this case lower 2-byte instruction is definitely a valid instruction
2545 // and whatever i=1 iteration has found out is true.
2546 inst_to_choose = 1;
2547 break;
2548 }
2549 else if (insn_size == 4)
2550 {
2551 // This instruction claims its a valid 4-byte instruction. But it could be a part of it's upper 4-byte instruction.
2552 // Lets try scanning upper 2 bytes to verify this.
2553 instruction_list.Append(prev_insn);
2554 inst_to_choose = 2;
2555 }
2556 }
2557 else if (i == 3)
2558 {
2559 if (insn_size == 4)
2560 // FIXME: We reached here that means instruction at [target - 4] has already claimed to be a 4-byte instruction,
2561 // and now instruction at [target - 6] is also claiming that it's a 4-byte instruction. This can not be true.
2562 // In this case we can not decide the valid previous instruction so we let lldb set the breakpoint at the address given by user.
2563 inst_to_choose = 0;
2564 else
2565 // This is straight-forward
2566 inst_to_choose = 2;
2567 break;
2568 }
2569 }
2570 else
2571 {
2572 // Decode failed, bytes do not form a valid instruction. So whatever previous iteration has found out is true.
2573 if (i > 1)
2574 {
2575 inst_to_choose = i - 1;
2576 break;
2577 }
2578 }
2579 }
2580
2581 // Check if we are able to find any valid instruction.
2582 if (inst_to_choose)
2583 {
2584 if (inst_to_choose > instruction_list.GetSize())
2585 inst_to_choose--;
2586 prev_insn = instruction_list.GetInstructionAtIndex(inst_to_choose - 1);
2587
2588 if (prev_insn->HasDelaySlot())
2589 {
2590 uint32_t shift_size = prev_insn->GetOpcode().GetByteSize();
2591 // Adjust the breakable address
2592 breakable_addr = addr - shift_size;
2593 if (log)
2594 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);
2595 }
2596 }
2597 break;
2598 }
2599 }
2600 return breakable_addr;
2601}
2602
Greg Clayton9585fbf2013-03-19 00:20:55 +00002603SourceManager &
2604Target::GetSourceManager ()
2605{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002606 if (!m_source_manager_ap)
Greg Clayton9585fbf2013-03-19 00:20:55 +00002607 m_source_manager_ap.reset (new SourceManager(shared_from_this()));
2608 return *m_source_manager_ap;
2609}
2610
Sean Callanan9998acd2014-12-05 01:21:59 +00002611ClangModulesDeclVendor *
2612Target::GetClangModulesDeclVendor ()
2613{
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00002614 static std::mutex s_clang_modules_decl_vendor_mutex; // If this is contended we can make it per-target
2615
Sean Callanan9998acd2014-12-05 01:21:59 +00002616 {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +00002617 std::lock_guard<std::mutex> guard(s_clang_modules_decl_vendor_mutex);
2618
Sean Callanan9998acd2014-12-05 01:21:59 +00002619 if (!m_clang_modules_decl_vendor_ap)
2620 {
2621 m_clang_modules_decl_vendor_ap.reset(ClangModulesDeclVendor::Create(*this));
2622 }
2623 }
2624
2625 return m_clang_modules_decl_vendor_ap.get();
2626}
Greg Clayton9585fbf2013-03-19 00:20:55 +00002627
Greg Clayton44d93782014-01-27 23:43:24 +00002628Target::StopHookSP
2629Target::CreateStopHook ()
Jim Ingham9575d842011-03-11 03:53:59 +00002630{
2631 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
Greg Clayton44d93782014-01-27 23:43:24 +00002632 Target::StopHookSP stop_hook_sp (new StopHook(shared_from_this(), new_uid));
2633 m_stop_hooks[new_uid] = stop_hook_sp;
2634 return stop_hook_sp;
Jim Ingham9575d842011-03-11 03:53:59 +00002635}
2636
2637bool
2638Target::RemoveStopHookByID (lldb::user_id_t user_id)
2639{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002640 size_t num_removed = m_stop_hooks.erase(user_id);
2641 return (num_removed != 0);
Jim Ingham9575d842011-03-11 03:53:59 +00002642}
2643
2644void
2645Target::RemoveAllStopHooks ()
2646{
2647 m_stop_hooks.clear();
2648}
2649
2650Target::StopHookSP
2651Target::GetStopHookByID (lldb::user_id_t user_id)
2652{
2653 StopHookSP found_hook;
2654
2655 StopHookCollection::iterator specified_hook_iter;
2656 specified_hook_iter = m_stop_hooks.find (user_id);
2657 if (specified_hook_iter != m_stop_hooks.end())
2658 found_hook = (*specified_hook_iter).second;
2659 return found_hook;
2660}
2661
2662bool
2663Target::SetStopHookActiveStateByID (lldb::user_id_t user_id, bool active_state)
2664{
2665 StopHookCollection::iterator specified_hook_iter;
2666 specified_hook_iter = m_stop_hooks.find (user_id);
2667 if (specified_hook_iter == m_stop_hooks.end())
2668 return false;
2669
2670 (*specified_hook_iter).second->SetIsActive (active_state);
2671 return true;
2672}
2673
2674void
2675Target::SetAllStopHooksActiveState (bool active_state)
2676{
2677 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2678 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2679 {
2680 (*pos).second->SetIsActive (active_state);
2681 }
2682}
2683
2684void
2685Target::RunStopHooks ()
2686{
Jim Ingham6026ca32011-05-12 02:06:14 +00002687 if (m_suppress_stop_hooks)
2688 return;
2689
Jim Ingham9575d842011-03-11 03:53:59 +00002690 if (!m_process_sp)
2691 return;
Enrico Granatae2e091b2012-08-03 22:24:48 +00002692
2693 // <rdar://problem/12027563> make sure we check that we are not stopped because of us running a user expression
2694 // since in that case we do not want to run the stop-hooks
2695 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2696 return;
2697
Jim Ingham9575d842011-03-11 03:53:59 +00002698 if (m_stop_hooks.empty())
2699 return;
2700
2701 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2702
2703 // If there aren't any active stop hooks, don't bother either:
2704 bool any_active_hooks = false;
2705 for (pos = m_stop_hooks.begin(); pos != end; pos++)
2706 {
2707 if ((*pos).second->IsActive())
2708 {
2709 any_active_hooks = true;
2710 break;
2711 }
2712 }
2713 if (!any_active_hooks)
2714 return;
2715
2716 CommandReturnObject result;
2717
2718 std::vector<ExecutionContext> exc_ctx_with_reasons;
2719 std::vector<SymbolContext> sym_ctx_with_reasons;
2720
2721 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2722 size_t num_threads = cur_threadlist.GetSize();
2723 for (size_t i = 0; i < num_threads; i++)
2724 {
2725 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex (i);
2726 if (cur_thread_sp->ThreadStoppedForAReason())
2727 {
Jason Molendab57e4a12013-11-04 09:33:30 +00002728 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
Jim Ingham9575d842011-03-11 03:53:59 +00002729 exc_ctx_with_reasons.push_back(ExecutionContext(m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2730 sym_ctx_with_reasons.push_back(cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2731 }
2732 }
2733
2734 // If no threads stopped for a reason, don't run the stop-hooks.
2735 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2736 if (num_exe_ctx == 0)
2737 return;
2738
Jim Ingham5b52f0c2011-06-02 23:58:26 +00002739 result.SetImmediateOutputStream (m_debugger.GetAsyncOutputStream());
2740 result.SetImmediateErrorStream (m_debugger.GetAsyncErrorStream());
Jim Ingham9575d842011-03-11 03:53:59 +00002741
2742 bool keep_going = true;
2743 bool hooks_ran = false;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002744 bool print_hook_header = (m_stop_hooks.size() != 1);
2745 bool print_thread_header = (num_exe_ctx != 1);
Jim Ingham381e25b2011-03-22 01:47:27 +00002746
Jim Ingham9575d842011-03-11 03:53:59 +00002747 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++)
2748 {
2749 // result.Clear();
2750 StopHookSP cur_hook_sp = (*pos).second;
2751 if (!cur_hook_sp->IsActive())
2752 continue;
2753
2754 bool any_thread_matched = false;
2755 for (size_t i = 0; keep_going && i < num_exe_ctx; i++)
2756 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002757 if ((cur_hook_sp->GetSpecifier() == nullptr
Jim Ingham9575d842011-03-11 03:53:59 +00002758 || cur_hook_sp->GetSpecifier()->SymbolContextMatches(sym_ctx_with_reasons[i]))
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002759 && (cur_hook_sp->GetThreadSpecifier() == nullptr
Jim Ingham3d902922012-03-07 22:03:04 +00002760 || cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(exc_ctx_with_reasons[i].GetThreadRef())))
Jim Ingham9575d842011-03-11 03:53:59 +00002761 {
2762 if (!hooks_ran)
2763 {
Jim Ingham9575d842011-03-11 03:53:59 +00002764 hooks_ran = true;
2765 }
Jim Ingham381e25b2011-03-22 01:47:27 +00002766 if (print_hook_header && !any_thread_matched)
Jim Ingham9575d842011-03-11 03:53:59 +00002767 {
Johnny Chenaeab25c2011-10-24 23:01:06 +00002768 const char *cmd = (cur_hook_sp->GetCommands().GetSize() == 1 ?
2769 cur_hook_sp->GetCommands().GetStringAtIndex(0) :
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00002770 nullptr);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002771 if (cmd)
Daniel Malead01b2952012-11-29 21:49:15 +00002772 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n", cur_hook_sp->GetID(), cmd);
Johnny Chenaeab25c2011-10-24 23:01:06 +00002773 else
Daniel Malead01b2952012-11-29 21:49:15 +00002774 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002775 any_thread_matched = true;
2776 }
2777
Jim Ingham381e25b2011-03-22 01:47:27 +00002778 if (print_thread_header)
Greg Claytonc14ee322011-09-22 04:58:26 +00002779 result.AppendMessageWithFormat("-- Thread %d\n", exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
Jim Ingham26c7bf92014-10-11 00:38:27 +00002780
2781 CommandInterpreterRunOptions options;
2782 options.SetStopOnContinue (true);
2783 options.SetStopOnError (true);
2784 options.SetEchoCommands (false);
2785 options.SetPrintResults (true);
2786 options.SetAddToHistory (false);
2787
2788 GetDebugger().GetCommandInterpreter().HandleCommands (cur_hook_sp->GetCommands(),
2789 &exc_ctx_with_reasons[i],
2790 options,
Greg Clayton32e0a752011-03-30 18:16:51 +00002791 result);
Jim Ingham9575d842011-03-11 03:53:59 +00002792
2793 // If the command started the target going again, we should bag out of
2794 // running the stop hooks.
Greg Clayton32e0a752011-03-30 18:16:51 +00002795 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2796 (result.GetStatus() == eReturnStatusSuccessContinuingResult))
Jim Ingham9575d842011-03-11 03:53:59 +00002797 {
Daniel Malead01b2952012-11-29 21:49:15 +00002798 result.AppendMessageWithFormat ("Aborting stop hooks, hook %" PRIu64 " set the program running.", cur_hook_sp->GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00002799 keep_going = false;
2800 }
2801 }
2802 }
2803 }
Jason Molenda879cf772011-09-23 00:42:55 +00002804
Caroline Tice969ed3d2011-05-02 20:41:46 +00002805 result.GetImmediateOutputStream()->Flush();
2806 result.GetImmediateErrorStream()->Flush();
Jim Ingham9575d842011-03-11 03:53:59 +00002807}
2808
Greg Claytonfbb76342013-11-20 21:07:01 +00002809const TargetPropertiesSP &
2810Target::GetGlobalProperties()
2811{
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002812 // NOTE: intentional leak so we don't crash if global destructor chain gets
2813 // called as other threads still use the result of this function
2814 static TargetPropertiesSP *g_settings_sp_ptr = nullptr;
Greg Clayton04df8ee2016-02-26 19:38:18 +00002815 static std::once_flag g_once_flag;
2816 std::call_once(g_once_flag, []() {
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002817 g_settings_sp_ptr = new TargetPropertiesSP(new TargetProperties(nullptr));
Greg Clayton04df8ee2016-02-26 19:38:18 +00002818 });
Greg Claytoncc2e27f2016-02-26 23:20:08 +00002819 return *g_settings_sp_ptr;
Greg Claytonfbb76342013-11-20 21:07:01 +00002820}
2821
2822Error
2823Target::Install (ProcessLaunchInfo *launch_info)
2824{
2825 Error error;
2826 PlatformSP platform_sp (GetPlatform());
2827 if (platform_sp)
2828 {
2829 if (platform_sp->IsRemote())
2830 {
2831 if (platform_sp->IsConnected())
2832 {
2833 // Install all files that have an install path, and always install the
2834 // main executable when connected to a remote platform
2835 const ModuleList& modules = GetImages();
2836 const size_t num_images = modules.GetSize();
2837 for (size_t idx = 0; idx < num_images; ++idx)
2838 {
2839 const bool is_main_executable = idx == 0;
2840 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2841 if (module_sp)
2842 {
2843 FileSpec local_file (module_sp->GetFileSpec());
2844 if (local_file)
2845 {
2846 FileSpec remote_file (module_sp->GetRemoteInstallFileSpec());
2847 if (!remote_file)
2848 {
2849 if (is_main_executable) // TODO: add setting for always installing main executable???
2850 {
2851 // Always install the main executable
Chaoren Lind3173f32015-05-29 19:52:29 +00002852 remote_file = platform_sp->GetRemoteWorkingDirectory();
2853 remote_file.AppendPathComponent(module_sp->GetFileSpec().GetFilename().GetCString());
Greg Claytonfbb76342013-11-20 21:07:01 +00002854 }
2855 }
2856 if (remote_file)
2857 {
2858 error = platform_sp->Install(local_file, remote_file);
2859 if (error.Success())
2860 {
2861 module_sp->SetPlatformFileSpec(remote_file);
2862 if (is_main_executable)
2863 {
Chaoren Lind3173f32015-05-29 19:52:29 +00002864 platform_sp->SetFilePermissions(remote_file, 0700);
Greg Claytonfbb76342013-11-20 21:07:01 +00002865 if (launch_info)
2866 launch_info->SetExecutableFile(remote_file, false);
2867 }
2868 }
2869 else
2870 break;
2871 }
2872 }
2873 }
2874 }
2875 }
2876 }
2877 }
2878 return error;
2879}
Greg Clayton7b242382011-07-08 00:48:09 +00002880
Greg Claytond5944cd2013-12-06 01:12:00 +00002881bool
2882Target::ResolveLoadAddress (addr_t load_addr, Address &so_addr, uint32_t stop_id)
2883{
2884 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2885}
2886
2887bool
Matthew Gardinerc928de32014-10-22 07:22:56 +00002888Target::ResolveFileAddress (lldb::addr_t file_addr, Address &resolved_addr)
2889{
2890 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2891}
2892
2893bool
Greg Claytond5944cd2013-12-06 01:12:00 +00002894Target::SetSectionLoadAddress (const SectionSP &section_sp, addr_t new_section_load_addr, bool warn_multiple)
2895{
2896 const addr_t old_section_load_addr = m_section_load_history.GetSectionLoadAddress (SectionLoadHistory::eStopIDNow, section_sp);
2897 if (old_section_load_addr != new_section_load_addr)
2898 {
2899 uint32_t stop_id = 0;
2900 ProcessSP process_sp(GetProcessSP());
2901 if (process_sp)
2902 stop_id = process_sp->GetStopID();
2903 else
2904 stop_id = m_section_load_history.GetLastStopID();
2905 if (m_section_load_history.SetSectionLoadAddress (stop_id, section_sp, new_section_load_addr, warn_multiple))
2906 return true; // Return true if the section load address was changed...
2907 }
2908 return false; // Return false to indicate nothing changed
Greg Claytond5944cd2013-12-06 01:12:00 +00002909}
2910
Greg Clayton8012cad2014-11-17 19:39:20 +00002911size_t
2912Target::UnloadModuleSections (const ModuleList &module_list)
2913{
2914 size_t section_unload_count = 0;
2915 size_t num_modules = module_list.GetSize();
2916 for (size_t i=0; i<num_modules; ++i)
2917 {
2918 section_unload_count += UnloadModuleSections (module_list.GetModuleAtIndex(i));
2919 }
2920 return section_unload_count;
2921}
2922
2923size_t
2924Target::UnloadModuleSections (const lldb::ModuleSP &module_sp)
2925{
2926 uint32_t stop_id = 0;
2927 ProcessSP process_sp(GetProcessSP());
2928 if (process_sp)
2929 stop_id = process_sp->GetStopID();
2930 else
2931 stop_id = m_section_load_history.GetLastStopID();
2932 SectionList *sections = module_sp->GetSectionList();
2933 size_t section_unload_count = 0;
2934 if (sections)
2935 {
2936 const uint32_t num_sections = sections->GetNumSections(0);
2937 for (uint32_t i = 0; i < num_sections; ++i)
2938 {
2939 section_unload_count += m_section_load_history.SetSectionUnloaded(stop_id, sections->GetSectionAtIndex(i));
2940 }
2941 }
2942 return section_unload_count;
2943}
2944
Greg Claytond5944cd2013-12-06 01:12:00 +00002945bool
2946Target::SetSectionUnloaded (const lldb::SectionSP &section_sp)
2947{
2948 uint32_t stop_id = 0;
2949 ProcessSP process_sp(GetProcessSP());
2950 if (process_sp)
2951 stop_id = process_sp->GetStopID();
2952 else
2953 stop_id = m_section_load_history.GetLastStopID();
2954 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp);
2955}
2956
2957bool
2958Target::SetSectionUnloaded (const lldb::SectionSP &section_sp, addr_t load_addr)
2959{
2960 uint32_t stop_id = 0;
2961 ProcessSP process_sp(GetProcessSP());
2962 if (process_sp)
2963 stop_id = process_sp->GetStopID();
2964 else
2965 stop_id = m_section_load_history.GetLastStopID();
2966 return m_section_load_history.SetSectionUnloaded (stop_id, section_sp, load_addr);
2967}
2968
2969void
2970Target::ClearAllLoadedSections ()
2971{
2972 m_section_load_history.Clear();
2973}
2974
Greg Claytonb09c5382013-12-13 17:20:18 +00002975Error
Greg Clayton8012cad2014-11-17 19:39:20 +00002976Target::Launch (ProcessLaunchInfo &launch_info, Stream *stream)
Greg Claytonb09c5382013-12-13 17:20:18 +00002977{
2978 Error error;
Todd Fialaac33cc92014-10-09 01:02:08 +00002979 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TARGET));
2980
2981 if (log)
2982 log->Printf ("Target::%s() called for %s", __FUNCTION__, launch_info.GetExecutableFile().GetPath().c_str ());
2983
Greg Claytonb09c5382013-12-13 17:20:18 +00002984 StateType state = eStateInvalid;
2985
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002986 // Scope to temporarily get the process state in case someone has manually
2987 // remotely connected already to a process and we can skip the platform
2988 // launching.
2989 {
2990 ProcessSP process_sp (GetProcessSP());
2991
2992 if (process_sp)
Todd Fialaac33cc92014-10-09 01:02:08 +00002993 {
Greg Clayton6b32f1e2013-12-18 02:06:45 +00002994 state = process_sp->GetState();
Todd Fialaac33cc92014-10-09 01:02:08 +00002995 if (log)
2996 log->Printf ("Target::%s the process exists, and its current state is %s", __FUNCTION__, StateAsCString (state));
2997 }
2998 else
2999 {
3000 if (log)
3001 log->Printf ("Target::%s the process instance doesn't currently exist.", __FUNCTION__);
3002 }
Greg Clayton6b32f1e2013-12-18 02:06:45 +00003003 }
3004
Greg Claytonb09c5382013-12-13 17:20:18 +00003005 launch_info.GetFlags().Set (eLaunchFlagDebug);
3006
3007 // Get the value of synchronous execution here. If you wait till after you have started to
3008 // run, then you could have hit a breakpoint, whose command might switch the value, and
3009 // then you'll pick up that incorrect value.
3010 Debugger &debugger = GetDebugger();
3011 const bool synchronous_execution = debugger.GetCommandInterpreter().GetSynchronous ();
3012
3013 PlatformSP platform_sp (GetPlatform());
3014
3015 // Finalize the file actions, and if none were given, default to opening
3016 // up a pseudo terminal
3017 const bool default_to_use_pty = platform_sp ? platform_sp->IsHost() : false;
Todd Fiala75f47c32014-10-11 21:42:09 +00003018 if (log)
3019 log->Printf ("Target::%s have platform=%s, platform_sp->IsHost()=%s, default_to_use_pty=%s",
3020 __FUNCTION__,
3021 platform_sp ? "true" : "false",
3022 platform_sp ? (platform_sp->IsHost () ? "true" : "false") : "n/a",
3023 default_to_use_pty ? "true" : "false");
3024
Greg Claytonb09c5382013-12-13 17:20:18 +00003025 launch_info.FinalizeFileActions (this, default_to_use_pty);
3026
3027 if (state == eStateConnected)
3028 {
3029 if (launch_info.GetFlags().Test (eLaunchFlagLaunchInTTY))
3030 {
3031 error.SetErrorString("can't launch in tty when launching through a remote connection");
3032 return error;
3033 }
3034 }
3035
3036 if (!launch_info.GetArchitecture().IsValid())
3037 launch_info.GetArchitecture() = GetArchitecture();
Todd Fiala015d8182014-07-22 23:41:36 +00003038
3039 // 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 +00003040 if (state != eStateConnected && platform_sp && platform_sp->CanDebugProcess ())
3041 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003042 if (log)
3043 log->Printf ("Target::%s asking the platform to debug the process", __FUNCTION__);
3044
Greg Clayton5df78fa2015-05-23 03:54:53 +00003045 // Get a weak pointer to the previous process if we have one
3046 ProcessWP process_wp;
3047 if (m_process_sp)
3048 process_wp = m_process_sp;
Greg Claytonb09c5382013-12-13 17:20:18 +00003049 m_process_sp = GetPlatform()->DebugProcess (launch_info,
3050 debugger,
3051 this,
Greg Claytonb09c5382013-12-13 17:20:18 +00003052 error);
Greg Clayton5df78fa2015-05-23 03:54:53 +00003053
3054 // Cleanup the old process since someone might still have a strong
3055 // reference to this process and we would like to allow it to cleanup
3056 // as much as it can without the object being destroyed. We try to
3057 // lock the shared pointer and if that works, then someone else still
3058 // has a strong reference to the process.
3059
3060 ProcessSP old_process_sp(process_wp.lock());
3061 if (old_process_sp)
3062 old_process_sp->Finalize();
Greg Claytonb09c5382013-12-13 17:20:18 +00003063 }
3064 else
3065 {
Todd Fialaac33cc92014-10-09 01:02:08 +00003066 if (log)
3067 log->Printf ("Target::%s the platform doesn't know how to debug a process, getting a process plugin to do this for us.", __FUNCTION__);
3068
Greg Claytonb09c5382013-12-13 17:20:18 +00003069 if (state == eStateConnected)
3070 {
3071 assert(m_process_sp);
3072 }
3073 else
3074 {
Todd Fiala015d8182014-07-22 23:41:36 +00003075 // Use a Process plugin to construct the process.
Greg Claytonb09c5382013-12-13 17:20:18 +00003076 const char *plugin_name = launch_info.GetProcessPluginName();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003077 CreateProcess(launch_info.GetListenerForProcess(debugger), plugin_name, nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00003078 }
Todd Fiala015d8182014-07-22 23:41:36 +00003079
3080 // Since we didn't have a platform launch the process, launch it here.
Greg Claytonb09c5382013-12-13 17:20:18 +00003081 if (m_process_sp)
3082 error = m_process_sp->Launch (launch_info);
3083 }
3084
3085 if (!m_process_sp)
3086 {
3087 if (error.Success())
3088 error.SetErrorString("failed to launch or debug process");
3089 return error;
3090 }
3091
3092 if (error.Success())
3093 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003094 if (synchronous_execution || !launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003095 {
Greg Clayton44d93782014-01-27 23:43:24 +00003096 ListenerSP hijack_listener_sp (launch_info.GetHijackListener());
Zachary Turnere6d213a2015-03-26 20:41:14 +00003097 if (!hijack_listener_sp)
3098 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003099 hijack_listener_sp = Listener::MakeListener("lldb.Target.Launch.hijack");
Zachary Turnere6d213a2015-03-26 20:41:14 +00003100 launch_info.SetHijackListener(hijack_listener_sp);
Jim Ingham583bbb12016-03-07 21:50:25 +00003101 m_process_sp->HijackProcessEvents(hijack_listener_sp);
Zachary Turnere6d213a2015-03-26 20:41:14 +00003102 }
Todd Fialaac33cc92014-10-09 01:02:08 +00003103
Jim Ingham583bbb12016-03-07 21:50:25 +00003104 StateType state = m_process_sp->WaitForProcessToStop(nullptr, nullptr, false, hijack_listener_sp, nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00003105
3106 if (state == eStateStopped)
3107 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003108 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry))
Greg Claytonb09c5382013-12-13 17:20:18 +00003109 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003110 if (synchronous_execution)
Greg Claytonb09c5382013-12-13 17:20:18 +00003111 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003112 error = m_process_sp->PrivateResume();
3113 if (error.Success())
Greg Claytonb09c5382013-12-13 17:20:18 +00003114 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003115 state = m_process_sp->WaitForProcessToStop(nullptr, nullptr, true, hijack_listener_sp, stream);
Ilia K064e69f2015-03-23 21:16:25 +00003116 const bool must_be_alive = false; // eStateExited is ok, so this must be false
3117 if (!StateIsStoppedState(state, must_be_alive))
3118 {
3119 error.SetErrorStringWithFormat("process isn't stopped: %s", StateAsCString(state));
3120 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003121 }
3122 }
Ilia K064e69f2015-03-23 21:16:25 +00003123 else
3124 {
Zachary Turnere6d213a2015-03-26 20:41:14 +00003125 m_process_sp->RestoreProcessEvents();
3126 error = m_process_sp->PrivateResume();
Zachary Turnere6d213a2015-03-26 20:41:14 +00003127 }
3128 if (!error.Success())
3129 {
Ilia K064e69f2015-03-23 21:16:25 +00003130 Error error2;
3131 error2.SetErrorStringWithFormat("process resume at entry point failed: %s", error.AsCString());
3132 error = error2;
3133 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003134 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003135 }
Greg Clayton40286e02014-04-30 20:29:09 +00003136 else if (state == eStateExited)
3137 {
Zachary Turner10687b02014-10-20 17:46:43 +00003138 bool with_shell = !!launch_info.GetShell();
Greg Clayton40286e02014-04-30 20:29:09 +00003139 const int exit_status = m_process_sp->GetExitStatus();
3140 const char *exit_desc = m_process_sp->GetExitDescription();
3141#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'."
3142 if (exit_desc && exit_desc[0])
3143 {
3144 if (with_shell)
3145 error.SetErrorStringWithFormat ("process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE, exit_status, exit_desc);
3146 else
3147 error.SetErrorStringWithFormat ("process exited with status %i (%s)", exit_status, exit_desc);
3148 }
3149 else
3150 {
3151 if (with_shell)
3152 error.SetErrorStringWithFormat ("process exited with status %i" LAUNCH_SHELL_MESSAGE, exit_status);
3153 else
3154 error.SetErrorStringWithFormat ("process exited with status %i", exit_status);
3155 }
3156 }
Greg Claytonb09c5382013-12-13 17:20:18 +00003157 else
3158 {
3159 error.SetErrorStringWithFormat ("initial process state wasn't stopped: %s", StateAsCString(state));
3160 }
3161 }
Greg Clayton44d93782014-01-27 23:43:24 +00003162 m_process_sp->RestoreProcessEvents ();
Greg Claytonb09c5382013-12-13 17:20:18 +00003163 }
3164 else
3165 {
Greg Clayton44d93782014-01-27 23:43:24 +00003166 Error error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003167 error2.SetErrorStringWithFormat ("process launch failed: %s", error.AsCString());
Greg Clayton44d93782014-01-27 23:43:24 +00003168 error = error2;
Greg Claytonb09c5382013-12-13 17:20:18 +00003169 }
3170 return error;
3171}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003172
3173Error
3174Target::Attach (ProcessAttachInfo &attach_info, Stream *stream)
3175{
3176 auto state = eStateInvalid;
3177 auto process_sp = GetProcessSP ();
3178 if (process_sp)
3179 {
3180 state = process_sp->GetState ();
3181 if (process_sp->IsAlive () && state != eStateConnected)
3182 {
3183 if (state == eStateAttaching)
3184 return Error ("process attach is in progress");
3185 return Error ("a process is already being debugged");
3186 }
3187 }
3188
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003189 const ModuleSP old_exec_module_sp = GetExecutableModule ();
3190
3191 // If no process info was specified, then use the target executable
3192 // name as the process to attach to by default
3193 if (!attach_info.ProcessInfoSpecified ())
3194 {
3195 if (old_exec_module_sp)
3196 attach_info.GetExecutableFile ().GetFilename () = old_exec_module_sp->GetPlatformFileSpec ().GetFilename ();
3197
3198 if (!attach_info.ProcessInfoSpecified ())
3199 {
3200 return Error ("no process specified, create a target with a file, or specify the --pid or --name");
3201 }
3202 }
3203
3204 const auto platform_sp = GetDebugger ().GetPlatformList ().GetSelectedPlatform ();
Greg Claytonb3788ea2015-10-05 22:58:37 +00003205 ListenerSP hijack_listener_sp;
3206 const bool async = attach_info.GetAsync();
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003207 if (!async)
Greg Claytonb3788ea2015-10-05 22:58:37 +00003208 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003209 hijack_listener_sp = Listener::MakeListener("lldb.Target.Attach.attach.hijack");
Greg Claytonb3788ea2015-10-05 22:58:37 +00003210 attach_info.SetHijackListener (hijack_listener_sp);
3211 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003212
3213 Error error;
3214 if (state != eStateConnected && platform_sp != nullptr && platform_sp->CanDebugProcess ())
3215 {
3216 SetPlatform (platform_sp);
3217 process_sp = platform_sp->Attach (attach_info, GetDebugger (), this, error);
3218 }
3219 else
3220 {
3221 if (state != eStateConnected)
3222 {
3223 const char *plugin_name = attach_info.GetProcessPluginName ();
3224 process_sp = CreateProcess (attach_info.GetListenerForProcess (GetDebugger ()), plugin_name, nullptr);
3225 if (process_sp == nullptr)
3226 {
3227 error.SetErrorStringWithFormat ("failed to create process using plugin %s", (plugin_name) ? plugin_name : "null");
3228 return error;
3229 }
3230 }
Greg Claytonb3788ea2015-10-05 22:58:37 +00003231 if (hijack_listener_sp)
Jim Ingham583bbb12016-03-07 21:50:25 +00003232 process_sp->HijackProcessEvents (hijack_listener_sp);
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003233 error = process_sp->Attach (attach_info);
3234 }
3235
Greg Claytone75e5d82015-10-20 00:14:20 +00003236 if (error.Success () && process_sp)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003237 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003238 if (async)
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003239 {
Greg Claytone75e5d82015-10-20 00:14:20 +00003240 process_sp->RestoreProcessEvents ();
3241 }
3242 else
3243 {
Jim Ingham583bbb12016-03-07 21:50:25 +00003244 state = process_sp->WaitForProcessToStop (nullptr, nullptr, false, attach_info.GetHijackListener(), stream);
Greg Claytone75e5d82015-10-20 00:14:20 +00003245 process_sp->RestoreProcessEvents ();
3246
3247 if (state != eStateStopped)
3248 {
3249 const char *exit_desc = process_sp->GetExitDescription ();
3250 if (exit_desc)
3251 error.SetErrorStringWithFormat ("%s", exit_desc);
3252 else
3253 error.SetErrorString ("process did not stop (no such process or permission problem?)");
3254 process_sp->Destroy (false);
3255 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003256 }
3257 }
3258 return error;
3259}
3260
Jim Ingham9575d842011-03-11 03:53:59 +00003261//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003262// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003263//--------------------------------------------------------------
Jim Ingham9575d842011-03-11 03:53:59 +00003264Target::StopHook::StopHook (lldb::TargetSP target_sp, lldb::user_id_t uid) :
3265 UserID (uid),
3266 m_target_sp (target_sp),
Jim Ingham9575d842011-03-11 03:53:59 +00003267 m_commands (),
3268 m_specifier_sp (),
Greg Claytone01e07b2013-04-18 18:10:51 +00003269 m_thread_spec_ap(),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003270 m_active (true)
Jim Ingham9575d842011-03-11 03:53:59 +00003271{
3272}
3273
3274Target::StopHook::StopHook (const StopHook &rhs) :
3275 UserID (rhs.GetID()),
3276 m_target_sp (rhs.m_target_sp),
3277 m_commands (rhs.m_commands),
3278 m_specifier_sp (rhs.m_specifier_sp),
Greg Claytone01e07b2013-04-18 18:10:51 +00003279 m_thread_spec_ap (),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003280 m_active (rhs.m_active)
Jim Ingham9575d842011-03-11 03:53:59 +00003281{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003282 if (rhs.m_thread_spec_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00003283 m_thread_spec_ap.reset (new ThreadSpec(*rhs.m_thread_spec_ap.get()));
3284}
3285
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003286Target::StopHook::~StopHook() = default;
Jim Ingham9575d842011-03-11 03:53:59 +00003287
3288void
Zachary Turner32abc6e2015-03-03 19:23:09 +00003289Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier)
3290{
3291 m_specifier_sp.reset(specifier);
3292}
3293
3294void
Jim Ingham9575d842011-03-11 03:53:59 +00003295Target::StopHook::SetThreadSpecifier (ThreadSpec *specifier)
3296{
3297 m_thread_spec_ap.reset (specifier);
3298}
Jim Ingham9575d842011-03-11 03:53:59 +00003299
3300void
3301Target::StopHook::GetDescription (Stream *s, lldb::DescriptionLevel level) const
3302{
3303 int indent_level = s->GetIndentLevel();
3304
3305 s->SetIndentLevel(indent_level + 2);
3306
Daniel Malead01b2952012-11-29 21:49:15 +00003307 s->Printf ("Hook: %" PRIu64 "\n", GetID());
Jim Ingham9575d842011-03-11 03:53:59 +00003308 if (m_active)
3309 s->Indent ("State: enabled\n");
3310 else
3311 s->Indent ("State: disabled\n");
3312
3313 if (m_specifier_sp)
3314 {
3315 s->Indent();
3316 s->PutCString ("Specifier:\n");
3317 s->SetIndentLevel (indent_level + 4);
3318 m_specifier_sp->GetDescription (s, level);
3319 s->SetIndentLevel (indent_level + 2);
3320 }
3321
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003322 if (m_thread_spec_ap)
Jim Ingham9575d842011-03-11 03:53:59 +00003323 {
3324 StreamString tmp;
3325 s->Indent("Thread:\n");
3326 m_thread_spec_ap->GetDescription (&tmp, level);
3327 s->SetIndentLevel (indent_level + 4);
3328 s->Indent (tmp.GetData());
3329 s->PutCString ("\n");
3330 s->SetIndentLevel (indent_level + 2);
3331 }
3332
3333 s->Indent ("Commands: \n");
3334 s->SetIndentLevel (indent_level + 4);
3335 uint32_t num_commands = m_commands.GetSize();
3336 for (uint32_t i = 0; i < num_commands; i++)
3337 {
3338 s->Indent(m_commands.GetStringAtIndex(i));
3339 s->PutCString ("\n");
3340 }
3341 s->SetIndentLevel (indent_level);
3342}
3343
Greg Clayton67cc0632012-08-22 17:17:09 +00003344//--------------------------------------------------------------
3345// class TargetProperties
3346//--------------------------------------------------------------
3347
3348OptionEnumValueElement
3349lldb_private::g_dynamic_value_types[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003350{
Greg Clayton67cc0632012-08-22 17:17:09 +00003351 { eNoDynamicValues, "no-dynamic-values", "Don't calculate the dynamic type of values"},
3352 { eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values even if you have to run the target."},
3353 { eDynamicDontRunTarget, "no-run-target", "Calculate the dynamic type of values, but don't run the target."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003354 { 0, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +00003355};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003356
Greg Clayton1f746072012-08-29 21:13:06 +00003357static OptionEnumValueElement
3358g_inline_breakpoint_enums[] =
3359{
3360 { 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."},
3361 { eInlineBreakpointsHeaders, "headers", "Only check for inline breakpoint locations when setting breakpoints in header files, but not when setting breakpoint in implementation source files (default)."},
3362 { 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 +00003363 { 0, nullptr, nullptr }
Greg Clayton1f746072012-08-29 21:13:06 +00003364};
3365
Jim Ingham0f063ba2013-03-02 00:26:47 +00003366typedef enum x86DisassemblyFlavor
3367{
3368 eX86DisFlavorDefault,
3369 eX86DisFlavorIntel,
3370 eX86DisFlavorATT
3371} x86DisassemblyFlavor;
3372
3373static OptionEnumValueElement
3374g_x86_dis_flavor_value_types[] =
3375{
3376 { eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3377 { eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
3378 { eX86DisFlavorATT, "att", "AT&T disassembler flavor."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003379 { 0, nullptr, nullptr }
Jim Ingham0f063ba2013-03-02 00:26:47 +00003380};
3381
Enrico Granata397ddd52013-05-21 20:13:34 +00003382static OptionEnumValueElement
Daniel Malead79ae052013-08-07 21:54:09 +00003383g_hex_immediate_style_values[] =
3384{
3385 { Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
3386 { Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003387 { 0, nullptr, nullptr }
Daniel Malead79ae052013-08-07 21:54:09 +00003388};
3389
3390static OptionEnumValueElement
Enrico Granata397ddd52013-05-21 20:13:34 +00003391g_load_script_from_sym_file_values[] =
3392{
3393 { eLoadScriptFromSymFileTrue, "true", "Load debug scripts inside symbol files"},
3394 { eLoadScriptFromSymFileFalse, "false", "Do not load debug scripts inside symbol files."},
3395 { eLoadScriptFromSymFileWarn, "warn", "Warn about debug scripts inside symbol files but do not load them."},
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003396 { 0, nullptr, nullptr }
Enrico Granata397ddd52013-05-21 20:13:34 +00003397};
3398
Greg Claytonfd814c52013-08-13 01:42:25 +00003399static OptionEnumValueElement
Jason Molenda878ae012016-02-19 00:05:17 +00003400g_load_current_working_dir_lldbinit_values[] =
3401{
3402 { eLoadCWDlldbinitTrue, "true", "Load .lldbinit files from current directory"},
3403 { eLoadCWDlldbinitFalse, "false", "Do not load .lldbinit files from current directory"},
3404 { eLoadCWDlldbinitWarn, "warn", "Warn about loading .lldbinit files from current directory"},
3405 { 0, nullptr, nullptr }
3406};
3407
3408static OptionEnumValueElement
Greg Claytonfd814c52013-08-13 01:42:25 +00003409g_memory_module_load_level_values[] =
3410{
Greg Clayton86eac942013-08-13 21:32:34 +00003411 { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."},
Greg Claytonfd814c52013-08-13 01:42:25 +00003412 { eMemoryModuleLoadLevelPartial, "partial" , "Load partial information when loading modules from memory. Currently this setting loads sections and function bounds."},
3413 { 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 +00003414 { 0, nullptr, nullptr }
Greg Claytonfd814c52013-08-13 01:42:25 +00003415};
3416
Greg Clayton67cc0632012-08-22 17:17:09 +00003417static PropertyDefinition
3418g_properties[] =
Caroline Ticedaccaa92010-09-20 20:44:43 +00003419{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003420 { "default-arch" , OptionValue::eTypeArch , true , 0 , nullptr, nullptr, "Default architecture to choose, when there's a choice." },
3421 { "move-to-nearest-code" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Move breakpoints to nearest code." },
3422 { "language" , OptionValue::eTypeLanguage , false, eLanguageTypeUnknown , nullptr, nullptr, "The language to use when interpreting expressions entered in commands." },
3423 { "expr-prefix" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "Path to a file containing expressions to be prepended to all expressions." },
3424 { "prefer-dynamic-value" , OptionValue::eTypeEnum , false, eDynamicDontRunTarget , nullptr, g_dynamic_value_types, "Should printed values be shown as their dynamic value." },
3425 { "enable-synthetic-value" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Should synthetic values be used by default whenever available." },
3426 { "skip-prologue" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Skip function prologues when setting breakpoints by name." },
3427 { "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 +00003428 "where it exists on the current system. It consists of an array of duples, the first element of each duple is "
3429 "some part (starting at the root) of the path to the file when it was built, "
3430 "and the second is where the remainder of the original build hierarchy is rooted on the local system. "
3431 "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 +00003432 { "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." },
3433 { "debug-file-search-paths" , OptionValue::eTypeFileSpecList, false, 0 , nullptr, nullptr, "List of directories to be searched when locating debug symbol files." },
3434 { "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 +00003435 { "auto-import-clang-modules" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Automatically load Clang modules referred to by the program." },
Jim Inghama1e541b2016-03-25 01:57:14 +00003436 { "auto-apply-fixits" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Automatically apply fixit hints to expressions." },
Jim Inghame5ee6f02016-03-29 22:00:08 +00003437 { "notify-about-fixits" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Print the fixed expression text." },
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003438 { "max-children-count" , OptionValue::eTypeSInt64 , false, 256 , nullptr, nullptr, "Maximum number of children to expand in any level of depth." },
3439 { "max-string-summary-length" , OptionValue::eTypeSInt64 , false, 1024 , nullptr, nullptr, "Maximum number of characters to show when using %s in summary strings." },
3440 { "max-memory-read-size" , OptionValue::eTypeSInt64 , false, 1024 , nullptr, nullptr, "Maximum number of bytes that 'memory read' will fetch before --force must be specified." },
3441 { "breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Consult the platform module avoid list when setting non-module specific breakpoints." },
3442 { "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." },
3443 { "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." },
3444 { "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." },
3445 { "inherit-env" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Inherit the environment from the process that is running LLDB." },
3446 { "input-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for reading its standard input." },
3447 { "output-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for writing its standard output." },
3448 { "error-path" , OptionValue::eTypeFileSpec , false, 0 , nullptr, nullptr, "The file/path to be used by the executable program for writing its standard error." },
3449 { "detach-on-error" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "debugserver will detach (rather than killing) a process if it loses connection with lldb." },
3450 { "disable-aslr" , OptionValue::eTypeBoolean , false, true , nullptr, nullptr, "Disable Address Space Layout Randomization (ASLR)" },
3451 { "disable-stdio" , OptionValue::eTypeBoolean , false, false , nullptr, nullptr, "Disable stdin/stdout for process (e.g. for a GUI application)" },
3452 { "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 +00003453 "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 +00003454 "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 +00003455 "Sometimes code might #include implementation files and cause inlined breakpoint locations in inlined implementation files. "
Todd Fialaad6eee62014-09-24 19:59:13 +00003456 "Always checking for inlined breakpoint locations can be expensive (memory and time), so if you have a project with many headers "
3457 "and find that setting breakpoints is slow, then you can change this setting to headers. "
3458 "This setting allows you to control exactly which strategy is used when setting "
Greg Clayton1f746072012-08-29 21:13:06 +00003459 "file and line breakpoints." },
Jim Ingham0f063ba2013-03-02 00:26:47 +00003460 // 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 +00003461 { "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." },
3462 { "use-hex-immediates" , OptionValue::eTypeBoolean , false, true, nullptr, nullptr, "Show immediates in disassembly as hexadecimal." },
3463 { "hex-immediate-style" , OptionValue::eTypeEnum , false, Disassembler::eHexStyleC, nullptr, g_hex_immediate_style_values, "Which style to use for printing hexadecimal disassembly values." },
3464 { "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." },
3465 { "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 +00003466 { "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 +00003467 { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, nullptr, g_memory_module_load_level_values,
Greg Clayton86eac942013-08-13 21:32:34 +00003468 "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. "
3469 "This setting helps users control how much information gets loaded when loading modules from memory."
3470 "'complete' is the default value for this setting which will load all sections and symbols by reading them from memory (slowest, most accurate). "
3471 "'partial' will load sections and attempt to find function bounds without downloading the symbol table (faster, still accurate, missing symbol names). "
3472 "'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 +00003473 { "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." },
3474 { "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." },
3475 { "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." },
3476 { "non-stop-mode" , OptionValue::eTypeBoolean , false, 0, nullptr, nullptr, "Disable lock-step debugging, instead control threads independently." },
3477 { nullptr , OptionValue::eTypeInvalid , false, 0 , nullptr, nullptr, nullptr }
Greg Clayton67cc0632012-08-22 17:17:09 +00003478};
Enrico Granata560558e2015-02-11 02:35:39 +00003479
Greg Clayton67cc0632012-08-22 17:17:09 +00003480enum
Caroline Ticedaccaa92010-09-20 20:44:43 +00003481{
Greg Clayton67cc0632012-08-22 17:17:09 +00003482 ePropertyDefaultArch,
Ilia K055ad9b2015-05-18 13:41:01 +00003483 ePropertyMoveToNearestCode,
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003484 ePropertyLanguage,
Greg Clayton67cc0632012-08-22 17:17:09 +00003485 ePropertyExprPrefix,
3486 ePropertyPreferDynamic,
3487 ePropertyEnableSynthetic,
3488 ePropertySkipPrologue,
3489 ePropertySourceMap,
3490 ePropertyExecutableSearchPaths,
Michael Sartaina7499c92013-07-01 19:45:50 +00003491 ePropertyDebugFileSearchPaths,
Sean Callanan85054342015-04-03 15:39:47 +00003492 ePropertyClangModuleSearchPaths,
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003493 ePropertyAutoImportClangModules,
Jim Inghama1e541b2016-03-25 01:57:14 +00003494 ePropertyAutoApplyFixIts,
Jim Inghame5ee6f02016-03-29 22:00:08 +00003495 ePropertyNotifyAboutFixIts,
Greg Clayton67cc0632012-08-22 17:17:09 +00003496 ePropertyMaxChildrenCount,
3497 ePropertyMaxSummaryLength,
Enrico Granatad325bf92013-06-04 22:54:16 +00003498 ePropertyMaxMemReadSize,
Greg Clayton67cc0632012-08-22 17:17:09 +00003499 ePropertyBreakpointUseAvoidList,
Greg Clayton45392552012-10-17 22:57:12 +00003500 ePropertyArg0,
Greg Clayton67cc0632012-08-22 17:17:09 +00003501 ePropertyRunArgs,
3502 ePropertyEnvVars,
3503 ePropertyInheritEnv,
3504 ePropertyInputPath,
3505 ePropertyOutputPath,
3506 ePropertyErrorPath,
Jim Ingham106d0282014-06-25 02:32:56 +00003507 ePropertyDetachOnError,
Greg Clayton67cc0632012-08-22 17:17:09 +00003508 ePropertyDisableASLR,
Greg Clayton1f746072012-08-29 21:13:06 +00003509 ePropertyDisableSTDIO,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003510 ePropertyInlineStrategy,
Jim Ingham17d023f2013-03-13 17:58:04 +00003511 ePropertyDisassemblyFlavor,
Daniel Malead79ae052013-08-07 21:54:09 +00003512 ePropertyUseHexImmediates,
3513 ePropertyHexImmediateStyle,
Enrico Granata2ea43cd2013-05-13 17:03:52 +00003514 ePropertyUseFastStepping,
Enrico Granata97303392013-05-21 00:00:30 +00003515 ePropertyLoadScriptFromSymbolFile,
Jason Molenda878ae012016-02-19 00:05:17 +00003516 ePropertyLoadCWDlldbinitFile,
Greg Claytonfb6621e2013-12-06 21:59:52 +00003517 ePropertyMemoryModuleLoadLevel,
Jason Molendaa4bea722014-02-14 05:06:49 +00003518 ePropertyDisplayExpressionsInCrashlogs,
Enrico Granata560558e2015-02-11 02:35:39 +00003519 ePropertyTrapHandlerNames,
Ewan Crawford78baa192015-05-13 09:18:18 +00003520 ePropertyDisplayRuntimeSupportValues,
3521 ePropertyNonStopModeEnabled
Greg Clayton67cc0632012-08-22 17:17:09 +00003522};
Caroline Ticedaccaa92010-09-20 20:44:43 +00003523
Greg Clayton67cc0632012-08-22 17:17:09 +00003524class TargetOptionValueProperties : public OptionValueProperties
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00003525{
Greg Clayton67cc0632012-08-22 17:17:09 +00003526public:
3527 TargetOptionValueProperties (const ConstString &name) :
3528 OptionValueProperties (name),
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003529 m_target(nullptr),
Greg Clayton67cc0632012-08-22 17:17:09 +00003530 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003531 {
Caroline Ticedaccaa92010-09-20 20:44:43 +00003532 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003533
Greg Clayton67cc0632012-08-22 17:17:09 +00003534 // This constructor is used when creating TargetOptionValueProperties when it
3535 // is part of a new lldb_private::Target instance. It will copy all current
3536 // global property values as needed
3537 TargetOptionValueProperties (Target *target, const TargetPropertiesSP &target_properties_sp) :
3538 OptionValueProperties(*target_properties_sp->GetValueProperties()),
3539 m_target (target),
3540 m_got_host_env (false)
Caroline Ticedaccaa92010-09-20 20:44:43 +00003541 {
Greg Clayton67cc0632012-08-22 17:17:09 +00003542 }
3543
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003544 const Property *
3545 GetPropertyAtIndex(const ExecutionContext *exe_ctx, bool will_modify, uint32_t idx) const override
Greg Clayton67cc0632012-08-22 17:17:09 +00003546 {
Bruce Mitchenerd93c4a32014-07-01 21:22:11 +00003547 // When getting the value for a key from the target options, we will always
Greg Clayton67cc0632012-08-22 17:17:09 +00003548 // try and grab the setting from the current target if there is one. Else we just
3549 // use the one from this instance.
3550 if (idx == ePropertyEnvVars)
3551 GetHostEnvironmentIfNeeded ();
3552
3553 if (exe_ctx)
3554 {
3555 Target *target = exe_ctx->GetTargetPtr();
3556 if (target)
3557 {
3558 TargetOptionValueProperties *target_properties = static_cast<TargetOptionValueProperties *>(target->GetValueProperties().get());
3559 if (this != target_properties)
3560 return target_properties->ProtectedGetPropertyAtIndex (idx);
3561 }
3562 }
3563 return ProtectedGetPropertyAtIndex (idx);
3564 }
Enrico Granata84a53df2013-05-20 22:29:23 +00003565
3566 lldb::TargetSP
3567 GetTargetSP ()
3568 {
3569 return m_target->shared_from_this();
3570 }
3571
Greg Clayton67cc0632012-08-22 17:17:09 +00003572protected:
Greg Clayton67cc0632012-08-22 17:17:09 +00003573 void
3574 GetHostEnvironmentIfNeeded () const
3575 {
3576 if (!m_got_host_env)
3577 {
3578 if (m_target)
3579 {
3580 m_got_host_env = true;
3581 const uint32_t idx = ePropertyInheritEnv;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003582 if (GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0))
Greg Clayton67cc0632012-08-22 17:17:09 +00003583 {
3584 PlatformSP platform_sp (m_target->GetPlatform());
3585 if (platform_sp)
3586 {
3587 StringList env;
3588 if (platform_sp->GetEnvironment(env))
3589 {
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003590 OptionValueDictionary *env_dict = GetPropertyAtIndexAsOptionValueDictionary(nullptr, ePropertyEnvVars);
Greg Clayton67cc0632012-08-22 17:17:09 +00003591 if (env_dict)
3592 {
3593 const bool can_replace = false;
3594 const size_t envc = env.GetSize();
3595 for (size_t idx=0; idx<envc; idx++)
3596 {
3597 const char *env_entry = env.GetStringAtIndex (idx);
3598 if (env_entry)
3599 {
3600 const char *equal_pos = ::strchr(env_entry, '=');
3601 ConstString key;
3602 // It is ok to have environment variables with no values
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003603 const char *value = nullptr;
Greg Clayton67cc0632012-08-22 17:17:09 +00003604 if (equal_pos)
3605 {
3606 key.SetCStringWithLength(env_entry, equal_pos - env_entry);
3607 if (equal_pos[1])
3608 value = equal_pos + 1;
3609 }
3610 else
3611 {
3612 key.SetCString(env_entry);
3613 }
3614 // Don't allow existing keys to be replaced with ones we get from the platform environment
3615 env_dict->SetValueForKey(key, OptionValueSP(new OptionValueString(value)), can_replace);
3616 }
3617 }
3618 }
3619 }
3620 }
3621 }
3622 }
3623 }
3624 }
3625 Target *m_target;
3626 mutable bool m_got_host_env;
3627};
3628
Greg Claytonfbb76342013-11-20 21:07:01 +00003629//----------------------------------------------------------------------
3630// TargetProperties
3631//----------------------------------------------------------------------
Greg Clayton67cc0632012-08-22 17:17:09 +00003632TargetProperties::TargetProperties (Target *target) :
Ilia K8f37ca52015-02-13 14:31:06 +00003633 Properties (),
3634 m_launch_info ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003635{
3636 if (target)
3637 {
3638 m_collection_sp.reset (new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003639
3640 // Set callbacks to update launch_info whenever "settins set" updated any of these properties
3641 m_collection_sp->SetValueChangedCallback(ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3642 m_collection_sp->SetValueChangedCallback(ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3643 m_collection_sp->SetValueChangedCallback(ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3644 m_collection_sp->SetValueChangedCallback(ePropertyInputPath, TargetProperties::InputPathValueChangedCallback, this);
3645 m_collection_sp->SetValueChangedCallback(ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback, this);
3646 m_collection_sp->SetValueChangedCallback(ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback, this);
3647 m_collection_sp->SetValueChangedCallback(ePropertyDetachOnError, TargetProperties::DetachOnErrorValueChangedCallback, this);
3648 m_collection_sp->SetValueChangedCallback(ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback, this);
3649 m_collection_sp->SetValueChangedCallback(ePropertyDisableSTDIO, TargetProperties::DisableSTDIOValueChangedCallback, this);
3650
3651 // Update m_launch_info once it was created
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003652 Arg0ValueChangedCallback(this, nullptr);
3653 RunArgsValueChangedCallback(this, nullptr);
3654 //EnvVarsValueChangedCallback(this, nullptr); // FIXME: cause segfault in Target::GetPlatform()
3655 InputPathValueChangedCallback(this, nullptr);
3656 OutputPathValueChangedCallback(this, nullptr);
3657 ErrorPathValueChangedCallback(this, nullptr);
3658 DetachOnErrorValueChangedCallback(this, nullptr);
3659 DisableASLRValueChangedCallback(this, nullptr);
3660 DisableSTDIOValueChangedCallback(this, nullptr);
Caroline Ticedaccaa92010-09-20 20:44:43 +00003661 }
3662 else
Greg Clayton67cc0632012-08-22 17:17:09 +00003663 {
3664 m_collection_sp.reset (new TargetOptionValueProperties(ConstString("target")));
3665 m_collection_sp->Initialize(g_properties);
3666 m_collection_sp->AppendProperty(ConstString("process"),
3667 ConstString("Settings specify to processes."),
3668 true,
3669 Process::GetGlobalProperties()->GetValueProperties());
3670 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003671}
3672
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003673TargetProperties::~TargetProperties() = default;
3674
Greg Clayton67cc0632012-08-22 17:17:09 +00003675ArchSpec
3676TargetProperties::GetDefaultArchitecture () const
3677{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003678 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(nullptr, ePropertyDefaultArch);
Greg Clayton67cc0632012-08-22 17:17:09 +00003679 if (value)
3680 return value->GetCurrentValue();
3681 return ArchSpec();
3682}
3683
3684void
3685TargetProperties::SetDefaultArchitecture (const ArchSpec& arch)
3686{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003687 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(nullptr, ePropertyDefaultArch);
Greg Clayton67cc0632012-08-22 17:17:09 +00003688 if (value)
3689 return value->SetCurrentValue(arch, true);
3690}
3691
Ilia K055ad9b2015-05-18 13:41:01 +00003692bool
3693TargetProperties::GetMoveToNearestCode() const
3694{
3695 const uint32_t idx = ePropertyMoveToNearestCode;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003696 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Ilia K055ad9b2015-05-18 13:41:01 +00003697}
3698
Greg Clayton67cc0632012-08-22 17:17:09 +00003699lldb::DynamicValueType
3700TargetProperties::GetPreferDynamicValue() const
3701{
3702 const uint32_t idx = ePropertyPreferDynamic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003703 return (lldb::DynamicValueType)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003704}
3705
3706bool
Greg Clayton15484402015-05-15 18:40:24 +00003707TargetProperties::SetPreferDynamicValue (lldb::DynamicValueType d)
3708{
3709 const uint32_t idx = ePropertyPreferDynamic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003710 return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, d);
Greg Clayton15484402015-05-15 18:40:24 +00003711}
3712
Greg Clayton15484402015-05-15 18:40:24 +00003713bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003714TargetProperties::GetDisableASLR () const
3715{
3716 const uint32_t idx = ePropertyDisableASLR;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003717 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003718}
3719
3720void
3721TargetProperties::SetDisableASLR (bool b)
3722{
3723 const uint32_t idx = ePropertyDisableASLR;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003724 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003725}
3726
3727bool
Jim Ingham106d0282014-06-25 02:32:56 +00003728TargetProperties::GetDetachOnError () const
3729{
3730 const uint32_t idx = ePropertyDetachOnError;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003731 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham106d0282014-06-25 02:32:56 +00003732}
3733
3734void
3735TargetProperties::SetDetachOnError (bool b)
3736{
3737 const uint32_t idx = ePropertyDetachOnError;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003738 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Jim Ingham106d0282014-06-25 02:32:56 +00003739}
3740
3741bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003742TargetProperties::GetDisableSTDIO () const
3743{
3744 const uint32_t idx = ePropertyDisableSTDIO;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003745 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003746}
3747
3748void
3749TargetProperties::SetDisableSTDIO (bool b)
3750{
3751 const uint32_t idx = ePropertyDisableSTDIO;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003752 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003753}
3754
Jim Ingham0f063ba2013-03-02 00:26:47 +00003755const char *
3756TargetProperties::GetDisassemblyFlavor () const
3757{
3758 const uint32_t idx = ePropertyDisassemblyFlavor;
3759 const char *return_value;
3760
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003761 x86DisassemblyFlavor flavor_value = (x86DisassemblyFlavor) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Jim Ingham0f063ba2013-03-02 00:26:47 +00003762 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3763 return return_value;
3764}
3765
Greg Clayton1f746072012-08-29 21:13:06 +00003766InlineStrategy
3767TargetProperties::GetInlineStrategy () const
3768{
3769 const uint32_t idx = ePropertyInlineStrategy;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003770 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton1f746072012-08-29 21:13:06 +00003771}
3772
Greg Clayton45392552012-10-17 22:57:12 +00003773const char *
3774TargetProperties::GetArg0 () const
3775{
3776 const uint32_t idx = ePropertyArg0;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003777 return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, nullptr);
Greg Clayton45392552012-10-17 22:57:12 +00003778}
3779
3780void
3781TargetProperties::SetArg0 (const char *arg)
3782{
3783 const uint32_t idx = ePropertyArg0;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003784 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, arg);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003785 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003786}
3787
Greg Clayton67cc0632012-08-22 17:17:09 +00003788bool
3789TargetProperties::GetRunArguments (Args &args) const
3790{
3791 const uint32_t idx = ePropertyRunArgs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003792 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Greg Clayton67cc0632012-08-22 17:17:09 +00003793}
3794
3795void
3796TargetProperties::SetRunArguments (const Args &args)
3797{
3798 const uint32_t idx = ePropertyRunArgs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003799 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003800 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003801}
3802
3803size_t
3804TargetProperties::GetEnvironmentAsArgs (Args &env) const
3805{
3806 const uint32_t idx = ePropertyEnvVars;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003807 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, env);
Greg Clayton67cc0632012-08-22 17:17:09 +00003808}
3809
Ilia K8f37ca52015-02-13 14:31:06 +00003810void
3811TargetProperties::SetEnvironmentFromArgs (const Args &env)
3812{
3813 const uint32_t idx = ePropertyEnvVars;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003814 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, env);
Greg Clayton896e0ec2015-03-26 00:15:24 +00003815 m_launch_info.GetEnvironmentEntries() = env;
Ilia K8f37ca52015-02-13 14:31:06 +00003816}
3817
Greg Clayton67cc0632012-08-22 17:17:09 +00003818bool
3819TargetProperties::GetSkipPrologue() const
3820{
3821 const uint32_t idx = ePropertySkipPrologue;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003822 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003823}
3824
3825PathMappingList &
3826TargetProperties::GetSourcePathMap () const
3827{
3828 const uint32_t idx = ePropertySourceMap;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003829 OptionValuePathMappings *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(nullptr, false, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003830 assert(option_value);
3831 return option_value->GetCurrentValue();
3832}
3833
3834FileSpecList &
Greg Clayton6920b522012-08-22 18:39:03 +00003835TargetProperties::GetExecutableSearchPaths ()
Greg Clayton67cc0632012-08-22 17:17:09 +00003836{
3837 const uint32_t idx = ePropertyExecutableSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003838 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003839 assert(option_value);
3840 return option_value->GetCurrentValue();
3841}
3842
Michael Sartaina7499c92013-07-01 19:45:50 +00003843FileSpecList &
3844TargetProperties::GetDebugFileSearchPaths ()
3845{
3846 const uint32_t idx = ePropertyDebugFileSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003847 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Michael Sartaina7499c92013-07-01 19:45:50 +00003848 assert(option_value);
3849 return option_value->GetCurrentValue();
3850}
3851
Sean Callanan85054342015-04-03 15:39:47 +00003852FileSpecList &
3853TargetProperties::GetClangModuleSearchPaths ()
3854{
3855 const uint32_t idx = ePropertyClangModuleSearchPaths;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003856 OptionValueFileSpecList *option_value = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr, false, idx);
Sean Callanan85054342015-04-03 15:39:47 +00003857 assert(option_value);
3858 return option_value->GetCurrentValue();
3859}
3860
Greg Clayton67cc0632012-08-22 17:17:09 +00003861bool
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003862TargetProperties::GetEnableAutoImportClangModules() const
3863{
3864 const uint32_t idx = ePropertyAutoImportClangModules;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003865 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003866}
3867
3868bool
Jim Inghama1e541b2016-03-25 01:57:14 +00003869TargetProperties::GetEnableAutoApplyFixIts() const
3870{
3871 const uint32_t idx = ePropertyAutoApplyFixIts;
3872 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
3873}
3874
3875bool
Jim Inghame5ee6f02016-03-29 22:00:08 +00003876TargetProperties::GetEnableNotifyAboutFixIts() const
3877{
3878 const uint32_t idx = ePropertyNotifyAboutFixIts;
3879 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
3880}
3881
3882bool
Greg Clayton67cc0632012-08-22 17:17:09 +00003883TargetProperties::GetEnableSyntheticValue () const
3884{
3885 const uint32_t idx = ePropertyEnableSynthetic;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003886 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003887}
3888
3889uint32_t
3890TargetProperties::GetMaximumNumberOfChildrenToDisplay() const
3891{
3892 const uint32_t idx = ePropertyMaxChildrenCount;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003893 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003894}
3895
3896uint32_t
3897TargetProperties::GetMaximumSizeOfStringSummary() const
3898{
3899 const uint32_t idx = ePropertyMaxSummaryLength;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003900 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003901}
3902
Enrico Granatad325bf92013-06-04 22:54:16 +00003903uint32_t
3904TargetProperties::GetMaximumMemReadSize () const
3905{
3906 const uint32_t idx = ePropertyMaxMemReadSize;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003907 return m_collection_sp->GetPropertyAtIndexAsSInt64(nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granatad325bf92013-06-04 22:54:16 +00003908}
3909
Greg Clayton67cc0632012-08-22 17:17:09 +00003910FileSpec
3911TargetProperties::GetStandardInputPath () const
3912{
3913 const uint32_t idx = ePropertyInputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003914 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003915}
3916
3917void
3918TargetProperties::SetStandardInputPath (const char *p)
3919{
3920 const uint32_t idx = ePropertyInputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003921 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003922}
3923
3924FileSpec
3925TargetProperties::GetStandardOutputPath () const
3926{
3927 const uint32_t idx = ePropertyOutputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003928 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003929}
3930
3931void
3932TargetProperties::SetStandardOutputPath (const char *p)
3933{
3934 const uint32_t idx = ePropertyOutputPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003935 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003936}
3937
3938FileSpec
3939TargetProperties::GetStandardErrorPath () const
3940{
3941 const uint32_t idx = ePropertyErrorPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003942 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003943}
3944
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003945LanguageType
3946TargetProperties::GetLanguage () const
3947{
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003948 OptionValueLanguage *value = m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage(nullptr, ePropertyLanguage);
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003949 if (value)
3950 return value->GetCurrentValue();
3951 return LanguageType();
3952}
3953
Greg Clayton6920b522012-08-22 18:39:03 +00003954const char *
3955TargetProperties::GetExpressionPrefixContentsAsCString ()
3956{
3957 const uint32_t idx = ePropertyExprPrefix;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003958 OptionValueFileSpec *file = m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false, idx);
Greg Clayton6920b522012-08-22 18:39:03 +00003959 if (file)
Jim Ingham1e4f4252012-08-22 21:21:16 +00003960 {
Greg Clayton0b0b5122012-08-30 18:15:10 +00003961 const bool null_terminate = true;
3962 DataBufferSP data_sp(file->GetFileContents(null_terminate));
Jim Ingham1e4f4252012-08-22 21:21:16 +00003963 if (data_sp)
3964 return (const char *) data_sp->GetBytes();
3965 }
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003966 return nullptr;
Greg Clayton6920b522012-08-22 18:39:03 +00003967}
3968
Greg Clayton67cc0632012-08-22 17:17:09 +00003969void
3970TargetProperties::SetStandardErrorPath (const char *p)
3971{
3972 const uint32_t idx = ePropertyErrorPath;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003973 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, p);
Greg Clayton67cc0632012-08-22 17:17:09 +00003974}
3975
3976bool
3977TargetProperties::GetBreakpointsConsultPlatformAvoidList ()
3978{
3979 const uint32_t idx = ePropertyBreakpointUseAvoidList;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003980 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003981}
3982
Jim Ingham17d023f2013-03-13 17:58:04 +00003983bool
Daniel Malead79ae052013-08-07 21:54:09 +00003984TargetProperties::GetUseHexImmediates () const
3985{
3986 const uint32_t idx = ePropertyUseHexImmediates;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003987 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Daniel Malead79ae052013-08-07 21:54:09 +00003988}
3989
3990bool
Jim Ingham17d023f2013-03-13 17:58:04 +00003991TargetProperties::GetUseFastStepping () const
3992{
3993 const uint32_t idx = ePropertyUseFastStepping;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00003994 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham17d023f2013-03-13 17:58:04 +00003995}
3996
Greg Claytonfb6621e2013-12-06 21:59:52 +00003997bool
3998TargetProperties::GetDisplayExpressionsInCrashlogs () const
3999{
4000 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004001 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Claytonfb6621e2013-12-06 21:59:52 +00004002}
4003
Enrico Granata397ddd52013-05-21 20:13:34 +00004004LoadScriptFromSymFile
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004005TargetProperties::GetLoadScriptFromSymbolFile () const
4006{
4007 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004008 return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004009}
4010
Jason Molenda878ae012016-02-19 00:05:17 +00004011LoadCWDlldbinitFile
4012TargetProperties::GetLoadCWDlldbinitFile () const
4013{
4014 const uint32_t idx = ePropertyLoadCWDlldbinitFile;
4015 return (LoadCWDlldbinitFile) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
4016}
4017
Daniel Malead79ae052013-08-07 21:54:09 +00004018Disassembler::HexImmediateStyle
4019TargetProperties::GetHexImmediateStyle () const
4020{
4021 const uint32_t idx = ePropertyHexImmediateStyle;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004022 return (Disassembler::HexImmediateStyle)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Daniel Malead79ae052013-08-07 21:54:09 +00004023}
4024
Greg Claytonfd814c52013-08-13 01:42:25 +00004025MemoryModuleLoadLevel
4026TargetProperties::GetMemoryModuleLoadLevel() const
4027{
4028 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004029 return (MemoryModuleLoadLevel)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value);
Greg Claytonfd814c52013-08-13 01:42:25 +00004030}
4031
Jason Molendaa4bea722014-02-14 05:06:49 +00004032bool
4033TargetProperties::GetUserSpecifiedTrapHandlerNames (Args &args) const
4034{
4035 const uint32_t idx = ePropertyTrapHandlerNames;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004036 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00004037}
Greg Claytonfd814c52013-08-13 01:42:25 +00004038
Jason Molendaa4bea722014-02-14 05:06:49 +00004039void
4040TargetProperties::SetUserSpecifiedTrapHandlerNames (const Args &args)
4041{
4042 const uint32_t idx = ePropertyTrapHandlerNames;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004043 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00004044}
Greg Clayton67cc0632012-08-22 17:17:09 +00004045
Enrico Granata560558e2015-02-11 02:35:39 +00004046bool
4047TargetProperties::GetDisplayRuntimeSupportValues () const
4048{
4049 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004050 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Enrico Granata560558e2015-02-11 02:35:39 +00004051}
4052
4053void
4054TargetProperties::SetDisplayRuntimeSupportValues (bool b)
4055{
4056 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004057 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Enrico Granata560558e2015-02-11 02:35:39 +00004058}
4059
Ewan Crawford78baa192015-05-13 09:18:18 +00004060bool
4061TargetProperties::GetNonStopModeEnabled () const
4062{
4063 const uint32_t idx = ePropertyNonStopModeEnabled;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004064 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Ewan Crawford78baa192015-05-13 09:18:18 +00004065}
4066
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004067void
4068TargetProperties::SetNonStopModeEnabled (bool b)
4069{
4070 const uint32_t idx = ePropertyNonStopModeEnabled;
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004071 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004072}
4073
Ilia K8f37ca52015-02-13 14:31:06 +00004074const ProcessLaunchInfo &
Ilia Kcc39d3f2015-02-13 17:07:55 +00004075TargetProperties::GetProcessLaunchInfo ()
Ilia K8f37ca52015-02-13 14:31:06 +00004076{
Ilia Kcc39d3f2015-02-13 17:07:55 +00004077 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
Ilia K8f37ca52015-02-13 14:31:06 +00004078 return m_launch_info;
4079}
4080
4081void
4082TargetProperties::SetProcessLaunchInfo(const ProcessLaunchInfo &launch_info)
4083{
4084 m_launch_info = launch_info;
4085 SetArg0(launch_info.GetArg0());
4086 SetRunArguments(launch_info.GetArguments());
4087 SetEnvironmentFromArgs(launch_info.GetEnvironmentEntries());
4088 const FileAction *input_file_action = launch_info.GetFileActionForFD(STDIN_FILENO);
4089 if (input_file_action)
4090 {
4091 const char *input_path = input_file_action->GetPath();
4092 if (input_path)
4093 SetStandardInputPath(input_path);
4094 }
4095 const FileAction *output_file_action = launch_info.GetFileActionForFD(STDOUT_FILENO);
4096 if (output_file_action)
4097 {
4098 const char *output_path = output_file_action->GetPath();
4099 if (output_path)
4100 SetStandardOutputPath(output_path);
4101 }
4102 const FileAction *error_file_action = launch_info.GetFileActionForFD(STDERR_FILENO);
4103 if (error_file_action)
4104 {
4105 const char *error_path = error_file_action->GetPath();
4106 if (error_path)
4107 SetStandardErrorPath(error_path);
4108 }
4109 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
4110 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4111 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
4112}
4113
4114void
4115TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr, OptionValue *)
4116{
4117 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4118 this_->m_launch_info.SetArg0(this_->GetArg0());
4119}
4120
4121void
4122TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr, OptionValue *)
4123{
4124 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4125 Args args;
4126 if (this_->GetRunArguments(args))
4127 this_->m_launch_info.GetArguments() = args;
4128}
4129
4130void
4131TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr, OptionValue *)
4132{
4133 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4134 Args args;
4135 if (this_->GetEnvironmentAsArgs(args))
4136 this_->m_launch_info.GetEnvironmentEntries() = args;
4137}
4138
4139void
4140TargetProperties::InputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4141{
4142 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004143 this_->m_launch_info.AppendOpenFileAction(STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004144}
4145
4146void
4147TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4148{
4149 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004150 this_->m_launch_info.AppendOpenFileAction(STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004151}
4152
4153void
4154TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr, OptionValue *)
4155{
4156 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
Chaoren Lind3173f32015-05-29 19:52:29 +00004157 this_->m_launch_info.AppendOpenFileAction(STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004158}
4159
4160void
4161TargetProperties::DetachOnErrorValueChangedCallback(void *target_property_ptr, OptionValue *)
4162{
4163 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4164 if (this_->GetDetachOnError())
4165 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4166 else
4167 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
4168}
4169
4170void
4171TargetProperties::DisableASLRValueChangedCallback(void *target_property_ptr, OptionValue *)
4172{
4173 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4174 if (this_->GetDisableASLR())
4175 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4176 else
4177 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
4178}
4179
4180void
4181TargetProperties::DisableSTDIOValueChangedCallback(void *target_property_ptr, OptionValue *)
4182{
4183 TargetProperties *this_ = reinterpret_cast<TargetProperties *>(target_property_ptr);
4184 if (this_->GetDisableSTDIO())
4185 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4186 else
4187 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
4188}
Ilia Keb2c19a2015-03-10 21:59:55 +00004189
4190//----------------------------------------------------------------------
4191// Target::TargetEventData
4192//----------------------------------------------------------------------
4193
4194Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp) :
4195 EventData (),
4196 m_target_sp (target_sp),
4197 m_module_list ()
4198{
4199}
4200
4201Target::TargetEventData::TargetEventData (const lldb::TargetSP &target_sp, const ModuleList &module_list) :
4202 EventData (),
4203 m_target_sp (target_sp),
4204 m_module_list (module_list)
4205{
4206}
4207
Eugene Zelenko8f30a652015-10-23 18:39:37 +00004208Target::TargetEventData::~TargetEventData() = default;
Ilia Keb2c19a2015-03-10 21:59:55 +00004209
4210const ConstString &
4211Target::TargetEventData::GetFlavorString ()
4212{
4213 static ConstString g_flavor ("Target::TargetEventData");
4214 return g_flavor;
4215}
4216
4217void
4218Target::TargetEventData::Dump (Stream *s) const
4219{
Oleksiy Vyalov8b85b032016-01-19 17:54:47 +00004220 for (size_t i = 0; i < m_module_list.GetSize(); ++i)
4221 {
4222 if (i != 0)
4223 *s << ", ";
4224 m_module_list.GetModuleAtIndex(i)->GetDescription(s, lldb::eDescriptionLevelBrief);
4225 }
Ilia Keb2c19a2015-03-10 21:59:55 +00004226}
4227
4228const Target::TargetEventData *
4229Target::TargetEventData::GetEventDataFromEvent (const Event *event_ptr)
4230{
4231 if (event_ptr)
4232 {
4233 const EventData *event_data = event_ptr->GetData();
4234 if (event_data && event_data->GetFlavor() == TargetEventData::GetFlavorString())
4235 return static_cast <const TargetEventData *> (event_ptr->GetData());
4236 }
Eugene Zelenkoe2ccd332015-12-11 19:52:15 +00004237 return nullptr;
Ilia Keb2c19a2015-03-10 21:59:55 +00004238}
4239
4240TargetSP
4241Target::TargetEventData::GetTargetFromEvent (const Event *event_ptr)
4242{
4243 TargetSP target_sp;
4244 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4245 if (event_data)
4246 target_sp = event_data->m_target_sp;
4247 return target_sp;
4248}
4249
4250ModuleList
4251Target::TargetEventData::GetModuleListFromEvent (const Event *event_ptr)
4252{
4253 ModuleList module_list;
4254 const TargetEventData *event_data = GetEventDataFromEvent (event_ptr);
4255 if (event_data)
4256 module_list = event_data->m_module_list;
4257 return module_list;
4258}