|
| 1 | +//===-- PostMortemProcess.cpp -----------------------------------*- C++ -*-===// |
| 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 "lldb/Target/PostMortemProcess.h" |
| 10 | + |
| 11 | +#include "lldb/Core/Module.h" |
| 12 | +#include "lldb/Utility/LLDBLog.h" |
| 13 | +#include "lldb/lldb-forward.h" |
| 14 | + |
| 15 | +using namespace lldb; |
| 16 | +using namespace lldb_private; |
| 17 | + |
| 18 | +lldb::addr_t PostMortemProcess::FindInMemory(lldb::addr_t low, |
| 19 | + lldb::addr_t high, |
| 20 | + const uint8_t *buf, size_t size) { |
| 21 | + const size_t region_size = high - low; |
| 22 | + if (region_size < size) |
| 23 | + return LLDB_INVALID_ADDRESS; |
| 24 | + |
| 25 | + llvm::ArrayRef<uint8_t> data = PeekMemory(low, high); |
| 26 | + if (data.empty()) { |
| 27 | + LLDB_LOG(GetLog(LLDBLog::Process), |
| 28 | + "Failed to get contiguous memory region for search. low: 0x{}, " |
| 29 | + "high: 0x{}. Failling back to Process::FindInMemory", |
| 30 | + low, high); |
| 31 | + // In an edge case when the search has to happen across non-contiguous |
| 32 | + // memory, we will have to fall back on the Process::FindInMemory. |
| 33 | + return Process::FindInMemory(low, high, buf, size); |
| 34 | + } |
| 35 | + |
| 36 | + return Process::FindInMemoryGeneric(data, low, high, buf, size); |
| 37 | +} |
| 38 | + |
| 39 | +llvm::ArrayRef<uint8_t> PostMortemProcess::PeekMemory(lldb::addr_t low, |
| 40 | + lldb::addr_t high) { |
| 41 | + return {}; |
| 42 | +} |
| 43 | + |
| 44 | +llvm::ArrayRef<uint8_t> |
| 45 | +PostMortemProcess::DoPeekMemory(lldb::ModuleSP &core_module_sp, |
| 46 | + VMRangeToFileOffset &core_aranges, |
| 47 | + lldb::addr_t low, lldb::addr_t high) { |
| 48 | + |
| 49 | + ObjectFile *core_objfile = core_module_sp->GetObjectFile(); |
| 50 | + |
| 51 | + if (core_objfile == nullptr) { |
| 52 | + return {}; |
| 53 | + } |
| 54 | + |
| 55 | + const VMRangeToFileOffset::Entry *core_memory_entry = |
| 56 | + core_aranges.FindEntryThatContains(low); |
| 57 | + if (core_memory_entry == nullptr || core_memory_entry->GetRangeEnd() < low) { |
| 58 | + return {}; |
| 59 | + } |
| 60 | + const lldb::addr_t offset = low - core_memory_entry->GetRangeBase(); |
| 61 | + const lldb::addr_t file_start = core_memory_entry->data.GetRangeBase(); |
| 62 | + const lldb::addr_t file_end = core_memory_entry->data.GetRangeEnd(); |
| 63 | + |
| 64 | + if (file_start == file_end) { |
| 65 | + return {}; |
| 66 | + } |
| 67 | + size_t bytes_available = 0; |
| 68 | + if (file_end > file_start + offset) |
| 69 | + bytes_available = file_end - (file_start + offset); |
| 70 | + |
| 71 | + size_t bytes_to_read = high - low; |
| 72 | + bytes_to_read = std::min(bytes_to_read, bytes_available); |
| 73 | + if (bytes_to_read == 0) { |
| 74 | + return {}; |
| 75 | + } |
| 76 | + DataExtractor extractor; |
| 77 | + core_objfile->GetData(core_memory_entry->data.GetRangeBase() + offset, |
| 78 | + bytes_to_read, extractor); |
| 79 | + if (extractor.GetByteSize() != bytes_to_read) { |
| 80 | + return {}; |
| 81 | + } |
| 82 | + return extractor.GetData(); |
| 83 | +} |
0 commit comments