1b60736ecSDimitry Andric //===-- ProcessTrace.cpp --------------------------------------------------===//
2b60736ecSDimitry Andric //
3b60736ecSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4b60736ecSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5b60736ecSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b60736ecSDimitry Andric //
7b60736ecSDimitry Andric //===----------------------------------------------------------------------===//
8b60736ecSDimitry Andric
9b60736ecSDimitry Andric #include "lldb/Target/ProcessTrace.h"
10b60736ecSDimitry Andric
11b60736ecSDimitry Andric #include <memory>
12b60736ecSDimitry Andric
13b60736ecSDimitry Andric #include "lldb/Core/Module.h"
14b60736ecSDimitry Andric #include "lldb/Core/PluginManager.h"
15b60736ecSDimitry Andric #include "lldb/Core/Section.h"
16145449b1SDimitry Andric #include "lldb/Target/ABI.h"
17b60736ecSDimitry Andric #include "lldb/Target/SectionLoadList.h"
18b60736ecSDimitry Andric #include "lldb/Target/Target.h"
19b60736ecSDimitry Andric
20b60736ecSDimitry Andric using namespace lldb;
21b60736ecSDimitry Andric using namespace lldb_private;
22b60736ecSDimitry Andric
LLDB_PLUGIN_DEFINE(ProcessTrace)23950076cdSDimitry Andric LLDB_PLUGIN_DEFINE(ProcessTrace)
24950076cdSDimitry Andric
25c0981da4SDimitry Andric llvm::StringRef ProcessTrace::GetPluginDescriptionStatic() {
26b60736ecSDimitry Andric return "Trace process plug-in.";
27b60736ecSDimitry Andric }
28b60736ecSDimitry Andric
Terminate()29b60736ecSDimitry Andric void ProcessTrace::Terminate() {
30b60736ecSDimitry Andric PluginManager::UnregisterPlugin(ProcessTrace::CreateInstance);
31b60736ecSDimitry Andric }
32b60736ecSDimitry Andric
CreateInstance(TargetSP target_sp,ListenerSP listener_sp,const FileSpec * crash_file,bool can_connect)33b60736ecSDimitry Andric ProcessSP ProcessTrace::CreateInstance(TargetSP target_sp,
34b60736ecSDimitry Andric ListenerSP listener_sp,
35b60736ecSDimitry Andric const FileSpec *crash_file,
36b60736ecSDimitry Andric bool can_connect) {
37b60736ecSDimitry Andric if (can_connect)
38b60736ecSDimitry Andric return nullptr;
39ac9a064cSDimitry Andric return std::make_shared<ProcessTrace>(target_sp, listener_sp, *crash_file);
40b60736ecSDimitry Andric }
41b60736ecSDimitry Andric
CanDebug(TargetSP target_sp,bool plugin_specified_by_name)42b60736ecSDimitry Andric bool ProcessTrace::CanDebug(TargetSP target_sp, bool plugin_specified_by_name) {
43b60736ecSDimitry Andric return plugin_specified_by_name;
44b60736ecSDimitry Andric }
45b60736ecSDimitry Andric
ProcessTrace(TargetSP target_sp,ListenerSP listener_sp,const FileSpec & core_file)46ac9a064cSDimitry Andric ProcessTrace::ProcessTrace(TargetSP target_sp, ListenerSP listener_sp,
47ac9a064cSDimitry Andric const FileSpec &core_file)
48ac9a064cSDimitry Andric : PostMortemProcess(target_sp, listener_sp, core_file) {}
49b60736ecSDimitry Andric
~ProcessTrace()50b60736ecSDimitry Andric ProcessTrace::~ProcessTrace() {
51b60736ecSDimitry Andric Clear();
52b60736ecSDimitry Andric // We need to call finalize on the process before destroying ourselves to
53b60736ecSDimitry Andric // make sure all of the broadcaster cleanup goes as planned. If we destruct
54b60736ecSDimitry Andric // this class, then Process::~Process() might have problems trying to fully
55b60736ecSDimitry Andric // destroy the broadcaster.
56b1c73532SDimitry Andric Finalize(true /* destructing */);
57b60736ecSDimitry Andric }
58b60736ecSDimitry Andric
DidAttach(ArchSpec & process_arch)59b60736ecSDimitry Andric void ProcessTrace::DidAttach(ArchSpec &process_arch) {
60b60736ecSDimitry Andric ListenerSP listener_sp(
61b60736ecSDimitry Andric Listener::MakeListener("lldb.process_trace.did_attach_listener"));
62b60736ecSDimitry Andric HijackProcessEvents(listener_sp);
63b60736ecSDimitry Andric
64b60736ecSDimitry Andric SetCanJIT(false);
65b60736ecSDimitry Andric StartPrivateStateThread();
66b60736ecSDimitry Andric SetPrivateState(eStateStopped);
67b60736ecSDimitry Andric
68b60736ecSDimitry Andric EventSP event_sp;
69e3b55780SDimitry Andric WaitForProcessToStop(std::nullopt, &event_sp, true, listener_sp);
70b60736ecSDimitry Andric
71b60736ecSDimitry Andric RestoreProcessEvents();
72b60736ecSDimitry Andric
73b60736ecSDimitry Andric Process::DidAttach(process_arch);
74b60736ecSDimitry Andric }
75b60736ecSDimitry Andric
DoUpdateThreadList(ThreadList & old_thread_list,ThreadList & new_thread_list)76b60736ecSDimitry Andric bool ProcessTrace::DoUpdateThreadList(ThreadList &old_thread_list,
77b60736ecSDimitry Andric ThreadList &new_thread_list) {
78b60736ecSDimitry Andric return false;
79b60736ecSDimitry Andric }
80b60736ecSDimitry Andric
RefreshStateAfterStop()81b60736ecSDimitry Andric void ProcessTrace::RefreshStateAfterStop() {}
82b60736ecSDimitry Andric
DoDestroy()83b60736ecSDimitry Andric Status ProcessTrace::DoDestroy() { return Status(); }
84b60736ecSDimitry Andric
ReadMemory(addr_t addr,void * buf,size_t size,Status & error)85b60736ecSDimitry Andric size_t ProcessTrace::ReadMemory(addr_t addr, void *buf, size_t size,
86b60736ecSDimitry Andric Status &error) {
87145449b1SDimitry Andric if (const ABISP &abi = GetABI())
88145449b1SDimitry Andric addr = abi->FixAnyAddress(addr);
89145449b1SDimitry Andric
90b60736ecSDimitry Andric // Don't allow the caching that lldb_private::Process::ReadMemory does since
91b60736ecSDimitry Andric // we have it all cached in the trace files.
92b60736ecSDimitry Andric return DoReadMemory(addr, buf, size, error);
93b60736ecSDimitry Andric }
94b60736ecSDimitry Andric
Clear()95b60736ecSDimitry Andric void ProcessTrace::Clear() { m_thread_list.Clear(); }
96b60736ecSDimitry Andric
Initialize()97b60736ecSDimitry Andric void ProcessTrace::Initialize() {
98b60736ecSDimitry Andric static llvm::once_flag g_once_flag;
99b60736ecSDimitry Andric
100b60736ecSDimitry Andric llvm::call_once(g_once_flag, []() {
101b60736ecSDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(),
102b60736ecSDimitry Andric GetPluginDescriptionStatic(), CreateInstance);
103b60736ecSDimitry Andric });
104b60736ecSDimitry Andric }
105b60736ecSDimitry Andric
GetArchitecture()106b60736ecSDimitry Andric ArchSpec ProcessTrace::GetArchitecture() {
107b60736ecSDimitry Andric return GetTarget().GetArchitecture();
108b60736ecSDimitry Andric }
109b60736ecSDimitry Andric
GetProcessInfo(ProcessInstanceInfo & info)110b60736ecSDimitry Andric bool ProcessTrace::GetProcessInfo(ProcessInstanceInfo &info) {
111b60736ecSDimitry Andric info.Clear();
112b60736ecSDimitry Andric info.SetProcessID(GetID());
113b60736ecSDimitry Andric info.SetArchitecture(GetArchitecture());
114b60736ecSDimitry Andric ModuleSP module_sp = GetTarget().GetExecutableModule();
115b60736ecSDimitry Andric if (module_sp) {
116b60736ecSDimitry Andric const bool add_exe_file_as_first_arg = false;
117b60736ecSDimitry Andric info.SetExecutableFile(GetTarget().GetExecutableModule()->GetFileSpec(),
118b60736ecSDimitry Andric add_exe_file_as_first_arg);
119b60736ecSDimitry Andric }
120b60736ecSDimitry Andric return true;
121b60736ecSDimitry Andric }
122b60736ecSDimitry Andric
DoReadMemory(addr_t addr,void * buf,size_t size,Status & error)123b60736ecSDimitry Andric size_t ProcessTrace::DoReadMemory(addr_t addr, void *buf, size_t size,
124b60736ecSDimitry Andric Status &error) {
125b60736ecSDimitry Andric Address resolved_address;
126b60736ecSDimitry Andric GetTarget().GetSectionLoadList().ResolveLoadAddress(addr, resolved_address);
127b60736ecSDimitry Andric
128b60736ecSDimitry Andric return GetTarget().ReadMemoryFromFileCache(resolved_address, buf, size,
129b60736ecSDimitry Andric error);
130b60736ecSDimitry Andric }
131