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