Definition
Runtime instrumentation is extra code, or debug hardware, that runs at chosen points while a program executes, to record what the program does there or to check or change it. The extra code at a point is a probe; a point that a program offers for attaching such code is a hook. The code is written into the source, inserted by build tools, or attached while the program runs. Instrumentation sees only the events that pass its points, and it changes the timing of what it observes.
The idea
Reading a program’s source shows what the program can do, not what it does on a given input. A developer needs to know how often a device requests heap memory, memory handed out on request while the program runs, of which sizes, and how high heap use climbs in one hour of operation. The answer depends on the packets the device receives, so no reading of the code gives it. A few added lines that run at every call of the allocation function, record the requested size, the returned address and the time, and then let the call go on, give the answer directly. Those added lines are instrumentation.
A traffic counter laid across a road is a fair picture. It counts every car that crosses it and reports the count later. Three things limit what the count means. Cars that take another road are never counted. A counter whose memory is full stops recording while cars keep crossing. And drivers slow down for the counter, so it changes the traffic it measures. Runtime instrumentation has the same three limits: coverage, record loss and perturbation. The picture stops being accurate in one respect: a probe can also check a call and stop it, which a road counter cannot do.
How it works
Instrumenting a program takes five decisions, in this order.
The event. Name it exactly, for example every request for heap memory, and list every way the program can cause it.
The points. Choose where probes run: at a function’s entry or return, at every branch, at every memory access, or at a hook the program already offers. A point before an action sees the request and can prevent the action; a point after it sees the result but cannot prevent it.
The insertion method. The source is edited, or the compiler inserts calls; for example, the option
-finstrument-functions, which common C compilers accept, inserts a call at every function’s entry and exit. Calls of one function are routed through a function wrapper §14.27, a function called in its place that calls it in turn. The program’s machine code is rewritten, before or during the run. An instruction of the running program is overwritten with a jump to a trampoline §14.28, a short piece of code that runs the probe and then continues the original code. Or the processor’s breakpoint and watchpoint §14.29 hardware, which stops the program when it reaches a chosen instruction or touches a chosen address, or its trace hardware reports events without any change to the code. The Static versus dynamic instrumentation §14.25 entry compares these methods.The probe body. The probe reads the context it needs, such as the arguments, the return value and a time stamp, and updates a counter or writes a record into a buffer, often a ring buffer §14.15, which reuses its memory in a circle.
The record path. A reader moves the records out of the buffer, to a file, a host computer or a serial line, before the space is reused. A probe that finds the buffer full loses its record.
After the probe, the original computation continues as if the probe had not run. The probe restores every register it used and the condition flags §4.42, the bits that record the outcome of the last comparison; it leaves the stack, the memory area that holds the local data of active function calls, as it found it; and it passes the return value and any error code through unchanged. A probe that runs inside an interrupt service routine §6.12 (ISR), the code the processor runs when a device signals an interrupt §6.11, must not wait for a lock or allocate memory: the ISR may have interrupted the very code that holds that lock, and that code cannot continue until the ISR finishes.
A probe only observes unless the code that calls it acts on its result. A probe enforces only if it runs before the action and the program skips the action when the probe refuses it. The Observation versus enforcement §1.20 entry treats this difference.
Three properties decide what a set of records proves.
Coverage: which of the real events pass a probe point at all. The Instrumentation coverage §14.35 entry treats it.
Retention: which probe executions leave a record that reaches the analysis. Tracing buffer loss §14.32, records dropped because the buffer was full, is the main cause of gaps here.
Perturbation: how much the probes change the behavior they observe. Instrumentation overhead §11.32 is the cost in time and memory.
Worked example
During one test run a device makes 100 heap allocations; the true number is known from a separate count. The program’s own code makes 80 of them by calling malloc, the C library’s allocation function, through a wrapper that records each call. A driver makes the other 20 by calling the allocator’s internal function directly, which bypasses the wrapper. Of the 80 probe executions, 10 find the trace buffer full, because the reader drains it too slowly, and lose their records. The analysis sees records.
The records cover , or 70%, of the allocations. The missing come from two separate gaps. 20 allocations never reached a probe: a coverage gap. 10 reached a probe but lost their record: a retention gap. A larger buffer, or a faster reader, closes only the retention gap; only a probe on the internal function, or on code that both routes pass, closes the coverage gap. Reading the 70 records as the device made 70 allocations undercounts by 30.
The probes also change the run. An allocation takes 5 microseconds without instrumentation, and the wrapper adds 2 microseconds, so each wrapped allocation takes microseconds, which is , or 40%, longer. Anything that depends on timing, such as which of two threads reaches a shared variable first, can change with it.
Even a wrapper that sees every allocation call sees no other use of memory: stack growth, arrays §14.5 reserved when the program is built, and buffers that a device fills by direct memory access §4.36 (DMA), that is, without the processor. A claim about all memory use needs a probe or another method for each of those categories.
Stated precisely
Let be the set of events of the studied kind that occur in one run, the set of those events that pass a probe point, and the set of events whose record reaches the analysis. Assume that the record path only loses records: it never invents, duplicates or alters one, so each record belongs to exactly one event in . Then , where means that every member of is a member of . The missing events split into two disjoint parts: where is the set of members of that are not in and joins two sets. The recorded fraction is , where is the number of members of a set . In the example, , , , and .
The absence of a record proves the absence of an event only if both parts are shown to be empty: , which is a claim about every route by which the program can cause the event, and , which a counter of lost records in the buffer can confirm for a given run.
A probe that is meant only to observe has this contract: for every input, the program with the probe produces the same outputs, return values, error codes and memory contents, outside the probe’s own records, as the program without it. Timing is never part of that contract. Every probe takes time, so a program whose behavior depends on timing can behave differently when instrumented.
Limits and common mistakes
What instrumentation perturbs. Probes add instructions, so the program runs longer. They displace data from the processor’s caches, the small fast memories that hold recently used data, so later code slows down too. Added code and data move other code and data to new addresses. Probes use stack space, which can turn a deep chain of calls into a stack overflow §4.10, in which the stack outgrows the memory set aside for it. A probe that takes a lock or disables interrupts changes which thread runs when. A race condition §9.2, an error whose outcome depends on the relative timing of two activities, can therefore disappear under instrumentation, or appear only under it.
What instrumentation misses. A probe at a function’s entry misses calls that the compiler inlined, that is, replaced with a copy of the function’s body, so that no call happens; calls to other names or internal functions that do the same work; work that devices do by DMA; objects that already existed when the probe was attached; and events that happen before the instrumentation is set up, such as early in start-up.
A probe that calls the function it instruments, such as an allocation probe that allocates memory for its record, calls itself again and again unless it detects that it is already running and returns at once.
Records can contain secrets, such as keys, passwords and packet contents, and need the same protection as the data they copy. A faulty probe, one that reads a bad address, waits inside an ISR or runs out of stack, crashes or stalls the program it observes.
The instrumented program is a different program. A result measured on an instrumented build holds for that build; a build that ships without the instrumentation needs its own measurement or a separate argument.
Questions answered
What behavior does the instrumentation perturb or fail to see?
Instrumentation perturbs timing and everything that depends on timing, and it fails to see every event that does not pass one of its probe points or whose record is lost. Each probe adds run time, uses stack space, displaces data from the caches and moves code and data to other addresses. A probe that takes a lock or disables interrupts also changes which thread runs when. These changes can alter the order in which concurrent activities meet, so a race condition can vanish or appear under instrumentation.
Instrumentation fails to see calls the compiler inlined, calls to other names or internal functions that do the same work, work that devices do by direct memory access (DMA) without the processor, objects created before the probes were attached, events that happen before the instrumentation starts, and events whose records are dropped when the trace buffer is full. A missing record therefore proves that an event did not happen only if every route to the event passes a probe and the record path is shown to have dropped nothing.
Related entries: Instrumentation overhead (§11.32), Probe and hook (§14.26), Tracing buffer loss (§14.32), Instrumentation coverage (§14.35), Observation versus enforcement (§1.20), Static versus dynamic instrumentation (§14.25), Function wrapper (§14.27), Trampoline (§14.28).