⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debug.texi

📁 该文件为c++的数学函数库!是一个非常有用的编程工具.它含有各种数学函数,为科学计算、工程应用等程序编写提供方便!
💻 TEXI
字号:
This chapter describes some tips and tricks for debugging numericalprograms which use GSL.@menu* Using gdb::                   * Examining floating point registers::  * Handling floating point exceptions::  * GCC warning options for numerical programs::  * Debugging References::        @end menu@node Using gdb@section Using gdb@cindex gdb@cindex debugging numerical programs@cindex breakpointsAny errors reported by the library are passed to the function@code{gsl_error}.  By running your programs under gdb and setting abreakpoint in this function you can automatically catch any libraryerrors.  You can add a breakpoint for every session by putting@examplebreak gsl_error@end example@comment @noindentinto your @file{.gdbinit} file in the directory where your program isstarted.  If the breakpoint catches an error then you can use a backtrace(@code{bt}) to see the call-tree, and the arguments which possiblycaused the error.  By moving up into the calling function you caninvestigate the values of variables at that point.  Here is an examplefrom the program @code{fft/test_trap}, which contains the followingline,@smallexamplestatus = gsl_fft_complex_wavetable_alloc (0, &complex_wavetable);@end smallexample@noindentThe function @code{gsl_fft_complex_wavetable_alloc} takes the length ofan FFT as its first argument.  When this line is executed an error willbe generated because the length of an FFT is not allowed to be zero.To debug this problem we start @code{gdb}, using the file@file{.gdbinit} to define a breakpoint in @code{gsl_error},@smallexamplebash$ gdb test_trapGDB is free software and you are welcome to distribute copiesof it under certain conditions; type "show copying" to seethe conditions.  There is absolutely no warranty for GDB;type "show warranty" for details.  GDB 4.16 (i586-debian-linux), Copyright 1996 Free Software Foundation, Inc.Breakpoint 1 at 0x8050b1e: file error.c, line 14.@end smallexample@noindentWhen we run the program this breakpoint catches the error and shows thereason for it. @smallexample(gdb) runStarting program: test_trap Breakpoint 1, gsl_error (reason=0x8052b0d     "length n must be positive integer",     file=0x8052b04 "c_init.c", line=108, gsl_errno=1)     at error.c:1414        if (gsl_error_handler) @end smallexample@comment @noindentThe first argument of @code{gsl_error} is always a string describing theerror.  Now we can look at the backtrace to see what caused the problem,@smallexample(gdb) bt#0  gsl_error (reason=0x8052b0d     "length n must be positive integer",     file=0x8052b04 "c_init.c", line=108, gsl_errno=1)    at error.c:14#1  0x8049376 in gsl_fft_complex_wavetable_alloc (n=0,    wavetable=0xbffff778) at c_init.c:108#2  0x8048a00 in main (argc=1, argv=0xbffff9bc)     at test_trap.c:94#3  0x80488be in ___crt_dummy__ ()@end smallexample@comment @noindentWe can see that the error was generated in the function@code{gsl_fft_complex_wavetable_alloc} when it was called with anargument of @var{n=0}.  The original call came from line 94 in thefile @file{test_trap.c}.By moving up to the level of the original call we can find the line thatcaused the error,@smallexample(gdb) up#1  0x8049376 in gsl_fft_complex_wavetable_alloc (n=0,    wavetable=0xbffff778) at c_init.c:108108   GSL_ERROR ("length n must be positive integer", GSL_EDOM);(gdb) up#2  0x8048a00 in main (argc=1, argv=0xbffff9bc)     at test_trap.c:9494    status = gsl_fft_complex_wavetable_alloc (0,        &complex_wavetable);@end smallexample@comment @noindentThus we have found the line that caused the problem.  From this point wecould also print out the values of other variables such as@code{complex_wavetable}.@node Examining floating point registers@section Examining floating point registersThe contents of floating point registers can be examined using thecommand @code{info float} (on supported platforms).@smallexample(gdb) info float     st0: 0xc4018b895aa17a945000  Valid Normal -7.838871e+308     st1: 0x3ff9ea3f50e4d7275000  Valid Normal 0.0285946     st2: 0x3fe790c64ce27dad4800  Valid Normal 6.7415931e-08     st3: 0x3ffaa3ef0df6607d7800  Spec  Normal 0.0400229     st4: 0x3c028000000000000000  Valid Normal 4.4501477e-308     st5: 0x3ffef5412c22219d9000  Zero  Normal 0.9580257     st6: 0x3fff8000000000000000  Valid Normal 1     st7: 0xc4028b65a1f6d243c800  Valid Normal -1.566206e+309   fctrl: 0x0272 53 bit; NEAR; mask DENOR UNDER LOS;   fstat: 0xb9ba flags 0001; top 7; excep DENOR OVERF UNDER LOS    ftag: 0x3fff     fip: 0x08048b5c     fcs: 0x051a0023  fopoff: 0x08086820  fopsel: 0x002b@end smallexample@noindentIndividual registers can be examined using the variables @var{$reg},where @var{reg} is the register name.@smallexample(gdb) p $st1 $1 = 0.02859464454261210347719@end smallexample@node Handling floating point exceptions@section Handling floating point exceptionsIt is possible to stop the program whenever a @code{SIGFPE} floatingpoint exception occurs.  This can be useful for finding the cause of anunexpected infinity or @code{NaN}.  The current handler settings can beshown with the command @code{info signal SIGFPE}.@smallexample(gdb) info signal SIGFPESignal  Stop  Print  Pass to program DescriptionSIGFPE  Yes   Yes    Yes             Arithmetic exception@end smallexample@noindentUnless the program uses a signal handler the default setting should bechanged so that SIGFPE is not passed to the program, as this would causeit to exit.  The command @code{handle SIGFPE stop nopass} prevents this.@smallexample(gdb) handle SIGFPE stop nopassSignal  Stop  Print  Pass to program DescriptionSIGFPE  Yes   Yes    No              Arithmetic exception@end smallexample@noindentDepending on the platform it may be necessary to instruct the kernel togenerate signals for floating point exceptions.  For programs using GSLthis can be achieved using the @code{GSL_IEEE_MODE} environment variablein conjunction with the function @code{gsl_ieee_env_setup()} as describedin @pxref{IEEE floating-point arithmetic}.@example(gdb) set env GSL_IEEE_MODE=double-precision@end example@node GCC warning options for numerical programs@section GCC warning options for numerical programs@cindex warning options@cindex gcc warning optionsWriting reliable numerical programs in C requires great care.  Thefollowing GCC warning options are recommended when compiling numericalprograms:@comment Uninitialized variables, conversions to and from integers or from@comment signed to unsigned integers can all cause hard-to-find problems.  For@comment many non-numerical programs compiling with @code{gcc}'s warning option@comment @code{-Wall} provides a good check against common errors.  However, for@comment numerical programs @code{-Wall} is not enough. @comment If you are unconvinced take a look at this program which contains an@comment error that can occur in numerical code,@comment @example@comment #include <math.h>@comment #include <stdio.h>@comment double f (int x);@comment int main ()@comment @{@comment   double a = 1.5;@comment   double y = f(a);@comment   printf("a = %g, sqrt(a) = %g\n", a, y);  @comment   return 0;@comment @}@comment double f(x) @{@comment   return sqrt(x);@comment @}@comment @end example@comment @noindent@comment This code compiles cleanly with @code{-Wall} but produces some strange@comment output,@comment @example@comment bash$ gcc -Wall tmp.c -lm@comment bash$ ./a.out @comment a = 1.5, sqrt(a) = 1@comment @end example@comment @noindent@comment Note that adding @code{-ansi} does not help here, since the program does@comment not contain any invalid constructs.  What is happening is that the@comment prototype for the function @code{f(int x)} is not consistent with the@comment function call @code{f(y)}, where @code{y} is a floating point@comment number.  This results in the argument being silently converted to an@comment integer.  This is valid C, but in a numerical program it also likely to@comment be a programming error so we would like to be warned about it. (If we@comment genuinely wanted to convert @code{y} to an integer then we could use an@comment explicit cast, @code{(int)y}).  @comment Fortunately GCC provides many additional warnings which can alert you to@comment problems such as this.  You just have to remember to use them.  Here is a@comment set of recommended warning options for numerical programs.@examplegcc -ansi -pedantic -Werror -Wall -W   -Wmissing-prototypes -Wstrict-prototypes   -Wtraditional -Wconversion -Wshadow  -Wpointer-arith -Wcast-qual -Wcast-align   -Wwrite-strings -Wnested-externs   -fshort-enums -fno-common -Dinline= -g -O4@end example@noindentFor details of each option consult the manual @cite{Using and PortingGCC}.  The following table gives a brief explanation of what types oferrors these options catch.@table @code@item -ansi -pedantic Use ANSI C, and reject any non-ANSI extensions.  These flags help inwriting portable programs that will compile on other systems.@item -Werror Consider warnings to be errors, so that compilation stops.  This preventswarnings from scrolling off the top of the screen and being lost.  Youwon't be able to compile the program until it is completelywarning-free.@item -Wall This turns on a set of warnings for common programming problems.  Youneed @code{-Wall}, but it is not enough on its own.@item -O4Turn on optimization.  The warnings for uninitialized variables in@code{-Wall} rely on the optimizer to analyze the code.  If there is nooptimization then these warnings aren't generated.@item -W This turns on some extra warnings not included in @code{-Wall}, such asmissing return values and comparisons between signed and unsignedintegers.@item -Wmissing-prototypes -Wstrict-prototypes Warn if there are any missing or inconsistent prototypes.  Withoutprototypes it is harder to detect problems with incorrect arguments. @item -Wtraditional This warns about certain constructs that behave differently intraditional and ANSI C. Whether the traditional or ANSI interpretationis used might be unpredictable on other compilers. @item -Wconversion The main use of this option is to warn about conversions from signed tounsigned integers.  For example, @code{unsigned int x = -1}.  If you needto perform such a conversion you can use an explicit cast.@item -WshadowThis warns whenever a local variable shadows another local variable.  Iftwo variables have the same name then it is a potential source ofconfusion.@item -Wpointer-arith -Wcast-qual -Wcast-align These options warn if you try to do pointer arithmetic for types whichdon't have a size, such as @code{void}, if you remove a @code{const}cast from a pointer, or if you cast a pointer to a type which has adifferent size, causing an invalid alignment.@item -Wwrite-stringsThis option gives string constants a @code{const} qualifier so that itwill be a compile-time error to attempt to overwrite them.@item -fshort-enums This option makes the type of @code{enum} as short as possible.  Normallythis makes an @code{enum} different from an @code{int}.  Consequently anyattempts to assign a pointer-to-int to a pointer-to-enum will generate acast-alignment warning.@item -fno-commonThis option prevents global variables being simultaneously defined indifferent object files (you get an error at link time).  Such a variableshould be defined in one file and referred to in other files with an@code{extern} declaration.@item -Wnested-externs This warns if an @code{extern} declaration is encountered within afunction.@item -Dinline= The @code{inline} keyword is not part of ANSI C. Thus if you want to use@code{-ansi} with a program which uses inline functions you can use thispreprocessor definition to remove the @code{inline} keywords.@item -g It always makes sense to put debugging symbols in the executable so thatyou can debug it using @code{gdb}.  The only effect of debugging symbolsis to increase the size of the file, and you can use the @code{strip}command to remove them later if necessary.@end table@comment For comparison, this is what happens when the test program above is@comment compiled with these options.@comment @example@comment bash$ gcc -ansi -pedantic -Werror -W -Wall -Wtraditional @comment -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align @comment -Wwrite-strings -Waggregate-return -Wstrict-prototypes -fshort-enums @comment -fno-common -Wmissing-prototypes -Wnested-externs -Dinline= @comment -g -O4 tmp.c @comment cc1: warnings being treated as errors@comment tmp.c:7: warning: function declaration isn't a prototype@comment tmp.c: In function `main':@comment tmp.c:9: warning: passing arg 1 of `f' as integer rather than floating @comment due to prototype@comment tmp.c: In function `f':@comment tmp.c:14: warning: type of `x' defaults to `int'@comment tmp.c:15: warning: passing arg 1 of `sqrt' as floating rather than integer @comment due to prototype@comment make: *** [tmp] Error 1@comment @end example@comment @noindent@comment The error in the prototype is flagged, plus the fact that we should have@comment defined main as @code{int main (void)} in ANSI C. Clearly there is some@comment work to do before this program is ready to run.@node Debugging References@section References and Further Reading@noindentThe following books are essential reading for anyone writing anddebugging numerical programs with @sc{gcc} and @sc{gdb}.@itemize @asis@itemR.M. Stallman, @cite{Using and Porting GNU CC}, Free SoftwareFoundation, ISBN 1882114388@itemR.M. Stallman, R.H. Pesch, @cite{Debugging with GDB: The GNUSource-Level Debugger}, Free Software Foundation, ISBN 1882114779@end itemize

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -