gcc -g -std=c11 -o reverse reverse1.c Script started on Sun 01 Feb 2015 09:57:49 PM PST [?1034hbash-4.2$ gdb reverse [?1034hGNU gdb (GDB) Fedora 7.6.1-46.fc19 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later 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-redhat-linux-gnu". For bug reporting instructions, please see: ... Reading symbols from /homes/bdmyers/cse374_demos/11/reverse...done. # When we run our program we get a segmentation fault, # so we will run in gdb and start interacting with the # program where the crash occurs. (gdb) run Starting program: /homes/bdmyers/cse374_demos/11/reverse Please enter a string: 1234 Program received signal SIGSEGV, Segmentation fault. 0x000000334bc861c0 in __strcpy_sse2 () from /lib64/libc.so.6 Missing separate debuginfos, use: debuginfo-install glibc-2.17-20.fc19.x86_64 # Now gdb has halted at the point of the segmentation fault. # We see that it occurred in a standard library function # called __strcpy_sse2. To find out how we got there, we # ask for a backtrace. GDB computes this backtrace by looking # at the stack (gdb) bt #0 0x000000334bc861c0 in __strcpy_sse2 () from /lib64/libc.so.6 #1 0x00000000004006b7 in reverse (s=0x7fffffffe440 "1234\n") at reverse1.c:24 #2 0x0000000000400765 in main () at reverse1.c:49 # The backtrace has three frames, one for each function call # that has not yet returned at the point of the crash. # We suspect the root cause of the segfault is in our reverse # function, rather than the standard library. So we zoom in # on frame #1. (gdb) f 1 #1 0x00000000004006b7 in reverse (s=0x7fffffffe440 "1234\n") at reverse1.c:24 24 strcpy(result,s); # This takes us to the point where strcpy() was called. # Lets see what the value of the arguments we passed # to the function were. (gdb) info locals result = 0x0 L = 32767 R = -6992 ch = 0 '\000' (gdb) print s $1 = 0x7fffffffe440 "1234\n" # We see that as expected s is a pointer to the # input string. However, result = 0x0 is a red flag. # 0x0 is never a valid address and strcpy expects # a valid address. # So our bug is we didn't setup a pointee and point # result to it. This bug is fixed in reverse2.c (gdb) quit A debugging session is active. Inferior 1 [process 25796] will be killed. Quit anyway? (y or n) y bash-4.2$ exit exit Script done on Sun 01 Feb 2015 10:00:47 PM PST