1349cc55cSDimitry Andric //===-- ObjectFileMinidump.cpp --------------------------------------------===//
2349cc55cSDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6349cc55cSDimitry Andric //
7349cc55cSDimitry Andric //===----------------------------------------------------------------------===//
8349cc55cSDimitry Andric
9349cc55cSDimitry Andric #include "ObjectFileMinidump.h"
10349cc55cSDimitry Andric
11349cc55cSDimitry Andric #include "MinidumpFileBuilder.h"
12349cc55cSDimitry Andric
13349cc55cSDimitry Andric #include "lldb/Core/ModuleSpec.h"
14349cc55cSDimitry Andric #include "lldb/Core/PluginManager.h"
15349cc55cSDimitry Andric #include "lldb/Core/Section.h"
16349cc55cSDimitry Andric #include "lldb/Target/Process.h"
17349cc55cSDimitry Andric
18349cc55cSDimitry Andric #include "llvm/Support/FileSystem.h"
19349cc55cSDimitry Andric
20349cc55cSDimitry Andric using namespace lldb;
21349cc55cSDimitry Andric using namespace lldb_private;
22349cc55cSDimitry Andric
LLDB_PLUGIN_DEFINE(ObjectFileMinidump)23349cc55cSDimitry Andric LLDB_PLUGIN_DEFINE(ObjectFileMinidump)
24349cc55cSDimitry Andric
25349cc55cSDimitry Andric void ObjectFileMinidump::Initialize() {
26349cc55cSDimitry Andric PluginManager::RegisterPlugin(
27349cc55cSDimitry Andric GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance,
28349cc55cSDimitry Andric CreateMemoryInstance, GetModuleSpecifications, SaveCore);
29349cc55cSDimitry Andric }
30349cc55cSDimitry Andric
Terminate()31349cc55cSDimitry Andric void ObjectFileMinidump::Terminate() {
32349cc55cSDimitry Andric PluginManager::UnregisterPlugin(CreateInstance);
33349cc55cSDimitry Andric }
34349cc55cSDimitry Andric
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)35349cc55cSDimitry Andric ObjectFile *ObjectFileMinidump::CreateInstance(
3681ad6265SDimitry Andric const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp,
37349cc55cSDimitry Andric lldb::offset_t data_offset, const lldb_private::FileSpec *file,
38349cc55cSDimitry Andric lldb::offset_t offset, lldb::offset_t length) {
39349cc55cSDimitry Andric return nullptr;
40349cc55cSDimitry Andric }
41349cc55cSDimitry Andric
CreateMemoryInstance(const lldb::ModuleSP & module_sp,WritableDataBufferSP data_sp,const ProcessSP & process_sp,lldb::addr_t header_addr)42349cc55cSDimitry Andric ObjectFile *ObjectFileMinidump::CreateMemoryInstance(
4381ad6265SDimitry Andric const lldb::ModuleSP &module_sp, WritableDataBufferSP data_sp,
44349cc55cSDimitry Andric const ProcessSP &process_sp, lldb::addr_t header_addr) {
45349cc55cSDimitry Andric return nullptr;
46349cc55cSDimitry Andric }
47349cc55cSDimitry Andric
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)48349cc55cSDimitry Andric size_t ObjectFileMinidump::GetModuleSpecifications(
49349cc55cSDimitry Andric const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
50349cc55cSDimitry Andric lldb::offset_t data_offset, lldb::offset_t file_offset,
51349cc55cSDimitry Andric lldb::offset_t length, lldb_private::ModuleSpecList &specs) {
52349cc55cSDimitry Andric specs.Clear();
53349cc55cSDimitry Andric return 0;
54349cc55cSDimitry Andric }
55349cc55cSDimitry Andric
SaveCore(const lldb::ProcessSP & process_sp,const lldb_private::FileSpec & outfile,lldb::SaveCoreStyle & core_style,lldb_private::Status & error)56349cc55cSDimitry Andric bool ObjectFileMinidump::SaveCore(const lldb::ProcessSP &process_sp,
57349cc55cSDimitry Andric const lldb_private::FileSpec &outfile,
58349cc55cSDimitry Andric lldb::SaveCoreStyle &core_style,
59349cc55cSDimitry Andric lldb_private::Status &error) {
60*c9157d92SDimitry Andric // Set default core style if it isn't set.
61*c9157d92SDimitry Andric if (core_style == SaveCoreStyle::eSaveCoreUnspecified)
62*c9157d92SDimitry Andric core_style = SaveCoreStyle::eSaveCoreStackOnly;
63349cc55cSDimitry Andric
64349cc55cSDimitry Andric if (!process_sp)
65349cc55cSDimitry Andric return false;
66349cc55cSDimitry Andric
67349cc55cSDimitry Andric MinidumpFileBuilder builder;
68349cc55cSDimitry Andric
69349cc55cSDimitry Andric Target &target = process_sp->GetTarget();
70349cc55cSDimitry Andric
71349cc55cSDimitry Andric error = builder.AddSystemInfo(target.GetArchitecture().GetTriple());
72349cc55cSDimitry Andric if (error.Fail())
73349cc55cSDimitry Andric return false;
74349cc55cSDimitry Andric
75349cc55cSDimitry Andric error = builder.AddModuleList(target);
76349cc55cSDimitry Andric if (error.Fail())
77349cc55cSDimitry Andric return false;
78349cc55cSDimitry Andric
79349cc55cSDimitry Andric builder.AddMiscInfo(process_sp);
80349cc55cSDimitry Andric
81349cc55cSDimitry Andric error = builder.AddThreadList(process_sp);
82349cc55cSDimitry Andric if (error.Fail())
83349cc55cSDimitry Andric return false;
84349cc55cSDimitry Andric
85*c9157d92SDimitry Andric // Add any exceptions but only if there are any in any threads.
86*c9157d92SDimitry Andric builder.AddExceptions(process_sp);
87349cc55cSDimitry Andric
88*c9157d92SDimitry Andric error = builder.AddMemoryList(process_sp, core_style);
89349cc55cSDimitry Andric if (error.Fail())
90349cc55cSDimitry Andric return false;
91349cc55cSDimitry Andric
92349cc55cSDimitry Andric if (target.GetArchitecture().GetTriple().getOS() ==
93349cc55cSDimitry Andric llvm::Triple::OSType::Linux) {
94349cc55cSDimitry Andric builder.AddLinuxFileStreams(process_sp);
95349cc55cSDimitry Andric }
96349cc55cSDimitry Andric
97349cc55cSDimitry Andric llvm::Expected<lldb::FileUP> maybe_core_file = FileSystem::Instance().Open(
98349cc55cSDimitry Andric outfile, File::eOpenOptionWriteOnly | File::eOpenOptionCanCreate);
99349cc55cSDimitry Andric if (!maybe_core_file) {
100349cc55cSDimitry Andric error = maybe_core_file.takeError();
101349cc55cSDimitry Andric return false;
102349cc55cSDimitry Andric }
103349cc55cSDimitry Andric lldb::FileUP core_file = std::move(maybe_core_file.get());
104349cc55cSDimitry Andric
105349cc55cSDimitry Andric error = builder.Dump(core_file);
106349cc55cSDimitry Andric if (error.Fail())
107349cc55cSDimitry Andric return false;
108349cc55cSDimitry Andric
109349cc55cSDimitry Andric return true;
110349cc55cSDimitry Andric }
111