|
| 1 | +//@ ignore-windows |
| 2 | +//@ ignore-apple |
| 3 | + |
| 4 | +// LLVM 17's embed-source implementation requires that source code is attached |
| 5 | +// for all files in the output DWARF debug info. This restriction was lifted in |
| 6 | +// LLVM 18 (87e22bdd2bd6d77d782f9d64b3e3ae5bdcd5080d). |
| 7 | +//@ min-llvm-version: 18 |
| 8 | + |
| 9 | +// This test should be replaced with one in tests/debuginfo once we can easily |
| 10 | +// tell via GDB or LLDB if debuginfo contains source code. Cheap tricks in LLDB |
| 11 | +// like setting an invalid source map path don't appear to work, maybe this'll |
| 12 | +// become easier once GDB supports DWARFv6? |
| 13 | + |
| 14 | +use std::collections::HashMap; |
| 15 | +use std::path::PathBuf; |
| 16 | +use std::rc::Rc; |
| 17 | + |
| 18 | +use gimli::{AttributeValue, EndianRcSlice, Reader, RunTimeEndian}; |
| 19 | +use object::{Object, ObjectSection}; |
| 20 | +use run_make_support::{gimli, object, rfs, rustc}; |
| 21 | + |
| 22 | +fn main() { |
| 23 | + let output = PathBuf::from("embed-source-main"); |
| 24 | + rustc() |
| 25 | + .input("main.rs") |
| 26 | + .output(&output) |
| 27 | + .arg("-g") |
| 28 | + .arg("-Zembed-source=yes") |
| 29 | + .arg("-Zdwarf-version=5") |
| 30 | + .run(); |
| 31 | + let output = rfs::read(output); |
| 32 | + let obj = object::File::parse(output.as_slice()).unwrap(); |
| 33 | + let endian = if obj.is_little_endian() { RunTimeEndian::Little } else { RunTimeEndian::Big }; |
| 34 | + let dwarf = gimli::Dwarf::load(|section| -> Result<_, ()> { |
| 35 | + let data = obj.section_by_name(section.name()).map(|s| s.uncompressed_data().unwrap()); |
| 36 | + Ok(EndianRcSlice::new(Rc::from(data.unwrap_or_default().as_ref()), endian)) |
| 37 | + }) |
| 38 | + .unwrap(); |
| 39 | + |
| 40 | + let mut sources = HashMap::new(); |
| 41 | + |
| 42 | + let mut iter = dwarf.units(); |
| 43 | + while let Some(header) = iter.next().unwrap() { |
| 44 | + let unit = dwarf.unit(header).unwrap(); |
| 45 | + let unit = unit.unit_ref(&dwarf); |
| 46 | + |
| 47 | + if let Some(program) = &unit.line_program { |
| 48 | + let header = program.header(); |
| 49 | + for file in header.file_names() { |
| 50 | + if let Some(source) = file.source() { |
| 51 | + let path = unit |
| 52 | + .attr_string(file.path_name()) |
| 53 | + .unwrap() |
| 54 | + .to_string_lossy() |
| 55 | + .unwrap() |
| 56 | + .to_string(); |
| 57 | + let source = |
| 58 | + unit.attr_string(source).unwrap().to_string_lossy().unwrap().to_string(); |
| 59 | + if !source.is_empty() { |
| 60 | + sources.insert(path, source); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + dbg!(&sources); |
| 68 | + assert_eq!(sources.len(), 1); |
| 69 | + assert_eq!(sources.get("main.rs").unwrap(), "// hello\nfn main() {}\n"); |
| 70 | +} |
0 commit comments