简体   繁体   中英

How to set a source files directory recursively in gdb?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM