GDB
The most standard debugging tool on UNIX is the GNU Debugger gdb. Its main functionnality is to display the piece of code which procuced a crash. To do it, compile your code with the -g option, so that debugging information will be added to the executable. This information is mainly a correspondance between the machine langage instructions and locations in the source. Then, execute the program from gdb.
For instance
int main(int argc, char **argv) {
int size = 100;
int a[size];
for(int i = 0; i < 100 * size; i++) a[i] = i;
}
> g++ -o bang -g bang.cc
> gdb ./bang
GNU gdb 6.1-debian
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-linux"...\
Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) run
Starting program: /tmp/bang
Program received signal SIGSEGV, Segmentation fault.
0x080483e3 in main (argc=1, argv=0xbffff8e4) at bang.cc:4
4 for(int i = 0; i < 100 * size; i++) a[i] = i;
(gdb) l
1 int main(int argc, char **argv) {
2 int size = 100;
3 int a[size];
4 for(int i = 0; i < 100 * size; i++) a[i] = i;
5 }
Note that gdb is a very primitive tool unable to spot tricky errors such as
memory leaks or forbidden access which do not crash the program.