Saturday, 19 October 2013

DTrace and SDT Probes

I wrote in my prior blog about adding support for SDT probes
in dynamically loaded kernel modules - aimed at people writing their
own drivers, rather than instrumenting the Linux kernel itself.
This experiment is just about complete.

DTrace includes a demonstration driver - "/proc/hello-world"
(located in the driver-2 directory). Its a standalone driver
which simply adds SDT probes in the open() and read() code.

DTrace itself is modified to detect these probes as the
module is loaded.

Heres an example:


/home/fox/src/dtrace@vmarch311-64: build/dtrace -l -P hworld
ID PROVIDER MODULE FUNCTION NAME
227663 hworld open_module open1 entry
227664 hworld open_module open2 entry
227667 hworld read_module read entry
/home/fox/src/dtrace@vmarch311-64: build/dtrace -n hworld:::
dtrace: description 'hworld:::' matched 3 probes
...


"hworld" is the provider name provided in the hello world driver, e.g.


...
DTRACE_PROBE1(hworld, open_module, open1, entry, num_opens);
...


At the moment, the #define for this macro is in the hworld.c driver,
but I will move the definition out of the driver. The macro is very
ugly, but thats really because C string concatentation in macros
is ugly:


#define DTRACE_PROBE1(provider, module, name, func, arg1) \
{extern void __dtrace_##provider##___##module##___##name##___##func(unsigned long); \
__dtrace_##provider##___##module##___##name##___##func ((unsigned long)arg1); \
asm(".pushsection .dtrace_section, \"ax\"\n"); \
asm(".global __dtrace_" #provider "___" #module "___" #name "___" #func "\n"); \
asm(".type __dtrace_" #provider "___" #module "___" #name "___" #func ", @function\n"); \
asm("__dtrace_" #provider "___" #module "___" #name "___" #func ": ret\n"); \
asm(" int3 ; int3 ; int3 ; int3 \n"); \
asm(".popsection\n"); \
}


Post created by CRiSP v11.0.20a-b6616


No comments:

Post a Comment