Definition
Static instrumentation is the insertion of probes, extra code that records or checks what a program does, before the program runs: in the source, through the compiler or linker §12.4, or by rewriting the program file. Dynamic instrumentation is the insertion, change or removal of probes while the program runs, without restarting it. The difference that matters is when the code changes. Static instrumentation needs a new program file and a restart. Dynamic instrumentation changes code that may be executing at that moment, and must make that change safely.
Where it fits
Main entry: Runtime instrumentation (§14.24).
The Runtime instrumentation entry explains probes, coverage and perturbation, which are the same for both kinds. The moment of insertion decides which of three costs a method pays: a rebuild, the correction of moved code, or a pause of the running program.
Source, compiler or linker insertion (static): probes are written into the source, inserted by the compiler, or added by the linker, which can route the calls of a chosen function to a function wrapper §14.27, a function that runs in its place and calls it. The result is a new program file, which must be built and started.
Static binary rewriting: a tool inserts code into a finished program file. Everything after an insertion moves, so every branch, pointer and table that refers to a moved address must be found and corrected; one missed reference makes the program jump to a wrong place.
Dynamic binary translation (dynamic): tools such as Valgrind, Pin and DynamoRIO copy each piece of code into a separate memory area just before it first runs, insert the probes into the copy, and run the copy. The original code is never written, so nothing is paused, but translation costs run time.
In-place patching (dynamic): a tool overwrites instructions of the running code with a breakpoint §14.29 instruction, which stops the program and hands control to the tool, or with a jump to a trampoline §14.28, a short piece of code that runs the probe and then the overwritten instructions, which must be corrected for their new address.
Prepared hook points §14.26 (built in, switched on while running): a function pointer that is normally empty, a flag, or reserved no-operation instructions at the start of each function. Switching a point on changes one pointer, flag or reserved instruction, but only points built in beforehand exist.
Changing instructions in place needs safe publication: a processor that fetches the patched bytes must see either all old or all new bytes, and its instruction cache, a copy of recently fetched code, must stop supplying the old bytes. Where one write cannot do that, the program is paused, or the change waits for a quiescent point §14.34, a moment when no thread executes the code being changed. The Dynamic patching and hot update §14.33 entry treats these steps.
Questions answered
When must code be rebuilt, relocated, or temporarily paused?
Code must be rebuilt when probes are inserted in the source, by the compiler or by the linker, because the probes become part of a new program file. Code must be relocated, meaning its addresses and every reference to them are corrected, whenever instructions move: when binary rewriting inserts code and shifts what follows, and when in-place patching copies overwritten instructions into a trampoline that runs them elsewhere. A running program must be paused, or the change must wait until no thread executes the affected code, when instructions are overwritten in place and no single write lets every processor see either all old or all new bytes. Dynamic binary translation needs neither a rebuild nor a pause, because it runs a modified copy, which the tool relocates itself, and never writes the original code. A prepared hook point is switched on by one write; a hook point that was not built in needs a rebuild.
Related entries: Runtime instrumentation (§14.24), Trampoline (§14.28), Dynamic patching and hot update (§14.33), Quiescent point (§14.34), Function wrapper (§14.27).