GCC can't do that but GDB (a debugger) sure can. Compile you program using the -g switch, like this:
gcc program.c -g
Then use gdb:
$ gdb ./a.out
(gdb) run
<segfault happens here>
(gdb) backtrace
<offending code is shown here>
Here is a nice tutorial to get you started with GDB.
Where the segfault occurs is generally only a clue as to where "the mistake which causes" it is in the code. The given location is not necessarily where the problem resides.
Answer from nc3b on Stack OverflowGCC can't do that but GDB (a debugger) sure can. Compile you program using the -g switch, like this:
gcc program.c -g
Then use gdb:
$ gdb ./a.out
(gdb) run
<segfault happens here>
(gdb) backtrace
<offending code is shown here>
Here is a nice tutorial to get you started with GDB.
Where the segfault occurs is generally only a clue as to where "the mistake which causes" it is in the code. The given location is not necessarily where the problem resides.
Also, you can give valgrind a try: if you install valgrind and run
valgrind --leak-check=full <program>
then it will run your program and display stack traces for any segfaults, as well as any invalid memory reads or writes and memory leaks. It's really quite useful.
Videos
Here is a sample program that would definitely cause segmentation fault :
include
int main() {
int *pVal = NULL;
printf("ptr value is : %d", *pVal);
return 0;
}
You need to compile in debug mode in order to add additional debug information in executable file:
gcc -g segFault.c
Then all you need is to run gdb and give that the executable file path (i.e a.out in this case). Then by just running it you can see that gdb highlights the line causing segmentation fault.
~/Dropbox/cprog/demos : $ gdb a.out
GNU gdb (GDB) 7.12
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin15.6.0".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from a.out...Reading symbols from /Users/rohankumar/Dropbox/cprog/demos/a.out.dSYM/Contents/Resources/DWARF/a.out...done.
done.
(gdb) run
Starting program: /Users/rohankumar/Dropbox/cprog/demos/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0000000100000f62 in main () at segFault.c:6
6 printf("ptr value is : %d", *pVal);
You can also print the values and see stacktrace of program. You can read more about gdb here .
Happy coding!
Run gdb and run your program from gdb, then use backtrace. you 'll get stack frame, which you can walk through by fram command and use print to check variables' values. Check gdb tips\docs over internet. You can use gdb to load already existing core file, generated by crashed program, to find spot where problem occured. Loaded core file is equal to state at crash point, you just usebacktrace.
Answer on your question was here: Determine the line of C code that causes a segmentation fault?