A simple DTrace userland example on FreeBSD


June 2013.
System used:

FreeBSD hostmaster1.example.com 9.1-STABLE FreeBSD 9.1-STABLE #0 r248486: Thu Jun 13 00:23:59 CEST 2013 root@hostmaster1.example.com:/usr/obj/usr/src/sys/HOSTMASTER1 amd64

Write your code


main.c

#include
#include
#include "main_probes.h"

int main(void) {
unsigned int count = 0;
while(1) {
sleep(1);
SUPERTEST_MACHIN_START(&count);
printf("count: %d\n", count++);
SUPERTEST_MACHIN_DONE();
}
}

main_probes.d

cat main_probes.d
provider supertest {
probe machin__start(unsigned int *);
probe machin__done();
};

Compile



dtrace -C -h -s main_probes.d
clang -c main.c
dtrace -G -s main_probes.d main.o
clang -lelf -o prog main_probes.o main.o

Use


Start the program

./prog
count: 0
count: 1
count: 2
count: 3
count: 4
...

Check that the probes are visible

dtrace -l | grep supertest
4 supertest4056 prog main machin-done
5 supertest4056 prog main machin-start

Use the probes

dtrace -n ':prog::machin-*'
dtrace: description ':prog::machin-*' matched 2 probes
dtrace: buffer size lowered to 2m
CPU ID FUNCTION:NAME
0 5 main:machin-start
0 4 main:machin-done
1 5 main:machin-start
1 4 main:machin-done
0 5 main:machin-start
0 4 main:machin-done
1 5 main:machin-start
1 4 main:machin-done
1 5 main:machin-start
1 4 main:machin-done