Proactive tracing

QNX SDP8.0System ArchitectureDeveloperUser

While the kernel provides an excellent unobtrusive method for instrumenting and monitoring processes, threads, and the state of your system in general, you can also have your applications proactively influence the event-collection process.

Using the TraceEvent() library call, applications themselves can inject custom events into the trace stream. This facility is especially useful when building large, tightly coupled, multicomponent systems.

First, one process must register itself as the designated trace-logging process by issuing the _NTO_TRACE_LOGGER_ATTACH command. This command has start_ev and stop_ev parameters, which can be NULL or pointers to sigevents that are to be delivered when tracing starts and stops:
TraceEvent(_NTO_TRACE_LOGGER_ATTACH, start_ev, stop_ev);
Note:
Unlike in past releases, only one process can perform proactive tracing at a given time.
Then, the following simple call would inject the integer values of eventcode, first, and second into the event stream:
TraceEvent(_NTO_TRACE_INSERTSUSEREVENT, eventcode, first, second);
You could also inject a string event (e.g., My Event) into the event stream, as shown in the following code:
#include <stdio.h>
#include <sys/trace.h>

/* Code to associate with emitted events */
#define MYEVENTCODE 12

int main(int argc, char **argv) {
    printf("My pid is %d \n", getpid());

    /* Inject two integer events (26, 1975) */
    TraceEvent(_NTO_TRACE_INSERTSUSEREVENT, MYEVENTCODE, 26, 1975);

    /* Inject a string event (My Event) */
    TraceEvent(_NTO_TRACE_INSERTUSRSTREVENT, MYEVENTCODE, "My Event");

    return 0;
}
The output, as gathered by the traceprinter data interpreter, would look something like this:
.
.
.
t:0x38ea737e CPU:00 USREVENT:EVENT:12, d0:26 d1:1975
.
.
.
t:0x38ea7cb0 CPU:00 USREVENT:EVENT:12 STR:"My Event"

Note that 12 was specified as the trace user eventcode for these events.

Page updated: