Shared Reader Stuff

Jokes of the proper kind, properly told, can do more to enlighten questions of politics, philosophy, and literature than any number of dull arguments.

—Isaac Asimov, scientist and writer (1920-92)

(Source: fast-t-feasts, via ashalynd)

Self-modifying code for debug tracing in quasi-C

Printing a program’s state as it runs is the simple but effective debugging tool of programmers everywhere. For efficiency, we usually disable the most verbose output in production. But sometimes you need to diagnose a problem in a deployed system. It would be convenient to declare “tracepoints” and enable them at runtime, like so:

tracepoint foo_entry;

int foo(int n) {
TRACE(foo_entry, "called foo(%d)\n", n);
// ...
}

// Called from UI, monitoring interface, etc.
void debug_foo() {
enable(&foo_entry);
}

Here’s a simple implementation of this API:

typedef int tracepoint;

#define TRACE(_point, _args...) \
do { \
if (_point) printf(_args); \
} while (0)


static inline void enable(tracepoint *point) {
*point = 1;
}

Each tracepoint is simply a global variable. The construct do { ... } while (0) is a standard trick to make macro-expanded code play nicely with its surroundings. We also use GCC’s syntax for macros with a variable number of arguments.

This approach does introduce a bit of overhead. One concern is that reading a global variable will cause a cache miss and will also evict a line of useful data from the cache. There’s also some impact from adding a branch instruction. We’ll develop a significantly more complicated implementation which avoids both of these problems.

Our new solution will be specific to x86-64 processors running Linux, though the idea can be ported to other platforms. This approach is inspired by various self-modifying-code schemes in the Linux kernel, such as ftracekprobesimmediate values, etc. It’s mostly intended as an example of how these tricks work. The code in this article is not production-ready.

Read more…