I met Segmentation Fault several times. So, gdb is the TOOL I have to learn.
Regarding to seg fault:
- At that point you should check the values of all array indices and pointers which are referenced in the line at which the error occurred. Typically you will find that either you have an array index with a value far out of range, or a pointer which is 0 (and thus unrefenceable).
- Another common error is forgetting an ampersand in a function call, say scanf().
- Still another one is that you have made a system call which failed, but you did not check its return value for an error code.
You compile with -g option.
GCC provides the
-gdebug option to store additional debugging information in object files and executables. This debugging information allows errors to be traced back from a specific machine instruction to the corresponding line in the original source file, and permits the execution of a program to be traced in a debugger, such as the GNU Debuggergdb. The debug option works by storing the names of functions and variables (and all the references to them) with the corresponding source code line-numbers in a symbol table in object files and executables.
gcc -g src.c -o src
Then, start gdb by typing
gdb src
Then, r command means run program.
r < input
I saw my problem pinpoint:
Program received signal SIGSEGV, Segmentation fault.
0x08048642 in entab (s=0xbfffefe4 ‘\001’ <repeats 110 times>) at Ex_1-21.c:41
41 for(i = 0; s[i] != ‘\n’; ++i){
Simple command:
- l: list
- b: breakpoint
- diable: cancel a breakpoint
- c: continue
- d: prints out the value of the indicated variable or expression every time the program pauses (e.g. at breakpoints and after executions of the n and s commands).
- undisplay: cancel a disp command
- p: prints out the value of the variable or expression just once.
- printf: (NO parentheses)
printf "X = %d, Y = %d\n",X,Y
- n: next
- s: step
- bt: backtrace
The program “call stack” is a list of functions which led up to the current one. Each function and its variables are assigned a “frame” with the most recently called function in frame 0 (the “bottom” frame).
- set: change the value of a program variable
- call: call a function in your program during execution
- define macroName … end: put together one or more commands into a macro. Then you could invoke it just by typing `macroName.
- kill: relinquish our executable file
- info locals: gdb will show the value of all local variables
- watch: A watchpoint will break program execution when the specified expression changes value, but it must be set when the variables used in the expression are in scope
- core file:
Core file contains the in-memory state of the program at the time it crashed.
To set core file size:
First, check $ulimit -c
If the result is zero, then it can be increased with the following command to allow core files of any size to be written:
$ulimit -c unlimited
Note that this setting only applies to the current shell.
- frame: transfer among frames
- shell: use shell command inside gdb
Regarding to core file: http://www.network-theory.co.uk/articles/gccdebug.html
http://www.ibm.com/developerworks/library/l-gdb/
More Reference:
http://heather.cs.ucdavis.edu/~matloff/UnixAndC/CLanguage/Debug.html
http://ftp.gnu.org/old-gnu/Manuals/gdb-5.1.1/html_node/gdb_toc.html