summaryrefslogtreecommitdiff
path: root/src/cli/main.zig
blob: 72b894adf7005d80884a0519be845d82e76ff1b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//! ztrace: many ways to watch a program, one recording.
const std = @import("std");
const util = @import("util.zig");

/// A command is a file with `usage` and `run(init, args)`.
const commands = .{
    .record = @import("record.zig"),
    .dbg = @import("dbg.zig"),
    .run = @import("run.zig"),
    .dump = @import("dump.zig"),
    .show = @import("show.zig"),
};

const usage =
    \\usage: ztrace COMMAND ...
    \\
    \\  record   run a program under ptrace into a recording
    \\  dbg      debug a program, recording it: step, break, look, go back
    \\  run      record a program that carries a ring producer (agent, instrumentation)
    \\  dump     print a recording's events
    \\  show     print a recording's summary, or the program's state at a moment
    \\
    \\`ztrace COMMAND --help` for each.
    \\
;

pub fn main(init: std.process.Init) !void {
    const args = try init.minimal.args.toSlice(init.arena.allocator());
    if (args.len < 2) util.fail(usage, .{});
    inline for (@typeInfo(@TypeOf(commands)).@"struct".field_names) |name| {
        if (std.mem.eql(u8, args[1], name)) {
            const command = @field(commands, name);
            if (args.len > 2 and std.mem.eql(u8, args[2], "--help")) {
                std.debug.print("{s}", .{command.usage});
                return;
            }
            return command.run(init, args[2..]);
        }
    }
    util.fail(usage, .{});
}