diff options
Diffstat (limited to 'anim/src/context.zig')
| -rw-r--r-- | anim/src/context.zig | 269 |
1 files changed, 186 insertions, 83 deletions
diff --git a/anim/src/context.zig b/anim/src/context.zig index b3591fc..2e86399 100644 --- a/anim/src/context.zig +++ b/anim/src/context.zig @@ -134,54 +134,45 @@ pub const Registers = struct { var landed = now.landed(.cpu, 0); var note: []const u8 = ""; const crossing: ?Recording.Syscall = if (r.syscalls) syscalls.inStep(now) else null; - // A register the call neither carries nor answers in is any register. - const takes_part = if (crossing) |made| std.mem.eql(u8, name, "rax") or for (syscalls.carried(made), 0..) |carried, k| { - if (std.mem.eql(u8, carried, name) and value_now == made.args[k]) break true; - } else false else false; - if (if (takes_part) crossing else null) |made| { - const plan = syscalls.Plan.of(made); - const is_rax = std.mem.eql(u8, name, "rax"); - const carrier: ?usize = if (is_rax) null else for (syscalls.carried(made), 1..) |carried, k| { - if (std.mem.eql(u8, carried, name) and value_now == made.args[k - 1]) break k; - } else null; - // What carries the call is framed as read, one register at a - // time: they are what crosses the line. - const framed = now.time - plan.registers - @as(f32, @floatFromInt(carrier orelse 0)) * time.flight.second; - landed = framed >= 0; - if (carrier != null) { - marks.read = 0; - marks.read_shows = @min(std.math.clamp(framed / time.read.in, 0, 1), now.reading(.cpu, 0)); - // A descriptor says what it is open on: the `value` class `fd`. - const names = zt.syscalls.names(made.nr); - if (landed and carrier.? >= 1 and carrier.? <= names.len and std.mem.endsWith(u8, names[carrier.? - 1], "fd")) note = rec.openOn(u.f.scratch, value_now, made.enter); - } - if (is_rax) { - // rax: what was there, then the result coming back (the - // call's number is the crossing's: it went in at the wrapper). - const back = now.time - plan.returns; - const result = back >= time.strike.in / 2.0; - const value: u64 = if (result) value_now else value_was; - if (back >= -time.strike.arm) { - marks = .{ .first = now.heat(0, null, back) }; - if (back >= 0 and now.walking()) marks.extend = back; - } - const hops = rows.reg(u, y, w, name, if (result) is.moment else was.moment, value, marks); - // What `open` gave back is a descriptor: the `value` class - // `fd`, this call's once it is back, the last one's before. - const gave = if (result) made else if (syscalls.latest(rec, now.stops[1])) |n| rec.syscalls[n] else null; - if (gave) |g| if (g.exit != null and g.result == value and std.mem.eql(u8, zt.syscalls.name(g.nr) orelse "", "open")) fdNote(u, hops, y, rec.openOn(u.f.scratch, value, g.exit.? + 1)); - return anchor(u, i, hops, y); + // A register is framed as carrying the call only while it holds the + // argument in the moment shown: the stop before the call, until the + // call is back. (Set by the wrapper, between the stops, it carried + // it where no stop saw it: the kernel's rows say it.) + const carrier: ?usize = if (crossing) |made| for (syscalls.carried(made), 1..) |carried, k| { + if (std.mem.eql(u8, carried, name) and value_was == made.args[k - 1]) break k; + } else null else null; + if (carrier) |k| if (crossing) |made| { + const framed = now.time - syscalls.Plan.of(made).registers - @as(f32, @floatFromInt(k - 1)) * time.flight.second; + marks.read = 0; + marks.read_shows = @min(std.math.clamp(framed / time.read.in, 0, 1), now.reading(.cpu, 0)); + // A descriptor says what it is open on: the `value` class `fd`. + const names = zt.syscalls.names(made.nr); + if (k <= names.len and std.mem.endsWith(u8, names[k - 1], "fd")) note = rec.openOn(u.f.scratch, value_was, made.enter); + }; + if (if (std.mem.eql(u8, name, "rax")) crossing else null) |made| { + // rax: what was there, then the result, once the kernel's rows + // have said it (the call's number went in at the wrapper). + const back = now.time - syscalls.Plan.of(made).struck; + const result = back >= time.strike.in / 2.0; + const value: u64 = if (result) value_now else value_was; + if (back >= -time.strike.arm) { + marks = .{ .first = now.heat(0, null, back) }; + if (back >= 0 and now.walking()) marks.extend = back; } - + const hops = rows.reg(u, y, w, name, if (result) is.moment else was.moment, value, marks); + // What `open` gave back is a descriptor: the `value` class + // `fd`, this call's once it is back, the last one's before. + const gave = if (result) made else if (syscalls.latest(rec, now.stops[1])) |n| rec.syscalls[n] else null; + if (gave) |g| if (g.exit != null and g.result == value and std.mem.eql(u8, zt.syscalls.name(g.nr) orelse "", "open")) fdNote(u, hops, y, rec.openOn(u.f.scratch, value, g.exit.? + 1)); + return anchor(u, i, hops, y); } else if (r.quiet) { // Unmarked: the values change as the mark moves on. landed = now.advancing() >= 0.5; } else { const age = now.registerAge(i); - // In a step that crosses into the kernel, what else the line - // changed is seen after the call is back. - const after_call: f32 = if (crossing) |made| syscalls.Plan.of(made).returns - syscalls.Plan.of(made).registers else 0; - const delay = (if (age == 0) ties.strikeDelay(now, i) else 0) + after_call; + // (In a step that crosses into the kernel, what else the line + // changed is seen after the call is back, after rax: `strikeDelay`.) + const delay = if (age == 0) ties.strikeDelay(now, i) else 0; landed = now.landed(.cpu, delay); marks.first = now.register(i, delay); // A register written: the rest of its chain is followed again. @@ -242,6 +233,24 @@ fn spanName(u: ui.Ui, rec: *const Recording, open: []const Recording.Call, i: us return if (v.decimal) u.print("{s}({d})", .{ name, arg }) else u.print("{s}({s}{s})", .{ name, v.prefix, v.digits() }); } +/// Frame `i`'s arguments, from the call the recording knows it to be +/// (the `i`th open call from the newest, when it is of this frame's +/// function): "" where it knows none. +pub fn callArguments(u: ui.Ui, rec: *const Recording, open: []const Recording.Call, i: usize, pc: u64) []const u8 { + if (i >= open.len) return ""; + const call = open[open.len - 1 - i]; + if (!std.mem.eql(u8, rec.name(call.function), frameName(u, rec, pc))) return ""; + return @import("calls.zig").arguments(u, rec, call); +} + +/// Where frame `i` is in the program's source, `file:line`: the newest at +/// the moment's own line, when the moment is on one (in a library it is +/// not); an older one where the recording knows its return address's. +pub fn frameWhere(u: ui.Ui, rec: *const Recording, moment: u64, i: usize, pc: u64) []const u8 { + const line = if (i == 0) rec.ownLine(moment) else rec.lineOf(pc); + return if (line) |l| u.print("{s}:{d}", .{ rec.file, l }) else ""; +} + /// A frame's name: its function's (no offset), or its module's and offset where there is no symbol. pub fn frameName(u: ui.Ui, rec: *const Recording, pc: u64) []const u8 { const name = rec.strings.get(pc) orelse return u.print("{x}", .{pc}); @@ -280,12 +289,21 @@ pub const Stack = struct { const hold = placed.value(k.stage.hold); const held = hold >= 0; const held_at: u64 = if (held) rec.register(@intFromFloat(hold), "rsp") -% 8 * @as(u64, @intFromFloat(placed.value(k.stage.hold_free))) else 0; - const window = if (held) [2]u64{ held_at, held_at } else [2]u64{ windowAt(now, is, slots, 0), windowAt(now, was, slots, 1) }; + var window = if (held) [2]u64{ held_at, held_at } else [2]u64{ windowAt(now, is, slots, 0), windowAt(now, was, slots, 1) }; + // A window that goes further than its rows can slide (a continue + // to a stop deeper or shallower than a call is): the old rows fade + // out before memory strikes and the new ones in after, each with + // the memory of its own stop. + const landed = now.landed(.memory, 0); + const jumped = @abs(@as(f32, @floatFromInt(@as(i64, @bitCast(window[0] -% window[1])))) / 8) > 16; + const since_memory = now.since(.memory, 0); + const jump_shows: f32 = if (!jumped or now.resting()) 1 else if (!landed) 1 - std.math.clamp((since_memory + time.quick) / time.quick, 0, 1) else std.math.clamp((since_memory - time.strike.in / 2.0) / time.quick, 0, 1); + if (jumped) window = if (landed) .{ window[0], window[0] } else .{ window[1], window[1] }; + const v = u.faded(jump_shows); // Going to the hold and coming from it, the rows scroll: the // remaining way, in rows, from where the window would be. const natural = windowAt(now, is, slots, 0); const hold_scroll: f32 = if (held) @as(f32, @floatFromInt(@as(i64, @bitCast(held_at -% natural)))) / 8 * (1 - cursors.glide(placed.value(k.stage.hold_in))) else 0; - const landed = now.landed(.memory, 0); // From rsp, or the range shown when the window is elsewhere: in // bytes from rsp, as the catalogue writes it (`rsp+20 to rsp+48`). const from_rsp: i64 = if (landed) @bitCast(window[0] -% is.rsp) else @bitCast(window[1] -% was.rsp); @@ -317,11 +335,46 @@ pub const Stack = struct { // travel down the lane from the old word to the new, with the slide. var travel_buf: [2][]const u8 = undefined; var travelling: usize = 0; - for ([_][]const u8{ "rsp", "rbp" }) |name| if (!now.resting() and was.named(name) != is.named(name)) { + for ([_][]const u8{ "rsp", "rbp" }) |name| if (!now.resting() and !jumped and was.named(name) != is.named(name)) { travel_buf[travelling] = name; travelling += 1; }; const travel = travel_buf[0..travelling]; + // The backtrace, drawn on the words that are each frame: each span + // glides from the frame's old extent to its new one with the slide. + // Worked out first: a name longer than the spans' room takes room + // from the chains of the rows beside it, which end before it. + var buf: [16]Frame = undefined; + var old_buf: [16]Frame = undefined; + const chain = frames(rec, now.stops[if (jumped and !landed) 1 else 0], &buf); + const old_chain = frames(rec, now.stops[if (jumped and landed) 0 else 1], &old_buf); + // Placed as the rows are: against the window after the step, and slid. + const open_calls = rec.stack(u.f.scratch, if (landed) now.stops[0] else now.stops[1]); + const named = if (landed) chain else old_chain; + const rows_of = struct { + fn at(addr: u64, base: u64) f32 { + return @as(f32, @floatFromInt(@as(i64, @bitCast(addr -% base)))) / 8; + } + }.at; + const Span = struct { y0: f32, y1: f32, name: []const u8, newest: bool, open: bool }; + var spans: [16]Span = undefined; + var n_spans: usize = 0; + for (chain, 0..) |frame, i| { + const old_frame = if (i < old_chain.len) old_chain[i] else frame; + const first = std.math.lerp(rows_of(old_frame.lo, window[0]), rows_of(frame.lo, window[0]), p) + slide; + if (first >= @as(f32, @floatFromInt(slots))) break; + const last = if (frame.hi) |hi| std.math.lerp(if (old_frame.hi) |oh| rows_of(oh, window[0]) else rows_of(hi, window[0]), rows_of(hi, window[0]), p) + slide else std.math.inf(f32); + if (last < 0) continue; + const open = last >= @as(f32, @floatFromInt(slots)); + const y1 = top + (@min(last, @as(f32, @floatFromInt(slots)) - 1) + 1) * m.row; + spans[n_spans] = .{ .y0 = top + @max(first, 0) * m.row, .y1 = y1, .name = spanName(u, rec, open_calls, i, if (i < named.len) named[i].pc else frame.pc), .newest = i == 0, .open = open }; + n_spans += 1; + } + // rsp or rbp travelling alone keeps its place in the pin it leaves and + // the one it joins: room there, closing and opening as it goes. + const going = cursors.standard(if (now.resting()) 1 else std.math.clamp(now.since(.cpu, ties.strikeDelay(now, index("rsp").?)) / time.move, 0, 1)); + const size = theme.size.mono.context; + const lone: ?[]const u8 = if (travel.len == 1) travel[0] else null; var n: i64 = @as(i64, @intFromFloat(@floor(-slide))) - 1; while (n <= @as(i64, @intCast(slots)) - @as(i64, @intFromFloat(@floor(slide)))) : (n += 1) { const y = top + (@as(f32, @floatFromInt(n)) + slide) * m.row; @@ -336,10 +389,23 @@ pub const Stack = struct { } // `free`: above rsp, not yet reached: its address at 60%, its word faint. if (held and addr < (if (landed) is.rsp else was.rsp)) { - rows.free(u, y, memory, addr, lead); + rows.free(v, y, memory, addr, lead); continue; } - const hops = rows.slot(u, y, box.w, memory, addr, names.names, names.shows, marks); + // A frame's name beside this row: the chain ends a character before it. + var room_w = box.w; + for (spans[0..n_spans]) |sp| { + const mid = (sp.y0 + sp.y1) / 2; + if (mid + m.row / 2 > y and mid - m.row / 2 < y + m.row) room_w = @min(room_w, rows.spanNameX(u, box.w, sp.name) - ui.advance(theme.size.mono.context) + theme.metric.slot.room); + } + var gap: f32 = 0; + if (lone) |name| { + const share: f32 = if (was.named(name) == addr) 1 - going else if (is.named(name) == addr) going else 0; + gap = share * (u.width(.mono, size, name) + ui.advance(size)); + } + const gapped = gap > 0 and names.names.len > 0 and names.shows > 0; + if (gapped) pinGap(v.faded(names.shows), y, names.names, lone.?, gap); + const hops = rows.slot(v, y, room_w, memory, addr, if (gapped) &.{} else names.names, names.shows, marks); // Only a row on show is a place a tie can land. if (y >= top - 1 and y + m.row <= box.h - m.pad + 1) { ties.anchor(u, .{ .word = addr }, .{ .x = theme.metric.slot.chain, .y = y, .w = if (hops.n > 0) hops.x[0] + hops.w[0] - theme.metric.slot.chain else 120, .h = m.row }); @@ -351,7 +417,6 @@ pub const Stack = struct { const row_px = u.at.xf.apply(.{ 0, top + m.row })[1] - lane_at[1]; ties.stack_window = .{ .first = window[0], .slots = slots, .x = lane_at[0], .top = lane_at[1], .row = row_px }; // The travelling tags: both of them at one word share a pin. - const going = cursors.standard(if (now.resting()) 1 else std.math.clamp(now.since(.cpu, 0) / time.move, 0, 1)); var places: [2]f32 = undefined; for (travel, places[0..travel.len]) |name, *place| { const at_old = @as(f32, @floatFromInt(@as(i64, @bitCast((was.named(name) orelse 0) -% window[0])))) / 8 + slide; @@ -362,33 +427,42 @@ pub const Stack = struct { if (i == 1 and @abs(places[0] - place) < 0.05) continue; if (place < -0.5 or place > @as(f32, @floatFromInt(slots)) - 0.5) continue; const both = i == 0 and travel.len == 2 and @abs(places[1] - place) < 0.05; - u.pinRow(theme.metric.slot.lane, top + place * m.row, if (both) &.{ "rsp", "rbp" } else &.{name}, theme.color.region.stack); + // Alone, from its place among the names it leaves to its place among those it joins. + const x = if (lone != null) std.math.lerp(pinPlace(u, pins(u, now, is, was, was.named(name) orelse 0, travel).names, name), pinPlace(u, pins(u, now, is, was, is.named(name) orelse 0, travel).names, name), going) else theme.metric.slot.lane; + u.pinRow(x, top + place * m.row, if (both) &.{ "rsp", "rbp" } else &.{name}, theme.color.region.stack); } - // The backtrace, drawn on the words that are each frame: each span - // glides from the frame's old extent to its new one with the slide. - var buf: [16]Frame = undefined; - var old_buf: [16]Frame = undefined; - const chain = frames(rec, now.stops[0], &buf); - const old_chain = frames(rec, now.stops[1], &old_buf); - // Placed as the rows are: against the window after the step, and slid. - const open_calls = rec.stack(u.f.scratch, if (landed) now.stops[0] else now.stops[1]); - const named = if (landed) chain else old_chain; - const rows_of = struct { - fn at(addr: u64, base: u64) f32 { - return @as(f32, @floatFromInt(@as(i64, @bitCast(addr -% base)))) / 8; + for (spans[0..n_spans]) |sp| rows.span(v, box.w, sp.y0, sp.y1, sp.name, sp.newest, sp.open); + veiled(u, box, now.veil); + } + + /// Where `name` goes in a row pin of `others` and it, in the registers' order. + fn pinPlace(u: ui.Ui, others: []const []const u8, name: []const u8) f32 { + const size = theme.size.mono.context; + var x: f32 = theme.metric.slot.lane; + for (others) |o| if (index(o).? < index(name).?) { + x += u.width(.mono, size, o) + ui.advance(size); + }; + return x; + } + + /// A row pin of `names` with room `gap` wide where `name` goes in it. + fn pinGap(u: ui.Ui, y: f32, names: []const []const u8, name: []const u8, gap: f32) void { + const size = theme.size.mono.context; + const p = theme.metric.pin; + var x: f32 = theme.metric.slot.lane; + var gapped = false; + for (names) |o| { + if (!gapped and index(o).? > index(name).?) { + x += gap; + gapped = true; } - }.at; - for (chain, 0..) |frame, i| { - const old_frame = if (i < old_chain.len) old_chain[i] else frame; - const first = std.math.lerp(rows_of(old_frame.lo, window[0]), rows_of(frame.lo, window[0]), p) + slide; - if (first >= @as(f32, @floatFromInt(slots))) break; - const last = if (frame.hi) |hi| std.math.lerp(if (old_frame.hi) |oh| rows_of(oh, window[0]) else rows_of(hi, window[0]), rows_of(hi, window[0]), p) + slide else std.math.inf(f32); - if (last < 0) continue; - const open = last >= @as(f32, @floatFromInt(slots)); - const y1 = top + (@min(last, @as(f32, @floatFromInt(slots)) - 1) + 1) * m.row; - rows.span(u, box.w, top + @max(first, 0) * m.row, y1, spanName(u, rec, open_calls, i, if (i < named.len) named[i].pc else frame.pc), i == 0, open); + u.text(.mono_bold, size, x, theme.baseline(y, m.row, size), o, t.primary); + x += u.width(.mono, size, o) + ui.advance(size); } - veiled(u, box, now.veil); + if (!gapped) x += gap; + const hx = x - ui.advance(size) + p.row_gap; + const mid = y + m.row / 2; + sk.draw.polygon(u.f, &.{ .{ hx + p.row_head[0], mid }, .{ hx, mid - p.row_head[1] / 2 }, .{ hx, mid + p.row_head[1] / 2 } }, u.at.pen(.{ .fill = ui.rgb(theme.color.region.stack) })); } /// The first word a window of `slots` shows, `skip` steps back: rsp, @@ -454,20 +528,49 @@ pub const Backtrace = struct { const u = at(f, placed, box); const now = b.cursor.now(placed); const rec = b.cursor.rec; - var buf: [24]Frame = undefined; - const chain = frames(rec, now.stop(.cpu), &buf); + // A frame is its words on the stack: the chain changes with memory. + // The frames both chains have (the same way back, counted from the + // oldest) slide to their new depth, as rows making room; those that + // end fade out first, those that begin fade in after. + var new_buf: [24]Frame = undefined; + var old_buf: [24]Frame = undefined; + const chains = [2][]Frame{ frames(rec, now.stops[1], &old_buf), frames(rec, now.stops[0], &new_buf) }; + var shared: usize = 0; + while (shared < @min(chains[0].len, chains[1].len) and chains[0][chains[0].len - 1 - shared].pc == chains[1][chains[1].len - 1 - shared].pc) shared += 1; + const since = if (now.walking()) now.since(.memory, 0) else if (now.landed(.memory, 0)) std.math.inf(f32) else -std.math.inf(f32); + const leave = std.math.clamp(since / time.quick, 0, 1); + const slide = cursors.glide(std.math.clamp((since - time.quick / 2.0) / time.move, 0, 1)); + const arrive = std.math.clamp((since - time.quick / 2.0 - time.move) / time.quick, 0, 1); const room = rowsOf(box); - const body = u.panel(box.w, box.h, "backtrace", .{ .focus = b.focus, .note = if (chain.len > room) u.print("newest on top \u{00b7} {d} more", .{chain.len - room}) else "newest on top" }); + const total = chains[if (slide >= 0.5) 1 else 0].len; + const body = u.panel(box.w, box.h, "backtrace", .{ .focus = b.focus, .note = if (total > room) u.print("newest on top \u{00b7} {d} more", .{total - room}) else "newest on top" }); clipBody(u, box); defer u.unclip(); - for (chain[0..@min(chain.len, room)], 0..) |frame, i| { - const y = body.y + m.pad + @as(f32, @floatFromInt(i)) * m.row; - const name = ui.symbol(u, rec.strings.get(frame.pc) orelse u.print("{x}", .{frame.pc})); - // The program's own frame says where in its source it is. - const where = if (i == 0) u.print("{s}:{d}", .{ rec.file, rec.own[now.marked()] }) else if (rec.lineOf(frame.pc)) |l| u.print("{s}:{d}", .{ rec.file, l }) else ""; - const open = rec.stack(u.f.scratch, now.stop(.cpu)); - const args = if (i < open.len) @import("calls.zig").arguments(u, rec, open[open.len - 1 - i]) else ""; - rows.callRow(u, y, box.w, i, name, args, where, i == 0); + const opens = [2][]const Recording.Call{ rec.stack(u.f.scratch, now.stops[1]), rec.stack(u.f.scratch, now.stops[0]) }; + const top = body.y + m.pad; + for (chains, 0..) |chain, side| { + const shows = if (side == 0) 1 - leave else arrive; + for (chain[0..@min(chain.len, room + shared)], 0..) |frame, i| { + const from_end = chain.len - 1 - i; + const kept = from_end < shared; + // A frame both have is drawn once, from the old chain to its place in the new. + if (kept and side == 1) continue; + var at_row: f32 = @floatFromInt(i); + var depth = i; + var which = side; + if (kept) { + const to = chains[1].len - 1 - from_end; + at_row = std.math.lerp(at_row, @as(f32, @floatFromInt(to)), slide); + if (slide >= 0.5) { + depth = to; + which = 1; + } + } else if (shows <= 0) continue; + if (at_row >= @as(f32, @floatFromInt(room))) continue; + const v = if (kept) u else u.faded(shows); + const name = ui.symbol(u, rec.strings.get(frame.pc) orelse u.print("{x}", .{frame.pc})); + rows.callRow(v, top + at_row * m.row, box.w, depth, name, callArguments(u, rec, opens[which], depth, frame.pc), frameWhere(u, rec, if (which == 1) now.marks[1] else now.marks[0], depth, frame.pc), depth == 0); + } } veiled(u, box, now.veil); } |
