LLDB  mainline
ObjectFileMinidump.cpp
Go to the documentation of this file.
1 //===-- ObjectFileMinidump.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 "ObjectFileMinidump.h"
10 
11 #include "MinidumpFileBuilder.h"
12 
13 #include "lldb/Core/ModuleSpec.h"
15 #include "lldb/Core/Section.h"
16 #include "lldb/Target/Process.h"
17 
18 #include "llvm/Support/FileSystem.h"
19 
20 using namespace lldb;
21 using namespace lldb_private;
22 
24 
25 void ObjectFileMinidump::Initialize() {
26  PluginManager::RegisterPlugin(
27  GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
28  CreateMemoryInstance, GetModuleSpecifications, SaveCore);
29 }
30 
32  PluginManager::UnregisterPlugin(CreateInstance);
33 }
34 
36  static ConstString g_name("minidump");
37  return g_name;
38 }
39 
41  const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
42  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
43  lldb::offset_t offset, lldb::offset_t length) {
44  return nullptr;
45 }
46 
48  const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
49  const ProcessSP &process_sp, lldb::addr_t header_addr) {
50  return nullptr;
51 }
52 
54  const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
55  lldb::offset_t data_offset, lldb::offset_t file_offset,
57  specs.Clear();
58  return 0;
59 }
60 
61 bool ObjectFileMinidump::SaveCore(const lldb::ProcessSP &process_sp,
62  const lldb_private::FileSpec &outfile,
63  lldb::SaveCoreStyle &core_style,
65  if (core_style != SaveCoreStyle::eSaveCoreStackOnly) {
66  error.SetErrorString("Only stack minidumps supported yet.");
67  return false;
68  }
69 
70  if (!process_sp)
71  return false;
72 
73  MinidumpFileBuilder builder;
74 
75  Target &target = process_sp->GetTarget();
76 
77  error = builder.AddSystemInfo(target.GetArchitecture().GetTriple());
78  if (error.Fail())
79  return false;
80 
81  error = builder.AddModuleList(target);
82  if (error.Fail())
83  return false;
84 
85  builder.AddMiscInfo(process_sp);
86 
87  if (target.GetArchitecture().GetMachine() == llvm::Triple::ArchType::x86_64) {
88  error = builder.AddThreadList(process_sp);
89  if (error.Fail())
90  return false;
91 
92  error = builder.AddException(process_sp);
93  if (error.Fail())
94  return false;
95 
96  error = builder.AddMemoryList(process_sp);
97  if (error.Fail())
98  return false;
99  }
100 
101  if (target.GetArchitecture().GetTriple().getOS() ==
102  llvm::Triple::OSType::Linux) {
103  builder.AddLinuxFileStreams(process_sp);
104  }
105 
106  llvm::Expected<lldb::FileUP> maybe_core_file = FileSystem::Instance().Open(
107  outfile, File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate);
108  if (!maybe_core_file) {
109  error = maybe_core_file.takeError();
110  return false;
111  }
112  lldb::FileUP core_file = std::move(maybe_core_file.get());
113 
114  error = builder.Dump(core_file);
115  if (error.Fail())
116  return false;
117 
118  return true;
119 }
MinidumpFileBuilder::AddMemoryList
lldb_private::Status AddMemoryList(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:558
MinidumpFileBuilder::AddMiscInfo
void AddMiscInfo(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:628
lldb_private::ArchSpec::GetMachine
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:667
ModuleSpec.h
ObjectFileMinidump::GetPluginNameStatic
static lldb_private::ConstString GetPluginNameStatic()
Definition: ObjectFileMinidump.cpp:35
lldb_private::ModuleSpecList
Definition: ModuleSpec.h:280
ObjectFileMinidump::SaveCore
static bool SaveCore(const lldb::ProcessSP &process_sp, const lldb_private::FileSpec &outfile, lldb::SaveCoreStyle &core_style, lldb_private::Status &error)
Definition: ObjectFileMinidump.cpp:61
lldb::offset_t
uint64_t offset_t
Definition: lldb-types.h:87
lldb_private::ArchSpec::GetTriple
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:444
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
ObjectFileMinidump::GetModuleSpecifications
static size_t GetModuleSpecifications(const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, lldb_private::ModuleSpecList &specs)
Definition: ObjectFileMinidump.cpp:53
lldb_private::Target
Definition: Target.h:450
Section.h
lldb::eSaveCoreStackOnly
@ eSaveCoreStackOnly
Definition: lldb-enumerations.h:1140
MinidumpFileBuilder::AddException
lldb_private::Status AddException(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:493
Process.h
MinidumpFileBuilder
Definition: MinidumpFileBuilder.h:40
lldb_private::FileSpec
Definition: FileSpec.h:56
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::ConstString
Definition: ConstString.h:40
ObjectFileMinidump.h
MinidumpFileBuilder::AddThreadList
lldb_private::Status AddThreadList(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:404
ObjectFileMinidump
Definition: ObjectFileMinidump.h:26
MinidumpFileBuilder::Dump
lldb_private::Status Dump(lldb::FileUP &core_file) const
Definition: MinidumpFileBuilder.cpp:705
lldb_private::ModuleSpecList::Clear
void Clear()
Definition: ModuleSpec.h:308
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:966
lldb_private::Status
Definition: Status.h:44
ObjectFileMinidump::Terminate
static void Terminate()
Definition: ObjectFileMinidump.cpp:31
lldb::SaveCoreStyle
SaveCoreStyle
Definition: lldb-enumerations.h:1136
PluginManager.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
MinidumpFileBuilder::AddSystemInfo
lldb_private::Status AddSystemInfo(const llvm::Triple &target_triple)
Definition: MinidumpFileBuilder.cpp:50
ObjectFileMinidump::CreateMemoryInstance
static lldb_private::ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
Definition: ObjectFileMinidump.cpp:47
MinidumpFileBuilder::AddModuleList
lldb_private::Status AddModuleList(lldb_private::Target &target)
Definition: MinidumpFileBuilder.cpp:194
MinidumpFileBuilder::AddLinuxFileStreams
void AddLinuxFileStreams(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:662
MinidumpFileBuilder.h
LLDB_PLUGIN_DEFINE
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
lldb
Definition: SBAddress.h:15
lldb_private::ObjectFile
Definition: ObjectFile.h:58
ObjectFileMinidump::CreateInstance
static lldb_private::ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const lldb_private::FileSpec *file, lldb::offset_t offset, lldb::offset_t length)
Definition: ObjectFileMinidump.cpp:40