magic-trace collects and displays high-resolution traces of what a process is doing. People have used it to:
magic-trace:
You use it like perf: point it to a process and off it goes. The key difference from perf is that instead of sampling call stacks throughout time, magic-trace uses Intel Processor Trace to snapshot a ring buffer of all control flow leading up to a chosen point in time^2. Then, you can explore an interactive timeline of what happened.
You can point magic-trace at a function such that when your application calls it, magic-trace takes a snapshot. Alternatively, attach it to a running process and detatch it with Ctrl+C, to see a trace of an arbitrary point in your program.
Grab a release binary from the latest release page.
chmod +x magic-trace^4sudo dpkg -i magic-trace*.debThen, test it by running magic-trace -help, which should bring up some help text.
man 3 dlopen. Download that, build it with gcc -ldl demo.c -o demo, then leave it running ./demo. We're going to use that program to learn how dlopen works.magic-trace attach -pid $(pidof demo). When you see the message that it's successfully attached, wait a couple seconds and Ctrl+C magic-trace. It will output a file called trace.fxt in your working directory.
That should have expanded into a trace. Zoom in until you can see an individual loop through dlopen/dlsym/cos/printf/dlclose.
cos. On my screen it takes ~5.7us.
Congratulations, you just magically traced your first program!
In contrast to traditional perf workflows, magic-trace excels at hypothesis generation. For example, you might notice that taking 6us to run cos is a really long time! If you zoom in even more, you'll see that there's actually five pink "[untraced]" cells in there. If you re-run magic-trace with root and pass it -trace-include-kernel, you'll see stacktraces for those. They're page fault handlers! The demo program actually calls cos twice. If you zoom in even more near the end of the 6us cos call, you'll see that the second call takes far less time and does not page fault.
magic-trace continuously records control flow into a ring buffer. Upon some sort of trigger, it takes a snapshot of that buffer and reconstructs call stacks.
There are two ways to take a snapshot:
We just did this one: Ctrl+C magic-trace. If magic-trace terminates without already having taken a snapshot, it takes a snapshot of the end of the program.
You can also trigger snapshots when the application calls a function. To do so, pass magic-trace the -trigger flag.
-trigger ? brings up a fuzzy-finding selector that lets you choose from all symbols in your executable,-trigger SYMBOL selects a specific, fully mangled, symbol you know ahead of time, and-trigger . selects the default symbol magic_trace_stop_indicator.Stop indicators are powerful. Here are some ideas for where you might want to place one:
You may leave the stop indicator in production code. It doesn't need to do anything in particular, magic-trace just needs the name. It is just an empty, but not inlined, function. It will cost ~10us to call, but only when magic-trace actually uses it to take a snapshot.
More documentation is available on the magic-trace wiki.
Join us on Discord to chat synchronously, or the GitHub discussion group to do so asynchronously.
If you'd like to contribute:
magic-trace does not send your code or derivatives of your code (including traces) anywhere.
The magic-trace UI is based on Perfetto, and runs entirely in your browser. As far as we can tell, that UI does not send your trace anywhere. If you're worried about that changing one day, set up your own local copy of the Perfetto UI and use that instead.
Tristan Hume is the original author of magic-trace. He wrote it while working at Jane Street, who currently maintains it.
Intel PT is the foundational technology upon which magic-trace rests. We'd like to thank the people at Intel for their years-long efforts to make it available, despite its slow uptake in the greater software community.
magic-trace would not be possible without perfs extensive support for Intel PT. perf does most of the work in interpreting Intel PT's output, and magic-trace likely wouldn't exist were it not for their efforts. Thank you, perf developers.
magic-trace's UI is a fork of Perfetto, with minor modifications. We'd like to thank the people at Google responsible for it. It's a high quality codebase that solves a hard problem well.
The ideas behind magic-trace are in no way unique. We've written down a list of prior art that has influenced its design.