ctrace.1
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 1 代码 · 共 507 行
1
507 行
.\" SCCSID: @(#)ctrace.1 8.2 11/8/90.TH ctrace 1.SH Namectrace \- C program debugger.SH Syntax\fBctrace\fR [\|\fIoptions\fR\|] [\|\fIfile\fR\|].br\fBctc\fR [\|\fIoptions\fR\|] [\|\fIfile\fR\|] .br\fBctcr\fR [\|\fIoptions\fR\|] [\|\fIfile\fR\|].SH Description.NXR "ctrace debugger".NXA "cc compiler" "ctrace debugger".NXR "ctc command".NXR "ctcr command"The .PN ctracecommand allows youto follow the execution of a C program, statement by statement.The.PN ctracecommand reads the C program in \fIfile\fR (or from standardinput if you do not specify \fIfile\fR) and inserts statements to printboth the text of each executable statement and the values of allvariables referenced or modified. It thenwrites the modified program to the standard output.You must put the output of .PN ctraceinto a temporary file becausethe .PN cccommand does not allow the use of a pipe.You then compile and execute this file..PPAs each statement in the program executes it is listed at theterminal. The statement is followed by the name and valueof any variables referenced ormodified in the statement, which is followed by any output from the statement.Loops in the trace output are detected and tracing is stopped until theloop is exited or a different sequence of statements within the loop isexecuted.A warning message is printed every 1000 times through the loop tohelp you detect infinite loops..PPThe trace output goes to the standard output so you can put it into a file for examination with an editor or the.PN tailcommand..PPThe.PN ctccommand is a shell script that prepares the specified C program.I filefor later execution.The.PN ctcrcommand is a shell script that both prepares and executes the specified C program.I file\^..SH Options.NXR "ctrace debugger" "options"The only options you will commonly use are:.TP 20.BI \-f " functions"Trace only these.I functions..TP.BI \-v " functions"Trace all but these.I functions..PPYou 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 printedas signed integers and, if appropriate, as characters.Double variables are printedas floating point numbers in scientific notation..igString arguments to the.MS string 3functions and return values from.PN gets ,.PN fgets ,and.PN sprintfare printed as strings....sp .5You can request that variables be printed in additional formats, ifappropriate, with these options:.TP 20.B \-eFloating point.TP.B \-oOctal.TP.B \-uUnsigned.TP.B \-xHexadecimal.PPThese options are used only in special circumstances:.TP 20.BI \-l " n"Checks.I nconsecutively executed statementsfor looping trace output, instead of thedefault of 20. Use 0 to get all the trace output from loops..TP.B \-PRuns the C preprocessor on the input before tracing it.You can also use the.BR \-D ,.BR \-I ,and.B \-Ucc(1)preprocessor options..TP.BI \-p " s"Changes the trace print functions from the default of \*(lqprintf(\*(rq.For example, \*(lqfprintf(stderr,\*(rqwould send the trace to the standard erroroutput..TP.BI \-r " f"Uses file.I fin place of the.I runtime.ctrace function package.This lets you change the entire print function, instead of just thename and leading arguments. For further information, see the.B \-poption..TP.B \-sSuppresses redundant trace output from simple assignment statements andstring copy function calls.This option can hide a bug caused by useof the = operator in place of the== operator..TP.BI \-t " n"Traces.I nvariables per statement instead of the default of 10(the maximum number is 20).The DIAGNOSTICS section explains when to use this option..SH Examples.NXR(e) "ctrace command"Assume the file \fIlc.c\fR contains the following C program:.EX 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 }.EE.sp .5When you enter the following commands and test datathe program is compiled and executed:.EXcc lc.ca.out1<CTRL/D>.EEThe output of the program is the number \fB2\fR, which is notcorrect because there is only one line in the test data.The error in this program is common, but subtle.When you invoke .PN ctracewith the following commands:.EXctrace lc.c >temp.ccc temp.ca.out.EEthe output is.EX 2 main() 6 nl = 0; /* nl == 0 */ 7 while ((c = getchar()) != EOF).EEThe program is now waiting for input.If you enter the same test data as before, the output is the following:.EX /* 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 */ 7 while ((c = getchar()) != EOF).EEIf you now enter an end of file character <CTRL/D>, the final outputis the following:.EX /* c == \-1 */10 printf("%d\\n", nl); /* nl == 2 */2 \* return \*.EE.brNote that the program output printed at the end of the trace linefor the \fBnl\fR variable.Also note the \fBreturn\fR comment addedby .PN ctraceat the end of the trace output.This shows the implicit return at the terminating brace in the function..PPThe trace output shows that variable \fBc\fR is assignedthe value \*(lq1\*(rq inline 7, but in line 8 it has been assigned the value \*(lq\\n\*(rq.Once your attention is drawn to this \fIif\fR statement,you realize that you used the assignment operator (=)instead of the equal operator (==) as intended in line 8.You can easily miss this error during code reading..SH Execution-time Trace Control.NXR "ctrace debugger" "statement-by-statement control"The default operation for.PN ctraceis to trace the entire program file, unless you use the.B \-for.B \-voptions to trace specific functions.This does not give you statement by statement controlof the tracing, nordoes it let you turn the tracing off and on when executing the tracedprogram..PPYou can do both of these by adding.PN ctroffand .PN ctronfunction 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.I ifstatements, and you can even conditionally include this code because .PN ctracedefines the CTRACE preprocessor variable.For example:.EX#ifdef CTRACE if (c == '!' && i > 1000) ctron();#endif.EEYou can also turn the trace off and onby setting static variable tr_ct_ to0 and 1, respectively.This is useful if you are using a debugger that cannot call thesefunctions directly, such as .MS adb 1 ..SH RestrictionsThe.NXR "ctrace debugger" "restricted".PN ctracecommand does not know about the componentsof aggregates such as structures,unions, and arrays. It cannot choose a format to print all thecomponents of an aggregate when an assignment is made to the entireaggregate. The.PN ctrace command may choose to print the address of an aggregateor use the wrong format(for example, %e for a structure with two integermembers) when printing the value of an aggregate..PPPointer values are always treated as pointers to character strings..PPThe loop trace output elimination is done separately for each file of amulti-file program. This can result in functions called from aloop still being traced, or the elimination of trace output from onefunction in a file until another in the same file is called..SH WarningsYou get a.PN ctracesyntax error if you omit the semicolon at the end of the last elementdeclaration in a structure or union, just before the right brace (}).This is optional in some C compilers..PP Defining a function with the same name as a system functionmay cause a syntax error if the number of arguments ischanged. Use a different name..PPThe.PN ctracecommand assumes that BADMAG is a preprocessor macro, and thatEOF and NULL are .PN #definedconstants.Declaring any of these to be variables, for example,.PN int\0EOF; ,will cause asyntax error..SH DiagnosticsThis section contains diagnostic messages from both.PN ctraceand.PN cc ,since the traced code often gets some.PN ccwarning messages.You can get.PN ccerror messages in some rare cases, all of which can be avoided..SS ctrace Diagnostics.NXR "ctrace debugger" "diagnostics"warning: some variables are not traced in this statement.sp .5.RSOnly 10 variables are traced in a statement to prevent the C compiler"out of tree space; simplify expression" error.Use the \fB\-t\fR option to increase this number..RE.PPwarning: statement too long to trace.sp .5.RSThis statement is over 400 characters long.Make sure that you are using tabs to indent your code, not spaces..RE.PPcannot handle preprocessor code, use .B \-P option.sp .5.RSThis is usually caused by .PN #ifdef/#endifpreprocessor statements in themiddle of a C statement, or by a semicolon at the end of a .PN #definepreprocessor statement..RE.PP\&'if ... else if' sequence too long.sp .5.RSSplit the sequence by removing an \fBelse\fR from the middle..RE.PPpossible syntax error, try -P option.sp .5.RSUse the.B \-Poption to preprocess the.PN ctraceinput, along with any appropriate.BR \-D ,.BR \-I ,and.B \-Upreprocessor options.If you still get the error message, check the Warnings section above..sp .5Using.PN ctracewith the.B \-Poption on a program thatincludes .PN <sys/types.h>or a header file that includes.PN <sys/types.h> also causes this error even though you are already using the.BR \-Poption..sp .5To avoid the problem, when you are using .PN ctrace\0-P ,surround the.PN #includeline for.PN <sys/types.h>with .PN #ifndef\0CTRACE/.PN #endif\0CTRACEpairs as shown in this example:.EX#ifndef CTRACE#include <sys/types.h>#endif CTRACE.EE.sp .5The .PN ctraceprogram defines the CTRACE preprocessor variable during its execution and ignores the files when it processes yourprogram.Later when you compile your program, the files are includedas usual by the C preprocessor.Note that the files listed here also include.PN <sys/types.h> or they may be included in other include files.Surrounding them with.PN #ifndef\0CTRACE/.PN #endif\0CTRACEpairs may allow .PN ctrace to process your program successfully..EX<sys/audit.h> <auth.h><sys/file.h> <grp.h><sys/param.h> <pwd.h><sys/socket.h> <signal.h><sys/sysmacros.h> <sys/time.h><sys/wait.h>.EE.RE.SS cc Diagnostics.NXR "cc compiler" "diagnostics"warning: floating point not implemented.brwarning: illegal combination of pointer and integer.brwarning: statement not reached.brwarning: sizeof returns 0.sp .5.RSIgnore these messages..RE.PPcompiler takes size of function.sp .5.RSSee the.PN ctrace"possible syntax error" message above..RE.PPyacc stack overflow.sp .5.RSSee the.PN ctrace\ 'if .. else if' "sequence too long" message above..RE.PPout of tree space; simplify expression.sp .5.RSUse the \fB\-t\fR option to reduce the number oftraced variables per statement from the default of 10.Ignore the "ctrace: too many variables to trace"warnings you will now get..RE.br.ne 7.PPredeclaration of signal.sp .5.RSYou may either need to correct the declaration of.MS signal 3 ,or to surround the .PN #include.PN <signal.h>statement with an.PN #ifndef\0CTRACE/.PN #endif\0CTRACEpair as described in the .PN ctraceDiagnostics section..RE.PPunimplemented structure assignment.sp .5.RSUse .PN pccinstead of .MS cc 1 ..RE.SH Files/usr/bin/ctc preparation shell script.br/usr/bin/ctcr preparation and run shell script.br/usr/lib/ctrace/runtime.c run-time trace package.SH See Alsoctype(3), printf(3s), setjmp(3), signal(3), string(3)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?