This question already has answers here:
Can't step into string.h function with GDB
(3 answers)
Closed 6 years ago.
Please look at the command line below. I set two breakpoints: one on strcpy and the other on printf. Why did it skip breakpoint 1?
root#ninja:~/Desktop/Programs# gcc -g -o exp exp.c
root#ninja:~/Desktop/Programs# gdb -q exp
Reading symbols from /root/Desktop/Programs/exp...done.
(gdb) list
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char str_a[20];
6
7 strcpy(str_a, "Hello world!\n");
8 printf(str_a);
9 }
(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (strcpy) pending.
(gdb) break printf
Breakpoint 2 at 0x8048300
(gdb) run
Starting program: /root/Desktop/Programs/exp
Breakpoint 2, 0xb7eabf54 in printf () from /lib/i386-linux-gnu/i686/cmov/libc.so.6
(gdb) i r eip
eip 0xb7eabf54 0xb7eabf54 <printf+4>
(gdb) cont
Continuing.
Hello world!
[Inferior 1 (process 3726) exited with code 015]
The first breakpoint is pending.
(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (strcpy) pending. <<here
try: (gdb) break 7
7 being the line number.
Related
I'm trying to debug a program in gdb with a functions source code but I always need to set the EXACT path of the C file for that function:
frinto#kali:~/Documents/theclang/programs/helloworld$ gdb -q char_array
Reading symbols from char_array...done.
(gdb) list
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char str_a[20];
6
7 strcpy(str_a, "Hello, world!\n");
8 printf(str_a);
9 }
(gdb) break 6
Breakpoint 1 at 0x11c6: file char_array.c, line 6.
(gdb) break strcpy
Breakpoint 2 at 0x1040
(gdb) break 8
Breakpoint 3 at 0x11dc: file char_array.c, line 8.
(gdb) dir ~/Documents/glibc-2.28/sysdeps/i386/i686/multiarch
Source directories searched: /home/frinto/Documents/glibc-2.28/sysdeps/i386/i686/multiarch:$cdir:$cwd
(gdb) cont
The program is not being run.
(gdb) run
Starting program: /home/frinto/Documents/theclang/programs/helloworld/char_array
Breakpoint 1, main () at char_array.c:7
7 strcpy(str_a, "Hello, world!\n");
(gdb) cont
Continuing.
Breakpoint 2, strcpy_ifunc () at ../sysdeps/i386/i686/multiarch/strcpy.c:29
29 libc_ifunc_redirected (__redirect_strcpy, strcpy, IFUNC_SELECTOR ());
(gdb) cont
Continuing.
How can I tell gdb to recursively look for strcpy.c in ~/Documents/glibc-2.28 without having to set the exact path every single time?
frinto#kali:~/Documents/theclang/programs/helloworld$ gdb -q char_array
Reading symbols from char_array...done.
(gdb) list
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char str_a[20];
6
7 strcpy(str_a, "Hello, world!\n");
8 printf(str_a);
9 }
(gdb) break 6
Breakpoint 1 at 0x11c6: file char_array.c, line 6.
(gdb) break strcpy
Breakpoint 2 at 0x1040
(gdb) break 8
Breakpoint 3 at 0x11dc: file char_array.c, line 8.
(gdb) dir /home/frinto/Documents/glibc-2.28
Source directories searched: /home/frinto/Documents/glibc-2.28:$cdir:$cwd
(gdb) run
Starting program: /home/frinto/Documents/theclang/programs/helloworld/char_array
Breakpoint 1, main () at char_array.c:7
7 strcpy(str_a, "Hello, world!\n");
(gdb) cont
Continuing.
Breakpoint 2, strcpy_ifunc () at ../sysdeps/i386/i686/multiarch/strcpy.c:29
29 ../sysdeps/i386/i686/multiarch/strcpy.c: No such file or directory.
(gdb) cont
Continuing.
Breakpoint 3, main () at char_array.c:8
8 printf(str_a);
(gdb)
I tried just setting the glibc directory but that doesn't work...
It is common to build GLIBC this way:
cd glibc-2.28 && mkdir build && cd build && ../configure --prefix=/usr && make
This results in source paths similar to ../sysdeps/i386/i686/multiarch/strcpy.c (they are relative to the build directory).
So what you want is:
cd ~/Documents/glibc-2.28 && mkdir build; cd -
gdb -ex 'dir ~/Documents/glibc-2.28/build' -q char_array
I'm doing a bit of reading about gdb, and I'm having trouble getting gdb (I'm running 7.11.1) to debug a function from a library.
The sample code used to learn about the debugger is quite simple:
#include <stdio.h>
#include <string.h>
int main() {
char str_a[20];
strcpy(str_a, "Hello, world!\n");
printf(str_a);
}
I compiled it with debugging symbols enabled, fired up GDB, and set some breakpoints:
(gdb) list
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char str_a[20];
6
7 strcpy(str_a, "Hello, world!\n");
8 printf(str_a);
9 }
(gdb) break 7
Breakpoint 1 at 0x4005ad: file char_array2.c, line 7.
(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 2 (strcpy) pending.
(gdb) break 8
Breakpoint 3 at 0x4005cf: file char_array2.c, line 8.
(gdb) run
Starting program: /home/david/hacking_the_art_of_exploitation/Chapter_2/char_array2
Breakpoint 1, main () at char_array2.c:7
7 strcpy(str_a, "Hello, world!\n");
(gdb) continue
Continuing.
Breakpoint 3, main () at char_array2.c:8
8 printf(str_a);
(gdb) continue
Continuing.
Hello, world!
[Inferior 1 (process 7061) exited normally]
As you can see, the debugger never descends into the strcpy function.
I've tried adding set stop-on-solib-events 1 to my .gdbinit. This leads to different but still undesirable results:
(gdb) run
Starting program: /home/david/hacking_the_art_of_exploitation/Chapter_2/char_array2
Stopped due to shared library event (no libraries added or removed)
I'm at a bit of a loss here. Thanks in advance for any help.
I believe you need the libc-dbg package and the libc source package for debugging a libc function. On Ubuntu you can install it via
sudo apt-get install libc6-dbg
mkdir ~/libc ; cd ~/libc
apt-get source libc6
I was debugging my program, then the last line happened, how can I fix it? I used the -fno-builtin to have a look at the strcpy() but it shows that the __strcpy_sse2_unaligned is getting called.
root#19:~/booksrc# gcc -fno-builtin -g char_array2.c
root#19:~/booksrc# gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) list
1 #include <stdio.h>
2 #include <string.h>
3
4 int main() {
5 char str_a[20];
6
7 strcpy(str_a, "Hello World!\n");
8 printf(str_a);
9 }
(gdb) break 6
Breakpoint 1 at 0x708: file char_array2.c, line 6.
(gdb) break strcpy
Breakpoint 2 at 0x5a0
(gdb) break 8
Breakpoint 3 at 0x71b: file char_array2.c, line 8.
(gdb) run
Starting program: /root/booksrc/a.out
Breakpoint 1, main () at char_array2.c:7
7 strcpy(str_a, "Hello World!\n");
(gdb) cont
Continuing.
Breakpoint 2, __strcpy_sse2_unaligned ()
at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:47
47 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.
__strcpy_sse2_unaligned is the implementation of strcpy which is used on your machine. glibc automatically chooses an optimized implementation based on CPU characteristics, using an IFUNC resolver.
This does not have to do anything with GCC and GCC built-ins. GCC emits a call to strcpy. It is just that glibc happens to call the function which it __strcpy_sse2_unaligned.
I wish to jump to a line, either in same context, or outside the function. I've got a "test.c"
1
2 #include<stdio.h>
3 void fa(int c)
4 {
5 printf("begin\n");/*I break here*/
6 printf("%d\n",c); /*I wish to jump 1 line here*/
7 }
8 void fb(){}
9
10 int main(){
11 int b=1;
12 int i=2;
13 fa('a');
14 fb(); /*I also want to jump here*/
15 return 0;
16 }
Then compiled it with gcc test.c -g and run it using gdb a.out.
gdb a.out
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
...
(gdb) b 5
Breakpoint 1 at 0x400571: file test.c, line 5.
(gdb) r
Starting program: /home/Troskyvs/a.out
Breakpoint 1, fa (c=97) at test.c:5
5 printf("begin\n");
(gdb) j 6
Continuing at 0x40057b.
97 # This line is odd!
[Inferior 1 (process 6583) exited normally]
(gdb) f
No stack. # Why it doesn't print line 6 source code
(gdb) j 14
The program is not being run.
# What happen here?
I also tried "jump +1" and "jump +14". Same result, don't work.
How "jump" could work in my way?
Well, it's doing what you asked it to do. It
jumped to line 6
executed the code printf("%d\n",c);, printed the value (97). See here to know why the value is 97
continued the execution, finished it. Proof
[Inferior 1 (process 6583) exited normally]
So, your program is over already. It's no longer running.
FWIW, if you want to stop/interrupt the normal the execution again, you have to set more that one break point after the jump destination to make it wait.
This question already has answers here:
gdb prints wrong values when modifying arguments
(3 answers)
Closed 6 years ago.
I have simple program:
#include <stdio.h>
void func(int i) {
i = 1;
printf("%d\n", i);
}
int main(int argc, char *argv[]){
func(0);
return 0;
}
and now:
gcc test.c -g -o test
gdb test
(gdb) b main
Breakpoint 1 at 0x400543: file test.c, line 9.
(gdb) run
Starting program: /tmp/test
Breakpoint 1, main (argc=1, argv=0x7fffffffe458) at test.c:9
9 func(0);
(gdb) s
func (i=0) at test.c:4
4 i =1;
(gdb) p i
$1 = 0
(gdb) n
5 printf("%d\n", i);
(gdb) p i
$2 = 0
(gdb)
Program works fine, shows "1", but why gdb shows me "0" value?
Debian wheezy.
I observed that on gcc-4.7, gcc-4.6.
On gcc-4.4 all is ok.
This is a bug that is fixed if you compile with -fvar-tracking. Your question is a tighter version of this SO question, which references a bug report on GCC 4.8.0 suggesting the above compile flag.