The AIX trace facility can be started from a program, via a subroutine call. The subroutine is trcstart and is in the librts.a library. The syntax of the trcstart subroutine is:
int trcstart(char *args)
where args is simply the options list that you would have entered for the trace command. By default, the system trace (channel 0) is started. If you want to start a generic trace, you should include a -g option in the args string. On successful completion, the trcstart subroutine returns the channel ID. For generic tracing this channel ID can be used to record to the private generic channel.
When compiling a program using this subroutine, the link to the librts.a library must be specifically requested (use -l rts as a compile option).
The controls for the trace routine are available as subroutines from the librts.a library. The subroutines return zero on successful completion. The subroutines are:
int trcon() | Begins or resumes collection of trace data. |
int trcoff() | Suspends collection of trace data. |
int trcstop() | Stops collection of trace data and terminates the trace routine. |
Each of the above subroutines for controlling trace:
To turn tracing on and off around individual sections of code, it may be more efficient for a program to issue the ioctl controls directly. This avoids the repetitive opening and closing of the trace control device. To use the ioctl interface in a program, include <sys/trcctl.h> to define the ioctl commands. The syntax of the ioctl is as follows:
ioctl (fd, CMD,Channel)
where:
fd | File descriptor returned from opening /dev/systrctl |
CMD | One of: TRCON, TRCOFF, or TRCSTOP |
Channel | Trace channel (0 for system trace) |
The following code example shows how to start a trace from a program and only trace around a specified section of code:
#include <fcntl.h> #include <sys/trcctl.h> extern int trcstart(char *arg); char *ctl_dev ="/dev/systrctl"; int ctl_fd; main() { printf("configuring trace collection \n"); if (trcstart("-ad")){ perror("trcstart"); exit(1); } printf("opening the trace device \n"); if((ctl_fd =open (ctl_dev,O_RDWR))<0){ perror("open ctl_dev"); exit(1); } printf("turning data collection on \n"); if(ioctl(ctl_fd,TRCON,0)){ perror("TRCON"); exit(1); } /* *** code here will be traced *** */ printf("The code to print this line will be traced."); printf("turning data collection off\n"); if (ioctl(ctl_fd,TRCOFF,0)){ perror("TRCOFF"); exit(1); } printf("stopping the trace daemon \n"); if (trcstop(0)){ perror("trcstop"); exit(1); } exit(0); }
Since no output file was specified in the parameter to the trcstart() subroutine, the output of the trace will be in /var/adm/ras/trcfile, which is also the default input file of the trcrpt command.