| 1 | //===-- Target.cpp --------------------------------------------------------===// |
| 2 | // |
| 3 | // 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Target/Target.h" |
| 10 | #include "lldb/Breakpoint/BreakpointIDList.h" |
| 11 | #include "lldb/Breakpoint/BreakpointPrecondition.h" |
| 12 | #include "lldb/Breakpoint/BreakpointResolver.h" |
| 13 | #include "lldb/Breakpoint/BreakpointResolverAddress.h" |
| 14 | #include "lldb/Breakpoint/BreakpointResolverFileLine.h" |
| 15 | #include "lldb/Breakpoint/BreakpointResolverFileRegex.h" |
| 16 | #include "lldb/Breakpoint/BreakpointResolverName.h" |
| 17 | #include "lldb/Breakpoint/BreakpointResolverScripted.h" |
| 18 | #include "lldb/Breakpoint/Watchpoint.h" |
| 19 | #include "lldb/Core/Debugger.h" |
| 20 | #include "lldb/Core/Module.h" |
| 21 | #include "lldb/Core/ModuleSpec.h" |
| 22 | #include "lldb/Core/PluginManager.h" |
| 23 | #include "lldb/Core/SearchFilter.h" |
| 24 | #include "lldb/Core/Section.h" |
| 25 | #include "lldb/Core/SourceManager.h" |
| 26 | #include "lldb/Core/StructuredDataImpl.h" |
| 27 | #include "lldb/Core/Telemetry.h" |
| 28 | #include "lldb/DataFormatters/FormatterSection.h" |
| 29 | #include "lldb/Expression/DiagnosticManager.h" |
| 30 | #include "lldb/Expression/ExpressionVariable.h" |
| 31 | #include "lldb/Expression/REPL.h" |
| 32 | #include "lldb/Expression/UserExpression.h" |
| 33 | #include "lldb/Expression/UtilityFunction.h" |
| 34 | #include "lldb/Host/Host.h" |
| 35 | #include "lldb/Host/PosixApi.h" |
| 36 | #include "lldb/Host/StreamFile.h" |
| 37 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 38 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 39 | #include "lldb/Interpreter/Interfaces/ScriptedStopHookInterface.h" |
| 40 | #include "lldb/Interpreter/OptionGroupWatchpoint.h" |
| 41 | #include "lldb/Interpreter/OptionValues.h" |
| 42 | #include "lldb/Interpreter/Property.h" |
| 43 | #include "lldb/Symbol/Function.h" |
| 44 | #include "lldb/Symbol/ObjectFile.h" |
| 45 | #include "lldb/Symbol/Symbol.h" |
| 46 | #include "lldb/Target/ABI.h" |
| 47 | #include "lldb/Target/ExecutionContext.h" |
| 48 | #include "lldb/Target/Language.h" |
| 49 | #include "lldb/Target/LanguageRuntime.h" |
| 50 | #include "lldb/Target/Process.h" |
| 51 | #include "lldb/Target/RegisterTypeBuilder.h" |
| 52 | #include "lldb/Target/SectionLoadList.h" |
| 53 | #include "lldb/Target/StackFrame.h" |
| 54 | #include "lldb/Target/StackFrameRecognizer.h" |
| 55 | #include "lldb/Target/SystemRuntime.h" |
| 56 | #include "lldb/Target/Thread.h" |
| 57 | #include "lldb/Target/ThreadSpec.h" |
| 58 | #include "lldb/Target/UnixSignals.h" |
| 59 | #include "lldb/Utility/Event.h" |
| 60 | #include "lldb/Utility/FileSpec.h" |
| 61 | #include "lldb/Utility/LLDBAssert.h" |
| 62 | #include "lldb/Utility/LLDBLog.h" |
| 63 | #include "lldb/Utility/Log.h" |
| 64 | #include "lldb/Utility/RealpathPrefixes.h" |
| 65 | #include "lldb/Utility/State.h" |
| 66 | #include "lldb/Utility/StreamString.h" |
| 67 | #include "lldb/Utility/Timer.h" |
| 68 | |
| 69 | #include "llvm/ADT/ScopeExit.h" |
| 70 | #include "llvm/ADT/SetVector.h" |
| 71 | #include "llvm/Support/ThreadPool.h" |
| 72 | |
| 73 | #include <memory> |
| 74 | #include <mutex> |
| 75 | #include <optional> |
| 76 | #include <sstream> |
| 77 | |
| 78 | using namespace lldb; |
| 79 | using namespace lldb_private; |
| 80 | |
| 81 | namespace { |
| 82 | |
| 83 | struct ExecutableInstaller { |
| 84 | |
| 85 | ExecutableInstaller(PlatformSP platform, ModuleSP module) |
| 86 | : m_platform{platform}, m_module{module}, |
| 87 | m_local_file{m_module->GetFileSpec()}, |
| 88 | m_remote_file{m_module->GetRemoteInstallFileSpec()} {} |
| 89 | |
| 90 | void setupRemoteFile() const { m_module->SetPlatformFileSpec(m_remote_file); } |
| 91 | |
| 92 | PlatformSP m_platform; |
| 93 | ModuleSP m_module; |
| 94 | const FileSpec m_local_file; |
| 95 | const FileSpec m_remote_file; |
| 96 | }; |
| 97 | |
| 98 | struct MainExecutableInstaller { |
| 99 | |
| 100 | MainExecutableInstaller(PlatformSP platform, ModuleSP module, TargetSP target, |
| 101 | ProcessLaunchInfo &launch_info) |
| 102 | : m_platform{platform}, m_module{module}, |
| 103 | m_local_file{m_module->GetFileSpec()}, |
| 104 | m_remote_file{ |
| 105 | getRemoteFileSpec(platform: m_platform, target, module: m_module, local_file: m_local_file)}, |
| 106 | m_launch_info{launch_info} {} |
| 107 | |
| 108 | void setupRemoteFile() const { |
| 109 | m_module->SetPlatformFileSpec(m_remote_file); |
| 110 | m_launch_info.SetExecutableFile(exe_file: m_remote_file, |
| 111 | /*add_exe_file_as_first_arg=*/false); |
| 112 | m_platform->SetFilePermissions(file_spec: m_remote_file, file_permissions: 0700 /*-rwx------*/); |
| 113 | } |
| 114 | |
| 115 | PlatformSP m_platform; |
| 116 | ModuleSP m_module; |
| 117 | const FileSpec m_local_file; |
| 118 | const FileSpec m_remote_file; |
| 119 | |
| 120 | private: |
| 121 | static FileSpec getRemoteFileSpec(PlatformSP platform, TargetSP target, |
| 122 | ModuleSP module, |
| 123 | const FileSpec &local_file) { |
| 124 | FileSpec remote_file = module->GetRemoteInstallFileSpec(); |
| 125 | if (remote_file || !target->GetAutoInstallMainExecutable()) |
| 126 | return remote_file; |
| 127 | |
| 128 | if (!local_file) |
| 129 | return {}; |
| 130 | |
| 131 | remote_file = platform->GetRemoteWorkingDirectory(); |
| 132 | remote_file.AppendPathComponent(component: local_file.GetFilename().GetCString()); |
| 133 | |
| 134 | return remote_file; |
| 135 | } |
| 136 | |
| 137 | ProcessLaunchInfo &m_launch_info; |
| 138 | }; |
| 139 | } // namespace |
| 140 | |
| 141 | template <typename Installer> |
| 142 | static Status installExecutable(const Installer &installer) { |
| 143 | if (!installer.m_local_file || !installer.m_remote_file) |
| 144 | return Status(); |
| 145 | |
| 146 | Status error = installer.m_platform->Install(installer.m_local_file, |
| 147 | installer.m_remote_file); |
| 148 | if (error.Fail()) |
| 149 | return error; |
| 150 | |
| 151 | installer.setupRemoteFile(); |
| 152 | return Status(); |
| 153 | } |
| 154 | |
| 155 | constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout; |
| 156 | |
| 157 | Target::Arch::Arch(const ArchSpec &spec) |
| 158 | : m_spec(spec), |
| 159 | m_plugin_up(PluginManager::CreateArchitectureInstance(arch: spec)) {} |
| 160 | |
| 161 | const Target::Arch &Target::Arch::operator=(const ArchSpec &spec) { |
| 162 | m_spec = spec; |
| 163 | m_plugin_up = PluginManager::CreateArchitectureInstance(arch: spec); |
| 164 | return *this; |
| 165 | } |
| 166 | |
| 167 | llvm::StringRef Target::GetStaticBroadcasterClass() { |
| 168 | static constexpr llvm::StringLiteral class_name("lldb.target" ); |
| 169 | return class_name; |
| 170 | } |
| 171 | |
| 172 | Target::Target(Debugger &debugger, const ArchSpec &target_arch, |
| 173 | const lldb::PlatformSP &platform_sp, bool is_dummy_target) |
| 174 | : TargetProperties(this), |
| 175 | Broadcaster(debugger.GetBroadcasterManager(), |
| 176 | Target::GetStaticBroadcasterClass().str()), |
| 177 | ExecutionContextScope(), m_debugger(debugger), m_platform_sp(platform_sp), |
| 178 | m_mutex(), m_arch(target_arch), m_images(this), m_section_load_history(), |
| 179 | m_breakpoint_list(false), m_internal_breakpoint_list(true), |
| 180 | m_watchpoint_list(), m_process_sp(), m_search_filter_sp(), |
| 181 | m_image_search_paths(ImageSearchPathsChanged, this), |
| 182 | m_source_manager_up(), m_stop_hooks(), m_stop_hook_next_id(0), |
| 183 | m_latest_stop_hook_id(0), m_valid(true), m_suppress_stop_hooks(false), |
| 184 | m_is_dummy_target(is_dummy_target), |
| 185 | m_frame_recognizer_manager_up( |
| 186 | std::make_unique<StackFrameRecognizerManager>()) { |
| 187 | SetEventName(event_mask: eBroadcastBitBreakpointChanged, name: "breakpoint-changed" ); |
| 188 | SetEventName(event_mask: eBroadcastBitModulesLoaded, name: "modules-loaded" ); |
| 189 | SetEventName(event_mask: eBroadcastBitModulesUnloaded, name: "modules-unloaded" ); |
| 190 | SetEventName(event_mask: eBroadcastBitWatchpointChanged, name: "watchpoint-changed" ); |
| 191 | SetEventName(event_mask: eBroadcastBitSymbolsLoaded, name: "symbols-loaded" ); |
| 192 | |
| 193 | CheckInWithManager(); |
| 194 | |
| 195 | LLDB_LOG(GetLog(LLDBLog::Object), "{0} Target::Target()" , |
| 196 | static_cast<void *>(this)); |
| 197 | if (target_arch.IsValid()) { |
| 198 | LLDB_LOG(GetLog(LLDBLog::Target), |
| 199 | "Target::Target created with architecture {0} ({1})" , |
| 200 | target_arch.GetArchitectureName(), |
| 201 | target_arch.GetTriple().getTriple().c_str()); |
| 202 | } |
| 203 | |
| 204 | UpdateLaunchInfoFromProperties(); |
| 205 | } |
| 206 | |
| 207 | Target::~Target() { |
| 208 | Log *log = GetLog(mask: LLDBLog::Object); |
| 209 | LLDB_LOG(log, "{0} Target::~Target()" , static_cast<void *>(this)); |
| 210 | DeleteCurrentProcess(); |
| 211 | } |
| 212 | |
| 213 | void Target::PrimeFromDummyTarget(Target &target) { |
| 214 | m_stop_hooks = target.m_stop_hooks; |
| 215 | m_stop_hook_next_id = target.m_stop_hook_next_id; |
| 216 | |
| 217 | for (const auto &breakpoint_sp : target.m_breakpoint_list.Breakpoints()) { |
| 218 | if (breakpoint_sp->IsInternal()) |
| 219 | continue; |
| 220 | |
| 221 | BreakpointSP new_bp( |
| 222 | Breakpoint::CopyFromBreakpoint(new_target: shared_from_this(), bp_to_copy_from: *breakpoint_sp)); |
| 223 | AddBreakpoint(breakpoint_sp: std::move(new_bp), internal: false); |
| 224 | } |
| 225 | |
| 226 | for (const auto &bp_name_entry : target.m_breakpoint_names) { |
| 227 | AddBreakpointName(bp_name: std::make_unique<BreakpointName>(args&: *bp_name_entry.second)); |
| 228 | } |
| 229 | |
| 230 | m_frame_recognizer_manager_up = std::make_unique<StackFrameRecognizerManager>( |
| 231 | args&: *target.m_frame_recognizer_manager_up); |
| 232 | |
| 233 | m_dummy_signals = target.m_dummy_signals; |
| 234 | } |
| 235 | |
| 236 | void Target::Dump(Stream *s, lldb::DescriptionLevel description_level) { |
| 237 | // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
| 238 | if (description_level != lldb::eDescriptionLevelBrief) { |
| 239 | s->Indent(); |
| 240 | s->PutCString(cstr: "Target\n" ); |
| 241 | s->IndentMore(); |
| 242 | m_images.Dump(s); |
| 243 | m_breakpoint_list.Dump(s); |
| 244 | m_internal_breakpoint_list.Dump(s); |
| 245 | s->IndentLess(); |
| 246 | } else { |
| 247 | Module *exe_module = GetExecutableModulePointer(); |
| 248 | if (exe_module) |
| 249 | s->PutCString(cstr: exe_module->GetFileSpec().GetFilename().GetCString()); |
| 250 | else |
| 251 | s->PutCString(cstr: "No executable module." ); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void Target::CleanupProcess() { |
| 256 | // Do any cleanup of the target we need to do between process instances. |
| 257 | // NB It is better to do this before destroying the process in case the |
| 258 | // clean up needs some help from the process. |
| 259 | m_breakpoint_list.ClearAllBreakpointSites(); |
| 260 | m_internal_breakpoint_list.ClearAllBreakpointSites(); |
| 261 | ResetBreakpointHitCounts(); |
| 262 | // Disable watchpoints just on the debugger side. |
| 263 | std::unique_lock<std::recursive_mutex> lock; |
| 264 | this->GetWatchpointList().GetListMutex(lock); |
| 265 | DisableAllWatchpoints(end_to_end: false); |
| 266 | ClearAllWatchpointHitCounts(); |
| 267 | ClearAllWatchpointHistoricValues(); |
| 268 | m_latest_stop_hook_id = 0; |
| 269 | } |
| 270 | |
| 271 | void Target::DeleteCurrentProcess() { |
| 272 | if (m_process_sp) { |
| 273 | // We dispose any active tracing sessions on the current process |
| 274 | m_trace_sp.reset(); |
| 275 | |
| 276 | if (m_process_sp->IsAlive()) |
| 277 | m_process_sp->Destroy(force_kill: false); |
| 278 | |
| 279 | m_process_sp->Finalize(destructing: false /* not destructing */); |
| 280 | |
| 281 | // Let the process finalize itself first, then clear the section load |
| 282 | // history. Some objects owned by the process might end up calling |
| 283 | // SectionLoadHistory::SetSectionUnloaded() which can create entries in |
| 284 | // the section load history that can mess up subsequent processes. |
| 285 | m_section_load_history.Clear(); |
| 286 | |
| 287 | CleanupProcess(); |
| 288 | |
| 289 | m_process_sp.reset(); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | const lldb::ProcessSP &Target::CreateProcess(ListenerSP listener_sp, |
| 294 | llvm::StringRef plugin_name, |
| 295 | const FileSpec *crash_file, |
| 296 | bool can_connect) { |
| 297 | if (!listener_sp) |
| 298 | listener_sp = GetDebugger().GetListener(); |
| 299 | DeleteCurrentProcess(); |
| 300 | m_process_sp = Process::FindPlugin(target_sp: shared_from_this(), plugin_name, |
| 301 | listener_sp, crash_file_path: crash_file, can_connect); |
| 302 | return m_process_sp; |
| 303 | } |
| 304 | |
| 305 | const lldb::ProcessSP &Target::GetProcessSP() const { return m_process_sp; } |
| 306 | |
| 307 | lldb::REPLSP Target::GetREPL(Status &err, lldb::LanguageType language, |
| 308 | const char *repl_options, bool can_create) { |
| 309 | if (language == eLanguageTypeUnknown) |
| 310 | language = m_debugger.GetREPLLanguage(); |
| 311 | |
| 312 | if (language == eLanguageTypeUnknown) { |
| 313 | LanguageSet repl_languages = Language::GetLanguagesSupportingREPLs(); |
| 314 | |
| 315 | if (auto single_lang = repl_languages.GetSingularLanguage()) { |
| 316 | language = *single_lang; |
| 317 | } else if (repl_languages.Empty()) { |
| 318 | err = Status::FromErrorString( |
| 319 | str: "LLDB isn't configured with REPL support for any languages." ); |
| 320 | return REPLSP(); |
| 321 | } else { |
| 322 | err = Status::FromErrorString( |
| 323 | str: "Multiple possible REPL languages. Please specify a language." ); |
| 324 | return REPLSP(); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | REPLMap::iterator pos = m_repl_map.find(x: language); |
| 329 | |
| 330 | if (pos != m_repl_map.end()) { |
| 331 | return pos->second; |
| 332 | } |
| 333 | |
| 334 | if (!can_create) { |
| 335 | err = Status::FromErrorStringWithFormat( |
| 336 | format: "Couldn't find an existing REPL for %s, and can't create a new one" , |
| 337 | Language::GetNameForLanguageType(language)); |
| 338 | return lldb::REPLSP(); |
| 339 | } |
| 340 | |
| 341 | Debugger *const debugger = nullptr; |
| 342 | lldb::REPLSP ret = REPL::Create(Status&: err, language, debugger, target: this, repl_options); |
| 343 | |
| 344 | if (ret) { |
| 345 | m_repl_map[language] = ret; |
| 346 | return m_repl_map[language]; |
| 347 | } |
| 348 | |
| 349 | if (err.Success()) { |
| 350 | err = Status::FromErrorStringWithFormat( |
| 351 | format: "Couldn't create a REPL for %s" , |
| 352 | Language::GetNameForLanguageType(language)); |
| 353 | } |
| 354 | |
| 355 | return lldb::REPLSP(); |
| 356 | } |
| 357 | |
| 358 | void Target::SetREPL(lldb::LanguageType language, lldb::REPLSP repl_sp) { |
| 359 | lldbassert(!m_repl_map.count(language)); |
| 360 | |
| 361 | m_repl_map[language] = repl_sp; |
| 362 | } |
| 363 | |
| 364 | void Target::Destroy() { |
| 365 | std::lock_guard<std::recursive_mutex> guard(m_mutex); |
| 366 | m_valid = false; |
| 367 | DeleteCurrentProcess(); |
| 368 | m_platform_sp.reset(); |
| 369 | m_arch = ArchSpec(); |
| 370 | ClearModules(delete_locations: true); |
| 371 | m_section_load_history.Clear(); |
| 372 | const bool notify = false; |
| 373 | m_breakpoint_list.RemoveAll(notify); |
| 374 | m_internal_breakpoint_list.RemoveAll(notify); |
| 375 | m_last_created_breakpoint.reset(); |
| 376 | m_watchpoint_list.RemoveAll(notify); |
| 377 | m_last_created_watchpoint.reset(); |
| 378 | m_search_filter_sp.reset(); |
| 379 | m_image_search_paths.Clear(notify); |
| 380 | m_stop_hooks.clear(); |
| 381 | m_stop_hook_next_id = 0; |
| 382 | m_suppress_stop_hooks = false; |
| 383 | m_repl_map.clear(); |
| 384 | Args signal_args; |
| 385 | ClearDummySignals(signal_names&: signal_args); |
| 386 | } |
| 387 | |
| 388 | llvm::StringRef Target::GetABIName() const { |
| 389 | lldb::ABISP abi_sp; |
| 390 | if (m_process_sp) |
| 391 | abi_sp = m_process_sp->GetABI(); |
| 392 | if (!abi_sp) |
| 393 | abi_sp = ABI::FindPlugin(process_sp: ProcessSP(), arch: GetArchitecture()); |
| 394 | if (abi_sp) |
| 395 | return abi_sp->GetPluginName(); |
| 396 | return {}; |
| 397 | } |
| 398 | |
| 399 | BreakpointList &Target::GetBreakpointList(bool internal) { |
| 400 | if (internal) |
| 401 | return m_internal_breakpoint_list; |
| 402 | else |
| 403 | return m_breakpoint_list; |
| 404 | } |
| 405 | |
| 406 | const BreakpointList &Target::GetBreakpointList(bool internal) const { |
| 407 | if (internal) |
| 408 | return m_internal_breakpoint_list; |
| 409 | else |
| 410 | return m_breakpoint_list; |
| 411 | } |
| 412 | |
| 413 | BreakpointSP Target::GetBreakpointByID(break_id_t break_id) { |
| 414 | BreakpointSP bp_sp; |
| 415 | |
| 416 | if (LLDB_BREAK_ID_IS_INTERNAL(break_id)) |
| 417 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID(breakID: break_id); |
| 418 | else |
| 419 | bp_sp = m_breakpoint_list.FindBreakpointByID(breakID: break_id); |
| 420 | |
| 421 | return bp_sp; |
| 422 | } |
| 423 | |
| 424 | lldb::BreakpointSP |
| 425 | lldb_private::Target::CreateBreakpointAtUserEntry(Status &error) { |
| 426 | ModuleSP main_module_sp = GetExecutableModule(); |
| 427 | FileSpecList shared_lib_filter; |
| 428 | shared_lib_filter.Append(file: main_module_sp->GetFileSpec()); |
| 429 | llvm::SetVector<std::string, std::vector<std::string>, |
| 430 | std::unordered_set<std::string>> |
| 431 | entryPointNamesSet; |
| 432 | for (LanguageType lang_type : Language::GetSupportedLanguages()) { |
| 433 | Language *lang = Language::FindPlugin(language: lang_type); |
| 434 | if (!lang) { |
| 435 | error = Status::FromErrorString(str: "Language not found\n" ); |
| 436 | return lldb::BreakpointSP(); |
| 437 | } |
| 438 | std::string entryPointName = lang->GetUserEntryPointName().str(); |
| 439 | if (!entryPointName.empty()) |
| 440 | entryPointNamesSet.insert(X: entryPointName); |
| 441 | } |
| 442 | if (entryPointNamesSet.empty()) { |
| 443 | error = Status::FromErrorString(str: "No entry point name found\n" ); |
| 444 | return lldb::BreakpointSP(); |
| 445 | } |
| 446 | BreakpointSP bp_sp = CreateBreakpoint( |
| 447 | containingModules: &shared_lib_filter, |
| 448 | /*containingSourceFiles=*/nullptr, func_names: entryPointNamesSet.takeVector(), |
| 449 | /*func_name_type_mask=*/eFunctionNameTypeFull, |
| 450 | /*language=*/eLanguageTypeUnknown, |
| 451 | /*offset=*/m_offset: 0, |
| 452 | /*skip_prologue=*/eLazyBoolNo, |
| 453 | /*internal=*/false, |
| 454 | /*hardware=*/request_hardware: false); |
| 455 | if (!bp_sp) { |
| 456 | error = Status::FromErrorString(str: "Breakpoint creation failed.\n" ); |
| 457 | return lldb::BreakpointSP(); |
| 458 | } |
| 459 | bp_sp->SetOneShot(true); |
| 460 | return bp_sp; |
| 461 | } |
| 462 | |
| 463 | BreakpointSP Target::CreateSourceRegexBreakpoint( |
| 464 | const FileSpecList *containingModules, |
| 465 | const FileSpecList *source_file_spec_list, |
| 466 | const std::unordered_set<std::string> &function_names, |
| 467 | RegularExpression source_regex, bool internal, bool hardware, |
| 468 | LazyBool move_to_nearest_code) { |
| 469 | SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList( |
| 470 | containingModules, containingSourceFiles: source_file_spec_list)); |
| 471 | if (move_to_nearest_code == eLazyBoolCalculate) |
| 472 | move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo; |
| 473 | BreakpointResolverSP resolver_sp(new BreakpointResolverFileRegex( |
| 474 | nullptr, std::move(source_regex), function_names, |
| 475 | !static_cast<bool>(move_to_nearest_code))); |
| 476 | |
| 477 | return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware: hardware, resolve_indirect_symbols: true); |
| 478 | } |
| 479 | |
| 480 | BreakpointSP Target::CreateBreakpoint(const FileSpecList *containingModules, |
| 481 | const FileSpec &file, uint32_t line_no, |
| 482 | uint32_t column, lldb::addr_t offset, |
| 483 | LazyBool check_inlines, |
| 484 | LazyBool skip_prologue, bool internal, |
| 485 | bool hardware, |
| 486 | LazyBool move_to_nearest_code) { |
| 487 | FileSpec remapped_file; |
| 488 | std::optional<llvm::StringRef> removed_prefix_opt = |
| 489 | GetSourcePathMap().ReverseRemapPath(file, fixed&: remapped_file); |
| 490 | if (!removed_prefix_opt) |
| 491 | remapped_file = file; |
| 492 | |
| 493 | if (check_inlines == eLazyBoolCalculate) { |
| 494 | const InlineStrategy inline_strategy = GetInlineStrategy(); |
| 495 | switch (inline_strategy) { |
| 496 | case eInlineBreakpointsNever: |
| 497 | check_inlines = eLazyBoolNo; |
| 498 | break; |
| 499 | |
| 500 | case eInlineBreakpointsHeaders: |
| 501 | if (remapped_file.IsSourceImplementationFile()) |
| 502 | check_inlines = eLazyBoolNo; |
| 503 | else |
| 504 | check_inlines = eLazyBoolYes; |
| 505 | break; |
| 506 | |
| 507 | case eInlineBreakpointsAlways: |
| 508 | check_inlines = eLazyBoolYes; |
| 509 | break; |
| 510 | } |
| 511 | } |
| 512 | SearchFilterSP filter_sp; |
| 513 | if (check_inlines == eLazyBoolNo) { |
| 514 | // Not checking for inlines, we are looking only for matching compile units |
| 515 | FileSpecList compile_unit_list; |
| 516 | compile_unit_list.Append(file: remapped_file); |
| 517 | filter_sp = GetSearchFilterForModuleAndCUList(containingModules, |
| 518 | containingSourceFiles: &compile_unit_list); |
| 519 | } else { |
| 520 | filter_sp = GetSearchFilterForModuleList(containingModuleList: containingModules); |
| 521 | } |
| 522 | if (skip_prologue == eLazyBoolCalculate) |
| 523 | skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo; |
| 524 | if (move_to_nearest_code == eLazyBoolCalculate) |
| 525 | move_to_nearest_code = GetMoveToNearestCode() ? eLazyBoolYes : eLazyBoolNo; |
| 526 | |
| 527 | SourceLocationSpec location_spec(remapped_file, line_no, column, |
| 528 | check_inlines, |
| 529 | !static_cast<bool>(move_to_nearest_code)); |
| 530 | if (!location_spec) |
| 531 | return nullptr; |
| 532 | |
| 533 | BreakpointResolverSP resolver_sp(new BreakpointResolverFileLine( |
| 534 | nullptr, offset, skip_prologue, location_spec, removed_prefix_opt)); |
| 535 | return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware: hardware, resolve_indirect_symbols: true); |
| 536 | } |
| 537 | |
| 538 | BreakpointSP Target::CreateBreakpoint(lldb::addr_t addr, bool internal, |
| 539 | bool hardware) { |
| 540 | Address so_addr; |
| 541 | |
| 542 | // Check for any reason we want to move this breakpoint to other address. |
| 543 | addr = GetBreakableLoadAddress(addr); |
| 544 | |
| 545 | // Attempt to resolve our load address if possible, though it is ok if it |
| 546 | // doesn't resolve to section/offset. |
| 547 | |
| 548 | // Try and resolve as a load address if possible |
| 549 | GetSectionLoadList().ResolveLoadAddress(load_addr: addr, so_addr); |
| 550 | if (!so_addr.IsValid()) { |
| 551 | // The address didn't resolve, so just set this as an absolute address |
| 552 | so_addr.SetOffset(addr); |
| 553 | } |
| 554 | BreakpointSP bp_sp(CreateBreakpoint(addr: so_addr, internal, request_hardware: hardware)); |
| 555 | return bp_sp; |
| 556 | } |
| 557 | |
| 558 | BreakpointSP Target::CreateBreakpoint(const Address &addr, bool internal, |
| 559 | bool hardware) { |
| 560 | SearchFilterSP filter_sp( |
| 561 | new SearchFilterForUnconstrainedSearches(shared_from_this())); |
| 562 | BreakpointResolverSP resolver_sp( |
| 563 | new BreakpointResolverAddress(nullptr, addr)); |
| 564 | return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware: hardware, resolve_indirect_symbols: false); |
| 565 | } |
| 566 | |
| 567 | lldb::BreakpointSP |
| 568 | Target::CreateAddressInModuleBreakpoint(lldb::addr_t file_addr, bool internal, |
| 569 | const FileSpec &file_spec, |
| 570 | bool request_hardware) { |
| 571 | SearchFilterSP filter_sp( |
| 572 | new SearchFilterForUnconstrainedSearches(shared_from_this())); |
| 573 | BreakpointResolverSP resolver_sp(new BreakpointResolverAddress( |
| 574 | nullptr, file_addr, file_spec)); |
| 575 | return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware, |
| 576 | resolve_indirect_symbols: false); |
| 577 | } |
| 578 | |
| 579 | BreakpointSP Target::CreateBreakpoint( |
| 580 | const FileSpecList *containingModules, |
| 581 | const FileSpecList *containingSourceFiles, const char *func_name, |
| 582 | FunctionNameType func_name_type_mask, LanguageType language, |
| 583 | lldb::addr_t offset, LazyBool skip_prologue, bool internal, bool hardware) { |
| 584 | BreakpointSP bp_sp; |
| 585 | if (func_name) { |
| 586 | SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList( |
| 587 | containingModules, containingSourceFiles)); |
| 588 | |
| 589 | if (skip_prologue == eLazyBoolCalculate) |
| 590 | skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo; |
| 591 | if (language == lldb::eLanguageTypeUnknown) |
| 592 | language = GetLanguage().AsLanguageType(); |
| 593 | |
| 594 | BreakpointResolverSP resolver_sp(new BreakpointResolverName( |
| 595 | nullptr, func_name, func_name_type_mask, language, Breakpoint::Exact, |
| 596 | offset, skip_prologue)); |
| 597 | bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware: hardware, resolve_indirect_symbols: true); |
| 598 | } |
| 599 | return bp_sp; |
| 600 | } |
| 601 | |
| 602 | lldb::BreakpointSP |
| 603 | Target::CreateBreakpoint(const FileSpecList *containingModules, |
| 604 | const FileSpecList *containingSourceFiles, |
| 605 | const std::vector<std::string> &func_names, |
| 606 | FunctionNameType func_name_type_mask, |
| 607 | LanguageType language, lldb::addr_t offset, |
| 608 | LazyBool skip_prologue, bool internal, bool hardware) { |
| 609 | BreakpointSP bp_sp; |
| 610 | size_t num_names = func_names.size(); |
| 611 | if (num_names > 0) { |
| 612 | SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList( |
| 613 | containingModules, containingSourceFiles)); |
| 614 | |
| 615 | if (skip_prologue == eLazyBoolCalculate) |
| 616 | skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo; |
| 617 | if (language == lldb::eLanguageTypeUnknown) |
| 618 | language = GetLanguage().AsLanguageType(); |
| 619 | |
| 620 | BreakpointResolverSP resolver_sp( |
| 621 | new BreakpointResolverName(nullptr, func_names, func_name_type_mask, |
| 622 | language, offset, skip_prologue)); |
| 623 | bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware: hardware, resolve_indirect_symbols: true); |
| 624 | } |
| 625 | return bp_sp; |
| 626 | } |
| 627 | |
| 628 | BreakpointSP |
| 629 | Target::CreateBreakpoint(const FileSpecList *containingModules, |
| 630 | const FileSpecList *containingSourceFiles, |
| 631 | const char *func_names[], size_t num_names, |
| 632 | FunctionNameType func_name_type_mask, |
| 633 | LanguageType language, lldb::addr_t offset, |
| 634 | LazyBool skip_prologue, bool internal, bool hardware) { |
| 635 | BreakpointSP bp_sp; |
| 636 | if (num_names > 0) { |
| 637 | SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList( |
| 638 | containingModules, containingSourceFiles)); |
| 639 | |
| 640 | if (skip_prologue == eLazyBoolCalculate) { |
| 641 | if (offset == 0) |
| 642 | skip_prologue = GetSkipPrologue() ? eLazyBoolYes : eLazyBoolNo; |
| 643 | else |
| 644 | skip_prologue = eLazyBoolNo; |
| 645 | } |
| 646 | if (language == lldb::eLanguageTypeUnknown) |
| 647 | language = GetLanguage().AsLanguageType(); |
| 648 | |
| 649 | BreakpointResolverSP resolver_sp(new BreakpointResolverName( |
| 650 | nullptr, func_names, num_names, func_name_type_mask, language, offset, |
| 651 | skip_prologue)); |
| 652 | resolver_sp->SetOffset(offset); |
| 653 | bp_sp = CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware: hardware, resolve_indirect_symbols: true); |
| 654 | } |
| 655 | return bp_sp; |
| 656 | } |
| 657 | |
| 658 | SearchFilterSP |
| 659 | Target::GetSearchFilterForModule(const FileSpec *containingModule) { |
| 660 | SearchFilterSP filter_sp; |
| 661 | if (containingModule != nullptr) { |
| 662 | // TODO: We should look into sharing module based search filters |
| 663 | // across many breakpoints like we do for the simple target based one |
| 664 | filter_sp = std::make_shared<SearchFilterByModule>(args: shared_from_this(), |
| 665 | args: *containingModule); |
| 666 | } else { |
| 667 | if (!m_search_filter_sp) |
| 668 | m_search_filter_sp = |
| 669 | std::make_shared<SearchFilterForUnconstrainedSearches>( |
| 670 | args: shared_from_this()); |
| 671 | filter_sp = m_search_filter_sp; |
| 672 | } |
| 673 | return filter_sp; |
| 674 | } |
| 675 | |
| 676 | SearchFilterSP |
| 677 | Target::GetSearchFilterForModuleList(const FileSpecList *containingModules) { |
| 678 | SearchFilterSP filter_sp; |
| 679 | if (containingModules && containingModules->GetSize() != 0) { |
| 680 | // TODO: We should look into sharing module based search filters |
| 681 | // across many breakpoints like we do for the simple target based one |
| 682 | filter_sp = std::make_shared<SearchFilterByModuleList>(args: shared_from_this(), |
| 683 | args: *containingModules); |
| 684 | } else { |
| 685 | if (!m_search_filter_sp) |
| 686 | m_search_filter_sp = |
| 687 | std::make_shared<SearchFilterForUnconstrainedSearches>( |
| 688 | args: shared_from_this()); |
| 689 | filter_sp = m_search_filter_sp; |
| 690 | } |
| 691 | return filter_sp; |
| 692 | } |
| 693 | |
| 694 | SearchFilterSP Target::GetSearchFilterForModuleAndCUList( |
| 695 | const FileSpecList *containingModules, |
| 696 | const FileSpecList *containingSourceFiles) { |
| 697 | if (containingSourceFiles == nullptr || containingSourceFiles->GetSize() == 0) |
| 698 | return GetSearchFilterForModuleList(containingModules); |
| 699 | |
| 700 | SearchFilterSP filter_sp; |
| 701 | if (containingModules == nullptr) { |
| 702 | // We could make a special "CU List only SearchFilter". Better yet was if |
| 703 | // these could be composable, but that will take a little reworking. |
| 704 | |
| 705 | filter_sp = std::make_shared<SearchFilterByModuleListAndCU>( |
| 706 | args: shared_from_this(), args: FileSpecList(), args: *containingSourceFiles); |
| 707 | } else { |
| 708 | filter_sp = std::make_shared<SearchFilterByModuleListAndCU>( |
| 709 | args: shared_from_this(), args: *containingModules, args: *containingSourceFiles); |
| 710 | } |
| 711 | return filter_sp; |
| 712 | } |
| 713 | |
| 714 | BreakpointSP Target::CreateFuncRegexBreakpoint( |
| 715 | const FileSpecList *containingModules, |
| 716 | const FileSpecList *containingSourceFiles, RegularExpression func_regex, |
| 717 | lldb::LanguageType requested_language, LazyBool skip_prologue, |
| 718 | bool internal, bool hardware) { |
| 719 | SearchFilterSP filter_sp(GetSearchFilterForModuleAndCUList( |
| 720 | containingModules, containingSourceFiles)); |
| 721 | bool skip = (skip_prologue == eLazyBoolCalculate) |
| 722 | ? GetSkipPrologue() |
| 723 | : static_cast<bool>(skip_prologue); |
| 724 | BreakpointResolverSP resolver_sp(new BreakpointResolverName( |
| 725 | nullptr, std::move(func_regex), requested_language, 0, skip)); |
| 726 | |
| 727 | return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware: hardware, resolve_indirect_symbols: true); |
| 728 | } |
| 729 | |
| 730 | lldb::BreakpointSP |
| 731 | Target::CreateExceptionBreakpoint(enum lldb::LanguageType language, |
| 732 | bool catch_bp, bool throw_bp, bool internal, |
| 733 | Args *additional_args, Status *error) { |
| 734 | BreakpointSP exc_bkpt_sp = LanguageRuntime::CreateExceptionBreakpoint( |
| 735 | target&: *this, language, catch_bp, throw_bp, is_internal: internal); |
| 736 | if (exc_bkpt_sp && additional_args) { |
| 737 | BreakpointPreconditionSP precondition_sp = exc_bkpt_sp->GetPrecondition(); |
| 738 | if (precondition_sp && additional_args) { |
| 739 | if (error) |
| 740 | *error = precondition_sp->ConfigurePrecondition(args&: *additional_args); |
| 741 | else |
| 742 | precondition_sp->ConfigurePrecondition(args&: *additional_args); |
| 743 | } |
| 744 | } |
| 745 | return exc_bkpt_sp; |
| 746 | } |
| 747 | |
| 748 | lldb::BreakpointSP Target::CreateScriptedBreakpoint( |
| 749 | const llvm::StringRef class_name, const FileSpecList *containingModules, |
| 750 | const FileSpecList *containingSourceFiles, bool internal, |
| 751 | bool request_hardware, StructuredData::ObjectSP , |
| 752 | Status *creation_error) { |
| 753 | SearchFilterSP filter_sp; |
| 754 | |
| 755 | lldb::SearchDepth depth = lldb::eSearchDepthTarget; |
| 756 | bool has_files = |
| 757 | containingSourceFiles && containingSourceFiles->GetSize() > 0; |
| 758 | bool has_modules = containingModules && containingModules->GetSize() > 0; |
| 759 | |
| 760 | if (has_files && has_modules) { |
| 761 | filter_sp = GetSearchFilterForModuleAndCUList(containingModules, |
| 762 | containingSourceFiles); |
| 763 | } else if (has_files) { |
| 764 | filter_sp = |
| 765 | GetSearchFilterForModuleAndCUList(containingModules: nullptr, containingSourceFiles); |
| 766 | } else if (has_modules) { |
| 767 | filter_sp = GetSearchFilterForModuleList(containingModules); |
| 768 | } else { |
| 769 | filter_sp = std::make_shared<SearchFilterForUnconstrainedSearches>( |
| 770 | args: shared_from_this()); |
| 771 | } |
| 772 | |
| 773 | BreakpointResolverSP resolver_sp(new BreakpointResolverScripted( |
| 774 | nullptr, class_name, depth, StructuredDataImpl(extra_args_sp))); |
| 775 | return CreateBreakpoint(filter_sp, resolver_sp, internal, request_hardware: false, resolve_indirect_symbols: true); |
| 776 | } |
| 777 | |
| 778 | BreakpointSP Target::CreateBreakpoint(SearchFilterSP &filter_sp, |
| 779 | BreakpointResolverSP &resolver_sp, |
| 780 | bool internal, bool request_hardware, |
| 781 | bool resolve_indirect_symbols) { |
| 782 | BreakpointSP bp_sp; |
| 783 | if (filter_sp && resolver_sp) { |
| 784 | const bool hardware = request_hardware || GetRequireHardwareBreakpoints(); |
| 785 | bp_sp.reset(p: new Breakpoint(*this, filter_sp, resolver_sp, hardware, |
| 786 | resolve_indirect_symbols)); |
| 787 | resolver_sp->SetBreakpoint(bp_sp); |
| 788 | AddBreakpoint(breakpoint_sp: bp_sp, internal); |
| 789 | } |
| 790 | return bp_sp; |
| 791 | } |
| 792 | |
| 793 | void Target::AddBreakpoint(lldb::BreakpointSP bp_sp, bool internal) { |
| 794 | if (!bp_sp) |
| 795 | return; |
| 796 | if (internal) |
| 797 | m_internal_breakpoint_list.Add(bp_sp, notify: false); |
| 798 | else |
| 799 | m_breakpoint_list.Add(bp_sp, notify: true); |
| 800 | |
| 801 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 802 | if (log) { |
| 803 | StreamString s; |
| 804 | bp_sp->GetDescription(s: &s, level: lldb::eDescriptionLevelVerbose); |
| 805 | LLDB_LOGF(log, "Target::%s (internal = %s) => break_id = %s\n" , |
| 806 | __FUNCTION__, bp_sp->IsInternal() ? "yes" : "no" , s.GetData()); |
| 807 | } |
| 808 | |
| 809 | bp_sp->ResolveBreakpoint(); |
| 810 | |
| 811 | if (!internal) { |
| 812 | m_last_created_breakpoint = bp_sp; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | void Target::AddNameToBreakpoint(BreakpointID &id, llvm::StringRef name, |
| 817 | Status &error) { |
| 818 | BreakpointSP bp_sp = |
| 819 | m_breakpoint_list.FindBreakpointByID(breakID: id.GetBreakpointID()); |
| 820 | if (!bp_sp) { |
| 821 | StreamString s; |
| 822 | id.GetDescription(s: &s, level: eDescriptionLevelBrief); |
| 823 | error = Status::FromErrorStringWithFormat(format: "Could not find breakpoint %s" , |
| 824 | s.GetData()); |
| 825 | return; |
| 826 | } |
| 827 | AddNameToBreakpoint(bp_sp, name, error); |
| 828 | } |
| 829 | |
| 830 | void Target::AddNameToBreakpoint(BreakpointSP &bp_sp, llvm::StringRef name, |
| 831 | Status &error) { |
| 832 | if (!bp_sp) |
| 833 | return; |
| 834 | |
| 835 | BreakpointName *bp_name = FindBreakpointName(name: ConstString(name), can_create: true, error); |
| 836 | if (!bp_name) |
| 837 | return; |
| 838 | |
| 839 | bp_name->ConfigureBreakpoint(bp_sp); |
| 840 | bp_sp->AddName(new_name: name); |
| 841 | } |
| 842 | |
| 843 | void Target::AddBreakpointName(std::unique_ptr<BreakpointName> bp_name) { |
| 844 | m_breakpoint_names.insert( |
| 845 | x: std::make_pair(x: bp_name->GetName(), y: std::move(bp_name))); |
| 846 | } |
| 847 | |
| 848 | BreakpointName *Target::FindBreakpointName(ConstString name, bool can_create, |
| 849 | Status &error) { |
| 850 | BreakpointID::StringIsBreakpointName(str: name.GetStringRef(), error); |
| 851 | if (!error.Success()) |
| 852 | return nullptr; |
| 853 | |
| 854 | BreakpointNameList::iterator iter = m_breakpoint_names.find(x: name); |
| 855 | if (iter != m_breakpoint_names.end()) { |
| 856 | return iter->second.get(); |
| 857 | } |
| 858 | |
| 859 | if (!can_create) { |
| 860 | error = Status::FromErrorStringWithFormat( |
| 861 | format: "Breakpoint name \"%s\" doesn't exist and " |
| 862 | "can_create is false." , |
| 863 | name.AsCString()); |
| 864 | return nullptr; |
| 865 | } |
| 866 | |
| 867 | return m_breakpoint_names |
| 868 | .insert(x: std::make_pair(x&: name, y: std::make_unique<BreakpointName>(args&: name))) |
| 869 | .first->second.get(); |
| 870 | } |
| 871 | |
| 872 | void Target::DeleteBreakpointName(ConstString name) { |
| 873 | BreakpointNameList::iterator iter = m_breakpoint_names.find(x: name); |
| 874 | |
| 875 | if (iter != m_breakpoint_names.end()) { |
| 876 | const char *name_cstr = name.AsCString(); |
| 877 | m_breakpoint_names.erase(position: iter); |
| 878 | for (auto bp_sp : m_breakpoint_list.Breakpoints()) |
| 879 | bp_sp->RemoveName(name_to_remove: name_cstr); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | void Target::RemoveNameFromBreakpoint(lldb::BreakpointSP &bp_sp, |
| 884 | ConstString name) { |
| 885 | bp_sp->RemoveName(name_to_remove: name.AsCString()); |
| 886 | } |
| 887 | |
| 888 | void Target::ConfigureBreakpointName( |
| 889 | BreakpointName &bp_name, const BreakpointOptions &new_options, |
| 890 | const BreakpointName::Permissions &new_permissions) { |
| 891 | bp_name.GetOptions().CopyOverSetOptions(rhs: new_options); |
| 892 | bp_name.GetPermissions().MergeInto(incoming: new_permissions); |
| 893 | ApplyNameToBreakpoints(bp_name); |
| 894 | } |
| 895 | |
| 896 | void Target::ApplyNameToBreakpoints(BreakpointName &bp_name) { |
| 897 | llvm::Expected<std::vector<BreakpointSP>> expected_vector = |
| 898 | m_breakpoint_list.FindBreakpointsByName(name: bp_name.GetName().AsCString()); |
| 899 | |
| 900 | if (!expected_vector) { |
| 901 | LLDB_LOG(GetLog(LLDBLog::Breakpoints), "invalid breakpoint name: {}" , |
| 902 | llvm::toString(expected_vector.takeError())); |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | for (auto bp_sp : *expected_vector) |
| 907 | bp_name.ConfigureBreakpoint(bp_sp); |
| 908 | } |
| 909 | |
| 910 | void Target::GetBreakpointNames(std::vector<std::string> &names) { |
| 911 | names.clear(); |
| 912 | for (const auto& bp_name_entry : m_breakpoint_names) { |
| 913 | names.push_back(x: bp_name_entry.first.AsCString()); |
| 914 | } |
| 915 | llvm::sort(C&: names); |
| 916 | } |
| 917 | |
| 918 | bool Target::ProcessIsValid() { |
| 919 | return (m_process_sp && m_process_sp->IsAlive()); |
| 920 | } |
| 921 | |
| 922 | static bool CheckIfWatchpointsSupported(Target *target, Status &error) { |
| 923 | std::optional<uint32_t> num_supported_hardware_watchpoints = |
| 924 | target->GetProcessSP()->GetWatchpointSlotCount(); |
| 925 | |
| 926 | // If unable to determine the # of watchpoints available, |
| 927 | // assume they are supported. |
| 928 | if (!num_supported_hardware_watchpoints) |
| 929 | return true; |
| 930 | |
| 931 | if (*num_supported_hardware_watchpoints == 0) { |
| 932 | error = Status::FromErrorStringWithFormat( |
| 933 | format: "Target supports (%u) hardware watchpoint slots.\n" , |
| 934 | *num_supported_hardware_watchpoints); |
| 935 | return false; |
| 936 | } |
| 937 | return true; |
| 938 | } |
| 939 | |
| 940 | // See also Watchpoint::SetWatchpointType(uint32_t type) and the |
| 941 | // OptionGroupWatchpoint::WatchType enum type. |
| 942 | WatchpointSP Target::CreateWatchpoint(lldb::addr_t addr, size_t size, |
| 943 | const CompilerType *type, uint32_t kind, |
| 944 | Status &error) { |
| 945 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 946 | LLDB_LOGF(log, |
| 947 | "Target::%s (addr = 0x%8.8" PRIx64 " size = %" PRIu64 |
| 948 | " type = %u)\n" , |
| 949 | __FUNCTION__, addr, (uint64_t)size, kind); |
| 950 | |
| 951 | WatchpointSP wp_sp; |
| 952 | if (!ProcessIsValid()) { |
| 953 | error = Status::FromErrorString(str: "process is not alive" ); |
| 954 | return wp_sp; |
| 955 | } |
| 956 | |
| 957 | if (addr == LLDB_INVALID_ADDRESS || size == 0) { |
| 958 | if (size == 0) |
| 959 | error = Status::FromErrorString( |
| 960 | str: "cannot set a watchpoint with watch_size of 0" ); |
| 961 | else |
| 962 | error = Status::FromErrorStringWithFormat( |
| 963 | format: "invalid watch address: %" PRIu64, addr); |
| 964 | return wp_sp; |
| 965 | } |
| 966 | |
| 967 | if (!LLDB_WATCH_TYPE_IS_VALID(kind)) { |
| 968 | error = |
| 969 | Status::FromErrorStringWithFormat(format: "invalid watchpoint type: %d" , kind); |
| 970 | } |
| 971 | |
| 972 | if (!CheckIfWatchpointsSupported(target: this, error)) |
| 973 | return wp_sp; |
| 974 | |
| 975 | // Currently we only support one watchpoint per address, with total number of |
| 976 | // watchpoints limited by the hardware which the inferior is running on. |
| 977 | |
| 978 | // Grab the list mutex while doing operations. |
| 979 | const bool notify = false; // Don't notify about all the state changes we do |
| 980 | // on creating the watchpoint. |
| 981 | |
| 982 | // Mask off ignored bits from watchpoint address. |
| 983 | if (ABISP abi = m_process_sp->GetABI()) |
| 984 | addr = abi->FixDataAddress(pc: addr); |
| 985 | |
| 986 | // LWP_TODO this sequence is looking for an existing watchpoint |
| 987 | // at the exact same user-specified address, disables the new one |
| 988 | // if addr/size/type match. If type/size differ, disable old one. |
| 989 | // This isn't correct, we need both watchpoints to use a shared |
| 990 | // WatchpointResource in the target, and expand the WatchpointResource |
| 991 | // to handle the needs of both Watchpoints. |
| 992 | // Also, even if the addresses don't match, they may need to be |
| 993 | // supported by the same WatchpointResource, e.g. a watchpoint |
| 994 | // watching 1 byte at 0x102 and a watchpoint watching 1 byte at 0x103. |
| 995 | // They're in the same word and must be watched by a single hardware |
| 996 | // watchpoint register. |
| 997 | |
| 998 | std::unique_lock<std::recursive_mutex> lock; |
| 999 | this->GetWatchpointList().GetListMutex(lock); |
| 1000 | WatchpointSP matched_sp = m_watchpoint_list.FindByAddress(addr); |
| 1001 | if (matched_sp) { |
| 1002 | size_t old_size = matched_sp->GetByteSize(); |
| 1003 | uint32_t old_type = |
| 1004 | (matched_sp->WatchpointRead() ? LLDB_WATCH_TYPE_READ : 0) | |
| 1005 | (matched_sp->WatchpointWrite() ? LLDB_WATCH_TYPE_WRITE : 0) | |
| 1006 | (matched_sp->WatchpointModify() ? LLDB_WATCH_TYPE_MODIFY : 0); |
| 1007 | // Return the existing watchpoint if both size and type match. |
| 1008 | if (size == old_size && kind == old_type) { |
| 1009 | wp_sp = matched_sp; |
| 1010 | wp_sp->SetEnabled(enabled: false, notify); |
| 1011 | } else { |
| 1012 | // Nil the matched watchpoint; we will be creating a new one. |
| 1013 | m_process_sp->DisableWatchpoint(wp_sp: matched_sp, notify); |
| 1014 | m_watchpoint_list.Remove(watchID: matched_sp->GetID(), notify: true); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | if (!wp_sp) { |
| 1019 | wp_sp = std::make_shared<Watchpoint>(args&: *this, args&: addr, args&: size, args&: type); |
| 1020 | wp_sp->SetWatchpointType(type: kind, notify); |
| 1021 | m_watchpoint_list.Add(wp_sp, notify: true); |
| 1022 | } |
| 1023 | |
| 1024 | error = m_process_sp->EnableWatchpoint(wp_sp, notify); |
| 1025 | LLDB_LOGF(log, "Target::%s (creation of watchpoint %s with id = %u)\n" , |
| 1026 | __FUNCTION__, error.Success() ? "succeeded" : "failed" , |
| 1027 | wp_sp->GetID()); |
| 1028 | |
| 1029 | if (error.Fail()) { |
| 1030 | // Enabling the watchpoint on the device side failed. Remove the said |
| 1031 | // watchpoint from the list maintained by the target instance. |
| 1032 | m_watchpoint_list.Remove(watchID: wp_sp->GetID(), notify: true); |
| 1033 | wp_sp.reset(); |
| 1034 | } else |
| 1035 | m_last_created_watchpoint = wp_sp; |
| 1036 | return wp_sp; |
| 1037 | } |
| 1038 | |
| 1039 | void Target::RemoveAllowedBreakpoints() { |
| 1040 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1041 | LLDB_LOGF(log, "Target::%s \n" , __FUNCTION__); |
| 1042 | |
| 1043 | m_breakpoint_list.RemoveAllowed(notify: true); |
| 1044 | |
| 1045 | m_last_created_breakpoint.reset(); |
| 1046 | } |
| 1047 | |
| 1048 | void Target::RemoveAllBreakpoints(bool internal_also) { |
| 1049 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1050 | LLDB_LOGF(log, "Target::%s (internal_also = %s)\n" , __FUNCTION__, |
| 1051 | internal_also ? "yes" : "no" ); |
| 1052 | |
| 1053 | m_breakpoint_list.RemoveAll(notify: true); |
| 1054 | if (internal_also) |
| 1055 | m_internal_breakpoint_list.RemoveAll(notify: false); |
| 1056 | |
| 1057 | m_last_created_breakpoint.reset(); |
| 1058 | } |
| 1059 | |
| 1060 | void Target::DisableAllBreakpoints(bool internal_also) { |
| 1061 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1062 | LLDB_LOGF(log, "Target::%s (internal_also = %s)\n" , __FUNCTION__, |
| 1063 | internal_also ? "yes" : "no" ); |
| 1064 | |
| 1065 | m_breakpoint_list.SetEnabledAll(false); |
| 1066 | if (internal_also) |
| 1067 | m_internal_breakpoint_list.SetEnabledAll(false); |
| 1068 | } |
| 1069 | |
| 1070 | void Target::DisableAllowedBreakpoints() { |
| 1071 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1072 | LLDB_LOGF(log, "Target::%s" , __FUNCTION__); |
| 1073 | |
| 1074 | m_breakpoint_list.SetEnabledAllowed(false); |
| 1075 | } |
| 1076 | |
| 1077 | void Target::EnableAllBreakpoints(bool internal_also) { |
| 1078 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1079 | LLDB_LOGF(log, "Target::%s (internal_also = %s)\n" , __FUNCTION__, |
| 1080 | internal_also ? "yes" : "no" ); |
| 1081 | |
| 1082 | m_breakpoint_list.SetEnabledAll(true); |
| 1083 | if (internal_also) |
| 1084 | m_internal_breakpoint_list.SetEnabledAll(true); |
| 1085 | } |
| 1086 | |
| 1087 | void Target::EnableAllowedBreakpoints() { |
| 1088 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1089 | LLDB_LOGF(log, "Target::%s" , __FUNCTION__); |
| 1090 | |
| 1091 | m_breakpoint_list.SetEnabledAllowed(true); |
| 1092 | } |
| 1093 | |
| 1094 | bool Target::RemoveBreakpointByID(break_id_t break_id) { |
| 1095 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1096 | LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n" , __FUNCTION__, |
| 1097 | break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no" ); |
| 1098 | |
| 1099 | if (DisableBreakpointByID(break_id)) { |
| 1100 | if (LLDB_BREAK_ID_IS_INTERNAL(break_id)) |
| 1101 | m_internal_breakpoint_list.Remove(breakID: break_id, notify: false); |
| 1102 | else { |
| 1103 | if (m_last_created_breakpoint) { |
| 1104 | if (m_last_created_breakpoint->GetID() == break_id) |
| 1105 | m_last_created_breakpoint.reset(); |
| 1106 | } |
| 1107 | m_breakpoint_list.Remove(breakID: break_id, notify: true); |
| 1108 | } |
| 1109 | return true; |
| 1110 | } |
| 1111 | return false; |
| 1112 | } |
| 1113 | |
| 1114 | bool Target::DisableBreakpointByID(break_id_t break_id) { |
| 1115 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1116 | LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n" , __FUNCTION__, |
| 1117 | break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no" ); |
| 1118 | |
| 1119 | BreakpointSP bp_sp; |
| 1120 | |
| 1121 | if (LLDB_BREAK_ID_IS_INTERNAL(break_id)) |
| 1122 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID(breakID: break_id); |
| 1123 | else |
| 1124 | bp_sp = m_breakpoint_list.FindBreakpointByID(breakID: break_id); |
| 1125 | if (bp_sp) { |
| 1126 | bp_sp->SetEnabled(false); |
| 1127 | return true; |
| 1128 | } |
| 1129 | return false; |
| 1130 | } |
| 1131 | |
| 1132 | bool Target::EnableBreakpointByID(break_id_t break_id) { |
| 1133 | Log *log = GetLog(mask: LLDBLog::Breakpoints); |
| 1134 | LLDB_LOGF(log, "Target::%s (break_id = %i, internal = %s)\n" , __FUNCTION__, |
| 1135 | break_id, LLDB_BREAK_ID_IS_INTERNAL(break_id) ? "yes" : "no" ); |
| 1136 | |
| 1137 | BreakpointSP bp_sp; |
| 1138 | |
| 1139 | if (LLDB_BREAK_ID_IS_INTERNAL(break_id)) |
| 1140 | bp_sp = m_internal_breakpoint_list.FindBreakpointByID(breakID: break_id); |
| 1141 | else |
| 1142 | bp_sp = m_breakpoint_list.FindBreakpointByID(breakID: break_id); |
| 1143 | |
| 1144 | if (bp_sp) { |
| 1145 | bp_sp->SetEnabled(true); |
| 1146 | return true; |
| 1147 | } |
| 1148 | return false; |
| 1149 | } |
| 1150 | |
| 1151 | void Target::ResetBreakpointHitCounts() { |
| 1152 | GetBreakpointList().ResetHitCounts(); |
| 1153 | } |
| 1154 | |
| 1155 | Status Target::SerializeBreakpointsToFile(const FileSpec &file, |
| 1156 | const BreakpointIDList &bp_ids, |
| 1157 | bool append) { |
| 1158 | Status error; |
| 1159 | |
| 1160 | if (!file) { |
| 1161 | error = Status::FromErrorString(str: "Invalid FileSpec." ); |
| 1162 | return error; |
| 1163 | } |
| 1164 | |
| 1165 | std::string path(file.GetPath()); |
| 1166 | StructuredData::ObjectSP input_data_sp; |
| 1167 | |
| 1168 | StructuredData::ArraySP break_store_sp; |
| 1169 | StructuredData::Array *break_store_ptr = nullptr; |
| 1170 | |
| 1171 | if (append) { |
| 1172 | input_data_sp = StructuredData::ParseJSONFromFile(file, error); |
| 1173 | if (error.Success()) { |
| 1174 | break_store_ptr = input_data_sp->GetAsArray(); |
| 1175 | if (!break_store_ptr) { |
| 1176 | error = Status::FromErrorStringWithFormat( |
| 1177 | format: "Tried to append to invalid input file %s" , path.c_str()); |
| 1178 | return error; |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | if (!break_store_ptr) { |
| 1184 | break_store_sp = std::make_shared<StructuredData::Array>(); |
| 1185 | break_store_ptr = break_store_sp.get(); |
| 1186 | } |
| 1187 | |
| 1188 | StreamFile out_file(path.c_str(), |
| 1189 | File::eOpenOptionTruncate | File::eOpenOptionWriteOnly | |
| 1190 | File::eOpenOptionCanCreate | |
| 1191 | File::eOpenOptionCloseOnExec, |
| 1192 | lldb::eFilePermissionsFileDefault); |
| 1193 | if (!out_file.GetFile().IsValid()) { |
| 1194 | error = Status::FromErrorStringWithFormat(format: "Unable to open output file: %s." , |
| 1195 | path.c_str()); |
| 1196 | return error; |
| 1197 | } |
| 1198 | |
| 1199 | std::unique_lock<std::recursive_mutex> lock; |
| 1200 | GetBreakpointList().GetListMutex(lock); |
| 1201 | |
| 1202 | if (bp_ids.GetSize() == 0) { |
| 1203 | const BreakpointList &breakpoints = GetBreakpointList(); |
| 1204 | |
| 1205 | size_t num_breakpoints = breakpoints.GetSize(); |
| 1206 | for (size_t i = 0; i < num_breakpoints; i++) { |
| 1207 | Breakpoint *bp = breakpoints.GetBreakpointAtIndex(i).get(); |
| 1208 | StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData(); |
| 1209 | // If a breakpoint can't serialize it, just ignore it for now: |
| 1210 | if (bkpt_save_sp) |
| 1211 | break_store_ptr->AddItem(item: bkpt_save_sp); |
| 1212 | } |
| 1213 | } else { |
| 1214 | |
| 1215 | std::unordered_set<lldb::break_id_t> processed_bkpts; |
| 1216 | const size_t count = bp_ids.GetSize(); |
| 1217 | for (size_t i = 0; i < count; ++i) { |
| 1218 | BreakpointID cur_bp_id = bp_ids.GetBreakpointIDAtIndex(index: i); |
| 1219 | lldb::break_id_t bp_id = cur_bp_id.GetBreakpointID(); |
| 1220 | |
| 1221 | if (bp_id != LLDB_INVALID_BREAK_ID) { |
| 1222 | // Only do each breakpoint once: |
| 1223 | std::pair<std::unordered_set<lldb::break_id_t>::iterator, bool> |
| 1224 | insert_result = processed_bkpts.insert(x: bp_id); |
| 1225 | if (!insert_result.second) |
| 1226 | continue; |
| 1227 | |
| 1228 | Breakpoint *bp = GetBreakpointByID(break_id: bp_id).get(); |
| 1229 | StructuredData::ObjectSP bkpt_save_sp = bp->SerializeToStructuredData(); |
| 1230 | // If the user explicitly asked to serialize a breakpoint, and we |
| 1231 | // can't, then raise an error: |
| 1232 | if (!bkpt_save_sp) { |
| 1233 | error = Status::FromErrorStringWithFormat( |
| 1234 | format: "Unable to serialize breakpoint %d" , bp_id); |
| 1235 | return error; |
| 1236 | } |
| 1237 | break_store_ptr->AddItem(item: bkpt_save_sp); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | break_store_ptr->Dump(s&: out_file, pretty_print: false); |
| 1243 | out_file.PutChar(ch: '\n'); |
| 1244 | return error; |
| 1245 | } |
| 1246 | |
| 1247 | Status Target::CreateBreakpointsFromFile(const FileSpec &file, |
| 1248 | BreakpointIDList &new_bps) { |
| 1249 | std::vector<std::string> no_names; |
| 1250 | return CreateBreakpointsFromFile(file, names&: no_names, new_bps); |
| 1251 | } |
| 1252 | |
| 1253 | Status Target::CreateBreakpointsFromFile(const FileSpec &file, |
| 1254 | std::vector<std::string> &names, |
| 1255 | BreakpointIDList &new_bps) { |
| 1256 | std::unique_lock<std::recursive_mutex> lock; |
| 1257 | GetBreakpointList().GetListMutex(lock); |
| 1258 | |
| 1259 | Status error; |
| 1260 | StructuredData::ObjectSP input_data_sp = |
| 1261 | StructuredData::ParseJSONFromFile(file, error); |
| 1262 | if (!error.Success()) { |
| 1263 | return error; |
| 1264 | } else if (!input_data_sp || !input_data_sp->IsValid()) { |
| 1265 | error = Status::FromErrorStringWithFormat( |
| 1266 | format: "Invalid JSON from input file: %s." , file.GetPath().c_str()); |
| 1267 | return error; |
| 1268 | } |
| 1269 | |
| 1270 | StructuredData::Array *bkpt_array = input_data_sp->GetAsArray(); |
| 1271 | if (!bkpt_array) { |
| 1272 | error = Status::FromErrorStringWithFormat( |
| 1273 | format: "Invalid breakpoint data from input file: %s." , file.GetPath().c_str()); |
| 1274 | return error; |
| 1275 | } |
| 1276 | |
| 1277 | size_t num_bkpts = bkpt_array->GetSize(); |
| 1278 | size_t num_names = names.size(); |
| 1279 | |
| 1280 | for (size_t i = 0; i < num_bkpts; i++) { |
| 1281 | StructuredData::ObjectSP bkpt_object_sp = bkpt_array->GetItemAtIndex(idx: i); |
| 1282 | // Peel off the breakpoint key, and feed the rest to the Breakpoint: |
| 1283 | StructuredData::Dictionary *bkpt_dict = bkpt_object_sp->GetAsDictionary(); |
| 1284 | if (!bkpt_dict) { |
| 1285 | error = Status::FromErrorStringWithFormat( |
| 1286 | format: "Invalid breakpoint data for element %zu from input file: %s." , i, |
| 1287 | file.GetPath().c_str()); |
| 1288 | return error; |
| 1289 | } |
| 1290 | StructuredData::ObjectSP bkpt_data_sp = |
| 1291 | bkpt_dict->GetValueForKey(key: Breakpoint::GetSerializationKey()); |
| 1292 | if (num_names && |
| 1293 | !Breakpoint::SerializedBreakpointMatchesNames(bkpt_object_sp&: bkpt_data_sp, names)) |
| 1294 | continue; |
| 1295 | |
| 1296 | BreakpointSP bkpt_sp = Breakpoint::CreateFromStructuredData( |
| 1297 | target_sp: shared_from_this(), data_object_sp&: bkpt_data_sp, error); |
| 1298 | if (!error.Success()) { |
| 1299 | error = Status::FromErrorStringWithFormat( |
| 1300 | format: "Error restoring breakpoint %zu from %s: %s." , i, |
| 1301 | file.GetPath().c_str(), error.AsCString()); |
| 1302 | return error; |
| 1303 | } |
| 1304 | new_bps.AddBreakpointID(bp_id: BreakpointID(bkpt_sp->GetID())); |
| 1305 | } |
| 1306 | return error; |
| 1307 | } |
| 1308 | |
| 1309 | // The flag 'end_to_end', default to true, signifies that the operation is |
| 1310 | // performed end to end, for both the debugger and the debuggee. |
| 1311 | |
| 1312 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end |
| 1313 | // to end operations. |
| 1314 | bool Target::RemoveAllWatchpoints(bool end_to_end) { |
| 1315 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1316 | LLDB_LOGF(log, "Target::%s\n" , __FUNCTION__); |
| 1317 | |
| 1318 | if (!end_to_end) { |
| 1319 | m_watchpoint_list.RemoveAll(notify: true); |
| 1320 | return true; |
| 1321 | } |
| 1322 | |
| 1323 | // Otherwise, it's an end to end operation. |
| 1324 | |
| 1325 | if (!ProcessIsValid()) |
| 1326 | return false; |
| 1327 | |
| 1328 | for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) { |
| 1329 | if (!wp_sp) |
| 1330 | return false; |
| 1331 | |
| 1332 | Status rc = m_process_sp->DisableWatchpoint(wp_sp); |
| 1333 | if (rc.Fail()) |
| 1334 | return false; |
| 1335 | } |
| 1336 | m_watchpoint_list.RemoveAll(notify: true); |
| 1337 | m_last_created_watchpoint.reset(); |
| 1338 | return true; // Success! |
| 1339 | } |
| 1340 | |
| 1341 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end |
| 1342 | // to end operations. |
| 1343 | bool Target::DisableAllWatchpoints(bool end_to_end) { |
| 1344 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1345 | LLDB_LOGF(log, "Target::%s\n" , __FUNCTION__); |
| 1346 | |
| 1347 | if (!end_to_end) { |
| 1348 | m_watchpoint_list.SetEnabledAll(false); |
| 1349 | return true; |
| 1350 | } |
| 1351 | |
| 1352 | // Otherwise, it's an end to end operation. |
| 1353 | |
| 1354 | if (!ProcessIsValid()) |
| 1355 | return false; |
| 1356 | |
| 1357 | for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) { |
| 1358 | if (!wp_sp) |
| 1359 | return false; |
| 1360 | |
| 1361 | Status rc = m_process_sp->DisableWatchpoint(wp_sp); |
| 1362 | if (rc.Fail()) |
| 1363 | return false; |
| 1364 | } |
| 1365 | return true; // Success! |
| 1366 | } |
| 1367 | |
| 1368 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list for end |
| 1369 | // to end operations. |
| 1370 | bool Target::EnableAllWatchpoints(bool end_to_end) { |
| 1371 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1372 | LLDB_LOGF(log, "Target::%s\n" , __FUNCTION__); |
| 1373 | |
| 1374 | if (!end_to_end) { |
| 1375 | m_watchpoint_list.SetEnabledAll(true); |
| 1376 | return true; |
| 1377 | } |
| 1378 | |
| 1379 | // Otherwise, it's an end to end operation. |
| 1380 | |
| 1381 | if (!ProcessIsValid()) |
| 1382 | return false; |
| 1383 | |
| 1384 | for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) { |
| 1385 | if (!wp_sp) |
| 1386 | return false; |
| 1387 | |
| 1388 | Status rc = m_process_sp->EnableWatchpoint(wp_sp); |
| 1389 | if (rc.Fail()) |
| 1390 | return false; |
| 1391 | } |
| 1392 | return true; // Success! |
| 1393 | } |
| 1394 | |
| 1395 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list. |
| 1396 | bool Target::ClearAllWatchpointHitCounts() { |
| 1397 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1398 | LLDB_LOGF(log, "Target::%s\n" , __FUNCTION__); |
| 1399 | |
| 1400 | for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) { |
| 1401 | if (!wp_sp) |
| 1402 | return false; |
| 1403 | |
| 1404 | wp_sp->ResetHitCount(); |
| 1405 | } |
| 1406 | return true; // Success! |
| 1407 | } |
| 1408 | |
| 1409 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list. |
| 1410 | bool Target::ClearAllWatchpointHistoricValues() { |
| 1411 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1412 | LLDB_LOGF(log, "Target::%s\n" , __FUNCTION__); |
| 1413 | |
| 1414 | for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) { |
| 1415 | if (!wp_sp) |
| 1416 | return false; |
| 1417 | |
| 1418 | wp_sp->ResetHistoricValues(); |
| 1419 | } |
| 1420 | return true; // Success! |
| 1421 | } |
| 1422 | |
| 1423 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list during |
| 1424 | // these operations. |
| 1425 | bool Target::IgnoreAllWatchpoints(uint32_t ignore_count) { |
| 1426 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1427 | LLDB_LOGF(log, "Target::%s\n" , __FUNCTION__); |
| 1428 | |
| 1429 | if (!ProcessIsValid()) |
| 1430 | return false; |
| 1431 | |
| 1432 | for (WatchpointSP wp_sp : m_watchpoint_list.Watchpoints()) { |
| 1433 | if (!wp_sp) |
| 1434 | return false; |
| 1435 | |
| 1436 | wp_sp->SetIgnoreCount(ignore_count); |
| 1437 | } |
| 1438 | return true; // Success! |
| 1439 | } |
| 1440 | |
| 1441 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list. |
| 1442 | bool Target::DisableWatchpointByID(lldb::watch_id_t watch_id) { |
| 1443 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1444 | LLDB_LOGF(log, "Target::%s (watch_id = %i)\n" , __FUNCTION__, watch_id); |
| 1445 | |
| 1446 | if (!ProcessIsValid()) |
| 1447 | return false; |
| 1448 | |
| 1449 | WatchpointSP wp_sp = m_watchpoint_list.FindByID(watchID: watch_id); |
| 1450 | if (wp_sp) { |
| 1451 | Status rc = m_process_sp->DisableWatchpoint(wp_sp); |
| 1452 | if (rc.Success()) |
| 1453 | return true; |
| 1454 | |
| 1455 | // Else, fallthrough. |
| 1456 | } |
| 1457 | return false; |
| 1458 | } |
| 1459 | |
| 1460 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list. |
| 1461 | bool Target::EnableWatchpointByID(lldb::watch_id_t watch_id) { |
| 1462 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1463 | LLDB_LOGF(log, "Target::%s (watch_id = %i)\n" , __FUNCTION__, watch_id); |
| 1464 | |
| 1465 | if (!ProcessIsValid()) |
| 1466 | return false; |
| 1467 | |
| 1468 | WatchpointSP wp_sp = m_watchpoint_list.FindByID(watchID: watch_id); |
| 1469 | if (wp_sp) { |
| 1470 | Status rc = m_process_sp->EnableWatchpoint(wp_sp); |
| 1471 | if (rc.Success()) |
| 1472 | return true; |
| 1473 | |
| 1474 | // Else, fallthrough. |
| 1475 | } |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
| 1479 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list. |
| 1480 | bool Target::RemoveWatchpointByID(lldb::watch_id_t watch_id) { |
| 1481 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1482 | LLDB_LOGF(log, "Target::%s (watch_id = %i)\n" , __FUNCTION__, watch_id); |
| 1483 | |
| 1484 | WatchpointSP watch_to_remove_sp = m_watchpoint_list.FindByID(watchID: watch_id); |
| 1485 | if (watch_to_remove_sp == m_last_created_watchpoint) |
| 1486 | m_last_created_watchpoint.reset(); |
| 1487 | |
| 1488 | if (DisableWatchpointByID(watch_id)) { |
| 1489 | m_watchpoint_list.Remove(watchID: watch_id, notify: true); |
| 1490 | return true; |
| 1491 | } |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | // Assumption: Caller holds the list mutex lock for m_watchpoint_list. |
| 1496 | bool Target::IgnoreWatchpointByID(lldb::watch_id_t watch_id, |
| 1497 | uint32_t ignore_count) { |
| 1498 | Log *log = GetLog(mask: LLDBLog::Watchpoints); |
| 1499 | LLDB_LOGF(log, "Target::%s (watch_id = %i)\n" , __FUNCTION__, watch_id); |
| 1500 | |
| 1501 | if (!ProcessIsValid()) |
| 1502 | return false; |
| 1503 | |
| 1504 | WatchpointSP wp_sp = m_watchpoint_list.FindByID(watchID: watch_id); |
| 1505 | if (wp_sp) { |
| 1506 | wp_sp->SetIgnoreCount(ignore_count); |
| 1507 | return true; |
| 1508 | } |
| 1509 | return false; |
| 1510 | } |
| 1511 | |
| 1512 | ModuleSP Target::GetExecutableModule() { |
| 1513 | // search for the first executable in the module list |
| 1514 | for (ModuleSP module_sp : m_images.Modules()) { |
| 1515 | lldb_private::ObjectFile *obj = module_sp->GetObjectFile(); |
| 1516 | if (obj == nullptr) |
| 1517 | continue; |
| 1518 | if (obj->GetType() == ObjectFile::Type::eTypeExecutable) |
| 1519 | return module_sp; |
| 1520 | } |
| 1521 | // as fall back return the first module loaded |
| 1522 | return m_images.GetModuleAtIndex(idx: 0); |
| 1523 | } |
| 1524 | |
| 1525 | Module *Target::GetExecutableModulePointer() { |
| 1526 | return GetExecutableModule().get(); |
| 1527 | } |
| 1528 | |
| 1529 | static void LoadScriptingResourceForModule(const ModuleSP &module_sp, |
| 1530 | Target *target) { |
| 1531 | Status error; |
| 1532 | StreamString feedback_stream; |
| 1533 | if (module_sp && !module_sp->LoadScriptingResourceInTarget(target, error, |
| 1534 | feedback_stream)) { |
| 1535 | if (error.AsCString()) |
| 1536 | target->GetDebugger().GetAsyncErrorStream()->Printf( |
| 1537 | format: "unable to load scripting data for module %s - error reported was " |
| 1538 | "%s\n" , |
| 1539 | module_sp->GetFileSpec().GetFileNameStrippingExtension().GetCString(), |
| 1540 | error.AsCString()); |
| 1541 | } |
| 1542 | if (feedback_stream.GetSize()) |
| 1543 | target->GetDebugger().GetAsyncErrorStream()->Printf( |
| 1544 | format: "%s\n" , feedback_stream.GetData()); |
| 1545 | } |
| 1546 | |
| 1547 | void Target::ClearModules(bool delete_locations) { |
| 1548 | ModulesDidUnload(module_list&: m_images, delete_locations); |
| 1549 | m_section_load_history.Clear(); |
| 1550 | m_images.Clear(); |
| 1551 | m_scratch_type_system_map.Clear(); |
| 1552 | } |
| 1553 | |
| 1554 | void Target::DidExec() { |
| 1555 | // When a process exec's we need to know about it so we can do some cleanup. |
| 1556 | m_breakpoint_list.RemoveInvalidLocations(arch: m_arch.GetSpec()); |
| 1557 | m_internal_breakpoint_list.RemoveInvalidLocations(arch: m_arch.GetSpec()); |
| 1558 | } |
| 1559 | |
| 1560 | void Target::SetExecutableModule(ModuleSP &executable_sp, |
| 1561 | LoadDependentFiles load_dependent_files) { |
| 1562 | telemetry::ScopedDispatcher<telemetry::ExecutableModuleInfo> helper( |
| 1563 | &m_debugger); |
| 1564 | Log *log = GetLog(mask: LLDBLog::Target); |
| 1565 | ClearModules(delete_locations: false); |
| 1566 | |
| 1567 | if (executable_sp) { |
| 1568 | lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; |
| 1569 | if (ProcessSP proc = GetProcessSP()) |
| 1570 | pid = proc->GetID(); |
| 1571 | |
| 1572 | helper.DispatchNow(populate_fields_cb: [&](telemetry::ExecutableModuleInfo *info) { |
| 1573 | info->exec_mod = executable_sp; |
| 1574 | info->uuid = executable_sp->GetUUID(); |
| 1575 | info->pid = pid; |
| 1576 | info->triple = executable_sp->GetArchitecture().GetTriple().getTriple(); |
| 1577 | info->is_start_entry = true; |
| 1578 | }); |
| 1579 | |
| 1580 | helper.DispatchOnExit(final_callback: [&, pid](telemetry::ExecutableModuleInfo *info) { |
| 1581 | info->exec_mod = executable_sp; |
| 1582 | info->uuid = executable_sp->GetUUID(); |
| 1583 | info->pid = pid; |
| 1584 | }); |
| 1585 | |
| 1586 | ElapsedTime elapsed(m_stats.GetCreateTime()); |
| 1587 | LLDB_SCOPED_TIMERF("Target::SetExecutableModule (executable = '%s')" , |
| 1588 | executable_sp->GetFileSpec().GetPath().c_str()); |
| 1589 | |
| 1590 | const bool notify = true; |
| 1591 | m_images.Append(module_sp: executable_sp, |
| 1592 | notify); // The first image is our executable file |
| 1593 | |
| 1594 | // If we haven't set an architecture yet, reset our architecture based on |
| 1595 | // what we found in the executable module. |
| 1596 | if (!m_arch.GetSpec().IsValid()) { |
| 1597 | m_arch = executable_sp->GetArchitecture(); |
| 1598 | LLDB_LOG(log, |
| 1599 | "Target::SetExecutableModule setting architecture to {0} ({1}) " |
| 1600 | "based on executable file" , |
| 1601 | m_arch.GetSpec().GetArchitectureName(), |
| 1602 | m_arch.GetSpec().GetTriple().getTriple()); |
| 1603 | } |
| 1604 | |
| 1605 | ObjectFile *executable_objfile = executable_sp->GetObjectFile(); |
| 1606 | bool load_dependents = true; |
| 1607 | switch (load_dependent_files) { |
| 1608 | case eLoadDependentsDefault: |
| 1609 | load_dependents = executable_sp->IsExecutable(); |
| 1610 | break; |
| 1611 | case eLoadDependentsYes: |
| 1612 | load_dependents = true; |
| 1613 | break; |
| 1614 | case eLoadDependentsNo: |
| 1615 | load_dependents = false; |
| 1616 | break; |
| 1617 | } |
| 1618 | |
| 1619 | if (executable_objfile && load_dependents) { |
| 1620 | // FileSpecList is not thread safe and needs to be synchronized. |
| 1621 | FileSpecList dependent_files; |
| 1622 | std::mutex dependent_files_mutex; |
| 1623 | |
| 1624 | // ModuleList is thread safe. |
| 1625 | ModuleList added_modules; |
| 1626 | |
| 1627 | auto GetDependentModules = [&](FileSpec dependent_file_spec) { |
| 1628 | FileSpec platform_dependent_file_spec; |
| 1629 | if (m_platform_sp) |
| 1630 | m_platform_sp->GetFileWithUUID(platform_file: dependent_file_spec, uuid_ptr: nullptr, |
| 1631 | local_file&: platform_dependent_file_spec); |
| 1632 | else |
| 1633 | platform_dependent_file_spec = dependent_file_spec; |
| 1634 | |
| 1635 | ModuleSpec module_spec(platform_dependent_file_spec, m_arch.GetSpec()); |
| 1636 | ModuleSP image_module_sp( |
| 1637 | GetOrCreateModule(module_spec, notify: false /* notify */)); |
| 1638 | if (image_module_sp) { |
| 1639 | added_modules.AppendIfNeeded(new_module: image_module_sp, notify: false); |
| 1640 | ObjectFile *objfile = image_module_sp->GetObjectFile(); |
| 1641 | if (objfile) { |
| 1642 | // Create a local copy of the dependent file list so we don't have |
| 1643 | // to lock for the whole duration of GetDependentModules. |
| 1644 | FileSpecList dependent_files_copy; |
| 1645 | { |
| 1646 | std::lock_guard<std::mutex> guard(dependent_files_mutex); |
| 1647 | dependent_files_copy = dependent_files; |
| 1648 | } |
| 1649 | |
| 1650 | // Remember the size of the local copy so we can append only the |
| 1651 | // modules that have been added by GetDependentModules. |
| 1652 | const size_t previous_dependent_files = |
| 1653 | dependent_files_copy.GetSize(); |
| 1654 | |
| 1655 | objfile->GetDependentModules(file_list&: dependent_files_copy); |
| 1656 | |
| 1657 | { |
| 1658 | std::lock_guard<std::mutex> guard(dependent_files_mutex); |
| 1659 | for (size_t i = previous_dependent_files; |
| 1660 | i < dependent_files_copy.GetSize(); ++i) |
| 1661 | dependent_files.AppendIfUnique( |
| 1662 | file: dependent_files_copy.GetFileSpecAtIndex(idx: i)); |
| 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | }; |
| 1667 | |
| 1668 | executable_objfile->GetDependentModules(file_list&: dependent_files); |
| 1669 | |
| 1670 | llvm::ThreadPoolTaskGroup task_group(Debugger::GetThreadPool()); |
| 1671 | for (uint32_t i = 0; i < dependent_files.GetSize(); i++) { |
| 1672 | // Process all currently known dependencies in parallel in the innermost |
| 1673 | // loop. This may create newly discovered dependencies to be appended to |
| 1674 | // dependent_files. We'll deal with these files during the next |
| 1675 | // iteration of the outermost loop. |
| 1676 | { |
| 1677 | std::lock_guard<std::mutex> guard(dependent_files_mutex); |
| 1678 | for (; i < dependent_files.GetSize(); i++) |
| 1679 | task_group.async(F&: GetDependentModules, |
| 1680 | ArgList: dependent_files.GetFileSpecAtIndex(idx: i)); |
| 1681 | } |
| 1682 | task_group.wait(); |
| 1683 | } |
| 1684 | ModulesDidLoad(module_list&: added_modules); |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | bool Target::SetArchitecture(const ArchSpec &arch_spec, bool set_platform, |
| 1690 | bool merge) { |
| 1691 | Log *log = GetLog(mask: LLDBLog::Target); |
| 1692 | bool missing_local_arch = !m_arch.GetSpec().IsValid(); |
| 1693 | bool replace_local_arch = true; |
| 1694 | bool compatible_local_arch = false; |
| 1695 | ArchSpec other(arch_spec); |
| 1696 | |
| 1697 | // Changing the architecture might mean that the currently selected platform |
| 1698 | // isn't compatible. Set the platform correctly if we are asked to do so, |
| 1699 | // otherwise assume the user will set the platform manually. |
| 1700 | if (set_platform) { |
| 1701 | if (other.IsValid()) { |
| 1702 | auto platform_sp = GetPlatform(); |
| 1703 | if (!platform_sp || !platform_sp->IsCompatibleArchitecture( |
| 1704 | arch: other, process_host_arch: {}, match: ArchSpec::CompatibleMatch, compatible_arch_ptr: nullptr)) { |
| 1705 | ArchSpec platform_arch; |
| 1706 | if (PlatformSP arch_platform_sp = |
| 1707 | GetDebugger().GetPlatformList().GetOrCreate(arch: other, process_host_arch: {}, |
| 1708 | platform_arch_ptr: &platform_arch)) { |
| 1709 | SetPlatform(arch_platform_sp); |
| 1710 | if (platform_arch.IsValid()) |
| 1711 | other = platform_arch; |
| 1712 | } |
| 1713 | } |
| 1714 | } |
| 1715 | } |
| 1716 | |
| 1717 | if (!missing_local_arch) { |
| 1718 | if (merge && m_arch.GetSpec().IsCompatibleMatch(rhs: arch_spec)) { |
| 1719 | other.MergeFrom(other: m_arch.GetSpec()); |
| 1720 | |
| 1721 | if (m_arch.GetSpec().IsCompatibleMatch(rhs: other)) { |
| 1722 | compatible_local_arch = true; |
| 1723 | |
| 1724 | if (m_arch.GetSpec().GetTriple() == other.GetTriple()) |
| 1725 | replace_local_arch = false; |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | |
| 1730 | if (compatible_local_arch || missing_local_arch) { |
| 1731 | // If we haven't got a valid arch spec, or the architectures are compatible |
| 1732 | // update the architecture, unless the one we already have is more |
| 1733 | // specified |
| 1734 | if (replace_local_arch) |
| 1735 | m_arch = other; |
| 1736 | LLDB_LOG(log, |
| 1737 | "Target::SetArchitecture merging compatible arch; arch " |
| 1738 | "is now {0} ({1})" , |
| 1739 | m_arch.GetSpec().GetArchitectureName(), |
| 1740 | m_arch.GetSpec().GetTriple().getTriple()); |
| 1741 | return true; |
| 1742 | } |
| 1743 | |
| 1744 | // If we have an executable file, try to reset the executable to the desired |
| 1745 | // architecture |
| 1746 | LLDB_LOGF( |
| 1747 | log, |
| 1748 | "Target::SetArchitecture changing architecture to %s (%s) from %s (%s)" , |
| 1749 | arch_spec.GetArchitectureName(), |
| 1750 | arch_spec.GetTriple().getTriple().c_str(), |
| 1751 | m_arch.GetSpec().GetArchitectureName(), |
| 1752 | m_arch.GetSpec().GetTriple().getTriple().c_str()); |
| 1753 | m_arch = other; |
| 1754 | ModuleSP executable_sp = GetExecutableModule(); |
| 1755 | |
| 1756 | ClearModules(delete_locations: true); |
| 1757 | // Need to do something about unsetting breakpoints. |
| 1758 | |
| 1759 | if (executable_sp) { |
| 1760 | LLDB_LOGF(log, |
| 1761 | "Target::SetArchitecture Trying to select executable file " |
| 1762 | "architecture %s (%s)" , |
| 1763 | arch_spec.GetArchitectureName(), |
| 1764 | arch_spec.GetTriple().getTriple().c_str()); |
| 1765 | ModuleSpec module_spec(executable_sp->GetFileSpec(), other); |
| 1766 | FileSpecList search_paths = GetExecutableSearchPaths(); |
| 1767 | Status error = ModuleList::GetSharedModule(module_spec, module_sp&: executable_sp, |
| 1768 | module_search_paths_ptr: &search_paths, old_modules: nullptr, did_create_ptr: nullptr); |
| 1769 | |
| 1770 | if (!error.Fail() && executable_sp) { |
| 1771 | SetExecutableModule(executable_sp, load_dependent_files: eLoadDependentsYes); |
| 1772 | return true; |
| 1773 | } |
| 1774 | } |
| 1775 | return false; |
| 1776 | } |
| 1777 | |
| 1778 | bool Target::MergeArchitecture(const ArchSpec &arch_spec) { |
| 1779 | Log *log = GetLog(mask: LLDBLog::Target); |
| 1780 | if (arch_spec.IsValid()) { |
| 1781 | if (m_arch.GetSpec().IsCompatibleMatch(rhs: arch_spec)) { |
| 1782 | // The current target arch is compatible with "arch_spec", see if we can |
| 1783 | // improve our current architecture using bits from "arch_spec" |
| 1784 | |
| 1785 | LLDB_LOGF(log, |
| 1786 | "Target::MergeArchitecture target has arch %s, merging with " |
| 1787 | "arch %s" , |
| 1788 | m_arch.GetSpec().GetTriple().getTriple().c_str(), |
| 1789 | arch_spec.GetTriple().getTriple().c_str()); |
| 1790 | |
| 1791 | // Merge bits from arch_spec into "merged_arch" and set our architecture |
| 1792 | ArchSpec merged_arch(m_arch.GetSpec()); |
| 1793 | merged_arch.MergeFrom(other: arch_spec); |
| 1794 | return SetArchitecture(arch_spec: merged_arch); |
| 1795 | } else { |
| 1796 | // The new architecture is different, we just need to replace it |
| 1797 | return SetArchitecture(arch_spec); |
| 1798 | } |
| 1799 | } |
| 1800 | return false; |
| 1801 | } |
| 1802 | |
| 1803 | void Target::NotifyWillClearList(const ModuleList &module_list) {} |
| 1804 | |
| 1805 | void Target::NotifyModuleAdded(const ModuleList &module_list, |
| 1806 | const ModuleSP &module_sp) { |
| 1807 | // A module is being added to this target for the first time |
| 1808 | if (m_valid) { |
| 1809 | ModuleList my_module_list; |
| 1810 | my_module_list.Append(module_sp); |
| 1811 | ModulesDidLoad(module_list&: my_module_list); |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | void Target::NotifyModuleRemoved(const ModuleList &module_list, |
| 1816 | const ModuleSP &module_sp) { |
| 1817 | // A module is being removed from this target. |
| 1818 | if (m_valid) { |
| 1819 | ModuleList my_module_list; |
| 1820 | my_module_list.Append(module_sp); |
| 1821 | ModulesDidUnload(module_list&: my_module_list, delete_locations: false); |
| 1822 | } |
| 1823 | } |
| 1824 | |
| 1825 | void Target::NotifyModuleUpdated(const ModuleList &module_list, |
| 1826 | const ModuleSP &old_module_sp, |
| 1827 | const ModuleSP &new_module_sp) { |
| 1828 | // A module is replacing an already added module |
| 1829 | if (m_valid) { |
| 1830 | m_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced(old_module_sp, |
| 1831 | new_module_sp); |
| 1832 | m_internal_breakpoint_list.UpdateBreakpointsWhenModuleIsReplaced( |
| 1833 | old_module_sp, new_module_sp); |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | void Target::NotifyModulesRemoved(lldb_private::ModuleList &module_list) { |
| 1838 | ModulesDidUnload(module_list, delete_locations: false); |
| 1839 | } |
| 1840 | |
| 1841 | void Target::ModulesDidLoad(ModuleList &module_list) { |
| 1842 | const size_t num_images = module_list.GetSize(); |
| 1843 | if (m_valid && num_images) { |
| 1844 | for (size_t idx = 0; idx < num_images; ++idx) { |
| 1845 | ModuleSP module_sp(module_list.GetModuleAtIndex(idx)); |
| 1846 | LoadScriptingResourceForModule(module_sp, target: this); |
| 1847 | LoadTypeSummariesForModule(module_sp); |
| 1848 | LoadFormattersForModule(module_sp); |
| 1849 | } |
| 1850 | m_breakpoint_list.UpdateBreakpoints(module_list, load: true, delete_locations: false); |
| 1851 | m_internal_breakpoint_list.UpdateBreakpoints(module_list, load: true, delete_locations: false); |
| 1852 | if (m_process_sp) { |
| 1853 | m_process_sp->ModulesDidLoad(module_list); |
| 1854 | } |
| 1855 | auto data_sp = |
| 1856 | std::make_shared<TargetEventData>(args: shared_from_this(), args&: module_list); |
| 1857 | BroadcastEvent(event_type: eBroadcastBitModulesLoaded, event_data_sp: data_sp); |
| 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | void Target::SymbolsDidLoad(ModuleList &module_list) { |
| 1862 | if (m_valid && module_list.GetSize()) { |
| 1863 | if (m_process_sp) { |
| 1864 | for (LanguageRuntime *runtime : m_process_sp->GetLanguageRuntimes()) { |
| 1865 | runtime->SymbolsDidLoad(module_list); |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | m_breakpoint_list.UpdateBreakpoints(module_list, load: true, delete_locations: false); |
| 1870 | m_internal_breakpoint_list.UpdateBreakpoints(module_list, load: true, delete_locations: false); |
| 1871 | auto data_sp = |
| 1872 | std::make_shared<TargetEventData>(args: shared_from_this(), args&: module_list); |
| 1873 | BroadcastEvent(event_type: eBroadcastBitSymbolsLoaded, event_data_sp: data_sp); |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | void Target::ModulesDidUnload(ModuleList &module_list, bool delete_locations) { |
| 1878 | if (m_valid && module_list.GetSize()) { |
| 1879 | UnloadModuleSections(module_list); |
| 1880 | auto data_sp = |
| 1881 | std::make_shared<TargetEventData>(args: shared_from_this(), args&: module_list); |
| 1882 | BroadcastEvent(event_type: eBroadcastBitModulesUnloaded, event_data_sp: data_sp); |
| 1883 | m_breakpoint_list.UpdateBreakpoints(module_list, load: false, delete_locations); |
| 1884 | m_internal_breakpoint_list.UpdateBreakpoints(module_list, load: false, |
| 1885 | delete_locations); |
| 1886 | |
| 1887 | // If a module was torn down it will have torn down the 'TypeSystemClang's |
| 1888 | // that we used as source 'ASTContext's for the persistent variables in |
| 1889 | // the current target. Those would now be unsafe to access because the |
| 1890 | // 'DeclOrigin' are now possibly stale. Thus clear all persistent |
| 1891 | // variables. We only want to flush 'TypeSystem's if the module being |
| 1892 | // unloaded was capable of describing a source type. JITted module unloads |
| 1893 | // happen frequently for Objective-C utility functions or the REPL and rely |
| 1894 | // on the persistent variables to stick around. |
| 1895 | const bool should_flush_type_systems = |
| 1896 | module_list.AnyOf(callback: [](lldb_private::Module &module) { |
| 1897 | auto *object_file = module.GetObjectFile(); |
| 1898 | |
| 1899 | if (!object_file) |
| 1900 | return false; |
| 1901 | |
| 1902 | auto type = object_file->GetType(); |
| 1903 | |
| 1904 | // eTypeExecutable: when debugged binary was rebuilt |
| 1905 | // eTypeSharedLibrary: if dylib was re-loaded |
| 1906 | return module.FileHasChanged() && |
| 1907 | (type == ObjectFile::eTypeObjectFile || |
| 1908 | type == ObjectFile::eTypeExecutable || |
| 1909 | type == ObjectFile::eTypeSharedLibrary); |
| 1910 | }); |
| 1911 | |
| 1912 | if (should_flush_type_systems) |
| 1913 | m_scratch_type_system_map.Clear(); |
| 1914 | } |
| 1915 | } |
| 1916 | |
| 1917 | bool Target::ModuleIsExcludedForUnconstrainedSearches( |
| 1918 | const FileSpec &module_file_spec) { |
| 1919 | if (GetBreakpointsConsultPlatformAvoidList()) { |
| 1920 | ModuleList matchingModules; |
| 1921 | ModuleSpec module_spec(module_file_spec); |
| 1922 | GetImages().FindModules(module_spec, matching_module_list&: matchingModules); |
| 1923 | size_t num_modules = matchingModules.GetSize(); |
| 1924 | |
| 1925 | // If there is more than one module for this file spec, only |
| 1926 | // return true if ALL the modules are on the black list. |
| 1927 | if (num_modules > 0) { |
| 1928 | for (size_t i = 0; i < num_modules; i++) { |
| 1929 | if (!ModuleIsExcludedForUnconstrainedSearches( |
| 1930 | module_sp: matchingModules.GetModuleAtIndex(idx: i))) |
| 1931 | return false; |
| 1932 | } |
| 1933 | return true; |
| 1934 | } |
| 1935 | } |
| 1936 | return false; |
| 1937 | } |
| 1938 | |
| 1939 | bool Target::ModuleIsExcludedForUnconstrainedSearches( |
| 1940 | const lldb::ModuleSP &module_sp) { |
| 1941 | if (GetBreakpointsConsultPlatformAvoidList()) { |
| 1942 | if (m_platform_sp) |
| 1943 | return m_platform_sp->ModuleIsExcludedForUnconstrainedSearches(target&: *this, |
| 1944 | module_sp); |
| 1945 | } |
| 1946 | return false; |
| 1947 | } |
| 1948 | |
| 1949 | size_t Target::ReadMemoryFromFileCache(const Address &addr, void *dst, |
| 1950 | size_t dst_len, Status &error) { |
| 1951 | SectionSP section_sp(addr.GetSection()); |
| 1952 | if (section_sp) { |
| 1953 | // If the contents of this section are encrypted, the on-disk file is |
| 1954 | // unusable. Read only from live memory. |
| 1955 | if (section_sp->IsEncrypted()) { |
| 1956 | error = Status::FromErrorString(str: "section is encrypted" ); |
| 1957 | return 0; |
| 1958 | } |
| 1959 | ModuleSP module_sp(section_sp->GetModule()); |
| 1960 | if (module_sp) { |
| 1961 | ObjectFile *objfile = section_sp->GetModule()->GetObjectFile(); |
| 1962 | if (objfile) { |
| 1963 | size_t bytes_read = objfile->ReadSectionData( |
| 1964 | section: section_sp.get(), section_offset: addr.GetOffset(), dst, dst_len); |
| 1965 | if (bytes_read > 0) |
| 1966 | return bytes_read; |
| 1967 | else |
| 1968 | error = Status::FromErrorStringWithFormat( |
| 1969 | format: "error reading data from section %s" , |
| 1970 | section_sp->GetName().GetCString()); |
| 1971 | } else |
| 1972 | error = Status::FromErrorString(str: "address isn't from a object file" ); |
| 1973 | } else |
| 1974 | error = Status::FromErrorString(str: "address isn't in a module" ); |
| 1975 | } else |
| 1976 | error = Status::FromErrorString( |
| 1977 | str: "address doesn't contain a section that points to a " |
| 1978 | "section in a object file" ); |
| 1979 | |
| 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | size_t Target::ReadMemory(const Address &addr, void *dst, size_t dst_len, |
| 1984 | Status &error, bool force_live_memory, |
| 1985 | lldb::addr_t *load_addr_ptr) { |
| 1986 | error.Clear(); |
| 1987 | |
| 1988 | Address fixed_addr = addr; |
| 1989 | if (ProcessIsValid()) |
| 1990 | if (const ABISP &abi = m_process_sp->GetABI()) |
| 1991 | fixed_addr.SetLoadAddress(load_addr: abi->FixAnyAddress(pc: addr.GetLoadAddress(target: this)), |
| 1992 | target: this); |
| 1993 | |
| 1994 | // if we end up reading this from process memory, we will fill this with the |
| 1995 | // actual load address |
| 1996 | if (load_addr_ptr) |
| 1997 | *load_addr_ptr = LLDB_INVALID_ADDRESS; |
| 1998 | |
| 1999 | size_t bytes_read = 0; |
| 2000 | |
| 2001 | addr_t load_addr = LLDB_INVALID_ADDRESS; |
| 2002 | addr_t file_addr = LLDB_INVALID_ADDRESS; |
| 2003 | Address resolved_addr; |
| 2004 | if (!fixed_addr.IsSectionOffset()) { |
| 2005 | SectionLoadList §ion_load_list = GetSectionLoadList(); |
| 2006 | if (section_load_list.IsEmpty()) { |
| 2007 | // No sections are loaded, so we must assume we are not running yet and |
| 2008 | // anything we are given is a file address. |
| 2009 | file_addr = |
| 2010 | fixed_addr.GetOffset(); // "fixed_addr" doesn't have a section, so |
| 2011 | // its offset is the file address |
| 2012 | m_images.ResolveFileAddress(vm_addr: file_addr, so_addr&: resolved_addr); |
| 2013 | } else { |
| 2014 | // We have at least one section loaded. This can be because we have |
| 2015 | // manually loaded some sections with "target modules load ..." or |
| 2016 | // because we have a live process that has sections loaded through |
| 2017 | // the dynamic loader |
| 2018 | load_addr = |
| 2019 | fixed_addr.GetOffset(); // "fixed_addr" doesn't have a section, so |
| 2020 | // its offset is the load address |
| 2021 | section_load_list.ResolveLoadAddress(load_addr, so_addr&: resolved_addr); |
| 2022 | } |
| 2023 | } |
| 2024 | if (!resolved_addr.IsValid()) |
| 2025 | resolved_addr = fixed_addr; |
| 2026 | |
| 2027 | // If we read from the file cache but can't get as many bytes as requested, |
| 2028 | // we keep the result around in this buffer, in case this result is the |
| 2029 | // best we can do. |
| 2030 | std::unique_ptr<uint8_t[]> file_cache_read_buffer; |
| 2031 | size_t file_cache_bytes_read = 0; |
| 2032 | |
| 2033 | // Read from file cache if read-only section. |
| 2034 | if (!force_live_memory && resolved_addr.IsSectionOffset()) { |
| 2035 | SectionSP section_sp(resolved_addr.GetSection()); |
| 2036 | if (section_sp) { |
| 2037 | auto permissions = Flags(section_sp->GetPermissions()); |
| 2038 | bool is_readonly = !permissions.Test(bit: ePermissionsWritable) && |
| 2039 | permissions.Test(bit: ePermissionsReadable); |
| 2040 | if (is_readonly) { |
| 2041 | file_cache_bytes_read = |
| 2042 | ReadMemoryFromFileCache(addr: resolved_addr, dst, dst_len, error); |
| 2043 | if (file_cache_bytes_read == dst_len) |
| 2044 | return file_cache_bytes_read; |
| 2045 | else if (file_cache_bytes_read > 0) { |
| 2046 | file_cache_read_buffer = |
| 2047 | std::make_unique<uint8_t[]>(num: file_cache_bytes_read); |
| 2048 | std::memcpy(dest: file_cache_read_buffer.get(), src: dst, n: file_cache_bytes_read); |
| 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | if (ProcessIsValid()) { |
| 2055 | if (load_addr == LLDB_INVALID_ADDRESS) |
| 2056 | load_addr = resolved_addr.GetLoadAddress(target: this); |
| 2057 | |
| 2058 | if (load_addr == LLDB_INVALID_ADDRESS) { |
| 2059 | ModuleSP addr_module_sp(resolved_addr.GetModule()); |
| 2060 | if (addr_module_sp && addr_module_sp->GetFileSpec()) |
| 2061 | error = Status::FromErrorStringWithFormatv( |
| 2062 | format: "{0:F}[{1:x+}] can't be resolved, {0:F} is not currently loaded" , |
| 2063 | args: addr_module_sp->GetFileSpec(), args: resolved_addr.GetFileAddress()); |
| 2064 | else |
| 2065 | error = Status::FromErrorStringWithFormat( |
| 2066 | format: "0x%" PRIx64 " can't be resolved" , resolved_addr.GetFileAddress()); |
| 2067 | } else { |
| 2068 | bytes_read = m_process_sp->ReadMemory(vm_addr: load_addr, buf: dst, size: dst_len, error); |
| 2069 | if (bytes_read != dst_len) { |
| 2070 | if (error.Success()) { |
| 2071 | if (bytes_read == 0) |
| 2072 | error = Status::FromErrorStringWithFormat( |
| 2073 | format: "read memory from 0x%" PRIx64 " failed" , load_addr); |
| 2074 | else |
| 2075 | error = Status::FromErrorStringWithFormat( |
| 2076 | format: "only %" PRIu64 " of %" PRIu64 |
| 2077 | " bytes were read from memory at 0x%" PRIx64, |
| 2078 | (uint64_t)bytes_read, (uint64_t)dst_len, load_addr); |
| 2079 | } |
| 2080 | } |
| 2081 | if (bytes_read) { |
| 2082 | if (load_addr_ptr) |
| 2083 | *load_addr_ptr = load_addr; |
| 2084 | return bytes_read; |
| 2085 | } |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | if (file_cache_read_buffer && file_cache_bytes_read > 0) { |
| 2090 | // Reading from the process failed. If we've previously succeeded in reading |
| 2091 | // something from the file cache, then copy that over and return that. |
| 2092 | std::memcpy(dest: dst, src: file_cache_read_buffer.get(), n: file_cache_bytes_read); |
| 2093 | return file_cache_bytes_read; |
| 2094 | } |
| 2095 | |
| 2096 | if (!file_cache_read_buffer && resolved_addr.IsSectionOffset()) { |
| 2097 | // If we didn't already try and read from the object file cache, then try |
| 2098 | // it after failing to read from the process. |
| 2099 | return ReadMemoryFromFileCache(addr: resolved_addr, dst, dst_len, error); |
| 2100 | } |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | size_t Target::ReadCStringFromMemory(const Address &addr, std::string &out_str, |
| 2105 | Status &error, bool force_live_memory) { |
| 2106 | char buf[256]; |
| 2107 | out_str.clear(); |
| 2108 | addr_t curr_addr = addr.GetLoadAddress(target: this); |
| 2109 | Address address(addr); |
| 2110 | while (true) { |
| 2111 | size_t length = ReadCStringFromMemory(addr: address, dst: buf, dst_max_len: sizeof(buf), result_error&: error, |
| 2112 | force_live_memory); |
| 2113 | if (length == 0) |
| 2114 | break; |
| 2115 | out_str.append(s: buf, n: length); |
| 2116 | // If we got "length - 1" bytes, we didn't get the whole C string, we need |
| 2117 | // to read some more characters |
| 2118 | if (length == sizeof(buf) - 1) |
| 2119 | curr_addr += length; |
| 2120 | else |
| 2121 | break; |
| 2122 | address = Address(curr_addr); |
| 2123 | } |
| 2124 | return out_str.size(); |
| 2125 | } |
| 2126 | |
| 2127 | size_t Target::ReadCStringFromMemory(const Address &addr, char *dst, |
| 2128 | size_t dst_max_len, Status &result_error, |
| 2129 | bool force_live_memory) { |
| 2130 | size_t total_cstr_len = 0; |
| 2131 | if (dst && dst_max_len) { |
| 2132 | result_error.Clear(); |
| 2133 | // NULL out everything just to be safe |
| 2134 | memset(s: dst, c: 0, n: dst_max_len); |
| 2135 | addr_t curr_addr = addr.GetLoadAddress(target: this); |
| 2136 | Address address(addr); |
| 2137 | |
| 2138 | // We could call m_process_sp->GetMemoryCacheLineSize() but I don't think |
| 2139 | // this really needs to be tied to the memory cache subsystem's cache line |
| 2140 | // size, so leave this as a fixed constant. |
| 2141 | const size_t cache_line_size = 512; |
| 2142 | |
| 2143 | size_t bytes_left = dst_max_len - 1; |
| 2144 | char *curr_dst = dst; |
| 2145 | |
| 2146 | while (bytes_left > 0) { |
| 2147 | addr_t cache_line_bytes_left = |
| 2148 | cache_line_size - (curr_addr % cache_line_size); |
| 2149 | addr_t bytes_to_read = |
| 2150 | std::min<addr_t>(a: bytes_left, b: cache_line_bytes_left); |
| 2151 | Status error; |
| 2152 | size_t bytes_read = ReadMemory(addr: address, dst: curr_dst, dst_len: bytes_to_read, error, |
| 2153 | force_live_memory); |
| 2154 | |
| 2155 | if (bytes_read == 0) { |
| 2156 | result_error = std::move(error); |
| 2157 | dst[total_cstr_len] = '\0'; |
| 2158 | break; |
| 2159 | } |
| 2160 | const size_t len = strlen(s: curr_dst); |
| 2161 | |
| 2162 | total_cstr_len += len; |
| 2163 | |
| 2164 | if (len < bytes_to_read) |
| 2165 | break; |
| 2166 | |
| 2167 | curr_dst += bytes_read; |
| 2168 | curr_addr += bytes_read; |
| 2169 | bytes_left -= bytes_read; |
| 2170 | address = Address(curr_addr); |
| 2171 | } |
| 2172 | } else { |
| 2173 | if (dst == nullptr) |
| 2174 | result_error = Status::FromErrorString(str: "invalid arguments" ); |
| 2175 | else |
| 2176 | result_error.Clear(); |
| 2177 | } |
| 2178 | return total_cstr_len; |
| 2179 | } |
| 2180 | |
| 2181 | addr_t Target::GetReasonableReadSize(const Address &addr) { |
| 2182 | addr_t load_addr = addr.GetLoadAddress(target: this); |
| 2183 | if (load_addr != LLDB_INVALID_ADDRESS && m_process_sp) { |
| 2184 | // Avoid crossing cache line boundaries. |
| 2185 | addr_t cache_line_size = m_process_sp->GetMemoryCacheLineSize(); |
| 2186 | return cache_line_size - (load_addr % cache_line_size); |
| 2187 | } |
| 2188 | |
| 2189 | // The read is going to go to the file cache, so we can just pick a largish |
| 2190 | // value. |
| 2191 | return 0x1000; |
| 2192 | } |
| 2193 | |
| 2194 | size_t Target::ReadStringFromMemory(const Address &addr, char *dst, |
| 2195 | size_t max_bytes, Status &error, |
| 2196 | size_t type_width, bool force_live_memory) { |
| 2197 | if (!dst || !max_bytes || !type_width || max_bytes < type_width) |
| 2198 | return 0; |
| 2199 | |
| 2200 | size_t total_bytes_read = 0; |
| 2201 | |
| 2202 | // Ensure a null terminator independent of the number of bytes that is |
| 2203 | // read. |
| 2204 | memset(s: dst, c: 0, n: max_bytes); |
| 2205 | size_t bytes_left = max_bytes - type_width; |
| 2206 | |
| 2207 | const char terminator[4] = {'\0', '\0', '\0', '\0'}; |
| 2208 | assert(sizeof(terminator) >= type_width && "Attempting to validate a " |
| 2209 | "string with more than 4 bytes " |
| 2210 | "per character!" ); |
| 2211 | |
| 2212 | Address address = addr; |
| 2213 | char *curr_dst = dst; |
| 2214 | |
| 2215 | error.Clear(); |
| 2216 | while (bytes_left > 0 && error.Success()) { |
| 2217 | addr_t bytes_to_read = |
| 2218 | std::min<addr_t>(a: bytes_left, b: GetReasonableReadSize(addr: address)); |
| 2219 | size_t bytes_read = |
| 2220 | ReadMemory(addr: address, dst: curr_dst, dst_len: bytes_to_read, error, force_live_memory); |
| 2221 | |
| 2222 | if (bytes_read == 0) |
| 2223 | break; |
| 2224 | |
| 2225 | // Search for a null terminator of correct size and alignment in |
| 2226 | // bytes_read |
| 2227 | size_t aligned_start = total_bytes_read - total_bytes_read % type_width; |
| 2228 | for (size_t i = aligned_start; |
| 2229 | i + type_width <= total_bytes_read + bytes_read; i += type_width) |
| 2230 | if (::memcmp(s1: &dst[i], s2: terminator, n: type_width) == 0) { |
| 2231 | error.Clear(); |
| 2232 | return i; |
| 2233 | } |
| 2234 | |
| 2235 | total_bytes_read += bytes_read; |
| 2236 | curr_dst += bytes_read; |
| 2237 | address.Slide(offset: bytes_read); |
| 2238 | bytes_left -= bytes_read; |
| 2239 | } |
| 2240 | return total_bytes_read; |
| 2241 | } |
| 2242 | |
| 2243 | size_t Target::ReadScalarIntegerFromMemory(const Address &addr, uint32_t byte_size, |
| 2244 | bool is_signed, Scalar &scalar, |
| 2245 | Status &error, |
| 2246 | bool force_live_memory) { |
| 2247 | uint64_t uval; |
| 2248 | |
| 2249 | if (byte_size <= sizeof(uval)) { |
| 2250 | size_t bytes_read = |
| 2251 | ReadMemory(addr, dst: &uval, dst_len: byte_size, error, force_live_memory); |
| 2252 | if (bytes_read == byte_size) { |
| 2253 | DataExtractor data(&uval, sizeof(uval), m_arch.GetSpec().GetByteOrder(), |
| 2254 | m_arch.GetSpec().GetAddressByteSize()); |
| 2255 | lldb::offset_t offset = 0; |
| 2256 | if (byte_size <= 4) |
| 2257 | scalar = data.GetMaxU32(offset_ptr: &offset, byte_size); |
| 2258 | else |
| 2259 | scalar = data.GetMaxU64(offset_ptr: &offset, byte_size); |
| 2260 | |
| 2261 | if (is_signed) |
| 2262 | scalar.SignExtend(bit_pos: byte_size * 8); |
| 2263 | return bytes_read; |
| 2264 | } |
| 2265 | } else { |
| 2266 | error = Status::FromErrorStringWithFormat( |
| 2267 | format: "byte size of %u is too large for integer scalar type" , byte_size); |
| 2268 | } |
| 2269 | return 0; |
| 2270 | } |
| 2271 | |
| 2272 | int64_t Target::ReadSignedIntegerFromMemory(const Address &addr, |
| 2273 | size_t integer_byte_size, |
| 2274 | int64_t fail_value, Status &error, |
| 2275 | bool force_live_memory) { |
| 2276 | Scalar scalar; |
| 2277 | if (ReadScalarIntegerFromMemory(addr, byte_size: integer_byte_size, is_signed: false, scalar, error, |
| 2278 | force_live_memory)) |
| 2279 | return scalar.SLongLong(fail_value); |
| 2280 | return fail_value; |
| 2281 | } |
| 2282 | |
| 2283 | uint64_t Target::ReadUnsignedIntegerFromMemory(const Address &addr, |
| 2284 | size_t integer_byte_size, |
| 2285 | uint64_t fail_value, Status &error, |
| 2286 | bool force_live_memory) { |
| 2287 | Scalar scalar; |
| 2288 | if (ReadScalarIntegerFromMemory(addr, byte_size: integer_byte_size, is_signed: false, scalar, error, |
| 2289 | force_live_memory)) |
| 2290 | return scalar.ULongLong(fail_value); |
| 2291 | return fail_value; |
| 2292 | } |
| 2293 | |
| 2294 | bool Target::ReadPointerFromMemory(const Address &addr, Status &error, |
| 2295 | Address &pointer_addr, |
| 2296 | bool force_live_memory) { |
| 2297 | Scalar scalar; |
| 2298 | if (ReadScalarIntegerFromMemory(addr, byte_size: m_arch.GetSpec().GetAddressByteSize(), |
| 2299 | is_signed: false, scalar, error, force_live_memory)) { |
| 2300 | addr_t pointer_vm_addr = scalar.ULongLong(LLDB_INVALID_ADDRESS); |
| 2301 | if (pointer_vm_addr != LLDB_INVALID_ADDRESS) { |
| 2302 | SectionLoadList §ion_load_list = GetSectionLoadList(); |
| 2303 | if (section_load_list.IsEmpty()) { |
| 2304 | // No sections are loaded, so we must assume we are not running yet and |
| 2305 | // anything we are given is a file address. |
| 2306 | m_images.ResolveFileAddress(vm_addr: pointer_vm_addr, so_addr&: pointer_addr); |
| 2307 | } else { |
| 2308 | // We have at least one section loaded. This can be because we have |
| 2309 | // manually loaded some sections with "target modules load ..." or |
| 2310 | // because we have a live process that has sections loaded through |
| 2311 | // the dynamic loader |
| 2312 | section_load_list.ResolveLoadAddress(load_addr: pointer_vm_addr, so_addr&: pointer_addr); |
| 2313 | } |
| 2314 | // We weren't able to resolve the pointer value, so just return an |
| 2315 | // address with no section |
| 2316 | if (!pointer_addr.IsValid()) |
| 2317 | pointer_addr.SetOffset(pointer_vm_addr); |
| 2318 | return true; |
| 2319 | } |
| 2320 | } |
| 2321 | return false; |
| 2322 | } |
| 2323 | |
| 2324 | ModuleSP Target::GetOrCreateModule(const ModuleSpec &orig_module_spec, |
| 2325 | bool notify, Status *error_ptr) { |
| 2326 | ModuleSP module_sp; |
| 2327 | |
| 2328 | Status error; |
| 2329 | |
| 2330 | // Apply any remappings specified in target.object-map: |
| 2331 | ModuleSpec module_spec(orig_module_spec); |
| 2332 | PathMappingList &obj_mapping = GetObjectPathMap(); |
| 2333 | if (std::optional<FileSpec> remapped_obj_file = |
| 2334 | obj_mapping.RemapPath(path: orig_module_spec.GetFileSpec().GetPath(), |
| 2335 | only_if_exists: true /* only_if_exists */)) { |
| 2336 | module_spec.GetFileSpec().SetPath(remapped_obj_file->GetPath()); |
| 2337 | } |
| 2338 | |
| 2339 | // First see if we already have this module in our module list. If we do, |
| 2340 | // then we're done, we don't need to consult the shared modules list. But |
| 2341 | // only do this if we are passed a UUID. |
| 2342 | |
| 2343 | if (module_spec.GetUUID().IsValid()) |
| 2344 | module_sp = m_images.FindFirstModule(module_spec); |
| 2345 | |
| 2346 | if (!module_sp) { |
| 2347 | llvm::SmallVector<ModuleSP, 1> |
| 2348 | old_modules; // This will get filled in if we have a new version |
| 2349 | // of the library |
| 2350 | bool did_create_module = false; |
| 2351 | FileSpecList search_paths = GetExecutableSearchPaths(); |
| 2352 | FileSpec symbol_file_spec; |
| 2353 | |
| 2354 | // Call locate module callback if set. This allows users to implement their |
| 2355 | // own module cache system. For example, to leverage build system artifacts, |
| 2356 | // to bypass pulling files from remote platform, or to search symbol files |
| 2357 | // from symbol servers. |
| 2358 | if (m_platform_sp) |
| 2359 | m_platform_sp->CallLocateModuleCallbackIfSet( |
| 2360 | module_spec, module_sp, symbol_file_spec, did_create_ptr: &did_create_module); |
| 2361 | |
| 2362 | // The result of this CallLocateModuleCallbackIfSet is one of the following. |
| 2363 | // 1. module_sp:loaded, symbol_file_spec:set |
| 2364 | // The callback found a module file and a symbol file for the |
| 2365 | // module_spec. We will call module_sp->SetSymbolFileFileSpec with |
| 2366 | // the symbol_file_spec later. |
| 2367 | // 2. module_sp:loaded, symbol_file_spec:empty |
| 2368 | // The callback only found a module file for the module_spec. |
| 2369 | // 3. module_sp:empty, symbol_file_spec:set |
| 2370 | // The callback only found a symbol file for the module. We continue |
| 2371 | // to find a module file for this module_spec and we will call |
| 2372 | // module_sp->SetSymbolFileFileSpec with the symbol_file_spec later. |
| 2373 | // 4. module_sp:empty, symbol_file_spec:empty |
| 2374 | // Platform does not exist, the callback is not set, the callback did |
| 2375 | // not find any module files nor any symbol files, the callback failed, |
| 2376 | // or something went wrong. We continue to find a module file for this |
| 2377 | // module_spec. |
| 2378 | |
| 2379 | if (!module_sp) { |
| 2380 | // If there are image search path entries, try to use them to acquire a |
| 2381 | // suitable image. |
| 2382 | if (m_image_search_paths.GetSize()) { |
| 2383 | ModuleSpec transformed_spec(module_spec); |
| 2384 | ConstString transformed_dir; |
| 2385 | if (m_image_search_paths.RemapPath( |
| 2386 | path: module_spec.GetFileSpec().GetDirectory(), new_path&: transformed_dir)) { |
| 2387 | transformed_spec.GetFileSpec().SetDirectory(transformed_dir); |
| 2388 | transformed_spec.GetFileSpec().SetFilename( |
| 2389 | module_spec.GetFileSpec().GetFilename()); |
| 2390 | error = ModuleList::GetSharedModule(module_spec: transformed_spec, module_sp, |
| 2391 | module_search_paths_ptr: &search_paths, old_modules: &old_modules, |
| 2392 | did_create_ptr: &did_create_module); |
| 2393 | } |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | if (!module_sp) { |
| 2398 | // If we have a UUID, we can check our global shared module list in case |
| 2399 | // we already have it. If we don't have a valid UUID, then we can't since |
| 2400 | // the path in "module_spec" will be a platform path, and we will need to |
| 2401 | // let the platform find that file. For example, we could be asking for |
| 2402 | // "/usr/lib/dyld" and if we do not have a UUID, we don't want to pick |
| 2403 | // the local copy of "/usr/lib/dyld" since our platform could be a remote |
| 2404 | // platform that has its own "/usr/lib/dyld" in an SDK or in a local file |
| 2405 | // cache. |
| 2406 | if (module_spec.GetUUID().IsValid()) { |
| 2407 | // We have a UUID, it is OK to check the global module list... |
| 2408 | error = |
| 2409 | ModuleList::GetSharedModule(module_spec, module_sp, module_search_paths_ptr: &search_paths, |
| 2410 | old_modules: &old_modules, did_create_ptr: &did_create_module); |
| 2411 | } |
| 2412 | |
| 2413 | if (!module_sp) { |
| 2414 | // The platform is responsible for finding and caching an appropriate |
| 2415 | // module in the shared module cache. |
| 2416 | if (m_platform_sp) { |
| 2417 | error = m_platform_sp->GetSharedModule( |
| 2418 | module_spec, process: m_process_sp.get(), module_sp, module_search_paths_ptr: &search_paths, |
| 2419 | old_modules: &old_modules, did_create_ptr: &did_create_module); |
| 2420 | } else { |
| 2421 | error = Status::FromErrorString(str: "no platform is currently set" ); |
| 2422 | } |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | // We found a module that wasn't in our target list. Let's make sure that |
| 2427 | // there wasn't an equivalent module in the list already, and if there was, |
| 2428 | // let's remove it. |
| 2429 | if (module_sp) { |
| 2430 | ObjectFile *objfile = module_sp->GetObjectFile(); |
| 2431 | if (objfile) { |
| 2432 | switch (objfile->GetType()) { |
| 2433 | case ObjectFile::eTypeCoreFile: /// A core file that has a checkpoint of |
| 2434 | /// a program's execution state |
| 2435 | case ObjectFile::eTypeExecutable: /// A normal executable |
| 2436 | case ObjectFile::eTypeDynamicLinker: /// The platform's dynamic linker |
| 2437 | /// executable |
| 2438 | case ObjectFile::eTypeObjectFile: /// An intermediate object file |
| 2439 | case ObjectFile::eTypeSharedLibrary: /// A shared library that can be |
| 2440 | /// used during execution |
| 2441 | break; |
| 2442 | case ObjectFile::eTypeDebugInfo: /// An object file that contains only |
| 2443 | /// debug information |
| 2444 | if (error_ptr) |
| 2445 | *error_ptr = Status::FromErrorString( |
| 2446 | str: "debug info files aren't valid target " |
| 2447 | "modules, please specify an executable" ); |
| 2448 | return ModuleSP(); |
| 2449 | case ObjectFile::eTypeStubLibrary: /// A library that can be linked |
| 2450 | /// against but not used for |
| 2451 | /// execution |
| 2452 | if (error_ptr) |
| 2453 | *error_ptr = Status::FromErrorString( |
| 2454 | str: "stub libraries aren't valid target " |
| 2455 | "modules, please specify an executable" ); |
| 2456 | return ModuleSP(); |
| 2457 | default: |
| 2458 | if (error_ptr) |
| 2459 | *error_ptr = Status::FromErrorString( |
| 2460 | str: "unsupported file type, please specify an executable" ); |
| 2461 | return ModuleSP(); |
| 2462 | } |
| 2463 | // GetSharedModule is not guaranteed to find the old shared module, for |
| 2464 | // instance in the common case where you pass in the UUID, it is only |
| 2465 | // going to find the one module matching the UUID. In fact, it has no |
| 2466 | // good way to know what the "old module" relevant to this target is, |
| 2467 | // since there might be many copies of a module with this file spec in |
| 2468 | // various running debug sessions, but only one of them will belong to |
| 2469 | // this target. So let's remove the UUID from the module list, and look |
| 2470 | // in the target's module list. Only do this if there is SOMETHING else |
| 2471 | // in the module spec... |
| 2472 | if (module_spec.GetUUID().IsValid() && |
| 2473 | !module_spec.GetFileSpec().GetFilename().IsEmpty() && |
| 2474 | !module_spec.GetFileSpec().GetDirectory().IsEmpty()) { |
| 2475 | ModuleSpec module_spec_copy(module_spec.GetFileSpec()); |
| 2476 | module_spec_copy.GetUUID().Clear(); |
| 2477 | |
| 2478 | ModuleList found_modules; |
| 2479 | m_images.FindModules(module_spec: module_spec_copy, matching_module_list&: found_modules); |
| 2480 | found_modules.ForEach(callback: [&](const ModuleSP &found_module) -> bool { |
| 2481 | old_modules.push_back(Elt: found_module); |
| 2482 | return true; |
| 2483 | }); |
| 2484 | } |
| 2485 | |
| 2486 | // If the locate module callback had found a symbol file, set it to the |
| 2487 | // module_sp before preloading symbols. |
| 2488 | if (symbol_file_spec) |
| 2489 | module_sp->SetSymbolFileFileSpec(symbol_file_spec); |
| 2490 | |
| 2491 | // Preload symbols outside of any lock, so hopefully we can do this for |
| 2492 | // each library in parallel. |
| 2493 | if (GetPreloadSymbols()) |
| 2494 | module_sp->PreloadSymbols(); |
| 2495 | llvm::SmallVector<ModuleSP, 1> replaced_modules; |
| 2496 | for (ModuleSP &old_module_sp : old_modules) { |
| 2497 | if (m_images.GetIndexForModule(module: old_module_sp.get()) != |
| 2498 | LLDB_INVALID_INDEX32) { |
| 2499 | if (replaced_modules.empty()) |
| 2500 | m_images.ReplaceModule(old_module_sp, new_module_sp: module_sp); |
| 2501 | else |
| 2502 | m_images.Remove(module_sp: old_module_sp); |
| 2503 | |
| 2504 | replaced_modules.push_back(Elt: std::move(old_module_sp)); |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | if (replaced_modules.size() > 1) { |
| 2509 | // The same new module replaced multiple old modules |
| 2510 | // simultaneously. It's not clear this should ever |
| 2511 | // happen (if we always replace old modules as we add |
| 2512 | // new ones, presumably we should never have more than |
| 2513 | // one old one). If there are legitimate cases where |
| 2514 | // this happens, then the ModuleList::Notifier interface |
| 2515 | // may need to be adjusted to allow reporting this. |
| 2516 | // In the meantime, just log that this has happened; just |
| 2517 | // above we called ReplaceModule on the first one, and Remove |
| 2518 | // on the rest. |
| 2519 | if (Log *log = GetLog(mask: LLDBLog::Target | LLDBLog::Modules)) { |
| 2520 | StreamString message; |
| 2521 | auto dump = [&message](Module &dump_module) -> void { |
| 2522 | UUID dump_uuid = dump_module.GetUUID(); |
| 2523 | |
| 2524 | message << '['; |
| 2525 | dump_module.GetDescription(s&: message.AsRawOstream()); |
| 2526 | message << " (uuid " ; |
| 2527 | |
| 2528 | if (dump_uuid.IsValid()) |
| 2529 | dump_uuid.Dump(s&: message); |
| 2530 | else |
| 2531 | message << "not specified" ; |
| 2532 | |
| 2533 | message << ")]" ; |
| 2534 | }; |
| 2535 | |
| 2536 | message << "New module " ; |
| 2537 | dump(*module_sp); |
| 2538 | message.AsRawOstream() |
| 2539 | << llvm::formatv(Fmt: " simultaneously replaced {0} old modules: " , |
| 2540 | Vals: replaced_modules.size()); |
| 2541 | for (ModuleSP &replaced_module_sp : replaced_modules) |
| 2542 | dump(*replaced_module_sp); |
| 2543 | |
| 2544 | log->PutString(str: message.GetString()); |
| 2545 | } |
| 2546 | } |
| 2547 | |
| 2548 | if (replaced_modules.empty()) |
| 2549 | m_images.Append(module_sp, notify); |
| 2550 | |
| 2551 | for (ModuleSP &old_module_sp : replaced_modules) { |
| 2552 | Module *old_module_ptr = old_module_sp.get(); |
| 2553 | old_module_sp.reset(); |
| 2554 | ModuleList::RemoveSharedModuleIfOrphaned(module_ptr: old_module_ptr); |
| 2555 | } |
| 2556 | } else |
| 2557 | module_sp.reset(); |
| 2558 | } |
| 2559 | } |
| 2560 | if (error_ptr) |
| 2561 | *error_ptr = std::move(error); |
| 2562 | return module_sp; |
| 2563 | } |
| 2564 | |
| 2565 | TargetSP Target::CalculateTarget() { return shared_from_this(); } |
| 2566 | |
| 2567 | ProcessSP Target::CalculateProcess() { return m_process_sp; } |
| 2568 | |
| 2569 | ThreadSP Target::CalculateThread() { return ThreadSP(); } |
| 2570 | |
| 2571 | StackFrameSP Target::CalculateStackFrame() { return StackFrameSP(); } |
| 2572 | |
| 2573 | void Target::CalculateExecutionContext(ExecutionContext &exe_ctx) { |
| 2574 | exe_ctx.Clear(); |
| 2575 | exe_ctx.SetTargetPtr(this); |
| 2576 | } |
| 2577 | |
| 2578 | PathMappingList &Target::GetImageSearchPathList() { |
| 2579 | return m_image_search_paths; |
| 2580 | } |
| 2581 | |
| 2582 | void Target::ImageSearchPathsChanged(const PathMappingList &path_list, |
| 2583 | void *baton) { |
| 2584 | Target *target = (Target *)baton; |
| 2585 | ModuleSP exe_module_sp(target->GetExecutableModule()); |
| 2586 | if (exe_module_sp) |
| 2587 | target->SetExecutableModule(executable_sp&: exe_module_sp, load_dependent_files: eLoadDependentsYes); |
| 2588 | } |
| 2589 | |
| 2590 | llvm::Expected<lldb::TypeSystemSP> |
| 2591 | Target::GetScratchTypeSystemForLanguage(lldb::LanguageType language, |
| 2592 | bool create_on_demand) { |
| 2593 | if (!m_valid) |
| 2594 | return llvm::createStringError(Fmt: "Invalid Target" ); |
| 2595 | |
| 2596 | if (language == eLanguageTypeMipsAssembler // GNU AS and LLVM use it for all |
| 2597 | // assembly code |
| 2598 | || language == eLanguageTypeUnknown) { |
| 2599 | LanguageSet languages_for_expressions = |
| 2600 | Language::GetLanguagesSupportingTypeSystemsForExpressions(); |
| 2601 | |
| 2602 | if (languages_for_expressions[eLanguageTypeC]) { |
| 2603 | language = eLanguageTypeC; // LLDB's default. Override by setting the |
| 2604 | // target language. |
| 2605 | } else { |
| 2606 | if (languages_for_expressions.Empty()) |
| 2607 | return llvm::createStringError( |
| 2608 | Fmt: "No expression support for any languages" ); |
| 2609 | language = (LanguageType)languages_for_expressions.bitvector.find_first(); |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | return m_scratch_type_system_map.GetTypeSystemForLanguage(language, target: this, |
| 2614 | can_create: create_on_demand); |
| 2615 | } |
| 2616 | |
| 2617 | CompilerType Target::GetRegisterType(const std::string &name, |
| 2618 | const lldb_private::RegisterFlags &flags, |
| 2619 | uint32_t byte_size) { |
| 2620 | RegisterTypeBuilderSP provider = PluginManager::GetRegisterTypeBuilder(target&: *this); |
| 2621 | assert(provider); |
| 2622 | return provider->GetRegisterType(name, flags, byte_size); |
| 2623 | } |
| 2624 | |
| 2625 | std::vector<lldb::TypeSystemSP> |
| 2626 | Target::GetScratchTypeSystems(bool create_on_demand) { |
| 2627 | if (!m_valid) |
| 2628 | return {}; |
| 2629 | |
| 2630 | // Some TypeSystem instances are associated with several LanguageTypes so |
| 2631 | // they will show up several times in the loop below. The SetVector filters |
| 2632 | // out all duplicates as they serve no use for the caller. |
| 2633 | std::vector<lldb::TypeSystemSP> scratch_type_systems; |
| 2634 | |
| 2635 | LanguageSet languages_for_expressions = |
| 2636 | Language::GetLanguagesSupportingTypeSystemsForExpressions(); |
| 2637 | |
| 2638 | for (auto bit : languages_for_expressions.bitvector.set_bits()) { |
| 2639 | auto language = (LanguageType)bit; |
| 2640 | auto type_system_or_err = |
| 2641 | GetScratchTypeSystemForLanguage(language, create_on_demand); |
| 2642 | if (!type_system_or_err) |
| 2643 | LLDB_LOG_ERROR( |
| 2644 | GetLog(LLDBLog::Target), type_system_or_err.takeError(), |
| 2645 | "Language '{1}' has expression support but no scratch type " |
| 2646 | "system available: {0}" , |
| 2647 | Language::GetNameForLanguageType(language)); |
| 2648 | else |
| 2649 | if (auto ts = *type_system_or_err) |
| 2650 | scratch_type_systems.push_back(x: ts); |
| 2651 | } |
| 2652 | |
| 2653 | std::sort(first: scratch_type_systems.begin(), last: scratch_type_systems.end()); |
| 2654 | scratch_type_systems.erase(first: llvm::unique(R&: scratch_type_systems), |
| 2655 | last: scratch_type_systems.end()); |
| 2656 | return scratch_type_systems; |
| 2657 | } |
| 2658 | |
| 2659 | PersistentExpressionState * |
| 2660 | Target::GetPersistentExpressionStateForLanguage(lldb::LanguageType language) { |
| 2661 | auto type_system_or_err = GetScratchTypeSystemForLanguage(language, create_on_demand: true); |
| 2662 | |
| 2663 | if (auto err = type_system_or_err.takeError()) { |
| 2664 | LLDB_LOG_ERROR( |
| 2665 | GetLog(LLDBLog::Target), std::move(err), |
| 2666 | "Unable to get persistent expression state for language {1}: {0}" , |
| 2667 | Language::GetNameForLanguageType(language)); |
| 2668 | return nullptr; |
| 2669 | } |
| 2670 | |
| 2671 | if (auto ts = *type_system_or_err) |
| 2672 | return ts->GetPersistentExpressionState(); |
| 2673 | |
| 2674 | LLDB_LOG(GetLog(LLDBLog::Target), |
| 2675 | "Unable to get persistent expression state for language {1}: {0}" , |
| 2676 | Language::GetNameForLanguageType(language)); |
| 2677 | return nullptr; |
| 2678 | } |
| 2679 | |
| 2680 | UserExpression *Target::GetUserExpressionForLanguage( |
| 2681 | llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language, |
| 2682 | Expression::ResultType desired_type, |
| 2683 | const EvaluateExpressionOptions &options, ValueObject *ctx_obj, |
| 2684 | Status &error) { |
| 2685 | auto type_system_or_err = |
| 2686 | GetScratchTypeSystemForLanguage(language: language.AsLanguageType()); |
| 2687 | if (auto err = type_system_or_err.takeError()) { |
| 2688 | error = Status::FromErrorStringWithFormat( |
| 2689 | format: "Could not find type system for language %s: %s" , |
| 2690 | Language::GetNameForLanguageType(language: language.AsLanguageType()), |
| 2691 | llvm::toString(E: std::move(err)).c_str()); |
| 2692 | return nullptr; |
| 2693 | } |
| 2694 | |
| 2695 | auto ts = *type_system_or_err; |
| 2696 | if (!ts) { |
| 2697 | error = Status::FromErrorStringWithFormat( |
| 2698 | format: "Type system for language %s is no longer live" , |
| 2699 | language.GetDescription().data()); |
| 2700 | return nullptr; |
| 2701 | } |
| 2702 | |
| 2703 | auto *user_expr = ts->GetUserExpression(expr, prefix, language, desired_type, |
| 2704 | options, ctx_obj); |
| 2705 | if (!user_expr) |
| 2706 | error = Status::FromErrorStringWithFormat( |
| 2707 | format: "Could not create an expression for language %s" , |
| 2708 | language.GetDescription().data()); |
| 2709 | |
| 2710 | return user_expr; |
| 2711 | } |
| 2712 | |
| 2713 | FunctionCaller *Target::GetFunctionCallerForLanguage( |
| 2714 | lldb::LanguageType language, const CompilerType &return_type, |
| 2715 | const Address &function_address, const ValueList &arg_value_list, |
| 2716 | const char *name, Status &error) { |
| 2717 | auto type_system_or_err = GetScratchTypeSystemForLanguage(language); |
| 2718 | if (auto err = type_system_or_err.takeError()) { |
| 2719 | error = Status::FromErrorStringWithFormat( |
| 2720 | format: "Could not find type system for language %s: %s" , |
| 2721 | Language::GetNameForLanguageType(language), |
| 2722 | llvm::toString(E: std::move(err)).c_str()); |
| 2723 | return nullptr; |
| 2724 | } |
| 2725 | auto ts = *type_system_or_err; |
| 2726 | if (!ts) { |
| 2727 | error = Status::FromErrorStringWithFormat( |
| 2728 | format: "Type system for language %s is no longer live" , |
| 2729 | Language::GetNameForLanguageType(language)); |
| 2730 | return nullptr; |
| 2731 | } |
| 2732 | auto *persistent_fn = ts->GetFunctionCaller(return_type, function_address, |
| 2733 | arg_value_list, name); |
| 2734 | if (!persistent_fn) |
| 2735 | error = Status::FromErrorStringWithFormat( |
| 2736 | format: "Could not create an expression for language %s" , |
| 2737 | Language::GetNameForLanguageType(language)); |
| 2738 | |
| 2739 | return persistent_fn; |
| 2740 | } |
| 2741 | |
| 2742 | llvm::Expected<std::unique_ptr<UtilityFunction>> |
| 2743 | Target::CreateUtilityFunction(std::string expression, std::string name, |
| 2744 | lldb::LanguageType language, |
| 2745 | ExecutionContext &exe_ctx) { |
| 2746 | auto type_system_or_err = GetScratchTypeSystemForLanguage(language); |
| 2747 | if (!type_system_or_err) |
| 2748 | return type_system_or_err.takeError(); |
| 2749 | auto ts = *type_system_or_err; |
| 2750 | if (!ts) |
| 2751 | return llvm::createStringError( |
| 2752 | S: llvm::StringRef("Type system for language " ) + |
| 2753 | Language::GetNameForLanguageType(language) + |
| 2754 | llvm::StringRef(" is no longer live" )); |
| 2755 | std::unique_ptr<UtilityFunction> utility_fn = |
| 2756 | ts->CreateUtilityFunction(text: std::move(expression), name: std::move(name)); |
| 2757 | if (!utility_fn) |
| 2758 | return llvm::createStringError( |
| 2759 | S: llvm::StringRef("Could not create an expression for language" ) + |
| 2760 | Language::GetNameForLanguageType(language)); |
| 2761 | |
| 2762 | DiagnosticManager diagnostics; |
| 2763 | if (!utility_fn->Install(diagnostic_manager&: diagnostics, exe_ctx)) |
| 2764 | return diagnostics.GetAsError(result: lldb::eExpressionSetupError, |
| 2765 | message: "Could not install utility function:" ); |
| 2766 | |
| 2767 | return std::move(utility_fn); |
| 2768 | } |
| 2769 | |
| 2770 | void Target::SettingsInitialize() { Process::SettingsInitialize(); } |
| 2771 | |
| 2772 | void Target::SettingsTerminate() { Process::SettingsTerminate(); } |
| 2773 | |
| 2774 | FileSpecList Target::GetDefaultExecutableSearchPaths() { |
| 2775 | return Target::GetGlobalProperties().GetExecutableSearchPaths(); |
| 2776 | } |
| 2777 | |
| 2778 | FileSpecList Target::GetDefaultDebugFileSearchPaths() { |
| 2779 | return Target::GetGlobalProperties().GetDebugFileSearchPaths(); |
| 2780 | } |
| 2781 | |
| 2782 | ArchSpec Target::GetDefaultArchitecture() { |
| 2783 | return Target::GetGlobalProperties().GetDefaultArchitecture(); |
| 2784 | } |
| 2785 | |
| 2786 | void Target::SetDefaultArchitecture(const ArchSpec &arch) { |
| 2787 | LLDB_LOG(GetLog(LLDBLog::Target), |
| 2788 | "setting target's default architecture to {0} ({1})" , |
| 2789 | arch.GetArchitectureName(), arch.GetTriple().getTriple()); |
| 2790 | Target::GetGlobalProperties().SetDefaultArchitecture(arch); |
| 2791 | } |
| 2792 | |
| 2793 | llvm::Error Target::SetLabel(llvm::StringRef label) { |
| 2794 | size_t n = LLDB_INVALID_INDEX32; |
| 2795 | if (llvm::to_integer(S: label, Num&: n)) |
| 2796 | return llvm::createStringError(Fmt: "Cannot use integer as target label." ); |
| 2797 | TargetList &targets = GetDebugger().GetTargetList(); |
| 2798 | for (size_t i = 0; i < targets.GetNumTargets(); i++) { |
| 2799 | TargetSP target_sp = targets.GetTargetAtIndex(index: i); |
| 2800 | if (target_sp && target_sp->GetLabel() == label) { |
| 2801 | return llvm::make_error<llvm::StringError>( |
| 2802 | Args: llvm::formatv( |
| 2803 | Fmt: "Cannot use label '{0}' since it's set in target #{1}." , Vals&: label, |
| 2804 | Vals&: i), |
| 2805 | Args: llvm::inconvertibleErrorCode()); |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | m_label = label.str(); |
| 2810 | return llvm::Error::success(); |
| 2811 | } |
| 2812 | |
| 2813 | Target *Target::GetTargetFromContexts(const ExecutionContext *exe_ctx_ptr, |
| 2814 | const SymbolContext *sc_ptr) { |
| 2815 | // The target can either exist in the "process" of ExecutionContext, or in |
| 2816 | // the "target_sp" member of SymbolContext. This accessor helper function |
| 2817 | // will get the target from one of these locations. |
| 2818 | |
| 2819 | Target *target = nullptr; |
| 2820 | if (sc_ptr != nullptr) |
| 2821 | target = sc_ptr->target_sp.get(); |
| 2822 | if (target == nullptr && exe_ctx_ptr) |
| 2823 | target = exe_ctx_ptr->GetTargetPtr(); |
| 2824 | return target; |
| 2825 | } |
| 2826 | |
| 2827 | ExpressionResults Target::EvaluateExpression( |
| 2828 | llvm::StringRef expr, ExecutionContextScope *exe_scope, |
| 2829 | lldb::ValueObjectSP &result_valobj_sp, |
| 2830 | const EvaluateExpressionOptions &options, std::string *fixed_expression, |
| 2831 | ValueObject *ctx_obj) { |
| 2832 | result_valobj_sp.reset(); |
| 2833 | |
| 2834 | ExpressionResults execution_results = eExpressionSetupError; |
| 2835 | |
| 2836 | if (expr.empty()) { |
| 2837 | m_stats.GetExpressionStats().NotifyFailure(); |
| 2838 | return execution_results; |
| 2839 | } |
| 2840 | |
| 2841 | // We shouldn't run stop hooks in expressions. |
| 2842 | bool old_suppress_value = m_suppress_stop_hooks; |
| 2843 | m_suppress_stop_hooks = true; |
| 2844 | auto on_exit = llvm::make_scope_exit(F: [this, old_suppress_value]() { |
| 2845 | m_suppress_stop_hooks = old_suppress_value; |
| 2846 | }); |
| 2847 | |
| 2848 | ExecutionContext exe_ctx; |
| 2849 | |
| 2850 | if (exe_scope) { |
| 2851 | exe_scope->CalculateExecutionContext(exe_ctx); |
| 2852 | } else if (m_process_sp) { |
| 2853 | m_process_sp->CalculateExecutionContext(exe_ctx); |
| 2854 | } else { |
| 2855 | CalculateExecutionContext(exe_ctx); |
| 2856 | } |
| 2857 | |
| 2858 | // Make sure we aren't just trying to see the value of a persistent variable |
| 2859 | // (something like "$0") |
| 2860 | // Only check for persistent variables the expression starts with a '$' |
| 2861 | lldb::ExpressionVariableSP persistent_var_sp; |
| 2862 | if (expr[0] == '$') { |
| 2863 | auto type_system_or_err = |
| 2864 | GetScratchTypeSystemForLanguage(language: eLanguageTypeC); |
| 2865 | if (auto err = type_system_or_err.takeError()) { |
| 2866 | LLDB_LOG_ERROR(GetLog(LLDBLog::Target), std::move(err), |
| 2867 | "Unable to get scratch type system" ); |
| 2868 | } else { |
| 2869 | auto ts = *type_system_or_err; |
| 2870 | if (!ts) |
| 2871 | LLDB_LOG_ERROR(GetLog(LLDBLog::Target), std::move(err), |
| 2872 | "Scratch type system is no longer live: {0}" ); |
| 2873 | else |
| 2874 | persistent_var_sp = |
| 2875 | ts->GetPersistentExpressionState()->GetVariable(name: expr); |
| 2876 | } |
| 2877 | } |
| 2878 | if (persistent_var_sp) { |
| 2879 | result_valobj_sp = persistent_var_sp->GetValueObject(); |
| 2880 | execution_results = eExpressionCompleted; |
| 2881 | } else { |
| 2882 | llvm::StringRef prefix = GetExpressionPrefixContents(); |
| 2883 | execution_results = |
| 2884 | UserExpression::Evaluate(exe_ctx, options, expr_cstr: expr, expr_prefix: prefix, |
| 2885 | result_valobj_sp, fixed_expression, ctx_obj); |
| 2886 | } |
| 2887 | |
| 2888 | if (execution_results == eExpressionCompleted) |
| 2889 | m_stats.GetExpressionStats().NotifySuccess(); |
| 2890 | else |
| 2891 | m_stats.GetExpressionStats().NotifyFailure(); |
| 2892 | return execution_results; |
| 2893 | } |
| 2894 | |
| 2895 | lldb::ExpressionVariableSP Target::GetPersistentVariable(ConstString name) { |
| 2896 | lldb::ExpressionVariableSP variable_sp; |
| 2897 | m_scratch_type_system_map.ForEach( |
| 2898 | callback: [name, &variable_sp](TypeSystemSP type_system) -> bool { |
| 2899 | auto ts = type_system.get(); |
| 2900 | if (!ts) |
| 2901 | return true; |
| 2902 | if (PersistentExpressionState *persistent_state = |
| 2903 | ts->GetPersistentExpressionState()) { |
| 2904 | variable_sp = persistent_state->GetVariable(name); |
| 2905 | |
| 2906 | if (variable_sp) |
| 2907 | return false; // Stop iterating the ForEach |
| 2908 | } |
| 2909 | return true; // Keep iterating the ForEach |
| 2910 | }); |
| 2911 | return variable_sp; |
| 2912 | } |
| 2913 | |
| 2914 | lldb::addr_t Target::GetPersistentSymbol(ConstString name) { |
| 2915 | lldb::addr_t address = LLDB_INVALID_ADDRESS; |
| 2916 | |
| 2917 | m_scratch_type_system_map.ForEach( |
| 2918 | callback: [name, &address](lldb::TypeSystemSP type_system) -> bool { |
| 2919 | auto ts = type_system.get(); |
| 2920 | if (!ts) |
| 2921 | return true; |
| 2922 | |
| 2923 | if (PersistentExpressionState *persistent_state = |
| 2924 | ts->GetPersistentExpressionState()) { |
| 2925 | address = persistent_state->LookupSymbol(name); |
| 2926 | if (address != LLDB_INVALID_ADDRESS) |
| 2927 | return false; // Stop iterating the ForEach |
| 2928 | } |
| 2929 | return true; // Keep iterating the ForEach |
| 2930 | }); |
| 2931 | return address; |
| 2932 | } |
| 2933 | |
| 2934 | llvm::Expected<lldb_private::Address> Target::GetEntryPointAddress() { |
| 2935 | Module *exe_module = GetExecutableModulePointer(); |
| 2936 | |
| 2937 | // Try to find the entry point address in the primary executable. |
| 2938 | const bool has_primary_executable = exe_module && exe_module->GetObjectFile(); |
| 2939 | if (has_primary_executable) { |
| 2940 | Address entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress(); |
| 2941 | if (entry_addr.IsValid()) |
| 2942 | return entry_addr; |
| 2943 | } |
| 2944 | |
| 2945 | const ModuleList &modules = GetImages(); |
| 2946 | const size_t num_images = modules.GetSize(); |
| 2947 | for (size_t idx = 0; idx < num_images; ++idx) { |
| 2948 | ModuleSP module_sp(modules.GetModuleAtIndex(idx)); |
| 2949 | if (!module_sp || !module_sp->GetObjectFile()) |
| 2950 | continue; |
| 2951 | |
| 2952 | Address entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress(); |
| 2953 | if (entry_addr.IsValid()) |
| 2954 | return entry_addr; |
| 2955 | } |
| 2956 | |
| 2957 | // We haven't found the entry point address. Return an appropriate error. |
| 2958 | if (!has_primary_executable) |
| 2959 | return llvm::createStringError( |
| 2960 | Fmt: "No primary executable found and could not find entry point address in " |
| 2961 | "any executable module" ); |
| 2962 | |
| 2963 | return llvm::createStringError( |
| 2964 | S: "Could not find entry point address for primary executable module \"" + |
| 2965 | exe_module->GetFileSpec().GetFilename().GetStringRef() + "\"" ); |
| 2966 | } |
| 2967 | |
| 2968 | lldb::addr_t Target::GetCallableLoadAddress(lldb::addr_t load_addr, |
| 2969 | AddressClass addr_class) const { |
| 2970 | auto arch_plugin = GetArchitecturePlugin(); |
| 2971 | return arch_plugin |
| 2972 | ? arch_plugin->GetCallableLoadAddress(addr: load_addr, addr_class) |
| 2973 | : load_addr; |
| 2974 | } |
| 2975 | |
| 2976 | lldb::addr_t Target::GetOpcodeLoadAddress(lldb::addr_t load_addr, |
| 2977 | AddressClass addr_class) const { |
| 2978 | auto arch_plugin = GetArchitecturePlugin(); |
| 2979 | return arch_plugin ? arch_plugin->GetOpcodeLoadAddress(addr: load_addr, addr_class) |
| 2980 | : load_addr; |
| 2981 | } |
| 2982 | |
| 2983 | lldb::addr_t Target::GetBreakableLoadAddress(lldb::addr_t addr) { |
| 2984 | auto arch_plugin = GetArchitecturePlugin(); |
| 2985 | return arch_plugin ? arch_plugin->GetBreakableLoadAddress(addr, target&: *this) : addr; |
| 2986 | } |
| 2987 | |
| 2988 | SourceManager &Target::GetSourceManager() { |
| 2989 | if (!m_source_manager_up) |
| 2990 | m_source_manager_up = std::make_unique<SourceManager>(args: shared_from_this()); |
| 2991 | return *m_source_manager_up; |
| 2992 | } |
| 2993 | |
| 2994 | Target::StopHookSP Target::CreateStopHook(StopHook::StopHookKind kind) { |
| 2995 | lldb::user_id_t new_uid = ++m_stop_hook_next_id; |
| 2996 | Target::StopHookSP stop_hook_sp; |
| 2997 | switch (kind) { |
| 2998 | case StopHook::StopHookKind::CommandBased: |
| 2999 | stop_hook_sp.reset(p: new StopHookCommandLine(shared_from_this(), new_uid)); |
| 3000 | break; |
| 3001 | case StopHook::StopHookKind::ScriptBased: |
| 3002 | stop_hook_sp.reset(p: new StopHookScripted(shared_from_this(), new_uid)); |
| 3003 | break; |
| 3004 | } |
| 3005 | m_stop_hooks[new_uid] = stop_hook_sp; |
| 3006 | return stop_hook_sp; |
| 3007 | } |
| 3008 | |
| 3009 | void Target::UndoCreateStopHook(lldb::user_id_t user_id) { |
| 3010 | if (!RemoveStopHookByID(uid: user_id)) |
| 3011 | return; |
| 3012 | if (user_id == m_stop_hook_next_id) |
| 3013 | m_stop_hook_next_id--; |
| 3014 | } |
| 3015 | |
| 3016 | bool Target::RemoveStopHookByID(lldb::user_id_t user_id) { |
| 3017 | size_t num_removed = m_stop_hooks.erase(x: user_id); |
| 3018 | return (num_removed != 0); |
| 3019 | } |
| 3020 | |
| 3021 | void Target::RemoveAllStopHooks() { m_stop_hooks.clear(); } |
| 3022 | |
| 3023 | Target::StopHookSP Target::GetStopHookByID(lldb::user_id_t user_id) { |
| 3024 | StopHookSP found_hook; |
| 3025 | |
| 3026 | StopHookCollection::iterator specified_hook_iter; |
| 3027 | specified_hook_iter = m_stop_hooks.find(x: user_id); |
| 3028 | if (specified_hook_iter != m_stop_hooks.end()) |
| 3029 | found_hook = (*specified_hook_iter).second; |
| 3030 | return found_hook; |
| 3031 | } |
| 3032 | |
| 3033 | bool Target::SetStopHookActiveStateByID(lldb::user_id_t user_id, |
| 3034 | bool active_state) { |
| 3035 | StopHookCollection::iterator specified_hook_iter; |
| 3036 | specified_hook_iter = m_stop_hooks.find(x: user_id); |
| 3037 | if (specified_hook_iter == m_stop_hooks.end()) |
| 3038 | return false; |
| 3039 | |
| 3040 | (*specified_hook_iter).second->SetIsActive(active_state); |
| 3041 | return true; |
| 3042 | } |
| 3043 | |
| 3044 | void Target::SetAllStopHooksActiveState(bool active_state) { |
| 3045 | StopHookCollection::iterator pos, end = m_stop_hooks.end(); |
| 3046 | for (pos = m_stop_hooks.begin(); pos != end; pos++) { |
| 3047 | (*pos).second->SetIsActive(active_state); |
| 3048 | } |
| 3049 | } |
| 3050 | |
| 3051 | bool Target::RunStopHooks(bool at_initial_stop) { |
| 3052 | if (m_suppress_stop_hooks) |
| 3053 | return false; |
| 3054 | |
| 3055 | if (!m_process_sp) |
| 3056 | return false; |
| 3057 | |
| 3058 | // Somebody might have restarted the process: |
| 3059 | // Still return false, the return value is about US restarting the target. |
| 3060 | lldb::StateType state = m_process_sp->GetState(); |
| 3061 | if (!(state == eStateStopped || state == eStateAttaching)) |
| 3062 | return false; |
| 3063 | |
| 3064 | if (m_stop_hooks.empty()) |
| 3065 | return false; |
| 3066 | |
| 3067 | bool no_active_hooks = |
| 3068 | llvm::none_of(Range&: m_stop_hooks, P: [at_initial_stop](auto &p) { |
| 3069 | bool should_run_now = |
| 3070 | !at_initial_stop || p.second->GetRunAtInitialStop(); |
| 3071 | return p.second->IsActive() && should_run_now; |
| 3072 | }); |
| 3073 | if (no_active_hooks) |
| 3074 | return false; |
| 3075 | |
| 3076 | // Make sure we check that we are not stopped because of us running a user |
| 3077 | // expression since in that case we do not want to run the stop-hooks. Note, |
| 3078 | // you can't just check whether the last stop was for a User Expression, |
| 3079 | // because breakpoint commands get run before stop hooks, and one of them |
| 3080 | // might have run an expression. You have to ensure you run the stop hooks |
| 3081 | // once per natural stop. |
| 3082 | uint32_t last_natural_stop = m_process_sp->GetModIDRef().GetLastNaturalStopID(); |
| 3083 | if (last_natural_stop != 0 && m_latest_stop_hook_id == last_natural_stop) |
| 3084 | return false; |
| 3085 | |
| 3086 | m_latest_stop_hook_id = last_natural_stop; |
| 3087 | |
| 3088 | std::vector<ExecutionContext> exc_ctx_with_reasons; |
| 3089 | |
| 3090 | ThreadList &cur_threadlist = m_process_sp->GetThreadList(); |
| 3091 | size_t num_threads = cur_threadlist.GetSize(); |
| 3092 | for (size_t i = 0; i < num_threads; i++) { |
| 3093 | lldb::ThreadSP cur_thread_sp = cur_threadlist.GetThreadAtIndex(idx: i); |
| 3094 | if (cur_thread_sp->ThreadStoppedForAReason()) { |
| 3095 | lldb::StackFrameSP cur_frame_sp = cur_thread_sp->GetStackFrameAtIndex(idx: 0); |
| 3096 | exc_ctx_with_reasons.emplace_back(args: m_process_sp.get(), args: cur_thread_sp.get(), |
| 3097 | args: cur_frame_sp.get()); |
| 3098 | } |
| 3099 | } |
| 3100 | |
| 3101 | // If no threads stopped for a reason, don't run the stop-hooks. |
| 3102 | // However, if this is the FIRST stop for this process, then we are in the |
| 3103 | // state where an attach or a core file load was completed without designating |
| 3104 | // a particular thread as responsible for the stop. In that case, we do |
| 3105 | // want to run the stop hooks, but do so just on one thread. |
| 3106 | size_t num_exe_ctx = exc_ctx_with_reasons.size(); |
| 3107 | if (num_exe_ctx == 0) { |
| 3108 | if (at_initial_stop && num_threads > 0) { |
| 3109 | lldb::ThreadSP thread_to_use_sp = cur_threadlist.GetThreadAtIndex(idx: 0); |
| 3110 | exc_ctx_with_reasons.emplace_back( |
| 3111 | args: m_process_sp.get(), args: thread_to_use_sp.get(), |
| 3112 | args: thread_to_use_sp->GetStackFrameAtIndex(idx: 0).get()); |
| 3113 | num_exe_ctx = 1; |
| 3114 | } else { |
| 3115 | return false; |
| 3116 | } |
| 3117 | } |
| 3118 | |
| 3119 | StreamSP output_sp = m_debugger.GetAsyncOutputStream(); |
| 3120 | auto on_exit = llvm::make_scope_exit(F: [output_sp] { output_sp->Flush(); }); |
| 3121 | |
| 3122 | bool = (m_stop_hooks.size() != 1); |
| 3123 | bool = (num_exe_ctx != 1); |
| 3124 | bool should_stop = false; |
| 3125 | bool requested_continue = false; |
| 3126 | |
| 3127 | for (auto stop_entry : m_stop_hooks) { |
| 3128 | StopHookSP cur_hook_sp = stop_entry.second; |
| 3129 | if (!cur_hook_sp->IsActive()) |
| 3130 | continue; |
| 3131 | if (at_initial_stop && !cur_hook_sp->GetRunAtInitialStop()) |
| 3132 | continue; |
| 3133 | |
| 3134 | bool any_thread_matched = false; |
| 3135 | for (auto exc_ctx : exc_ctx_with_reasons) { |
| 3136 | if (!cur_hook_sp->ExecutionContextPasses(exe_ctx: exc_ctx)) |
| 3137 | continue; |
| 3138 | |
| 3139 | if (print_hook_header && !any_thread_matched) { |
| 3140 | StreamString s; |
| 3141 | cur_hook_sp->GetDescription(s, level: eDescriptionLevelBrief); |
| 3142 | if (s.GetSize() != 0) |
| 3143 | output_sp->Printf(format: "\n- Hook %" PRIu64 " (%s)\n" , cur_hook_sp->GetID(), |
| 3144 | s.GetData()); |
| 3145 | else |
| 3146 | output_sp->Printf(format: "\n- Hook %" PRIu64 "\n" , cur_hook_sp->GetID()); |
| 3147 | any_thread_matched = true; |
| 3148 | } |
| 3149 | |
| 3150 | if (print_thread_header) |
| 3151 | output_sp->Printf(format: "-- Thread %d\n" , |
| 3152 | exc_ctx.GetThreadPtr()->GetIndexID()); |
| 3153 | |
| 3154 | auto result = cur_hook_sp->HandleStop(exe_ctx&: exc_ctx, output: output_sp); |
| 3155 | switch (result) { |
| 3156 | case StopHook::StopHookResult::KeepStopped: |
| 3157 | if (cur_hook_sp->GetAutoContinue()) |
| 3158 | requested_continue = true; |
| 3159 | else |
| 3160 | should_stop = true; |
| 3161 | break; |
| 3162 | case StopHook::StopHookResult::RequestContinue: |
| 3163 | requested_continue = true; |
| 3164 | break; |
| 3165 | case StopHook::StopHookResult::NoPreference: |
| 3166 | // Do nothing |
| 3167 | break; |
| 3168 | case StopHook::StopHookResult::AlreadyContinued: |
| 3169 | // We don't have a good way to prohibit people from restarting the |
| 3170 | // target willy nilly in a stop hook. If the hook did so, give a |
| 3171 | // gentle suggestion here and back out of the hook processing. |
| 3172 | output_sp->Printf(format: "\nAborting stop hooks, hook %" PRIu64 |
| 3173 | " set the program running.\n" |
| 3174 | " Consider using '-G true' to make " |
| 3175 | "stop hooks auto-continue.\n" , |
| 3176 | cur_hook_sp->GetID()); |
| 3177 | // FIXME: if we are doing non-stop mode for real, we would have to |
| 3178 | // check that OUR thread was restarted, otherwise we should keep |
| 3179 | // processing stop hooks. |
| 3180 | return true; |
| 3181 | } |
| 3182 | } |
| 3183 | } |
| 3184 | |
| 3185 | // Resume iff at least one hook requested to continue and no hook asked to |
| 3186 | // stop. |
| 3187 | if (requested_continue && !should_stop) { |
| 3188 | Log *log = GetLog(mask: LLDBLog::Process); |
| 3189 | Status error = m_process_sp->PrivateResume(); |
| 3190 | if (error.Success()) { |
| 3191 | LLDB_LOG(log, "Resuming from RunStopHooks" ); |
| 3192 | return true; |
| 3193 | } else { |
| 3194 | LLDB_LOG(log, "Resuming from RunStopHooks failed: {0}" , error); |
| 3195 | return false; |
| 3196 | } |
| 3197 | } |
| 3198 | |
| 3199 | return false; |
| 3200 | } |
| 3201 | |
| 3202 | TargetProperties &Target::GetGlobalProperties() { |
| 3203 | // NOTE: intentional leak so we don't crash if global destructor chain gets |
| 3204 | // called as other threads still use the result of this function |
| 3205 | static TargetProperties *g_settings_ptr = |
| 3206 | new TargetProperties(nullptr); |
| 3207 | return *g_settings_ptr; |
| 3208 | } |
| 3209 | |
| 3210 | Status Target::Install(ProcessLaunchInfo *launch_info) { |
| 3211 | Status error; |
| 3212 | PlatformSP platform_sp(GetPlatform()); |
| 3213 | if (!platform_sp || !platform_sp->IsRemote() || !platform_sp->IsConnected()) |
| 3214 | return error; |
| 3215 | |
| 3216 | // Install all files that have an install path when connected to a |
| 3217 | // remote platform. If target.auto-install-main-executable is set then |
| 3218 | // also install the main executable even if it does not have an explicit |
| 3219 | // install path specified. |
| 3220 | |
| 3221 | for (auto module_sp : GetImages().Modules()) { |
| 3222 | if (module_sp == GetExecutableModule()) { |
| 3223 | MainExecutableInstaller installer{platform_sp, module_sp, |
| 3224 | shared_from_this(), *launch_info}; |
| 3225 | error = installExecutable(installer); |
| 3226 | } else { |
| 3227 | ExecutableInstaller installer{platform_sp, module_sp}; |
| 3228 | error = installExecutable(installer); |
| 3229 | } |
| 3230 | |
| 3231 | if (error.Fail()) |
| 3232 | return error; |
| 3233 | } |
| 3234 | |
| 3235 | return error; |
| 3236 | } |
| 3237 | |
| 3238 | bool Target::ResolveLoadAddress(addr_t load_addr, Address &so_addr, |
| 3239 | uint32_t stop_id, bool allow_section_end) { |
| 3240 | return m_section_load_history.ResolveLoadAddress(stop_id, load_addr, so_addr, |
| 3241 | allow_section_end); |
| 3242 | } |
| 3243 | |
| 3244 | bool Target::ResolveFileAddress(lldb::addr_t file_addr, |
| 3245 | Address &resolved_addr) { |
| 3246 | return m_images.ResolveFileAddress(vm_addr: file_addr, so_addr&: resolved_addr); |
| 3247 | } |
| 3248 | |
| 3249 | bool Target::SetSectionLoadAddress(const SectionSP §ion_sp, |
| 3250 | addr_t new_section_load_addr, |
| 3251 | bool warn_multiple) { |
| 3252 | const addr_t old_section_load_addr = |
| 3253 | m_section_load_history.GetSectionLoadAddress( |
| 3254 | stop_id: SectionLoadHistory::eStopIDNow, section_sp); |
| 3255 | if (old_section_load_addr != new_section_load_addr) { |
| 3256 | uint32_t stop_id = 0; |
| 3257 | ProcessSP process_sp(GetProcessSP()); |
| 3258 | if (process_sp) |
| 3259 | stop_id = process_sp->GetStopID(); |
| 3260 | else |
| 3261 | stop_id = m_section_load_history.GetLastStopID(); |
| 3262 | if (m_section_load_history.SetSectionLoadAddress( |
| 3263 | stop_id, section_sp, load_addr: new_section_load_addr, warn_multiple)) |
| 3264 | return true; // Return true if the section load address was changed... |
| 3265 | } |
| 3266 | return false; // Return false to indicate nothing changed |
| 3267 | } |
| 3268 | |
| 3269 | size_t Target::UnloadModuleSections(const ModuleList &module_list) { |
| 3270 | size_t section_unload_count = 0; |
| 3271 | size_t num_modules = module_list.GetSize(); |
| 3272 | for (size_t i = 0; i < num_modules; ++i) { |
| 3273 | section_unload_count += |
| 3274 | UnloadModuleSections(module_sp: module_list.GetModuleAtIndex(idx: i)); |
| 3275 | } |
| 3276 | return section_unload_count; |
| 3277 | } |
| 3278 | |
| 3279 | size_t Target::UnloadModuleSections(const lldb::ModuleSP &module_sp) { |
| 3280 | uint32_t stop_id = 0; |
| 3281 | ProcessSP process_sp(GetProcessSP()); |
| 3282 | if (process_sp) |
| 3283 | stop_id = process_sp->GetStopID(); |
| 3284 | else |
| 3285 | stop_id = m_section_load_history.GetLastStopID(); |
| 3286 | SectionList *sections = module_sp->GetSectionList(); |
| 3287 | size_t section_unload_count = 0; |
| 3288 | if (sections) { |
| 3289 | const uint32_t num_sections = sections->GetNumSections(depth: 0); |
| 3290 | for (uint32_t i = 0; i < num_sections; ++i) { |
| 3291 | section_unload_count += m_section_load_history.SetSectionUnloaded( |
| 3292 | stop_id, section_sp: sections->GetSectionAtIndex(idx: i)); |
| 3293 | } |
| 3294 | } |
| 3295 | return section_unload_count; |
| 3296 | } |
| 3297 | |
| 3298 | bool Target::SetSectionUnloaded(const lldb::SectionSP §ion_sp) { |
| 3299 | uint32_t stop_id = 0; |
| 3300 | ProcessSP process_sp(GetProcessSP()); |
| 3301 | if (process_sp) |
| 3302 | stop_id = process_sp->GetStopID(); |
| 3303 | else |
| 3304 | stop_id = m_section_load_history.GetLastStopID(); |
| 3305 | return m_section_load_history.SetSectionUnloaded(stop_id, section_sp); |
| 3306 | } |
| 3307 | |
| 3308 | bool Target::SetSectionUnloaded(const lldb::SectionSP §ion_sp, |
| 3309 | addr_t load_addr) { |
| 3310 | uint32_t stop_id = 0; |
| 3311 | ProcessSP process_sp(GetProcessSP()); |
| 3312 | if (process_sp) |
| 3313 | stop_id = process_sp->GetStopID(); |
| 3314 | else |
| 3315 | stop_id = m_section_load_history.GetLastStopID(); |
| 3316 | return m_section_load_history.SetSectionUnloaded(stop_id, section_sp, |
| 3317 | load_addr); |
| 3318 | } |
| 3319 | |
| 3320 | void Target::ClearAllLoadedSections() { m_section_load_history.Clear(); } |
| 3321 | |
| 3322 | lldb_private::SummaryStatisticsSP Target::GetSummaryStatisticsSPForProviderName( |
| 3323 | lldb_private::TypeSummaryImpl &summary_provider) { |
| 3324 | return m_summary_statistics_cache.GetSummaryStatisticsForProvider( |
| 3325 | provider&: summary_provider); |
| 3326 | } |
| 3327 | |
| 3328 | SummaryStatisticsCache &Target::GetSummaryStatisticsCache() { |
| 3329 | return m_summary_statistics_cache; |
| 3330 | } |
| 3331 | |
| 3332 | void Target::SaveScriptedLaunchInfo(lldb_private::ProcessInfo &process_info) { |
| 3333 | if (process_info.IsScriptedProcess()) { |
| 3334 | // Only copy scripted process launch options. |
| 3335 | ProcessLaunchInfo &default_launch_info = const_cast<ProcessLaunchInfo &>( |
| 3336 | GetGlobalProperties().GetProcessLaunchInfo()); |
| 3337 | default_launch_info.SetProcessPluginName("ScriptedProcess" ); |
| 3338 | default_launch_info.SetScriptedMetadata(process_info.GetScriptedMetadata()); |
| 3339 | SetProcessLaunchInfo(default_launch_info); |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | Status Target::Launch(ProcessLaunchInfo &launch_info, Stream *stream) { |
| 3344 | m_stats.SetLaunchOrAttachTime(); |
| 3345 | Status error; |
| 3346 | Log *log = GetLog(mask: LLDBLog::Target); |
| 3347 | |
| 3348 | LLDB_LOGF(log, "Target::%s() called for %s" , __FUNCTION__, |
| 3349 | launch_info.GetExecutableFile().GetPath().c_str()); |
| 3350 | |
| 3351 | StateType state = eStateInvalid; |
| 3352 | |
| 3353 | // Scope to temporarily get the process state in case someone has manually |
| 3354 | // remotely connected already to a process and we can skip the platform |
| 3355 | // launching. |
| 3356 | { |
| 3357 | ProcessSP process_sp(GetProcessSP()); |
| 3358 | |
| 3359 | if (process_sp) { |
| 3360 | state = process_sp->GetState(); |
| 3361 | LLDB_LOGF(log, |
| 3362 | "Target::%s the process exists, and its current state is %s" , |
| 3363 | __FUNCTION__, StateAsCString(state)); |
| 3364 | } else { |
| 3365 | LLDB_LOGF(log, "Target::%s the process instance doesn't currently exist." , |
| 3366 | __FUNCTION__); |
| 3367 | } |
| 3368 | } |
| 3369 | |
| 3370 | launch_info.GetFlags().Set(eLaunchFlagDebug); |
| 3371 | |
| 3372 | SaveScriptedLaunchInfo(process_info&: launch_info); |
| 3373 | |
| 3374 | // Get the value of synchronous execution here. If you wait till after you |
| 3375 | // have started to run, then you could have hit a breakpoint, whose command |
| 3376 | // might switch the value, and then you'll pick up that incorrect value. |
| 3377 | Debugger &debugger = GetDebugger(); |
| 3378 | const bool synchronous_execution = |
| 3379 | debugger.GetCommandInterpreter().GetSynchronous(); |
| 3380 | |
| 3381 | PlatformSP platform_sp(GetPlatform()); |
| 3382 | |
| 3383 | FinalizeFileActions(info&: launch_info); |
| 3384 | |
| 3385 | if (state == eStateConnected) { |
| 3386 | if (launch_info.GetFlags().Test(bit: eLaunchFlagLaunchInTTY)) |
| 3387 | return Status::FromErrorString( |
| 3388 | str: "can't launch in tty when launching through a remote connection" ); |
| 3389 | } |
| 3390 | |
| 3391 | if (!launch_info.GetArchitecture().IsValid()) |
| 3392 | launch_info.GetArchitecture() = GetArchitecture(); |
| 3393 | |
| 3394 | // Hijacking events of the process to be created to be sure that all events |
| 3395 | // until the first stop are intercepted (in case if platform doesn't define |
| 3396 | // its own hijacking listener or if the process is created by the target |
| 3397 | // manually, without the platform). |
| 3398 | if (!launch_info.GetHijackListener()) |
| 3399 | launch_info.SetHijackListener(Listener::MakeListener( |
| 3400 | name: Process::LaunchSynchronousHijackListenerName.data())); |
| 3401 | |
| 3402 | // If we're not already connected to the process, and if we have a platform |
| 3403 | // that can launch a process for debugging, go ahead and do that here. |
| 3404 | if (state != eStateConnected && platform_sp && |
| 3405 | platform_sp->CanDebugProcess() && !launch_info.IsScriptedProcess()) { |
| 3406 | LLDB_LOGF(log, "Target::%s asking the platform to debug the process" , |
| 3407 | __FUNCTION__); |
| 3408 | |
| 3409 | // If there was a previous process, delete it before we make the new one. |
| 3410 | // One subtle point, we delete the process before we release the reference |
| 3411 | // to m_process_sp. That way even if we are the last owner, the process |
| 3412 | // will get Finalized before it gets destroyed. |
| 3413 | DeleteCurrentProcess(); |
| 3414 | |
| 3415 | m_process_sp = |
| 3416 | GetPlatform()->DebugProcess(launch_info, debugger, target&: *this, error); |
| 3417 | |
| 3418 | } else { |
| 3419 | LLDB_LOGF(log, |
| 3420 | "Target::%s the platform doesn't know how to debug a " |
| 3421 | "process, getting a process plugin to do this for us." , |
| 3422 | __FUNCTION__); |
| 3423 | |
| 3424 | if (state == eStateConnected) { |
| 3425 | assert(m_process_sp); |
| 3426 | } else { |
| 3427 | // Use a Process plugin to construct the process. |
| 3428 | CreateProcess(listener_sp: launch_info.GetListener(), |
| 3429 | plugin_name: launch_info.GetProcessPluginName(), crash_file: nullptr, can_connect: false); |
| 3430 | } |
| 3431 | |
| 3432 | // Since we didn't have a platform launch the process, launch it here. |
| 3433 | if (m_process_sp) { |
| 3434 | m_process_sp->HijackProcessEvents(listener_sp: launch_info.GetHijackListener()); |
| 3435 | m_process_sp->SetShadowListener(launch_info.GetShadowListener()); |
| 3436 | error = m_process_sp->Launch(launch_info); |
| 3437 | } |
| 3438 | } |
| 3439 | |
| 3440 | if (!error.Success()) |
| 3441 | return error; |
| 3442 | |
| 3443 | if (!m_process_sp) |
| 3444 | return Status::FromErrorString(str: "failed to launch or debug process" ); |
| 3445 | |
| 3446 | bool rebroadcast_first_stop = |
| 3447 | !synchronous_execution && |
| 3448 | launch_info.GetFlags().Test(bit: eLaunchFlagStopAtEntry); |
| 3449 | |
| 3450 | assert(launch_info.GetHijackListener()); |
| 3451 | |
| 3452 | EventSP first_stop_event_sp; |
| 3453 | state = m_process_sp->WaitForProcessToStop(timeout: std::nullopt, event_sp_ptr: &first_stop_event_sp, |
| 3454 | wait_always: rebroadcast_first_stop, |
| 3455 | hijack_listener: launch_info.GetHijackListener()); |
| 3456 | m_process_sp->RestoreProcessEvents(); |
| 3457 | |
| 3458 | if (rebroadcast_first_stop) { |
| 3459 | // We don't need to run the stop hooks by hand here, they will get |
| 3460 | // triggered when this rebroadcast event gets fetched. |
| 3461 | assert(first_stop_event_sp); |
| 3462 | m_process_sp->BroadcastEvent(event_sp&: first_stop_event_sp); |
| 3463 | return error; |
| 3464 | } |
| 3465 | // Run the stop hooks that want to run at entry. |
| 3466 | RunStopHooks(at_initial_stop: true /* at entry point */); |
| 3467 | |
| 3468 | switch (state) { |
| 3469 | case eStateStopped: { |
| 3470 | if (launch_info.GetFlags().Test(bit: eLaunchFlagStopAtEntry)) |
| 3471 | break; |
| 3472 | if (synchronous_execution) |
| 3473 | // Now we have handled the stop-from-attach, and we are just |
| 3474 | // switching to a synchronous resume. So we should switch to the |
| 3475 | // SyncResume hijacker. |
| 3476 | m_process_sp->ResumeSynchronous(stream); |
| 3477 | else |
| 3478 | error = m_process_sp->Resume(); |
| 3479 | if (!error.Success()) { |
| 3480 | error = Status::FromErrorStringWithFormat( |
| 3481 | format: "process resume at entry point failed: %s" , error.AsCString()); |
| 3482 | } |
| 3483 | } break; |
| 3484 | case eStateExited: { |
| 3485 | bool with_shell = !!launch_info.GetShell(); |
| 3486 | const int exit_status = m_process_sp->GetExitStatus(); |
| 3487 | const char *exit_desc = m_process_sp->GetExitDescription(); |
| 3488 | std::string desc; |
| 3489 | if (exit_desc && exit_desc[0]) |
| 3490 | desc = " (" + std::string(exit_desc) + ')'; |
| 3491 | if (with_shell) |
| 3492 | error = Status::FromErrorStringWithFormat( |
| 3493 | format: "process exited with status %i%s\n" |
| 3494 | "'r' and 'run' are aliases that default to launching through a " |
| 3495 | "shell.\n" |
| 3496 | "Try launching without going through a shell by using " |
| 3497 | "'process launch'." , |
| 3498 | exit_status, desc.c_str()); |
| 3499 | else |
| 3500 | error = Status::FromErrorStringWithFormat( |
| 3501 | format: "process exited with status %i%s" , exit_status, desc.c_str()); |
| 3502 | } break; |
| 3503 | default: |
| 3504 | error = Status::FromErrorStringWithFormat( |
| 3505 | format: "initial process state wasn't stopped: %s" , StateAsCString(state)); |
| 3506 | break; |
| 3507 | } |
| 3508 | return error; |
| 3509 | } |
| 3510 | |
| 3511 | void Target::SetTrace(const TraceSP &trace_sp) { m_trace_sp = trace_sp; } |
| 3512 | |
| 3513 | TraceSP Target::GetTrace() { return m_trace_sp; } |
| 3514 | |
| 3515 | llvm::Expected<TraceSP> Target::CreateTrace() { |
| 3516 | if (!m_process_sp) |
| 3517 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 3518 | S: "A process is required for tracing" ); |
| 3519 | if (m_trace_sp) |
| 3520 | return llvm::createStringError(EC: llvm::inconvertibleErrorCode(), |
| 3521 | S: "A trace already exists for the target" ); |
| 3522 | |
| 3523 | llvm::Expected<TraceSupportedResponse> trace_type = |
| 3524 | m_process_sp->TraceSupported(); |
| 3525 | if (!trace_type) |
| 3526 | return llvm::createStringError( |
| 3527 | EC: llvm::inconvertibleErrorCode(), Fmt: "Tracing is not supported. %s" , |
| 3528 | Vals: llvm::toString(E: trace_type.takeError()).c_str()); |
| 3529 | if (llvm::Expected<TraceSP> trace_sp = |
| 3530 | Trace::FindPluginForLiveProcess(plugin_name: trace_type->name, process&: *m_process_sp)) |
| 3531 | m_trace_sp = *trace_sp; |
| 3532 | else |
| 3533 | return llvm::createStringError( |
| 3534 | EC: llvm::inconvertibleErrorCode(), |
| 3535 | Fmt: "Couldn't create a Trace object for the process. %s" , |
| 3536 | Vals: llvm::toString(E: trace_sp.takeError()).c_str()); |
| 3537 | return m_trace_sp; |
| 3538 | } |
| 3539 | |
| 3540 | llvm::Expected<TraceSP> Target::GetTraceOrCreate() { |
| 3541 | if (m_trace_sp) |
| 3542 | return m_trace_sp; |
| 3543 | return CreateTrace(); |
| 3544 | } |
| 3545 | |
| 3546 | Status Target::Attach(ProcessAttachInfo &attach_info, Stream *stream) { |
| 3547 | m_stats.SetLaunchOrAttachTime(); |
| 3548 | auto state = eStateInvalid; |
| 3549 | auto process_sp = GetProcessSP(); |
| 3550 | if (process_sp) { |
| 3551 | state = process_sp->GetState(); |
| 3552 | if (process_sp->IsAlive() && state != eStateConnected) { |
| 3553 | if (state == eStateAttaching) |
| 3554 | return Status::FromErrorString(str: "process attach is in progress" ); |
| 3555 | return Status::FromErrorString(str: "a process is already being debugged" ); |
| 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | const ModuleSP old_exec_module_sp = GetExecutableModule(); |
| 3560 | |
| 3561 | // If no process info was specified, then use the target executable name as |
| 3562 | // the process to attach to by default |
| 3563 | if (!attach_info.ProcessInfoSpecified()) { |
| 3564 | if (old_exec_module_sp) |
| 3565 | attach_info.GetExecutableFile().SetFilename( |
| 3566 | old_exec_module_sp->GetPlatformFileSpec().GetFilename()); |
| 3567 | |
| 3568 | if (!attach_info.ProcessInfoSpecified()) { |
| 3569 | return Status::FromErrorString( |
| 3570 | str: "no process specified, create a target with a file, or " |
| 3571 | "specify the --pid or --name" ); |
| 3572 | } |
| 3573 | } |
| 3574 | |
| 3575 | const auto platform_sp = |
| 3576 | GetDebugger().GetPlatformList().GetSelectedPlatform(); |
| 3577 | ListenerSP hijack_listener_sp; |
| 3578 | const bool async = attach_info.GetAsync(); |
| 3579 | if (!async) { |
| 3580 | hijack_listener_sp = Listener::MakeListener( |
| 3581 | name: Process::AttachSynchronousHijackListenerName.data()); |
| 3582 | attach_info.SetHijackListener(hijack_listener_sp); |
| 3583 | } |
| 3584 | |
| 3585 | Status error; |
| 3586 | if (state != eStateConnected && platform_sp != nullptr && |
| 3587 | platform_sp->CanDebugProcess() && !attach_info.IsScriptedProcess()) { |
| 3588 | SetPlatform(platform_sp); |
| 3589 | process_sp = platform_sp->Attach(attach_info, debugger&: GetDebugger(), target: this, error); |
| 3590 | } else { |
| 3591 | if (state != eStateConnected) { |
| 3592 | SaveScriptedLaunchInfo(process_info&: attach_info); |
| 3593 | llvm::StringRef plugin_name = attach_info.GetProcessPluginName(); |
| 3594 | process_sp = |
| 3595 | CreateProcess(listener_sp: attach_info.GetListenerForProcess(debugger&: GetDebugger()), |
| 3596 | plugin_name, crash_file: nullptr, can_connect: false); |
| 3597 | if (!process_sp) { |
| 3598 | error = Status::FromErrorStringWithFormatv( |
| 3599 | format: "failed to create process using plugin '{0}'" , |
| 3600 | args: plugin_name.empty() ? "<empty>" : plugin_name); |
| 3601 | return error; |
| 3602 | } |
| 3603 | } |
| 3604 | if (hijack_listener_sp) |
| 3605 | process_sp->HijackProcessEvents(listener_sp: hijack_listener_sp); |
| 3606 | error = process_sp->Attach(attach_info); |
| 3607 | } |
| 3608 | |
| 3609 | if (error.Success() && process_sp) { |
| 3610 | if (async) { |
| 3611 | process_sp->RestoreProcessEvents(); |
| 3612 | } else { |
| 3613 | // We are stopping all the way out to the user, so update selected frames. |
| 3614 | state = process_sp->WaitForProcessToStop( |
| 3615 | timeout: std::nullopt, event_sp_ptr: nullptr, wait_always: false, hijack_listener: attach_info.GetHijackListener(), stream, |
| 3616 | use_run_lock: true, select_most_relevant: SelectMostRelevantFrame); |
| 3617 | process_sp->RestoreProcessEvents(); |
| 3618 | |
| 3619 | // Run the stop hooks here. Since we were hijacking the events, they |
| 3620 | // wouldn't have gotten run as part of event delivery. |
| 3621 | RunStopHooks(/* at_initial_stop= */ true); |
| 3622 | |
| 3623 | if (state != eStateStopped) { |
| 3624 | const char *exit_desc = process_sp->GetExitDescription(); |
| 3625 | if (exit_desc) |
| 3626 | error = Status::FromErrorStringWithFormat(format: "%s" , exit_desc); |
| 3627 | else |
| 3628 | error = Status::FromErrorString( |
| 3629 | str: "process did not stop (no such process or permission problem?)" ); |
| 3630 | process_sp->Destroy(force_kill: false); |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | return error; |
| 3635 | } |
| 3636 | |
| 3637 | void Target::FinalizeFileActions(ProcessLaunchInfo &info) { |
| 3638 | Log *log = GetLog(mask: LLDBLog::Process); |
| 3639 | |
| 3640 | // Finalize the file actions, and if none were given, default to opening up a |
| 3641 | // pseudo terminal |
| 3642 | PlatformSP platform_sp = GetPlatform(); |
| 3643 | const bool default_to_use_pty = |
| 3644 | m_platform_sp ? m_platform_sp->IsHost() : false; |
| 3645 | LLDB_LOG( |
| 3646 | log, |
| 3647 | "have platform={0}, platform_sp->IsHost()={1}, default_to_use_pty={2}" , |
| 3648 | bool(platform_sp), |
| 3649 | platform_sp ? (platform_sp->IsHost() ? "true" : "false" ) : "n/a" , |
| 3650 | default_to_use_pty); |
| 3651 | |
| 3652 | // If nothing for stdin or stdout or stderr was specified, then check the |
| 3653 | // process for any default settings that were set with "settings set" |
| 3654 | if (info.GetFileActionForFD(STDIN_FILENO) == nullptr || |
| 3655 | info.GetFileActionForFD(STDOUT_FILENO) == nullptr || |
| 3656 | info.GetFileActionForFD(STDERR_FILENO) == nullptr) { |
| 3657 | LLDB_LOG(log, "at least one of stdin/stdout/stderr was not set, evaluating " |
| 3658 | "default handling" ); |
| 3659 | |
| 3660 | if (info.GetFlags().Test(bit: eLaunchFlagLaunchInTTY)) { |
| 3661 | // Do nothing, if we are launching in a remote terminal no file actions |
| 3662 | // should be done at all. |
| 3663 | return; |
| 3664 | } |
| 3665 | |
| 3666 | if (info.GetFlags().Test(bit: eLaunchFlagDisableSTDIO)) { |
| 3667 | LLDB_LOG(log, "eLaunchFlagDisableSTDIO set, adding suppression action " |
| 3668 | "for stdin, stdout and stderr" ); |
| 3669 | info.AppendSuppressFileAction(STDIN_FILENO, read: true, write: false); |
| 3670 | info.AppendSuppressFileAction(STDOUT_FILENO, read: false, write: true); |
| 3671 | info.AppendSuppressFileAction(STDERR_FILENO, read: false, write: true); |
| 3672 | } else { |
| 3673 | // Check for any values that might have gotten set with any of: (lldb) |
| 3674 | // settings set target.input-path (lldb) settings set target.output-path |
| 3675 | // (lldb) settings set target.error-path |
| 3676 | FileSpec in_file_spec; |
| 3677 | FileSpec out_file_spec; |
| 3678 | FileSpec err_file_spec; |
| 3679 | // Only override with the target settings if we don't already have an |
| 3680 | // action for in, out or error |
| 3681 | if (info.GetFileActionForFD(STDIN_FILENO) == nullptr) |
| 3682 | in_file_spec = GetStandardInputPath(); |
| 3683 | if (info.GetFileActionForFD(STDOUT_FILENO) == nullptr) |
| 3684 | out_file_spec = GetStandardOutputPath(); |
| 3685 | if (info.GetFileActionForFD(STDERR_FILENO) == nullptr) |
| 3686 | err_file_spec = GetStandardErrorPath(); |
| 3687 | |
| 3688 | LLDB_LOG(log, "target stdin='{0}', target stdout='{1}', stderr='{2}'" , |
| 3689 | in_file_spec, out_file_spec, err_file_spec); |
| 3690 | |
| 3691 | if (in_file_spec) { |
| 3692 | info.AppendOpenFileAction(STDIN_FILENO, file_spec: in_file_spec, read: true, write: false); |
| 3693 | LLDB_LOG(log, "appended stdin open file action for {0}" , in_file_spec); |
| 3694 | } |
| 3695 | |
| 3696 | if (out_file_spec) { |
| 3697 | info.AppendOpenFileAction(STDOUT_FILENO, file_spec: out_file_spec, read: false, write: true); |
| 3698 | LLDB_LOG(log, "appended stdout open file action for {0}" , |
| 3699 | out_file_spec); |
| 3700 | } |
| 3701 | |
| 3702 | if (err_file_spec) { |
| 3703 | info.AppendOpenFileAction(STDERR_FILENO, file_spec: err_file_spec, read: false, write: true); |
| 3704 | LLDB_LOG(log, "appended stderr open file action for {0}" , |
| 3705 | err_file_spec); |
| 3706 | } |
| 3707 | |
| 3708 | if (default_to_use_pty) { |
| 3709 | llvm::Error Err = info.SetUpPtyRedirection(); |
| 3710 | LLDB_LOG_ERROR(log, std::move(Err), "SetUpPtyRedirection failed: {0}" ); |
| 3711 | } |
| 3712 | } |
| 3713 | } |
| 3714 | } |
| 3715 | |
| 3716 | void Target::AddDummySignal(llvm::StringRef name, LazyBool pass, LazyBool notify, |
| 3717 | LazyBool stop) { |
| 3718 | if (name.empty()) |
| 3719 | return; |
| 3720 | // Don't add a signal if all the actions are trivial: |
| 3721 | if (pass == eLazyBoolCalculate && notify == eLazyBoolCalculate |
| 3722 | && stop == eLazyBoolCalculate) |
| 3723 | return; |
| 3724 | |
| 3725 | auto& elem = m_dummy_signals[name]; |
| 3726 | elem.pass = pass; |
| 3727 | elem.notify = notify; |
| 3728 | elem.stop = stop; |
| 3729 | } |
| 3730 | |
| 3731 | bool Target::UpdateSignalFromDummy(UnixSignalsSP signals_sp, |
| 3732 | const DummySignalElement &elem) { |
| 3733 | if (!signals_sp) |
| 3734 | return false; |
| 3735 | |
| 3736 | int32_t signo |
| 3737 | = signals_sp->GetSignalNumberFromName(name: elem.first().str().c_str()); |
| 3738 | if (signo == LLDB_INVALID_SIGNAL_NUMBER) |
| 3739 | return false; |
| 3740 | |
| 3741 | if (elem.second.pass == eLazyBoolYes) |
| 3742 | signals_sp->SetShouldSuppress(signo, value: false); |
| 3743 | else if (elem.second.pass == eLazyBoolNo) |
| 3744 | signals_sp->SetShouldSuppress(signo, value: true); |
| 3745 | |
| 3746 | if (elem.second.notify == eLazyBoolYes) |
| 3747 | signals_sp->SetShouldNotify(signo, value: true); |
| 3748 | else if (elem.second.notify == eLazyBoolNo) |
| 3749 | signals_sp->SetShouldNotify(signo, value: false); |
| 3750 | |
| 3751 | if (elem.second.stop == eLazyBoolYes) |
| 3752 | signals_sp->SetShouldStop(signo, value: true); |
| 3753 | else if (elem.second.stop == eLazyBoolNo) |
| 3754 | signals_sp->SetShouldStop(signo, value: false); |
| 3755 | return true; |
| 3756 | } |
| 3757 | |
| 3758 | bool Target::ResetSignalFromDummy(UnixSignalsSP signals_sp, |
| 3759 | const DummySignalElement &elem) { |
| 3760 | if (!signals_sp) |
| 3761 | return false; |
| 3762 | int32_t signo |
| 3763 | = signals_sp->GetSignalNumberFromName(name: elem.first().str().c_str()); |
| 3764 | if (signo == LLDB_INVALID_SIGNAL_NUMBER) |
| 3765 | return false; |
| 3766 | bool do_pass = elem.second.pass != eLazyBoolCalculate; |
| 3767 | bool do_stop = elem.second.stop != eLazyBoolCalculate; |
| 3768 | bool do_notify = elem.second.notify != eLazyBoolCalculate; |
| 3769 | signals_sp->ResetSignal(signo, reset_stop: do_stop, reset_notify: do_notify, reset_suppress: do_pass); |
| 3770 | return true; |
| 3771 | } |
| 3772 | |
| 3773 | void Target::UpdateSignalsFromDummy(UnixSignalsSP signals_sp, |
| 3774 | StreamSP warning_stream_sp) { |
| 3775 | if (!signals_sp) |
| 3776 | return; |
| 3777 | |
| 3778 | for (const auto &elem : m_dummy_signals) { |
| 3779 | if (!UpdateSignalFromDummy(signals_sp, elem)) |
| 3780 | warning_stream_sp->Printf(format: "Target signal '%s' not found in process\n" , |
| 3781 | elem.first().str().c_str()); |
| 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | void Target::ClearDummySignals(Args &signal_names) { |
| 3786 | ProcessSP process_sp = GetProcessSP(); |
| 3787 | // The simplest case, delete them all with no process to update. |
| 3788 | if (signal_names.GetArgumentCount() == 0 && !process_sp) { |
| 3789 | m_dummy_signals.clear(); |
| 3790 | return; |
| 3791 | } |
| 3792 | UnixSignalsSP signals_sp; |
| 3793 | if (process_sp) |
| 3794 | signals_sp = process_sp->GetUnixSignals(); |
| 3795 | |
| 3796 | for (const Args::ArgEntry &entry : signal_names) { |
| 3797 | const char *signal_name = entry.c_str(); |
| 3798 | auto elem = m_dummy_signals.find(Key: signal_name); |
| 3799 | // If we didn't find it go on. |
| 3800 | // FIXME: Should I pipe error handling through here? |
| 3801 | if (elem == m_dummy_signals.end()) { |
| 3802 | continue; |
| 3803 | } |
| 3804 | if (signals_sp) |
| 3805 | ResetSignalFromDummy(signals_sp, elem: *elem); |
| 3806 | m_dummy_signals.erase(I: elem); |
| 3807 | } |
| 3808 | } |
| 3809 | |
| 3810 | void Target::PrintDummySignals(Stream &strm, Args &signal_args) { |
| 3811 | strm.Printf(format: "NAME PASS STOP NOTIFY\n" ); |
| 3812 | strm.Printf(format: "=========== ======= ======= =======\n" ); |
| 3813 | |
| 3814 | auto str_for_lazy = [] (LazyBool lazy) -> const char * { |
| 3815 | switch (lazy) { |
| 3816 | case eLazyBoolCalculate: return "not set" ; |
| 3817 | case eLazyBoolYes: return "true " ; |
| 3818 | case eLazyBoolNo: return "false " ; |
| 3819 | } |
| 3820 | llvm_unreachable("Fully covered switch above!" ); |
| 3821 | }; |
| 3822 | size_t num_args = signal_args.GetArgumentCount(); |
| 3823 | for (const auto &elem : m_dummy_signals) { |
| 3824 | bool print_it = false; |
| 3825 | for (size_t idx = 0; idx < num_args; idx++) { |
| 3826 | if (elem.first() == signal_args.GetArgumentAtIndex(idx)) { |
| 3827 | print_it = true; |
| 3828 | break; |
| 3829 | } |
| 3830 | } |
| 3831 | if (print_it) { |
| 3832 | strm.Printf(format: "%-11s " , elem.first().str().c_str()); |
| 3833 | strm.Printf(format: "%s %s %s\n" , str_for_lazy(elem.second.pass), |
| 3834 | str_for_lazy(elem.second.stop), |
| 3835 | str_for_lazy(elem.second.notify)); |
| 3836 | } |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | // Target::StopHook |
| 3841 | Target::StopHook::StopHook(lldb::TargetSP target_sp, lldb::user_id_t uid) |
| 3842 | : UserID(uid), m_target_sp(target_sp), m_specifier_sp(), |
| 3843 | m_thread_spec_up() {} |
| 3844 | |
| 3845 | Target::StopHook::StopHook(const StopHook &rhs) |
| 3846 | : UserID(rhs.GetID()), m_target_sp(rhs.m_target_sp), |
| 3847 | m_specifier_sp(rhs.m_specifier_sp), m_thread_spec_up(), |
| 3848 | m_active(rhs.m_active), m_auto_continue(rhs.m_auto_continue) { |
| 3849 | if (rhs.m_thread_spec_up) |
| 3850 | m_thread_spec_up = std::make_unique<ThreadSpec>(args&: *rhs.m_thread_spec_up); |
| 3851 | } |
| 3852 | |
| 3853 | void Target::StopHook::SetSpecifier(SymbolContextSpecifier *specifier) { |
| 3854 | m_specifier_sp.reset(p: specifier); |
| 3855 | } |
| 3856 | |
| 3857 | void Target::StopHook::SetThreadSpecifier(ThreadSpec *specifier) { |
| 3858 | m_thread_spec_up.reset(p: specifier); |
| 3859 | } |
| 3860 | |
| 3861 | bool Target::StopHook::ExecutionContextPasses(const ExecutionContext &exc_ctx) { |
| 3862 | SymbolContextSpecifier *specifier = GetSpecifier(); |
| 3863 | if (!specifier) |
| 3864 | return true; |
| 3865 | |
| 3866 | bool will_run = true; |
| 3867 | if (exc_ctx.GetFramePtr()) |
| 3868 | will_run = GetSpecifier()->SymbolContextMatches( |
| 3869 | sc: exc_ctx.GetFramePtr()->GetSymbolContext(resolve_scope: eSymbolContextEverything)); |
| 3870 | if (will_run && GetThreadSpecifier() != nullptr) |
| 3871 | will_run = |
| 3872 | GetThreadSpecifier()->ThreadPassesBasicTests(thread&: exc_ctx.GetThreadRef()); |
| 3873 | |
| 3874 | return will_run; |
| 3875 | } |
| 3876 | |
| 3877 | void Target::StopHook::GetDescription(Stream &s, |
| 3878 | lldb::DescriptionLevel level) const { |
| 3879 | |
| 3880 | // For brief descriptions, only print the subclass description: |
| 3881 | if (level == eDescriptionLevelBrief) { |
| 3882 | GetSubclassDescription(s, level); |
| 3883 | return; |
| 3884 | } |
| 3885 | |
| 3886 | unsigned indent_level = s.GetIndentLevel(); |
| 3887 | |
| 3888 | s.SetIndentLevel(indent_level + 2); |
| 3889 | |
| 3890 | s.Printf(format: "Hook: %" PRIu64 "\n" , GetID()); |
| 3891 | if (m_active) |
| 3892 | s.Indent(s: "State: enabled\n" ); |
| 3893 | else |
| 3894 | s.Indent(s: "State: disabled\n" ); |
| 3895 | |
| 3896 | if (m_auto_continue) |
| 3897 | s.Indent(s: "AutoContinue on\n" ); |
| 3898 | |
| 3899 | if (m_specifier_sp) { |
| 3900 | s.Indent(); |
| 3901 | s.PutCString(cstr: "Specifier:\n" ); |
| 3902 | s.SetIndentLevel(indent_level + 4); |
| 3903 | m_specifier_sp->GetDescription(s: &s, level); |
| 3904 | s.SetIndentLevel(indent_level + 2); |
| 3905 | } |
| 3906 | |
| 3907 | if (m_thread_spec_up) { |
| 3908 | StreamString tmp; |
| 3909 | s.Indent(s: "Thread:\n" ); |
| 3910 | m_thread_spec_up->GetDescription(s: &tmp, level); |
| 3911 | s.SetIndentLevel(indent_level + 4); |
| 3912 | s.Indent(s: tmp.GetString()); |
| 3913 | s.PutCString(cstr: "\n" ); |
| 3914 | s.SetIndentLevel(indent_level + 2); |
| 3915 | } |
| 3916 | GetSubclassDescription(s, level); |
| 3917 | } |
| 3918 | |
| 3919 | void Target::StopHookCommandLine::GetSubclassDescription( |
| 3920 | Stream &s, lldb::DescriptionLevel level) const { |
| 3921 | // The brief description just prints the first command. |
| 3922 | if (level == eDescriptionLevelBrief) { |
| 3923 | if (m_commands.GetSize() == 1) |
| 3924 | s.PutCString(cstr: m_commands.GetStringAtIndex(idx: 0)); |
| 3925 | return; |
| 3926 | } |
| 3927 | s.Indent(s: "Commands: \n" ); |
| 3928 | s.SetIndentLevel(s.GetIndentLevel() + 4); |
| 3929 | uint32_t num_commands = m_commands.GetSize(); |
| 3930 | for (uint32_t i = 0; i < num_commands; i++) { |
| 3931 | s.Indent(s: m_commands.GetStringAtIndex(idx: i)); |
| 3932 | s.PutCString(cstr: "\n" ); |
| 3933 | } |
| 3934 | s.SetIndentLevel(s.GetIndentLevel() - 4); |
| 3935 | } |
| 3936 | |
| 3937 | // Target::StopHookCommandLine |
| 3938 | void Target::StopHookCommandLine::SetActionFromString(const std::string &string) { |
| 3939 | GetCommands().SplitIntoLines(lines: string); |
| 3940 | } |
| 3941 | |
| 3942 | void Target::StopHookCommandLine::SetActionFromStrings( |
| 3943 | const std::vector<std::string> &strings) { |
| 3944 | for (auto string : strings) |
| 3945 | GetCommands().AppendString(str: string.c_str()); |
| 3946 | } |
| 3947 | |
| 3948 | Target::StopHook::StopHookResult |
| 3949 | Target::StopHookCommandLine::HandleStop(ExecutionContext &exc_ctx, |
| 3950 | StreamSP output_sp) { |
| 3951 | assert(exc_ctx.GetTargetPtr() && "Can't call PerformAction on a context " |
| 3952 | "with no target" ); |
| 3953 | |
| 3954 | if (!m_commands.GetSize()) |
| 3955 | return StopHookResult::KeepStopped; |
| 3956 | |
| 3957 | CommandReturnObject result(false); |
| 3958 | result.SetImmediateOutputStream(output_sp); |
| 3959 | result.SetInteractive(false); |
| 3960 | Debugger &debugger = exc_ctx.GetTargetPtr()->GetDebugger(); |
| 3961 | CommandInterpreterRunOptions options; |
| 3962 | options.SetStopOnContinue(true); |
| 3963 | options.SetStopOnError(true); |
| 3964 | options.SetEchoCommands(false); |
| 3965 | options.SetPrintResults(true); |
| 3966 | options.SetPrintErrors(true); |
| 3967 | options.SetAddToHistory(false); |
| 3968 | |
| 3969 | // Force Async: |
| 3970 | bool old_async = debugger.GetAsyncExecution(); |
| 3971 | debugger.SetAsyncExecution(true); |
| 3972 | debugger.GetCommandInterpreter().HandleCommands(commands: GetCommands(), context: exc_ctx, |
| 3973 | options, result); |
| 3974 | debugger.SetAsyncExecution(old_async); |
| 3975 | lldb::ReturnStatus status = result.GetStatus(); |
| 3976 | if (status == eReturnStatusSuccessContinuingNoResult || |
| 3977 | status == eReturnStatusSuccessContinuingResult) |
| 3978 | return StopHookResult::AlreadyContinued; |
| 3979 | return StopHookResult::KeepStopped; |
| 3980 | } |
| 3981 | |
| 3982 | // Target::StopHookScripted |
| 3983 | Status Target::StopHookScripted::SetScriptCallback( |
| 3984 | std::string class_name, StructuredData::ObjectSP ) { |
| 3985 | Status error; |
| 3986 | |
| 3987 | ScriptInterpreter *script_interp = |
| 3988 | GetTarget()->GetDebugger().GetScriptInterpreter(); |
| 3989 | if (!script_interp) { |
| 3990 | error = Status::FromErrorString(str: "No script interpreter installed." ); |
| 3991 | return error; |
| 3992 | } |
| 3993 | |
| 3994 | m_interface_sp = script_interp->CreateScriptedStopHookInterface(); |
| 3995 | if (!m_interface_sp) { |
| 3996 | error = Status::FromErrorStringWithFormat( |
| 3997 | format: "ScriptedStopHook::%s () - ERROR: %s" , __FUNCTION__, |
| 3998 | "Script interpreter couldn't create Scripted Stop Hook Interface" ); |
| 3999 | return error; |
| 4000 | } |
| 4001 | |
| 4002 | m_class_name = class_name; |
| 4003 | m_extra_args.SetObjectSP(extra_args_sp); |
| 4004 | |
| 4005 | auto obj_or_err = m_interface_sp->CreatePluginObject( |
| 4006 | class_name: m_class_name, target_sp: GetTarget(), args_sp: m_extra_args); |
| 4007 | if (!obj_or_err) { |
| 4008 | return Status::FromError(error: obj_or_err.takeError()); |
| 4009 | } |
| 4010 | |
| 4011 | StructuredData::ObjectSP object_sp = *obj_or_err; |
| 4012 | if (!object_sp || !object_sp->IsValid()) { |
| 4013 | error = Status::FromErrorStringWithFormat( |
| 4014 | format: "ScriptedStopHook::%s () - ERROR: %s" , __FUNCTION__, |
| 4015 | "Failed to create valid script object" ); |
| 4016 | return error; |
| 4017 | } |
| 4018 | |
| 4019 | return {}; |
| 4020 | } |
| 4021 | |
| 4022 | Target::StopHook::StopHookResult |
| 4023 | Target::StopHookScripted::HandleStop(ExecutionContext &exc_ctx, |
| 4024 | StreamSP output_sp) { |
| 4025 | assert(exc_ctx.GetTargetPtr() && "Can't call HandleStop on a context " |
| 4026 | "with no target" ); |
| 4027 | |
| 4028 | if (!m_interface_sp) |
| 4029 | return StopHookResult::KeepStopped; |
| 4030 | |
| 4031 | lldb::StreamSP stream = std::make_shared<lldb_private::StreamString>(); |
| 4032 | auto should_stop_or_err = m_interface_sp->HandleStop(exe_ctx&: exc_ctx, output_sp&: stream); |
| 4033 | output_sp->PutCString( |
| 4034 | cstr: reinterpret_cast<StreamString *>(stream.get())->GetData()); |
| 4035 | if (!should_stop_or_err) |
| 4036 | return StopHookResult::KeepStopped; |
| 4037 | |
| 4038 | return *should_stop_or_err ? StopHookResult::KeepStopped |
| 4039 | : StopHookResult::RequestContinue; |
| 4040 | } |
| 4041 | |
| 4042 | void Target::StopHookScripted::GetSubclassDescription( |
| 4043 | Stream &s, lldb::DescriptionLevel level) const { |
| 4044 | if (level == eDescriptionLevelBrief) { |
| 4045 | s.PutCString(cstr: m_class_name); |
| 4046 | return; |
| 4047 | } |
| 4048 | s.Indent(s: "Class:" ); |
| 4049 | s.Printf(format: "%s\n" , m_class_name.c_str()); |
| 4050 | |
| 4051 | // Now print the extra args: |
| 4052 | // FIXME: We should use StructuredData.GetDescription on the m_extra_args |
| 4053 | // but that seems to rely on some printing plugin that doesn't exist. |
| 4054 | if (!m_extra_args.IsValid()) |
| 4055 | return; |
| 4056 | StructuredData::ObjectSP object_sp = m_extra_args.GetObjectSP(); |
| 4057 | if (!object_sp || !object_sp->IsValid()) |
| 4058 | return; |
| 4059 | |
| 4060 | StructuredData::Dictionary *as_dict = object_sp->GetAsDictionary(); |
| 4061 | if (!as_dict || !as_dict->IsValid()) |
| 4062 | return; |
| 4063 | |
| 4064 | uint32_t num_keys = as_dict->GetSize(); |
| 4065 | if (num_keys == 0) |
| 4066 | return; |
| 4067 | |
| 4068 | s.Indent(s: "Args:\n" ); |
| 4069 | s.SetIndentLevel(s.GetIndentLevel() + 4); |
| 4070 | |
| 4071 | auto print_one_element = [&s](llvm::StringRef key, |
| 4072 | StructuredData::Object *object) { |
| 4073 | s.Indent(); |
| 4074 | s.Format(format: "{0} : {1}\n" , args&: key, args: object->GetStringValue()); |
| 4075 | return true; |
| 4076 | }; |
| 4077 | |
| 4078 | as_dict->ForEach(callback: print_one_element); |
| 4079 | |
| 4080 | s.SetIndentLevel(s.GetIndentLevel() - 4); |
| 4081 | } |
| 4082 | |
| 4083 | static constexpr OptionEnumValueElement g_dynamic_value_types[] = { |
| 4084 | { |
| 4085 | .value: eNoDynamicValues, |
| 4086 | .string_value: "no-dynamic-values" , |
| 4087 | .usage: "Don't calculate the dynamic type of values" , |
| 4088 | }, |
| 4089 | { |
| 4090 | .value: eDynamicCanRunTarget, |
| 4091 | .string_value: "run-target" , |
| 4092 | .usage: "Calculate the dynamic type of values " |
| 4093 | "even if you have to run the target." , |
| 4094 | }, |
| 4095 | { |
| 4096 | .value: eDynamicDontRunTarget, |
| 4097 | .string_value: "no-run-target" , |
| 4098 | .usage: "Calculate the dynamic type of values, but don't run the target." , |
| 4099 | }, |
| 4100 | }; |
| 4101 | |
| 4102 | OptionEnumValues lldb_private::GetDynamicValueTypes() { |
| 4103 | return OptionEnumValues(g_dynamic_value_types); |
| 4104 | } |
| 4105 | |
| 4106 | static constexpr OptionEnumValueElement g_inline_breakpoint_enums[] = { |
| 4107 | { |
| 4108 | .value: eInlineBreakpointsNever, |
| 4109 | .string_value: "never" , |
| 4110 | .usage: "Never look for inline breakpoint locations (fastest). This setting " |
| 4111 | "should only be used if you know that no inlining occurs in your" |
| 4112 | "programs." , |
| 4113 | }, |
| 4114 | { |
| 4115 | .value: eInlineBreakpointsHeaders, |
| 4116 | .string_value: "headers" , |
| 4117 | .usage: "Only check for inline breakpoint locations when setting breakpoints " |
| 4118 | "in header files, but not when setting breakpoint in implementation " |
| 4119 | "source files (default)." , |
| 4120 | }, |
| 4121 | { |
| 4122 | .value: eInlineBreakpointsAlways, |
| 4123 | .string_value: "always" , |
| 4124 | .usage: "Always look for inline breakpoint locations when setting file and " |
| 4125 | "line breakpoints (slower but most accurate)." , |
| 4126 | }, |
| 4127 | }; |
| 4128 | |
| 4129 | enum x86DisassemblyFlavor { |
| 4130 | eX86DisFlavorDefault, |
| 4131 | eX86DisFlavorIntel, |
| 4132 | eX86DisFlavorATT |
| 4133 | }; |
| 4134 | |
| 4135 | static constexpr OptionEnumValueElement g_x86_dis_flavor_value_types[] = { |
| 4136 | { |
| 4137 | .value: eX86DisFlavorDefault, |
| 4138 | .string_value: "default" , |
| 4139 | .usage: "Disassembler default (currently att)." , |
| 4140 | }, |
| 4141 | { |
| 4142 | .value: eX86DisFlavorIntel, |
| 4143 | .string_value: "intel" , |
| 4144 | .usage: "Intel disassembler flavor." , |
| 4145 | }, |
| 4146 | { |
| 4147 | .value: eX86DisFlavorATT, |
| 4148 | .string_value: "att" , |
| 4149 | .usage: "AT&T disassembler flavor." , |
| 4150 | }, |
| 4151 | }; |
| 4152 | |
| 4153 | static constexpr OptionEnumValueElement g_import_std_module_value_types[] = { |
| 4154 | { |
| 4155 | .value: eImportStdModuleFalse, |
| 4156 | .string_value: "false" , |
| 4157 | .usage: "Never import the 'std' C++ module in the expression parser." , |
| 4158 | }, |
| 4159 | { |
| 4160 | .value: eImportStdModuleFallback, |
| 4161 | .string_value: "fallback" , |
| 4162 | .usage: "Retry evaluating expressions with an imported 'std' C++ module if they" |
| 4163 | " failed to parse without the module. This allows evaluating more " |
| 4164 | "complex expressions involving C++ standard library types." |
| 4165 | }, |
| 4166 | { |
| 4167 | .value: eImportStdModuleTrue, |
| 4168 | .string_value: "true" , |
| 4169 | .usage: "Always import the 'std' C++ module. This allows evaluating more " |
| 4170 | "complex expressions involving C++ standard library types. This feature" |
| 4171 | " is experimental." |
| 4172 | }, |
| 4173 | }; |
| 4174 | |
| 4175 | static constexpr OptionEnumValueElement |
| 4176 | g_dynamic_class_info_helper_value_types[] = { |
| 4177 | { |
| 4178 | .value: eDynamicClassInfoHelperAuto, |
| 4179 | .string_value: "auto" , |
| 4180 | .usage: "Automatically determine the most appropriate method for the " |
| 4181 | "target OS." , |
| 4182 | }, |
| 4183 | {.value: eDynamicClassInfoHelperRealizedClassesStruct, .string_value: "RealizedClassesStruct" , |
| 4184 | .usage: "Prefer using the realized classes struct." }, |
| 4185 | {.value: eDynamicClassInfoHelperCopyRealizedClassList, .string_value: "CopyRealizedClassList" , |
| 4186 | .usage: "Prefer using the CopyRealizedClassList API." }, |
| 4187 | {.value: eDynamicClassInfoHelperGetRealizedClassList, .string_value: "GetRealizedClassList" , |
| 4188 | .usage: "Prefer using the GetRealizedClassList API." }, |
| 4189 | }; |
| 4190 | |
| 4191 | static constexpr OptionEnumValueElement g_hex_immediate_style_values[] = { |
| 4192 | { |
| 4193 | .value: Disassembler::eHexStyleC, |
| 4194 | .string_value: "c" , |
| 4195 | .usage: "C-style (0xffff)." , |
| 4196 | }, |
| 4197 | { |
| 4198 | .value: Disassembler::eHexStyleAsm, |
| 4199 | .string_value: "asm" , |
| 4200 | .usage: "Asm-style (0ffffh)." , |
| 4201 | }, |
| 4202 | }; |
| 4203 | |
| 4204 | static constexpr OptionEnumValueElement g_load_script_from_sym_file_values[] = { |
| 4205 | { |
| 4206 | .value: eLoadScriptFromSymFileTrue, |
| 4207 | .string_value: "true" , |
| 4208 | .usage: "Load debug scripts inside symbol files" , |
| 4209 | }, |
| 4210 | { |
| 4211 | .value: eLoadScriptFromSymFileFalse, |
| 4212 | .string_value: "false" , |
| 4213 | .usage: "Do not load debug scripts inside symbol files." , |
| 4214 | }, |
| 4215 | { |
| 4216 | .value: eLoadScriptFromSymFileWarn, |
| 4217 | .string_value: "warn" , |
| 4218 | .usage: "Warn about debug scripts inside symbol files but do not load them." , |
| 4219 | }, |
| 4220 | }; |
| 4221 | |
| 4222 | static constexpr OptionEnumValueElement g_load_cwd_lldbinit_values[] = { |
| 4223 | { |
| 4224 | .value: eLoadCWDlldbinitTrue, |
| 4225 | .string_value: "true" , |
| 4226 | .usage: "Load .lldbinit files from current directory" , |
| 4227 | }, |
| 4228 | { |
| 4229 | .value: eLoadCWDlldbinitFalse, |
| 4230 | .string_value: "false" , |
| 4231 | .usage: "Do not load .lldbinit files from current directory" , |
| 4232 | }, |
| 4233 | { |
| 4234 | .value: eLoadCWDlldbinitWarn, |
| 4235 | .string_value: "warn" , |
| 4236 | .usage: "Warn about loading .lldbinit files from current directory" , |
| 4237 | }, |
| 4238 | }; |
| 4239 | |
| 4240 | static constexpr OptionEnumValueElement g_memory_module_load_level_values[] = { |
| 4241 | { |
| 4242 | .value: eMemoryModuleLoadLevelMinimal, |
| 4243 | .string_value: "minimal" , |
| 4244 | .usage: "Load minimal information when loading modules from memory. Currently " |
| 4245 | "this setting loads sections only." , |
| 4246 | }, |
| 4247 | { |
| 4248 | .value: eMemoryModuleLoadLevelPartial, |
| 4249 | .string_value: "partial" , |
| 4250 | .usage: "Load partial information when loading modules from memory. Currently " |
| 4251 | "this setting loads sections and function bounds." , |
| 4252 | }, |
| 4253 | { |
| 4254 | .value: eMemoryModuleLoadLevelComplete, |
| 4255 | .string_value: "complete" , |
| 4256 | .usage: "Load complete information when loading modules from memory. Currently " |
| 4257 | "this setting loads sections and all symbols." , |
| 4258 | }, |
| 4259 | }; |
| 4260 | |
| 4261 | #define LLDB_PROPERTIES_target |
| 4262 | #include "TargetProperties.inc" |
| 4263 | |
| 4264 | enum { |
| 4265 | #define LLDB_PROPERTIES_target |
| 4266 | #include "TargetPropertiesEnum.inc" |
| 4267 | ePropertyExperimental, |
| 4268 | }; |
| 4269 | |
| 4270 | class TargetOptionValueProperties |
| 4271 | : public Cloneable<TargetOptionValueProperties, OptionValueProperties> { |
| 4272 | public: |
| 4273 | TargetOptionValueProperties(llvm::StringRef name) : Cloneable(name) {} |
| 4274 | |
| 4275 | const Property * |
| 4276 | GetPropertyAtIndex(size_t idx, |
| 4277 | const ExecutionContext *exe_ctx = nullptr) const override { |
| 4278 | // When getting the value for a key from the target options, we will always |
| 4279 | // try and grab the setting from the current target if there is one. Else |
| 4280 | // we just use the one from this instance. |
| 4281 | if (exe_ctx) { |
| 4282 | Target *target = exe_ctx->GetTargetPtr(); |
| 4283 | if (target) { |
| 4284 | TargetOptionValueProperties *target_properties = |
| 4285 | static_cast<TargetOptionValueProperties *>( |
| 4286 | target->GetValueProperties().get()); |
| 4287 | if (this != target_properties) |
| 4288 | return target_properties->ProtectedGetPropertyAtIndex(idx); |
| 4289 | } |
| 4290 | } |
| 4291 | return ProtectedGetPropertyAtIndex(idx); |
| 4292 | } |
| 4293 | }; |
| 4294 | |
| 4295 | // TargetProperties |
| 4296 | #define LLDB_PROPERTIES_target_experimental |
| 4297 | #include "TargetProperties.inc" |
| 4298 | |
| 4299 | enum { |
| 4300 | #define LLDB_PROPERTIES_target_experimental |
| 4301 | #include "TargetPropertiesEnum.inc" |
| 4302 | }; |
| 4303 | |
| 4304 | class TargetExperimentalOptionValueProperties |
| 4305 | : public Cloneable<TargetExperimentalOptionValueProperties, |
| 4306 | OptionValueProperties> { |
| 4307 | public: |
| 4308 | TargetExperimentalOptionValueProperties() |
| 4309 | : Cloneable(Properties::GetExperimentalSettingsName()) {} |
| 4310 | }; |
| 4311 | |
| 4312 | TargetExperimentalProperties::TargetExperimentalProperties() |
| 4313 | : Properties(OptionValuePropertiesSP( |
| 4314 | new TargetExperimentalOptionValueProperties())) { |
| 4315 | m_collection_sp->Initialize(setting_definitions: g_target_experimental_properties); |
| 4316 | } |
| 4317 | |
| 4318 | // TargetProperties |
| 4319 | TargetProperties::TargetProperties(Target *target) |
| 4320 | : Properties(), m_launch_info(), m_target(target) { |
| 4321 | if (target) { |
| 4322 | m_collection_sp = |
| 4323 | OptionValueProperties::CreateLocalCopy(global_properties: Target::GetGlobalProperties()); |
| 4324 | |
| 4325 | // Set callbacks to update launch_info whenever "settins set" updated any |
| 4326 | // of these properties |
| 4327 | m_collection_sp->SetValueChangedCallback( |
| 4328 | property_idx: ePropertyArg0, callback: [this] { Arg0ValueChangedCallback(); }); |
| 4329 | m_collection_sp->SetValueChangedCallback( |
| 4330 | property_idx: ePropertyRunArgs, callback: [this] { RunArgsValueChangedCallback(); }); |
| 4331 | m_collection_sp->SetValueChangedCallback( |
| 4332 | property_idx: ePropertyEnvVars, callback: [this] { EnvVarsValueChangedCallback(); }); |
| 4333 | m_collection_sp->SetValueChangedCallback( |
| 4334 | property_idx: ePropertyUnsetEnvVars, callback: [this] { EnvVarsValueChangedCallback(); }); |
| 4335 | m_collection_sp->SetValueChangedCallback( |
| 4336 | property_idx: ePropertyInheritEnv, callback: [this] { EnvVarsValueChangedCallback(); }); |
| 4337 | m_collection_sp->SetValueChangedCallback( |
| 4338 | property_idx: ePropertyInputPath, callback: [this] { InputPathValueChangedCallback(); }); |
| 4339 | m_collection_sp->SetValueChangedCallback( |
| 4340 | property_idx: ePropertyOutputPath, callback: [this] { OutputPathValueChangedCallback(); }); |
| 4341 | m_collection_sp->SetValueChangedCallback( |
| 4342 | property_idx: ePropertyErrorPath, callback: [this] { ErrorPathValueChangedCallback(); }); |
| 4343 | m_collection_sp->SetValueChangedCallback(property_idx: ePropertyDetachOnError, callback: [this] { |
| 4344 | DetachOnErrorValueChangedCallback(); |
| 4345 | }); |
| 4346 | m_collection_sp->SetValueChangedCallback( |
| 4347 | property_idx: ePropertyDisableASLR, callback: [this] { DisableASLRValueChangedCallback(); }); |
| 4348 | m_collection_sp->SetValueChangedCallback( |
| 4349 | property_idx: ePropertyInheritTCC, callback: [this] { InheritTCCValueChangedCallback(); }); |
| 4350 | m_collection_sp->SetValueChangedCallback( |
| 4351 | property_idx: ePropertyDisableSTDIO, callback: [this] { DisableSTDIOValueChangedCallback(); }); |
| 4352 | |
| 4353 | m_collection_sp->SetValueChangedCallback( |
| 4354 | property_idx: ePropertySaveObjectsDir, callback: [this] { CheckJITObjectsDir(); }); |
| 4355 | m_experimental_properties_up = |
| 4356 | std::make_unique<TargetExperimentalProperties>(); |
| 4357 | m_collection_sp->AppendProperty( |
| 4358 | name: Properties::GetExperimentalSettingsName(), |
| 4359 | desc: "Experimental settings - setting these won't produce " |
| 4360 | "errors if the setting is not present." , |
| 4361 | is_global: true, value_sp: m_experimental_properties_up->GetValueProperties()); |
| 4362 | } else { |
| 4363 | m_collection_sp = std::make_shared<TargetOptionValueProperties>(args: "target" ); |
| 4364 | m_collection_sp->Initialize(setting_definitions: g_target_properties); |
| 4365 | m_experimental_properties_up = |
| 4366 | std::make_unique<TargetExperimentalProperties>(); |
| 4367 | m_collection_sp->AppendProperty( |
| 4368 | name: Properties::GetExperimentalSettingsName(), |
| 4369 | desc: "Experimental settings - setting these won't produce " |
| 4370 | "errors if the setting is not present." , |
| 4371 | is_global: true, value_sp: m_experimental_properties_up->GetValueProperties()); |
| 4372 | m_collection_sp->AppendProperty( |
| 4373 | name: "process" , desc: "Settings specific to processes." , is_global: true, |
| 4374 | value_sp: Process::GetGlobalProperties().GetValueProperties()); |
| 4375 | m_collection_sp->SetValueChangedCallback( |
| 4376 | property_idx: ePropertySaveObjectsDir, callback: [this] { CheckJITObjectsDir(); }); |
| 4377 | } |
| 4378 | } |
| 4379 | |
| 4380 | TargetProperties::~TargetProperties() = default; |
| 4381 | |
| 4382 | void TargetProperties::UpdateLaunchInfoFromProperties() { |
| 4383 | Arg0ValueChangedCallback(); |
| 4384 | RunArgsValueChangedCallback(); |
| 4385 | EnvVarsValueChangedCallback(); |
| 4386 | InputPathValueChangedCallback(); |
| 4387 | OutputPathValueChangedCallback(); |
| 4388 | ErrorPathValueChangedCallback(); |
| 4389 | DetachOnErrorValueChangedCallback(); |
| 4390 | DisableASLRValueChangedCallback(); |
| 4391 | InheritTCCValueChangedCallback(); |
| 4392 | DisableSTDIOValueChangedCallback(); |
| 4393 | } |
| 4394 | |
| 4395 | std::optional<bool> TargetProperties::GetExperimentalPropertyValue( |
| 4396 | size_t prop_idx, ExecutionContext *exe_ctx) const { |
| 4397 | const Property *exp_property = |
| 4398 | m_collection_sp->GetPropertyAtIndex(idx: ePropertyExperimental, exe_ctx); |
| 4399 | OptionValueProperties *exp_values = |
| 4400 | exp_property->GetValue()->GetAsProperties(); |
| 4401 | if (exp_values) |
| 4402 | return exp_values->GetPropertyAtIndexAs<bool>(idx: prop_idx, exe_ctx); |
| 4403 | return std::nullopt; |
| 4404 | } |
| 4405 | |
| 4406 | bool TargetProperties::GetInjectLocalVariables( |
| 4407 | ExecutionContext *exe_ctx) const { |
| 4408 | return GetExperimentalPropertyValue(prop_idx: ePropertyInjectLocalVars, exe_ctx) |
| 4409 | .value_or(true); |
| 4410 | } |
| 4411 | |
| 4412 | bool TargetProperties::GetUseDIL(ExecutionContext *exe_ctx) const { |
| 4413 | const Property *exp_property = |
| 4414 | m_collection_sp->GetPropertyAtIndex(idx: ePropertyExperimental, exe_ctx); |
| 4415 | OptionValueProperties *exp_values = |
| 4416 | exp_property->GetValue()->GetAsProperties(); |
| 4417 | if (exp_values) |
| 4418 | return exp_values->GetPropertyAtIndexAs<bool>(ePropertyUseDIL, exe_ctx) |
| 4419 | .value_or(false); |
| 4420 | else |
| 4421 | return true; |
| 4422 | } |
| 4423 | |
| 4424 | void TargetProperties::SetUseDIL(ExecutionContext *exe_ctx, bool b) { |
| 4425 | const Property *exp_property = |
| 4426 | m_collection_sp->GetPropertyAtIndex(idx: ePropertyExperimental, exe_ctx); |
| 4427 | OptionValueProperties *exp_values = |
| 4428 | exp_property->GetValue()->GetAsProperties(); |
| 4429 | if (exp_values) |
| 4430 | exp_values->SetPropertyAtIndex(ePropertyUseDIL, true, exe_ctx); |
| 4431 | } |
| 4432 | |
| 4433 | ArchSpec TargetProperties::GetDefaultArchitecture() const { |
| 4434 | const uint32_t idx = ePropertyDefaultArch; |
| 4435 | return GetPropertyAtIndexAs<ArchSpec>(idx, default_value: {}); |
| 4436 | } |
| 4437 | |
| 4438 | void TargetProperties::SetDefaultArchitecture(const ArchSpec &arch) { |
| 4439 | const uint32_t idx = ePropertyDefaultArch; |
| 4440 | SetPropertyAtIndex(idx, t: arch); |
| 4441 | } |
| 4442 | |
| 4443 | bool TargetProperties::GetMoveToNearestCode() const { |
| 4444 | const uint32_t idx = ePropertyMoveToNearestCode; |
| 4445 | return GetPropertyAtIndexAs<bool>( |
| 4446 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4447 | } |
| 4448 | |
| 4449 | lldb::DynamicValueType TargetProperties::GetPreferDynamicValue() const { |
| 4450 | const uint32_t idx = ePropertyPreferDynamic; |
| 4451 | return GetPropertyAtIndexAs<lldb::DynamicValueType>( |
| 4452 | idx, static_cast<lldb::DynamicValueType>( |
| 4453 | g_target_properties[idx].default_uint_value)); |
| 4454 | } |
| 4455 | |
| 4456 | bool TargetProperties::SetPreferDynamicValue(lldb::DynamicValueType d) { |
| 4457 | const uint32_t idx = ePropertyPreferDynamic; |
| 4458 | return SetPropertyAtIndex(idx, t: d); |
| 4459 | } |
| 4460 | |
| 4461 | bool TargetProperties::GetPreloadSymbols() const { |
| 4462 | if (INTERRUPT_REQUESTED(m_target->GetDebugger(), |
| 4463 | "Interrupted checking preload symbols" )) { |
| 4464 | return false; |
| 4465 | } |
| 4466 | const uint32_t idx = ePropertyPreloadSymbols; |
| 4467 | return GetPropertyAtIndexAs<bool>( |
| 4468 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4469 | } |
| 4470 | |
| 4471 | void TargetProperties::SetPreloadSymbols(bool b) { |
| 4472 | const uint32_t idx = ePropertyPreloadSymbols; |
| 4473 | SetPropertyAtIndex(idx, t: b); |
| 4474 | } |
| 4475 | |
| 4476 | bool TargetProperties::GetDisableASLR() const { |
| 4477 | const uint32_t idx = ePropertyDisableASLR; |
| 4478 | return GetPropertyAtIndexAs<bool>( |
| 4479 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4480 | } |
| 4481 | |
| 4482 | void TargetProperties::SetDisableASLR(bool b) { |
| 4483 | const uint32_t idx = ePropertyDisableASLR; |
| 4484 | SetPropertyAtIndex(idx, t: b); |
| 4485 | } |
| 4486 | |
| 4487 | bool TargetProperties::GetInheritTCC() const { |
| 4488 | const uint32_t idx = ePropertyInheritTCC; |
| 4489 | return GetPropertyAtIndexAs<bool>( |
| 4490 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4491 | } |
| 4492 | |
| 4493 | void TargetProperties::SetInheritTCC(bool b) { |
| 4494 | const uint32_t idx = ePropertyInheritTCC; |
| 4495 | SetPropertyAtIndex(idx, t: b); |
| 4496 | } |
| 4497 | |
| 4498 | bool TargetProperties::GetDetachOnError() const { |
| 4499 | const uint32_t idx = ePropertyDetachOnError; |
| 4500 | return GetPropertyAtIndexAs<bool>( |
| 4501 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4502 | } |
| 4503 | |
| 4504 | void TargetProperties::SetDetachOnError(bool b) { |
| 4505 | const uint32_t idx = ePropertyDetachOnError; |
| 4506 | SetPropertyAtIndex(idx, t: b); |
| 4507 | } |
| 4508 | |
| 4509 | bool TargetProperties::GetDisableSTDIO() const { |
| 4510 | const uint32_t idx = ePropertyDisableSTDIO; |
| 4511 | return GetPropertyAtIndexAs<bool>( |
| 4512 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4513 | } |
| 4514 | |
| 4515 | void TargetProperties::SetDisableSTDIO(bool b) { |
| 4516 | const uint32_t idx = ePropertyDisableSTDIO; |
| 4517 | SetPropertyAtIndex(idx, t: b); |
| 4518 | } |
| 4519 | llvm::StringRef TargetProperties::GetLaunchWorkingDirectory() const { |
| 4520 | const uint32_t idx = ePropertyLaunchWorkingDir; |
| 4521 | return GetPropertyAtIndexAs<llvm::StringRef>( |
| 4522 | idx, g_target_properties[idx].default_cstr_value); |
| 4523 | } |
| 4524 | |
| 4525 | bool TargetProperties::GetParallelModuleLoad() const { |
| 4526 | const uint32_t idx = ePropertyParallelModuleLoad; |
| 4527 | return GetPropertyAtIndexAs<bool>( |
| 4528 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4529 | } |
| 4530 | |
| 4531 | const char *TargetProperties::GetDisassemblyFlavor() const { |
| 4532 | const uint32_t idx = ePropertyDisassemblyFlavor; |
| 4533 | const char *return_value; |
| 4534 | |
| 4535 | x86DisassemblyFlavor flavor_value = |
| 4536 | GetPropertyAtIndexAs<x86DisassemblyFlavor>( |
| 4537 | idx, static_cast<x86DisassemblyFlavor>( |
| 4538 | g_target_properties[idx].default_uint_value)); |
| 4539 | |
| 4540 | return_value = g_x86_dis_flavor_value_types[flavor_value].string_value; |
| 4541 | return return_value; |
| 4542 | } |
| 4543 | |
| 4544 | const char *TargetProperties::GetDisassemblyCPU() const { |
| 4545 | const uint32_t idx = ePropertyDisassemblyCPU; |
| 4546 | llvm::StringRef str = GetPropertyAtIndexAs<llvm::StringRef>( |
| 4547 | idx, g_target_properties[idx].default_cstr_value); |
| 4548 | return str.empty() ? nullptr : str.data(); |
| 4549 | } |
| 4550 | |
| 4551 | const char *TargetProperties::GetDisassemblyFeatures() const { |
| 4552 | const uint32_t idx = ePropertyDisassemblyFeatures; |
| 4553 | llvm::StringRef str = GetPropertyAtIndexAs<llvm::StringRef>( |
| 4554 | idx, g_target_properties[idx].default_cstr_value); |
| 4555 | return str.empty() ? nullptr : str.data(); |
| 4556 | } |
| 4557 | |
| 4558 | InlineStrategy TargetProperties::GetInlineStrategy() const { |
| 4559 | const uint32_t idx = ePropertyInlineStrategy; |
| 4560 | return GetPropertyAtIndexAs<InlineStrategy>( |
| 4561 | idx, |
| 4562 | static_cast<InlineStrategy>(g_target_properties[idx].default_uint_value)); |
| 4563 | } |
| 4564 | |
| 4565 | // Returning RealpathPrefixes, but the setting's type is FileSpecList. We do |
| 4566 | // this because we want the FileSpecList to normalize the file paths for us. |
| 4567 | RealpathPrefixes TargetProperties::GetSourceRealpathPrefixes() const { |
| 4568 | const uint32_t idx = ePropertySourceRealpathPrefixes; |
| 4569 | return RealpathPrefixes(GetPropertyAtIndexAs<FileSpecList>(idx, default_value: {})); |
| 4570 | } |
| 4571 | |
| 4572 | llvm::StringRef TargetProperties::GetArg0() const { |
| 4573 | const uint32_t idx = ePropertyArg0; |
| 4574 | return GetPropertyAtIndexAs<llvm::StringRef>( |
| 4575 | idx, g_target_properties[idx].default_cstr_value); |
| 4576 | } |
| 4577 | |
| 4578 | void TargetProperties::SetArg0(llvm::StringRef arg) { |
| 4579 | const uint32_t idx = ePropertyArg0; |
| 4580 | SetPropertyAtIndex(idx, t: arg); |
| 4581 | m_launch_info.SetArg0(arg); |
| 4582 | } |
| 4583 | |
| 4584 | bool TargetProperties::GetRunArguments(Args &args) const { |
| 4585 | const uint32_t idx = ePropertyRunArgs; |
| 4586 | return m_collection_sp->GetPropertyAtIndexAsArgs(idx, args); |
| 4587 | } |
| 4588 | |
| 4589 | void TargetProperties::SetRunArguments(const Args &args) { |
| 4590 | const uint32_t idx = ePropertyRunArgs; |
| 4591 | m_collection_sp->SetPropertyAtIndexFromArgs(idx, args); |
| 4592 | m_launch_info.GetArguments() = args; |
| 4593 | } |
| 4594 | |
| 4595 | Environment TargetProperties::ComputeEnvironment() const { |
| 4596 | Environment env; |
| 4597 | |
| 4598 | if (m_target && |
| 4599 | GetPropertyAtIndexAs<bool>( |
| 4600 | ePropertyInheritEnv, |
| 4601 | g_target_properties[ePropertyInheritEnv].default_uint_value != 0)) { |
| 4602 | if (auto platform_sp = m_target->GetPlatform()) { |
| 4603 | Environment platform_env = platform_sp->GetEnvironment(); |
| 4604 | for (const auto &KV : platform_env) |
| 4605 | env[KV.first()] = KV.second; |
| 4606 | } |
| 4607 | } |
| 4608 | |
| 4609 | Args property_unset_env; |
| 4610 | m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyUnsetEnvVars, |
| 4611 | property_unset_env); |
| 4612 | for (const auto &var : property_unset_env) |
| 4613 | env.erase(Key: var.ref()); |
| 4614 | |
| 4615 | Args property_env; |
| 4616 | m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyEnvVars, property_env); |
| 4617 | for (const auto &KV : Environment(property_env)) |
| 4618 | env[KV.first()] = KV.second; |
| 4619 | |
| 4620 | return env; |
| 4621 | } |
| 4622 | |
| 4623 | Environment TargetProperties::GetEnvironment() const { |
| 4624 | return ComputeEnvironment(); |
| 4625 | } |
| 4626 | |
| 4627 | Environment TargetProperties::GetInheritedEnvironment() const { |
| 4628 | Environment environment; |
| 4629 | |
| 4630 | if (m_target == nullptr) |
| 4631 | return environment; |
| 4632 | |
| 4633 | if (!GetPropertyAtIndexAs<bool>( |
| 4634 | ePropertyInheritEnv, |
| 4635 | g_target_properties[ePropertyInheritEnv].default_uint_value != 0)) |
| 4636 | return environment; |
| 4637 | |
| 4638 | PlatformSP platform_sp = m_target->GetPlatform(); |
| 4639 | if (platform_sp == nullptr) |
| 4640 | return environment; |
| 4641 | |
| 4642 | Environment platform_environment = platform_sp->GetEnvironment(); |
| 4643 | for (const auto &KV : platform_environment) |
| 4644 | environment[KV.first()] = KV.second; |
| 4645 | |
| 4646 | Args property_unset_environment; |
| 4647 | m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyUnsetEnvVars, |
| 4648 | property_unset_environment); |
| 4649 | for (const auto &var : property_unset_environment) |
| 4650 | environment.erase(Key: var.ref()); |
| 4651 | |
| 4652 | return environment; |
| 4653 | } |
| 4654 | |
| 4655 | Environment TargetProperties::GetTargetEnvironment() const { |
| 4656 | Args property_environment; |
| 4657 | m_collection_sp->GetPropertyAtIndexAsArgs(ePropertyEnvVars, |
| 4658 | property_environment); |
| 4659 | Environment environment; |
| 4660 | for (const auto &KV : Environment(property_environment)) |
| 4661 | environment[KV.first()] = KV.second; |
| 4662 | |
| 4663 | return environment; |
| 4664 | } |
| 4665 | |
| 4666 | void TargetProperties::SetEnvironment(Environment env) { |
| 4667 | // TODO: Get rid of the Args intermediate step |
| 4668 | const uint32_t idx = ePropertyEnvVars; |
| 4669 | m_collection_sp->SetPropertyAtIndexFromArgs(idx, args: Args(env)); |
| 4670 | } |
| 4671 | |
| 4672 | bool TargetProperties::GetSkipPrologue() const { |
| 4673 | const uint32_t idx = ePropertySkipPrologue; |
| 4674 | return GetPropertyAtIndexAs<bool>( |
| 4675 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4676 | } |
| 4677 | |
| 4678 | PathMappingList &TargetProperties::GetSourcePathMap() const { |
| 4679 | const uint32_t idx = ePropertySourceMap; |
| 4680 | OptionValuePathMappings *option_value = |
| 4681 | m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(idx); |
| 4682 | assert(option_value); |
| 4683 | return option_value->GetCurrentValue(); |
| 4684 | } |
| 4685 | |
| 4686 | PathMappingList &TargetProperties::GetObjectPathMap() const { |
| 4687 | const uint32_t idx = ePropertyObjectMap; |
| 4688 | OptionValuePathMappings *option_value = |
| 4689 | m_collection_sp->GetPropertyAtIndexAsOptionValuePathMappings(idx); |
| 4690 | assert(option_value); |
| 4691 | return option_value->GetCurrentValue(); |
| 4692 | } |
| 4693 | |
| 4694 | bool TargetProperties::GetAutoSourceMapRelative() const { |
| 4695 | const uint32_t idx = ePropertyAutoSourceMapRelative; |
| 4696 | return GetPropertyAtIndexAs<bool>( |
| 4697 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4698 | } |
| 4699 | |
| 4700 | void TargetProperties::AppendExecutableSearchPaths(const FileSpec &dir) { |
| 4701 | const uint32_t idx = ePropertyExecutableSearchPaths; |
| 4702 | OptionValueFileSpecList *option_value = |
| 4703 | m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpecList(idx); |
| 4704 | assert(option_value); |
| 4705 | option_value->AppendCurrentValue(value: dir); |
| 4706 | } |
| 4707 | |
| 4708 | FileSpecList TargetProperties::GetExecutableSearchPaths() { |
| 4709 | const uint32_t idx = ePropertyExecutableSearchPaths; |
| 4710 | return GetPropertyAtIndexAs<FileSpecList>(idx, default_value: {}); |
| 4711 | } |
| 4712 | |
| 4713 | FileSpecList TargetProperties::GetDebugFileSearchPaths() { |
| 4714 | const uint32_t idx = ePropertyDebugFileSearchPaths; |
| 4715 | return GetPropertyAtIndexAs<FileSpecList>(idx, default_value: {}); |
| 4716 | } |
| 4717 | |
| 4718 | FileSpecList TargetProperties::GetClangModuleSearchPaths() { |
| 4719 | const uint32_t idx = ePropertyClangModuleSearchPaths; |
| 4720 | return GetPropertyAtIndexAs<FileSpecList>(idx, default_value: {}); |
| 4721 | } |
| 4722 | |
| 4723 | bool TargetProperties::GetEnableAutoImportClangModules() const { |
| 4724 | const uint32_t idx = ePropertyAutoImportClangModules; |
| 4725 | return GetPropertyAtIndexAs<bool>( |
| 4726 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4727 | } |
| 4728 | |
| 4729 | ImportStdModule TargetProperties::GetImportStdModule() const { |
| 4730 | const uint32_t idx = ePropertyImportStdModule; |
| 4731 | return GetPropertyAtIndexAs<ImportStdModule>( |
| 4732 | idx, static_cast<ImportStdModule>( |
| 4733 | g_target_properties[idx].default_uint_value)); |
| 4734 | } |
| 4735 | |
| 4736 | DynamicClassInfoHelper TargetProperties::GetDynamicClassInfoHelper() const { |
| 4737 | const uint32_t idx = ePropertyDynamicClassInfoHelper; |
| 4738 | return GetPropertyAtIndexAs<DynamicClassInfoHelper>( |
| 4739 | idx, static_cast<DynamicClassInfoHelper>( |
| 4740 | g_target_properties[idx].default_uint_value)); |
| 4741 | } |
| 4742 | |
| 4743 | bool TargetProperties::GetEnableAutoApplyFixIts() const { |
| 4744 | const uint32_t idx = ePropertyAutoApplyFixIts; |
| 4745 | return GetPropertyAtIndexAs<bool>( |
| 4746 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4747 | } |
| 4748 | |
| 4749 | uint64_t TargetProperties::GetNumberOfRetriesWithFixits() const { |
| 4750 | const uint32_t idx = ePropertyRetriesWithFixIts; |
| 4751 | return GetPropertyAtIndexAs<uint64_t>( |
| 4752 | idx, g_target_properties[idx].default_uint_value); |
| 4753 | } |
| 4754 | |
| 4755 | bool TargetProperties::GetEnableNotifyAboutFixIts() const { |
| 4756 | const uint32_t idx = ePropertyNotifyAboutFixIts; |
| 4757 | return GetPropertyAtIndexAs<bool>( |
| 4758 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4759 | } |
| 4760 | |
| 4761 | FileSpec TargetProperties::GetSaveJITObjectsDir() const { |
| 4762 | const uint32_t idx = ePropertySaveObjectsDir; |
| 4763 | return GetPropertyAtIndexAs<FileSpec>(idx, default_value: {}); |
| 4764 | } |
| 4765 | |
| 4766 | void TargetProperties::CheckJITObjectsDir() { |
| 4767 | FileSpec new_dir = GetSaveJITObjectsDir(); |
| 4768 | if (!new_dir) |
| 4769 | return; |
| 4770 | |
| 4771 | const FileSystem &instance = FileSystem::Instance(); |
| 4772 | bool exists = instance.Exists(file_spec: new_dir); |
| 4773 | bool is_directory = instance.IsDirectory(file_spec: new_dir); |
| 4774 | std::string path = new_dir.GetPath(denormalize: true); |
| 4775 | bool writable = llvm::sys::fs::can_write(Path: path); |
| 4776 | if (exists && is_directory && writable) |
| 4777 | return; |
| 4778 | |
| 4779 | m_collection_sp->GetPropertyAtIndex(ePropertySaveObjectsDir) |
| 4780 | ->GetValue() |
| 4781 | ->Clear(); |
| 4782 | |
| 4783 | std::string buffer; |
| 4784 | llvm::raw_string_ostream os(buffer); |
| 4785 | os << "JIT object dir '" << path << "' " ; |
| 4786 | if (!exists) |
| 4787 | os << "does not exist" ; |
| 4788 | else if (!is_directory) |
| 4789 | os << "is not a directory" ; |
| 4790 | else if (!writable) |
| 4791 | os << "is not writable" ; |
| 4792 | |
| 4793 | std::optional<lldb::user_id_t> debugger_id; |
| 4794 | if (m_target) |
| 4795 | debugger_id = m_target->GetDebugger().GetID(); |
| 4796 | Debugger::ReportError(message: buffer, debugger_id); |
| 4797 | } |
| 4798 | |
| 4799 | bool TargetProperties::GetEnableSyntheticValue() const { |
| 4800 | const uint32_t idx = ePropertyEnableSynthetic; |
| 4801 | return GetPropertyAtIndexAs<bool>( |
| 4802 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4803 | } |
| 4804 | |
| 4805 | bool TargetProperties::ShowHexVariableValuesWithLeadingZeroes() const { |
| 4806 | const uint32_t idx = ePropertyShowHexVariableValuesWithLeadingZeroes; |
| 4807 | return GetPropertyAtIndexAs<bool>( |
| 4808 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4809 | } |
| 4810 | |
| 4811 | uint32_t TargetProperties::GetMaxZeroPaddingInFloatFormat() const { |
| 4812 | const uint32_t idx = ePropertyMaxZeroPaddingInFloatFormat; |
| 4813 | return GetPropertyAtIndexAs<uint64_t>( |
| 4814 | idx, g_target_properties[idx].default_uint_value); |
| 4815 | } |
| 4816 | |
| 4817 | uint32_t TargetProperties::GetMaximumNumberOfChildrenToDisplay() const { |
| 4818 | const uint32_t idx = ePropertyMaxChildrenCount; |
| 4819 | return GetPropertyAtIndexAs<uint64_t>( |
| 4820 | idx, g_target_properties[idx].default_uint_value); |
| 4821 | } |
| 4822 | |
| 4823 | std::pair<uint32_t, bool> |
| 4824 | TargetProperties::GetMaximumDepthOfChildrenToDisplay() const { |
| 4825 | const uint32_t idx = ePropertyMaxChildrenDepth; |
| 4826 | auto *option_value = |
| 4827 | m_collection_sp->GetPropertyAtIndexAsOptionValueUInt64(idx); |
| 4828 | bool is_default = !option_value->OptionWasSet(); |
| 4829 | return {option_value->GetCurrentValue(), is_default}; |
| 4830 | } |
| 4831 | |
| 4832 | uint32_t TargetProperties::GetMaximumSizeOfStringSummary() const { |
| 4833 | const uint32_t idx = ePropertyMaxSummaryLength; |
| 4834 | return GetPropertyAtIndexAs<uint64_t>( |
| 4835 | idx, g_target_properties[idx].default_uint_value); |
| 4836 | } |
| 4837 | |
| 4838 | uint32_t TargetProperties::GetMaximumMemReadSize() const { |
| 4839 | const uint32_t idx = ePropertyMaxMemReadSize; |
| 4840 | return GetPropertyAtIndexAs<uint64_t>( |
| 4841 | idx, g_target_properties[idx].default_uint_value); |
| 4842 | } |
| 4843 | |
| 4844 | FileSpec TargetProperties::GetStandardInputPath() const { |
| 4845 | const uint32_t idx = ePropertyInputPath; |
| 4846 | return GetPropertyAtIndexAs<FileSpec>(idx, default_value: {}); |
| 4847 | } |
| 4848 | |
| 4849 | void TargetProperties::SetStandardInputPath(llvm::StringRef path) { |
| 4850 | const uint32_t idx = ePropertyInputPath; |
| 4851 | SetPropertyAtIndex(idx, t: path); |
| 4852 | } |
| 4853 | |
| 4854 | FileSpec TargetProperties::GetStandardOutputPath() const { |
| 4855 | const uint32_t idx = ePropertyOutputPath; |
| 4856 | return GetPropertyAtIndexAs<FileSpec>(idx, default_value: {}); |
| 4857 | } |
| 4858 | |
| 4859 | void TargetProperties::SetStandardOutputPath(llvm::StringRef path) { |
| 4860 | const uint32_t idx = ePropertyOutputPath; |
| 4861 | SetPropertyAtIndex(idx, t: path); |
| 4862 | } |
| 4863 | |
| 4864 | FileSpec TargetProperties::GetStandardErrorPath() const { |
| 4865 | const uint32_t idx = ePropertyErrorPath; |
| 4866 | return GetPropertyAtIndexAs<FileSpec>(idx, default_value: {}); |
| 4867 | } |
| 4868 | |
| 4869 | void TargetProperties::SetStandardErrorPath(llvm::StringRef path) { |
| 4870 | const uint32_t idx = ePropertyErrorPath; |
| 4871 | SetPropertyAtIndex(idx, t: path); |
| 4872 | } |
| 4873 | |
| 4874 | SourceLanguage TargetProperties::GetLanguage() const { |
| 4875 | const uint32_t idx = ePropertyLanguage; |
| 4876 | return {GetPropertyAtIndexAs<LanguageType>(idx, default_value: {})}; |
| 4877 | } |
| 4878 | |
| 4879 | llvm::StringRef TargetProperties::GetExpressionPrefixContents() { |
| 4880 | const uint32_t idx = ePropertyExprPrefix; |
| 4881 | OptionValueFileSpec *file = |
| 4882 | m_collection_sp->GetPropertyAtIndexAsOptionValueFileSpec(idx); |
| 4883 | if (file) { |
| 4884 | DataBufferSP data_sp(file->GetFileContents()); |
| 4885 | if (data_sp) |
| 4886 | return llvm::StringRef( |
| 4887 | reinterpret_cast<const char *>(data_sp->GetBytes()), |
| 4888 | data_sp->GetByteSize()); |
| 4889 | } |
| 4890 | return "" ; |
| 4891 | } |
| 4892 | |
| 4893 | uint64_t TargetProperties::GetExprErrorLimit() const { |
| 4894 | const uint32_t idx = ePropertyExprErrorLimit; |
| 4895 | return GetPropertyAtIndexAs<uint64_t>( |
| 4896 | idx, g_target_properties[idx].default_uint_value); |
| 4897 | } |
| 4898 | |
| 4899 | uint64_t TargetProperties::GetExprAllocAddress() const { |
| 4900 | const uint32_t idx = ePropertyExprAllocAddress; |
| 4901 | return GetPropertyAtIndexAs<uint64_t>( |
| 4902 | idx, g_target_properties[idx].default_uint_value); |
| 4903 | } |
| 4904 | |
| 4905 | uint64_t TargetProperties::GetExprAllocSize() const { |
| 4906 | const uint32_t idx = ePropertyExprAllocSize; |
| 4907 | return GetPropertyAtIndexAs<uint64_t>( |
| 4908 | idx, g_target_properties[idx].default_uint_value); |
| 4909 | } |
| 4910 | |
| 4911 | uint64_t TargetProperties::GetExprAllocAlign() const { |
| 4912 | const uint32_t idx = ePropertyExprAllocAlign; |
| 4913 | return GetPropertyAtIndexAs<uint64_t>( |
| 4914 | idx, g_target_properties[idx].default_uint_value); |
| 4915 | } |
| 4916 | |
| 4917 | bool TargetProperties::GetBreakpointsConsultPlatformAvoidList() { |
| 4918 | const uint32_t idx = ePropertyBreakpointUseAvoidList; |
| 4919 | return GetPropertyAtIndexAs<bool>( |
| 4920 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4921 | } |
| 4922 | |
| 4923 | bool TargetProperties::GetUseHexImmediates() const { |
| 4924 | const uint32_t idx = ePropertyUseHexImmediates; |
| 4925 | return GetPropertyAtIndexAs<bool>( |
| 4926 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4927 | } |
| 4928 | |
| 4929 | bool TargetProperties::GetUseFastStepping() const { |
| 4930 | const uint32_t idx = ePropertyUseFastStepping; |
| 4931 | return GetPropertyAtIndexAs<bool>( |
| 4932 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4933 | } |
| 4934 | |
| 4935 | bool TargetProperties::GetDisplayExpressionsInCrashlogs() const { |
| 4936 | const uint32_t idx = ePropertyDisplayExpressionsInCrashlogs; |
| 4937 | return GetPropertyAtIndexAs<bool>( |
| 4938 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4939 | } |
| 4940 | |
| 4941 | LoadScriptFromSymFile TargetProperties::GetLoadScriptFromSymbolFile() const { |
| 4942 | const uint32_t idx = ePropertyLoadScriptFromSymbolFile; |
| 4943 | return GetPropertyAtIndexAs<LoadScriptFromSymFile>( |
| 4944 | idx, static_cast<LoadScriptFromSymFile>( |
| 4945 | g_target_properties[idx].default_uint_value)); |
| 4946 | } |
| 4947 | |
| 4948 | LoadCWDlldbinitFile TargetProperties::GetLoadCWDlldbinitFile() const { |
| 4949 | const uint32_t idx = ePropertyLoadCWDlldbinitFile; |
| 4950 | return GetPropertyAtIndexAs<LoadCWDlldbinitFile>( |
| 4951 | idx, static_cast<LoadCWDlldbinitFile>( |
| 4952 | g_target_properties[idx].default_uint_value)); |
| 4953 | } |
| 4954 | |
| 4955 | Disassembler::HexImmediateStyle TargetProperties::GetHexImmediateStyle() const { |
| 4956 | const uint32_t idx = ePropertyHexImmediateStyle; |
| 4957 | return GetPropertyAtIndexAs<Disassembler::HexImmediateStyle>( |
| 4958 | idx, static_cast<Disassembler::HexImmediateStyle>( |
| 4959 | g_target_properties[idx].default_uint_value)); |
| 4960 | } |
| 4961 | |
| 4962 | MemoryModuleLoadLevel TargetProperties::GetMemoryModuleLoadLevel() const { |
| 4963 | const uint32_t idx = ePropertyMemoryModuleLoadLevel; |
| 4964 | return GetPropertyAtIndexAs<MemoryModuleLoadLevel>( |
| 4965 | idx, static_cast<MemoryModuleLoadLevel>( |
| 4966 | g_target_properties[idx].default_uint_value)); |
| 4967 | } |
| 4968 | |
| 4969 | bool TargetProperties::GetUserSpecifiedTrapHandlerNames(Args &args) const { |
| 4970 | const uint32_t idx = ePropertyTrapHandlerNames; |
| 4971 | return m_collection_sp->GetPropertyAtIndexAsArgs(idx, args); |
| 4972 | } |
| 4973 | |
| 4974 | void TargetProperties::SetUserSpecifiedTrapHandlerNames(const Args &args) { |
| 4975 | const uint32_t idx = ePropertyTrapHandlerNames; |
| 4976 | m_collection_sp->SetPropertyAtIndexFromArgs(idx, args); |
| 4977 | } |
| 4978 | |
| 4979 | bool TargetProperties::GetDisplayRuntimeSupportValues() const { |
| 4980 | const uint32_t idx = ePropertyDisplayRuntimeSupportValues; |
| 4981 | return GetPropertyAtIndexAs<bool>( |
| 4982 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4983 | } |
| 4984 | |
| 4985 | void TargetProperties::SetDisplayRuntimeSupportValues(bool b) { |
| 4986 | const uint32_t idx = ePropertyDisplayRuntimeSupportValues; |
| 4987 | SetPropertyAtIndex(idx, t: b); |
| 4988 | } |
| 4989 | |
| 4990 | bool TargetProperties::GetDisplayRecognizedArguments() const { |
| 4991 | const uint32_t idx = ePropertyDisplayRecognizedArguments; |
| 4992 | return GetPropertyAtIndexAs<bool>( |
| 4993 | idx, g_target_properties[idx].default_uint_value != 0); |
| 4994 | } |
| 4995 | |
| 4996 | void TargetProperties::SetDisplayRecognizedArguments(bool b) { |
| 4997 | const uint32_t idx = ePropertyDisplayRecognizedArguments; |
| 4998 | SetPropertyAtIndex(idx, t: b); |
| 4999 | } |
| 5000 | |
| 5001 | const ProcessLaunchInfo &TargetProperties::GetProcessLaunchInfo() const { |
| 5002 | return m_launch_info; |
| 5003 | } |
| 5004 | |
| 5005 | void TargetProperties::SetProcessLaunchInfo( |
| 5006 | const ProcessLaunchInfo &launch_info) { |
| 5007 | m_launch_info = launch_info; |
| 5008 | SetArg0(launch_info.GetArg0()); |
| 5009 | SetRunArguments(launch_info.GetArguments()); |
| 5010 | SetEnvironment(launch_info.GetEnvironment()); |
| 5011 | const FileAction *input_file_action = |
| 5012 | launch_info.GetFileActionForFD(STDIN_FILENO); |
| 5013 | if (input_file_action) { |
| 5014 | SetStandardInputPath(input_file_action->GetPath()); |
| 5015 | } |
| 5016 | const FileAction *output_file_action = |
| 5017 | launch_info.GetFileActionForFD(STDOUT_FILENO); |
| 5018 | if (output_file_action) { |
| 5019 | SetStandardOutputPath(output_file_action->GetPath()); |
| 5020 | } |
| 5021 | const FileAction *error_file_action = |
| 5022 | launch_info.GetFileActionForFD(STDERR_FILENO); |
| 5023 | if (error_file_action) { |
| 5024 | SetStandardErrorPath(error_file_action->GetPath()); |
| 5025 | } |
| 5026 | SetDetachOnError(launch_info.GetFlags().Test(bit: lldb::eLaunchFlagDetachOnError)); |
| 5027 | SetDisableASLR(launch_info.GetFlags().Test(bit: lldb::eLaunchFlagDisableASLR)); |
| 5028 | SetInheritTCC( |
| 5029 | launch_info.GetFlags().Test(bit: lldb::eLaunchFlagInheritTCCFromParent)); |
| 5030 | SetDisableSTDIO(launch_info.GetFlags().Test(bit: lldb::eLaunchFlagDisableSTDIO)); |
| 5031 | } |
| 5032 | |
| 5033 | bool TargetProperties::GetRequireHardwareBreakpoints() const { |
| 5034 | const uint32_t idx = ePropertyRequireHardwareBreakpoints; |
| 5035 | return GetPropertyAtIndexAs<bool>( |
| 5036 | idx, g_target_properties[idx].default_uint_value != 0); |
| 5037 | } |
| 5038 | |
| 5039 | void TargetProperties::SetRequireHardwareBreakpoints(bool b) { |
| 5040 | const uint32_t idx = ePropertyRequireHardwareBreakpoints; |
| 5041 | m_collection_sp->SetPropertyAtIndex(idx, t: b); |
| 5042 | } |
| 5043 | |
| 5044 | bool TargetProperties::GetAutoInstallMainExecutable() const { |
| 5045 | const uint32_t idx = ePropertyAutoInstallMainExecutable; |
| 5046 | return GetPropertyAtIndexAs<bool>( |
| 5047 | idx, g_target_properties[idx].default_uint_value != 0); |
| 5048 | } |
| 5049 | |
| 5050 | void TargetProperties::Arg0ValueChangedCallback() { |
| 5051 | m_launch_info.SetArg0(GetArg0()); |
| 5052 | } |
| 5053 | |
| 5054 | void TargetProperties::RunArgsValueChangedCallback() { |
| 5055 | Args args; |
| 5056 | if (GetRunArguments(args)) |
| 5057 | m_launch_info.GetArguments() = args; |
| 5058 | } |
| 5059 | |
| 5060 | void TargetProperties::EnvVarsValueChangedCallback() { |
| 5061 | m_launch_info.GetEnvironment() = ComputeEnvironment(); |
| 5062 | } |
| 5063 | |
| 5064 | void TargetProperties::InputPathValueChangedCallback() { |
| 5065 | m_launch_info.AppendOpenFileAction(STDIN_FILENO, file_spec: GetStandardInputPath(), read: true, |
| 5066 | write: false); |
| 5067 | } |
| 5068 | |
| 5069 | void TargetProperties::OutputPathValueChangedCallback() { |
| 5070 | m_launch_info.AppendOpenFileAction(STDOUT_FILENO, file_spec: GetStandardOutputPath(), |
| 5071 | read: false, write: true); |
| 5072 | } |
| 5073 | |
| 5074 | void TargetProperties::ErrorPathValueChangedCallback() { |
| 5075 | m_launch_info.AppendOpenFileAction(STDERR_FILENO, file_spec: GetStandardErrorPath(), |
| 5076 | read: false, write: true); |
| 5077 | } |
| 5078 | |
| 5079 | void TargetProperties::DetachOnErrorValueChangedCallback() { |
| 5080 | if (GetDetachOnError()) |
| 5081 | m_launch_info.GetFlags().Set(lldb::eLaunchFlagDetachOnError); |
| 5082 | else |
| 5083 | m_launch_info.GetFlags().Clear(mask: lldb::eLaunchFlagDetachOnError); |
| 5084 | } |
| 5085 | |
| 5086 | void TargetProperties::DisableASLRValueChangedCallback() { |
| 5087 | if (GetDisableASLR()) |
| 5088 | m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableASLR); |
| 5089 | else |
| 5090 | m_launch_info.GetFlags().Clear(mask: lldb::eLaunchFlagDisableASLR); |
| 5091 | } |
| 5092 | |
| 5093 | void TargetProperties::InheritTCCValueChangedCallback() { |
| 5094 | if (GetInheritTCC()) |
| 5095 | m_launch_info.GetFlags().Set(lldb::eLaunchFlagInheritTCCFromParent); |
| 5096 | else |
| 5097 | m_launch_info.GetFlags().Clear(mask: lldb::eLaunchFlagInheritTCCFromParent); |
| 5098 | } |
| 5099 | |
| 5100 | void TargetProperties::DisableSTDIOValueChangedCallback() { |
| 5101 | if (GetDisableSTDIO()) |
| 5102 | m_launch_info.GetFlags().Set(lldb::eLaunchFlagDisableSTDIO); |
| 5103 | else |
| 5104 | m_launch_info.GetFlags().Clear(mask: lldb::eLaunchFlagDisableSTDIO); |
| 5105 | } |
| 5106 | |
| 5107 | bool TargetProperties::GetDebugUtilityExpression() const { |
| 5108 | const uint32_t idx = ePropertyDebugUtilityExpression; |
| 5109 | return GetPropertyAtIndexAs<bool>( |
| 5110 | idx, g_target_properties[idx].default_uint_value != 0); |
| 5111 | } |
| 5112 | |
| 5113 | void TargetProperties::SetDebugUtilityExpression(bool debug) { |
| 5114 | const uint32_t idx = ePropertyDebugUtilityExpression; |
| 5115 | SetPropertyAtIndex(idx, t: debug); |
| 5116 | } |
| 5117 | |
| 5118 | // Target::TargetEventData |
| 5119 | |
| 5120 | Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp) |
| 5121 | : EventData(), m_target_sp(target_sp), m_module_list() {} |
| 5122 | |
| 5123 | Target::TargetEventData::TargetEventData(const lldb::TargetSP &target_sp, |
| 5124 | const ModuleList &module_list) |
| 5125 | : EventData(), m_target_sp(target_sp), m_module_list(module_list) {} |
| 5126 | |
| 5127 | Target::TargetEventData::~TargetEventData() = default; |
| 5128 | |
| 5129 | llvm::StringRef Target::TargetEventData::GetFlavorString() { |
| 5130 | return "Target::TargetEventData" ; |
| 5131 | } |
| 5132 | |
| 5133 | void Target::TargetEventData::Dump(Stream *s) const { |
| 5134 | for (size_t i = 0; i < m_module_list.GetSize(); ++i) { |
| 5135 | if (i != 0) |
| 5136 | *s << ", " ; |
| 5137 | m_module_list.GetModuleAtIndex(idx: i)->GetDescription( |
| 5138 | s&: s->AsRawOstream(), level: lldb::eDescriptionLevelBrief); |
| 5139 | } |
| 5140 | } |
| 5141 | |
| 5142 | const Target::TargetEventData * |
| 5143 | Target::TargetEventData::GetEventDataFromEvent(const Event *event_ptr) { |
| 5144 | if (event_ptr) { |
| 5145 | const EventData *event_data = event_ptr->GetData(); |
| 5146 | if (event_data && |
| 5147 | event_data->GetFlavor() == TargetEventData::GetFlavorString()) |
| 5148 | return static_cast<const TargetEventData *>(event_ptr->GetData()); |
| 5149 | } |
| 5150 | return nullptr; |
| 5151 | } |
| 5152 | |
| 5153 | TargetSP Target::TargetEventData::GetTargetFromEvent(const Event *event_ptr) { |
| 5154 | TargetSP target_sp; |
| 5155 | const TargetEventData *event_data = GetEventDataFromEvent(event_ptr); |
| 5156 | if (event_data) |
| 5157 | target_sp = event_data->m_target_sp; |
| 5158 | return target_sp; |
| 5159 | } |
| 5160 | |
| 5161 | ModuleList |
| 5162 | Target::TargetEventData::GetModuleListFromEvent(const Event *event_ptr) { |
| 5163 | ModuleList module_list; |
| 5164 | const TargetEventData *event_data = GetEventDataFromEvent(event_ptr); |
| 5165 | if (event_data) |
| 5166 | module_list = event_data->m_module_list; |
| 5167 | return module_list; |
| 5168 | } |
| 5169 | |
| 5170 | std::recursive_mutex &Target::GetAPIMutex() { |
| 5171 | if (GetProcessSP() && GetProcessSP()->CurrentThreadIsPrivateStateThread()) |
| 5172 | return m_private_mutex; |
| 5173 | else |
| 5174 | return m_mutex; |
| 5175 | } |
| 5176 | |
| 5177 | /// Get metrics associated with this target in JSON format. |
| 5178 | llvm::json::Value |
| 5179 | Target::ReportStatistics(const lldb_private::StatisticsOptions &options) { |
| 5180 | return m_stats.ToJSON(target&: *this, options); |
| 5181 | } |
| 5182 | |
| 5183 | void Target::ResetStatistics() { m_stats.Reset(target&: *this); } |
| 5184 | |
| 5185 | bool Target::HasLoadedSections() { return !GetSectionLoadList().IsEmpty(); } |
| 5186 | |
| 5187 | lldb::addr_t Target::GetSectionLoadAddress(const lldb::SectionSP §ion_sp) { |
| 5188 | return GetSectionLoadList().GetSectionLoadAddress(section_sp); |
| 5189 | } |
| 5190 | |
| 5191 | void Target::ClearSectionLoadList() { GetSectionLoadList().Clear(); } |
| 5192 | |
| 5193 | void Target::DumpSectionLoadList(Stream &s) { |
| 5194 | GetSectionLoadList().Dump(s, target: this); |
| 5195 | } |
| 5196 | |