blob: bc0bdae0ff34a38301f625dff405bab931674a0d [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- Target.cpp ----------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
Pavel Labath181b8232018-12-14 15:59:49 +00009#include "lldb/Target/Target.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000010#include "Plugins/ExpressionParser/Clang/ClangASTSource.h"
11#include "Plugins/ExpressionParser/Clang/ClangModulesDeclVendor.h"
12#include "Plugins/ExpressionParser/Clang/ClangPersistentVariables.h"
Jim Ingham01f16662016-09-14 19:07:35 +000013#include "lldb/Breakpoint/BreakpointIDList.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Breakpoint/BreakpointResolver.h"
15#include "lldb/Breakpoint/BreakpointResolverAddress.h"
16#include "lldb/Breakpoint/BreakpointResolverFileLine.h"
Jim Ingham969795f2011-09-21 01:17:13 +000017#include "lldb/Breakpoint/BreakpointResolverFileRegex.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Breakpoint/BreakpointResolverName.h"
Jim Ingham3815e702018-09-13 21:35:32 +000019#include "lldb/Breakpoint/BreakpointResolverScripted.h"
Johnny Chen01a67862011-10-14 00:42:25 +000020#include "lldb/Breakpoint/Watchpoint.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000021#include "lldb/Core/Debugger.h"
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Core/Module.h"
23#include "lldb/Core/ModuleSpec.h"
Pavel Labath13e37d42017-10-25 21:05:31 +000024#include "lldb/Core/PluginManager.h"
Jim Ingham3815e702018-09-13 21:35:32 +000025#include "lldb/Core/SearchFilter.h"
Pavel Labath181b8232018-12-14 15:59:49 +000026#include "lldb/Core/Section.h"
Greg Clayton9585fbf2013-03-19 00:20:55 +000027#include "lldb/Core/SourceManager.h"
Greg Clayton44d93782014-01-27 23:43:24 +000028#include "lldb/Core/StreamFile.h"
Jim Ingham3815e702018-09-13 21:35:32 +000029#include "lldb/Core/StructuredDataImpl.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000030#include "lldb/Core/ValueObject.h"
Sean Callanan66810412015-10-19 23:11:07 +000031#include "lldb/Expression/REPL.h"
Jim Ingham151c0322015-09-15 21:13:50 +000032#include "lldb/Expression/UserExpression.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Host/Host.h"
Pavel Labathb6dbe9a2017-07-18 13:14:01 +000034#include "lldb/Host/PosixApi.h"
Jim Ingham9575d842011-03-11 03:53:59 +000035#include "lldb/Interpreter/CommandInterpreter.h"
36#include "lldb/Interpreter/CommandReturnObject.h"
Johnny Chenb90827e2012-06-04 23:19:54 +000037#include "lldb/Interpreter/OptionGroupWatchpoint.h"
Greg Clayton67cc0632012-08-22 17:17:09 +000038#include "lldb/Interpreter/OptionValues.h"
39#include "lldb/Interpreter/Property.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000040#include "lldb/Symbol/ClangASTContext.h"
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000041#include "lldb/Symbol/Function.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000042#include "lldb/Symbol/ObjectFile.h"
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +000043#include "lldb/Symbol/Symbol.h"
Jim Ingham151c0322015-09-15 21:13:50 +000044#include "lldb/Target/Language.h"
Zachary Turnera78bd7f2015-03-03 23:11:11 +000045#include "lldb/Target/LanguageRuntime.h"
46#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Target/Process.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000048#include "lldb/Target/SectionLoadList.h"
Jason Molendab57e4a12013-11-04 09:33:30 +000049#include "lldb/Target/StackFrame.h"
Jason Molendaeef51062013-11-05 03:57:19 +000050#include "lldb/Target/SystemRuntime.h"
Jim Ingham9575d842011-03-11 03:53:59 +000051#include "lldb/Target/Thread.h"
52#include "lldb/Target/ThreadSpec.h"
Pavel Labath181b8232018-12-14 15:59:49 +000053#include "lldb/Utility/Event.h"
Zachary Turner5713a052017-03-22 18:40:07 +000054#include "lldb/Utility/FileSpec.h"
Sean Callanan9a6940c2015-10-21 00:36:34 +000055#include "lldb/Utility/LLDBAssert.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000056#include "lldb/Utility/Log.h"
Pavel Labathd821c992018-08-07 11:07:21 +000057#include "lldb/Utility/State.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000058#include "lldb/Utility/StreamString.h"
Pavel Labath38d06322017-06-29 14:32:17 +000059#include "lldb/Utility/Timer.h"
Pavel Labath181b8232018-12-14 15:59:49 +000060#include <mutex>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
62using namespace lldb;
63using namespace lldb_private;
64
Pavel Labath43d35412016-12-06 11:24:51 +000065constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
66
Pavel Labath13e37d42017-10-25 21:05:31 +000067Target::Arch::Arch(const ArchSpec &spec)
68 : m_spec(spec),
69 m_plugin_up(PluginManager::CreateArchitectureInstance(spec)) {}
70
71const Target::Arch& Target::Arch::operator=(const ArchSpec &spec) {
72 m_spec = spec;
73 m_plugin_up = PluginManager::CreateArchitectureInstance(spec);
74 return *this;
75}
76
Kate Stoneb9c1b512016-09-06 20:57:50 +000077ConstString &Target::GetStaticBroadcasterClass() {
78 static ConstString class_name("lldb.target");
79 return class_name;
Jim Ingham4bddaeb2012-02-16 06:50:00 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082Target::Target(Debugger &debugger, const ArchSpec &target_arch,
83 const lldb::PlatformSP &platform_sp, bool is_dummy_target)
Saleem Abdulrasoolbb19a132016-05-19 05:13:57 +000084 : TargetProperties(this),
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 Broadcaster(debugger.GetBroadcasterManager(),
86 Target::GetStaticBroadcasterClass().AsCString()),
87 ExecutionContextScope(), m_debugger(debugger), m_platform_sp(platform_sp),
Davide Italiano24fff242018-04-13 18:02:39 +000088 m_mutex(), m_arch(target_arch), m_images(this), m_section_load_history(),
89 m_breakpoint_list(false), m_internal_breakpoint_list(true),
90 m_watchpoint_list(), m_process_sp(), m_search_filter_sp(),
91 m_image_search_paths(ImageSearchPathsChanged, this), m_ast_importer_sp(),
92 m_source_manager_ap(), m_stop_hooks(), m_stop_hook_next_id(0),
93 m_valid(true), m_suppress_stop_hooks(false),
94 m_is_dummy_target(is_dummy_target),
95 m_stats_storage(static_cast<int>(StatisticKind::StatisticMax))
Jim Ingham893c9322014-11-22 01:42:44 +000096
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097{
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 SetEventName(eBroadcastBitBreakpointChanged, "breakpoint-changed");
99 SetEventName(eBroadcastBitModulesLoaded, "modules-loaded");
100 SetEventName(eBroadcastBitModulesUnloaded, "modules-unloaded");
101 SetEventName(eBroadcastBitWatchpointChanged, "watchpoint-changed");
102 SetEventName(eBroadcastBitSymbolsLoaded, "symbols-loaded");
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 CheckInWithManager();
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
107 if (log)
108 log->Printf("%p Target::Target()", static_cast<void *>(this));
Pavel Labath13e37d42017-10-25 21:05:31 +0000109 if (target_arch.IsValid()) {
110 LogIfAnyCategoriesSet(LIBLLDB_LOG_TARGET,
111 "Target::Target created with architecture %s (%s)",
112 target_arch.GetArchitectureName(),
113 target_arch.GetTriple().getTriple().c_str());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000114 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117Target::~Target() {
118 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
119 if (log)
120 log->Printf("%p Target::~Target()", static_cast<void *>(this));
121 DeleteCurrentProcess();
Eugene Zelenko8f30a652015-10-23 18:39:37 +0000122}
123
Kate Stoneb9c1b512016-09-06 20:57:50 +0000124void Target::PrimeFromDummyTarget(Target *target) {
125 if (!target)
126 return;
Jim Ingham893c9322014-11-22 01:42:44 +0000127
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 m_stop_hooks = target->m_stop_hooks;
129
130 for (BreakpointSP breakpoint_sp : target->m_breakpoint_list.Breakpoints()) {
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 Inghamb842f2e2017-09-14 20:22:49 +0000137
138 for (auto bp_name_entry : target->m_breakpoint_names)
139 {
140
141 BreakpointName *new_bp_name = new BreakpointName(*bp_name_entry.second);
142 AddBreakpointName(new_bp_name);
143 }
Jim Ingham893c9322014-11-22 01:42:44 +0000144}
145
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146void Target::Dump(Stream *s, lldb::DescriptionLevel description_level) {
147 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
148 if (description_level != lldb::eDescriptionLevelBrief) {
149 s->Indent();
150 s->PutCString("Target\n");
151 s->IndentMore();
152 m_images.Dump(s);
153 m_breakpoint_list.Dump(s);
154 m_internal_breakpoint_list.Dump(s);
155 s->IndentLess();
156 } else {
157 Module *exe_module = GetExecutableModulePointer();
158 if (exe_module)
159 s->PutCString(exe_module->GetFileSpec().GetFilename().GetCString());
Caroline Ticeceb6b132010-10-26 03:11:13 +0000160 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000161 s->PutCString("No executable module.");
162 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163}
164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165void Target::CleanupProcess() {
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 std::unique_lock<std::recursive_mutex> lock;
173 this->GetWatchpointList().GetListMutex(lock);
174 DisableAllWatchpoints(false);
175 ClearAllWatchpointHitCounts();
176 ClearAllWatchpointHistoricValues();
Greg Clayton90ba8112012-12-05 00:16:59 +0000177}
178
Kate Stoneb9c1b512016-09-06 20:57:50 +0000179void Target::DeleteCurrentProcess() {
180 if (m_process_sp) {
Greg Claytond5944cd2013-12-06 01:12:00 +0000181 m_section_load_history.Clear();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 if (m_process_sp->IsAlive())
183 m_process_sp->Destroy(false);
184
185 m_process_sp->Finalize();
186
187 CleanupProcess();
188
189 m_process_sp.reset();
190 }
Greg Clayton3418c852011-08-10 02:10:13 +0000191}
192
Kate Stoneb9c1b512016-09-06 20:57:50 +0000193const lldb::ProcessSP &Target::CreateProcess(ListenerSP listener_sp,
Zachary Turner31659452016-11-17 21:15:14 +0000194 llvm::StringRef plugin_name,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195 const FileSpec *crash_file) {
Pavel Labathcb8c6992019-01-07 10:59:57 +0000196 if (!listener_sp)
197 listener_sp = GetDebugger().GetListener();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000198 DeleteCurrentProcess();
199 m_process_sp = Process::FindPlugin(shared_from_this(), plugin_name,
200 listener_sp, crash_file);
201 return m_process_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202}
203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204const lldb::ProcessSP &Target::GetProcessSP() const { return m_process_sp; }
205
Zachary Turner97206d52017-05-12 04:51:55 +0000206lldb::REPLSP Target::GetREPL(Status &err, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000207 const char *repl_options, bool can_create) {
208 if (language == eLanguageTypeUnknown) {
209 std::set<LanguageType> repl_languages;
210
211 Language::GetLanguagesSupportingREPLs(repl_languages);
212
213 if (repl_languages.size() == 1) {
214 language = *repl_languages.begin();
215 } else if (repl_languages.size() == 0) {
216 err.SetErrorStringWithFormat(
217 "LLDB isn't configured with REPL support for any languages.");
218 return REPLSP();
219 } else {
220 err.SetErrorStringWithFormat(
221 "Multiple possible REPL languages. Please specify a language.");
222 return REPLSP();
223 }
224 }
225
226 REPLMap::iterator pos = m_repl_map.find(language);
227
228 if (pos != m_repl_map.end()) {
229 return pos->second;
230 }
231
232 if (!can_create) {
233 err.SetErrorStringWithFormat(
234 "Couldn't find an existing REPL for %s, and can't create a new one",
235 Language::GetNameForLanguageType(language));
236 return lldb::REPLSP();
237 }
238
239 Debugger *const debugger = nullptr;
240 lldb::REPLSP ret = REPL::Create(err, language, debugger, this, repl_options);
241
242 if (ret) {
243 m_repl_map[language] = ret;
244 return m_repl_map[language];
245 }
246
247 if (err.Success()) {
248 err.SetErrorStringWithFormat("Couldn't create a REPL for %s",
249 Language::GetNameForLanguageType(language));
250 }
251
252 return lldb::REPLSP();
253}
254
255void Target::SetREPL(lldb::LanguageType language, lldb::REPLSP repl_sp) {
256 lldbassert(!m_repl_map.count(language));
257
258 m_repl_map[language] = repl_sp;
259}
260
261void Target::Destroy() {
262 std::lock_guard<std::recursive_mutex> guard(m_mutex);
263 m_valid = false;
264 DeleteCurrentProcess();
265 m_platform_sp.reset();
Pavel Labath13e37d42017-10-25 21:05:31 +0000266 m_arch = ArchSpec();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 ClearModules(true);
268 m_section_load_history.Clear();
269 const bool notify = false;
270 m_breakpoint_list.RemoveAll(notify);
271 m_internal_breakpoint_list.RemoveAll(notify);
272 m_last_created_breakpoint.reset();
273 m_last_created_watchpoint.reset();
274 m_search_filter_sp.reset();
275 m_image_search_paths.Clear(notify);
276 m_stop_hooks.clear();
277 m_stop_hook_next_id = 0;
278 m_suppress_stop_hooks = false;
279}
280
281BreakpointList &Target::GetBreakpointList(bool internal) {
282 if (internal)
283 return m_internal_breakpoint_list;
284 else
285 return m_breakpoint_list;
286}
287
288const BreakpointList &Target::GetBreakpointList(bool internal) const {
289 if (internal)
290 return m_internal_breakpoint_list;
291 else
292 return m_breakpoint_list;
293}
294
295BreakpointSP Target::GetBreakpointByID(break_id_t break_id) {
296 BreakpointSP bp_sp;
297
298 if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
299 bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
300 else
301 bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
302
303 return bp_sp;
304}
305
306BreakpointSP Target::CreateSourceRegexBreakpoint(
307 const FileSpecList *containingModules,
308 const FileSpecList *source_file_spec_list,
309 const std::unordered_set<std::string> &function_names,
310 RegularExpression &source_regex, bool internal, bool hardware,
311 LazyBool move_to_nearest_code) {
312 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
313 containingModules, source_file_spec_list));
314 if (move_to_nearest_code == eLazyBoolCalculate)
315 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
316 BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex(
317 nullptr, source_regex, function_names,
318 !static_cast<bool>(move_to_nearest_code)));
319
320 return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
321}
322
323BreakpointSP Target::CreateBreakpoint(const FileSpecList *containingModules,
324 const FileSpec &file, uint32_t line_no,
Adrian Prantl431b1582018-08-30 15:11:00 +0000325 uint32_t column, lldb::addr_t offset,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000326 LazyBool check_inlines,
327 LazyBool skip_prologue, bool internal,
328 bool hardware,
329 LazyBool move_to_nearest_code) {
330 FileSpec remapped_file;
Greg Clayton86188d82018-05-21 14:14:36 +0000331 if (!GetSourcePathMap().ReverseRemapPath(file, remapped_file))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000332 remapped_file = file;
333
334 if (check_inlines == eLazyBoolCalculate) {
335 const InlineStrategy inline_strategy = GetInlineStrategy();
336 switch (inline_strategy) {
337 case eInlineBreakpointsNever:
338 check_inlines = eLazyBoolNo;
339 break;
340
341 case eInlineBreakpointsHeaders:
342 if (remapped_file.IsSourceImplementationFile())
343 check_inlines = eLazyBoolNo;
344 else
345 check_inlines = eLazyBoolYes;
346 break;
347
348 case eInlineBreakpointsAlways:
349 check_inlines = eLazyBoolYes;
350 break;
351 }
352 }
353 SearchFilterSP filter_sp;
354 if (check_inlines == eLazyBoolNo) {
355 // Not checking for inlines, we are looking only for matching compile units
356 FileSpecList compile_unit_list;
357 compile_unit_list.Append(remapped_file);
358 filter_sp = GetSearchFilterForModuleAndCUList(containingModules,
359 &compile_unit_list);
360 } else {
361 filter_sp = GetSearchFilterForModuleList(containingModules);
362 }
363 if (skip_prologue == eLazyBoolCalculate)
364 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
365 if (move_to_nearest_code == eLazyBoolCalculate)
366 move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo;
367
368 BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine(
Adrian Prantl431b1582018-08-30 15:11:00 +0000369 nullptr, remapped_file, line_no, column, offset, check_inlines,
370 skip_prologue, !static_cast<bool>(move_to_nearest_code)));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000371 return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
372}
373
374BreakpointSP Target::CreateBreakpoint(lldb::addr_t addr, bool internal,
375 bool hardware) {
376 Address so_addr;
377
378 // Check for any reason we want to move this breakpoint to other address.
379 addr = GetBreakableLoadAddress(addr);
380
Adrian Prantl05097242018-04-30 16:49:04 +0000381 // Attempt to resolve our load address if possible, though it is ok if it
382 // doesn't resolve to section/offset.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000383
384 // Try and resolve as a load address if possible
385 GetSectionLoadList().ResolveLoadAddress(addr, so_addr);
386 if (!so_addr.IsValid()) {
387 // The address didn't resolve, so just set this as an absolute address
388 so_addr.SetOffset(addr);
389 }
390 BreakpointSP bp_sp(CreateBreakpoint(so_addr, internal, hardware));
391 return bp_sp;
392}
393
394BreakpointSP Target::CreateBreakpoint(const Address &addr, bool internal,
395 bool hardware) {
396 SearchFilterSP filter_sp(
397 new SearchFilterForUnconstrainedSearches(shared_from_this()));
398 BreakpointResolverSP resolver_sp(
399 new BreakpointResolverAddress(nullptr, addr));
400 return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, false);
401}
402
403lldb::BreakpointSP
404Target::CreateAddressInModuleBreakpoint(lldb::addr_t file_addr, bool internal,
405 const FileSpec *file_spec,
406 bool request_hardware) {
407 SearchFilterSP filter_sp(
408 new SearchFilterForUnconstrainedSearches(shared_from_this()));
409 BreakpointResolverSP resolver_sp(
410 new BreakpointResolverAddress(nullptr, file_addr, file_spec));
411 return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware,
412 false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413}
414
Zachary Turner117b1fa2018-10-25 20:45:40 +0000415BreakpointSP Target::CreateBreakpoint(
416 const FileSpecList *containingModules,
417 const FileSpecList *containingSourceFiles, const char *func_name,
418 FunctionNameType func_name_type_mask, LanguageType language,
419 lldb::addr_t offset, LazyBool skip_prologue, bool internal, bool hardware) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000420 BreakpointSP bp_sp;
421 if (func_name) {
422 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
423 containingModules, containingSourceFiles));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000424
Greg Clayton03da4cc2013-04-19 21:31:16 +0000425 if (skip_prologue == eLazyBoolCalculate)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000426 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
427 if (language == lldb::eLanguageTypeUnknown)
428 language = GetLanguage();
429
430 BreakpointResolverSP resolver_sp(new BreakpointResolverName(
431 nullptr, func_name, func_name_type_mask, language, Breakpoint::Exact,
432 offset, skip_prologue));
433 bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
434 }
435 return bp_sp;
436}
437
438lldb::BreakpointSP
439Target::CreateBreakpoint(const FileSpecList *containingModules,
440 const FileSpecList *containingSourceFiles,
441 const std::vector<std::string> &func_names,
Zachary Turner117b1fa2018-10-25 20:45:40 +0000442 FunctionNameType func_name_type_mask,
443 LanguageType language, lldb::addr_t offset,
444 LazyBool skip_prologue, bool internal, bool hardware) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000445 BreakpointSP bp_sp;
446 size_t num_names = func_names.size();
447 if (num_names > 0) {
448 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
449 containingModules, containingSourceFiles));
450
451 if (skip_prologue == eLazyBoolCalculate)
452 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
453 if (language == lldb::eLanguageTypeUnknown)
454 language = GetLanguage();
455
456 BreakpointResolverSP resolver_sp(
457 new BreakpointResolverName(nullptr, func_names, func_name_type_mask,
458 language, offset, skip_prologue));
459 bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
460 }
461 return bp_sp;
462}
463
Zachary Turner117b1fa2018-10-25 20:45:40 +0000464BreakpointSP
465Target::CreateBreakpoint(const FileSpecList *containingModules,
466 const FileSpecList *containingSourceFiles,
467 const char *func_names[], size_t num_names,
468 FunctionNameType func_name_type_mask,
469 LanguageType language, lldb::addr_t offset,
470 LazyBool skip_prologue, bool internal, bool hardware) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000471 BreakpointSP bp_sp;
472 if (num_names > 0) {
473 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
474 containingModules, containingSourceFiles));
475
476 if (skip_prologue == eLazyBoolCalculate) {
477 if (offset == 0)
Greg Clayton03da4cc2013-04-19 21:31:16 +0000478 skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000479 else
480 skip_prologue = eLazyBoolNo;
Greg Clayton1b72fcb2010-08-24 00:45:41 +0000481 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000482 if (language == lldb::eLanguageTypeUnknown)
483 language = GetLanguage();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484
Kate Stoneb9c1b512016-09-06 20:57:50 +0000485 BreakpointResolverSP resolver_sp(new BreakpointResolverName(
486 nullptr, func_names, num_names, func_name_type_mask, language, offset,
487 skip_prologue));
488 resolver_sp->SetOffset(offset);
489 bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
490 }
491 return bp_sp;
Jim Ingham133e0fb2012-03-03 02:05:11 +0000492}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493
494SearchFilterSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000495Target::GetSearchFilterForModule(const FileSpec *containingModule) {
496 SearchFilterSP filter_sp;
497 if (containingModule != nullptr) {
498 // TODO: We should look into sharing module based search filters
499 // across many breakpoints like we do for the simple target based one
500 filter_sp.reset(
501 new SearchFilterByModule(shared_from_this(), *containingModule));
502 } else {
503 if (!m_search_filter_sp)
504 m_search_filter_sp.reset(
505 new SearchFilterForUnconstrainedSearches(shared_from_this()));
506 filter_sp = m_search_filter_sp;
507 }
508 return filter_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509}
510
Jim Ingham969795f2011-09-21 01:17:13 +0000511SearchFilterSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512Target::GetSearchFilterForModuleList(const FileSpecList *containingModules) {
513 SearchFilterSP filter_sp;
514 if (containingModules && containingModules->GetSize() != 0) {
515 // TODO: We should look into sharing module based search filters
516 // across many breakpoints like we do for the simple target based one
517 filter_sp.reset(
518 new SearchFilterByModuleList(shared_from_this(), *containingModules));
519 } else {
520 if (!m_search_filter_sp)
521 m_search_filter_sp.reset(
522 new SearchFilterForUnconstrainedSearches(shared_from_this()));
523 filter_sp = m_search_filter_sp;
524 }
525 return filter_sp;
Jim Ingham969795f2011-09-21 01:17:13 +0000526}
527
Kate Stoneb9c1b512016-09-06 20:57:50 +0000528SearchFilterSP Target::GetSearchFilterForModuleAndCUList(
529 const FileSpecList *containingModules,
530 const FileSpecList *containingSourceFiles) {
531 if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0)
532 return GetSearchFilterForModuleList(containingModules);
533
534 SearchFilterSP filter_sp;
535 if (containingModules == nullptr) {
536 // We could make a special "CU List only SearchFilter". Better yet was if
Adrian Prantl05097242018-04-30 16:49:04 +0000537 // these could be composable, but that will take a little reworking.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538
539 filter_sp.reset(new SearchFilterByModuleListAndCU(
540 shared_from_this(), FileSpecList(), *containingSourceFiles));
541 } else {
542 filter_sp.reset(new SearchFilterByModuleListAndCU(
543 shared_from_this(), *containingModules, *containingSourceFiles));
544 }
545 return filter_sp;
Jim Ingham87df91b2011-09-23 00:54:11 +0000546}
547
Kate Stoneb9c1b512016-09-06 20:57:50 +0000548BreakpointSP Target::CreateFuncRegexBreakpoint(
549 const FileSpecList *containingModules,
550 const FileSpecList *containingSourceFiles, RegularExpression &func_regex,
551 lldb::LanguageType requested_language, LazyBool skip_prologue,
552 bool internal, bool hardware) {
553 SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList(
554 containingModules, containingSourceFiles));
555 bool skip = (skip_prologue == eLazyBoolCalculate)
556 ? GetSkipPrologue()
557 : static_cast<bool>(skip_prologue);
558 BreakpointResolverSP resolver_sp(new BreakpointResolverName(
559 nullptr, func_regex, requested_language, 0, skip));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560
Kate Stoneb9c1b512016-09-06 20:57:50 +0000561 return CreateBreakpoint(filter_sp, resolver_sp, internal, hardware, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562}
563
Jim Ingham219ba192012-03-05 04:47:34 +0000564lldb::BreakpointSP
Kate Stoneb9c1b512016-09-06 20:57:50 +0000565Target::CreateExceptionBreakpoint(enum lldb::LanguageType language,
566 bool catch_bp, bool throw_bp, bool internal,
Zachary Turner97206d52017-05-12 04:51:55 +0000567 Args *additional_args, Status *error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000568 BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint(
569 *this, language, catch_bp, throw_bp, internal);
570 if (exc_bkpt_sp && additional_args) {
571 Breakpoint::BreakpointPreconditionSP precondition_sp =
572 exc_bkpt_sp->GetPrecondition();
573 if (precondition_sp && additional_args) {
574 if (error)
575 *error = precondition_sp->ConfigurePrecondition(*additional_args);
576 else
577 precondition_sp->ConfigurePrecondition(*additional_args);
Jim Inghama72b31c2015-04-22 19:42:18 +0000578 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000579 }
580 return exc_bkpt_sp;
Jim Ingham219ba192012-03-05 04:47:34 +0000581}
Jim Inghama72b31c2015-04-22 19:42:18 +0000582
Jim Ingham3815e702018-09-13 21:35:32 +0000583lldb::BreakpointSP
584Target::CreateScriptedBreakpoint(const llvm::StringRef class_name,
585 const FileSpecList *containingModules,
586 const FileSpecList *containingSourceFiles,
587 bool internal,
588 bool request_hardware,
589 StructuredData::ObjectSP extra_args_sp,
590 Status *creation_error)
591{
592 SearchFilterSP filter_sp;
593
594 lldb::SearchDepth depth = lldb::eSearchDepthTarget;
595 bool has_files = containingSourceFiles && containingSourceFiles->GetSize() > 0;
596 bool has_modules = containingModules && containingModules->GetSize() > 0;
597
598 if (has_files && has_modules) {
599 filter_sp = GetSearchFilterForModuleAndCUList(
600 containingModules, containingSourceFiles);
601 } else if (has_files) {
602 filter_sp = GetSearchFilterForModuleAndCUList(
603 nullptr, containingSourceFiles);
604 } else if (has_modules) {
605 filter_sp = GetSearchFilterForModuleList(containingModules);
606 } else {
607 filter_sp.reset(new SearchFilterForUnconstrainedSearches(shared_from_this()));
608 }
609
610 StructuredDataImpl *extra_args_impl = new StructuredDataImpl();
611 if (extra_args_sp)
612 extra_args_impl->SetObjectSP(extra_args_sp);
613
614 BreakpointResolverSP resolver_sp(new
615 BreakpointResolverScripted(nullptr, class_name,
616 depth,
617 extra_args_impl,
618 *GetDebugger().GetCommandInterpreter()
619 .GetScriptInterpreter()));
620 return CreateBreakpoint(filter_sp, resolver_sp, internal, false, true);
621
622}
623
624
Kate Stoneb9c1b512016-09-06 20:57:50 +0000625BreakpointSP Target::CreateBreakpoint(SearchFilterSP &filter_sp,
626 BreakpointResolverSP &resolver_sp,
627 bool internal, bool request_hardware,
628 bool resolve_indirect_symbols) {
629 BreakpointSP bp_sp;
630 if (filter_sp && resolver_sp) {
Jonas Devliegheree103ae92018-11-15 01:18:15 +0000631 const bool hardware = request_hardware || GetRequireHardwareBreakpoints();
632 bp_sp.reset(new Breakpoint(*this, filter_sp, resolver_sp, hardware,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000633 resolve_indirect_symbols));
634 resolver_sp->SetBreakpoint(bp_sp.get());
635 AddBreakpoint(bp_sp, internal);
636 }
637 return bp_sp;
Jim Ingham33df7cd2014-12-06 01:28:03 +0000638}
639
Kate Stoneb9c1b512016-09-06 20:57:50 +0000640void Target::AddBreakpoint(lldb::BreakpointSP bp_sp, bool internal) {
641 if (!bp_sp)
642 return;
643 if (internal)
644 m_internal_breakpoint_list.Add(bp_sp, false);
645 else
646 m_breakpoint_list.Add(bp_sp, true);
Jim Ingham33df7cd2014-12-06 01:28:03 +0000647
Kate Stoneb9c1b512016-09-06 20:57:50 +0000648 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
649 if (log) {
650 StreamString s;
651 bp_sp->GetDescription(&s, lldb::eDescriptionLevelVerbose);
652 log->Printf("Target::%s (internal = %s) => break_id = %s\n", __FUNCTION__,
653 bp_sp->IsInternal() ? "yes" : "no", s.GetData());
654 }
Jim Ingham33df7cd2014-12-06 01:28:03 +0000655
Kate Stoneb9c1b512016-09-06 20:57:50 +0000656 bp_sp->ResolveBreakpoint();
Jim Ingham33df7cd2014-12-06 01:28:03 +0000657
Kate Stoneb9c1b512016-09-06 20:57:50 +0000658 if (!internal) {
659 m_last_created_breakpoint = bp_sp;
660 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661}
662
Jim Inghamb842f2e2017-09-14 20:22:49 +0000663void Target::AddNameToBreakpoint(BreakpointID &id,
664 const char *name,
665 Status &error)
666 {
667 BreakpointSP bp_sp
668 = m_breakpoint_list.FindBreakpointByID(id.GetBreakpointID());
669 if (!bp_sp)
670 {
671 StreamString s;
672 id.GetDescription(&s, eDescriptionLevelBrief);
673 error.SetErrorStringWithFormat("Could not find breakpoint %s",
674 s.GetData());
675 return;
676 }
677 AddNameToBreakpoint(bp_sp, name, error);
678 }
679
680void Target::AddNameToBreakpoint(BreakpointSP &bp_sp,
681 const char *name,
682 Status &error)
683 {
684 if (!bp_sp)
685 return;
686
687 BreakpointName *bp_name = FindBreakpointName(ConstString(name), true, error);
688 if (!bp_name)
689 return;
690
691 bp_name->ConfigureBreakpoint(bp_sp);
692 bp_sp->AddName(name);
693 }
694
695void Target::AddBreakpointName(BreakpointName *bp_name) {
696 m_breakpoint_names.insert(std::make_pair(bp_name->GetName(), bp_name));
697}
698
699BreakpointName *Target::FindBreakpointName(const ConstString &name,
700 bool can_create,
701 Status &error)
702{
703 BreakpointID::StringIsBreakpointName(name.GetStringRef(), error);
704 if (!error.Success())
705 return nullptr;
706
707 BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
708 if (iter == m_breakpoint_names.end()) {
709 if (!can_create)
710 {
711 error.SetErrorStringWithFormat("Breakpoint name \"%s\" doesn't exist and "
712 "can_create is false.", name.AsCString());
713 return nullptr;
714 }
715
716 iter = m_breakpoint_names.insert(std::make_pair(name,
717 new BreakpointName(name)))
718 .first;
719 }
720 return (iter->second);
721}
722
723void
724Target::DeleteBreakpointName(const ConstString &name)
725{
726 BreakpointNameList::iterator iter = m_breakpoint_names.find(name);
727
728 if (iter != m_breakpoint_names.end()) {
729 const char *name_cstr = name.AsCString();
730 m_breakpoint_names.erase(iter);
731 for (auto bp_sp : m_breakpoint_list.Breakpoints())
732 bp_sp->RemoveName(name_cstr);
733 }
734}
735
736void Target::RemoveNameFromBreakpoint(lldb::BreakpointSP &bp_sp,
737 const ConstString &name)
738{
739 bp_sp->RemoveName(name.AsCString());
740}
741
742void Target::ConfigureBreakpointName(BreakpointName &bp_name,
743 const BreakpointOptions &new_options,
744 const BreakpointName::Permissions &new_permissions)
745{
746 bp_name.GetOptions().CopyOverSetOptions(new_options);
747 bp_name.GetPermissions().MergeInto(new_permissions);
748 ApplyNameToBreakpoints(bp_name);
749}
750
751void Target::ApplyNameToBreakpoints(BreakpointName &bp_name) {
752 BreakpointList bkpts_with_name(false);
753 m_breakpoint_list.FindBreakpointsByName(bp_name.GetName().AsCString(),
754 bkpts_with_name);
755
756 for (auto bp_sp : bkpts_with_name.Breakpoints())
757 bp_name.ConfigureBreakpoint(bp_sp);
758}
759
760void Target::GetBreakpointNames(std::vector<std::string> &names)
761{
762 names.clear();
763 for (auto bp_name : m_breakpoint_names) {
764 names.push_back(bp_name.first.AsCString());
765 }
Jonas Devlieghere9bbba272019-01-08 23:25:06 +0000766 llvm::sort(names.begin(), names.end());
Jim Inghamb842f2e2017-09-14 20:22:49 +0000767}
768
Kate Stoneb9c1b512016-09-06 20:57:50 +0000769bool Target::ProcessIsValid() {
770 return (m_process_sp && m_process_sp->IsAlive());
Johnny Chen86364b42011-09-20 23:28:55 +0000771}
772
Jason Molendac0e793d2018-11-09 22:33:26 +0000773static bool CheckIfWatchpointsSupported(Target *target, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000774 uint32_t num_supported_hardware_watchpoints;
Zachary Turner97206d52017-05-12 04:51:55 +0000775 Status rc = target->GetProcessSP()->GetWatchpointSupportInfo(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000776 num_supported_hardware_watchpoints);
Jason Molendac0e793d2018-11-09 22:33:26 +0000777
778 // If unable to determine the # of watchpoints available,
779 // assume they are supported.
780 if (rc.Fail())
781 return true;
782
Kate Stoneb9c1b512016-09-06 20:57:50 +0000783 if (num_supported_hardware_watchpoints == 0) {
784 error.SetErrorStringWithFormat(
785 "Target supports (%u) hardware watchpoint slots.\n",
786 num_supported_hardware_watchpoints);
787 return false;
788 }
789 return true;
Johnny Chenb90827e2012-06-04 23:19:54 +0000790}
791
Adrian Prantl05097242018-04-30 16:49:04 +0000792// See also Watchpoint::SetWatchpointType(uint32_t type) and the
793// OptionGroupWatchpoint::WatchType enum type.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794WatchpointSP Target::CreateWatchpoint(lldb::addr_t addr, size_t size,
795 const CompilerType *type, uint32_t kind,
Zachary Turner97206d52017-05-12 04:51:55 +0000796 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000797 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
798 if (log)
799 log->Printf("Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64
800 " type = %u)\n",
801 __FUNCTION__, addr, (uint64_t)size, kind);
Johnny Chen0c406372011-09-14 20:23:45 +0000802
Kate Stoneb9c1b512016-09-06 20:57:50 +0000803 WatchpointSP wp_sp;
804 if (!ProcessIsValid()) {
805 error.SetErrorString("process is not alive");
Johnny Chen01a67862011-10-14 00:42:25 +0000806 return wp_sp;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000807 }
Johnny Chen887062a2011-09-12 23:38:44 +0000808
Kate Stoneb9c1b512016-09-06 20:57:50 +0000809 if (addr == LLDB_INVALID_ADDRESS || size == 0) {
810 if (size == 0)
811 error.SetErrorString("cannot set a watchpoint with watch_size of 0");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813 error.SetErrorStringWithFormat("invalid watch address: %" PRIu64, addr);
814 return wp_sp;
815 }
816
817 if (!LLDB_WATCH_TYPE_IS_VALID(kind)) {
818 error.SetErrorStringWithFormat("invalid watchpoint type: %d", kind);
819 }
820
Jason Molendac0e793d2018-11-09 22:33:26 +0000821 if (!CheckIfWatchpointsSupported(this, error))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000822 return wp_sp;
823
Adrian Prantl05097242018-04-30 16:49:04 +0000824 // Currently we only support one watchpoint per address, with total number of
825 // watchpoints limited by the hardware which the inferior is running on.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000826
827 // Grab the list mutex while doing operations.
828 const bool notify = false; // Don't notify about all the state changes we do
829 // on creating the watchpoint.
830 std::unique_lock<std::recursive_mutex> lock;
831 this->GetWatchpointList().GetListMutex(lock);
832 WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr);
833 if (matched_sp) {
834 size_t old_size = matched_sp->GetByteSize();
835 uint32_t old_type =
836 (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) |
837 (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0);
838 // Return the existing watchpoint if both size and type match.
839 if (size == old_size && kind == old_type) {
840 wp_sp = matched_sp;
841 wp_sp->SetEnabled(false, notify);
842 } else {
843 // Nil the matched watchpoint; we will be creating a new one.
844 m_process_sp->DisableWatchpoint(matched_sp.get(), notify);
845 m_watchpoint_list.Remove(matched_sp->GetID(), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000847 }
848
849 if (!wp_sp) {
850 wp_sp.reset(new Watchpoint(*this, addr, size, type));
851 wp_sp->SetWatchpointType(kind, notify);
852 m_watchpoint_list.Add(wp_sp, true);
853 }
854
855 error = m_process_sp->EnableWatchpoint(wp_sp.get(), notify);
856 if (log)
857 log->Printf("Target::%s (creation of watchpoint %s with id = %u)\n",
858 __FUNCTION__, error.Success() ? "succeeded" : "failed",
859 wp_sp->GetID());
860
861 if (error.Fail()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000862 // Enabling the watchpoint on the device side failed. Remove the said
863 // watchpoint from the list maintained by the target instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000864 m_watchpoint_list.Remove(wp_sp->GetID(), true);
865 // See if we could provide more helpful error message.
866 if (!OptionGroupWatchpoint::IsWatchSizeSupported(size))
867 error.SetErrorStringWithFormat(
868 "watch size of %" PRIu64 " is not supported", (uint64_t)size);
869
870 wp_sp.reset();
871 } else
872 m_last_created_watchpoint = wp_sp;
873 return wp_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874}
875
Jim Inghamb842f2e2017-09-14 20:22:49 +0000876void Target::RemoveAllowedBreakpoints ()
877{
878 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
879 if (log)
880 log->Printf("Target::%s \n", __FUNCTION__);
881
882 m_breakpoint_list.RemoveAllowed(true);
883
884 m_last_created_breakpoint.reset();
885}
886
Kate Stoneb9c1b512016-09-06 20:57:50 +0000887void Target::RemoveAllBreakpoints(bool internal_also) {
888 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
889 if (log)
890 log->Printf("Target::%s (internal_also = %s)\n", __FUNCTION__,
891 internal_also ? "yes" : "no");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892
Kate Stoneb9c1b512016-09-06 20:57:50 +0000893 m_breakpoint_list.RemoveAll(true);
894 if (internal_also)
895 m_internal_breakpoint_list.RemoveAll(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896
Kate Stoneb9c1b512016-09-06 20:57:50 +0000897 m_last_created_breakpoint.reset();
898}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899
Kate Stoneb9c1b512016-09-06 20:57:50 +0000900void Target::DisableAllBreakpoints(bool internal_also) {
901 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
902 if (log)
903 log->Printf("Target::%s (internal_also = %s)\n", __FUNCTION__,
904 internal_also ? "yes" : "no");
905
906 m_breakpoint_list.SetEnabledAll(false);
907 if (internal_also)
908 m_internal_breakpoint_list.SetEnabledAll(false);
909}
910
Jim Inghamb842f2e2017-09-14 20:22:49 +0000911void Target::DisableAllowedBreakpoints() {
912 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
913 if (log)
914 log->Printf("Target::%s", __FUNCTION__);
915
916 m_breakpoint_list.SetEnabledAllowed(false);
917}
918
Kate Stoneb9c1b512016-09-06 20:57:50 +0000919void Target::EnableAllBreakpoints(bool internal_also) {
920 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
921 if (log)
922 log->Printf("Target::%s (internal_also = %s)\n", __FUNCTION__,
923 internal_also ? "yes" : "no");
924
925 m_breakpoint_list.SetEnabledAll(true);
926 if (internal_also)
927 m_internal_breakpoint_list.SetEnabledAll(true);
928}
929
Jim Inghamb842f2e2017-09-14 20:22:49 +0000930void Target::EnableAllowedBreakpoints() {
931 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
932 if (log)
933 log->Printf("Target::%s", __FUNCTION__);
934
935 m_breakpoint_list.SetEnabledAllowed(true);
936}
937
Kate Stoneb9c1b512016-09-06 20:57:50 +0000938bool Target::RemoveBreakpointByID(break_id_t break_id) {
939 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
940 if (log)
941 log->Printf("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
942 break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
943
944 if (DisableBreakpointByID(break_id)) {
945 if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
946 m_internal_breakpoint_list.Remove(break_id, false);
947 else {
948 if (m_last_created_breakpoint) {
949 if (m_last_created_breakpoint->GetID() == break_id)
950 m_last_created_breakpoint.reset();
951 }
952 m_breakpoint_list.Remove(break_id, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000953 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000954 return true;
955 }
956 return false;
957}
958
959bool Target::DisableBreakpointByID(break_id_t break_id) {
960 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
961 if (log)
962 log->Printf("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
963 break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
964
965 BreakpointSP bp_sp;
966
967 if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
968 bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
969 else
970 bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
971 if (bp_sp) {
972 bp_sp->SetEnabled(false);
973 return true;
974 }
975 return false;
976}
977
978bool Target::EnableBreakpointByID(break_id_t break_id) {
979 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_BREAKPOINTS));
980 if (log)
981 log->Printf("Target::%s (break_id = %i, internal = %s)\n", __FUNCTION__,
982 break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no");
983
984 BreakpointSP bp_sp;
985
986 if (LLDB_BREAK_ID_IS_INTERNAL(break_id))
987 bp_sp = m_internal_breakpoint_list.FindBreakpointByID(break_id);
988 else
989 bp_sp = m_breakpoint_list.FindBreakpointByID(break_id);
990
991 if (bp_sp) {
992 bp_sp->SetEnabled(true);
993 return true;
994 }
995 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000996}
997
Zachary Turner97206d52017-05-12 04:51:55 +0000998Status Target::SerializeBreakpointsToFile(const FileSpec &file,
999 const BreakpointIDList &bp_ids,
1000 bool append) {
1001 Status error;
Jim Ingham01f16662016-09-14 19:07:35 +00001002
1003 if (!file) {
1004 error.SetErrorString("Invalid FileSpec.");
1005 return error;
1006 }
1007
1008 std::string path(file.GetPath());
Jim Ingham2d3628e2016-09-22 23:42:42 +00001009 StructuredData::ObjectSP input_data_sp;
1010
1011 StructuredData::ArraySP break_store_sp;
1012 StructuredData::Array *break_store_ptr = nullptr;
1013
1014 if (append) {
1015 input_data_sp = StructuredData::ParseJSONFromFile(file, error);
1016 if (error.Success()) {
1017 break_store_ptr = input_data_sp->GetAsArray();
1018 if (!break_store_ptr) {
1019 error.SetErrorStringWithFormat(
1020 "Tried to append to invalid input file %s", path.c_str());
1021 return error;
1022 }
1023 }
1024 }
1025
1026 if (!break_store_ptr) {
1027 break_store_sp.reset(new StructuredData::Array());
1028 break_store_ptr = break_store_sp.get();
1029 }
1030
Jim Ingham01f16662016-09-14 19:07:35 +00001031 StreamFile out_file(path.c_str(),
1032 File::OpenOptions::eOpenOptionTruncate |
1033 File::OpenOptions::eOpenOptionWrite |
1034 File::OpenOptions::eOpenOptionCanCreate |
1035 File::OpenOptions::eOpenOptionCloseOnExec,
1036 lldb::eFilePermissionsFileDefault);
1037 if (!out_file.GetFile().IsValid()) {
1038 error.SetErrorStringWithFormat("Unable to open output file: %s.",
1039 path.c_str());
1040 return error;
1041 }
1042
1043 std::unique_lock<std::recursive_mutex> lock;
1044 GetBreakpointList().GetListMutex(lock);
1045
Jim Ingham01f16662016-09-14 19:07:35 +00001046 if (bp_ids.GetSize() == 0) {
1047 const BreakpointList &breakpoints = GetBreakpointList();
1048
1049 size_t num_breakpoints = breakpoints.GetSize();
1050 for (size_t i = 0; i < num_breakpoints; i++) {
1051 Breakpoint *bp = breakpoints.GetBreakpointAtIndex(i).get();
1052 StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
1053 // If a breakpoint can't serialize it, just ignore it for now:
1054 if (bkpt_save_sp)
Jim Ingham2d3628e2016-09-22 23:42:42 +00001055 break_store_ptr->AddItem(bkpt_save_sp);
Jim Ingham01f16662016-09-14 19:07:35 +00001056 }
1057 } else {
1058
1059 std::unordered_set<lldb::break_id_t> processed_bkpts;
1060 const size_t count = bp_ids.GetSize();
1061 for (size_t i = 0; i < count; ++i) {
1062 BreakpointID cur_bp_id = bp_ids.GetBreakpointIDAtIndex(i);
1063 lldb::break_id_t bp_id = cur_bp_id.GetBreakpointID();
1064
1065 if (bp_id != LLDB_INVALID_BREAK_ID) {
1066 // Only do each breakpoint once:
1067 std::pair<std::unordered_set<lldb::break_id_t>::iterator, bool>
1068 insert_result = processed_bkpts.insert(bp_id);
1069 if (!insert_result.second)
1070 continue;
1071
1072 Breakpoint *bp = GetBreakpointByID(bp_id).get();
1073 StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData();
1074 // If the user explicitly asked to serialize a breakpoint, and we
Adrian Prantl05097242018-04-30 16:49:04 +00001075 // can't, then raise an error:
Jim Ingham01f16662016-09-14 19:07:35 +00001076 if (!bkpt_save_sp) {
1077 error.SetErrorStringWithFormat("Unable to serialize breakpoint %d",
1078 bp_id);
1079 return error;
1080 }
Jim Ingham2d3628e2016-09-22 23:42:42 +00001081 break_store_ptr->AddItem(bkpt_save_sp);
Jim Ingham01f16662016-09-14 19:07:35 +00001082 }
1083 }
1084 }
1085
Jim Ingham2d3628e2016-09-22 23:42:42 +00001086 break_store_ptr->Dump(out_file, false);
Jim Ingham01f16662016-09-14 19:07:35 +00001087 out_file.PutChar('\n');
1088 return error;
1089}
1090
Zachary Turner97206d52017-05-12 04:51:55 +00001091Status Target::CreateBreakpointsFromFile(const FileSpec &file,
1092 BreakpointIDList &new_bps) {
Jim Ingham3acdf382016-09-22 22:20:28 +00001093 std::vector<std::string> no_names;
1094 return CreateBreakpointsFromFile(file, no_names, new_bps);
1095}
1096
Zachary Turner97206d52017-05-12 04:51:55 +00001097Status Target::CreateBreakpointsFromFile(const FileSpec &file,
1098 std::vector<std::string> &names,
1099 BreakpointIDList &new_bps) {
Jim Ingham01f16662016-09-14 19:07:35 +00001100 std::unique_lock<std::recursive_mutex> lock;
1101 GetBreakpointList().GetListMutex(lock);
1102
Zachary Turner97206d52017-05-12 04:51:55 +00001103 Status error;
Jim Ingham01f16662016-09-14 19:07:35 +00001104 StructuredData::ObjectSP input_data_sp =
1105 StructuredData::ParseJSONFromFile(file, error);
1106 if (!error.Success()) {
1107 return error;
1108 } else if (!input_data_sp || !input_data_sp->IsValid()) {
1109 error.SetErrorStringWithFormat("Invalid JSON from input file: %s.",
1110 file.GetPath().c_str());
1111 return error;
1112 }
1113
1114 StructuredData::Array *bkpt_array = input_data_sp->GetAsArray();
1115 if (!bkpt_array) {
1116 error.SetErrorStringWithFormat(
1117 "Invalid breakpoint data from input file: %s.", file.GetPath().c_str());
1118 return error;
1119 }
1120
1121 size_t num_bkpts = bkpt_array->GetSize();
Jim Ingham3acdf382016-09-22 22:20:28 +00001122 size_t num_names = names.size();
1123
Jim Ingham01f16662016-09-14 19:07:35 +00001124 for (size_t i = 0; i < num_bkpts; i++) {
1125 StructuredData::ObjectSP bkpt_object_sp = bkpt_array->GetItemAtIndex(i);
1126 // Peel off the breakpoint key, and feed the rest to the Breakpoint:
1127 StructuredData::Dictionary *bkpt_dict = bkpt_object_sp->GetAsDictionary();
1128 if (!bkpt_dict) {
1129 error.SetErrorStringWithFormat(
1130 "Invalid breakpoint data for element %zu from input file: %s.", i,
1131 file.GetPath().c_str());
1132 return error;
1133 }
1134 StructuredData::ObjectSP bkpt_data_sp =
1135 bkpt_dict->GetValueForKey(Breakpoint::GetSerializationKey());
Jim Ingham3acdf382016-09-22 22:20:28 +00001136 if (num_names &&
1137 !Breakpoint::SerializedBreakpointMatchesNames(bkpt_data_sp, names))
1138 continue;
1139
Jim Ingham01f16662016-09-14 19:07:35 +00001140 BreakpointSP bkpt_sp =
1141 Breakpoint::CreateFromStructuredData(*this, bkpt_data_sp, error);
1142 if (!error.Success()) {
1143 error.SetErrorStringWithFormat(
1144 "Error restoring breakpoint %zu from %s: %s.", i,
1145 file.GetPath().c_str(), error.AsCString());
1146 return error;
1147 }
1148 new_bps.AddBreakpointID(BreakpointID(bkpt_sp->GetID()));
1149 }
1150 return error;
1151}
1152
Johnny Chenedf50372011-09-23 21:21:43 +00001153// The flag 'end_to_end', default to true, signifies that the operation is
1154// performed end to end, for both the debugger and the debuggee.
1155
Johnny Chen01a67862011-10-14 00:42:25 +00001156// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1157// to end operations.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001158bool Target::RemoveAllWatchpoints(bool end_to_end) {
1159 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1160 if (log)
1161 log->Printf("Target::%s\n", __FUNCTION__);
Johnny Chen86364b42011-09-20 23:28:55 +00001162
Kate Stoneb9c1b512016-09-06 20:57:50 +00001163 if (!end_to_end) {
1164 m_watchpoint_list.RemoveAll(true);
1165 return true;
1166 }
Johnny Chenedf50372011-09-23 21:21:43 +00001167
Kate Stoneb9c1b512016-09-06 20:57:50 +00001168 // Otherwise, it's an end to end operation.
Johnny Chenedf50372011-09-23 21:21:43 +00001169
Kate Stoneb9c1b512016-09-06 20:57:50 +00001170 if (!ProcessIsValid())
1171 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001172
Kate Stoneb9c1b512016-09-06 20:57:50 +00001173 size_t num_watchpoints = m_watchpoint_list.GetSize();
1174 for (size_t i = 0; i < num_watchpoints; ++i) {
1175 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1176 if (!wp_sp)
1177 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001178
Zachary Turner97206d52017-05-12 04:51:55 +00001179 Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001180 if (rc.Fail())
1181 return false;
1182 }
1183 m_watchpoint_list.RemoveAll(true);
1184 m_last_created_watchpoint.reset();
1185 return true; // Success!
Johnny Chen86364b42011-09-20 23:28:55 +00001186}
1187
Adrian Prantl05097242018-04-30 16:49:04 +00001188// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1189// to end operations.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001190bool Target::DisableAllWatchpoints(bool end_to_end) {
1191 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1192 if (log)
1193 log->Printf("Target::%s\n", __FUNCTION__);
Johnny Chen86364b42011-09-20 23:28:55 +00001194
Kate Stoneb9c1b512016-09-06 20:57:50 +00001195 if (!end_to_end) {
1196 m_watchpoint_list.SetEnabledAll(false);
1197 return true;
1198 }
Johnny Chenedf50372011-09-23 21:21:43 +00001199
Kate Stoneb9c1b512016-09-06 20:57:50 +00001200 // Otherwise, it's an end to end operation.
Johnny Chenedf50372011-09-23 21:21:43 +00001201
Kate Stoneb9c1b512016-09-06 20:57:50 +00001202 if (!ProcessIsValid())
1203 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001204
Kate Stoneb9c1b512016-09-06 20:57:50 +00001205 size_t num_watchpoints = m_watchpoint_list.GetSize();
1206 for (size_t i = 0; i < num_watchpoints; ++i) {
1207 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1208 if (!wp_sp)
1209 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001210
Zachary Turner97206d52017-05-12 04:51:55 +00001211 Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001212 if (rc.Fail())
1213 return false;
1214 }
1215 return true; // Success!
Johnny Chen86364b42011-09-20 23:28:55 +00001216}
1217
Adrian Prantl05097242018-04-30 16:49:04 +00001218// Assumption: Caller holds the list mutex lock for m_watchpoint_list for end
1219// to end operations.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001220bool Target::EnableAllWatchpoints(bool end_to_end) {
1221 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1222 if (log)
1223 log->Printf("Target::%s\n", __FUNCTION__);
Johnny Chen86364b42011-09-20 23:28:55 +00001224
Kate Stoneb9c1b512016-09-06 20:57:50 +00001225 if (!end_to_end) {
1226 m_watchpoint_list.SetEnabledAll(true);
1227 return true;
1228 }
Johnny Chenedf50372011-09-23 21:21:43 +00001229
Kate Stoneb9c1b512016-09-06 20:57:50 +00001230 // Otherwise, it's an end to end operation.
Johnny Chenedf50372011-09-23 21:21:43 +00001231
Kate Stoneb9c1b512016-09-06 20:57:50 +00001232 if (!ProcessIsValid())
1233 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001234
Kate Stoneb9c1b512016-09-06 20:57:50 +00001235 size_t num_watchpoints = m_watchpoint_list.GetSize();
1236 for (size_t i = 0; i < num_watchpoints; ++i) {
1237 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1238 if (!wp_sp)
1239 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001240
Zachary Turner97206d52017-05-12 04:51:55 +00001241 Status rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001242 if (rc.Fail())
1243 return false;
1244 }
1245 return true; // Success!
Johnny Chen86364b42011-09-20 23:28:55 +00001246}
1247
Johnny Chena4d6bc92012-02-25 06:44:30 +00001248// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001249bool Target::ClearAllWatchpointHitCounts() {
1250 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1251 if (log)
1252 log->Printf("Target::%s\n", __FUNCTION__);
Johnny Chena4d6bc92012-02-25 06:44:30 +00001253
Kate Stoneb9c1b512016-09-06 20:57:50 +00001254 size_t num_watchpoints = m_watchpoint_list.GetSize();
1255 for (size_t i = 0; i < num_watchpoints; ++i) {
1256 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1257 if (!wp_sp)
1258 return false;
Johnny Chena4d6bc92012-02-25 06:44:30 +00001259
Kate Stoneb9c1b512016-09-06 20:57:50 +00001260 wp_sp->ResetHitCount();
1261 }
1262 return true; // Success!
Johnny Chena4d6bc92012-02-25 06:44:30 +00001263}
1264
Enrico Granata5e3fe042015-02-11 00:37:54 +00001265// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001266bool Target::ClearAllWatchpointHistoricValues() {
1267 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1268 if (log)
1269 log->Printf("Target::%s\n", __FUNCTION__);
1270
1271 size_t num_watchpoints = m_watchpoint_list.GetSize();
1272 for (size_t i = 0; i < num_watchpoints; ++i) {
1273 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1274 if (!wp_sp)
1275 return false;
1276
1277 wp_sp->ResetHistoricValues();
1278 }
1279 return true; // Success!
Enrico Granata5e3fe042015-02-11 00:37:54 +00001280}
1281
Adrian Prantl05097242018-04-30 16:49:04 +00001282// Assumption: Caller holds the list mutex lock for m_watchpoint_list during
1283// these operations.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001284bool Target::IgnoreAllWatchpoints(uint32_t ignore_count) {
1285 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1286 if (log)
1287 log->Printf("Target::%s\n", __FUNCTION__);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001288
Kate Stoneb9c1b512016-09-06 20:57:50 +00001289 if (!ProcessIsValid())
1290 return false;
Johnny Chen6cc60e82011-10-05 21:35:46 +00001291
Kate Stoneb9c1b512016-09-06 20:57:50 +00001292 size_t num_watchpoints = m_watchpoint_list.GetSize();
1293 for (size_t i = 0; i < num_watchpoints; ++i) {
1294 WatchpointSP wp_sp = m_watchpoint_list.GetByIndex(i);
1295 if (!wp_sp)
1296 return false;
Johnny Chen6cc60e82011-10-05 21:35:46 +00001297
Kate Stoneb9c1b512016-09-06 20:57:50 +00001298 wp_sp->SetIgnoreCount(ignore_count);
1299 }
1300 return true; // Success!
Johnny Chen6cc60e82011-10-05 21:35:46 +00001301}
1302
Johnny Chen01a67862011-10-14 00:42:25 +00001303// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001304bool Target::DisableWatchpointByID(lldb::watch_id_t watch_id) {
1305 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1306 if (log)
1307 log->Printf("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
Johnny Chen86364b42011-09-20 23:28:55 +00001308
Kate Stoneb9c1b512016-09-06 20:57:50 +00001309 if (!ProcessIsValid())
Johnny Chen86364b42011-09-20 23:28:55 +00001310 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001311
1312 WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
1313 if (wp_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +00001314 Status rc = m_process_sp->DisableWatchpoint(wp_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001315 if (rc.Success())
1316 return true;
1317
1318 // Else, fallthrough.
1319 }
1320 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001321}
1322
Johnny Chen01a67862011-10-14 00:42:25 +00001323// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001324bool Target::EnableWatchpointByID(lldb::watch_id_t watch_id) {
1325 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1326 if (log)
1327 log->Printf("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
Johnny Chen86364b42011-09-20 23:28:55 +00001328
Kate Stoneb9c1b512016-09-06 20:57:50 +00001329 if (!ProcessIsValid())
Johnny Chen86364b42011-09-20 23:28:55 +00001330 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001331
1332 WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
1333 if (wp_sp) {
Zachary Turner97206d52017-05-12 04:51:55 +00001334 Status rc = m_process_sp->EnableWatchpoint(wp_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001335 if (rc.Success())
1336 return true;
1337
1338 // Else, fallthrough.
1339 }
1340 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001341}
1342
Johnny Chen01a67862011-10-14 00:42:25 +00001343// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001344bool Target::RemoveWatchpointByID(lldb::watch_id_t watch_id) {
1345 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1346 if (log)
1347 log->Printf("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
Johnny Chen86364b42011-09-20 23:28:55 +00001348
Kate Stoneb9c1b512016-09-06 20:57:50 +00001349 WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watch_id);
1350 if (watch_to_remove_sp == m_last_created_watchpoint)
1351 m_last_created_watchpoint.reset();
1352
1353 if (DisableWatchpointByID(watch_id)) {
1354 m_watchpoint_list.Remove(watch_id, true);
1355 return true;
1356 }
1357 return false;
Johnny Chen86364b42011-09-20 23:28:55 +00001358}
1359
Johnny Chen01a67862011-10-14 00:42:25 +00001360// Assumption: Caller holds the list mutex lock for m_watchpoint_list.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001361bool Target::IgnoreWatchpointByID(lldb::watch_id_t watch_id,
1362 uint32_t ignore_count) {
1363 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_WATCHPOINTS));
1364 if (log)
1365 log->Printf("Target::%s (watch_id = %i)\n", __FUNCTION__, watch_id);
Johnny Chen6cc60e82011-10-05 21:35:46 +00001366
Kate Stoneb9c1b512016-09-06 20:57:50 +00001367 if (!ProcessIsValid())
Johnny Chen6cc60e82011-10-05 21:35:46 +00001368 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001369
1370 WatchpointSP wp_sp = m_watchpoint_list.FindByID(watch_id);
1371 if (wp_sp) {
1372 wp_sp->SetIgnoreCount(ignore_count);
1373 return true;
1374 }
1375 return false;
Johnny Chen6cc60e82011-10-05 21:35:46 +00001376}
1377
Kate Stoneb9c1b512016-09-06 20:57:50 +00001378ModuleSP Target::GetExecutableModule() {
1379 // search for the first executable in the module list
1380 for (size_t i = 0; i < m_images.GetSize(); ++i) {
1381 ModuleSP module_sp = m_images.GetModuleAtIndex(i);
1382 lldb_private::ObjectFile *obj = module_sp->GetObjectFile();
1383 if (obj == nullptr)
1384 continue;
1385 if (obj->GetType() == ObjectFile::Type::eTypeExecutable)
1386 return module_sp;
1387 }
1388 // as fall back return the first module loaded
1389 return m_images.GetModuleAtIndex(0);
Greg Claytonaa149cb2011-08-11 02:48:45 +00001390}
1391
Kate Stoneb9c1b512016-09-06 20:57:50 +00001392Module *Target::GetExecutableModulePointer() {
1393 return GetExecutableModule().get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001394}
1395
Kate Stoneb9c1b512016-09-06 20:57:50 +00001396static void LoadScriptingResourceForModule(const ModuleSP &module_sp,
1397 Target *target) {
Zachary Turner97206d52017-05-12 04:51:55 +00001398 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001399 StreamString feedback_stream;
1400 if (module_sp &&
1401 !module_sp->LoadScriptingResourceInTarget(target, error,
1402 &feedback_stream)) {
1403 if (error.AsCString())
1404 target->GetDebugger().GetErrorFile()->Printf(
1405 "unable to load scripting data for module %s - error reported was "
1406 "%s\n",
1407 module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(),
1408 error.AsCString());
1409 }
1410 if (feedback_stream.GetSize())
1411 target->GetDebugger().GetErrorFile()->Printf("%s\n",
1412 feedback_stream.GetData());
Enrico Granata17598482012-11-08 02:22:02 +00001413}
1414
Kate Stoneb9c1b512016-09-06 20:57:50 +00001415void Target::ClearModules(bool delete_locations) {
1416 ModulesDidUnload(m_images, delete_locations);
1417 m_section_load_history.Clear();
1418 m_images.Clear();
1419 m_scratch_type_system_map.Clear();
1420 m_ast_importer_sp.reset();
Greg Clayton095eeaa2013-11-05 23:28:00 +00001421}
1422
Kate Stoneb9c1b512016-09-06 20:57:50 +00001423void Target::DidExec() {
1424 // When a process exec's we need to know about it so we can do some cleanup.
Pavel Labath13e37d42017-10-25 21:05:31 +00001425 m_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
1426 m_internal_breakpoint_list.RemoveInvalidLocations(m_arch.GetSpec());
Greg Claytonb35db632013-11-09 00:03:31 +00001427}
1428
Kate Stoneb9c1b512016-09-06 20:57:50 +00001429void Target::SetExecutableModule(ModuleSP &executable_sp,
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001430 LoadDependentFiles load_dependent_files) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001431 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
1432 ClearModules(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433
Kate Stoneb9c1b512016-09-06 20:57:50 +00001434 if (executable_sp) {
Pavel Labathf9d16472017-05-15 13:02:37 +00001435 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
1436 Timer scoped_timer(func_cat,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001437 "Target::SetExecutableModule (executable = '%s')",
1438 executable_sp->GetFileSpec().GetPath().c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439
Kate Stoneb9c1b512016-09-06 20:57:50 +00001440 m_images.Append(executable_sp); // The first image is our executable file
Jason Molendae1b68ad2012-12-05 00:25:49 +00001441
Kate Stoneb9c1b512016-09-06 20:57:50 +00001442 // If we haven't set an architecture yet, reset our architecture based on
1443 // what we found in the executable module.
Pavel Labath13e37d42017-10-25 21:05:31 +00001444 if (!m_arch.GetSpec().IsValid()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001445 m_arch = executable_sp->GetArchitecture();
Pavel Labath13e37d42017-10-25 21:05:31 +00001446 LLDB_LOG(log,
1447 "setting architecture to {0} ({1}) based on executable file",
1448 m_arch.GetSpec().GetArchitectureName(),
1449 m_arch.GetSpec().GetTriple().getTriple());
Jason Molendadad8af42015-11-10 04:11:37 +00001450 }
1451
Kate Stoneb9c1b512016-09-06 20:57:50 +00001452 FileSpecList dependent_files;
1453 ObjectFile *executable_objfile = executable_sp->GetObjectFile();
Jonas Devlieghere041e68f2018-09-27 06:59:15 +00001454 bool load_dependents = true;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001455 switch (load_dependent_files) {
1456 case eLoadDependentsDefault:
Jonas Devlieghere041e68f2018-09-27 06:59:15 +00001457 load_dependents = executable_sp->IsExecutable();
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001458 break;
1459 case eLoadDependentsYes:
Jonas Devlieghere041e68f2018-09-27 06:59:15 +00001460 load_dependents = true;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001461 break;
1462 case eLoadDependentsNo:
Jonas Devlieghere041e68f2018-09-27 06:59:15 +00001463 load_dependents = false;
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001464 break;
1465 }
Greg Clayton095eeaa2013-11-05 23:28:00 +00001466
Jonas Devlieghere041e68f2018-09-27 06:59:15 +00001467 if (executable_objfile && load_dependents) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001468 executable_objfile->GetDependentModules(dependent_files);
1469 for (uint32_t i = 0; i < dependent_files.GetSize(); i++) {
1470 FileSpec dependent_file_spec(
1471 dependent_files.GetFileSpecPointerAtIndex(i));
1472 FileSpec platform_dependent_file_spec;
Greg Clayton67cc0632012-08-22 17:17:09 +00001473 if (m_platform_sp)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001474 m_platform_sp->GetFileWithUUID(dependent_file_spec, nullptr,
1475 platform_dependent_file_spec);
1476 else
1477 platform_dependent_file_spec = dependent_file_spec;
1478
Pavel Labath13e37d42017-10-25 21:05:31 +00001479 ModuleSpec module_spec(platform_dependent_file_spec, m_arch.GetSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001480 ModuleSP image_module_sp(GetSharedModule(module_spec));
1481 if (image_module_sp) {
1482 ObjectFile *objfile = image_module_sp->GetObjectFile();
1483 if (objfile)
1484 objfile->GetDependentModules(dependent_files);
1485 }
1486 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001487 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001488 }
Jim Inghamc6674fd2011-10-28 23:14:11 +00001489}
1490
Greg Clayton19c8f392018-08-06 16:56:10 +00001491bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001492 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
Pavel Labath13e37d42017-10-25 21:05:31 +00001493 bool missing_local_arch = !m_arch.GetSpec().IsValid();
Kate Stoneb9c1b512016-09-06 20:57:50 +00001494 bool replace_local_arch = true;
1495 bool compatible_local_arch = false;
1496 ArchSpec other(arch_spec);
Greg Claytone72dfb32012-02-24 01:59:29 +00001497
Greg Clayton19c8f392018-08-06 16:56:10 +00001498 // Changing the architecture might mean that the currently selected platform
1499 // isn't compatible. Set the platform correctly if we are asked to do so,
1500 // otherwise assume the user will set the platform manually.
1501 if (set_platform) {
1502 if (other.IsValid()) {
1503 auto platform_sp = GetPlatform();
1504 if (!platform_sp ||
1505 !platform_sp->IsCompatibleArchitecture(other, false, nullptr)) {
1506 ArchSpec platform_arch;
1507 auto arch_platform_sp =
1508 Platform::GetPlatformForArchitecture(other, &platform_arch);
1509 if (arch_platform_sp) {
1510 SetPlatform(arch_platform_sp);
1511 if (platform_arch.IsValid())
1512 other = platform_arch;
1513 }
1514 }
1515 }
1516 }
1517
Kate Stoneb9c1b512016-09-06 20:57:50 +00001518 if (!missing_local_arch) {
Pavel Labath13e37d42017-10-25 21:05:31 +00001519 if (m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
1520 other.MergeFrom(m_arch.GetSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001521
Pavel Labath13e37d42017-10-25 21:05:31 +00001522 if (m_arch.GetSpec().IsCompatibleMatch(other)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001523 compatible_local_arch = true;
1524 bool arch_changed, vendor_changed, os_changed, os_ver_changed,
1525 env_changed;
1526
Pavel Labath13e37d42017-10-25 21:05:31 +00001527 m_arch.GetSpec().PiecewiseTripleCompare(other, arch_changed, vendor_changed,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001528 os_changed, os_ver_changed, env_changed);
1529
1530 if (!arch_changed && !vendor_changed && !os_changed && !env_changed)
1531 replace_local_arch = false;
1532 }
1533 }
1534 }
1535
1536 if (compatible_local_arch || missing_local_arch) {
1537 // If we haven't got a valid arch spec, or the architectures are compatible
Adrian Prantl05097242018-04-30 16:49:04 +00001538 // update the architecture, unless the one we already have is more
1539 // specified
Kate Stoneb9c1b512016-09-06 20:57:50 +00001540 if (replace_local_arch)
1541 m_arch = other;
Pavel Labath13e37d42017-10-25 21:05:31 +00001542 LLDB_LOG(log, "set architecture to {0} ({1})",
1543 m_arch.GetSpec().GetArchitectureName(),
1544 m_arch.GetSpec().GetTriple().getTriple());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001545 return true;
1546 }
1547
1548 // If we have an executable file, try to reset the executable to the desired
1549 // architecture
1550 if (log)
1551 log->Printf("Target::SetArchitecture changing architecture to %s (%s)",
1552 arch_spec.GetArchitectureName(),
1553 arch_spec.GetTriple().getTriple().c_str());
1554 m_arch = other;
1555 ModuleSP executable_sp = GetExecutableModule();
1556
1557 ClearModules(true);
1558 // Need to do something about unsetting breakpoints.
1559
1560 if (executable_sp) {
1561 if (log)
1562 log->Printf("Target::SetArchitecture Trying to select executable file "
1563 "architecture %s (%s)",
1564 arch_spec.GetArchitectureName(),
1565 arch_spec.GetTriple().getTriple().c_str());
1566 ModuleSpec module_spec(executable_sp->GetFileSpec(), other);
Zachary Turner97206d52017-05-12 04:51:55 +00001567 Status error = ModuleList::GetSharedModule(module_spec, executable_sp,
1568 &GetExecutableSearchPaths(),
1569 nullptr, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001570
1571 if (!error.Fail() && executable_sp) {
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00001572 SetExecutableModule(executable_sp, eLoadDependentsYes);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001573 return true;
1574 }
1575 }
1576 return false;
Greg Claytondb598232011-01-07 01:57:07 +00001577}
1578
Kate Stoneb9c1b512016-09-06 20:57:50 +00001579bool Target::MergeArchitecture(const ArchSpec &arch_spec) {
Jason Molendadf9f7962018-10-23 23:45:56 +00001580 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
Kate Stoneb9c1b512016-09-06 20:57:50 +00001581 if (arch_spec.IsValid()) {
Pavel Labath13e37d42017-10-25 21:05:31 +00001582 if (m_arch.GetSpec().IsCompatibleMatch(arch_spec)) {
Adrian Prantl05097242018-04-30 16:49:04 +00001583 // The current target arch is compatible with "arch_spec", see if we can
1584 // improve our current architecture using bits from "arch_spec"
Greg Claytonc749eb82011-07-11 05:12:02 +00001585
Jason Molendadf9f7962018-10-23 23:45:56 +00001586 if (log)
1587 log->Printf("Target::MergeArchitecture target has arch %s, merging with "
1588 "arch %s",
1589 m_arch.GetSpec().GetTriple().getTriple().c_str(),
1590 arch_spec.GetTriple().getTriple().c_str());
1591
Kate Stoneb9c1b512016-09-06 20:57:50 +00001592 // Merge bits from arch_spec into "merged_arch" and set our architecture
Pavel Labath13e37d42017-10-25 21:05:31 +00001593 ArchSpec merged_arch(m_arch.GetSpec());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001594 merged_arch.MergeFrom(arch_spec);
1595 return SetArchitecture(merged_arch);
1596 } else {
1597 // The new architecture is different, we just need to replace it
1598 return SetArchitecture(arch_spec);
Greg Claytondda4f7b2010-06-30 23:03:03 +00001599 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001600 }
1601 return false;
1602}
Greg Claytonc749eb82011-07-11 05:12:02 +00001603
Kate Stoneb9c1b512016-09-06 20:57:50 +00001604void Target::WillClearList(const ModuleList &module_list) {}
1605
1606void Target::ModuleAdded(const ModuleList &module_list,
1607 const ModuleSP &module_sp) {
1608 // A module is being added to this target for the first time
1609 if (m_valid) {
1610 ModuleList my_module_list;
1611 my_module_list.Append(module_sp);
1612 LoadScriptingResourceForModule(module_sp, this);
1613 ModulesDidLoad(my_module_list);
1614 }
1615}
1616
1617void Target::ModuleRemoved(const ModuleList &module_list,
1618 const ModuleSP &module_sp) {
1619 // A module is being removed from this target.
1620 if (m_valid) {
1621 ModuleList my_module_list;
1622 my_module_list.Append(module_sp);
1623 ModulesDidUnload(my_module_list, false);
1624 }
1625}
1626
1627void Target::ModuleUpdated(const ModuleList &module_list,
1628 const ModuleSP &old_module_sp,
1629 const ModuleSP &new_module_sp) {
1630 // A module is replacing an already added module
1631 if (m_valid) {
1632 m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp,
1633 new_module_sp);
1634 m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(
1635 old_module_sp, new_module_sp);
1636 }
1637}
1638
1639void Target::ModulesDidLoad(ModuleList &module_list) {
1640 if (m_valid && module_list.GetSize()) {
1641 m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1642 m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1643 if (m_process_sp) {
1644 m_process_sp->ModulesDidLoad(module_list);
1645 }
1646 BroadcastEvent(eBroadcastBitModulesLoaded,
1647 new TargetEventData(this->shared_from_this(), module_list));
1648 }
1649}
1650
1651void Target::SymbolsDidLoad(ModuleList &module_list) {
1652 if (m_valid && module_list.GetSize()) {
1653 if (m_process_sp) {
1654 LanguageRuntime *runtime =
1655 m_process_sp->GetLanguageRuntime(lldb::eLanguageTypeObjC);
1656 if (runtime) {
1657 ObjCLanguageRuntime *objc_runtime = (ObjCLanguageRuntime *)runtime;
1658 objc_runtime->SymbolsDidLoad(module_list);
1659 }
1660 }
1661
1662 m_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1663 m_internal_breakpoint_list.UpdateBreakpoints(module_list, true, false);
1664 BroadcastEvent(eBroadcastBitSymbolsLoaded,
1665 new TargetEventData(this->shared_from_this(), module_list));
1666 }
1667}
1668
1669void Target::ModulesDidUnload(ModuleList &module_list, bool delete_locations) {
1670 if (m_valid && module_list.GetSize()) {
1671 UnloadModuleSections(module_list);
1672 m_breakpoint_list.UpdateBreakpoints(module_list, false, delete_locations);
1673 m_internal_breakpoint_list.UpdateBreakpoints(module_list, false,
1674 delete_locations);
1675 BroadcastEvent(eBroadcastBitModulesUnloaded,
1676 new TargetEventData(this->shared_from_this(), module_list));
1677 }
1678}
1679
1680bool Target::ModuleIsExcludedForUnconstrainedSearches(
1681 const FileSpec &module_file_spec) {
1682 if (GetBreakpointsConsultPlatformAvoidList()) {
1683 ModuleList matchingModules;
1684 ModuleSpec module_spec(module_file_spec);
1685 size_t num_modules = GetImages().FindModules(module_spec, matchingModules);
1686
1687 // If there is more than one module for this file spec, only return true if
1688 // ALL the modules are on the
1689 // black list.
1690 if (num_modules > 0) {
1691 for (size_t i = 0; i < num_modules; i++) {
1692 if (!ModuleIsExcludedForUnconstrainedSearches(
1693 matchingModules.GetModuleAtIndex(i)))
1694 return false;
1695 }
1696 return true;
1697 }
1698 }
1699 return false;
1700}
1701
1702bool Target::ModuleIsExcludedForUnconstrainedSearches(
1703 const lldb::ModuleSP &module_sp) {
1704 if (GetBreakpointsConsultPlatformAvoidList()) {
1705 if (m_platform_sp)
1706 return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches(*this,
1707 module_sp);
1708 }
1709 return false;
1710}
1711
1712size_t Target::ReadMemoryFromFileCache(const Address &addr, void *dst,
Zachary Turner97206d52017-05-12 04:51:55 +00001713 size_t dst_len, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001714 SectionSP section_sp(addr.GetSection());
1715 if (section_sp) {
1716 // If the contents of this section are encrypted, the on-disk file is
1717 // unusable. Read only from live memory.
1718 if (section_sp->IsEncrypted()) {
1719 error.SetErrorString("section is encrypted");
1720 return 0;
1721 }
1722 ModuleSP module_sp(section_sp->GetModule());
1723 if (module_sp) {
1724 ObjectFile *objfile = section_sp->GetModule()->GetObjectFile();
1725 if (objfile) {
1726 size_t bytes_read = objfile->ReadSectionData(
1727 section_sp.get(), addr.GetOffset(), dst, dst_len);
Greg Claytondb598232011-01-07 01:57:07 +00001728 if (bytes_read > 0)
Kate Stoneb9c1b512016-09-06 20:57:50 +00001729 return bytes_read;
Greg Claytondda4f7b2010-06-30 23:03:03 +00001730 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001731 error.SetErrorStringWithFormat("error reading data from section %s",
1732 section_sp->GetName().GetCString());
1733 } else
1734 error.SetErrorString("address isn't from a object file");
1735 } else
1736 error.SetErrorString("address isn't in a module");
1737 } else
1738 error.SetErrorString("address doesn't contain a section that points to a "
1739 "section in a object file");
1740
1741 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001742}
1743
Kate Stoneb9c1b512016-09-06 20:57:50 +00001744size_t Target::ReadMemory(const Address &addr, bool prefer_file_cache,
Zachary Turner97206d52017-05-12 04:51:55 +00001745 void *dst, size_t dst_len, Status &error,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001746 lldb::addr_t *load_addr_ptr) {
1747 error.Clear();
1748
Adrian Prantl05097242018-04-30 16:49:04 +00001749 // if we end up reading this from process memory, we will fill this with the
1750 // actual load address
Kate Stoneb9c1b512016-09-06 20:57:50 +00001751 if (load_addr_ptr)
1752 *load_addr_ptr = LLDB_INVALID_ADDRESS;
1753
1754 size_t bytes_read = 0;
1755
1756 addr_t load_addr = LLDB_INVALID_ADDRESS;
1757 addr_t file_addr = LLDB_INVALID_ADDRESS;
1758 Address resolved_addr;
1759 if (!addr.IsSectionOffset()) {
1760 SectionLoadList &section_load_list = GetSectionLoadList();
1761 if (section_load_list.IsEmpty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001762 // No sections are loaded, so we must assume we are not running yet and
1763 // anything we are given is a file address.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001764 file_addr = addr.GetOffset(); // "addr" doesn't have a section, so its
1765 // offset is the file address
1766 m_images.ResolveFileAddress(file_addr, resolved_addr);
1767 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00001768 // We have at least one section loaded. This can be because we have
1769 // manually loaded some sections with "target modules load ..." or
1770 // because we have have a live process that has sections loaded through
1771 // the dynamic loader
Kate Stoneb9c1b512016-09-06 20:57:50 +00001772 load_addr = addr.GetOffset(); // "addr" doesn't have a section, so its
1773 // offset is the load address
1774 section_load_list.ResolveLoadAddress(load_addr, resolved_addr);
1775 }
1776 }
1777 if (!resolved_addr.IsValid())
1778 resolved_addr = addr;
1779
1780 if (prefer_file_cache) {
1781 bytes_read = ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
1782 if (bytes_read > 0)
1783 return bytes_read;
1784 }
1785
1786 if (ProcessIsValid()) {
1787 if (load_addr == LLDB_INVALID_ADDRESS)
1788 load_addr = resolved_addr.GetLoadAddress(this);
1789
1790 if (load_addr == LLDB_INVALID_ADDRESS) {
1791 ModuleSP addr_module_sp(resolved_addr.GetModule());
1792 if (addr_module_sp && addr_module_sp->GetFileSpec())
Zachary Turner827d5d72016-12-16 04:27:00 +00001793 error.SetErrorStringWithFormatv(
1794 "{0:F}[{1:x+}] can't be resolved, {0:F} is not currently loaded",
1795 addr_module_sp->GetFileSpec(), resolved_addr.GetFileAddress());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001796 else
1797 error.SetErrorStringWithFormat("0x%" PRIx64 " can't be resolved",
1798 resolved_addr.GetFileAddress());
1799 } else {
1800 bytes_read = m_process_sp->ReadMemory(load_addr, dst, dst_len, error);
1801 if (bytes_read != dst_len) {
1802 if (error.Success()) {
1803 if (bytes_read == 0)
1804 error.SetErrorStringWithFormat(
1805 "read memory from 0x%" PRIx64 " failed", load_addr);
1806 else
1807 error.SetErrorStringWithFormat(
1808 "only %" PRIu64 " of %" PRIu64
1809 " bytes were read from memory at 0x%" PRIx64,
1810 (uint64_t)bytes_read, (uint64_t)dst_len, load_addr);
1811 }
1812 }
1813 if (bytes_read) {
1814 if (load_addr_ptr)
1815 *load_addr_ptr = load_addr;
1816 return bytes_read;
1817 }
Adrian Prantl05097242018-04-30 16:49:04 +00001818 // If the address is not section offset we have an address that doesn't
1819 // resolve to any address in any currently loaded shared libraries and we
1820 // failed to read memory so there isn't anything more we can do. If it is
1821 // section offset, we might be able to read cached memory from the object
1822 // file.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001823 if (!resolved_addr.IsSectionOffset())
1824 return 0;
1825 }
1826 }
1827
1828 if (!prefer_file_cache && resolved_addr.IsSectionOffset()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001829 // If we didn't already try and read from the object file cache, then try
1830 // it after failing to read from the process.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001831 return ReadMemoryFromFileCache(resolved_addr, dst, dst_len, error);
1832 }
1833 return 0;
1834}
1835
1836size_t Target::ReadCStringFromMemory(const Address &addr, std::string &out_str,
Zachary Turner97206d52017-05-12 04:51:55 +00001837 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001838 char buf[256];
1839 out_str.clear();
1840 addr_t curr_addr = addr.GetLoadAddress(this);
1841 Address address(addr);
1842 while (1) {
1843 size_t length = ReadCStringFromMemory(address, buf, sizeof(buf), error);
1844 if (length == 0)
1845 break;
1846 out_str.append(buf, length);
Adrian Prantl05097242018-04-30 16:49:04 +00001847 // If we got "length - 1" bytes, we didn't get the whole C string, we need
1848 // to read some more characters
Kate Stoneb9c1b512016-09-06 20:57:50 +00001849 if (length == sizeof(buf) - 1)
1850 curr_addr += length;
1851 else
1852 break;
1853 address = Address(curr_addr);
1854 }
1855 return out_str.size();
1856}
1857
1858size_t Target::ReadCStringFromMemory(const Address &addr, char *dst,
Zachary Turner97206d52017-05-12 04:51:55 +00001859 size_t dst_max_len, Status &result_error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001860 size_t total_cstr_len = 0;
1861 if (dst && dst_max_len) {
1862 result_error.Clear();
1863 // NULL out everything just to be safe
1864 memset(dst, 0, dst_max_len);
Zachary Turner97206d52017-05-12 04:51:55 +00001865 Status error;
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001866 addr_t curr_addr = addr.GetLoadAddress(this);
1867 Address address(addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00001868
Adrian Prantl05097242018-04-30 16:49:04 +00001869 // We could call m_process_sp->GetMemoryCacheLineSize() but I don't think
1870 // this really needs to be tied to the memory cache subsystem's cache line
1871 // size, so leave this as a fixed constant.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001872 const size_t cache_line_size = 512;
1873
1874 size_t bytes_left = dst_max_len - 1;
1875 char *curr_dst = dst;
1876
1877 while (bytes_left > 0) {
1878 addr_t cache_line_bytes_left =
1879 cache_line_size - (curr_addr % cache_line_size);
1880 addr_t bytes_to_read =
1881 std::min<addr_t>(bytes_left, cache_line_bytes_left);
1882 size_t bytes_read =
1883 ReadMemory(address, false, curr_dst, bytes_to_read, error);
1884
1885 if (bytes_read == 0) {
1886 result_error = error;
1887 dst[total_cstr_len] = '\0';
1888 break;
1889 }
1890 const size_t len = strlen(curr_dst);
1891
1892 total_cstr_len += len;
1893
1894 if (len < bytes_to_read)
1895 break;
1896
1897 curr_dst += bytes_read;
1898 curr_addr += bytes_read;
1899 bytes_left -= bytes_read;
1900 address = Address(curr_addr);
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001901 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00001902 } else {
1903 if (dst == nullptr)
1904 result_error.SetErrorString("invalid arguments");
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001905 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00001906 result_error.Clear();
1907 }
1908 return total_cstr_len;
Enrico Granata6b4ddc62013-01-21 19:20:50 +00001909}
1910
Kate Stoneb9c1b512016-09-06 20:57:50 +00001911size_t Target::ReadScalarIntegerFromMemory(const Address &addr,
1912 bool prefer_file_cache,
1913 uint32_t byte_size, bool is_signed,
Zachary Turner97206d52017-05-12 04:51:55 +00001914 Scalar &scalar, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001915 uint64_t uval;
1916
1917 if (byte_size <= sizeof(uval)) {
1918 size_t bytes_read =
1919 ReadMemory(addr, prefer_file_cache, &uval, byte_size, error);
1920 if (bytes_read == byte_size) {
Pavel Labath13e37d42017-10-25 21:05:31 +00001921 DataExtractor data(&uval, sizeof(uval), m_arch.GetSpec().GetByteOrder(),
1922 m_arch.GetSpec().GetAddressByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00001923 lldb::offset_t offset = 0;
1924 if (byte_size <= 4)
1925 scalar = data.GetMaxU32(&offset, byte_size);
1926 else
1927 scalar = data.GetMaxU64(&offset, byte_size);
1928
1929 if (is_signed)
1930 scalar.SignExtend(byte_size * 8);
1931 return bytes_read;
1932 }
1933 } else {
1934 error.SetErrorStringWithFormat(
1935 "byte size of %u is too large for integer scalar type", byte_size);
1936 }
1937 return 0;
1938}
1939
1940uint64_t Target::ReadUnsignedIntegerFromMemory(const Address &addr,
1941 bool prefer_file_cache,
1942 size_t integer_byte_size,
1943 uint64_t fail_value,
Zachary Turner97206d52017-05-12 04:51:55 +00001944 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001945 Scalar scalar;
1946 if (ReadScalarIntegerFromMemory(addr, prefer_file_cache, integer_byte_size,
1947 false, scalar, error))
1948 return scalar.ULongLong(fail_value);
1949 return fail_value;
1950}
1951
1952bool Target::ReadPointerFromMemory(const Address &addr, bool prefer_file_cache,
Zachary Turner97206d52017-05-12 04:51:55 +00001953 Status &error, Address &pointer_addr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001954 Scalar scalar;
1955 if (ReadScalarIntegerFromMemory(addr, prefer_file_cache,
Pavel Labath13e37d42017-10-25 21:05:31 +00001956 m_arch.GetSpec().GetAddressByteSize(), false, scalar,
Kate Stoneb9c1b512016-09-06 20:57:50 +00001957 error)) {
1958 addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS);
1959 if (pointer_vm_addr != LLDB_INVALID_ADDRESS) {
1960 SectionLoadList &section_load_list = GetSectionLoadList();
1961 if (section_load_list.IsEmpty()) {
Adrian Prantl05097242018-04-30 16:49:04 +00001962 // No sections are loaded, so we must assume we are not running yet and
1963 // anything we are given is a file address.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001964 m_images.ResolveFileAddress(pointer_vm_addr, pointer_addr);
1965 } else {
Adrian Prantl05097242018-04-30 16:49:04 +00001966 // We have at least one section loaded. This can be because we have
1967 // manually loaded some sections with "target modules load ..." or
1968 // because we have have a live process that has sections loaded through
1969 // the dynamic loader
Kate Stoneb9c1b512016-09-06 20:57:50 +00001970 section_load_list.ResolveLoadAddress(pointer_vm_addr, pointer_addr);
1971 }
Adrian Prantl05097242018-04-30 16:49:04 +00001972 // We weren't able to resolve the pointer value, so just return an
1973 // address with no section
Kate Stoneb9c1b512016-09-06 20:57:50 +00001974 if (!pointer_addr.IsValid())
1975 pointer_addr.SetOffset(pointer_vm_addr);
1976 return true;
1977 }
1978 }
1979 return false;
1980}
1981
1982ModuleSP Target::GetSharedModule(const ModuleSpec &module_spec,
Zachary Turner97206d52017-05-12 04:51:55 +00001983 Status *error_ptr) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00001984 ModuleSP module_sp;
1985
Zachary Turner97206d52017-05-12 04:51:55 +00001986 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00001987
1988 // First see if we already have this module in our module list. If we do,
Adrian Prantl05097242018-04-30 16:49:04 +00001989 // then we're done, we don't need to consult the shared modules list. But
1990 // only do this if we are passed a UUID.
Kate Stoneb9c1b512016-09-06 20:57:50 +00001991
1992 if (module_spec.GetUUID().IsValid())
1993 module_sp = m_images.FindFirstModule(module_spec);
1994
1995 if (!module_sp) {
1996 ModuleSP old_module_sp; // This will get filled in if we have a new version
1997 // of the library
1998 bool did_create_module = false;
1999
2000 // If there are image search path entries, try to use them first to acquire
2001 // a suitable image.
2002 if (m_image_search_paths.GetSize()) {
2003 ModuleSpec transformed_spec(module_spec);
2004 if (m_image_search_paths.RemapPath(
2005 module_spec.GetFileSpec().GetDirectory(),
2006 transformed_spec.GetFileSpec().GetDirectory())) {
2007 transformed_spec.GetFileSpec().GetFilename() =
2008 module_spec.GetFileSpec().GetFilename();
2009 error = ModuleList::GetSharedModule(transformed_spec, module_sp,
2010 &GetExecutableSearchPaths(),
2011 &old_module_sp, &did_create_module);
2012 }
2013 }
2014
2015 if (!module_sp) {
2016 // If we have a UUID, we can check our global shared module list in case
2017 // we already have it. If we don't have a valid UUID, then we can't since
2018 // the path in "module_spec" will be a platform path, and we will need to
2019 // let the platform find that file. For example, we could be asking for
2020 // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick
2021 // the local copy of "/usr/lib/dyld" since our platform could be a remote
2022 // platform that has its own "/usr/lib/dyld" in an SDK or in a local file
2023 // cache.
2024 if (module_spec.GetUUID().IsValid()) {
2025 // We have a UUID, it is OK to check the global module list...
2026 error = ModuleList::GetSharedModule(module_spec, module_sp,
2027 &GetExecutableSearchPaths(),
2028 &old_module_sp, &did_create_module);
2029 }
2030
2031 if (!module_sp) {
2032 // The platform is responsible for finding and caching an appropriate
2033 // module in the shared module cache.
2034 if (m_platform_sp) {
2035 error = m_platform_sp->GetSharedModule(
2036 module_spec, m_process_sp.get(), module_sp,
2037 &GetExecutableSearchPaths(), &old_module_sp, &did_create_module);
2038 } else {
2039 error.SetErrorString("no platform is currently set");
Greg Claytond16e1e52011-07-12 17:06:17 +00002040 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002041 }
Greg Claytond16e1e52011-07-12 17:06:17 +00002042 }
Greg Claytond16e1e52011-07-12 17:06:17 +00002043
Kate Stoneb9c1b512016-09-06 20:57:50 +00002044 // We found a module that wasn't in our target list. Let's make sure that
Adrian Prantl05097242018-04-30 16:49:04 +00002045 // there wasn't an equivalent module in the list already, and if there was,
2046 // let's remove it.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002047 if (module_sp) {
2048 ObjectFile *objfile = module_sp->GetObjectFile();
2049 if (objfile) {
2050 switch (objfile->GetType()) {
2051 case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of
2052 /// a program's execution state
2053 case ObjectFile::eTypeExecutable: /// A normal executable
2054 case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker
2055 /// executable
2056 case ObjectFile::eTypeObjectFile: /// An intermediate object file
2057 case ObjectFile::eTypeSharedLibrary: /// A shared library that can be
2058 /// used during execution
2059 break;
2060 case ObjectFile::eTypeDebugInfo: /// An object file that contains only
2061 /// debug information
2062 if (error_ptr)
2063 error_ptr->SetErrorString("debug info files aren't valid target "
2064 "modules, please specify an executable");
2065 return ModuleSP();
2066 case ObjectFile::eTypeStubLibrary: /// A library that can be linked
2067 /// against but not used for
2068 /// execution
2069 if (error_ptr)
2070 error_ptr->SetErrorString("stub libraries aren't valid target "
2071 "modules, please specify an executable");
2072 return ModuleSP();
2073 default:
2074 if (error_ptr)
2075 error_ptr->SetErrorString(
2076 "unsupported file type, please specify an executable");
2077 return ModuleSP();
Greg Claytond16e1e52011-07-12 17:06:17 +00002078 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002079 // GetSharedModule is not guaranteed to find the old shared module, for
Adrian Prantl05097242018-04-30 16:49:04 +00002080 // instance in the common case where you pass in the UUID, it is only
2081 // going to find the one module matching the UUID. In fact, it has no
2082 // good way to know what the "old module" relevant to this target is,
2083 // since there might be many copies of a module with this file spec in
2084 // various running debug sessions, but only one of them will belong to
2085 // this target. So let's remove the UUID from the module list, and look
2086 // in the target's module list. Only do this if there is SOMETHING else
2087 // in the module spec...
Kate Stoneb9c1b512016-09-06 20:57:50 +00002088 if (!old_module_sp) {
2089 if (module_spec.GetUUID().IsValid() &&
2090 !module_spec.GetFileSpec().GetFilename().IsEmpty() &&
2091 !module_spec.GetFileSpec().GetDirectory().IsEmpty()) {
2092 ModuleSpec module_spec_copy(module_spec.GetFileSpec());
2093 module_spec_copy.GetUUID().Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002094
Kate Stoneb9c1b512016-09-06 20:57:50 +00002095 ModuleList found_modules;
2096 size_t num_found =
2097 m_images.FindModules(module_spec_copy, found_modules);
2098 if (num_found == 1) {
2099 old_module_sp = found_modules.GetModuleAtIndex(0);
Jim Ingham4a94c912012-05-17 18:38:42 +00002100 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002101 }
Greg Claytonb69bb2e2012-04-27 00:58:27 +00002102 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002103
Jim Ingham7fca8c02017-04-28 00:51:06 +00002104 // Preload symbols outside of any lock, so hopefully we can do this for
2105 // each library in parallel.
2106 if (GetPreloadSymbols())
2107 module_sp->PreloadSymbols();
2108
Kate Stoneb9c1b512016-09-06 20:57:50 +00002109 if (old_module_sp &&
2110 m_images.GetIndexForModule(old_module_sp.get()) !=
2111 LLDB_INVALID_INDEX32) {
2112 m_images.ReplaceModule(old_module_sp, module_sp);
2113 Module *old_module_ptr = old_module_sp.get();
2114 old_module_sp.reset();
2115 ModuleList::RemoveSharedModuleIfOrphaned(old_module_ptr);
2116 } else
2117 m_images.Append(module_sp);
2118 } else
2119 module_sp.reset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002120 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002121 }
2122 if (error_ptr)
2123 *error_ptr = error;
2124 return module_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002125}
2126
Kate Stoneb9c1b512016-09-06 20:57:50 +00002127TargetSP Target::CalculateTarget() { return shared_from_this(); }
2128
2129ProcessSP Target::CalculateProcess() { return m_process_sp; }
2130
2131ThreadSP Target::CalculateThread() { return ThreadSP(); }
2132
2133StackFrameSP Target::CalculateStackFrame() { return StackFrameSP(); }
2134
2135void Target::CalculateExecutionContext(ExecutionContext &exe_ctx) {
2136 exe_ctx.Clear();
2137 exe_ctx.SetTargetPtr(this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002138}
2139
Kate Stoneb9c1b512016-09-06 20:57:50 +00002140PathMappingList &Target::GetImageSearchPathList() {
2141 return m_image_search_paths;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002142}
2143
Kate Stoneb9c1b512016-09-06 20:57:50 +00002144void Target::ImageSearchPathsChanged(const PathMappingList &path_list,
2145 void *baton) {
2146 Target *target = (Target *)baton;
2147 ModuleSP exe_module_sp(target->GetExecutableModule());
2148 if (exe_module_sp)
Jonas Devliegheref9a07e92018-09-20 09:09:05 +00002149 target->SetExecutableModule(exe_module_sp, eLoadDependentsYes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002150}
2151
Zachary Turner97206d52017-05-12 04:51:55 +00002152TypeSystem *Target::GetScratchTypeSystemForLanguage(Status *error,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002153 lldb::LanguageType language,
2154 bool create_on_demand) {
2155 if (!m_valid)
2156 return nullptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002157
Kate Stoneb9c1b512016-09-06 20:57:50 +00002158 if (error) {
2159 error->Clear();
2160 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002161
Kate Stoneb9c1b512016-09-06 20:57:50 +00002162 if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all
2163 // assembly code
2164 || language == eLanguageTypeUnknown) {
2165 std::set<lldb::LanguageType> languages_for_types;
2166 std::set<lldb::LanguageType> languages_for_expressions;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002167
Kate Stoneb9c1b512016-09-06 20:57:50 +00002168 Language::GetLanguagesSupportingTypeSystems(languages_for_types,
2169 languages_for_expressions);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002170
Kate Stoneb9c1b512016-09-06 20:57:50 +00002171 if (languages_for_expressions.count(eLanguageTypeC)) {
2172 language = eLanguageTypeC; // LLDB's default. Override by setting the
2173 // target language.
2174 } else {
2175 if (languages_for_expressions.empty()) {
Greg Clayton5beec212015-10-08 21:04:34 +00002176 return nullptr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002177 } else {
2178 language = *languages_for_expressions.begin();
2179 }
Sean Callanana994b0b2015-10-02 18:40:30 +00002180 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002181 }
Sean Callananb92bd752015-10-01 16:28:02 +00002182
Kate Stoneb9c1b512016-09-06 20:57:50 +00002183 return m_scratch_type_system_map.GetTypeSystemForLanguage(language, this,
2184 create_on_demand);
Sean Callananb92bd752015-10-01 16:28:02 +00002185}
2186
2187PersistentExpressionState *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002188Target::GetPersistentExpressionStateForLanguage(lldb::LanguageType language) {
2189 TypeSystem *type_system =
2190 GetScratchTypeSystemForLanguage(nullptr, language, true);
2191
2192 if (type_system) {
2193 return type_system->GetPersistentExpressionState();
2194 } else {
2195 return nullptr;
2196 }
Jim Ingham151c0322015-09-15 21:13:50 +00002197}
2198
Kate Stoneb9c1b512016-09-06 20:57:50 +00002199UserExpression *Target::GetUserExpressionForLanguage(
Zachary Turnerc5d7df92016-11-08 04:52:16 +00002200 llvm::StringRef expr, llvm::StringRef prefix, lldb::LanguageType language,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002201 Expression::ResultType desired_type,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002202 const EvaluateExpressionOptions &options,
2203 ValueObject *ctx_obj, Status &error) {
Zachary Turner97206d52017-05-12 04:51:55 +00002204 Status type_system_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002205
2206 TypeSystem *type_system =
2207 GetScratchTypeSystemForLanguage(&type_system_error, language);
2208 UserExpression *user_expr = nullptr;
2209
2210 if (!type_system) {
2211 error.SetErrorStringWithFormat(
2212 "Could not find type system for language %s: %s",
2213 Language::GetNameForLanguageType(language),
2214 type_system_error.AsCString());
2215 return nullptr;
2216 }
2217
Zachary Turnerc5d7df92016-11-08 04:52:16 +00002218 user_expr = type_system->GetUserExpression(expr, prefix, language,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002219 desired_type, options, ctx_obj);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002220 if (!user_expr)
2221 error.SetErrorStringWithFormat(
2222 "Could not create an expression for language %s",
2223 Language::GetNameForLanguageType(language));
2224
2225 return user_expr;
Jim Ingham151c0322015-09-15 21:13:50 +00002226}
2227
Kate Stoneb9c1b512016-09-06 20:57:50 +00002228FunctionCaller *Target::GetFunctionCallerForLanguage(
2229 lldb::LanguageType language, const CompilerType &return_type,
2230 const Address &function_address, const ValueList &arg_value_list,
Zachary Turner97206d52017-05-12 04:51:55 +00002231 const char *name, Status &error) {
2232 Status type_system_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002233 TypeSystem *type_system =
2234 GetScratchTypeSystemForLanguage(&type_system_error, language);
2235 FunctionCaller *persistent_fn = nullptr;
2236
2237 if (!type_system) {
2238 error.SetErrorStringWithFormat(
2239 "Could not find type system for language %s: %s",
2240 Language::GetNameForLanguageType(language),
2241 type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00002242 return persistent_fn;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002243 }
2244
2245 persistent_fn = type_system->GetFunctionCaller(return_type, function_address,
2246 arg_value_list, name);
2247 if (!persistent_fn)
2248 error.SetErrorStringWithFormat(
2249 "Could not create an expression for language %s",
2250 Language::GetNameForLanguageType(language));
2251
2252 return persistent_fn;
Jim Ingham151c0322015-09-15 21:13:50 +00002253}
2254
2255UtilityFunction *
Kate Stoneb9c1b512016-09-06 20:57:50 +00002256Target::GetUtilityFunctionForLanguage(const char *text,
2257 lldb::LanguageType language,
Zachary Turner97206d52017-05-12 04:51:55 +00002258 const char *name, Status &error) {
2259 Status type_system_error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002260 TypeSystem *type_system =
2261 GetScratchTypeSystemForLanguage(&type_system_error, language);
2262 UtilityFunction *utility_fn = nullptr;
2263
2264 if (!type_system) {
2265 error.SetErrorStringWithFormat(
2266 "Could not find type system for language %s: %s",
2267 Language::GetNameForLanguageType(language),
2268 type_system_error.AsCString());
Jim Ingham151c0322015-09-15 21:13:50 +00002269 return utility_fn;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002270 }
2271
2272 utility_fn = type_system->GetUtilityFunction(text, name);
2273 if (!utility_fn)
2274 error.SetErrorStringWithFormat(
2275 "Could not create an expression for language %s",
2276 Language::GetNameForLanguageType(language));
2277
2278 return utility_fn;
Jim Ingham151c0322015-09-15 21:13:50 +00002279}
2280
Kate Stoneb9c1b512016-09-06 20:57:50 +00002281ClangASTContext *Target::GetScratchClangASTContext(bool create_on_demand) {
2282 if (m_valid) {
2283 if (TypeSystem *type_system = GetScratchTypeSystemForLanguage(
2284 nullptr, eLanguageTypeC, create_on_demand))
2285 return llvm::dyn_cast<ClangASTContext>(type_system);
2286 }
2287 return nullptr;
2288}
2289
2290ClangASTImporterSP Target::GetClangASTImporter() {
2291 if (m_valid) {
2292 if (!m_ast_importer_sp) {
2293 m_ast_importer_sp.reset(new ClangASTImporter());
Sean Callanan4bf80d52011-11-15 22:27:19 +00002294 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002295 return m_ast_importer_sp;
2296 }
2297 return ClangASTImporterSP();
Sean Callananb92bd752015-10-01 16:28:02 +00002298}
2299
Kate Stoneb9c1b512016-09-06 20:57:50 +00002300void Target::SettingsInitialize() { Process::SettingsInitialize(); }
2301
2302void Target::SettingsTerminate() { Process::SettingsTerminate(); }
2303
2304FileSpecList Target::GetDefaultExecutableSearchPaths() {
2305 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2306 if (properties_sp)
2307 return properties_sp->GetExecutableSearchPaths();
2308 return FileSpecList();
Sean Callanan686b2312011-11-16 18:20:47 +00002309}
2310
Kate Stoneb9c1b512016-09-06 20:57:50 +00002311FileSpecList Target::GetDefaultDebugFileSearchPaths() {
2312 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2313 if (properties_sp)
2314 return properties_sp->GetDebugFileSearchPaths();
2315 return FileSpecList();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002316}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002317
Kate Stoneb9c1b512016-09-06 20:57:50 +00002318FileSpecList Target::GetDefaultClangModuleSearchPaths() {
2319 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2320 if (properties_sp)
2321 return properties_sp->GetClangModuleSearchPaths();
2322 return FileSpecList();
Greg Clayton99d0faf2010-11-18 23:32:35 +00002323}
Caroline Ticedaccaa92010-09-20 20:44:43 +00002324
Kate Stoneb9c1b512016-09-06 20:57:50 +00002325ArchSpec Target::GetDefaultArchitecture() {
2326 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2327 if (properties_sp)
2328 return properties_sp->GetDefaultArchitecture();
2329 return ArchSpec();
Greg Claytonc859e2d2012-02-13 23:10:39 +00002330}
2331
Kate Stoneb9c1b512016-09-06 20:57:50 +00002332void Target::SetDefaultArchitecture(const ArchSpec &arch) {
2333 TargetPropertiesSP properties_sp(Target::GetGlobalProperties());
2334 if (properties_sp) {
2335 LogIfAnyCategoriesSet(
2336 LIBLLDB_LOG_TARGET, "Target::SetDefaultArchitecture setting target's "
2337 "default architecture to %s (%s)",
2338 arch.GetArchitectureName(), arch.GetTriple().getTriple().c_str());
2339 return properties_sp->SetDefaultArchitecture(arch);
2340 }
Michael Sartaina7499c92013-07-01 19:45:50 +00002341}
2342
Kate Stoneb9c1b512016-09-06 20:57:50 +00002343Target *Target::GetTargetFromContexts(const ExecutionContext *exe_ctx_ptr,
2344 const SymbolContext *sc_ptr) {
2345 // The target can either exist in the "process" of ExecutionContext, or in
2346 // the "target_sp" member of SymbolContext. This accessor helper function
2347 // will get the target from one of these locations.
2348
2349 Target *target = nullptr;
2350 if (sc_ptr != nullptr)
2351 target = sc_ptr->target_sp.get();
2352 if (target == nullptr && exe_ctx_ptr)
2353 target = exe_ctx_ptr->GetTargetPtr();
2354 return target;
Sean Callanan85054342015-04-03 15:39:47 +00002355}
2356
Kate Stoneb9c1b512016-09-06 20:57:50 +00002357ExpressionResults Target::EvaluateExpression(
Zachary Turnerc5d7df92016-11-08 04:52:16 +00002358 llvm::StringRef expr, ExecutionContextScope *exe_scope,
Kate Stoneb9c1b512016-09-06 20:57:50 +00002359 lldb::ValueObjectSP &result_valobj_sp,
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002360 const EvaluateExpressionOptions &options, std::string *fixed_expression,
2361 ValueObject *ctx_obj) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002362 result_valobj_sp.reset();
Caroline Ticedaccaa92010-09-20 20:44:43 +00002363
Kate Stoneb9c1b512016-09-06 20:57:50 +00002364 ExpressionResults execution_results = eExpressionSetupError;
Caroline Ticedaccaa92010-09-20 20:44:43 +00002365
Zachary Turnerc5d7df92016-11-08 04:52:16 +00002366 if (expr.empty())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002367 return execution_results;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002368
Adrian Prantl05097242018-04-30 16:49:04 +00002369 // We shouldn't run stop hooks in expressions. Be sure to reset this if you
2370 // return anywhere within this function.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002371 bool old_suppress_value = m_suppress_stop_hooks;
2372 m_suppress_stop_hooks = true;
2373
2374 ExecutionContext exe_ctx;
2375
2376 if (exe_scope) {
2377 exe_scope->CalculateExecutionContext(exe_ctx);
2378 } else if (m_process_sp) {
2379 m_process_sp->CalculateExecutionContext(exe_ctx);
2380 } else {
2381 CalculateExecutionContext(exe_ctx);
2382 }
2383
Adrian Prantl05097242018-04-30 16:49:04 +00002384 // Make sure we aren't just trying to see the value of a persistent variable
2385 // (something like "$0")
Kate Stoneb9c1b512016-09-06 20:57:50 +00002386 lldb::ExpressionVariableSP persistent_var_sp;
2387 // Only check for persistent variables the expression starts with a '$'
Zachary Turnerc5d7df92016-11-08 04:52:16 +00002388 if (expr[0] == '$')
Kate Stoneb9c1b512016-09-06 20:57:50 +00002389 persistent_var_sp = GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC)
2390 ->GetPersistentExpressionState()
Zachary Turnerc5d7df92016-11-08 04:52:16 +00002391 ->GetVariable(expr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002392
2393 if (persistent_var_sp) {
2394 result_valobj_sp = persistent_var_sp->GetValueObject();
2395 execution_results = eExpressionCompleted;
2396 } else {
Pavel Labath50251fc2017-12-21 10:54:30 +00002397 llvm::StringRef prefix = GetExpressionPrefixContents();
Zachary Turner97206d52017-05-12 04:51:55 +00002398 Status error;
Zachary Turnerc5d7df92016-11-08 04:52:16 +00002399 execution_results = UserExpression::Evaluate(exe_ctx, options, expr, prefix,
2400 result_valobj_sp, error,
2401 0, // Line Number
Aleksandr Urakov40624a02019-02-05 09:14:36 +00002402 fixed_expression,
2403 nullptr, // Module
2404 ctx_obj);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002405 }
2406
2407 m_suppress_stop_hooks = old_suppress_value;
2408
2409 return execution_results;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002410}
2411
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002412lldb::ExpressionVariableSP
Kate Stoneb9c1b512016-09-06 20:57:50 +00002413Target::GetPersistentVariable(const ConstString &name) {
2414 lldb::ExpressionVariableSP variable_sp;
2415 m_scratch_type_system_map.ForEach(
Zachary Turner3bc714b2017-03-02 00:05:25 +00002416 [name, &variable_sp](TypeSystem *type_system) -> bool {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002417 if (PersistentExpressionState *persistent_state =
2418 type_system->GetPersistentExpressionState()) {
2419 variable_sp = persistent_state->GetVariable(name);
Greg Clayton5beec212015-10-08 21:04:34 +00002420
Kate Stoneb9c1b512016-09-06 20:57:50 +00002421 if (variable_sp)
2422 return false; // Stop iterating the ForEach
Sean Callanan8f1f9a12015-09-30 19:57:57 +00002423 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002424 return true; // Keep iterating the ForEach
2425 });
2426 return variable_sp;
Zachary Turner32abc6e2015-03-03 19:23:09 +00002427}
2428
Kate Stoneb9c1b512016-09-06 20:57:50 +00002429lldb::addr_t Target::GetPersistentSymbol(const ConstString &name) {
2430 lldb::addr_t address = LLDB_INVALID_ADDRESS;
2431
2432 m_scratch_type_system_map.ForEach(
Zachary Turner3bc714b2017-03-02 00:05:25 +00002433 [name, &address](TypeSystem *type_system) -> bool {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002434 if (PersistentExpressionState *persistent_state =
2435 type_system->GetPersistentExpressionState()) {
2436 address = persistent_state->LookupSymbol(name);
2437 if (address != LLDB_INVALID_ADDRESS)
2438 return false; // Stop iterating the ForEach
Sean Callananb92bd752015-10-01 16:28:02 +00002439 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002440 return true; // Keep iterating the ForEach
2441 });
2442 return address;
Sean Callananb92bd752015-10-01 16:28:02 +00002443}
2444
Kate Stoneb9c1b512016-09-06 20:57:50 +00002445lldb::addr_t Target::GetCallableLoadAddress(lldb::addr_t load_addr,
2446 AddressClass addr_class) const {
Tatyana Krasnukha7aa9e7b2018-09-21 18:56:44 +00002447 auto arch_plugin = GetArchitecturePlugin();
2448 return arch_plugin ?
2449 arch_plugin->GetCallableLoadAddress(load_addr, addr_class) : load_addr;
Greg Claytonf3ef3d22011-05-22 22:46:53 +00002450}
2451
Kate Stoneb9c1b512016-09-06 20:57:50 +00002452lldb::addr_t Target::GetOpcodeLoadAddress(lldb::addr_t load_addr,
2453 AddressClass addr_class) const {
Tatyana Krasnukha7aa9e7b2018-09-21 18:56:44 +00002454 auto arch_plugin = GetArchitecturePlugin();
2455 return arch_plugin ?
2456 arch_plugin->GetOpcodeLoadAddress(load_addr, addr_class) : load_addr;
Bhushan D. Attarde7f3daed2015-08-26 06:04:54 +00002457}
2458
Kate Stoneb9c1b512016-09-06 20:57:50 +00002459lldb::addr_t Target::GetBreakableLoadAddress(lldb::addr_t addr) {
Tatyana Krasnukha7aa9e7b2018-09-21 18:56:44 +00002460 auto arch_plugin = GetArchitecturePlugin();
2461 return arch_plugin ?
2462 arch_plugin->GetBreakableLoadAddress(addr, *this) : addr;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002463}
2464
2465SourceManager &Target::GetSourceManager() {
2466 if (!m_source_manager_ap)
2467 m_source_manager_ap.reset(new SourceManager(shared_from_this()));
2468 return *m_source_manager_ap;
2469}
2470
2471ClangModulesDeclVendor *Target::GetClangModulesDeclVendor() {
2472 static std::mutex s_clang_modules_decl_vendor_mutex; // If this is contended
2473 // we can make it
2474 // per-target
2475
2476 {
2477 std::lock_guard<std::mutex> guard(s_clang_modules_decl_vendor_mutex);
2478
2479 if (!m_clang_modules_decl_vendor_ap) {
2480 m_clang_modules_decl_vendor_ap.reset(
2481 ClangModulesDeclVendor::Create(*this));
2482 }
2483 }
2484
2485 return m_clang_modules_decl_vendor_ap.get();
2486}
2487
2488Target::StopHookSP Target::CreateStopHook() {
2489 lldb::user_id_t new_uid = ++m_stop_hook_next_id;
2490 Target::StopHookSP stop_hook_sp(new StopHook(shared_from_this(), new_uid));
2491 m_stop_hooks[new_uid] = stop_hook_sp;
2492 return stop_hook_sp;
2493}
2494
2495bool Target::RemoveStopHookByID(lldb::user_id_t user_id) {
2496 size_t num_removed = m_stop_hooks.erase(user_id);
2497 return (num_removed != 0);
2498}
2499
2500void Target::RemoveAllStopHooks() { m_stop_hooks.clear(); }
2501
2502Target::StopHookSP Target::GetStopHookByID(lldb::user_id_t user_id) {
2503 StopHookSP found_hook;
2504
2505 StopHookCollection::iterator specified_hook_iter;
2506 specified_hook_iter = m_stop_hooks.find(user_id);
2507 if (specified_hook_iter != m_stop_hooks.end())
2508 found_hook = (*specified_hook_iter).second;
2509 return found_hook;
2510}
2511
2512bool Target::SetStopHookActiveStateByID(lldb::user_id_t user_id,
2513 bool active_state) {
2514 StopHookCollection::iterator specified_hook_iter;
2515 specified_hook_iter = m_stop_hooks.find(user_id);
2516 if (specified_hook_iter == m_stop_hooks.end())
2517 return false;
2518
2519 (*specified_hook_iter).second->SetIsActive(active_state);
2520 return true;
2521}
2522
2523void Target::SetAllStopHooksActiveState(bool active_state) {
2524 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2525 for (pos = m_stop_hooks.begin(); pos != end; pos++) {
2526 (*pos).second->SetIsActive(active_state);
2527 }
2528}
2529
2530void Target::RunStopHooks() {
2531 if (m_suppress_stop_hooks)
2532 return;
2533
2534 if (!m_process_sp)
2535 return;
Jim Ingham45816d62017-12-05 02:34:05 +00002536
2537 // Somebody might have restarted the process:
2538 if (m_process_sp->GetState() != eStateStopped)
2539 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002540
2541 // <rdar://problem/12027563> make sure we check that we are not stopped
Adrian Prantl05097242018-04-30 16:49:04 +00002542 // because of us running a user expression since in that case we do not want
2543 // to run the stop-hooks
Kate Stoneb9c1b512016-09-06 20:57:50 +00002544 if (m_process_sp->GetModIDRef().IsLastResumeForUserExpression())
2545 return;
2546
2547 if (m_stop_hooks.empty())
2548 return;
2549
2550 StopHookCollection::iterator pos, end = m_stop_hooks.end();
2551
2552 // If there aren't any active stop hooks, don't bother either:
2553 bool any_active_hooks = false;
2554 for (pos = m_stop_hooks.begin(); pos != end; pos++) {
2555 if ((*pos).second->IsActive()) {
2556 any_active_hooks = true;
2557 break;
2558 }
2559 }
2560 if (!any_active_hooks)
2561 return;
2562
2563 CommandReturnObject result;
2564
2565 std::vector<ExecutionContext> exc_ctx_with_reasons;
2566 std::vector<SymbolContext> sym_ctx_with_reasons;
2567
2568 ThreadList &cur_threadlist = m_process_sp->GetThreadList();
2569 size_t num_threads = cur_threadlist.GetSize();
2570 for (size_t i = 0; i < num_threads; i++) {
2571 lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex(i);
2572 if (cur_thread_sp->ThreadStoppedForAReason()) {
2573 lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(0);
2574 exc_ctx_with_reasons.push_back(ExecutionContext(
2575 m_process_sp.get(), cur_thread_sp.get(), cur_frame_sp.get()));
2576 sym_ctx_with_reasons.push_back(
2577 cur_frame_sp->GetSymbolContext(eSymbolContextEverything));
2578 }
2579 }
2580
2581 // If no threads stopped for a reason, don't run the stop-hooks.
2582 size_t num_exe_ctx = exc_ctx_with_reasons.size();
2583 if (num_exe_ctx == 0)
2584 return;
2585
2586 result.SetImmediateOutputStream(m_debugger.GetAsyncOutputStream());
2587 result.SetImmediateErrorStream(m_debugger.GetAsyncErrorStream());
2588
2589 bool keep_going = true;
2590 bool hooks_ran = false;
2591 bool print_hook_header = (m_stop_hooks.size() != 1);
2592 bool print_thread_header = (num_exe_ctx != 1);
2593
2594 for (pos = m_stop_hooks.begin(); keep_going && pos != end; pos++) {
2595 // result.Clear();
2596 StopHookSP cur_hook_sp = (*pos).second;
2597 if (!cur_hook_sp->IsActive())
2598 continue;
2599
2600 bool any_thread_matched = false;
2601 for (size_t i = 0; keep_going && i < num_exe_ctx; i++) {
2602 if ((cur_hook_sp->GetSpecifier() == nullptr ||
2603 cur_hook_sp->GetSpecifier()->SymbolContextMatches(
2604 sym_ctx_with_reasons[i])) &&
2605 (cur_hook_sp->GetThreadSpecifier() == nullptr ||
2606 cur_hook_sp->GetThreadSpecifier()->ThreadPassesBasicTests(
2607 exc_ctx_with_reasons[i].GetThreadRef()))) {
2608 if (!hooks_ran) {
2609 hooks_ran = true;
2610 }
2611 if (print_hook_header && !any_thread_matched) {
2612 const char *cmd =
2613 (cur_hook_sp->GetCommands().GetSize() == 1
2614 ? cur_hook_sp->GetCommands().GetStringAtIndex(0)
2615 : nullptr);
2616 if (cmd)
2617 result.AppendMessageWithFormat("\n- Hook %" PRIu64 " (%s)\n",
2618 cur_hook_sp->GetID(), cmd);
2619 else
2620 result.AppendMessageWithFormat("\n- Hook %" PRIu64 "\n",
2621 cur_hook_sp->GetID());
2622 any_thread_matched = true;
2623 }
2624
2625 if (print_thread_header)
2626 result.AppendMessageWithFormat(
2627 "-- Thread %d\n",
2628 exc_ctx_with_reasons[i].GetThreadPtr()->GetIndexID());
2629
2630 CommandInterpreterRunOptions options;
2631 options.SetStopOnContinue(true);
2632 options.SetStopOnError(true);
2633 options.SetEchoCommands(false);
2634 options.SetPrintResults(true);
2635 options.SetAddToHistory(false);
2636
2637 GetDebugger().GetCommandInterpreter().HandleCommands(
2638 cur_hook_sp->GetCommands(), &exc_ctx_with_reasons[i], options,
2639 result);
2640
2641 // If the command started the target going again, we should bag out of
2642 // running the stop hooks.
2643 if ((result.GetStatus() == eReturnStatusSuccessContinuingNoResult) ||
2644 (result.GetStatus() == eReturnStatusSuccessContinuingResult)) {
Jim Ingham45816d62017-12-05 02:34:05 +00002645 // But only complain if there were more stop hooks to do:
2646 StopHookCollection::iterator tmp = pos;
2647 if (++tmp != end)
2648 result.AppendMessageWithFormat("\nAborting stop hooks, hook %" PRIu64
2649 " set the program running.\n",
2650 cur_hook_sp->GetID());
Kate Stoneb9c1b512016-09-06 20:57:50 +00002651 keep_going = false;
2652 }
2653 }
2654 }
2655 }
2656
2657 result.GetImmediateOutputStream()->Flush();
2658 result.GetImmediateErrorStream()->Flush();
2659}
2660
2661const TargetPropertiesSP &Target::GetGlobalProperties() {
2662 // NOTE: intentional leak so we don't crash if global destructor chain gets
2663 // called as other threads still use the result of this function
Pavel Labath5f05ea82016-10-19 15:12:45 +00002664 static TargetPropertiesSP *g_settings_sp_ptr =
2665 new TargetPropertiesSP(new TargetProperties(nullptr));
Kate Stoneb9c1b512016-09-06 20:57:50 +00002666 return *g_settings_sp_ptr;
2667}
2668
Zachary Turner97206d52017-05-12 04:51:55 +00002669Status Target::Install(ProcessLaunchInfo *launch_info) {
2670 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002671 PlatformSP platform_sp(GetPlatform());
2672 if (platform_sp) {
2673 if (platform_sp->IsRemote()) {
2674 if (platform_sp->IsConnected()) {
2675 // Install all files that have an install path, and always install the
2676 // main executable when connected to a remote platform
2677 const ModuleList &modules = GetImages();
2678 const size_t num_images = modules.GetSize();
2679 for (size_t idx = 0; idx < num_images; ++idx) {
2680 ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2681 if (module_sp) {
2682 const bool is_main_executable = module_sp == GetExecutableModule();
2683 FileSpec local_file(module_sp->GetFileSpec());
2684 if (local_file) {
2685 FileSpec remote_file(module_sp->GetRemoteInstallFileSpec());
2686 if (!remote_file) {
2687 if (is_main_executable) // TODO: add setting for always
2688 // installing main executable???
2689 {
2690 // Always install the main executable
2691 remote_file = platform_sp->GetRemoteWorkingDirectory();
2692 remote_file.AppendPathComponent(
2693 module_sp->GetFileSpec().GetFilename().GetCString());
2694 }
2695 }
2696 if (remote_file) {
2697 error = platform_sp->Install(local_file, remote_file);
2698 if (error.Success()) {
2699 module_sp->SetPlatformFileSpec(remote_file);
2700 if (is_main_executable) {
2701 platform_sp->SetFilePermissions(remote_file, 0700);
2702 if (launch_info)
2703 launch_info->SetExecutableFile(remote_file, false);
2704 }
2705 } else
2706 break;
2707 }
2708 }
2709 }
2710 }
2711 }
2712 }
2713 }
2714 return error;
2715}
2716
2717bool Target::ResolveLoadAddress(addr_t load_addr, Address &so_addr,
2718 uint32_t stop_id) {
2719 return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr);
2720}
2721
2722bool Target::ResolveFileAddress(lldb::addr_t file_addr,
2723 Address &resolved_addr) {
2724 return m_images.ResolveFileAddress(file_addr, resolved_addr);
2725}
2726
2727bool Target::SetSectionLoadAddress(const SectionSP &section_sp,
2728 addr_t new_section_load_addr,
2729 bool warn_multiple) {
2730 const addr_t old_section_load_addr =
2731 m_section_load_history.GetSectionLoadAddress(
2732 SectionLoadHistory::eStopIDNow, section_sp);
2733 if (old_section_load_addr != new_section_load_addr) {
2734 uint32_t stop_id = 0;
2735 ProcessSP process_sp(GetProcessSP());
2736 if (process_sp)
2737 stop_id = process_sp->GetStopID();
Greg Claytonb09c5382013-12-13 17:20:18 +00002738 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00002739 stop_id = m_section_load_history.GetLastStopID();
2740 if (m_section_load_history.SetSectionLoadAddress(
2741 stop_id, section_sp, new_section_load_addr, warn_multiple))
2742 return true; // Return true if the section load address was changed...
2743 }
2744 return false; // Return false to indicate nothing changed
2745}
Todd Fialaac33cc92014-10-09 01:02:08 +00002746
Kate Stoneb9c1b512016-09-06 20:57:50 +00002747size_t Target::UnloadModuleSections(const ModuleList &module_list) {
2748 size_t section_unload_count = 0;
2749 size_t num_modules = module_list.GetSize();
2750 for (size_t i = 0; i < num_modules; ++i) {
2751 section_unload_count +=
2752 UnloadModuleSections(module_list.GetModuleAtIndex(i));
2753 }
2754 return section_unload_count;
2755}
Todd Fiala015d8182014-07-22 23:41:36 +00002756
Kate Stoneb9c1b512016-09-06 20:57:50 +00002757size_t Target::UnloadModuleSections(const lldb::ModuleSP &module_sp) {
2758 uint32_t stop_id = 0;
2759 ProcessSP process_sp(GetProcessSP());
2760 if (process_sp)
2761 stop_id = process_sp->GetStopID();
2762 else
2763 stop_id = m_section_load_history.GetLastStopID();
2764 SectionList *sections = module_sp->GetSectionList();
2765 size_t section_unload_count = 0;
2766 if (sections) {
2767 const uint32_t num_sections = sections->GetNumSections(0);
2768 for (uint32_t i = 0; i < num_sections; ++i) {
2769 section_unload_count += m_section_load_history.SetSectionUnloaded(
2770 stop_id, sections->GetSectionAtIndex(i));
Greg Claytonb09c5382013-12-13 17:20:18 +00002771 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002772 }
2773 return section_unload_count;
2774}
2775
2776bool Target::SetSectionUnloaded(const lldb::SectionSP &section_sp) {
2777 uint32_t stop_id = 0;
2778 ProcessSP process_sp(GetProcessSP());
2779 if (process_sp)
2780 stop_id = process_sp->GetStopID();
2781 else
2782 stop_id = m_section_load_history.GetLastStopID();
2783 return m_section_load_history.SetSectionUnloaded(stop_id, section_sp);
2784}
2785
2786bool Target::SetSectionUnloaded(const lldb::SectionSP &section_sp,
2787 addr_t load_addr) {
2788 uint32_t stop_id = 0;
2789 ProcessSP process_sp(GetProcessSP());
2790 if (process_sp)
2791 stop_id = process_sp->GetStopID();
2792 else
2793 stop_id = m_section_load_history.GetLastStopID();
2794 return m_section_load_history.SetSectionUnloaded(stop_id, section_sp,
2795 load_addr);
2796}
2797
2798void Target::ClearAllLoadedSections() { m_section_load_history.Clear(); }
2799
Zachary Turner97206d52017-05-12 04:51:55 +00002800Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) {
2801 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002802 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_TARGET));
2803
2804 if (log)
2805 log->Printf("Target::%s() called for %s", __FUNCTION__,
2806 launch_info.GetExecutableFile().GetPath().c_str());
2807
2808 StateType state = eStateInvalid;
2809
2810 // Scope to temporarily get the process state in case someone has manually
2811 // remotely connected already to a process and we can skip the platform
2812 // launching.
2813 {
2814 ProcessSP process_sp(GetProcessSP());
2815
2816 if (process_sp) {
2817 state = process_sp->GetState();
2818 if (log)
2819 log->Printf(
2820 "Target::%s the process exists, and its current state is %s",
2821 __FUNCTION__, StateAsCString(state));
2822 } else {
2823 if (log)
2824 log->Printf("Target::%s the process instance doesn't currently exist.",
2825 __FUNCTION__);
2826 }
2827 }
2828
2829 launch_info.GetFlags().Set(eLaunchFlagDebug);
2830
2831 // Get the value of synchronous execution here. If you wait till after you
Adrian Prantl05097242018-04-30 16:49:04 +00002832 // have started to run, then you could have hit a breakpoint, whose command
2833 // might switch the value, and then you'll pick up that incorrect value.
Kate Stoneb9c1b512016-09-06 20:57:50 +00002834 Debugger &debugger = GetDebugger();
2835 const bool synchronous_execution =
2836 debugger.GetCommandInterpreter().GetSynchronous();
2837
2838 PlatformSP platform_sp(GetPlatform());
2839
Pavel Labath8e55dde2019-01-08 11:55:19 +00002840 FinalizeFileActions(launch_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002841
2842 if (state == eStateConnected) {
2843 if (launch_info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
2844 error.SetErrorString(
2845 "can't launch in tty when launching through a remote connection");
2846 return error;
2847 }
2848 }
2849
2850 if (!launch_info.GetArchitecture().IsValid())
2851 launch_info.GetArchitecture() = GetArchitecture();
2852
2853 // If we're not already connected to the process, and if we have a platform
2854 // that can launch a process for debugging, go ahead and do that here.
2855 if (state != eStateConnected && platform_sp &&
2856 platform_sp->CanDebugProcess()) {
2857 if (log)
2858 log->Printf("Target::%s asking the platform to debug the process",
2859 __FUNCTION__);
2860
Jim Ingham362d0222018-12-18 01:49:02 +00002861 // If there was a previous process, delete it before we make the new one.
2862 // One subtle point, we delete the process before we release the reference
2863 // to m_process_sp. That way even if we are the last owner, the process
2864 // will get Finalized before it gets destroyed.
2865 DeleteCurrentProcess();
2866
Kate Stoneb9c1b512016-09-06 20:57:50 +00002867 m_process_sp =
2868 GetPlatform()->DebugProcess(launch_info, debugger, this, error);
2869
Kate Stoneb9c1b512016-09-06 20:57:50 +00002870 } else {
2871 if (log)
2872 log->Printf("Target::%s the platform doesn't know how to debug a "
2873 "process, getting a process plugin to do this for us.",
2874 __FUNCTION__);
2875
2876 if (state == eStateConnected) {
2877 assert(m_process_sp);
2878 } else {
2879 // Use a Process plugin to construct the process.
2880 const char *plugin_name = launch_info.GetProcessPluginName();
Pavel Labathcb8c6992019-01-07 10:59:57 +00002881 CreateProcess(launch_info.GetListener(), plugin_name, nullptr);
Greg Claytonb09c5382013-12-13 17:20:18 +00002882 }
2883
Kate Stoneb9c1b512016-09-06 20:57:50 +00002884 // Since we didn't have a platform launch the process, launch it here.
2885 if (m_process_sp)
2886 error = m_process_sp->Launch(launch_info);
2887 }
2888
2889 if (!m_process_sp) {
Greg Claytonb09c5382013-12-13 17:20:18 +00002890 if (error.Success())
Kate Stoneb9c1b512016-09-06 20:57:50 +00002891 error.SetErrorString("failed to launch or debug process");
Greg Claytonb09c5382013-12-13 17:20:18 +00002892 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002893 }
2894
2895 if (error.Success()) {
2896 if (synchronous_execution ||
2897 !launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) {
2898 ListenerSP hijack_listener_sp(launch_info.GetHijackListener());
2899 if (!hijack_listener_sp) {
2900 hijack_listener_sp =
2901 Listener::MakeListener("lldb.Target.Launch.hijack");
2902 launch_info.SetHijackListener(hijack_listener_sp);
2903 m_process_sp->HijackProcessEvents(hijack_listener_sp);
2904 }
2905
2906 StateType state = m_process_sp->WaitForProcessToStop(
Pavel Labathe3e21cf2016-11-30 11:56:32 +00002907 llvm::None, nullptr, false, hijack_listener_sp, nullptr);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002908
2909 if (state == eStateStopped) {
2910 if (!launch_info.GetFlags().Test(eLaunchFlagStopAtEntry)) {
2911 if (synchronous_execution) {
2912 error = m_process_sp->PrivateResume();
2913 if (error.Success()) {
2914 state = m_process_sp->WaitForProcessToStop(
Pavel Labathe3e21cf2016-11-30 11:56:32 +00002915 llvm::None, nullptr, true, hijack_listener_sp, stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +00002916 const bool must_be_alive =
2917 false; // eStateExited is ok, so this must be false
2918 if (!StateIsStoppedState(state, must_be_alive)) {
2919 error.SetErrorStringWithFormat("process isn't stopped: %s",
2920 StateAsCString(state));
2921 }
2922 }
2923 } else {
2924 m_process_sp->RestoreProcessEvents();
2925 error = m_process_sp->PrivateResume();
2926 }
2927 if (!error.Success()) {
Zachary Turner97206d52017-05-12 04:51:55 +00002928 Status error2;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002929 error2.SetErrorStringWithFormat(
2930 "process resume at entry point failed: %s", error.AsCString());
2931 error = error2;
2932 }
2933 }
2934 } else if (state == eStateExited) {
2935 bool with_shell = !!launch_info.GetShell();
2936 const int exit_status = m_process_sp->GetExitStatus();
2937 const char *exit_desc = m_process_sp->GetExitDescription();
2938#define LAUNCH_SHELL_MESSAGE \
2939 "\n'r' and 'run' are aliases that default to launching through a " \
2940 "shell.\nTry launching without going through a shell by using 'process " \
2941 "launch'."
2942 if (exit_desc && exit_desc[0]) {
2943 if (with_shell)
2944 error.SetErrorStringWithFormat(
2945 "process exited with status %i (%s)" LAUNCH_SHELL_MESSAGE,
2946 exit_status, exit_desc);
2947 else
2948 error.SetErrorStringWithFormat("process exited with status %i (%s)",
2949 exit_status, exit_desc);
2950 } else {
2951 if (with_shell)
2952 error.SetErrorStringWithFormat(
2953 "process exited with status %i" LAUNCH_SHELL_MESSAGE,
2954 exit_status);
2955 else
2956 error.SetErrorStringWithFormat("process exited with status %i",
2957 exit_status);
2958 }
2959 } else {
2960 error.SetErrorStringWithFormat(
2961 "initial process state wasn't stopped: %s", StateAsCString(state));
2962 }
2963 }
2964 m_process_sp->RestoreProcessEvents();
2965 } else {
Zachary Turner97206d52017-05-12 04:51:55 +00002966 Status error2;
Kate Stoneb9c1b512016-09-06 20:57:50 +00002967 error2.SetErrorStringWithFormat("process launch failed: %s",
2968 error.AsCString());
2969 error = error2;
2970 }
2971 return error;
Greg Claytonb09c5382013-12-13 17:20:18 +00002972}
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002973
Zachary Turner97206d52017-05-12 04:51:55 +00002974Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00002975 auto state = eStateInvalid;
2976 auto process_sp = GetProcessSP();
2977 if (process_sp) {
2978 state = process_sp->GetState();
2979 if (process_sp->IsAlive() && state != eStateConnected) {
2980 if (state == eStateAttaching)
Zachary Turner97206d52017-05-12 04:51:55 +00002981 return Status("process attach is in progress");
2982 return Status("a process is already being debugged");
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002983 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002984 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002985
Kate Stoneb9c1b512016-09-06 20:57:50 +00002986 const ModuleSP old_exec_module_sp = GetExecutableModule();
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002987
Adrian Prantl05097242018-04-30 16:49:04 +00002988 // If no process info was specified, then use the target executable name as
2989 // the process to attach to by default
Kate Stoneb9c1b512016-09-06 20:57:50 +00002990 if (!attach_info.ProcessInfoSpecified()) {
2991 if (old_exec_module_sp)
2992 attach_info.GetExecutableFile().GetFilename() =
2993 old_exec_module_sp->GetPlatformFileSpec().GetFilename();
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002994
Kate Stoneb9c1b512016-09-06 20:57:50 +00002995 if (!attach_info.ProcessInfoSpecified()) {
Zachary Turner97206d52017-05-12 04:51:55 +00002996 return Status("no process specified, create a target with a file, or "
2997 "specify the --pid or --name");
Oleksiy Vyalov37386142015-02-10 22:49:57 +00002998 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00002999 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003000
Kate Stoneb9c1b512016-09-06 20:57:50 +00003001 const auto platform_sp =
3002 GetDebugger().GetPlatformList().GetSelectedPlatform();
3003 ListenerSP hijack_listener_sp;
3004 const bool async = attach_info.GetAsync();
3005 if (!async) {
3006 hijack_listener_sp =
3007 Listener::MakeListener("lldb.Target.Attach.attach.hijack");
3008 attach_info.SetHijackListener(hijack_listener_sp);
3009 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003010
Zachary Turner97206d52017-05-12 04:51:55 +00003011 Status error;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003012 if (state != eStateConnected && platform_sp != nullptr &&
3013 platform_sp->CanDebugProcess()) {
3014 SetPlatform(platform_sp);
3015 process_sp = platform_sp->Attach(attach_info, GetDebugger(), this, error);
3016 } else {
3017 if (state != eStateConnected) {
3018 const char *plugin_name = attach_info.GetProcessPluginName();
3019 process_sp =
3020 CreateProcess(attach_info.GetListenerForProcess(GetDebugger()),
3021 plugin_name, nullptr);
3022 if (process_sp == nullptr) {
3023 error.SetErrorStringWithFormat(
3024 "failed to create process using plugin %s",
3025 (plugin_name) ? plugin_name : "null");
3026 return error;
3027 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003028 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003029 if (hijack_listener_sp)
3030 process_sp->HijackProcessEvents(hijack_listener_sp);
3031 error = process_sp->Attach(attach_info);
3032 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003033
Kate Stoneb9c1b512016-09-06 20:57:50 +00003034 if (error.Success() && process_sp) {
3035 if (async) {
3036 process_sp->RestoreProcessEvents();
3037 } else {
3038 state = process_sp->WaitForProcessToStop(
Pavel Labathe3e21cf2016-11-30 11:56:32 +00003039 llvm::None, nullptr, false, attach_info.GetHijackListener(), stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003040 process_sp->RestoreProcessEvents();
3041
3042 if (state != eStateStopped) {
3043 const char *exit_desc = process_sp->GetExitDescription();
3044 if (exit_desc)
3045 error.SetErrorStringWithFormat("%s", exit_desc);
Greg Claytone75e5d82015-10-20 00:14:20 +00003046 else
Kate Stoneb9c1b512016-09-06 20:57:50 +00003047 error.SetErrorString(
3048 "process did not stop (no such process or permission problem?)");
3049 process_sp->Destroy(false);
3050 }
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003051 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003052 }
3053 return error;
Oleksiy Vyalov37386142015-02-10 22:49:57 +00003054}
3055
Pavel Labath8e55dde2019-01-08 11:55:19 +00003056void Target::FinalizeFileActions(ProcessLaunchInfo &info) {
3057 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
3058
3059 // Finalize the file actions, and if none were given, default to opening up a
3060 // pseudo terminal
3061 PlatformSP platform_sp = GetPlatform();
3062 const bool default_to_use_pty =
3063 m_platform_sp ? m_platform_sp->IsHost() : false;
3064 LLDB_LOG(
3065 log,
3066 "have platform={0}, platform_sp->IsHost()={1}, default_to_use_pty={2}",
3067 bool(platform_sp),
3068 platform_sp ? (platform_sp->IsHost() ? "true" : "false") : "n/a",
3069 default_to_use_pty);
3070
3071 // If nothing for stdin or stdout or stderr was specified, then check the
3072 // process for any default settings that were set with "settings set"
3073 if (info.GetFileActionForFD(STDIN_FILENO) == nullptr ||
3074 info.GetFileActionForFD(STDOUT_FILENO) == nullptr ||
3075 info.GetFileActionForFD(STDERR_FILENO) == nullptr) {
3076 LLDB_LOG(log, "at least one of stdin/stdout/stderr was not set, evaluating "
3077 "default handling");
3078
3079 if (info.GetFlags().Test(eLaunchFlagLaunchInTTY)) {
3080 // Do nothing, if we are launching in a remote terminal no file actions
3081 // should be done at all.
3082 return;
3083 }
3084
3085 if (info.GetFlags().Test(eLaunchFlagDisableSTDIO)) {
3086 LLDB_LOG(log, "eLaunchFlagDisableSTDIO set, adding suppression action "
3087 "for stdin, stdout and stderr");
3088 info.AppendSuppressFileAction(STDIN_FILENO, true, false);
3089 info.AppendSuppressFileAction(STDOUT_FILENO, false, true);
3090 info.AppendSuppressFileAction(STDERR_FILENO, false, true);
3091 } else {
3092 // Check for any values that might have gotten set with any of: (lldb)
3093 // settings set target.input-path (lldb) settings set target.output-path
3094 // (lldb) settings set target.error-path
3095 FileSpec in_file_spec;
3096 FileSpec out_file_spec;
3097 FileSpec err_file_spec;
3098 // Only override with the target settings if we don't already have an
3099 // action for in, out or error
3100 if (info.GetFileActionForFD(STDIN_FILENO) == nullptr)
3101 in_file_spec = GetStandardInputPath();
3102 if (info.GetFileActionForFD(STDOUT_FILENO) == nullptr)
3103 out_file_spec = GetStandardOutputPath();
3104 if (info.GetFileActionForFD(STDERR_FILENO) == nullptr)
3105 err_file_spec = GetStandardErrorPath();
3106
3107 LLDB_LOG(log, "target stdin='{0}', target stdout='{1}', stderr='{1}'",
3108 in_file_spec, out_file_spec, err_file_spec);
3109
3110 if (in_file_spec) {
3111 info.AppendOpenFileAction(STDIN_FILENO, in_file_spec, true, false);
3112 LLDB_LOG(log, "appended stdin open file action for {0}", in_file_spec);
3113 }
3114
3115 if (out_file_spec) {
3116 info.AppendOpenFileAction(STDOUT_FILENO, out_file_spec, false, true);
3117 LLDB_LOG(log, "appended stdout open file action for {0}",
3118 out_file_spec);
3119 }
3120
3121 if (err_file_spec) {
3122 info.AppendOpenFileAction(STDERR_FILENO, err_file_spec, false, true);
3123 LLDB_LOG(log, "appended stderr open file action for {0}",
3124 err_file_spec);
3125 }
3126
3127 if (default_to_use_pty &&
3128 (!in_file_spec || !out_file_spec || !err_file_spec)) {
3129 llvm::Error Err = info.SetUpPtyRedirection();
3130 LLDB_LOG_ERROR(log, std::move(Err), "SetUpPtyRedirection failed: {0}");
3131 }
3132 }
3133 }
3134}
3135
Jim Ingham9575d842011-03-11 03:53:59 +00003136//--------------------------------------------------------------
Greg Claytonfbb76342013-11-20 21:07:01 +00003137// Target::StopHook
Jim Ingham9575d842011-03-11 03:53:59 +00003138//--------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003139Target::StopHook::StopHook(lldb::TargetSP target_sp, lldb::user_id_t uid)
3140 : UserID(uid), m_target_sp(target_sp), m_commands(), m_specifier_sp(),
3141 m_thread_spec_ap(), m_active(true) {}
3142
3143Target::StopHook::StopHook(const StopHook &rhs)
3144 : UserID(rhs.GetID()), m_target_sp(rhs.m_target_sp),
3145 m_commands(rhs.m_commands), m_specifier_sp(rhs.m_specifier_sp),
3146 m_thread_spec_ap(), m_active(rhs.m_active) {
3147 if (rhs.m_thread_spec_ap)
3148 m_thread_spec_ap.reset(new ThreadSpec(*rhs.m_thread_spec_ap.get()));
Jim Ingham9575d842011-03-11 03:53:59 +00003149}
3150
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003151Target::StopHook::~StopHook() = default;
Jim Ingham9575d842011-03-11 03:53:59 +00003152
Kate Stoneb9c1b512016-09-06 20:57:50 +00003153void Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier) {
3154 m_specifier_sp.reset(specifier);
Zachary Turner32abc6e2015-03-03 19:23:09 +00003155}
3156
Kate Stoneb9c1b512016-09-06 20:57:50 +00003157void Target::StopHook::SetThreadSpecifier(ThreadSpec *specifier) {
3158 m_thread_spec_ap.reset(specifier);
Jim Ingham9575d842011-03-11 03:53:59 +00003159}
Jim Ingham9575d842011-03-11 03:53:59 +00003160
Kate Stoneb9c1b512016-09-06 20:57:50 +00003161void Target::StopHook::GetDescription(Stream *s,
3162 lldb::DescriptionLevel level) const {
3163 int indent_level = s->GetIndentLevel();
Jim Ingham9575d842011-03-11 03:53:59 +00003164
Kate Stoneb9c1b512016-09-06 20:57:50 +00003165 s->SetIndentLevel(indent_level + 2);
3166
3167 s->Printf("Hook: %" PRIu64 "\n", GetID());
3168 if (m_active)
3169 s->Indent("State: enabled\n");
3170 else
3171 s->Indent("State: disabled\n");
3172
3173 if (m_specifier_sp) {
3174 s->Indent();
3175 s->PutCString("Specifier:\n");
3176 s->SetIndentLevel(indent_level + 4);
3177 m_specifier_sp->GetDescription(s, level);
Jim Ingham9575d842011-03-11 03:53:59 +00003178 s->SetIndentLevel(indent_level + 2);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003179 }
Jim Ingham9575d842011-03-11 03:53:59 +00003180
Kate Stoneb9c1b512016-09-06 20:57:50 +00003181 if (m_thread_spec_ap) {
3182 StreamString tmp;
3183 s->Indent("Thread:\n");
3184 m_thread_spec_ap->GetDescription(&tmp, level);
3185 s->SetIndentLevel(indent_level + 4);
Zachary Turnerc1564272016-11-16 21:15:24 +00003186 s->Indent(tmp.GetString());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003187 s->PutCString("\n");
3188 s->SetIndentLevel(indent_level + 2);
3189 }
Jim Ingham9575d842011-03-11 03:53:59 +00003190
Kate Stoneb9c1b512016-09-06 20:57:50 +00003191 s->Indent("Commands: \n");
3192 s->SetIndentLevel(indent_level + 4);
3193 uint32_t num_commands = m_commands.GetSize();
3194 for (uint32_t i = 0; i < num_commands; i++) {
3195 s->Indent(m_commands.GetStringAtIndex(i));
3196 s->PutCString("\n");
3197 }
3198 s->SetIndentLevel(indent_level);
Jim Ingham9575d842011-03-11 03:53:59 +00003199}
3200
Greg Clayton67cc0632012-08-22 17:17:09 +00003201//--------------------------------------------------------------
3202// class TargetProperties
3203//--------------------------------------------------------------
3204
Jonas Devliegheree103ae92018-11-15 01:18:15 +00003205// clang-format off
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003206static constexpr OptionEnumValueElement g_dynamic_value_types[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003207 {eNoDynamicValues, "no-dynamic-values",
3208 "Don't calculate the dynamic type of values"},
3209 {eDynamicCanRunTarget, "run-target", "Calculate the dynamic type of values "
3210 "even if you have to run the target."},
3211 {eDynamicDontRunTarget, "no-run-target",
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003212 "Calculate the dynamic type of values, but don't run the target."} };
Caroline Ticedaccaa92010-09-20 20:44:43 +00003213
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003214OptionEnumValues lldb_private::GetDynamicValueTypes() {
3215 return OptionEnumValues(g_dynamic_value_types);
3216}
3217
3218static constexpr OptionEnumValueElement g_inline_breakpoint_enums[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003219 {eInlineBreakpointsNever, "never", "Never look for inline breakpoint "
3220 "locations (fastest). This setting "
3221 "should only be used if you know that "
3222 "no inlining occurs in your programs."},
3223 {eInlineBreakpointsHeaders, "headers",
3224 "Only check for inline breakpoint locations when setting breakpoints in "
3225 "header files, but not when setting breakpoint in implementation source "
3226 "files (default)."},
3227 {eInlineBreakpointsAlways, "always",
3228 "Always look for inline breakpoint locations when setting file and line "
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003229 "breakpoints (slower but most accurate)."} };
Greg Clayton1f746072012-08-29 21:13:06 +00003230
Kate Stoneb9c1b512016-09-06 20:57:50 +00003231typedef enum x86DisassemblyFlavor {
3232 eX86DisFlavorDefault,
3233 eX86DisFlavorIntel,
3234 eX86DisFlavorATT
Jim Ingham0f063ba2013-03-02 00:26:47 +00003235} x86DisassemblyFlavor;
3236
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003237static constexpr OptionEnumValueElement g_x86_dis_flavor_value_types[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003238 {eX86DisFlavorDefault, "default", "Disassembler default (currently att)."},
3239 {eX86DisFlavorIntel, "intel", "Intel disassembler flavor."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003240 {eX86DisFlavorATT, "att", "AT&T disassembler flavor."} };
Kate Stoneb9c1b512016-09-06 20:57:50 +00003241
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003242static constexpr OptionEnumValueElement g_hex_immediate_style_values[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003243 {Disassembler::eHexStyleC, "c", "C-style (0xffff)."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003244 {Disassembler::eHexStyleAsm, "asm", "Asm-style (0ffffh)."} };
Kate Stoneb9c1b512016-09-06 20:57:50 +00003245
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003246static constexpr OptionEnumValueElement g_load_script_from_sym_file_values[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003247 {eLoadScriptFromSymFileTrue, "true",
3248 "Load debug scripts inside symbol files"},
3249 {eLoadScriptFromSymFileFalse, "false",
3250 "Do not load debug scripts inside symbol files."},
3251 {eLoadScriptFromSymFileWarn, "warn",
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003252 "Warn about debug scripts inside symbol files but do not load them."} };
Kate Stoneb9c1b512016-09-06 20:57:50 +00003253
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003254static constexpr
3255OptionEnumValueElement g_load_current_working_dir_lldbinit_values[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003256 {eLoadCWDlldbinitTrue, "true",
3257 "Load .lldbinit files from current directory"},
3258 {eLoadCWDlldbinitFalse, "false",
3259 "Do not load .lldbinit files from current directory"},
3260 {eLoadCWDlldbinitWarn, "warn",
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003261 "Warn about loading .lldbinit files from current directory"} };
Kate Stoneb9c1b512016-09-06 20:57:50 +00003262
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003263static constexpr OptionEnumValueElement g_memory_module_load_level_values[] = {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003264 {eMemoryModuleLoadLevelMinimal, "minimal",
3265 "Load minimal information when loading modules from memory. Currently "
3266 "this setting loads sections only."},
3267 {eMemoryModuleLoadLevelPartial, "partial",
3268 "Load partial information when loading modules from memory. Currently "
3269 "this setting loads sections and function bounds."},
3270 {eMemoryModuleLoadLevelComplete, "complete",
3271 "Load complete information when loading modules from memory. Currently "
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003272 "this setting loads sections and all symbols."} };
Kate Stoneb9c1b512016-09-06 20:57:50 +00003273
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003274static constexpr PropertyDefinition g_properties[] = {
3275 {"default-arch", OptionValue::eTypeArch, true, 0, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003276 "Default architecture to choose, when there's a choice."},
3277 {"move-to-nearest-code", OptionValue::eTypeBoolean, false, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003278 {}, "Move breakpoints to nearest code."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003279 {"language", OptionValue::eTypeLanguage, false, eLanguageTypeUnknown,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003280 nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003281 "The language to use when interpreting expressions entered in commands."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003282 {"expr-prefix", OptionValue::eTypeFileSpec, false, 0, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003283 "Path to a file containing expressions to be prepended to all "
3284 "expressions."},
3285 {"prefer-dynamic-value", OptionValue::eTypeEnum, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003286 eDynamicDontRunTarget, nullptr, OptionEnumValues(g_dynamic_value_types),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003287 "Should printed values be shown as their dynamic value."},
3288 {"enable-synthetic-value", OptionValue::eTypeBoolean, false, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003289 {}, "Should synthetic values be used by default whenever available."},
3290 {"skip-prologue", OptionValue::eTypeBoolean, false, true, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003291 "Skip function prologues when setting breakpoints by name."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003292 {"source-map", OptionValue::eTypePathMap, false, 0, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003293 "Source path remappings are used to track the change of location between "
3294 "a source file when built, and "
3295 "where it exists on the current system. It consists of an array of "
3296 "duples, the first element of each duple is "
3297 "some part (starting at the root) of the path to the file when it was "
3298 "built, "
3299 "and the second is where the remainder of the original build hierarchy is "
3300 "rooted on the local system. "
3301 "Each element of the array is checked in order and the first one that "
3302 "results in a match wins."},
3303 {"exec-search-paths", OptionValue::eTypeFileSpecList, false, 0, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003304 {}, "Executable search paths to use when locating executable files "
3305 "whose paths don't match the local file system."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003306 {"debug-file-search-paths", OptionValue::eTypeFileSpecList, false, 0,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003307 nullptr, {},
Jan Kratochvil4c993ce2019-01-03 23:11:06 +00003308 "List of directories to be searched when locating debug symbol files. "
3309 "See also symbols.enable-external-lookup."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003310 {"clang-module-search-paths", OptionValue::eTypeFileSpecList, false, 0,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003311 nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003312 "List of directories to be searched when locating modules for Clang."},
3313 {"auto-import-clang-modules", OptionValue::eTypeBoolean, false, true,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003314 nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003315 "Automatically load Clang modules referred to by the program."},
3316 {"auto-apply-fixits", OptionValue::eTypeBoolean, false, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003317 {}, "Automatically apply fix-it hints to expressions."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003318 {"notify-about-fixits", OptionValue::eTypeBoolean, false, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003319 {}, "Print the fixed expression text."},
Sean Callanan5deb06e2016-09-26 20:18:51 +00003320 {"save-jit-objects", OptionValue::eTypeBoolean, false, false, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003321 {}, "Save intermediate object files generated by the LLVM JIT"},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003322 {"max-children-count", OptionValue::eTypeSInt64, false, 256, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003323 {}, "Maximum number of children to expand in any level of depth."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003324 {"max-string-summary-length", OptionValue::eTypeSInt64, false, 1024,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003325 nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003326 "Maximum number of characters to show when using %s in summary strings."},
3327 {"max-memory-read-size", OptionValue::eTypeSInt64, false, 1024, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003328 {}, "Maximum number of bytes that 'memory read' will fetch before "
3329 "--force must be specified."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003330 {"breakpoints-use-platform-avoid-list", OptionValue::eTypeBoolean, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003331 true, nullptr, {}, "Consult the platform module avoid list when "
3332 "setting non-module specific breakpoints."},
3333 {"arg0", OptionValue::eTypeString, false, 0, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003334 "The first argument passed to the program in the argument array which can "
3335 "be different from the executable itself."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003336 {"run-args", OptionValue::eTypeArgs, false, 0, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003337 "A list containing all the arguments to be passed to the executable when "
3338 "it is run. Note that this does NOT include the argv[0] which is in "
3339 "target.arg0."},
3340 {"env-vars", OptionValue::eTypeDictionary, false, OptionValue::eTypeString,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003341 nullptr, {}, "A list of all the environment variables to be passed "
3342 "to the executable's environment, and their values."},
3343 {"inherit-env", OptionValue::eTypeBoolean, false, true, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003344 "Inherit the environment from the process that is running LLDB."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003345 {"input-path", OptionValue::eTypeFileSpec, false, 0, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003346 "The file/path to be used by the executable program for reading its "
3347 "standard input."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003348 {"output-path", OptionValue::eTypeFileSpec, false, 0, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003349 "The file/path to be used by the executable program for writing its "
3350 "standard output."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003351 {"error-path", OptionValue::eTypeFileSpec, false, 0, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003352 "The file/path to be used by the executable program for writing its "
3353 "standard error."},
3354 {"detach-on-error", OptionValue::eTypeBoolean, false, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003355 {}, "debugserver will detach (rather than killing) a process if it "
Kate Stoneb9c1b512016-09-06 20:57:50 +00003356 "loses connection with lldb."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003357 {"preload-symbols", OptionValue::eTypeBoolean, false, true, nullptr, {},
Jim Ingham7fca8c02017-04-28 00:51:06 +00003358 "Enable loading of symbol tables before they are needed."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003359 {"disable-aslr", OptionValue::eTypeBoolean, false, true, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003360 "Disable Address Space Layout Randomization (ASLR)"},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003361 {"disable-stdio", OptionValue::eTypeBoolean, false, false, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003362 "Disable stdin/stdout for process (e.g. for a GUI application)"},
3363 {"inline-breakpoint-strategy", OptionValue::eTypeEnum, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003364 eInlineBreakpointsAlways, nullptr,
3365 OptionEnumValues(g_inline_breakpoint_enums),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003366 "The strategy to use when settings breakpoints by file and line. "
3367 "Breakpoint locations can end up being inlined by the compiler, so that a "
3368 "compile unit 'a.c' might contain an inlined function from another source "
3369 "file. "
3370 "Usually this is limited to breakpoint locations from inlined functions "
3371 "from header or other include files, or more accurately "
3372 "non-implementation source files. "
3373 "Sometimes code might #include implementation files and cause inlined "
3374 "breakpoint locations in inlined implementation files. "
3375 "Always checking for inlined breakpoint locations can be expensive "
3376 "(memory and time), so if you have a project with many headers "
3377 "and find that setting breakpoints is slow, then you can change this "
3378 "setting to headers. "
3379 "This setting allows you to control exactly which strategy is used when "
3380 "setting "
3381 "file and line breakpoints."},
3382 // FIXME: This is the wrong way to do per-architecture settings, but we
3383 // don't have a general per architecture settings system in place yet.
3384 {"x86-disassembly-flavor", OptionValue::eTypeEnum, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003385 eX86DisFlavorDefault, nullptr,
3386 OptionEnumValues(g_x86_dis_flavor_value_types),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003387 "The default disassembly flavor to use for x86 or x86-64 targets."},
3388 {"use-hex-immediates", OptionValue::eTypeBoolean, false, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003389 {}, "Show immediates in disassembly as hexadecimal."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003390 {"hex-immediate-style", OptionValue::eTypeEnum, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003391 Disassembler::eHexStyleC, nullptr,
3392 OptionEnumValues(g_hex_immediate_style_values),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003393 "Which style to use for printing hexadecimal disassembly values."},
3394 {"use-fast-stepping", OptionValue::eTypeBoolean, false, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003395 {}, "Use a fast stepping algorithm based on running from branch to "
3396 "branch rather than instruction single-stepping."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003397 {"load-script-from-symbol-file", OptionValue::eTypeEnum, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003398 eLoadScriptFromSymFileWarn, nullptr,
3399 OptionEnumValues(g_load_script_from_sym_file_values),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003400 "Allow LLDB to load scripting resources embedded in symbol files when "
3401 "available."},
3402 {"load-cwd-lldbinit", OptionValue::eTypeEnum, false, eLoadCWDlldbinitWarn,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003403 nullptr, OptionEnumValues(g_load_current_working_dir_lldbinit_values),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003404 "Allow LLDB to .lldbinit files from the current directory automatically."},
3405 {"memory-module-load-level", OptionValue::eTypeEnum, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003406 eMemoryModuleLoadLevelComplete, nullptr,
3407 OptionEnumValues(g_memory_module_load_level_values),
Kate Stoneb9c1b512016-09-06 20:57:50 +00003408 "Loading modules from memory can be slow as reading the symbol tables and "
3409 "other data can take a long time depending on your connection to the "
3410 "debug target. "
3411 "This setting helps users control how much information gets loaded when "
3412 "loading modules from memory."
3413 "'complete' is the default value for this setting which will load all "
3414 "sections and symbols by reading them from memory (slowest, most "
3415 "accurate). "
3416 "'partial' will load sections and attempt to find function bounds without "
3417 "downloading the symbol table (faster, still accurate, missing symbol "
3418 "names). "
3419 "'minimal' is the fastest setting and will load section data with no "
3420 "symbols, but should rarely be used as stack frames in these memory "
3421 "regions will be inaccurate and not provide any context (fastest). "},
3422 {"display-expression-in-crashlogs", OptionValue::eTypeBoolean, false, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003423 nullptr, {}, "Expressions that crash will show up in crash logs if "
3424 "the host system supports executable specific crash log "
3425 "strings and this setting is set to true."},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003426 {"trap-handler-names", OptionValue::eTypeArray, true,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003427 OptionValue::eTypeString, nullptr, {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003428 "A list of trap handler function names, e.g. a common Unix user process "
3429 "one is _sigtramp."},
3430 {"display-runtime-support-values", OptionValue::eTypeBoolean, false, false,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003431 nullptr, {}, "If true, LLDB will show variables that are meant to "
3432 "support the operation of a language's runtime support."},
Kuba Mracek4c7f5d52018-12-20 23:38:19 +00003433 {"display-recognized-arguments", OptionValue::eTypeBoolean, false, false,
3434 nullptr, {}, "Show recognized arguments in variable listings by default."},
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003435 {"non-stop-mode", OptionValue::eTypeBoolean, false, 0, nullptr, {},
Jonas Devliegheree103ae92018-11-15 01:18:15 +00003436 "Disable lock-step debugging, instead control threads independently."},
3437 {"require-hardware-breakpoint", OptionValue::eTypeBoolean, false, 0,
3438 nullptr, {}, "Require all breakpoints to be hardware breakpoints."}};
3439// clang-format on
Kate Stoneb9c1b512016-09-06 20:57:50 +00003440
3441enum {
3442 ePropertyDefaultArch,
3443 ePropertyMoveToNearestCode,
3444 ePropertyLanguage,
3445 ePropertyExprPrefix,
3446 ePropertyPreferDynamic,
3447 ePropertyEnableSynthetic,
3448 ePropertySkipPrologue,
3449 ePropertySourceMap,
3450 ePropertyExecutableSearchPaths,
3451 ePropertyDebugFileSearchPaths,
3452 ePropertyClangModuleSearchPaths,
3453 ePropertyAutoImportClangModules,
3454 ePropertyAutoApplyFixIts,
3455 ePropertyNotifyAboutFixIts,
Sean Callanan5deb06e2016-09-26 20:18:51 +00003456 ePropertySaveObjects,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003457 ePropertyMaxChildrenCount,
3458 ePropertyMaxSummaryLength,
3459 ePropertyMaxMemReadSize,
3460 ePropertyBreakpointUseAvoidList,
3461 ePropertyArg0,
3462 ePropertyRunArgs,
3463 ePropertyEnvVars,
3464 ePropertyInheritEnv,
3465 ePropertyInputPath,
3466 ePropertyOutputPath,
3467 ePropertyErrorPath,
3468 ePropertyDetachOnError,
Jim Ingham7fca8c02017-04-28 00:51:06 +00003469 ePropertyPreloadSymbols,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003470 ePropertyDisableASLR,
3471 ePropertyDisableSTDIO,
3472 ePropertyInlineStrategy,
3473 ePropertyDisassemblyFlavor,
3474 ePropertyUseHexImmediates,
3475 ePropertyHexImmediateStyle,
3476 ePropertyUseFastStepping,
3477 ePropertyLoadScriptFromSymbolFile,
3478 ePropertyLoadCWDlldbinitFile,
3479 ePropertyMemoryModuleLoadLevel,
3480 ePropertyDisplayExpressionsInCrashlogs,
3481 ePropertyTrapHandlerNames,
3482 ePropertyDisplayRuntimeSupportValues,
Kuba Mracek4c7f5d52018-12-20 23:38:19 +00003483 ePropertyDisplayRecognizedArguments,
Kate Stoneb9c1b512016-09-06 20:57:50 +00003484 ePropertyNonStopModeEnabled,
Jonas Devliegheree103ae92018-11-15 01:18:15 +00003485 ePropertyRequireHardwareBreakpoints,
3486 ePropertyExperimental,
Jim Ingham0f063ba2013-03-02 00:26:47 +00003487};
3488
Kate Stoneb9c1b512016-09-06 20:57:50 +00003489class TargetOptionValueProperties : public OptionValueProperties {
Greg Clayton67cc0632012-08-22 17:17:09 +00003490public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003491 TargetOptionValueProperties(const ConstString &name)
3492 : OptionValueProperties(name), m_target(nullptr), m_got_host_env(false) {}
Caroline Ticedaccaa92010-09-20 20:44:43 +00003493
Kate Stoneb9c1b512016-09-06 20:57:50 +00003494 // This constructor is used when creating TargetOptionValueProperties when it
3495 // is part of a new lldb_private::Target instance. It will copy all current
3496 // global property values as needed
3497 TargetOptionValueProperties(Target *target,
3498 const TargetPropertiesSP &target_properties_sp)
3499 : OptionValueProperties(*target_properties_sp->GetValueProperties()),
3500 m_target(target), m_got_host_env(false) {}
Greg Clayton67cc0632012-08-22 17:17:09 +00003501
Kate Stoneb9c1b512016-09-06 20:57:50 +00003502 const Property *GetPropertyAtIndex(const ExecutionContext *exe_ctx,
3503 bool will_modify,
3504 uint32_t idx) const override {
3505 // When getting the value for a key from the target options, we will always
Adrian Prantl05097242018-04-30 16:49:04 +00003506 // try and grab the setting from the current target if there is one. Else
3507 // we just use the one from this instance.
Kate Stoneb9c1b512016-09-06 20:57:50 +00003508 if (idx == ePropertyEnvVars)
3509 GetHostEnvironmentIfNeeded();
3510
3511 if (exe_ctx) {
3512 Target *target = exe_ctx->GetTargetPtr();
3513 if (target) {
3514 TargetOptionValueProperties *target_properties =
3515 static_cast<TargetOptionValueProperties *>(
3516 target->GetValueProperties().get());
3517 if (this != target_properties)
3518 return target_properties->ProtectedGetPropertyAtIndex(idx);
3519 }
Greg Clayton67cc0632012-08-22 17:17:09 +00003520 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003521 return ProtectedGetPropertyAtIndex(idx);
3522 }
3523
3524 lldb::TargetSP GetTargetSP() { return m_target->shared_from_this(); }
3525
Greg Clayton67cc0632012-08-22 17:17:09 +00003526protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003527 void GetHostEnvironmentIfNeeded() const {
3528 if (!m_got_host_env) {
3529 if (m_target) {
3530 m_got_host_env = true;
3531 const uint32_t idx = ePropertyInheritEnv;
3532 if (GetPropertyAtIndexAsBoolean(
3533 nullptr, idx, g_properties[idx].default_uint_value != 0)) {
3534 PlatformSP platform_sp(m_target->GetPlatform());
3535 if (platform_sp) {
Pavel Labath62930e52018-01-10 11:57:31 +00003536 Environment env = platform_sp->GetEnvironment();
3537 OptionValueDictionary *env_dict =
3538 GetPropertyAtIndexAsOptionValueDictionary(nullptr,
3539 ePropertyEnvVars);
3540 if (env_dict) {
3541 const bool can_replace = false;
3542 for (const auto &KV : env) {
3543 // Don't allow existing keys to be replaced with ones we get
3544 // from the platform environment
3545 env_dict->SetValueForKey(
3546 ConstString(KV.first()),
3547 OptionValueSP(new OptionValueString(KV.second.c_str())),
3548 can_replace);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003549 }
Greg Clayton67cc0632012-08-22 17:17:09 +00003550 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003551 }
Greg Clayton67cc0632012-08-22 17:17:09 +00003552 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003553 }
Greg Clayton67cc0632012-08-22 17:17:09 +00003554 }
Kate Stoneb9c1b512016-09-06 20:57:50 +00003555 }
3556 Target *m_target;
3557 mutable bool m_got_host_env;
Greg Clayton67cc0632012-08-22 17:17:09 +00003558};
3559
Greg Claytonfbb76342013-11-20 21:07:01 +00003560//----------------------------------------------------------------------
3561// TargetProperties
3562//----------------------------------------------------------------------
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003563static constexpr PropertyDefinition g_experimental_properties[]{
Kate Stoneb9c1b512016-09-06 20:57:50 +00003564 {"inject-local-vars", OptionValue::eTypeBoolean, true, true, nullptr,
Tatyana Krasnukha8fe53c492018-09-26 18:50:19 +00003565 {},
Kate Stoneb9c1b512016-09-06 20:57:50 +00003566 "If true, inject local variables explicitly into the expression text. "
3567 "This will fix symbol resolution when there are name collisions between "
3568 "ivars and local variables. "
3569 "But it can make expressions run much more slowly."},
Sean Callanan68e44232017-09-28 20:20:25 +00003570 {"use-modern-type-lookup", OptionValue::eTypeBoolean, true, false, nullptr,
Tatyana Krasnukhae40db052018-09-27 07:11:58 +00003571 {}, "If true, use Clang's modern type lookup infrastructure."}};
Jim Inghambed67792016-07-07 18:25:48 +00003572
Sean Callanan68e44232017-09-28 20:20:25 +00003573enum { ePropertyInjectLocalVars = 0, ePropertyUseModernTypeLookup };
Jim Inghambed67792016-07-07 18:25:48 +00003574
Kate Stoneb9c1b512016-09-06 20:57:50 +00003575class TargetExperimentalOptionValueProperties : public OptionValueProperties {
Jim Inghambed67792016-07-07 18:25:48 +00003576public:
Kate Stoneb9c1b512016-09-06 20:57:50 +00003577 TargetExperimentalOptionValueProperties()
3578 : OptionValueProperties(
3579 ConstString(Properties::GetExperimentalSettingsName())) {}
Jim Inghambed67792016-07-07 18:25:48 +00003580};
3581
Kate Stoneb9c1b512016-09-06 20:57:50 +00003582TargetExperimentalProperties::TargetExperimentalProperties()
3583 : Properties(OptionValuePropertiesSP(
3584 new TargetExperimentalOptionValueProperties())) {
3585 m_collection_sp->Initialize(g_experimental_properties);
Jim Inghambed67792016-07-07 18:25:48 +00003586}
3587
3588//----------------------------------------------------------------------
3589// TargetProperties
3590//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +00003591TargetProperties::TargetProperties(Target *target)
3592 : Properties(), m_launch_info() {
3593 if (target) {
3594 m_collection_sp.reset(
3595 new TargetOptionValueProperties(target, Target::GetGlobalProperties()));
Ilia K8f37ca52015-02-13 14:31:06 +00003596
Adrian Prantl05097242018-04-30 16:49:04 +00003597 // Set callbacks to update launch_info whenever "settins set" updated any
3598 // of these properties
Kate Stoneb9c1b512016-09-06 20:57:50 +00003599 m_collection_sp->SetValueChangedCallback(
3600 ePropertyArg0, TargetProperties::Arg0ValueChangedCallback, this);
3601 m_collection_sp->SetValueChangedCallback(
3602 ePropertyRunArgs, TargetProperties::RunArgsValueChangedCallback, this);
3603 m_collection_sp->SetValueChangedCallback(
3604 ePropertyEnvVars, TargetProperties::EnvVarsValueChangedCallback, this);
3605 m_collection_sp->SetValueChangedCallback(
3606 ePropertyInputPath, TargetProperties::InputPathValueChangedCallback,
3607 this);
3608 m_collection_sp->SetValueChangedCallback(
3609 ePropertyOutputPath, TargetProperties::OutputPathValueChangedCallback,
3610 this);
3611 m_collection_sp->SetValueChangedCallback(
3612 ePropertyErrorPath, TargetProperties::ErrorPathValueChangedCallback,
3613 this);
3614 m_collection_sp->SetValueChangedCallback(
3615 ePropertyDetachOnError,
3616 TargetProperties::DetachOnErrorValueChangedCallback, this);
3617 m_collection_sp->SetValueChangedCallback(
3618 ePropertyDisableASLR, TargetProperties::DisableASLRValueChangedCallback,
3619 this);
3620 m_collection_sp->SetValueChangedCallback(
3621 ePropertyDisableSTDIO,
3622 TargetProperties::DisableSTDIOValueChangedCallback, this);
Jim Inghambed67792016-07-07 18:25:48 +00003623
Kate Stoneb9c1b512016-09-06 20:57:50 +00003624 m_experimental_properties_up.reset(new TargetExperimentalProperties());
3625 m_collection_sp->AppendProperty(
3626 ConstString(Properties::GetExperimentalSettingsName()),
3627 ConstString("Experimental settings - setting these won't produce "
3628 "errors if the setting is not present."),
3629 true, m_experimental_properties_up->GetValueProperties());
Jim Inghambed67792016-07-07 18:25:48 +00003630
Kate Stoneb9c1b512016-09-06 20:57:50 +00003631 // Update m_launch_info once it was created
3632 Arg0ValueChangedCallback(this, nullptr);
3633 RunArgsValueChangedCallback(this, nullptr);
3634 // EnvVarsValueChangedCallback(this, nullptr); // FIXME: cause segfault in
3635 // Target::GetPlatform()
3636 InputPathValueChangedCallback(this, nullptr);
3637 OutputPathValueChangedCallback(this, nullptr);
3638 ErrorPathValueChangedCallback(this, nullptr);
3639 DetachOnErrorValueChangedCallback(this, nullptr);
3640 DisableASLRValueChangedCallback(this, nullptr);
3641 DisableSTDIOValueChangedCallback(this, nullptr);
3642 } else {
3643 m_collection_sp.reset(
3644 new TargetOptionValueProperties(ConstString("target")));
3645 m_collection_sp->Initialize(g_properties);
3646 m_experimental_properties_up.reset(new TargetExperimentalProperties());
3647 m_collection_sp->AppendProperty(
3648 ConstString(Properties::GetExperimentalSettingsName()),
3649 ConstString("Experimental settings - setting these won't produce "
3650 "errors if the setting is not present."),
3651 true, m_experimental_properties_up->GetValueProperties());
3652 m_collection_sp->AppendProperty(
3653 ConstString("process"), ConstString("Settings specific to processes."),
3654 true, Process::GetGlobalProperties()->GetValueProperties());
3655 }
Caroline Ticedaccaa92010-09-20 20:44:43 +00003656}
3657
Eugene Zelenko8f30a652015-10-23 18:39:37 +00003658TargetProperties::~TargetProperties() = default;
3659
Kate Stoneb9c1b512016-09-06 20:57:50 +00003660bool TargetProperties::GetInjectLocalVariables(
3661 ExecutionContext *exe_ctx) const {
3662 const Property *exp_property = m_collection_sp->GetPropertyAtIndex(
3663 exe_ctx, false, ePropertyExperimental);
3664 OptionValueProperties *exp_values =
3665 exp_property->GetValue()->GetAsProperties();
3666 if (exp_values)
3667 return exp_values->GetPropertyAtIndexAsBoolean(
3668 exe_ctx, ePropertyInjectLocalVars, true);
3669 else
3670 return true;
Jim Inghambed67792016-07-07 18:25:48 +00003671}
3672
Kate Stoneb9c1b512016-09-06 20:57:50 +00003673void TargetProperties::SetInjectLocalVariables(ExecutionContext *exe_ctx,
3674 bool b) {
3675 const Property *exp_property =
3676 m_collection_sp->GetPropertyAtIndex(exe_ctx, true, ePropertyExperimental);
3677 OptionValueProperties *exp_values =
3678 exp_property->GetValue()->GetAsProperties();
3679 if (exp_values)
3680 exp_values->SetPropertyAtIndexAsBoolean(exe_ctx, ePropertyInjectLocalVars,
3681 true);
Jim Inghambed67792016-07-07 18:25:48 +00003682}
3683
Sean Callanan68e44232017-09-28 20:20:25 +00003684bool TargetProperties::GetUseModernTypeLookup() const {
3685 const Property *exp_property = m_collection_sp->GetPropertyAtIndex(
3686 nullptr, false, ePropertyExperimental);
3687 OptionValueProperties *exp_values =
3688 exp_property->GetValue()->GetAsProperties();
3689 if (exp_values)
3690 return exp_values->GetPropertyAtIndexAsBoolean(
3691 nullptr, ePropertyUseModernTypeLookup, true);
3692 else
3693 return true;
3694}
3695
Kate Stoneb9c1b512016-09-06 20:57:50 +00003696ArchSpec TargetProperties::GetDefaultArchitecture() const {
3697 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(
3698 nullptr, ePropertyDefaultArch);
3699 if (value)
3700 return value->GetCurrentValue();
3701 return ArchSpec();
Greg Clayton67cc0632012-08-22 17:17:09 +00003702}
3703
Kate Stoneb9c1b512016-09-06 20:57:50 +00003704void TargetProperties::SetDefaultArchitecture(const ArchSpec &arch) {
3705 OptionValueArch *value = m_collection_sp->GetPropertyAtIndexAsOptionValueArch(
3706 nullptr, ePropertyDefaultArch);
3707 if (value)
3708 return value->SetCurrentValue(arch, true);
Greg Clayton67cc0632012-08-22 17:17:09 +00003709}
3710
Kate Stoneb9c1b512016-09-06 20:57:50 +00003711bool TargetProperties::GetMoveToNearestCode() const {
3712 const uint32_t idx = ePropertyMoveToNearestCode;
3713 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3714 nullptr, idx, g_properties[idx].default_uint_value != 0);
Ilia K055ad9b2015-05-18 13:41:01 +00003715}
3716
Kate Stoneb9c1b512016-09-06 20:57:50 +00003717lldb::DynamicValueType TargetProperties::GetPreferDynamicValue() const {
3718 const uint32_t idx = ePropertyPreferDynamic;
3719 return (lldb::DynamicValueType)
3720 m_collection_sp->GetPropertyAtIndexAsEnumeration(
3721 nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003722}
3723
Kate Stoneb9c1b512016-09-06 20:57:50 +00003724bool TargetProperties::SetPreferDynamicValue(lldb::DynamicValueType d) {
3725 const uint32_t idx = ePropertyPreferDynamic;
3726 return m_collection_sp->SetPropertyAtIndexAsEnumeration(nullptr, idx, d);
Greg Clayton15484402015-05-15 18:40:24 +00003727}
3728
Jim Ingham7fca8c02017-04-28 00:51:06 +00003729bool TargetProperties::GetPreloadSymbols() const {
3730 const uint32_t idx = ePropertyPreloadSymbols;
3731 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3732 nullptr, idx, g_properties[idx].default_uint_value != 0);
3733}
3734
3735void TargetProperties::SetPreloadSymbols(bool b) {
3736 const uint32_t idx = ePropertyPreloadSymbols;
3737 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
3738}
3739
Kate Stoneb9c1b512016-09-06 20:57:50 +00003740bool TargetProperties::GetDisableASLR() const {
3741 const uint32_t idx = ePropertyDisableASLR;
3742 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3743 nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003744}
3745
Kate Stoneb9c1b512016-09-06 20:57:50 +00003746void TargetProperties::SetDisableASLR(bool b) {
3747 const uint32_t idx = ePropertyDisableASLR;
3748 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003749}
3750
Kate Stoneb9c1b512016-09-06 20:57:50 +00003751bool TargetProperties::GetDetachOnError() const {
3752 const uint32_t idx = ePropertyDetachOnError;
3753 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3754 nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham106d0282014-06-25 02:32:56 +00003755}
3756
Kate Stoneb9c1b512016-09-06 20:57:50 +00003757void TargetProperties::SetDetachOnError(bool b) {
3758 const uint32_t idx = ePropertyDetachOnError;
3759 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Jim Ingham106d0282014-06-25 02:32:56 +00003760}
3761
Kate Stoneb9c1b512016-09-06 20:57:50 +00003762bool TargetProperties::GetDisableSTDIO() const {
3763 const uint32_t idx = ePropertyDisableSTDIO;
3764 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3765 nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003766}
3767
Kate Stoneb9c1b512016-09-06 20:57:50 +00003768void TargetProperties::SetDisableSTDIO(bool b) {
3769 const uint32_t idx = ePropertyDisableSTDIO;
3770 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Greg Clayton67cc0632012-08-22 17:17:09 +00003771}
3772
Kate Stoneb9c1b512016-09-06 20:57:50 +00003773const char *TargetProperties::GetDisassemblyFlavor() const {
3774 const uint32_t idx = ePropertyDisassemblyFlavor;
3775 const char *return_value;
3776
3777 x86DisassemblyFlavor flavor_value =
3778 (x86DisassemblyFlavor)m_collection_sp->GetPropertyAtIndexAsEnumeration(
3779 nullptr, idx, g_properties[idx].default_uint_value);
3780 return_value = g_x86_dis_flavor_value_types[flavor_value].string_value;
3781 return return_value;
Jim Ingham0f063ba2013-03-02 00:26:47 +00003782}
3783
Kate Stoneb9c1b512016-09-06 20:57:50 +00003784InlineStrategy TargetProperties::GetInlineStrategy() const {
3785 const uint32_t idx = ePropertyInlineStrategy;
3786 return (InlineStrategy)m_collection_sp->GetPropertyAtIndexAsEnumeration(
3787 nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton1f746072012-08-29 21:13:06 +00003788}
3789
Zachary Turner31d97a52016-11-17 18:08:12 +00003790llvm::StringRef TargetProperties::GetArg0() const {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003791 const uint32_t idx = ePropertyArg0;
Zachary Turner31d97a52016-11-17 18:08:12 +00003792 return m_collection_sp->GetPropertyAtIndexAsString(nullptr, idx, llvm::StringRef());
Greg Clayton45392552012-10-17 22:57:12 +00003793}
3794
Zachary Turner31d97a52016-11-17 18:08:12 +00003795void TargetProperties::SetArg0(llvm::StringRef arg) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003796 const uint32_t idx = ePropertyArg0;
Zachary Turner514d8cd2016-09-23 18:06:53 +00003797 m_collection_sp->SetPropertyAtIndexAsString(
Zachary Turner31d97a52016-11-17 18:08:12 +00003798 nullptr, idx, arg);
Kate Stoneb9c1b512016-09-06 20:57:50 +00003799 m_launch_info.SetArg0(arg);
Greg Clayton45392552012-10-17 22:57:12 +00003800}
3801
Kate Stoneb9c1b512016-09-06 20:57:50 +00003802bool TargetProperties::GetRunArguments(Args &args) const {
3803 const uint32_t idx = ePropertyRunArgs;
3804 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Greg Clayton67cc0632012-08-22 17:17:09 +00003805}
3806
Kate Stoneb9c1b512016-09-06 20:57:50 +00003807void TargetProperties::SetRunArguments(const Args &args) {
3808 const uint32_t idx = ePropertyRunArgs;
3809 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
3810 m_launch_info.GetArguments() = args;
Greg Clayton67cc0632012-08-22 17:17:09 +00003811}
3812
Pavel Labath62930e52018-01-10 11:57:31 +00003813Environment TargetProperties::GetEnvironment() const {
3814 // TODO: Get rid of the Args intermediate step
3815 Args env;
Kate Stoneb9c1b512016-09-06 20:57:50 +00003816 const uint32_t idx = ePropertyEnvVars;
Pavel Labath62930e52018-01-10 11:57:31 +00003817 m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, env);
3818 return Environment(env);
Greg Clayton67cc0632012-08-22 17:17:09 +00003819}
3820
Pavel Labath62930e52018-01-10 11:57:31 +00003821void TargetProperties::SetEnvironment(Environment env) {
3822 // TODO: Get rid of the Args intermediate step
Kate Stoneb9c1b512016-09-06 20:57:50 +00003823 const uint32_t idx = ePropertyEnvVars;
Pavel Labath62930e52018-01-10 11:57:31 +00003824 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, Args(env));
3825 m_launch_info.GetEnvironment() = std::move(env);
Ilia K8f37ca52015-02-13 14:31:06 +00003826}
3827
Kate Stoneb9c1b512016-09-06 20:57:50 +00003828bool TargetProperties::GetSkipPrologue() const {
3829 const uint32_t idx = ePropertySkipPrologue;
3830 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3831 nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003832}
3833
Kate Stoneb9c1b512016-09-06 20:57:50 +00003834PathMappingList &TargetProperties::GetSourcePathMap() const {
3835 const uint32_t idx = ePropertySourceMap;
3836 OptionValuePathMappings *option_value =
3837 m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(nullptr,
3838 false, idx);
3839 assert(option_value);
3840 return option_value->GetCurrentValue();
Greg Clayton67cc0632012-08-22 17:17:09 +00003841}
3842
Kate Stoneb9c1b512016-09-06 20:57:50 +00003843FileSpecList &TargetProperties::GetExecutableSearchPaths() {
3844 const uint32_t idx = ePropertyExecutableSearchPaths;
3845 OptionValueFileSpecList *option_value =
3846 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
3847 false, idx);
3848 assert(option_value);
3849 return option_value->GetCurrentValue();
Greg Clayton67cc0632012-08-22 17:17:09 +00003850}
3851
Kate Stoneb9c1b512016-09-06 20:57:50 +00003852FileSpecList &TargetProperties::GetDebugFileSearchPaths() {
3853 const uint32_t idx = ePropertyDebugFileSearchPaths;
3854 OptionValueFileSpecList *option_value =
3855 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
3856 false, idx);
3857 assert(option_value);
3858 return option_value->GetCurrentValue();
Michael Sartaina7499c92013-07-01 19:45:50 +00003859}
3860
Kate Stoneb9c1b512016-09-06 20:57:50 +00003861FileSpecList &TargetProperties::GetClangModuleSearchPaths() {
3862 const uint32_t idx = ePropertyClangModuleSearchPaths;
3863 OptionValueFileSpecList *option_value =
3864 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(nullptr,
3865 false, idx);
3866 assert(option_value);
3867 return option_value->GetCurrentValue();
Sean Callanan85054342015-04-03 15:39:47 +00003868}
3869
Kate Stoneb9c1b512016-09-06 20:57:50 +00003870bool TargetProperties::GetEnableAutoImportClangModules() const {
3871 const uint32_t idx = ePropertyAutoImportClangModules;
3872 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3873 nullptr, idx, g_properties[idx].default_uint_value != 0);
Sean Callananf0c5aeb2015-04-20 16:31:29 +00003874}
3875
Kate Stoneb9c1b512016-09-06 20:57:50 +00003876bool TargetProperties::GetEnableAutoApplyFixIts() const {
3877 const uint32_t idx = ePropertyAutoApplyFixIts;
3878 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3879 nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Inghama1e541b2016-03-25 01:57:14 +00003880}
3881
Kate Stoneb9c1b512016-09-06 20:57:50 +00003882bool TargetProperties::GetEnableNotifyAboutFixIts() const {
3883 const uint32_t idx = ePropertyNotifyAboutFixIts;
3884 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3885 nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Inghame5ee6f02016-03-29 22:00:08 +00003886}
3887
Sean Callanan5deb06e2016-09-26 20:18:51 +00003888bool TargetProperties::GetEnableSaveObjects() const {
3889 const uint32_t idx = ePropertySaveObjects;
3890 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3891 nullptr, idx, g_properties[idx].default_uint_value != 0);
3892}
3893
Kate Stoneb9c1b512016-09-06 20:57:50 +00003894bool TargetProperties::GetEnableSyntheticValue() const {
3895 const uint32_t idx = ePropertyEnableSynthetic;
3896 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3897 nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003898}
3899
Kate Stoneb9c1b512016-09-06 20:57:50 +00003900uint32_t TargetProperties::GetMaximumNumberOfChildrenToDisplay() const {
3901 const uint32_t idx = ePropertyMaxChildrenCount;
3902 return m_collection_sp->GetPropertyAtIndexAsSInt64(
3903 nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003904}
3905
Kate Stoneb9c1b512016-09-06 20:57:50 +00003906uint32_t TargetProperties::GetMaximumSizeOfStringSummary() const {
3907 const uint32_t idx = ePropertyMaxSummaryLength;
3908 return m_collection_sp->GetPropertyAtIndexAsSInt64(
3909 nullptr, idx, g_properties[idx].default_uint_value);
Greg Clayton67cc0632012-08-22 17:17:09 +00003910}
3911
Kate Stoneb9c1b512016-09-06 20:57:50 +00003912uint32_t TargetProperties::GetMaximumMemReadSize() const {
3913 const uint32_t idx = ePropertyMaxMemReadSize;
3914 return m_collection_sp->GetPropertyAtIndexAsSInt64(
3915 nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granatad325bf92013-06-04 22:54:16 +00003916}
3917
Kate Stoneb9c1b512016-09-06 20:57:50 +00003918FileSpec TargetProperties::GetStandardInputPath() const {
3919 const uint32_t idx = ePropertyInputPath;
3920 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003921}
3922
Zachary Turner27a5c2b2016-09-23 22:11:51 +00003923void TargetProperties::SetStandardInputPath(llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003924 const uint32_t idx = ePropertyInputPath;
Zachary Turner27a5c2b2016-09-23 22:11:51 +00003925 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
Greg Clayton67cc0632012-08-22 17:17:09 +00003926}
3927
Kate Stoneb9c1b512016-09-06 20:57:50 +00003928FileSpec TargetProperties::GetStandardOutputPath() const {
3929 const uint32_t idx = ePropertyOutputPath;
3930 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003931}
3932
Zachary Turner27a5c2b2016-09-23 22:11:51 +00003933void TargetProperties::SetStandardOutputPath(llvm::StringRef path) {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003934 const uint32_t idx = ePropertyOutputPath;
Zachary Turner27a5c2b2016-09-23 22:11:51 +00003935 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
Greg Clayton67cc0632012-08-22 17:17:09 +00003936}
3937
Kate Stoneb9c1b512016-09-06 20:57:50 +00003938FileSpec TargetProperties::GetStandardErrorPath() const {
3939 const uint32_t idx = ePropertyErrorPath;
3940 return m_collection_sp->GetPropertyAtIndexAsFileSpec(nullptr, idx);
Greg Clayton67cc0632012-08-22 17:17:09 +00003941}
3942
Zachary Turner27a5c2b2016-09-23 22:11:51 +00003943void TargetProperties::SetStandardErrorPath(llvm::StringRef path) {
3944 const uint32_t idx = ePropertyErrorPath;
3945 m_collection_sp->SetPropertyAtIndexAsString(nullptr, idx, path);
3946}
3947
Kate Stoneb9c1b512016-09-06 20:57:50 +00003948LanguageType TargetProperties::GetLanguage() const {
3949 OptionValueLanguage *value =
3950 m_collection_sp->GetPropertyAtIndexAsOptionValueLanguage(
3951 nullptr, ePropertyLanguage);
3952 if (value)
3953 return value->GetCurrentValue();
3954 return LanguageType();
Dawn Perchik23b1dec2015-07-21 22:05:07 +00003955}
3956
Pavel Labath50251fc2017-12-21 10:54:30 +00003957llvm::StringRef TargetProperties::GetExpressionPrefixContents() {
Kate Stoneb9c1b512016-09-06 20:57:50 +00003958 const uint32_t idx = ePropertyExprPrefix;
3959 OptionValueFileSpec *file =
3960 m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(nullptr, false,
3961 idx);
3962 if (file) {
Pavel Labath50251fc2017-12-21 10:54:30 +00003963 DataBufferSP data_sp(file->GetFileContents());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003964 if (data_sp)
Pavel Labath50251fc2017-12-21 10:54:30 +00003965 return llvm::StringRef(
3966 reinterpret_cast<const char *>(data_sp->GetBytes()),
3967 data_sp->GetByteSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +00003968 }
Pavel Labath50251fc2017-12-21 10:54:30 +00003969 return "";
Greg Clayton6920b522012-08-22 18:39:03 +00003970}
3971
Kate Stoneb9c1b512016-09-06 20:57:50 +00003972bool TargetProperties::GetBreakpointsConsultPlatformAvoidList() {
3973 const uint32_t idx = ePropertyBreakpointUseAvoidList;
3974 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3975 nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton67cc0632012-08-22 17:17:09 +00003976}
3977
Kate Stoneb9c1b512016-09-06 20:57:50 +00003978bool TargetProperties::GetUseHexImmediates() const {
3979 const uint32_t idx = ePropertyUseHexImmediates;
3980 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3981 nullptr, idx, g_properties[idx].default_uint_value != 0);
Daniel Malead79ae052013-08-07 21:54:09 +00003982}
3983
Kate Stoneb9c1b512016-09-06 20:57:50 +00003984bool TargetProperties::GetUseFastStepping() const {
3985 const uint32_t idx = ePropertyUseFastStepping;
3986 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3987 nullptr, idx, g_properties[idx].default_uint_value != 0);
Jim Ingham17d023f2013-03-13 17:58:04 +00003988}
3989
Kate Stoneb9c1b512016-09-06 20:57:50 +00003990bool TargetProperties::GetDisplayExpressionsInCrashlogs() const {
3991 const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs;
3992 return m_collection_sp->GetPropertyAtIndexAsBoolean(
3993 nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Claytonfb6621e2013-12-06 21:59:52 +00003994}
3995
Kate Stoneb9c1b512016-09-06 20:57:50 +00003996LoadScriptFromSymFile TargetProperties::GetLoadScriptFromSymbolFile() const {
3997 const uint32_t idx = ePropertyLoadScriptFromSymbolFile;
3998 return (LoadScriptFromSymFile)
3999 m_collection_sp->GetPropertyAtIndexAsEnumeration(
4000 nullptr, idx, g_properties[idx].default_uint_value);
Enrico Granata2ea43cd2013-05-13 17:03:52 +00004001}
4002
Kate Stoneb9c1b512016-09-06 20:57:50 +00004003LoadCWDlldbinitFile TargetProperties::GetLoadCWDlldbinitFile() const {
4004 const uint32_t idx = ePropertyLoadCWDlldbinitFile;
4005 return (LoadCWDlldbinitFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(
4006 nullptr, idx, g_properties[idx].default_uint_value);
Jason Molenda878ae012016-02-19 00:05:17 +00004007}
4008
Kate Stoneb9c1b512016-09-06 20:57:50 +00004009Disassembler::HexImmediateStyle TargetProperties::GetHexImmediateStyle() const {
4010 const uint32_t idx = ePropertyHexImmediateStyle;
4011 return (Disassembler::HexImmediateStyle)
4012 m_collection_sp->GetPropertyAtIndexAsEnumeration(
4013 nullptr, idx, g_properties[idx].default_uint_value);
Daniel Malead79ae052013-08-07 21:54:09 +00004014}
4015
Kate Stoneb9c1b512016-09-06 20:57:50 +00004016MemoryModuleLoadLevel TargetProperties::GetMemoryModuleLoadLevel() const {
4017 const uint32_t idx = ePropertyMemoryModuleLoadLevel;
4018 return (MemoryModuleLoadLevel)
4019 m_collection_sp->GetPropertyAtIndexAsEnumeration(
4020 nullptr, idx, g_properties[idx].default_uint_value);
Greg Claytonfd814c52013-08-13 01:42:25 +00004021}
4022
Kate Stoneb9c1b512016-09-06 20:57:50 +00004023bool TargetProperties::GetUserSpecifiedTrapHandlerNames(Args &args) const {
4024 const uint32_t idx = ePropertyTrapHandlerNames;
4025 return m_collection_sp->GetPropertyAtIndexAsArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00004026}
Greg Claytonfd814c52013-08-13 01:42:25 +00004027
Kate Stoneb9c1b512016-09-06 20:57:50 +00004028void TargetProperties::SetUserSpecifiedTrapHandlerNames(const Args &args) {
4029 const uint32_t idx = ePropertyTrapHandlerNames;
4030 m_collection_sp->SetPropertyAtIndexFromArgs(nullptr, idx, args);
Jason Molendaa4bea722014-02-14 05:06:49 +00004031}
Greg Clayton67cc0632012-08-22 17:17:09 +00004032
Kate Stoneb9c1b512016-09-06 20:57:50 +00004033bool TargetProperties::GetDisplayRuntimeSupportValues() const {
4034 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
4035 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Enrico Granata560558e2015-02-11 02:35:39 +00004036}
4037
Kate Stoneb9c1b512016-09-06 20:57:50 +00004038void TargetProperties::SetDisplayRuntimeSupportValues(bool b) {
4039 const uint32_t idx = ePropertyDisplayRuntimeSupportValues;
4040 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Enrico Granata560558e2015-02-11 02:35:39 +00004041}
4042
Kuba Mracek4c7f5d52018-12-20 23:38:19 +00004043bool TargetProperties::GetDisplayRecognizedArguments() const {
4044 const uint32_t idx = ePropertyDisplayRecognizedArguments;
4045 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
4046}
4047
4048void TargetProperties::SetDisplayRecognizedArguments(bool b) {
4049 const uint32_t idx = ePropertyDisplayRecognizedArguments;
4050 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
4051}
4052
Kate Stoneb9c1b512016-09-06 20:57:50 +00004053bool TargetProperties::GetNonStopModeEnabled() const {
4054 const uint32_t idx = ePropertyNonStopModeEnabled;
4055 return m_collection_sp->GetPropertyAtIndexAsBoolean(nullptr, idx, false);
Ewan Crawford78baa192015-05-13 09:18:18 +00004056}
4057
Kate Stoneb9c1b512016-09-06 20:57:50 +00004058void TargetProperties::SetNonStopModeEnabled(bool b) {
4059 const uint32_t idx = ePropertyNonStopModeEnabled;
4060 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
Ewan Crawfordaa7eda72015-06-24 15:14:26 +00004061}
4062
Kate Stoneb9c1b512016-09-06 20:57:50 +00004063const ProcessLaunchInfo &TargetProperties::GetProcessLaunchInfo() {
4064 m_launch_info.SetArg0(GetArg0()); // FIXME: Arg0 callback doesn't work
4065 return m_launch_info;
Ilia K8f37ca52015-02-13 14:31:06 +00004066}
4067
Kate Stoneb9c1b512016-09-06 20:57:50 +00004068void TargetProperties::SetProcessLaunchInfo(
4069 const ProcessLaunchInfo &launch_info) {
4070 m_launch_info = launch_info;
4071 SetArg0(launch_info.GetArg0());
4072 SetRunArguments(launch_info.GetArguments());
Pavel Labath62930e52018-01-10 11:57:31 +00004073 SetEnvironment(launch_info.GetEnvironment());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004074 const FileAction *input_file_action =
4075 launch_info.GetFileActionForFD(STDIN_FILENO);
4076 if (input_file_action) {
Zachary Turner27a5c2b2016-09-23 22:11:51 +00004077 SetStandardInputPath(input_file_action->GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004078 }
4079 const FileAction *output_file_action =
4080 launch_info.GetFileActionForFD(STDOUT_FILENO);
4081 if (output_file_action) {
Zachary Turner27a5c2b2016-09-23 22:11:51 +00004082 SetStandardOutputPath(output_file_action->GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004083 }
4084 const FileAction *error_file_action =
4085 launch_info.GetFileActionForFD(STDERR_FILENO);
4086 if (error_file_action) {
Zachary Turner27a5c2b2016-09-23 22:11:51 +00004087 SetStandardErrorPath(error_file_action->GetPath());
Kate Stoneb9c1b512016-09-06 20:57:50 +00004088 }
4089 SetDetachOnError(launch_info.GetFlags().Test(lldb::eLaunchFlagDetachOnError));
4090 SetDisableASLR(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableASLR));
4091 SetDisableSTDIO(launch_info.GetFlags().Test(lldb::eLaunchFlagDisableSTDIO));
Ilia K8f37ca52015-02-13 14:31:06 +00004092}
4093
Jonas Devliegheree103ae92018-11-15 01:18:15 +00004094bool TargetProperties::GetRequireHardwareBreakpoints() const {
4095 const uint32_t idx = ePropertyRequireHardwareBreakpoints;
4096 return m_collection_sp->GetPropertyAtIndexAsBoolean(
4097 nullptr, idx, g_properties[idx].default_uint_value != 0);
4098}
4099
4100void TargetProperties::SetRequireHardwareBreakpoints(bool b) {
4101 const uint32_t idx = ePropertyRequireHardwareBreakpoints;
4102 m_collection_sp->SetPropertyAtIndexAsBoolean(nullptr, idx, b);
4103}
4104
Kate Stoneb9c1b512016-09-06 20:57:50 +00004105void TargetProperties::Arg0ValueChangedCallback(void *target_property_ptr,
4106 OptionValue *) {
4107 TargetProperties *this_ =
4108 reinterpret_cast<TargetProperties *>(target_property_ptr);
4109 this_->m_launch_info.SetArg0(this_->GetArg0());
Ilia K8f37ca52015-02-13 14:31:06 +00004110}
4111
Kate Stoneb9c1b512016-09-06 20:57:50 +00004112void TargetProperties::RunArgsValueChangedCallback(void *target_property_ptr,
4113 OptionValue *) {
4114 TargetProperties *this_ =
4115 reinterpret_cast<TargetProperties *>(target_property_ptr);
4116 Args args;
4117 if (this_->GetRunArguments(args))
4118 this_->m_launch_info.GetArguments() = args;
Ilia K8f37ca52015-02-13 14:31:06 +00004119}
4120
Kate Stoneb9c1b512016-09-06 20:57:50 +00004121void TargetProperties::EnvVarsValueChangedCallback(void *target_property_ptr,
4122 OptionValue *) {
4123 TargetProperties *this_ =
4124 reinterpret_cast<TargetProperties *>(target_property_ptr);
Pavel Labath62930e52018-01-10 11:57:31 +00004125 this_->m_launch_info.GetEnvironment() = this_->GetEnvironment();
Ilia K8f37ca52015-02-13 14:31:06 +00004126}
4127
Kate Stoneb9c1b512016-09-06 20:57:50 +00004128void TargetProperties::InputPathValueChangedCallback(void *target_property_ptr,
4129 OptionValue *) {
4130 TargetProperties *this_ =
4131 reinterpret_cast<TargetProperties *>(target_property_ptr);
4132 this_->m_launch_info.AppendOpenFileAction(
4133 STDIN_FILENO, this_->GetStandardInputPath(), true, false);
Ilia K8f37ca52015-02-13 14:31:06 +00004134}
4135
Kate Stoneb9c1b512016-09-06 20:57:50 +00004136void TargetProperties::OutputPathValueChangedCallback(void *target_property_ptr,
4137 OptionValue *) {
4138 TargetProperties *this_ =
4139 reinterpret_cast<TargetProperties *>(target_property_ptr);
4140 this_->m_launch_info.AppendOpenFileAction(
4141 STDOUT_FILENO, this_->GetStandardOutputPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004142}
4143
Kate Stoneb9c1b512016-09-06 20:57:50 +00004144void TargetProperties::ErrorPathValueChangedCallback(void *target_property_ptr,
4145 OptionValue *) {
4146 TargetProperties *this_ =
4147 reinterpret_cast<TargetProperties *>(target_property_ptr);
4148 this_->m_launch_info.AppendOpenFileAction(
4149 STDERR_FILENO, this_->GetStandardErrorPath(), false, true);
Ilia K8f37ca52015-02-13 14:31:06 +00004150}
4151
Kate Stoneb9c1b512016-09-06 20:57:50 +00004152void TargetProperties::DetachOnErrorValueChangedCallback(
4153 void *target_property_ptr, OptionValue *) {
4154 TargetProperties *this_ =
4155 reinterpret_cast<TargetProperties *>(target_property_ptr);
4156 if (this_->GetDetachOnError())
4157 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError);
4158 else
4159 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDetachOnError);
Ilia K8f37ca52015-02-13 14:31:06 +00004160}
4161
Kate Stoneb9c1b512016-09-06 20:57:50 +00004162void TargetProperties::DisableASLRValueChangedCallback(
4163 void *target_property_ptr, OptionValue *) {
4164 TargetProperties *this_ =
4165 reinterpret_cast<TargetProperties *>(target_property_ptr);
4166 if (this_->GetDisableASLR())
4167 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR);
4168 else
4169 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableASLR);
Ilia K8f37ca52015-02-13 14:31:06 +00004170}
4171
Kate Stoneb9c1b512016-09-06 20:57:50 +00004172void TargetProperties::DisableSTDIOValueChangedCallback(
4173 void *target_property_ptr, OptionValue *) {
4174 TargetProperties *this_ =
4175 reinterpret_cast<TargetProperties *>(target_property_ptr);
4176 if (this_->GetDisableSTDIO())
4177 this_->m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO);
4178 else
4179 this_->m_launch_info.GetFlags().Clear(lldb::eLaunchFlagDisableSTDIO);
Ilia K8f37ca52015-02-13 14:31:06 +00004180}
Ilia Keb2c19a2015-03-10 21:59:55 +00004181
4182//----------------------------------------------------------------------
4183// Target::TargetEventData
4184//----------------------------------------------------------------------
4185
Kate Stoneb9c1b512016-09-06 20:57:50 +00004186Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp)
4187 : EventData(), m_target_sp(target_sp), m_module_list() {}
Ilia Keb2c19a2015-03-10 21:59:55 +00004188
Kate Stoneb9c1b512016-09-06 20:57:50 +00004189Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp,
4190 const ModuleList &module_list)
4191 : EventData(), m_target_sp(target_sp), m_module_list(module_list) {}
Ilia Keb2c19a2015-03-10 21:59:55 +00004192
Eugene Zelenko8f30a652015-10-23 18:39:37 +00004193Target::TargetEventData::~TargetEventData() = default;
Ilia Keb2c19a2015-03-10 21:59:55 +00004194
Kate Stoneb9c1b512016-09-06 20:57:50 +00004195const ConstString &Target::TargetEventData::GetFlavorString() {
4196 static ConstString g_flavor("Target::TargetEventData");
4197 return g_flavor;
Ilia Keb2c19a2015-03-10 21:59:55 +00004198}
4199
Kate Stoneb9c1b512016-09-06 20:57:50 +00004200void Target::TargetEventData::Dump(Stream *s) const {
4201 for (size_t i = 0; i < m_module_list.GetSize(); ++i) {
4202 if (i != 0)
4203 *s << ", ";
4204 m_module_list.GetModuleAtIndex(i)->GetDescription(
4205 s, lldb::eDescriptionLevelBrief);
4206 }
Ilia Keb2c19a2015-03-10 21:59:55 +00004207}
4208
4209const Target::TargetEventData *
Kate Stoneb9c1b512016-09-06 20:57:50 +00004210Target::TargetEventData::GetEventDataFromEvent(const Event *event_ptr) {
4211 if (event_ptr) {
4212 const EventData *event_data = event_ptr->GetData();
4213 if (event_data &&
4214 event_data->GetFlavor() == TargetEventData::GetFlavorString())
4215 return static_cast<const TargetEventData *>(event_ptr->GetData());
4216 }
4217 return nullptr;
Ilia Keb2c19a2015-03-10 21:59:55 +00004218}
4219
Kate Stoneb9c1b512016-09-06 20:57:50 +00004220TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) {
4221 TargetSP target_sp;
4222 const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
4223 if (event_data)
4224 target_sp = event_data->m_target_sp;
4225 return target_sp;
Ilia Keb2c19a2015-03-10 21:59:55 +00004226}
4227
4228ModuleList
Kate Stoneb9c1b512016-09-06 20:57:50 +00004229Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) {
4230 ModuleList module_list;
4231 const TargetEventData *event_data = GetEventDataFromEvent(event_ptr);
4232 if (event_data)
4233 module_list = event_data->m_module_list;
4234 return module_list;
Ilia Keb2c19a2015-03-10 21:59:55 +00004235}