Something went wrong. Try again.
Shells in OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116open Import
type kind = Open | Openat | Openat2 | Env | Pos | Fn | Opt | Gettype action = Read | Write | Delete
let kind_jsont = Jsont.enum ~kind:"Kind" [ ("Open", Open); ("Openat", Openat); ("Openat2", Openat2); ("Env", Env); ("Pos", Pos); ("Fn", Fn); ("Opt", Opt); ("Get", Get) (* Catch-all clause *); ]
let action_jsont = Jsont.enum ~kind:"Action" [ ("Read", Read); ("Write", Write); ("Delete", Delete) ]
let kind_of_string s = match String.lowercase_ascii s with | "open" -> Open | "openat" -> Openat | "openat2" -> Openat2 | "env" -> Env | "pos" -> Pos | "fn" -> Fn | "opt" -> Opt | _ -> invalid_arg ("Unknown trace kind: " ^ s)
type entry = { kind : kind; arg : string; action : action; comm : string }
let make_entry ?(comm = "msh") ~arg kind action = { kind; arg; action; comm }
type t = entry list
let entry_jsont = let make kind arg action comm = { kind; arg; action; comm } in Jsont.Object.map ~kind:"Entry" make |> Jsont.Object.mem "kind" kind_jsont ~enc:(fun t -> t.kind) |> Jsont.Object.mem "arg" Jsont.string ~enc:(fun t -> t.arg) |> Jsont.Object.mem "action" action_jsont ~enc:(fun t -> t.action) |> Jsont.Object.mem "comm" Jsont.string ~enc:(fun t -> t.comm) |> Jsont.Object.finish
let jsont = Jsont.list entry_jsont
module Flags = struct type t = int
let ( + ) a b = a lor b
include Raw_flagsend
let empty = []let has_flag flags f = Int.equal (flags land f) f
let of_csv_line line = match Astring.String.cuts ~sep:"," line with | [ "env"; arg; comm; _ ] -> { kind = Env; comm; arg; action = Read } | [ kind; arg; comm; flags ] -> let kind = kind_of_string kind in let flags = int_of_string flags in let action = if has_flag flags Flags.o_wronly || has_flag flags Flags.o_rdwr then Write else if has_flag flags Flags.o_rdonly then Read else Write in { kind; arg; comm; action } | _ -> invalid_arg ("trace entry: " ^ line)
let add ?(comm = "msh") ~kind ~arg ~action t = { kind; arg; action; comm } :: tlet combine a b = a @ b
let of_bpftrace trace = Astring.String.cuts ~sep:"\n" trace |> List.tl |> List.filter (fun v -> not (String.equal String.empty v)) |> List.map of_csv_line
let pp_entry fmt e = let mode = match e.action with | Write -> "wrote to" | Read -> "read" | Delete -> "deleted" in Fmt.pf fmt "%s %s %s" e.comm mode e.arg
let pp fmt = Fmt.lst pp_entry fmt
let reads ~kind t = List.filter_map (fun e -> match (kind e, e.action) with true, Read -> Some e.arg | _ -> None) t
let writes ~kind t = List.filter_map (fun e -> match (kind e, e.action) with true, Write -> Some e.arg | _ -> None) t
let is_open t = match t.kind with Open | Openat | Openat2 -> true | _ -> falselet is_env t = match t.kind with Env -> true | _ -> false
let diff t1 t2 = let l1 = List.length t1 in assert (l1 <= List.length t2); List.filteri (fun i _ -> i > l1) t2