Script started on Fri 24 Apr 2009 01:01:08 PM PDT ]0;perkins@localhost:~/cse374[?1034h[perkins@localhost cse374]$ gdb reverse [?1034hGNU gdb Fedora (6.8-23.fc9) Copyright (C) 2008 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 "i386-redhat-linux-gnu"... (gdb) run Starting program: /home/perkins/cse374/reverse Please enter a string: hope it works this time Program received signal SIGSEGV, Segmentation fault. strcpy (dest=, src=) at strcpy.c:40 40 s[off] = c; (gdb) bt #0 strcpy (dest=, src=) at strcpy.c:40 #1 0x080484c3 in reverse (s=0xbf91a4cc "hope it works this time\n") at reverse.c:19 #2 0x08048563 in main () at reverse.c:41 (gdb) lstist 35 size_t n; 36 37 do 38 { 39 c = *s++; 40 s[off] = c; 41 } 42 while (c != '\0'); 43 44 n = s - src; (gdb) print s No symbol "s" in current context. (gdb) p off No symbol "off" in current context. (gdb) up #1 0x080484c3 in reverse (s=0xbf91a4cc "hope it works this time\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 0x08048563 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 0; (gdb) down #1 0x080484c3 in reverse (s=0xbf91a4cc "hope it works this time\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) p L $1 = 10801140 (gdb) p R $2 = 0 (gdb) p s $3 = 0xbf91a4cc "hope it works this time\n" (gdb) p *s $4 = 104 'h' (gdb) p *(s+6) $5 = 116 't' (gdb) p s[4] $6 = 32 ' ' (gdb) p result $7 = 0x0 (gdb) list 24 ch = result[L]; 25 result[L] = result[R]; 26 result[R] = ch; 27 L++; R--; 28 } 29 30 return result; 31 } 32 33 (gdb) list 10 5 6 #define MAX_STR 100 /* length of longest input string */ 7 8 #include 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 */ (gdb) list 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) { 24 ch = result[L]; (gdb) quit The program is running. Exit anyway? (y or n) y ]0;perkins@localhost:~/cse374[perkins@localhost cse374]$ gcc -Wall -g reverse reverse.c[1@-[1@o[1@ ]0;perkins@localhost:~/cse374[perkins@localhost cse374]$ ./reverse Please enter a string: this time for sure! The original string was: >this time for sure! < Backwards, that string is: >< Thank you for trying our program. ]0;perkins@localhost:~/cse374[perkins@localhost cse374]$ gdb reverse [?1034hGNU gdb Fedora (6.8-23.fc9) Copyright (C) 2008 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 "i386-redhat-linux-gnu"... (gdb) break reverse Breakpoint 1 at 0x80484da: file reverse.c, line 14. (gdb) run Starting program: /home/perkins/cse374/reverse Please enter a string: help! Breakpoint 1, reverse (s=0xbffadb5c "help!\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 /* allocate result string */ (gdb) s 19 result = (char *)malloc((strlen(s)+1)*sizeof(char)); (gdb) p result $1 = 0x0 (gdb) s __libc_malloc (bytes=) at malloc.c:3544 3544 __malloc_ptr_t (*hook) (size_t, __const __malloc_ptr_t) = __malloc_hook; (gdb) finish Run till exit from #0 __libc_malloc (bytes=) at malloc.c:3544 0x080484f7 in reverse (s=0xbffadb5c "help!\n") at reverse.c:19 19 result = (char *)malloc((strlen(s)+1)*sizeof(char)); Value returned is $2 = (void *) 0x92eb008 (gdb) p result $3 = 0x0 (gdb) s 22 strcpy(result,s); (gdb) p result $4 = 0x92eb008 "" (gdb) p *result $5 = 0 '\0' (gdb) n 24 L = 0; (gdb) p result $6 = 0x92eb008 "help!\n" (gdb) n 25 R = strlen(result); (gdb) n 26 while (L < R) { (gdb) p L $7 = 0 (gdb) p R $8 = 6 (gdb) p result $9 = 0x92eb008 "help!\n" (gdb) n 27 ch = result[L]; (gdb) n 28 result[L] = result[R]; (gdb) p ch $10 = 104 'h' (gdb) n 29 result[R] = ch; (gdb) n 30 L++; R--; (gdb) n 26 while (L < R) { (gdb) p result $11 = 0x92eb008 "" (gdb) p *result $12 = 0 '\0' (gdb) p *(result+1) $13 = 101 'e' (gdb) p result[6] $14 = 104 'h' (gdb) kill Kill the program being debugged? (y or n) y (gdb) quit ]0;perkins@localhost:~/cse374[perkins@localhost cse374]$ gdb reverse./reverse gcc -Wall -g -o reverse reverse.c ]0;perkins@localhost:~/cse374[perkins@localhost cse374]$ gcc -Wall -g -o reverse reverse.c db reverse./reverse Please enter a string: ah ha! The original string was: >ah ha! < Backwards, that string is: > !ah ha< Thank you for trying our program. ]0;perkins@localhost:~/cse374[perkins@localhost cse374]$ gdb reverse [?1034hGNU gdb Fedora (6.8-23.fc9) Copyright (C) 2008 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 "i386-redhat-linux-gnu"... (gdb) run Starting program: /home/perkins/cse374/reverse Please enter a string: b^C Program received signal SIGINT, Interrupt. 0x00690416 in __kernel_vsyscall () (gdb) Quit (gdb) quit The program is running. Exit anyway? (y or n) y ]0;perkins@localhost:~/cse374[perkins@localhost cse374]$ gdb reverse [?1034hGNU gdb Fedora (6.8-23.fc9) Copyright (C) 2008 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 "i386-redhat-linux-gnu"... (gdb) break reverse Breakpoint 1 at 0x80484da: file reverse.c, line 14. (gdb) run Starting program: /home/perkins/cse374/reverse Please enter a string: hello Breakpoint 1, reverse (s=0xbfd0d8bc "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 /* allocate result string */ (gdb) print s $1 = 0xbfd0d8bc "hello\n" (gdb) up #1 0x080485af in main () at reverse.c:44 44 rev_line = reverse(line); (gdb) print $2 = 0xbfd0d8bc "hello\n" (gdb) list 39 char line[MAX_STR]; /* original input line */ 40 char * rev_line; /* backwards copy from reverse function */ 41 42 printf("Please enter a string: "); 43 fgets(line, MAX_STR, stdin); 44 rev_line = reverse(line); 45 printf("The original string was: >%s<\n", line); 46 printf("Backwards, that string is: >%s<\n", rev_line); 47 printf("Thank you for trying our program.\n"); 48 return 0; (gdb) quit The program is running. Exit anyway? (y or n) y ]0;perkins@localhost:~/cse374[perkins@localhost cse374]$ exit exit Script done on Fri 24 Apr 2009 01:20:31 PM PDT