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  const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp,
37  lldb::offset_t data_offset, const lldb_private::FileSpec *file,
38  lldb::offset_t offset, lldb::offset_t length) {
39  return nullptr;
40 }
41 
43  const lldb::ModuleSP &module_sp, DataBufferSP &data_sp,
44  const ProcessSP &process_sp, lldb::addr_t header_addr) {
45  return nullptr;
46 }
47 
49  const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
50  lldb::offset_t data_offset, lldb::offset_t file_offset,
52  specs.Clear();
53  return 0;
54 }
55 
56 bool ObjectFileMinidump::SaveCore(const lldb::ProcessSP &process_sp,
57  const lldb_private::FileSpec &outfile,
58  lldb::SaveCoreStyle &core_style,
60  if (core_style != SaveCoreStyle::eSaveCoreStackOnly) {
61  error.SetErrorString("Only stack minidumps supported yet.");
62  return false;
63  }
64 
65  if (!process_sp)
66  return false;
67 
68  MinidumpFileBuilder builder;
69 
70  Target &target = process_sp->GetTarget();
71 
72  error = builder.AddSystemInfo(target.GetArchitecture().GetTriple());
73  if (error.Fail())
74  return false;
75 
76  error = builder.AddModuleList(target);
77  if (error.Fail())
78  return false;
79 
80  builder.AddMiscInfo(process_sp);
81 
82  if (target.GetArchitecture().GetMachine() == llvm::Triple::ArchType::x86_64) {
83  error = builder.AddThreadList(process_sp);
84  if (error.Fail())
85  return false;
86 
87  error = builder.AddException(process_sp);
88  if (error.Fail())
89  return false;
90 
91  error = builder.AddMemoryList(process_sp);
92  if (error.Fail())
93  return false;
94  }
95 
96  if (target.GetArchitecture().GetTriple().getOS() ==
97  llvm::Triple::OSType::Linux) {
98  builder.AddLinuxFileStreams(process_sp);
99  }
100 
101  llvm::Expected<lldb::FileUP> maybe_core_file = FileSystem::Instance().Open(
102  outfile, File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate);
103  if (!maybe_core_file) {
104  error = maybe_core_file.takeError();
105  return false;
106  }
107  lldb::FileUP core_file = std::move(maybe_core_file.get());
108 
109  error = builder.Dump(core_file);
110  if (error.Fail())
111  return false;
112 
113  return true;
114 }
MinidumpFileBuilder::AddMemoryList
lldb_private::Status AddMemoryList(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:559
MinidumpFileBuilder::AddMiscInfo
void AddMiscInfo(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:629
lldb_private::ArchSpec::GetMachine
llvm::Triple::ArchType GetMachine() const
Returns a machine family for the current architecture.
Definition: ArchSpec.cpp:667
ModuleSpec.h
lldb_private::ModuleSpecList
Definition: ModuleSpec.h:274
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:56
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:48
lldb_private::Target
Definition: Target.h:452
Section.h
lldb::eSaveCoreStackOnly
@ eSaveCoreStackOnly
Definition: lldb-enumerations.h:1140
MinidumpFileBuilder::AddException
lldb_private::Status AddException(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:494
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
ObjectFileMinidump.h
MinidumpFileBuilder::AddThreadList
lldb_private::Status AddThreadList(const lldb::ProcessSP &process_sp)
Definition: MinidumpFileBuilder.cpp:405
ObjectFileMinidump
Definition: ObjectFileMinidump.h:26
MinidumpFileBuilder::Dump
lldb_private::Status Dump(lldb::FileUP &core_file) const
Definition: MinidumpFileBuilder.cpp:706
lldb_private::ModuleSpecList::Clear
void Clear()
Definition: ModuleSpec.h:302
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:968
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:42
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:663
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:60
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:35