09.19.txt Lecture Notes 09/19/05 CISC105 05F (0) Segmentation faults Finding the cause of a seg fault: > ./segFault1 enter value for x:5 Segmentation fault > step 1) type the following at the command prompt: > unlimit core > If this worked, then when you run your program again, you'll get the following: > ./segFault1 enter value for x:5 Segmentation fault > unlimit core > ./segFault1 enter value for x:5 Segmentation fault (core dumped) > ls #09.19.txt# segFault1.c segFault3 09.19.txt segFault1~ segFault3.c 09.19.txt~ segFault2 segFault3.c~ core segFault2.c segFault1 segFault2.c~ > Note that after we do "unlimit core", when we have a segfault, we get a "core" file... The core file allows us to do a kind of "autopsy" of our dead program and figure out, just like a medical examiner, or a CSI, why and how the program died. Because these core files usually just take up space, by default, they don't get created. To turn on the creation of core files, we do "unlimit core". Step 2: We have to prepare the program to be "autopsy-friendly". We do that by compiling in a special way. We use the -g flag when we compile. The -g flag stands for debuG. > cc -g segFault1.c -o segFault1 > ./segFault1 enter value for x:5 Segmentation fault (core dumped) > Step 3: Now the interesting part starts. We use dbx with the core file to pinpoint the exact location of the error: > ./segFault1 enter value for x:5 Segmentation fault (core dumped) > dbx segFault1 core For information about new features see `help changes' To remove this message, put `dbxenv suppress_startup_message 7.3' in your .dbxrc Reading segFault1 core file header read successfully Reading ld.so.1 Reading libc.so.1 Reading libdl.so.1 Reading libc_psr.so.1 program terminated by signal SEGV (no mapping at the fault address) 0xff30f3d8: number+0x0804: st %l5, [%o0] Current function is main 22 scanf("%d",x); /* should be &x */ (dbx) This tells us that the error occured on line 22, where there is a scanf without an & on the variable. To get out of dbx, we use "quit". 22 scanf("%d",x); /* should be &x */ (dbx) quit > Then we would use emacs to fix the program. Some things to know: (a) "unlimit core" ... once you type it, it is in effect until you log off. If you log off and log back on, you have to retype it. It only affects the current "shell" (i.e. the window you are typing in.) (b) With "cc" compiler, you use dbx. With "gcc" compiler, you use gdb. Regarding core files, they work the same way. > gcc -g segFault1.c -o segFault1 > ./segFault1 enter value for x:5 Bus error (core dumped) > Note that this example shows a "bus error".... a bus error is essentially the same thing as a seg fault for our purposes. [Technical difference: bus error is reference to an address that is not a multiple of 4, where a segmentation fault is a reference to an address you have no authority to look at.] In gdb, you might need to use the "bt" command (backtrace) to see which line of code caused the problem. > gdb segFault1 core GNU gdb 5.0 Copyright 2000 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 "sparc-sun-solaris2.9"... Core was generated by `./segFault1'. Program terminated with signal 10, Bus Error. Reading symbols from /usr/lib/libc.so.1...done. Loaded symbols for /usr/lib/libc.so.1 Reading symbols from /usr/lib/libdl.so.1...done. Loaded symbols for /usr/lib/libdl.so.1 Reading symbols from /usr/platform/SUNW,Sun-Fire/lib/libc_psr.so.1...done. Loaded symbols for /usr/platform/SUNW,Sun-Fire/lib/libc_psr.so.1 #0 0xff30f3d8 in number () from /usr/lib/libc.so.1 (gdb) bt #0 0xff30f3d8 in number () from /usr/lib/libc.so.1 #1 0xff30ea24 in __doscan_u () from /usr/lib/libc.so.1 #2 0xff30e100 in _doscan () from /usr/lib/libc.so.1 #3 0xff314338 in vscanf () from /usr/lib/libc.so.1 #4 0xff313130 in scanf () from /usr/lib/libc.so.1 #5 0x10628 in main () at segFault1.c:22 (gdb) To leave gdb, same thing: quit Next time: 9/21/05... (2) Making a web page with simple HTML Need a volunteer for extra credit (upgrade your lab to 100% good for any lab this semester... can be redeemed at anytime this semester...) Chris Bednarski (bedrock)