ctrace [options] [file]
As each statement in the program executes, it will be listed at the terminal, followed by the name and value of any variables referenced or modified in the statement; these variable names and values will be followed by any output from the statement. Loops in the trace output are detected and tracing is stopped until the loop is exited or a different sequence of statements within the loop is executed. A warning message is printed after each 1000 loop cycles to help the user detect infinite loops. The trace output goes to the standard output so the user can put it into a file for examination with an editor or the bfs(1) or tail(1) commands.
The options commonly used are:
The user may want to add to the default formats for printing variables. Long and pointer variables are always printed as signed integers. Pointers to character arrays are also printed as strings if appropriate. char, short, and int variables are also printed as signed integers and, if appropriate, as characters. float, double, and long double variables are printed as floating point numbers in scientific notation. The user can request that variables be printed in additional formats, if appropriate, with these options:
These options are used only in special circumstances:
1 #include <stdio.h> 2 main() / count lines in input / 3 { 4 int c, nl; 5 6 nl = 0; 7 while ((c = getchar()) != EOF) 8 if (c = '\n') 9 ++nl; 10 printf("%d\n", nl); 11 }
these commands and test data are entered:
cc lc.c
a.out
1
<Ctrl>-d
the program will be compiled and executed.
The output of the program will be the number 2, which is
incorrect because there is only one line in the test data.
The error in this program is common, but subtle.
If the user invokes ctrace with these commands:
ctrace lc.c >temp.c
cc temp.c
a.out
the output will be:
2 main() 6 nl = 0; / nl == 0 / 7 while ((c = getchar()) != EOF)
The program is now waiting for input. If the user enters the same test data as before, the output will be:
/ c == 49 or '1' / 8 if (c = '\n') /* c == 10 or '\n' */ 9 ++nl; /* nl == 1 */ 7 while ((c = getchar()) != EOF) /* c == 10 or '\n' */ 8 if (c = '\n') /* c == 10 or '\n' */ 9 ++nl; /* nl == 2 */ /* repeating */
Once the end-of-file character (<Ctrl>-d) is entered, the final output will be:
/* repeated < 1 time */ 7 while ((c = getchar()) != EOF) /* c == -1 */ 10 printf("%d\n", nl); /* nl == 2 */2/* return */
Note the information printed out at the end of the trace line for the nl variable following line 10. Also note the return comment added by ctrace at the end of the trace output. This shows the implicit return at the terminating brace in the function.
The trace output shows that variable c is assigned the value 1 in line 7, but in line 8 it has the value ``\n''. Once user attention is drawn to this if statement, he or she will probably realize that the assignment operator (=) was used in place of the equality operator (==). This error can easily be missed during code reading.
The user can do both of these by adding ctroff() and ctron() function calls to the program to turn the tracing off and on, respectively, at execution time. Thus, complex criteria can be arbitrarily coded for trace control with if statements, and this code can even be conditionally included because ctrace defines the CTRACE preprocessor variable. For example:
#ifdef CTRACE if (c == '!' && i > 1000) ctron(); #endif
These functions can also be called from debug(1) if they are compiled with the -g option. For example, to trace all but lines 7 to 10 in the main function, enter:
debug a.out debug> stop lc.c @7 {set ctroff();} EVENT [1] assigned debug> stop lc.c @11 {set ctron();} EVENT [2] assigned debug> run
The trace can be turned off and on by setting static variable tr_ct_ to 0 and 1, respectively. This on/off option is useful if a user is using a debugger that can not call these functions directly.
warning: some variables are not traced in this statement
warning: statement too long to trace
cannot handle preprocessor code, use -P option
'if . . . else if' sequence too long
possible syntax error, try -P option
ctrace assumes that BADMAG is a preprocessor macro, and that
EOF and NULL are #defined constants.
Declaring any of these to be variables, for example,
"int EOF;", will cause a syntax error.
Pointer values are always treated as pointers to character strings.
ctrace does not know about the components of aggregates like structures, unions, and arrays. It cannot choose a format to print all the components of an aggregate when an assignment is made to the entire aggregate. ctrace may choose to print the address of an aggregate or use the wrong format (for example, 3.149050e-311 for a structure with two integer members) when printing the value of an aggregate.
The loop trace output elimination is done separately for each file of a multi-file program. Separate output elimination can result in functions called from a loop still being traced, or the elimination of trace output from one function in a file until another in the same file is called.