| ... | ... | @@ -1944,19 +1944,42 @@ noinline fn showMyTrace() usize { |
| 1944 | 1944 | return @returnAddress(); |
| 1945 | 1945 | } |
| 1946 | 1946 | |
| 1947 | | pub fn Trace(comptime size: usize, comptime stack_frame_count: usize) type { |
| 1947 | /// This API helps you track where a value originated and where it was mutated, |
| 1948 | /// or any other points of interest. |
| 1949 | /// In debug mode, it adds a small size penalty (104 bytes on 64-bit architectures) |
| 1950 | /// to the aggregate that you add it to. |
| 1951 | /// In release mode, it is size 0 and all methods are no-ops. |
| 1952 | /// This is a pre-made type with default settings. |
| 1953 | /// For more advanced usage, see `ConfigurableTrace`. |
| 1954 | pub const Trace = ConfigurableTrace(2, 4, builtin.mode == .Debug); |
| 1955 | |
| 1956 | pub fn ConfigurableTrace(comptime size: usize, comptime stack_frame_count: usize, comptime enabled: bool) type { |
| 1948 | 1957 | return struct { |
| 1949 | | addrs: [size][stack_frame_count]usize = undefined, |
| 1950 | | notes: [size][]const u8 = undefined, |
| 1951 | | index: usize = 0, |
| 1958 | addrs: [actual_size][stack_frame_count]usize = undefined, |
| 1959 | notes: [actual_size][]const u8 = undefined, |
| 1960 | index: Index = 0, |
| 1952 | 1961 | |
| 1953 | | const frames_init = [1]usize{0} ** stack_frame_count; |
| 1962 | const actual_size = if (enabled) size else 0; |
| 1963 | const Index = if (enabled) usize else u0; |
| 1954 | 1964 | |
| 1955 | | pub noinline fn add(t: *@This(), note: []const u8) void { |
| 1965 | pub const enabled = enabled; |
| 1966 | |
| 1967 | pub const add = if (enabled) addNoInline else addNoOp; |
| 1968 | |
| 1969 | pub noinline fn addNoInline(t: *@This(), note: []const u8) void { |
| 1970 | comptime assert(enabled); |
| 1956 | 1971 | return addAddr(t, @returnAddress(), note); |
| 1957 | 1972 | } |
| 1958 | 1973 | |
| 1974 | pub inline fn addNoOp(t: *@This(), note: []const u8) void { |
| 1975 | _ = t; |
| 1976 | _ = note; |
| 1977 | comptime assert(!enabled); |
| 1978 | } |
| 1979 | |
| 1959 | 1980 | pub fn addAddr(t: *@This(), addr: usize, note: []const u8) void { |
| 1981 | if (!enabled) return; |
| 1982 | |
| 1960 | 1983 | if (t.index < size) { |
| 1961 | 1984 | t.notes[t.index] = note; |
| 1962 | 1985 | t.addrs[t.index] = [1]usize{0} ** stack_frame_count; |
| ... | ... | @@ -1972,6 +1995,8 @@ pub fn Trace(comptime size: usize, comptime stack_frame_count: usize) type { |
| 1972 | 1995 | } |
| 1973 | 1996 | |
| 1974 | 1997 | pub fn dump(t: @This()) void { |
| 1998 | if (!enabled) return; |
| 1999 | |
| 1975 | 2000 | const tty_config = detectTTYConfig(); |
| 1976 | 2001 | const stderr = io.getStdErr().writer(); |
| 1977 | 2002 | const end = @maximum(t.index, size); |