summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibCoredump/Reader.cpp
blob: e071f472ad7f8321d0c3c12a325380c47be9856f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/ByteReader.h>
#include <AK/Function.h>
#include <AK/HashTable.h>
#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <AK/LexicalPath.h>
#include <LibCompress/Gzip.h>
#include <LibCoredump/Reader.h>
#include <LibFileSystem/FileSystem.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

namespace Coredump {

OwnPtr<Reader> Reader::create(StringView path)
{
    auto file_or_error = Core::MappedFile::map(path);
    if (file_or_error.is_error())
        return {};

    if (!Compress::GzipDecompressor::is_likely_compressed(file_or_error.value()->bytes())) {
        // It's an uncompressed coredump.
        return AK::adopt_own_if_nonnull(new (nothrow) Reader(file_or_error.release_value()));
    }

    auto decompressed_data = decompress_coredump(file_or_error.value()->bytes());
    if (!decompressed_data.has_value())
        return {};
    return adopt_own_if_nonnull(new (nothrow) Reader(decompressed_data.release_value()));
}

Reader::Reader(ByteBuffer buffer)
    : Reader(buffer.bytes())
{
    m_coredump_buffer = move(buffer);
}

Reader::Reader(NonnullRefPtr<Core::MappedFile> file)
    : Reader(file->bytes())
{
    m_mapped_file = move(file);
}

Reader::Reader(ReadonlyBytes coredump_bytes)
    : m_coredump_bytes(coredump_bytes)
    , m_coredump_image(m_coredump_bytes)
{
    size_t index = 0;
    m_coredump_image.for_each_program_header([this, &index](auto pheader) {
        if (pheader.type() == PT_NOTE) {
            m_notes_segment_index = index;
            return IterationDecision::Break;
        }
        ++index;
        return IterationDecision::Continue;
    });
    VERIFY(m_notes_segment_index != -1);
}

Optional<ByteBuffer> Reader::decompress_coredump(ReadonlyBytes raw_coredump)
{
    auto decompressed_coredump = Compress::GzipDecompressor::decompress_all(raw_coredump);
    if (!decompressed_coredump.is_error())
        return decompressed_coredump.release_value();

    // If we didn't manage to decompress it, try and parse it as decompressed coredump
    auto bytebuffer = ByteBuffer::copy(raw_coredump);
    if (bytebuffer.is_error())
        return {};
    return bytebuffer.release_value();
}

Reader::NotesEntryIterator::NotesEntryIterator(u8 const* notes_data)
    : m_current(bit_cast<const ELF::Core::NotesEntry*>(notes_data))
    , start(notes_data)
{
}

ELF::Core::NotesEntryHeader::Type Reader::NotesEntryIterator::type() const
{
    VERIFY(m_current->header.type == ELF::Core::NotesEntryHeader::Type::ProcessInfo
        || m_current->header.type == ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo
        || m_current->header.type == ELF::Core::NotesEntryHeader::Type::ThreadInfo
        || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Metadata
        || m_current->header.type == ELF::Core::NotesEntryHeader::Type::Null);
    return m_current->header.type;
}

const ELF::Core::NotesEntry* Reader::NotesEntryIterator::current() const
{
    return m_current;
}

void Reader::NotesEntryIterator::next()
{
    VERIFY(!at_end());
    switch (type()) {
    case ELF::Core::NotesEntryHeader::Type::ProcessInfo: {
        auto const* current = bit_cast<const ELF::Core::ProcessInfo*>(m_current);
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
        break;
    }
    case ELF::Core::NotesEntryHeader::Type::ThreadInfo: {
        auto const* current = bit_cast<const ELF::Core::ThreadInfo*>(m_current);
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current + 1);
        break;
    }
    case ELF::Core::NotesEntryHeader::Type::MemoryRegionInfo: {
        auto const* current = bit_cast<const ELF::Core::MemoryRegionInfo*>(m_current);
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current->region_name + strlen(current->region_name) + 1);
        break;
    }
    case ELF::Core::NotesEntryHeader::Type::Metadata: {
        auto const* current = bit_cast<const ELF::Core::Metadata*>(m_current);
        m_current = bit_cast<const ELF::Core::NotesEntry*>(current->json_data + strlen(current->json_data) + 1);
        break;
    }
    default:
        VERIFY_NOT_REACHED();
    }
}

bool Reader::NotesEntryIterator::at_end() const
{
    return type() == ELF::Core::NotesEntryHeader::Type::Null;
}

Optional<FlatPtr> Reader::peek_memory(FlatPtr address) const
{
    auto region = region_containing(address);
    if (!region.has_value())
        return {};

    FlatPtr offset_in_region = address - region->region_start;
    auto* region_data = bit_cast<u8 const*>(image().program_header(region->program_header_index).raw_data());
    FlatPtr value { 0 };
    ByteReader::load(region_data + offset_in_region, value);
    return value;
}

const JsonObject Reader::process_info() const
{
    const ELF::Core::ProcessInfo* process_info_notes_entry = nullptr;
    NotesEntryIterator it(bit_cast<u8 const*>(m_coredump_image.program_header(m_notes_segment_index).raw_data()));
    for (; !it.at_end(); it.next()) {
        if (it.type() != ELF::Core::NotesEntryHeader::Type::ProcessInfo)
            continue;
        process_info_notes_entry = bit_cast<const ELF::Core::ProcessInfo*>(it.current());
        break;
    }
    if (!process_info_notes_entry)
        return {};
    auto const* json_data_ptr = process_info_notes_entry->json_data;
    auto process_info_json_value = JsonValue::from_string({ json_data_ptr, strlen(json_data_ptr) });
    if (process_info_json_value.is_error())
        return {};
    if (!process_info_json_value.value().is_object())
        return {};
    return process_info_json_value.value().as_object();
    // FIXME: Maybe just cache this on the Reader instance after first access.
}

Optional<MemoryRegionInfo> Reader::first_region_for_object(StringView object_name) const
{
    Optional<MemoryRegionInfo> ret;
    for_each_memory_region_info([&ret, &object_name](auto& region_info) {
        if (region_info.object_name() == object_name) {
            ret = region_info;
            return IterationDecision::Break;
        }
        return IterationDecision::Continue;
    });
    return ret;
}

Optional<MemoryRegionInfo> Reader::region_containing(FlatPtr address) const
{
    Optional<MemoryRegionInfo> ret;
    for_each_memory_region_info([&ret, address](auto const& region_info) {
        if (region_info.region_start <= address && region_info.region_end >= address) {
            ret = region_info;
            return IterationDecision::Break;
        }
        return IterationDecision::Continue;
    });
    return ret;
}

int Reader::process_pid() const
{
    auto process_info = this->process_info();
    auto pid = process_info.get_integer<int>("pid"sv).value_or(0);
    return pid;
}

u8 Reader::process_termination_signal() const
{
    auto process_info = this->process_info();
    auto termination_signal = process_info.get_u8("termination_signal"sv);
    if (!termination_signal.has_value() || *termination_signal <= SIGINVAL || *termination_signal >= NSIG)
        return SIGINVAL;
    return *termination_signal;
}

DeprecatedString Reader::process_executable_path() const
{
    auto process_info = this->process_info();
    auto executable_path = process_info.get_deprecated_string("executable_path"sv);
    return executable_path.value_or({});
}

Vector<DeprecatedString> Reader::process_arguments() const
{
    auto process_info = this->process_info();
    auto arguments = process_info.get_array("arguments"sv);
    if (!arguments.has_value())
        return {};
    Vector<DeprecatedString> vector;
    arguments->for_each([&](auto& value) {
        if (value.is_string())
            vector.append(value.as_string());
    });
    return vector;
}

Vector<DeprecatedString> Reader::process_environment() const
{
    auto process_info = this->process_info();
    auto environment = process_info.get_array("environment"sv);
    if (!environment.has_value())
        return {};
    Vector<DeprecatedString> vector;
    environment->for_each([&](auto& value) {
        if (value.is_string())
            vector.append(value.as_string());
    });
    return vector;
}

HashMap<DeprecatedString, DeprecatedString> Reader::metadata() const
{
    const ELF::Core::Metadata* metadata_notes_entry = nullptr;
    NotesEntryIterator it(bit_cast<u8 const*>(m_coredump_image.program_header(m_notes_segment_index).raw_data()));
    for (; !it.at_end(); it.next()) {
        if (it.type() != ELF::Core::NotesEntryHeader::Type::Metadata)
            continue;
        metadata_notes_entry = bit_cast<const ELF::Core::Metadata*>(it.current());
        break;
    }
    if (!metadata_notes_entry)
        return {};
    auto const* json_data_ptr = metadata_notes_entry->json_data;
    auto metadata_json_value = JsonValue::from_string({ json_data_ptr, strlen(json_data_ptr) });
    if (metadata_json_value.is_error())
        return {};
    if (!metadata_json_value.value().is_object())
        return {};
    HashMap<DeprecatedString, DeprecatedString> metadata;
    metadata_json_value.value().as_object().for_each_member([&](auto& key, auto& value) {
        metadata.set(key, value.as_string_or({}));
    });
    return metadata;
}

Reader::LibraryData const* Reader::library_containing(FlatPtr address) const
{
    static HashMap<DeprecatedString, OwnPtr<LibraryData>> cached_libs;
    auto region = region_containing(address);
    if (!region.has_value())
        return {};

    auto name = region->object_name();
    DeprecatedString path = resolve_object_path(name);

    if (!cached_libs.contains(path)) {
        auto file_or_error = Core::MappedFile::map(path);
        if (file_or_error.is_error())
            return {};
        auto image = ELF::Image(file_or_error.value()->bytes());
        cached_libs.set(path, make<LibraryData>(name, static_cast<FlatPtr>(region->region_start), file_or_error.release_value(), move(image)));
    }

    auto lib_data = cached_libs.get(path).value();
    return lib_data;
}

DeprecatedString Reader::resolve_object_path(StringView name) const
{
    // TODO: There are other places where similar method is implemented or would be useful.
    //       (e.g. UserspaceEmulator, LibSymbolication, Profiler, and DynamicLinker itself)
    //       We should consider creating unified implementation in the future.

    if (name.starts_with('/') || !FileSystem::looks_like_shared_library(name)) {
        return name;
    }

    Vector<DeprecatedString> library_search_directories;

    // If LD_LIBRARY_PATH is present, check its folders first
    for (auto& environment_variable : process_environment()) {
        auto prefix = "LD_LIBRARY_PATH="sv;
        if (environment_variable.starts_with(prefix)) {
            auto ld_library_path = environment_variable.substring_view(prefix.length());

            // FIXME: This code won't handle folders with ":" in the name correctly.
            for (auto directory : ld_library_path.split_view(':')) {
                library_search_directories.append(directory);
            }
        }
    }

    // Add default paths that DynamicLinker uses
    library_search_directories.append("/usr/lib/"sv);
    library_search_directories.append("/usr/local/lib/"sv);

    // Search for the first readable library file
    for (auto& directory : library_search_directories) {
        auto full_path = LexicalPath::join(directory, name).string();

        if (access(full_path.characters(), R_OK) != 0)
            continue;

        return full_path;
    }

    return name;
}

void Reader::for_each_library(Function<void(LibraryInfo)> func) const
{
    HashTable<DeprecatedString> libraries;
    for_each_memory_region_info([&](auto const& region) {
        auto name = region.object_name();
        if (name.is_null() || libraries.contains(name))
            return IterationDecision::Continue;

        libraries.set(name);

        DeprecatedString path = resolve_object_path(name);

        func(LibraryInfo { name, path, static_cast<FlatPtr>(region.region_start) });
        return IterationDecision::Continue;
    });
}

}