Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 1 | //===-- ObjectFileBreakpad.cpp -------------------------------- -*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h" |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 10 | #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h" |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 11 | #include "lldb/Core/ModuleSpec.h" |
| 12 | #include "lldb/Core/PluginManager.h" |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Section.h" |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 14 | |
| 15 | using namespace lldb; |
| 16 | using namespace lldb_private; |
| 17 | using namespace lldb_private::breakpad; |
| 18 | |
| 19 | namespace { |
| 20 | struct Header { |
| 21 | ArchSpec arch; |
| 22 | UUID uuid; |
| 23 | static llvm::Optional<Header> parse(llvm::StringRef text); |
| 24 | }; |
| 25 | } // namespace |
| 26 | |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 27 | llvm::Optional<Header> Header::parse(llvm::StringRef text) { |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 28 | llvm::StringRef line; |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 29 | std::tie(line, text) = text.split('\n'); |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 30 | auto Module = ModuleRecord::parse(line); |
| 31 | if (!Module) |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 32 | return llvm::None; |
| 33 | |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 34 | llvm::Triple triple; |
Pavel Labath | 5b18ddb | 2019-01-24 04:17:59 +0000 | [diff] [blame] | 35 | triple.setArch(Module->Arch); |
| 36 | triple.setOS(Module->OS); |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 37 | |
| 38 | std::tie(line, text) = text.split('\n'); |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 39 | |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 40 | auto Info = InfoRecord::parse(line); |
Pavel Labath | 5b18ddb | 2019-01-24 04:17:59 +0000 | [diff] [blame] | 41 | UUID uuid = Info && Info->ID ? Info->ID : Module->ID; |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 42 | return Header{ArchSpec(triple), std::move(uuid)}; |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Pavel Labath | e84f784 | 2019-07-31 11:57:34 +0000 | [diff] [blame^] | 45 | char ObjectFileBreakpad::ID; |
| 46 | |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 47 | void ObjectFileBreakpad::Initialize() { |
| 48 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 49 | GetPluginDescriptionStatic(), CreateInstance, |
Pavel Labath | 871f2b6 | 2018-12-10 18:17:53 +0000 | [diff] [blame] | 50 | CreateMemoryInstance, GetModuleSpecifications); |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void ObjectFileBreakpad::Terminate() { |
| 54 | PluginManager::UnregisterPlugin(CreateInstance); |
| 55 | } |
| 56 | |
| 57 | ConstString ObjectFileBreakpad::GetPluginNameStatic() { |
| 58 | static ConstString g_name("breakpad"); |
| 59 | return g_name; |
| 60 | } |
| 61 | |
| 62 | ObjectFile *ObjectFileBreakpad::CreateInstance( |
| 63 | const ModuleSP &module_sp, DataBufferSP &data_sp, offset_t data_offset, |
| 64 | const FileSpec *file, offset_t file_offset, offset_t length) { |
| 65 | if (!data_sp) { |
| 66 | data_sp = MapFileData(*file, length, file_offset); |
| 67 | if (!data_sp) |
| 68 | return nullptr; |
| 69 | data_offset = 0; |
| 70 | } |
| 71 | auto text = toStringRef(data_sp->GetData()); |
| 72 | llvm::Optional<Header> header = Header::parse(text); |
| 73 | if (!header) |
| 74 | return nullptr; |
| 75 | |
| 76 | // Update the data to contain the entire file if it doesn't already |
| 77 | if (data_sp->GetByteSize() < length) { |
| 78 | data_sp = MapFileData(*file, length, file_offset); |
| 79 | if (!data_sp) |
| 80 | return nullptr; |
| 81 | data_offset = 0; |
| 82 | } |
| 83 | |
| 84 | return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file, |
| 85 | file_offset, length, std::move(header->arch), |
| 86 | std::move(header->uuid)); |
| 87 | } |
| 88 | |
| 89 | ObjectFile *ObjectFileBreakpad::CreateMemoryInstance( |
| 90 | const ModuleSP &module_sp, DataBufferSP &data_sp, |
| 91 | const ProcessSP &process_sp, addr_t header_addr) { |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
| 95 | size_t ObjectFileBreakpad::GetModuleSpecifications( |
| 96 | const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset, |
| 97 | offset_t file_offset, offset_t length, ModuleSpecList &specs) { |
| 98 | auto text = toStringRef(data_sp->GetData()); |
| 99 | llvm::Optional<Header> header = Header::parse(text); |
| 100 | if (!header) |
| 101 | return 0; |
| 102 | ModuleSpec spec(file, std::move(header->arch)); |
| 103 | spec.GetUUID() = std::move(header->uuid); |
| 104 | specs.Append(spec); |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp, |
| 109 | DataBufferSP &data_sp, |
| 110 | offset_t data_offset, |
| 111 | const FileSpec *file, offset_t offset, |
| 112 | offset_t length, ArchSpec arch, |
| 113 | UUID uuid) |
| 114 | : ObjectFile(module_sp, file, offset, length, data_sp, data_offset), |
| 115 | m_arch(std::move(arch)), m_uuid(std::move(uuid)) {} |
| 116 | |
| 117 | bool ObjectFileBreakpad::ParseHeader() { |
| 118 | // We already parsed the header during initialization. |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | Symtab *ObjectFileBreakpad::GetSymtab() { |
| 123 | // TODO |
| 124 | return nullptr; |
| 125 | } |
| 126 | |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 127 | void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) { |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 128 | if (m_sections_up) |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 129 | return; |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 130 | m_sections_up = llvm::make_unique<SectionList>(); |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 131 | |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 132 | llvm::Optional<Record::Kind> current_section; |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 133 | offset_t section_start; |
| 134 | llvm::StringRef text = toStringRef(m_data.GetData()); |
| 135 | uint32_t next_section_id = 1; |
| 136 | auto maybe_add_section = [&](const uint8_t *end_ptr) { |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 137 | if (!current_section) |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 138 | return; // We have been called before parsing the first line. |
| 139 | |
| 140 | offset_t end_offset = end_ptr - m_data.GetDataStart(); |
| 141 | auto section_sp = std::make_shared<Section>( |
| 142 | GetModule(), this, next_section_id++, |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 143 | ConstString(toString(*current_section)), eSectionTypeOther, |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 144 | /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start, |
| 145 | end_offset - section_start, /*log2align*/ 0, /*flags*/ 0); |
Jonas Devlieghere | d5b4403 | 2019-02-13 06:25:41 +0000 | [diff] [blame] | 146 | m_sections_up->AddSection(section_sp); |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 147 | unified_section_list.AddSection(section_sp); |
| 148 | }; |
| 149 | while (!text.empty()) { |
| 150 | llvm::StringRef line; |
| 151 | std::tie(line, text) = text.split('\n'); |
| 152 | |
Pavel Labath | dfaafbc | 2019-04-04 13:23:25 +0000 | [diff] [blame] | 153 | llvm::Optional<Record::Kind> next_section = Record::classify(line); |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 154 | if (next_section == Record::Line) { |
| 155 | // Line records logically belong to the preceding Func record, so we put |
| 156 | // them in the same section. |
| 157 | next_section = Record::Func; |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 158 | } |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 159 | if (next_section == current_section) |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 160 | continue; |
| 161 | |
| 162 | // Changing sections, finish off the previous one, if there was any. |
| 163 | maybe_add_section(line.bytes_begin()); |
| 164 | // And start a new one. |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 165 | current_section = next_section; |
Pavel Labath | ed42ea4 | 2019-01-07 11:14:08 +0000 | [diff] [blame] | 166 | section_start = line.bytes_begin() - m_data.GetDataStart(); |
| 167 | } |
| 168 | // Finally, add the last section. |
| 169 | maybe_add_section(m_data.GetDataEnd()); |
Pavel Labath | 1f6b247 | 2018-12-10 17:16:38 +0000 | [diff] [blame] | 170 | } |