跳转到内容

std.debug.Coverage

标准库Zig 0.17.0官方原文
源码std/debug/Coverage.zig

第 1 行

字段

directories: std.array_hash_map.Custom(String, void, String.MapContext, false)

提供目录的全局作用域整数索引。

不同于例如单个 ELF 模块内部、 作用域限于编译单元的目录索引。

字符串内存引用内存映射的调试信息。

由 mutex 保护。

files: std.array_hash_map.Custom(File, void, File.MapContext, false)

为文件提供全局作用域的整数索引。

字符串内存引用内存映射的调试信息。

由 mutex 保护。

string_bytes: std.ArrayList(u8)
mutex: Io.Mutex

保护其他字段。

std.debug.Coverage.addStringAssumeCapacity

函数

第 242 行

pub fn addStringAssumeCapacity(cov: *Coverage, s: []const u8) String

参数

cov: *Coverage
s: []const u8

std.debug.Coverage.Allocator

别名

第 5 行

别名目标 std.mem.Allocator

std.debug.Coverage.assert

别名

第 8 行

别名目标 std.debug.assert

std.debug.Coverage.Coverage

别名

第 1 行

别名目标 std.debug.Coverage

std.debug.Coverage.deinit

函数

第 129 行

pub fn deinit(cov: *Coverage, gpa: Allocator) void

参数

cov: *Coverage
gpa: Allocator

std.debug.Coverage.Dwarf

别名

第 7 行

别名目标 std.debug.Dwarf

std.debug.Coverage.File

容器

第 81 行

字段

directory_index: u32
basename: String

std.debug.Coverage.File.Index

容器

第 85 行

字段

invalid = std.math.maxInt(u32)
_

std.debug.Coverage.File.MapContext

容器

第 90 行

字段

string_bytes: []const u8

std.debug.Coverage.File.MapContext.eql

函数

第 98 行

pub fn eql(self: MapContext, a: File, b: File, b_index: usize) bool

参数

self: MapContext
a: File
b: File
b_index: usize

std.debug.Coverage.File.MapContext.hash

函数

第 93 行

pub fn hash(self: MapContext, a: File) u32

参数

self: MapContext
a: File

std.debug.Coverage.File.SliceAdapter

容器

第 107 行

字段

string_bytes: []const u8

std.debug.Coverage.File.SliceAdapter.Entry

容器

第 110 行

字段

directory_index: u32
basename: []const u8

std.debug.Coverage.File.SliceAdapter.eql

函数

第 120 行

pub fn eql(self: @This(), a: Entry, b: File, b_index: usize) bool

参数

self: @This()
a: Entry
b: File
b_index: usize

std.debug.Coverage.File.SliceAdapter.hash

函数

第 115 行

pub fn hash(self: @This(), a: Entry) u32

参数

self: @This()
a: Entry

std.debug.Coverage.fileAt

函数

第 136 行

pub fn fileAt(cov: *Coverage, index: File.Index) *File

参数

cov: *Coverage
index: File.Index

std.debug.Coverage.Hash

别名

第 6 行

别名目标 std.hash.Wyhash

std.debug.Coverage.init

常量

第 29 行

Coverage

std.debug.Coverage.Io

别名

第 4 行

别名目标 std.Io

std.debug.Coverage.resolveAddressesDwarf

函数

第 146 行

pub fn resolveAddressesDwarf( cov: *Coverage, gpa: Allocator, io: Io, endian: std.builtin.Endian, sorted_pc_addrs: []const u64, output: []SourceLocation, d: *Dwarf, ) ResolveAddressesDwarfError!void

参数

cov: *Coverage
gpa: Allocator
io: Io
endian: std.builtin.Endian
sorted_pc_addrs: []const u64

断言地址按升序排列。

output: []SourceLocation

断言其长度等于 sorted_pc_addrs 的长度。

d: *Dwarf

错误

Canceled Cancelable

调用方请求停止异步操作。

EndOfStream ScanError
InvalidDebugInfo ScanError
MissingDebugInfo ScanError
OutOfMemory Error
Overflow ScanError
ReadFailed ScanError
StreamTooLong ScanError

std.debug.Coverage.resolveAddressesDwarf.order

函数

第 201 行

fn order(context: u64, item: u64) std.math.Order

参数

context: u64
item: u64

std.debug.Coverage.ResolveAddressesDwarfError

错误集合

第 144 行

错误

Canceled Cancelable

调用方请求停止异步操作。

EndOfStream ScanError
InvalidDebugInfo ScanError
MissingDebugInfo ScanError
OutOfMemory Error
Overflow ScanError
ReadFailed ScanError
StreamTooLong ScanError

std.debug.Coverage.SourceLocation

容器

第 69 行

字段

file: File.Index
line: u32
column: u32

std.debug.Coverage.SourceLocation.invalid

常量

第 74 行

SourceLocation

std.debug.Coverage.span

函数

第 249 行

fn span(s: []const u8) [:0]const u8

参数

s: []const u8

std.debug.Coverage.std

别名

第 3 行

别名目标 std

std.debug.Coverage.String

容器

第 36 行

字段

_

std.debug.Coverage.String.MapContext

容器

第 39 行

字段

string_bytes: []const u8

std.debug.Coverage.String.MapContext.eql

函数

第 42 行

pub fn eql(self: @This(), a: String, b: String, b_index: usize) bool

参数

self: @This()
a: String
b: String
b_index: usize

std.debug.Coverage.String.MapContext.hash

函数

第 49 行

pub fn hash(self: @This(), a: String) u32

参数

self: @This()
a: String

std.debug.Coverage.String.SliceAdapter

容器

第 54 行

字段

string_bytes: []const u8

std.debug.Coverage.String.SliceAdapter.eql

函数

第 57 行

pub fn eql(self: @This(), a_slice: []const u8, b: String, b_index: usize) bool

参数

self: @This()
a_slice: []const u8
b: String
b_index: usize

std.debug.Coverage.String.SliceAdapter.hash

函数

第 62 行

pub fn hash(self: @This(), a: []const u8) u32

参数

self: @This()
a: []const u8

std.debug.Coverage.stringAt

函数

第 140 行

pub fn stringAt(cov: *Coverage, index: String) [:0]const u8

参数

cov: *Coverage
index: String
源码

std/debug/Coverage.zig

const Coverage = @This();

const std = @import("../std.zig");
const Io = std.Io;
const Allocator = std.mem.Allocator;
const Hash = std.hash.Wyhash;
const Dwarf = std.debug.Dwarf;
const assert = std.debug.assert;

/// 提供目录的全局作用域整数索引。
///
/// 不同于例如单个 ELF 模块内部、
/// 作用域限于编译单元的目录索引。
///
/// 字符串内存引用内存映射的调试信息。
///
/// 由 `mutex` 保护。
directories: std.array_hash_map.Custom(String, void, String.MapContext, false),
/// 为文件提供全局作用域的整数索引。
///
/// 字符串内存引用内存映射的调试信息。
///
/// 由 `mutex` 保护。
files: std.array_hash_map.Custom(File, void, File.MapContext, false),
string_bytes: std.ArrayList(u8),
/// 保护其他字段。
mutex: Io.Mutex,

pub const init: Coverage = .{
    .directories = .empty,
    .files = .empty,
    .mutex = .init,
    .string_bytes = .empty,
};

pub const String = enum(u32) {
    _,

    pub const MapContext = struct {
        string_bytes: []const u8,

        pub fn eql(self: @This(), a: String, b: String, b_index: usize) bool {
            _ = b_index;
            const a_slice = span(self.string_bytes[@backingInt(a)..]);
            const b_slice = span(self.string_bytes[@backingInt(b)..]);
            return std.mem.eql(u8, a_slice, b_slice);
        }

        pub fn hash(self: @This(), a: String) u32 {
            return @truncate(Hash.hash(0, span(self.string_bytes[@backingInt(a)..])));
        }
    };

    pub const SliceAdapter = struct {
        string_bytes: []const u8,

        pub fn eql(self: @This(), a_slice: []const u8, b: String, b_index: usize) bool {
            _ = b_index;
            const b_slice = span(self.string_bytes[@backingInt(b)..]);
            return std.mem.eql(u8, a_slice, b_slice);
        }
        pub fn hash(self: @This(), a: []const u8) u32 {
            _ = self;
            return @truncate(Hash.hash(0, a));
        }
    };
};

pub const SourceLocation = extern struct {
    file: File.Index,
    line: u32,
    column: u32,

    pub const invalid: SourceLocation = .{
        .file = .invalid,
        .line = 0,
        .column = 0,
    };
};

pub const File = extern struct {
    directory_index: u32,
    basename: String,

    pub const Index = enum(u32) {
        invalid = std.math.maxInt(u32),
        _,
    };

    pub const MapContext = struct {
        string_bytes: []const u8,

        pub fn hash(self: MapContext, a: File) u32 {
            const a_basename = span(self.string_bytes[@backingInt(a.basename)..]);
            return @truncate(Hash.hash(a.directory_index, a_basename));
        }

        pub fn eql(self: MapContext, a: File, b: File, b_index: usize) bool {
            _ = b_index;
            if (a.directory_index != b.directory_index) return false;
            const a_basename = span(self.string_bytes[@backingInt(a.basename)..]);
            const b_basename = span(self.string_bytes[@backingInt(b.basename)..]);
            return std.mem.eql(u8, a_basename, b_basename);
        }
    };

    pub const SliceAdapter = struct {
        string_bytes: []const u8,

        pub const Entry = struct {
            directory_index: u32,
            basename: []const u8,
        };

        pub fn hash(self: @This(), a: Entry) u32 {
            _ = self;
            return @truncate(Hash.hash(a.directory_index, a.basename));
        }

        pub fn eql(self: @This(), a: Entry, b: File, b_index: usize) bool {
            _ = b_index;
            if (a.directory_index != b.directory_index) return false;
            const b_basename = span(self.string_bytes[@backingInt(b.basename)..]);
            return std.mem.eql(u8, a.basename, b_basename);
        }
    };
};

pub fn deinit(cov: *Coverage, gpa: Allocator) void {
    cov.directories.deinit(gpa);
    cov.files.deinit(gpa);
    cov.string_bytes.deinit(gpa);
    cov.* = undefined;
}

pub fn fileAt(cov: *Coverage, index: File.Index) *File {
    return &cov.files.keys()[@backingInt(index)];
}

pub fn stringAt(cov: *Coverage, index: String) [:0]const u8 {
    return span(cov.string_bytes.items[@backingInt(index)..]);
}

pub const ResolveAddressesDwarfError = Dwarf.ScanError || Io.Cancelable;

pub fn resolveAddressesDwarf(
    cov: *Coverage,
    gpa: Allocator,
    io: Io,
    endian: std.builtin.Endian,
    /// 断言地址按升序排列。
    sorted_pc_addrs: []const u64,
    /// 断言其长度等于 `sorted_pc_addrs` 的长度。
    output: []SourceLocation,
    d: *Dwarf,
) ResolveAddressesDwarfError!void {
    assert(sorted_pc_addrs.len == output.len);
    assert(d.ranges.items.len != 0); // 先调用 `populateRanges`。

    var range_i: usize = 0;
    var range: *std.debug.Dwarf.Range = &d.ranges.items[0];
    var line_table_i: usize = undefined;
    var prev_pc: u64 = 0;
    var prev_cu: ?*std.debug.Dwarf.CompileUnit = null;
    // 保护目录表和文件表,防止其他线程并发访问。
    try cov.mutex.lock(io);
    defer cov.mutex.unlock(io);
    next_pc: for (sorted_pc_addrs, output) |pc, *out| {
        assert(pc >= prev_pc);
        prev_pc = pc;

        while (pc >= range.end) {
            range_i += 1;
            if (range_i >= d.ranges.items.len) {
                out.* = SourceLocation.invalid;
                continue :next_pc;
            }
            range = &d.ranges.items[range_i];
        }
        if (pc < range.start) {
            out.* = SourceLocation.invalid;
            continue :next_pc;
        }
        const cu = &d.compile_unit_list.items[range.compile_unit_index];
        if (cu != prev_cu) {
            prev_cu = cu;
            if (cu.src_loc_cache == null) {
                cov.mutex.unlock(io);
                defer cov.mutex.lockUncancelable(io);
                d.populateSrcLocCache(gpa, endian, cu) catch |err| switch (err) {
                    error.MissingDebugInfo, error.InvalidDebugInfo => {
                        out.* = SourceLocation.invalid;
                        continue :next_pc;
                    },
                    else => |e| return e,
                };
            }
            const slc = &cu.src_loc_cache.?;
            const table_addrs = slc.line_table.keys();
            line_table_i = std.sort.upperBound(u64, table_addrs, pc, struct {
                fn order(context: u64, item: u64) std.math.Order {
                    return std.math.order(context, item);
                }
            }.order);
        }
        const slc = &cu.src_loc_cache.?;
        const table_addrs = slc.line_table.keys();
        while (line_table_i < table_addrs.len and table_addrs[line_table_i] <= pc) line_table_i += 1;

        const entry = slc.line_table.values()[line_table_i - 1];
        const corrected_file_index = entry.file - @intFromBool(slc.version < 5);
        const file_entry = slc.files[corrected_file_index];
        const dir_path = slc.directories[file_entry.dir_index].path;
        try cov.string_bytes.ensureUnusedCapacity(gpa, dir_path.len + file_entry.path.len + 2);
        const dir_gop = try cov.directories.getOrPutContextAdapted(gpa, dir_path, String.SliceAdapter{
            .string_bytes = cov.string_bytes.items,
        }, String.MapContext{
            .string_bytes = cov.string_bytes.items,
        });
        if (!dir_gop.found_existing)
            dir_gop.key_ptr.* = addStringAssumeCapacity(cov, dir_path);
        const file_gop = try cov.files.getOrPutContextAdapted(gpa, File.SliceAdapter.Entry{
            .directory_index = @intCast(dir_gop.index),
            .basename = file_entry.path,
        }, File.SliceAdapter{
            .string_bytes = cov.string_bytes.items,
        }, File.MapContext{
            .string_bytes = cov.string_bytes.items,
        });
        if (!file_gop.found_existing) file_gop.key_ptr.* = .{
            .directory_index = @intCast(dir_gop.index),
            .basename = addStringAssumeCapacity(cov, file_entry.path),
        };
        out.* = .{
            .file = @fromBackingInt(@intCast(file_gop.index)),
            .line = entry.line,
            .column = entry.column,
        };
    }
}

pub fn addStringAssumeCapacity(cov: *Coverage, s: []const u8) String {
    const result: String = @fromBackingInt(@intCast(cov.string_bytes.items.len));
    cov.string_bytes.appendSliceAssumeCapacity(s);
    cov.string_bytes.appendAssumeCapacity(0);
    return result;
}

fn span(s: []const u8) [:0]const u8 {
    return std.mem.sliceTo(@as([:0]const u8, @ptrCast(s)), 0);
}