| 1 |
use super::*; |
| 2 |
use crate::linux::maps_reader::MappingInfo; |
| 3 |
use crate::linux::module_reader::{BuildId, ReadFromModule, SoName}; |
| 4 |
|
| 5 |
/// Write information about the mappings in effect. Because we are using the |
| 6 |
/// minidump format, the information about the mappings is pretty limited. |
| 7 |
/// Because of this, we also include the full, unparsed, /proc/$x/maps file in |
| 8 |
/// another stream in the file. |
| 9 |
pub fn write( |
| 10 |
config: &mut MinidumpWriter, |
| 11 |
buffer: &mut DumpBuf, |
| 12 |
dumper: &mut PtraceDumper, |
| 13 |
) -> Result<MDRawDirectory, errors::SectionMappingsError> { |
| 14 |
let mut modules = Vec::new(); |
| 15 |
|
| 16 |
// First write all the mappings from the dumper |
| 17 |
for map_idx in 0..dumper.mappings.len() { |
| 18 |
// If the mapping is uninteresting, or if |
| 19 |
// there is caller-provided information about this mapping |
| 20 |
// in the user_mapping_list list, skip it |
| 21 |
|
| 22 |
if !dumper.mappings[map_idx].is_interesting() |
| 23 |
|| dumper.mappings[map_idx].is_contained_in(&config.user_mapping_list) |
| 24 |
{ |
| 25 |
continue; |
| 26 |
} |
| 27 |
log::debug!("retrieving build id for {:?}", &dumper.mappings[map_idx]); |
| 28 |
let BuildId(identifier) = dumper |
| 29 |
.from_process_memory_for_index(map_idx) |
| 30 |
.or_else(|e| { |
| 31 |
// If the mapping has an associated name that is a file, try to read the build id |
| 32 |
// from the file. If there is no note segment with the build id in |
| 33 |
// the program headers, we can't get to the note section if the section header |
| 34 |
// table isn't loaded. |
| 35 |
if let Some(path) = &dumper.mappings[map_idx].name { |
| 36 |
let path = std::path::Path::new(&path); |
| 37 |
if path.exists() { |
| 38 |
log::debug!("failed to get build id from process memory ({e}), attempting to retrieve from {}", path.display()); |
| 39 |
return BuildId::read_from_file(path) |
| 40 |
.map_err(errors::DumperError::ModuleReaderError); |
| 41 |
} |
| 42 |
log::debug!( |
| 43 |
"not attempting to get build id from {}: path does not exist", |
| 44 |
path.display() |
| 45 |
); |
| 46 |
} |
| 47 |
Err(e) |
| 48 |
}) |
| 49 |
.unwrap_or_else(|e| { |
| 50 |
log::warn!("failed to get build id for mapping: {e}"); |
| 51 |
BuildId(Vec::new()) |
| 52 |
}); |
| 53 |
|
| 54 |
// If the identifier is all 0, its an uninteresting mapping (bmc#1676109) |
| 55 |
if identifier.is_empty() || identifier.iter().all(|&x| x == 0) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
|
| 59 |
// SONAME should always be accessible through program headers alone, so we don't really |
| 60 |
// need to fall back to trying to read from the mapping file. |
| 61 |
let soname = dumper |
| 62 |
.from_process_memory_for_index(map_idx) |
| 63 |
.ok() |
| 64 |
.map(|SoName(n)| n); |
| 65 |
|
| 66 |
let module = fill_raw_module(buffer, &dumper.mappings[map_idx], &identifier, soname)?; |
| 67 |
modules.push(module); |
| 68 |
} |
| 69 |
|
| 70 |
// Next write all the mappings provided by the caller |
| 71 |
for user in &config.user_mapping_list { |
| 72 |
// GUID was provided by caller. |
| 73 |
let module = fill_raw_module(buffer, &user.mapping, &user.identifier, None)?; |
| 74 |
modules.push(module); |
| 75 |
} |
| 76 |
|
| 77 |
let list_header = MemoryWriter::<u32>::alloc_with_val(buffer, modules.len() as u32)?; |
| 78 |
|
| 79 |
let mut dirent = MDRawDirectory { |
| 80 |
stream_type: MDStreamType::ModuleListStream as u32, |
| 81 |
location: list_header.location(), |
| 82 |
}; |
| 83 |
|
| 84 |
if !modules.is_empty() { |
| 85 |
let mapping_list = MemoryArrayWriter::<MDRawModule>::alloc_from_iter(buffer, modules)?; |
| 86 |
dirent.location.data_size += mapping_list.location().data_size; |
| 87 |
} |
| 88 |
|
| 89 |
Ok(dirent) |
| 90 |
} |
| 91 |
|
| 92 |
fn fill_raw_module( |
| 93 |
buffer: &mut DumpBuf, |
| 94 |
mapping: &MappingInfo, |
| 95 |
identifier: &[u8], |
| 96 |
soname: Option<String>, |
| 97 |
) -> Result<MDRawModule, errors::SectionMappingsError> { |
| 98 |
let cv_record = if identifier.is_empty() { |
| 99 |
// Just zeroes |
| 100 |
Default::default() |
| 101 |
} else { |
| 102 |
let cv_signature = crate::minidump_format::format::CvSignature::Elf as u32; |
| 103 |
let array_size = std::mem::size_of_val(&cv_signature) + identifier.len(); |
| 104 |
|
| 105 |
let mut sig_section = MemoryArrayWriter::<u8>::alloc_array(buffer, array_size)?; |
| 106 |
for (index, val) in cv_signature |
| 107 |
.to_ne_bytes() |
| 108 |
.iter() |
| 109 |
.chain(identifier.iter()) |
| 110 |
.enumerate() |
| 111 |
{ |
| 112 |
sig_section.set_value_at(buffer, *val, index)?; |
| 113 |
} |
| 114 |
sig_section.location() |
| 115 |
}; |
| 116 |
|
| 117 |
let (file_path, _, so_version) = mapping |
| 118 |
.get_mapping_effective_path_name_and_version(soname) |
| 119 |
.map_err(|e| errors::SectionMappingsError::GetEffectivePathError(mapping.clone(), e))?; |
| 120 |
let name_header = write_string_to_location(buffer, file_path.to_string_lossy().as_ref())?; |
| 121 |
|
| 122 |
let version_info = so_version.map_or(Default::default(), |sov| format::VS_FIXEDFILEINFO { |
| 123 |
signature: format::VS_FFI_SIGNATURE, |
| 124 |
struct_version: format::VS_FFI_STRUCVERSION, |
| 125 |
file_version_hi: sov.major, |
| 126 |
file_version_lo: sov.minor, |
| 127 |
product_version_hi: sov.patch, |
| 128 |
product_version_lo: sov.prerelease, |
| 129 |
..Default::default() |
| 130 |
}); |
| 131 |
|
| 132 |
let raw_module = MDRawModule { |
| 133 |
base_of_image: mapping.start_address as u64, |
| 134 |
size_of_image: mapping.size as u32, |
| 135 |
cv_record, |
| 136 |
module_name_rva: name_header.rva, |
| 137 |
version_info, |
| 138 |
..Default::default() |
| 139 |
}; |
| 140 |
|
| 141 |
Ok(raw_module) |
| 142 |
} |