753 lines
29 KiB
Plaintext
753 lines
29 KiB
Plaintext
#! @file codeview.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains the writer of an executable's debugging information for Windows'
|
||
|
|
#! debuggers, as a PDB file beside it holding CodeView: the types of the
|
||
|
|
#! variables, each function's code with its frame, its variables and where
|
||
|
|
#! each is over which code, the calls inlined into it with their lines, the
|
||
|
|
#! lines of its code, and the names a debugger finds each function by. It
|
||
|
|
#! describes what the DWARF in the executable does, which other debuggers read.
|
||
|
|
#!
|
||
|
|
#! Every function makes its frame of `rbp` as it begins, so each variable in
|
||
|
|
#! its frame is at an offset from `rbp`, sixteen bytes below where the DWARF
|
||
|
|
#! names it from, the caller's frame. The executable names the file, which is
|
||
|
|
#! named by a GUID made from what it holds once it is written.
|
||
|
|
|
||
|
|
import std::io;
|
||
|
|
import sakura::compiler;
|
||
|
|
import binary::amd64;
|
||
|
|
import binary::elf;
|
||
|
|
import binary::pdb;
|
||
|
|
import binary::pe;
|
||
|
|
module sakura::native;
|
||
|
|
|
||
|
|
|
||
|
|
#! How far `rbp` is below the caller's frame, which the DWARF names variables
|
||
|
|
#! from: past the return address and the saved `rbp`.
|
||
|
|
const rbp_below_frame: i64 = 16;
|
||
|
|
|
||
|
|
#! The most bytes of code one range of a variable holds.
|
||
|
|
const range_most: u64 = 0xFFFF;
|
||
|
|
|
||
|
|
#! What writing the PDB file keeps.
|
||
|
|
struct codeview_writer {
|
||
|
|
var b: &building;
|
||
|
|
var file: pdb::writer;
|
||
|
|
var symbols: pdb::symbol_stream;
|
||
|
|
var checksums: pdb::file_checksums;
|
||
|
|
var inlinees: pdb::inlinee_lines;
|
||
|
|
var lines: byte_writer; #! The subsections of each function's lines.
|
||
|
|
var section: u16; #! The section holding the code, from 1.
|
||
|
|
var directory: strview; #! The directory the build was in.
|
||
|
|
var paths: vector<string>; #! The path of each file named, as a debugger finds it.
|
||
|
|
var file_at: vector<u32>; #! Where each source's entry is among the checksums, or `never`.
|
||
|
|
var ids: vector<u32>; #! The ID of each function inlined somewhere, or 0.
|
||
|
|
var complete: vector<u32>; #! The index of each type, by key, or 0.
|
||
|
|
var declared: vector<u32>; #! The index of each struct's declaration, by key, or 0.
|
||
|
|
var procedure: u32; #! The type every function is given.
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The path of the PDB file an executable's debugging information is written
|
||
|
|
#! to: the executable's, with `.pdb` in place of `.exe`, within the directory
|
||
|
|
#! the build was in where it is relative.
|
||
|
|
#! @param output The executable's path.
|
||
|
|
#! @param directory The directory the build was in.
|
||
|
|
#! @returns The path.
|
||
|
|
pub def pdb_path_of(output: strview, directory: strview): string {
|
||
|
|
var stem := output;
|
||
|
|
if output.length >= 4 {
|
||
|
|
val ending := output.substr(output.length - 4, 4).lower();
|
||
|
|
if ending.view() == ".exe" {
|
||
|
|
stem = output.substr(0, output.length - 4);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return $"{stem}.pdb" if is_absolute(stem);
|
||
|
|
return $"{directory}/{stem}.pdb";
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a path is absolute, on Linux or Windows.
|
||
|
|
#! @param path The path.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def is_absolute(path: strview): bool {
|
||
|
|
return false if path.length == 0;
|
||
|
|
return true if path[0] == '/' as u8 || path[0] == '\\' as u8;
|
||
|
|
return path.length > 1 && path[1] == ':' as u8;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The key of the type of a word, which no layout is, of an address of nothing
|
||
|
|
#! the program says, and of an `f32` held as a real, as DWARF's are.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @returns The key.
|
||
|
|
def codeview_writer::word_key(&this): u64 {
|
||
|
|
return this.b.c.program.layouts.length;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! @copydoc codeview_writer::word_key
|
||
|
|
def codeview_writer::address_key(&this): u64 {
|
||
|
|
return this.b.c.program.layouts.length + 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! @copydoc codeview_writer::word_key
|
||
|
|
def codeview_writer::widened_key(&this): u64 {
|
||
|
|
return this.b.c.program.layouts.length + 2;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Where a source's entry is among the checksums, naming it the first time:
|
||
|
|
#! its path, within the directory the build was in where it is relative.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param source The source.
|
||
|
|
#! @returns Where its entry is.
|
||
|
|
def codeview_writer::checksum(&mut this, source: u32): u32 {
|
||
|
|
if this.file_at[source] != never {
|
||
|
|
return this.file_at[source];
|
||
|
|
}
|
||
|
|
val written := this.b.c.checker.units[source].source.path;
|
||
|
|
var path := is_absolute(written) ? string::from(written, nothing {}) : $"{this.directory}/{written}";
|
||
|
|
val name := this.file.names.offset_of(path.view());
|
||
|
|
this.paths.append(move(&mut path));
|
||
|
|
val at := this.checksums.file(name);
|
||
|
|
this.file_at[source] = at;
|
||
|
|
return at;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Types.
|
||
|
|
|
||
|
|
#! The type of an integer of a size.
|
||
|
|
#! @param size Its bytes.
|
||
|
|
#! @param signed Whether it is signed.
|
||
|
|
#! @returns The type.
|
||
|
|
def integer_type(size: u64, signed: bool): u32 {
|
||
|
|
if size == 1 { return signed ? pdb::t_int1 : pdb::t_uint1; }
|
||
|
|
if size == 2 { return signed ? pdb::t_int2 : pdb::t_uint2; }
|
||
|
|
if size == 4 { return signed ? pdb::t_int4 : pdb::t_uint4; }
|
||
|
|
return signed ? pdb::t_int8 : pdb::t_uint8;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The name of a layout's type, as problems write it.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param shape The layout.
|
||
|
|
#! @returns The name.
|
||
|
|
def codeview_writer::type_name(&this, shape: &layout): string {
|
||
|
|
if shape.type_id != 0 {
|
||
|
|
var text := this.b.c.checker.describe(shape.type_id);
|
||
|
|
if text.length != 0 {
|
||
|
|
return move(&mut text);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if shape.name != 0 {
|
||
|
|
return string::from(this.b.c.checker.names.text(shape.name), nothing {});
|
||
|
|
}
|
||
|
|
return string::from("?", nothing {});
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The unique name of a layout's type, which tells it apart from another of the
|
||
|
|
#! same name, as one generic's two instances.
|
||
|
|
#! @param key The type's key.
|
||
|
|
#! @returns The name.
|
||
|
|
def unique_name(key: u64): string {
|
||
|
|
return $"sakura@{key}";
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a layout is written as a struct, which a pointer names by its
|
||
|
|
#! declaration, so a struct reaching itself through one is written.
|
||
|
|
#! @param shape The layout.
|
||
|
|
#! @returns Whether it is.
|
||
|
|
def written_as_struct(shape: &layout): bool {
|
||
|
|
return shape.kind == lk::record || shape.kind == lk::variant || shape.kind == lk::trait_reference || shape.kind == lk::function;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The index of a type, writing it and those it names the first time.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param key The type's key: its layout, or one past the layouts or two or three.
|
||
|
|
#! @returns The index.
|
||
|
|
def codeview_writer::type_of(&mut this, key: u64): u32 {
|
||
|
|
return this.complete[key] if this.complete[key] != 0;
|
||
|
|
val made := this.make_type(key);
|
||
|
|
this.complete[key] = made;
|
||
|
|
return made;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The index of what a pointer or reference reaches: a struct's declaration,
|
||
|
|
#! or the type itself.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param key The key of what it reaches.
|
||
|
|
#! @returns The index.
|
||
|
|
def codeview_writer::reached(&mut this, key: u64): u32 {
|
||
|
|
val shape: layout = this.b.c.program.layouts[key];
|
||
|
|
return this.type_of(key) if !written_as_struct(&shape);
|
||
|
|
if this.declared[key] == 0 {
|
||
|
|
val name := this.type_name(&shape);
|
||
|
|
val unique := unique_name(key);
|
||
|
|
this.declared[key] = this.file.types.structure(name.view(), unique.view(), 0, 0, 0, true);
|
||
|
|
}
|
||
|
|
return this.declared[key];
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Adds the members of a struct or union: each field but those holding nothing,
|
||
|
|
#! at its offset.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param shape The layout.
|
||
|
|
#! @param list The members.
|
||
|
|
def codeview_writer::fields(&mut this, shape: &layout, list: &mut pdb::field_list): void {
|
||
|
|
for f in 0..shape.count {
|
||
|
|
val field: slot_field = this.b.c.program.fields[shape.fields as u64 + f as u64];
|
||
|
|
continue if field.layout == never || this.b.c.program.layouts[field.layout].kind == lk::empty;
|
||
|
|
val type_index := this.type_of(field.layout as u64);
|
||
|
|
list.member(type_index, field.offset, this.b.c.checker.names.text(field.name));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Adds a struct's record, its members given.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param key The type's key.
|
||
|
|
#! @param shape The layout.
|
||
|
|
#! @param list The members.
|
||
|
|
#! @returns Its index.
|
||
|
|
def codeview_writer::structure(&mut this, key: u64, shape: &layout, list: &pdb::field_list): u32 {
|
||
|
|
val name := this.type_name(shape);
|
||
|
|
val unique := unique_name(key);
|
||
|
|
val members := this.file.types.fields(list);
|
||
|
|
return this.file.types.structure(name.view(), unique.view(), members, list.count, shape.size, false);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes a type's record, and those it names.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param key The type's key.
|
||
|
|
#! @returns Its index.
|
||
|
|
def codeview_writer::make_type(&mut this, key: u64): u32 {
|
||
|
|
return pdb::t_uint8 if key == this.word_key();
|
||
|
|
return pdb::t_64p | pdb::t_void if key == this.address_key();
|
||
|
|
return pdb::t_real64 if key == this.widened_key();
|
||
|
|
|
||
|
|
val program := &this.b.c.program;
|
||
|
|
val shape: layout = program.layouts[key];
|
||
|
|
var list := pdb::field_list::new();
|
||
|
|
match shape.kind {
|
||
|
|
# An enum is laid out as its integer, with a field for each member whose
|
||
|
|
# offset is the member's value.
|
||
|
|
lk::signed, lk::unsigned => {
|
||
|
|
val underlying := integer_type(shape.size, shape.kind == lk::signed);
|
||
|
|
return underlying if shape.name == 0 || shape.count == 0;
|
||
|
|
for f in 0..shape.count {
|
||
|
|
val field: slot_field = program.fields[shape.fields as u64 + f as u64];
|
||
|
|
list.enumerate(field.offset as i64, this.b.c.checker.names.text(field.name));
|
||
|
|
}
|
||
|
|
val name := this.type_name(&shape);
|
||
|
|
val unique := unique_name(key);
|
||
|
|
val members := this.file.types.fields(&list);
|
||
|
|
return this.file.types.enumeration(name.view(), unique.view(), underlying, members, list.count, false);
|
||
|
|
}
|
||
|
|
lk::floating => return shape.size == 4 ? pdb::t_real32 : pdb::t_real64;
|
||
|
|
lk::boolean => return pdb::t_bool08;
|
||
|
|
|
||
|
|
# A reference is the address of what it refers to.
|
||
|
|
lk::pointer, lk::reference => {
|
||
|
|
if shape.element == 0 || program.layouts[shape.element - 1].kind == lk::empty {
|
||
|
|
return pdb::t_64p | pdb::t_void;
|
||
|
|
}
|
||
|
|
val reached := this.reached(shape.element as u64 - 1);
|
||
|
|
return this.file.types.pointer(reached, shape.kind == lk::reference);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A reference to a trait is a reference, then the index of the vtable.
|
||
|
|
lk::trait_reference => {
|
||
|
|
list.member(pdb::t_64p | pdb::t_void, 0, "address");
|
||
|
|
list.member(pdb::t_uint8, 8, "vtable");
|
||
|
|
return this.structure(key, &shape, &list);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A function value is its function's index plus one, then its environment.
|
||
|
|
lk::function => {
|
||
|
|
list.member(pdb::t_uint8, 0, "function");
|
||
|
|
list.member(pdb::t_64p | pdb::t_void, 8, "environment");
|
||
|
|
return this.structure(key, &shape, &list);
|
||
|
|
}
|
||
|
|
lk::fixed => {
|
||
|
|
val element := this.type_of(shape.element as u64 - 1);
|
||
|
|
return this.file.types.array(element, shape.size);
|
||
|
|
}
|
||
|
|
lk::record => {
|
||
|
|
this.fields(&shape, &mut list);
|
||
|
|
return this.structure(key, &shape, &list);
|
||
|
|
}
|
||
|
|
|
||
|
|
# A union laid out without a tag is its fields over one another, the empty
|
||
|
|
# one held where the first word is null. One with a tag is the tag, then each
|
||
|
|
# field over one another past it, as a struct holding an unnamed union is.
|
||
|
|
lk::variant => {
|
||
|
|
if shape.niche == 0 {
|
||
|
|
var tags := pdb::field_list::new();
|
||
|
|
for f in 0..shape.count {
|
||
|
|
val field: slot_field = program.fields[shape.fields as u64 + f as u64];
|
||
|
|
tags.enumerate(f as i64, this.b.c.checker.names.text(field.name));
|
||
|
|
}
|
||
|
|
val name := this.type_name(&shape);
|
||
|
|
val tag_name := $"{name}::tag";
|
||
|
|
val tag_unique := $"sakura@{key}.tag";
|
||
|
|
val members := this.file.types.fields(&tags);
|
||
|
|
val tag := this.file.types.enumeration(tag_name.view(), tag_unique.view(), pdb::t_uint8, members, tags.count, false);
|
||
|
|
list.member(tag, 0, "tag");
|
||
|
|
}
|
||
|
|
this.fields(&shape, &mut list);
|
||
|
|
return this.structure(key, &shape, &list);
|
||
|
|
}
|
||
|
|
else => {
|
||
|
|
return this.structure(key, &shape, &list);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return pdb::t_notype;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes the definition of each struct declared only, so each declaration is
|
||
|
|
#! found to one, and those their definitions declare in turn.
|
||
|
|
#! @param this The writer.
|
||
|
|
def codeview_writer::define_declared(&mut this): void {
|
||
|
|
var again := true;
|
||
|
|
while again {
|
||
|
|
again = false;
|
||
|
|
for key in 0..this.declared.length {
|
||
|
|
if this.declared[key] != 0 && this.complete[key] == 0 {
|
||
|
|
_ = this.type_of(key);
|
||
|
|
again = true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Variables.
|
||
|
|
|
||
|
|
#! The code of a function from one offset to another, in ranges of at most
|
||
|
|
#! `range_most` bytes, each given to `each`.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param index The function.
|
||
|
|
#! @param from Where it begins, within the function's code.
|
||
|
|
#! @param to Where it ends.
|
||
|
|
#! @param out The ranges, added.
|
||
|
|
def codeview_writer::ranges_of(&this, index: u32, from: u64, to: u64, out: &mut vector<pdb::code_range>): void {
|
||
|
|
var at := from;
|
||
|
|
while at < to {
|
||
|
|
val length := to - at > range_most ? range_most : to - at;
|
||
|
|
out.append(pdb::code_range { section = this.section, offset = (this.b.starts[index] + at) as u32, length = length as u16 });
|
||
|
|
at += length;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes a variable its frame holds: its name and type, and where it is from
|
||
|
|
#! `rbp`, wherever its scope is, or over the code its scope covers. One of an
|
||
|
|
#! object of the heap is the address of that object, which the frame holds.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param index The function.
|
||
|
|
#! @param each The variable.
|
||
|
|
#! @param scoped Whether it is where its scope's code is, rather than wherever
|
||
|
|
#! its scope is, as a parameter is.
|
||
|
|
def codeview_writer::framed_variable(&mut this, index: u32, each: &placed_variable, scoped: bool): void {
|
||
|
|
var type_index := this.type_of(each.layout as u64);
|
||
|
|
if each.boxed {
|
||
|
|
type_index = this.file.types.pointer(type_index, false);
|
||
|
|
}
|
||
|
|
this.symbols.local(type_index, each.param ? pdb::local_parameter : 0, this.b.c.checker.names.text(each.name));
|
||
|
|
val offset := (rbp_below_frame + each.offset) as i32;
|
||
|
|
if !scoped {
|
||
|
|
this.symbols.in_frame_throughout(offset);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
var ranges := vector::<pdb::code_range>::new();
|
||
|
|
this.ranges_of(index, each.from as u64, each.to as u64, &mut ranges);
|
||
|
|
for &over in ranges {
|
||
|
|
this.symbols.in_frame(offset, over);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a word of a variable held in values can be named where it is over a
|
||
|
|
#! range: in a register, or, but for the address of an object of the heap, in
|
||
|
|
#! the frame. A word known, as a constant, can't be.
|
||
|
|
#! @param kind Where the word is, as a `word_spot`.
|
||
|
|
#! @param boxed Whether the word is the address of the object the variable is in.
|
||
|
|
#! @returns Whether it can.
|
||
|
|
def nameable(kind: u8, boxed: bool): bool {
|
||
|
|
return kind == word_spot::gpr as u8 || kind == word_spot::xmm as u8 || (kind == word_spot::frame as u8 && !boxed);
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes a variable held in values: its name and type, then where each of its
|
||
|
|
#! words is over each range of code it is somewhere in, where that can be
|
||
|
|
#! named. One named nowhere is said to be optimized out.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param index The function.
|
||
|
|
#! @param each The variable.
|
||
|
|
def codeview_writer::listed_variable(&mut this, index: u32, each: &placed_variable): void {
|
||
|
|
val shape: layout = this.b.c.program.layouts[each.layout];
|
||
|
|
val widened := !each.boxed && !each.narrow && shape.kind == lk::floating && shape.size == 4;
|
||
|
|
val type_index := this.type_of(widened ? this.widened_key() : each.layout as u64);
|
||
|
|
val words := each.boxed ? 1u : word_count(&shape) as u64;
|
||
|
|
|
||
|
|
var named := false;
|
||
|
|
for r in each.ranges..each.ranges + each.count {
|
||
|
|
val range: located_range = this.b.debug.ranges[r as u64];
|
||
|
|
for w in 0..words {
|
||
|
|
named = named || nameable(range.kinds[w], each.boxed);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
var flags: u16 = each.param ? pdb::local_parameter : 0;
|
||
|
|
if !named {
|
||
|
|
flags = flags | 0x0100; # Optimized out.
|
||
|
|
}
|
||
|
|
this.symbols.local(type_index, flags, this.b.c.checker.names.text(each.name));
|
||
|
|
|
||
|
|
var ranges := vector::<pdb::code_range>::new();
|
||
|
|
for r in each.ranges..each.ranges + each.count {
|
||
|
|
val range: located_range = this.b.debug.ranges[r as u64];
|
||
|
|
ranges.clear();
|
||
|
|
this.ranges_of(index, range.from as u64, range.to as u64, &mut ranges);
|
||
|
|
for &over in ranges {
|
||
|
|
for w in 0..words {
|
||
|
|
val kind := range.kinds[w];
|
||
|
|
val spot := range.spots[w];
|
||
|
|
continue if !nameable(kind, each.boxed);
|
||
|
|
if words > 1 {
|
||
|
|
# Each word of a variable of several is a part of it, where it is in
|
||
|
|
# a register; the frame holds none of a part's.
|
||
|
|
if kind == word_spot::gpr as u8 {
|
||
|
|
this.symbols.part_in_register(pdb::amd64_register(spot as u8, 8), (8 * w) as u32, over);
|
||
|
|
} else if kind == word_spot::xmm as u8 {
|
||
|
|
this.symbols.part_in_register(pdb::amd64_xmm(spot as u8), (8 * w) as u32, over);
|
||
|
|
}
|
||
|
|
} else if kind == word_spot::gpr as u8 {
|
||
|
|
if each.boxed {
|
||
|
|
this.symbols.through_register(pdb::amd64_register(spot as u8, 8), 0, over);
|
||
|
|
} else {
|
||
|
|
val width := shape.size == 1 || shape.size == 2 || shape.size == 4 ? shape.size : 8;
|
||
|
|
this.symbols.in_register(pdb::amd64_register(spot as u8, width), over);
|
||
|
|
}
|
||
|
|
} else if kind == word_spot::xmm as u8 {
|
||
|
|
this.symbols.in_register(pdb::amd64_xmm(spot as u8), over);
|
||
|
|
} else {
|
||
|
|
this.symbols.in_frame((rbp_below_frame + spot as i64) as i32, over);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes the variables of a function's own code, or of a site inlined into it:
|
||
|
|
#! its parameters, then its locals.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param index The function.
|
||
|
|
#! @param site One more than the site, or 0 for the function's own code.
|
||
|
|
def codeview_writer::variables(&mut this, index: u32, site: u32): void {
|
||
|
|
val debug := &this.b.debug;
|
||
|
|
for v in debug.variable_first[index]..debug.variable_first[index + 1] {
|
||
|
|
val each: placed_variable = debug.variables[v as u64];
|
||
|
|
continue if each.site != site || this.b.c.program.layouts[each.layout].kind == lk::empty;
|
||
|
|
if each.held {
|
||
|
|
this.listed_variable(index, &each);
|
||
|
|
} else if each.param || site != 0 {
|
||
|
|
this.framed_variable(index, &each, false);
|
||
|
|
} else if each.to > each.from {
|
||
|
|
this.framed_variable(index, &each, true);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Inline sites.
|
||
|
|
|
||
|
|
#! The ID of a function inlined somewhere, naming where it begins among the
|
||
|
|
#! inlinees' lines the first time.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param function The function.
|
||
|
|
#! @returns Its ID.
|
||
|
|
def codeview_writer::inlinee(&mut this, function: u32): u32 {
|
||
|
|
return this.ids[function] if this.ids[function] != 0;
|
||
|
|
val inlined := &this.b.c.program.functions[function];
|
||
|
|
val id := this.file.ids.function_id(0, this.procedure, inlined.name.view());
|
||
|
|
var source: u32 = 0;
|
||
|
|
val located := this.b.source_of(function, 0, &mut source);
|
||
|
|
this.inlinees.add(id, this.checksum(source), located.line as u32);
|
||
|
|
this.ids[function] = id;
|
||
|
|
return id;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! The site inlined directly within another that a site is within, or the site
|
||
|
|
#! itself where it is directly within.
|
||
|
|
#! @param sites The function's inline sites.
|
||
|
|
#! @param site One more than the site.
|
||
|
|
#! @param outer One more than the other, or 0 for the function's own code.
|
||
|
|
#! @returns One more than the site directly within.
|
||
|
|
def directly_within(sites: &vector<inline_site>, site: u32, outer: u32): u32 {
|
||
|
|
var at := site;
|
||
|
|
var depth: u64 = 0;
|
||
|
|
while at != 0 && depth <= sites.length {
|
||
|
|
val parent := sites[at as u64 - 1].parent;
|
||
|
|
return at if parent == outer;
|
||
|
|
at = parent;
|
||
|
|
depth++;
|
||
|
|
}
|
||
|
|
return site;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Whether a row of a function's line table holds code: no later one begins
|
||
|
|
#! where it does, so it is the line of the code there, as DWARF's readers take
|
||
|
|
#! the last row at an offset.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param row The row.
|
||
|
|
#! @param last One past the function's last row.
|
||
|
|
#! @returns Whether it does.
|
||
|
|
def codeview_writer::holds_code(&this, row: u64, last: u64): bool {
|
||
|
|
return row + 1 >= last || this.b.rows[row + 1].offset != this.b.rows[row].offset;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes the sites inlined directly within a site of a function, or within its
|
||
|
|
#! own code, each with the annotations of the code it is: each run of its code,
|
||
|
|
#! and the line of each, where the code of a site within it is at the line it
|
||
|
|
#! was called at. Each holds its variables and the sites within it.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param index The function.
|
||
|
|
#! @param parent One more than the site, or 0.
|
||
|
|
def codeview_writer::sites(&mut this, index: u32, parent: u32): void {
|
||
|
|
val sites := &this.b.c.program.functions[index].body.sites;
|
||
|
|
val first := this.b.row_first[index] as u64;
|
||
|
|
val last := this.b.row_first[index + 1] as u64;
|
||
|
|
val size := this.b.ends[index] - this.b.starts[index];
|
||
|
|
for k in 0..sites.length {
|
||
|
|
val site: inline_site = sites[k];
|
||
|
|
continue if site.parent != parent;
|
||
|
|
val number := k as u32 + 1;
|
||
|
|
|
||
|
|
# The rows holding code from the site's first to its last. A site with
|
||
|
|
# none is no code's, as the DWARF says too.
|
||
|
|
var begins := last;
|
||
|
|
var ends := last;
|
||
|
|
for r in first..last {
|
||
|
|
continue if !this.holds_code(r, last) || !within(sites, this.b.rows[r].site, number);
|
||
|
|
if begins == last {
|
||
|
|
begins = r;
|
||
|
|
}
|
||
|
|
ends = r + 1;
|
||
|
|
}
|
||
|
|
continue if begins == last;
|
||
|
|
|
||
|
|
var file: u32 = 0;
|
||
|
|
val start := this.b.source_of(site.function, 0, &mut file);
|
||
|
|
val inlinee := this.inlinee(site.function);
|
||
|
|
var notes := pdb::annotations::new(this.checksum(file), start.line as u32);
|
||
|
|
for r in begins..ends {
|
||
|
|
continue if !this.holds_code(r, last);
|
||
|
|
val row: line_row = this.b.rows[r];
|
||
|
|
if !within(sites, row.site, number) {
|
||
|
|
notes.away(row.offset as u32);
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
# Code of a site within this one is at the line that site was called at.
|
||
|
|
var at := row.at;
|
||
|
|
if row.site != number {
|
||
|
|
at = sites[directly_within(sites, row.site, number) as u64 - 1].at;
|
||
|
|
}
|
||
|
|
var source: u32 = 0;
|
||
|
|
val located := this.b.source_of(index, at, &mut source);
|
||
|
|
notes.at_line(row.offset as u32, this.checksum(source), located.line as u32);
|
||
|
|
}
|
||
|
|
val end := ends < last ? this.b.rows[ends].offset : size;
|
||
|
|
notes.finish(end as u32);
|
||
|
|
|
||
|
|
this.symbols.inline_site(inlinee, notes.bytes.view());
|
||
|
|
this.variables(index, number);
|
||
|
|
this.sites(index, number);
|
||
|
|
this.symbols.end();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# Functions.
|
||
|
|
|
||
|
|
#! A line of a function's code.
|
||
|
|
struct code_line {
|
||
|
|
var offset: u64; #! Where its code begins, within the function's.
|
||
|
|
var file: u32; #! Its file, as its entry among the checksums.
|
||
|
|
var line: u32;
|
||
|
|
var column: u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes the lines of a function's code: each row of its own code at its own
|
||
|
|
#! line, and the code of each site inlined into it at the line it was called
|
||
|
|
#! at, once for each run. Each run of lines of one file is a part of the code
|
||
|
|
#! of its own, so a reader taking one file's lines alone finds each where the
|
||
|
|
#! code is, as a function whose globals are from several files has.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param index The function.
|
||
|
|
def codeview_writer::function_lines(&mut this, index: u32): void {
|
||
|
|
val sites := &this.b.c.program.functions[index].body.sites;
|
||
|
|
val size := this.b.ends[index] - this.b.starts[index];
|
||
|
|
var found := vector::<code_line>::new();
|
||
|
|
val rows_end := this.b.row_first[index + 1] as u64;
|
||
|
|
for r in this.b.row_first[index]..this.b.row_first[index + 1] {
|
||
|
|
continue if !this.holds_code(r as u64, rows_end);
|
||
|
|
val row: line_row = this.b.rows[r as u64];
|
||
|
|
var at := row.at;
|
||
|
|
if row.site != 0 {
|
||
|
|
at = sites[directly_within(sites, row.site, 0) as u64 - 1].at;
|
||
|
|
}
|
||
|
|
var source: u32 = 0;
|
||
|
|
val located := this.b.source_of(index, at, &mut source);
|
||
|
|
val made := code_line { offset = row.offset, file = this.checksum(source), line = located.line as u32, column = located.column as u32 };
|
||
|
|
if row.site != 0 && found.length != 0 {
|
||
|
|
val last := found.last();
|
||
|
|
continue if made.file == last.file && made.line == last.line && made.column == last.column;
|
||
|
|
}
|
||
|
|
found.append(made);
|
||
|
|
}
|
||
|
|
|
||
|
|
var first: u64 = 0;
|
||
|
|
while first < found.length {
|
||
|
|
var end := first + 1;
|
||
|
|
while end < found.length && found[end].file == found[first].file {
|
||
|
|
end++;
|
||
|
|
}
|
||
|
|
val begins := found[first].offset;
|
||
|
|
val ends := end < found.length ? found[end].offset : size;
|
||
|
|
var lines := pdb::function_lines::new(this.section, (this.b.starts[index] + begins) as u32, (ends - begins) as u32);
|
||
|
|
for k in first..end {
|
||
|
|
val each: code_line = found[k];
|
||
|
|
lines.line(each.file, (each.offset - begins) as u32, each.line, each.column as u16);
|
||
|
|
}
|
||
|
|
lines.finish();
|
||
|
|
pdb::subsection(&mut this.lines, pdb::debug_s_lines, lines.bytes.view());
|
||
|
|
first = end;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#! Writes a function: its procedure, with its frame, its variables, and the
|
||
|
|
#! sites inlined into it, the global naming it, and the lines of its code.
|
||
|
|
#! @param this The writer.
|
||
|
|
#! @param index The function.
|
||
|
|
def codeview_writer::function(&mut this, index: u32): void {
|
||
|
|
val function := &this.b.c.program.functions[index];
|
||
|
|
val debug := &this.b.debug;
|
||
|
|
val size := this.b.ends[index] - this.b.starts[index];
|
||
|
|
var leaving := size;
|
||
|
|
val returns := debug.return_first[index + 1];
|
||
|
|
if returns > debug.return_first[index] {
|
||
|
|
leaving = debug.returns[returns as u64 - 1] as u64;
|
||
|
|
}
|
||
|
|
var saved: u32 = 0;
|
||
|
|
for &r in kept_gprs {
|
||
|
|
if (debug.saves[index] & (1u << r as u64) as u32) != 0 {
|
||
|
|
saved += 8;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
val at := this.symbols.procedure(
|
||
|
|
true, function.name.view(), this.procedure, this.section, this.b.starts[index] as u32, size as u32,
|
||
|
|
debug.bodies[index], leaving as u32, pdb::proc_has_frame_pointer
|
||
|
|
);
|
||
|
|
this.symbols.frame(debug.frames[index], saved, pdb::frame_locals_rbp | pdb::frame_params_rbp);
|
||
|
|
this.variables(index, 0);
|
||
|
|
this.sites(index, 0);
|
||
|
|
this.symbols.end();
|
||
|
|
this.file.procedure(function.name.view(), 1, at, true);
|
||
|
|
this.function_lines(index);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
# The file.
|
||
|
|
|
||
|
|
#! Writes the PDB file of an executable's debugging information, then stamps
|
||
|
|
#! the executable, which names the file by its path already, with its GUID.
|
||
|
|
#! @param b The building.
|
||
|
|
#! @param exe The executable's file.
|
||
|
|
#! @param path The PDB file's path.
|
||
|
|
#! @param directory The directory the build was in.
|
||
|
|
#! @returns Whether the file was written.
|
||
|
|
pub def describe_pdb(b: &building, exe: &mut byte_writer, path: strview, directory: strview): bool {
|
||
|
|
val program := &b.c.program;
|
||
|
|
val keys := program.layouts.length + 3;
|
||
|
|
var w := codeview_writer {
|
||
|
|
b = b,
|
||
|
|
file = pdb::writer::new(),
|
||
|
|
symbols = pdb::symbol_stream::new(),
|
||
|
|
checksums = pdb::file_checksums::new(),
|
||
|
|
inlinees = pdb::inlinee_lines::new(),
|
||
|
|
lines = byte_writer::new(),
|
||
|
|
section = 0,
|
||
|
|
directory = directory,
|
||
|
|
paths = vector::<string>::new(),
|
||
|
|
file_at = vector::<u32>::new(),
|
||
|
|
ids = vector::<u32>::new(),
|
||
|
|
complete = vector::<u32>::new(),
|
||
|
|
declared = vector::<u32>::new(),
|
||
|
|
procedure = 0
|
||
|
|
};
|
||
|
|
w.file_at.resize(b.c.checker.units.length);
|
||
|
|
for i in 0..w.file_at.length {
|
||
|
|
w.file_at[i] = never;
|
||
|
|
}
|
||
|
|
w.ids.resize(program.functions.length);
|
||
|
|
w.complete.resize(keys);
|
||
|
|
w.declared.resize(keys);
|
||
|
|
|
||
|
|
# The sections, as the executable's headers say, and the one of the code.
|
||
|
|
w.file.sections = pe::section_headers(exe.view());
|
||
|
|
for k in 0..w.file.sections.length / 40 {
|
||
|
|
if w.file.sections.get_u32(40 * k + 12) as u64 == b.text_base - base_address {
|
||
|
|
w.section = (k + 1) as u16;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false if w.section == 0;
|
||
|
|
|
||
|
|
# The module, compiled from the main file, its functions' type that of a
|
||
|
|
# function taking nothing, as a debugger can't call them anyway.
|
||
|
|
val main_file := program.functions[program.main].file;
|
||
|
|
_ = w.checksum(main_file);
|
||
|
|
val module_name := string::from(w.paths[0].view(), nothing {});
|
||
|
|
w.symbols.object_name(module_name.view());
|
||
|
|
val version: u16[3] = [1, 0, 0];
|
||
|
|
w.symbols.compiler(pdb::language_cxx, pdb::cv_machine_x64, version, "sakura");
|
||
|
|
val arguments := w.file.types.arguments(span::<u32>::empty());
|
||
|
|
w.procedure = w.file.types.procedure(pdb::t_void, arguments, 0);
|
||
|
|
|
||
|
|
for i in 0..program.functions.length {
|
||
|
|
continue if b.starts[i] == b.missing || b.ends[i] <= b.starts[i];
|
||
|
|
w.function(i as u32);
|
||
|
|
}
|
||
|
|
w.define_declared();
|
||
|
|
|
||
|
|
# The module's lines: each function's, then the files they name, then where
|
||
|
|
# each function inlined begins.
|
||
|
|
pdb::subsection(&mut w.lines, pdb::debug_s_file_checksums, w.checksums.bytes.view());
|
||
|
|
if w.inlinees.count != 0 {
|
||
|
|
pdb::subsection(&mut w.lines, pdb::debug_s_inlinee_lines, w.inlinees.bytes.view());
|
||
|
|
}
|
||
|
|
var files := vector::<strview>::new();
|
||
|
|
for &each in w.paths {
|
||
|
|
files.append(each.view());
|
||
|
|
}
|
||
|
|
val ours := w.file.add_module(module_name.view(), module_name.view(), &mut w.symbols.bytes, &mut w.lines, files.view());
|
||
|
|
|
||
|
|
# The linker's module, naming each section.
|
||
|
|
var linker := pdb::symbol_stream::new();
|
||
|
|
linker.object_name("* Linker *");
|
||
|
|
linker.compiler(pdb::language_link, pdb::cv_machine_x64, version, "sakura");
|
||
|
|
linker.sections(&w.file.sections);
|
||
|
|
var no_lines := byte_writer::new();
|
||
|
|
_ = w.file.add_module("* Linker *", "", &mut linker.bytes, &mut no_lines, span::<strview>::empty());
|
||
|
|
|
||
|
|
# Every section the system keeps once loaded is the module's.
|
||
|
|
for k in 0..w.file.sections.length / 40 {
|
||
|
|
val characteristics := w.file.sections.get_u32(40 * k + 36);
|
||
|
|
continue if (characteristics & 0x02000000) != 0;
|
||
|
|
w.file.contribute(ours, (k + 1) as u16, 0, w.file.sections.get_u32(40 * k + 8), characteristics);
|
||
|
|
}
|
||
|
|
|
||
|
|
# The names a debugger finds the code by, as disassemblers show them.
|
||
|
|
var named := vector::<elf::symbol>::new();
|
||
|
|
b.name_code(&mut named);
|
||
|
|
for &each in named {
|
||
|
|
w.file.public(each.name, w.section, (each.address - b.text_base) as u32, pdb::public_function | pdb::public_code);
|
||
|
|
}
|
||
|
|
|
||
|
|
val bytes := w.file.file();
|
||
|
|
return false if !write_bytes(path, bytes.view(), 0o644);
|
||
|
|
return pe::stamp_codeview(exe, w.file.guid, w.file.signature);
|
||
|
|
}
|