# 2014 February 3 # # Using GDB (debugger) on final variation of reverse program (reverse4.c) # # Added comments are preceded by a single # and a space. Lines starting # with # followed immediately by a number and no space are output from GDB. # # # Compile with the -g option to fully utilize the debugger. # Use GDB on the executable. # $ gcc -g -Wall -g -o reverse reverse4.c $ gdb reverse GNU 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/campbl4/GDB_Demo/reverse...done. # # Use break to set a breakpoint at main # (gdb) break main Breakpoint 1 at 0x4007a1: file reverse4.c, line 46. # # run the executable # (gdb) run Starting program: /homes/campbl4/GDB_Demo/reverse Breakpoint 1, main () at reverse4.c:46 46 printf("Please enter a string: "); Missing separate debuginfos, use: debuginfo-install glibc-2.17-20.fc19.x86_64 # # Use the step command to execute the next statement (line 46) # (gdb) step 47 fgets(line, MAX_STR, stdin); # # Use the step command to execute the next statement (line 47) # (gdb) step Please enter a string: hello 50 line[strlen(line)-1] = '\0'; # # print the current value of line (prior to executing line 50) # the \n character is still at position 5 # the \0 character is at position 6 # strlen(line) is 6 (h-e-l-l-o-\n) # (gdb) print line $1 = "hello\n\000\000`\343\377\377\377\177\000\000\070\001\301K3\000\000\000X\326\377\367\377\177\000\000}\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000}\b@\000\000\000\000\000\277", '\000' , "\060\b@\000\000\000\000\000\360\005@\000\000\000\000\000 \344\377\377" (gdb) step 52 rev_line = reverse(line); # # print the value of rev_line # line 52 has not been executed yet so the assignment hasn't happened # (gdb) print rev_line $2 = 0x0 (gdb) print line $3 = "hello\000\000\000`\343\377\377\377\177\000\000\070\001\301K3\000\000\000X\326\377\367\377\177\000\000}\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000\001\000\000\000\000\000\000\000}\b@\000\000\000\000\000\277", '\000' , "\060\b@\000\000\000\000\000\360\005@\000\000\000\000\000 \344\377\377" # # step at line 52 of main will step into the function call for reverse # (gdb) step reverse (s=0x7fffffffe2d0 "hello") at reverse4.c:15 15 char * result = NULL; /* the reversed string */ # # allocate enough memory to hold the size of the string s, plus one more to hold \0 # (gdb) step 20 result = (char *) malloc(strlen(s)+1); # # about to perform the copy # (gdb) step 23 strcpy(result,s); # # have performed the copy # (gdb) step 25 L = 0; # # continue to the end of the program # now the output in both line and rev_line has taken out the \n which indicated that the user was done # entering input, but was not actually counted as part of the input itself # (gdb) continue Continuing. The original string was: >hello< Backwards, that string is: >olleh< Thank you for trying our program. [Inferior 1 (process 23966) exited normally] # # quit gdb # (gdb) quit $