One of the core components of Merry, is an eBPF-based side effect tracer. eBPF supports adding user-space probes. For this to work, eBPF expects programs to define the absolute offset of the symbol they wish to trace inside the program. In practice, this often requires reading shared libraries that are loaded dynamically at runtime. Luckily enough, Thomas has recently developed a pretty easy-to-use Elf parser in OCaml.
The first user-space function I wanted to trace was libc's getenv. Whilst not all libraries and programming languages use it to interact with the process's environment, some do (like OCaml when you call Sys.getenv).
The ostrace tool I wrote (née opentrace) now supports uprobes.
I also updated the open-traces to use the Linux security module (LSM) hooks that eBPF exposes. These are much more likely to catch all open-like calls. Unfortunately, the hook does not contain the same amount of information as tracing the syscalls or uring requests.
let () = print_endline (Sys.getenv "HOME")
When run through ostrace.
$ ostrace ./main.exe
pid,cgid,comm,kind,flags,mode,filename,payload,return
167048,666510,"main.exe",lsm,32800,67141633,"/home/patrick/documents/phd/dev/ostrace/_build/default/example/main.exe","",0
167048,666510,"main.exe",lsm,32800,67141633,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/ld-linux-x86-64.so.2","",0
167048,666510,"main.exe",openat,524288,0,"/nix/store/vz2z46bspxsljs79w4zm0qv7r5jaxamq-shell/lib/glibc-hwcaps/x86-64-v3/librt.so.1","",-2
167048,666510,"main.exe",openat,524288,0,"/nix/store/vz2z46bspxsljs79w4zm0qv7r5jaxamq-shell/lib/glibc-hwcaps/x86-64-v2/librt.so.1","",-2
167048,666510,"main.exe",openat,524288,0,"/nix/store/vz2z46bspxsljs79w4zm0qv7r5jaxamq-shell/lib/librt.so.1","",-2
167048,666510,"main.exe",openat,524288,0,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/glibc-hwcaps/x86-64-v3/librt.so.1","",-2
167048,666510,"main.exe",openat,524288,0,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/glibc-hwcaps/x86-64-v2/librt.so.1","",-2
167048,666510,"main.exe",lsm,32768,67141633,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/librt.so.1","",0
167048,666510,"main.exe",openat,524288,0,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/librt.so.1","",3
167048,666510,"main.exe",lsm,32768,67141633,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/libm.so.6","",0
167048,666510,"main.exe",openat,524288,0,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/libm.so.6","",3
167048,666510,"main.exe",lsm,32768,67141633,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/libpthread.so.0","",0
167048,666510,"main.exe",openat,524288,0,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/libpthread.so.0","",3
167048,666510,"main.exe",lsm,32768,67141633,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/libc.so.6","",0
167048,666510,"main.exe",openat,524288,0,"/nix/store/8kvxvr3pmsypxiypq4g8zy13glnfr7nx-glibc-2.42-67/lib/libc.so.6","",3
167048,666510,"main.exe",getenv,0,0,"OCAMLRUNPARAM","",0
167048,666510,"main.exe",getenv,0,0,"CAMLRUNPARAM","",0
167048,666510,"main.exe",getenv,0,0,"OCAML_RUNTIME_EVENTS_DIR","",0
167048,666510,"main.exe",getenv,0,0,"OCAML_RUNTIME_EVENTS_PRESERVE","",0
167048,666510,"main.exe",getenv,0,0,"OCAML_RUNTIME_EVENTS_START","",0
167048,666510,"main.exe",getenv,0,0,"OCAMLRUNPARAM","",0
167048,666510,"main.exe",getenv,0,0,"CAMLRUNPARAM","",0
167048,666510,"main.exe",getenv,0,0,"HOME","/home/patrick",1