Path: blob/main/contrib/llvm-project/lldb/source/Target/ProcessTrace.cpp
39587 views
//===-- ProcessTrace.cpp --------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Target/ProcessTrace.h"910#include <memory>1112#include "lldb/Core/Module.h"13#include "lldb/Core/PluginManager.h"14#include "lldb/Core/Section.h"15#include "lldb/Target/ABI.h"16#include "lldb/Target/SectionLoadList.h"17#include "lldb/Target/Target.h"1819using namespace lldb;20using namespace lldb_private;2122LLDB_PLUGIN_DEFINE(ProcessTrace)2324llvm::StringRef ProcessTrace::GetPluginDescriptionStatic() {25return "Trace process plug-in.";26}2728void ProcessTrace::Terminate() {29PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance);30}3132ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,33ListenerSP listener_sp,34const FileSpec *crash_file,35bool can_connect) {36if (can_connect)37return nullptr;38return std::make_shared<ProcessTrace>(target_sp, listener_sp, *crash_file);39}4041bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {42return plugin_specified_by_name;43}4445ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp,46const FileSpec &core_file)47: PostMortemProcess(target_sp, listener_sp, core_file) {}4849ProcessTrace::~ProcessTrace() {50Clear();51// We need to call finalize on the process before destroying ourselves to52// make sure all of the broadcaster cleanup goes as planned. If we destruct53// this class, then Process::~Process() might have problems trying to fully54// destroy the broadcaster.55Finalize(true /* destructing */);56}5758void ProcessTrace::DidAttach(ArchSpec &process_arch) {59ListenerSP listener_sp(60Listener::MakeListener("lldb.process_trace.did_attach_listener"));61HijackProcessEvents(listener_sp);6263SetCanJIT(false);64StartPrivateStateThread();65SetPrivateState(eStateStopped);6667EventSP event_sp;68WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp);6970RestoreProcessEvents();7172Process::DidAttach(process_arch);73}7475bool ProcessTrace::DoUpdateThreadList(ThreadList &old_thread_list,76ThreadList &new_thread_list) {77return false;78}7980void ProcessTrace::RefreshStateAfterStop() {}8182Status ProcessTrace::DoDestroy() { return Status(); }8384size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,85Status &error) {86if (const ABISP &abi = GetABI())87addr = abi->FixAnyAddress(addr);8889// Don't allow the caching that lldb_private::Process::ReadMemory does since90// we have it all cached in the trace files.91return DoReadMemory(addr, buf, size, error);92}9394void ProcessTrace::Clear() { m_thread_list.Clear(); }9596void ProcessTrace::Initialize() {97static llvm::once_flag g_once_flag;9899llvm::call_once(g_once_flag, []() {100PluginManager::RegisterPlugin(GetPluginNameStatic(),101GetPluginDescriptionStatic(), CreateInstance);102});103}104105ArchSpec ProcessTrace::GetArchitecture() {106return GetTarget().GetArchitecture();107}108109bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo &info) {110info.Clear();111info.SetProcessID(GetID());112info.SetArchitecture(GetArchitecture());113ModuleSP module_sp = GetTarget().GetExecutableModule();114if (module_sp) {115const bool add_exe_file_as_first_arg = false;116info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),117add_exe_file_as_first_arg);118}119return true;120}121122size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,123Status &error) {124Address resolved_address;125GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);126127return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,128error);129}130131132