tracexec
A small utility for tracing execve{,at} and pre-exec behavior.
tracexec helps you to figure out what and how programs get executed when you execute a command.
It's useful for debugging build systems, understanding what shell scripts actually do, figuring out what programs does a proprietary software run, etc.
Showcases
TUI mode with pseudo terminal
In TUI mode with a pseudo terminal, you can view the details of exec events and interact with the processes within the pseudo terminal at ease.
Tracing setuid binaries
With root privileges, you can also trace setuid binaries and see how they work. But do note that this is not compatible with seccomp-bpf optimization so it is much less performant. You can use eBPF mode which is more performant in such scenarios.
sudo tracexec --user $(whoami) tui -t -- sudo ls
Nested setuid binary tracing is also possible: A real world use case is to trace extra-x86_64-build
(Arch Linux's build tool that requires sudo):
In this real world example, we can easily see that _FORTIFY_SOURCE
is redefined from 2
to 3
, which lead to a compiler error.
Use tracexec as a debugger launcher
tracexec can also be used as a debugger launcher to make debugging programs easier. For example, it's not trivial or convenient to debug a program executed by a shell/python script(which can use pipes as stdio for the program). The following video shows how to use tracexec to launch gdb to detach two simple programs piped together by a shell script.
https://github.com/kxxt/tracexec/assets/18085551/72c755a5-0f2f-4bf9-beb9-98c8d6b5e5fd
Please read the gdb-launcher example for more details.
eBPF mode
The eBPF mode is currently experimental.
It is known to work on Linux 6.6 lts and 6.10 and probably works on all 6.x kernels.
For kernel versions less than 6.2, you'll need to enable ebpf-no-rcu-kfuncs
feature.
It won't work on kernel version < 5.17.
The following examples shows how to use eBPF in TUI mode.
The eBPF
command also supports regular log
and collect
subcommands.
System-wide Exec Tracing
Follow Fork mode with eBPF
Log mode
In log mode, by default, tracexec
will print filename, argv and the diff of the environment variables and file descriptors.
example: tracexec log -- bash
(In an interactive bash shell)
Reconstruct the command line with --show-cmdline
# example:
Try to reproduce stdio in the reconstructed command line
--stdio-in-cmdline
and --fd-in-cmdline
can be used to reproduce(hopefully) the stdio used by a process.
But do note that the result might be inaccurate when pipes, sockets, etc are involved.
Show the interpreter indicated by shebang with --show-interpreter
And show the cwd with --show-cwd
.
# example: Running Arch Linux makepkg
Usage
General CLI help:
)
TUI Mode:
<CMD>...
)
Log Mode:
<CMD>...
)
Collect and export data:
Collect exec events and export them
Usage: tracexec collect [OPTIONS] --format <FORMAT> -- <CMD>...
Arguments:
<CMD>... command to be executed
Options:
--successful-only Only show successful calls
--fd-in-cmdline [Experimental] Try to reproduce file descriptors in commandline. This might result in an unexecutable cmdline if pipes, sockets, etc. are involved.
--stdio-in-cmdline [Experimental] Try to reproduce stdio in commandline. This might result in an unexecutable cmdline if pipes, sockets, etc. are involved.
--resolve-proc-self-exe Resolve /proc/self/exe symlink
--no-resolve-proc-self-exe Do not resolve /proc/self/exe symlink
--seccomp-bpf <SECCOMP_BPF> Controls whether to enable seccomp-bpf optimization, which greatly improves performance [default: auto] [possible values: auto, on, off]
--tracer-delay <TRACER_DELAY> Delay between polling, in microseconds. The default is 500 when seccomp-bpf is enabled, otherwise 1.
-F, --format <FORMAT> the format for exported exec events [possible values: json-stream, json]
-p, --pretty prettify the output if supported
-o, --output <OUTPUT> Output, stderr by default. A single hyphen '-' represents stdout.
--foreground Set the terminal foreground process group to tracee. This option is useful when tracexec is used interactively. [default]
--no-foreground Do not set the terminal foreground process group to tracee
-h, --help Print help
eBPF backend supports similar commands:
Experimental ebpf mode
Usage: tracexec ebpf <COMMAND>
Commands:
log Run tracexec in logging mode
tui Run tracexec in TUI mode, stdin/out/err are redirected to /dev/null by default
collect Collect exec events and export them
help Print this message or the help of the given subcommand(s)
Options:
-h, --help Print help
Profile
tracexec
can be configured with a profile file. The profile file is a toml file that can be used to set fallback options.
The profile file should be placed at $XDG_CONFIG_HOME/tracexec/
or $HOME/.config/tracexec/
and named config.toml
.
A template profile file can be found at https://github.com/kxxt/tracexec/blob/main/config.toml
As a warning, the profile format is not stable yet and may change in the future. You may need to update your profile file when upgrading tracexec.
Known issues
- Non UTF-8 strings are converted to UTF-8 in a lossy way, which means that the output may be inaccurate.
- For eBPF backend, it might be impossible to show some details of the tracee, See https://mozillazg.com/2024/03/ebpf-tracepoint-syscalls-sys-enter-execve-can-not-get-filename-argv-values-case-en.html
- The output is not stable yet, which means that the output may change in the future.
- Test coverage is not good enough.
- The pseudo terminal can't pass through certain key combinations and terminal features.
Origin
This project was born out of the need to trace the execution of programs.
Initially I simply use strace -Y -f -qqq -s99999 -e trace=execve,execveat <command>
.
But the output is still too verbose so that's why I created this project.