Script started on Mon 23 Oct 2017 03:27:57 PM PDT $ emacs reverse.c & [1] 1739 $ gcc -Wall -g -std=c11 -o reverse reverse.c c $ ./reverse Please enter a string: hello Segmentation fault (core dumped) $ gdb ./reverse GNU gdb (GDB) Red Hat Enterprise Linux 7.11-67.el7 Copyright (C) 2016 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". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./reverse...done. (gdb) run Starting program: /home/perkins/cse374/11-gdb-files/reverse Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7.x86_64 Please enter a string: hello Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7aac785 in __strcpy_sse2_unaligned () from /lib64/libc.so.6 (gdb) bt #0 0x00007ffff7aac785 in __strcpy_sse2_unaligned () from /lib64/libc.so.6 #1 0x00000000004006bd in reverse (s=0x7fffffffd940 "hello\n") at reverse.c:19 #2 0x000000000040076b in main () at reverse.c:41 (gdb) up #1 0x00000000004006bd in reverse (s=0x7fffffffd940 "hello\n") at reverse.c:19 19 strcpy(result, s); (gdb) list 14 char * result = NULL; /* the reversed string */ 15 int L, R; 16 char ch; 17 18 /* copy original string then reverse and return the copy */ 19 strcpy(result, s); 20 21 L = 0; 22 R = strlen(result); 23 while (L < R) { (gdb) up #2 0x000000000040076b in main () at reverse.c:41 41 rev_line = reverse(line); (gdb) list 36 char line[MAX_STR]; /* original input line */ 37 char * rev_line; /* backwards copy from reverse function */ 38 39 printf("Please enter a string: "); 40 fgets(line, MAX_STR, stdin); 41 rev_line = reverse(line); 42 printf("The original string was: >%s<\n", line); 43 printf("Backwards, that string is: >%s<\n", rev_line); 44 printf("Thank you for trying our program.\n"); 45 return EXIT_SUCCESS; (gdb) p line $1 = "hello\n\000\000\000\000\000\000\000\000\000\000v\000\000\000\000\000\000\000\250\201\242\367\377\177\000\000\000\000\000\000\000\000\000\000\307\023\337\367\377\177\000\000\001\000\000\000\000\000\000\000\375\a@\000\000\000\000\000////////\000\000\000\000\000\000\000\000\260\a@\000\000\000\000\000\240\005@\000\000\000\000\000\220\332\377\377" (gdb) down #1 0x00000000004006bd in reverse (s=0x7fffffffd940 "hello\n") at reverse.c:19 19 strcpy(result, s); (gdb) p s $2 = 0x7fffffffd940 "hello\n" (gdb) p result $3 = 0x0 (gdb) p L $4 = 32767 (gdb) p R $5 = -9808 (gdb) quit A debugging session is active. Inferior 1 [process 1848] will be killed. Quit anyway? (y or n) y $ gdb ./reverse GNU gdb (GDB) Red Hat Enterprise Linux 7.11-67.el7 Copyright (C) 2016 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". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./reverse...done. (gdb) list 25 result[L] = result[R]; 26 result[R] = ch; 27 L++; R--; 28 } 29 30 return result; 31 } 32 33 34 /* Ask the user for a string, then print it forwards and backwards. */ (gdb) break reverse Breakpoint 1 at 0x4006a2: file reverse.c, line 14. (gdb) run Starting program: /home/perkins/cse374/11-gdb-files/reverse Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7.x86_64 Please enter a string: hello Breakpoint 1, reverse (s=0x7fffffffd940 "hello\n") at reverse.c:14 14 char * result = NULL; /* the reversed string */ (gdb) list 9 #include 10 #include 11 12 /* Return a new string with the contents of s backwards */ 13 char * reverse(char * s) { 14 char * result = NULL; /* the reversed string */ 15 int L, R; 16 char ch; 17 18 /* copy original string then reverse and return the copy */ (gdb) p s $1 = 0x7fffffffd940 "hello\n" (gdb) s 19 strcpy(result, s); (gdb) p s $2 = 0x7fffffffd940 "hello\n" (gdb) p result $3 = 0x0 (gdb) s Program received signal SIGSEGV, Segmentation fault. 0x00007ffff7aac785 in __strcpy_sse2_unaligned () from /lib64/libc.so.6 (gdb) quit A debugging session is active. Inferior 1 [process 1979] will be killed. Quit anyway? (y or n) y $ gcc -Wall -g -std=c11 -o reverse reverse.c $ gdb ./reverse GNU gdb (GDB) Red Hat Enterprise Linux 7.11-67.el7 Copyright (C) 2016 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". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from ./reverse...done. (gdb) break reverse Breakpoint 1 at 0x4006f2: file reverse.c, line 14. (gdb) run Starting program: /home/perkins/cse374/11-gdb-files/reverse Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7.x86_64 Please enter a string: hello Breakpoint 1, reverse (s=0x7fffffffd940 "hello\n") at reverse.c:14 14 char * result = NULL; /* the reversed string */ (gdb) bt #0 reverse (s=0x7fffffffd940 "hello\n") at reverse.c:14 #1 0x00000000004007de in main () at reverse.c:45 (gdb) p s $1 = 0x7fffffffd940 "hello\n" (gdb) p result $2 = 0x0 (gdb) s 19 int strsize = strlen(s); (gdb) s 20 result = (char *)malloc(strsize+1); (gdb) p strsize $3 = 6 (gdb) p result $4 = 0x0 (gdb) s 23 strcpy(result, s); (gdb) p result $5 = 0x602010 "" (gdb) s 25 L = 0; (gdb) p result $6 = 0x602010 "hello\n" (gdb) continue Continuing. The original string was: >hello < Backwards, that string is: >< Thank you for trying our program. [Inferior 1 (process 2004) exited normally] (gdb) quit $ !gcc gcc -Wall -g -std=c11 -o reverse reverse.c $ ./reverse Please enter a string: does it work now? The original string was: >does it work now?< Backwards, that string is: >?won krow ti seod< Thank you for trying our program. $ exit exit Script done on Mon 23 Oct 2017 04:22:14 PM PDT