ctrace allows you to follow the execution of a C program, statement by statement. The effect is similar to executing a shell procedure with the -x option. ctrace reads the C program in filename (or from standard input if you do not specify filename), inserts statements to print the text of each executable statement and the values of all variables referenced or modified, and writes the modified program to the standard output. You must put the output of ctrace into a temporary file because the cc.1v command does not allow the use of a pipe. You then compile and execute this 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, 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 every 1000 times through the loop to help you detect infinite loops. The trace output goes to the standard output so you can put it into a file for examination with an editor or the tail.1 command.
You 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 printed as strings if appropriate. char, short, and int variables are printed as signed integers and, if appropriate, as characters. double variables are printed as floating-point numbers in scientific notation. You can request that variables be printed in additional formats, if appropriate, with these options:
These options are used only in special circumstances:
These options are used to tailor the run-time trace package when the traced program will run in a non-UNIX system environment:
You can do both of these by adding ctroff() and ctron() function calls to your program to turn the tracing off and on, respectively, at execution time. Thus, you can code arbitrarily complex criteria for trace control with if statements, and you can even conditionally include this code because ctrace defines the CTRACE preprocessor variable. For example:
#ifdef CTRACE if (c == '!' && i > 1000) ctron(); #endif
dbx a.outYou can also turn the trace off and on by setting the static variable.B tr_ct_ to 0 and 1, respectively. This is useful if you are using a debugger that cannot call these functions directly, such as adb.1
when at 7 { call ctroff(); cont; }
when at 11 { call ctron(); cont; }
run
If the file lc.c contains this C program:
#include <stdio.h> main() /* count lines in input */ { int c, nl; nl = 0; while ((c = getchar()) != EOF) if (c = '\n') ++nl; printf("%d\n", nl); }
cc lc.c a.out 1 CTRL-D,
If you invoke ctrace with these commands:
ctrace lc.c >temp.c cc temp.c a.out
the output will be:
main() nl = 0; /* nl == 0 */ while ((c = getchar()) != EOF)
The program is now waiting for input. If you enter the same test data as before, the output will be:
/* c == 49 or '1' */ if (c = '\n') /* c == 10 or '\n' */ ++nl; /* nl == 1 */ while ((c = getchar()) != EOF) /* c == 10 or '\n' */ if (c = '\n') /* c == 10 or '\n' */ ++nl; /* nl == 2 */ /* repeating */
If you now enter an end of file character (CTRL-D) the final output will be:
/* repeated <1 time */ while ((c = getchar()) != EOF) /* c == -1 */ printf("%d\n", nl); /* nl == 2 */2 /* return */
Program output is printed at the end of the trace line for the nl variable. 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 your attention is drawn to this if statement, you will probably realize that you used the assignment operator = in place of the equal operator ==. You can easily miss this error during code reading.
This section contains diagnostic messages from both ctrace and cc.1v since the traced code often gets some cc warning messages. You can get cc error messages in some rare cases, all of which can be avoided.
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
compiler takes size of function
yacc stack overflow
out of tree space; simplify expression
redeclaration of signal
Defining a function with the same name as a system function may cause a syntax error if the number of arguments is changed. Just use a different name.
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.
The loop trace output elimination is done separately for each file of a multi-file program. This 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.
Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97