summaryrefslogtreecommitdiff
path: root/targets/agent_zig.zig
blob: bfb2f5df02318d26ca1260c2c8c4f9cc37da4ddf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//! A Zig program that annotates itself: agent_api.c again, with the agent
//! compiled in as a module instead of linked as a library.
const std = @import("std");
const agent = @import("ztrace-agent");

pub fn main() void {
    var numbers = [16]i32{ 9, 3, 14, 0, 7, 12, 5, 1, 15, 8, 2, 11, 6, 13, 4, 10 };
    agent.watch(std.mem.asBytes(&numbers), "numbers");
    for (1..numbers.len) |i| {
        const value = numbers[i];
        var j = i;
        while (j > 0 and numbers[j - 1] > value) : (j -= 1) numbers[j] = numbers[j - 1];
        numbers[j] = value;
        agent.mark("inserted", i);
    }
    var checksum: u64 = 0;
    for (numbers) |v| checksum = checksum *% 31 +% @as(u64, @intCast(v));
    agent.snapshot(std.mem.asBytes(&checksum));
    agent.mark("done", checksum);
}