如何使用WSL中使用GDB进行Windows的可执行文件?

发布于 2025-01-18 03:15:14 字数 4306 浏览 0 评论 0原文

坦率地说,我什至不确定这是否是 GDB 可以做的事情,但到目前为止我所做的大量搜索都没有给我一个“是”或“否”的答案。

当我尝试使用为 Linux 构建并在 WSL 中打开的 GDB 安装来调试应用程序时,它无法在程序中的任何位置插入断点,声称它无法访问该地址处的内存。如果我在 Windows 上使用为 Windows 构建的 GDB 执行此操作,则不会发生此错误(在您问为什么我不只使用 Windows 构建之前,这是因为我遇到了其他问题。我可以打开这也是一个问题)

我也收到了 GDB 的内部错误,但不幸的是,我现在似乎无法重新创建它。

我尝试过重建 GDB,以及切换到另一个版本的 GDB(与我的 Windows 版本相同),

我正在使用 Ubuntu 20.04 和 GDB 10.2 的 WSL 安装,配置如下:

(gdb) show configuration
This GDB was configured as follows:
   configure --host=x86_64-pc-linux-gnu --target=x86_64-pc-linux-gnu
             --with-auto-load-dir=$debugdir:$datadir/auto-load
             --with-auto-load-safe-path=$debugdir:$datadir/auto-load
             --without-expat
             --with-gdb-datadir=/usr/local/share/gdb (relocatable)
             --with-jit-reader-dir=/usr/local/lib/gdb (relocatable)
             --without-libunwind-ia64
             --without-lzma
             --without-babeltrace
             --without-intel-pt
             --without-mpfr
             --without-xxhash
             --without-python
             --without-python-libdir
             --without-debuginfod
             --without-guile
             --disable-source-highlight
             --with-separate-debug-dir=/usr/local/lib/debug (relocatable)

要查看这是否是一个问题对于我正在调试的特定程序,我在 NASM 中制作了一个非常小的程序(我的原始项目也在 NASM 中)并将其编译如下:

nasm -f win32 -gcv8 Test.asm
gcc -m32 -g Test.obj -o Test.exe

源程序集非常简单。它只是用字符串和整数调用printf

; Test.asm
    global      _main
    
    extern      _printf
    
    section     .data
fmt:    db      "%s, %d", 0x0
string: db      "Testing...", 0x0

    section     .bss
num:    resd        1

    section     .text
_main:
    mov dword   [num], 28
    
    push dword  [num]
    push        string
    push        fmt
    call        _printf
    add     esp, 12
    
    ret

当尝试在 WSL 中使用 GDB 进行调试时,这是我得到的输出:

(gdb) file Test.exe
Reading symbols from Test.exe...
(gdb) set architecture i386:x86-64
The target architecture is set to "i386:x86-64".
(gdb) start
Temporary breakpoint 1 at 0x401520
Starting program: /mnt/c/NASM/Test.exe
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x401520

编辑:在进一步研究之后,我发现了一些看起来很重要的东西。 GDB仅在程序运行时无法访问内存和设置断点。在启动程序之前,我可以自由地设置断点和反汇编。

(gdb) disas main
Dump of assembler code for function main:
   0x00401520 <+0>:     mov    DWORD PTR ds:0x405028,0x1c
   0x0040152a <+10>:    push   DWORD PTR ds:0x405028
   0x00401530 <+16>:    push   0x40300b
   0x00401535 <+21>:    push   0x403004
   0x0040153a <+26>:    call   0x40249c <printf>
   0x0040153f <+31>:    add    esp,0xc
   0x00401542 <+34>:    ret
   0x00401543 <+35>:    xchg   ax,ax
   0x00401545 <+37>:    xchg   ax,ax
   0x00401547 <+39>:    xchg   ax,ax
   0x00401549 <+41>:    xchg   ax,ax
   0x0040154b <+43>:    xchg   ax,ax
   0x0040154d <+45>:    xchg   ax,ax
   0x0040154f <+47>:    nop
End of assembler dump.
(gdb) b *main+26
Breakpoint 1 at 0x40153a
(gdb) run
Starting program: /mnt/c/NASM/Test.exe
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x40153a
(gdb) disas main
Dump of assembler code for function main:
   0x00401520 <+0>:     Cannot access memory at address 0

编辑2: 我不知道这些信息有多大用处,但我确实找到了一种始终导致 GDB 内部错误的方法。开始执行程序,然后将体系结构设置为“自动”,每次尝试都会导致内部错误。

(gdb) file Test.exe
Reading symbols from Test.exe...
(gdb) start
Temporary breakpoint 1 at 0x401520
Starting program: /mnt/c/NASM/Test.exe
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
warning: Architecture rejected target-supplied description
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x401520

(gdb) set architecture auto
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
/mnt/c/Users/Joshua/gdb-10.2/gdb/arch-utils.c:503: internal-error: could not select an architecture automatically
A problem internal to GDB has been detected,
further debugging may prove unreliable.

如果这个问题的答案真的像“为 Linux 构建的 GDB 无法调试为 Windows 构建的应用程序”一样简单……我会非常难过,而且也很恼火,因为我无法在任何地方找到该信息。

I'm frankly not even sure if this is a thing GDB can do, but no amount of searching I've done so far has given me a 'yes' or 'no'.

When I attempt to debug an application using a GDB installation built for Linux and opened in WSL, it is unable to insert a breakpoint anywhere in the program, claiming it can not access the memory at that address. If I do this from Windows with a GDB built for Windows, this error does not happen (and before you ask why I don't just use the Windows build, it's because I'm having other miscellaneous issues with that one. I may open a question for that as well)

I've got an internal error from GDB as well, but unfortunately, I can't seem to recreate it right now.

I've tried rebuilding GDB, as well as switching to another version of GDB (the same as my Windows build)

I'm using a WSL installation of Ubuntu 20.04 and GDB 10.2, configured as follows:

(gdb) show configuration
This GDB was configured as follows:
   configure --host=x86_64-pc-linux-gnu --target=x86_64-pc-linux-gnu
             --with-auto-load-dir=$debugdir:$datadir/auto-load
             --with-auto-load-safe-path=$debugdir:$datadir/auto-load
             --without-expat
             --with-gdb-datadir=/usr/local/share/gdb (relocatable)
             --with-jit-reader-dir=/usr/local/lib/gdb (relocatable)
             --without-libunwind-ia64
             --without-lzma
             --without-babeltrace
             --without-intel-pt
             --without-mpfr
             --without-xxhash
             --without-python
             --without-python-libdir
             --without-debuginfod
             --without-guile
             --disable-source-highlight
             --with-separate-debug-dir=/usr/local/lib/debug (relocatable)

To see if this was an issue with the particular program I was debugging, I made a very minimal program in NASM (my original project was also in NASM) and compiled it as follows:

nasm -f win32 -gcv8 Test.asm
gcc -m32 -g Test.obj -o Test.exe

The source assembly is very simple. It just calls printf with a string and integer.

; Test.asm
    global      _main
    
    extern      _printf
    
    section     .data
fmt:    db      "%s, %d", 0x0
string: db      "Testing...", 0x0

    section     .bss
num:    resd        1

    section     .text
_main:
    mov dword   [num], 28
    
    push dword  [num]
    push        string
    push        fmt
    call        _printf
    add     esp, 12
    
    ret

When attempting to debug this with GDB in WSL, this is the output I get:

(gdb) file Test.exe
Reading symbols from Test.exe...
(gdb) set architecture i386:x86-64
The target architecture is set to "i386:x86-64".
(gdb) start
Temporary breakpoint 1 at 0x401520
Starting program: /mnt/c/NASM/Test.exe
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x401520

EDIT: After poking at it some more, I discovered something that seems important. GDB is only unable to access the memory and place breakpoints when the program is running. Before I've started the program, I can place breakpoints and disassemble freely.

(gdb) disas main
Dump of assembler code for function main:
   0x00401520 <+0>:     mov    DWORD PTR ds:0x405028,0x1c
   0x0040152a <+10>:    push   DWORD PTR ds:0x405028
   0x00401530 <+16>:    push   0x40300b
   0x00401535 <+21>:    push   0x403004
   0x0040153a <+26>:    call   0x40249c <printf>
   0x0040153f <+31>:    add    esp,0xc
   0x00401542 <+34>:    ret
   0x00401543 <+35>:    xchg   ax,ax
   0x00401545 <+37>:    xchg   ax,ax
   0x00401547 <+39>:    xchg   ax,ax
   0x00401549 <+41>:    xchg   ax,ax
   0x0040154b <+43>:    xchg   ax,ax
   0x0040154d <+45>:    xchg   ax,ax
   0x0040154f <+47>:    nop
End of assembler dump.
(gdb) b *main+26
Breakpoint 1 at 0x40153a
(gdb) run
Starting program: /mnt/c/NASM/Test.exe
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x40153a
(gdb) disas main
Dump of assembler code for function main:
   0x00401520 <+0>:     Cannot access memory at address 0

EDIT 2:
I don't know how useful this information might be, but I did find a method that consistently causes an internal error for GDB. Starting execution of the program, then setting the architecture to auto causes an internal error every time I've tried it.

(gdb) file Test.exe
Reading symbols from Test.exe...
(gdb) start
Temporary breakpoint 1 at 0x401520
Starting program: /mnt/c/NASM/Test.exe
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
warning: Architecture rejected target-supplied description
Warning:
Cannot insert breakpoint 1.
Cannot access memory at address 0x401520

(gdb) set architecture auto
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
/mnt/c/Users/Joshua/gdb-10.2/gdb/arch-utils.c:503: internal-error: could not select an architecture automatically
A problem internal to GDB has been detected,
further debugging may prove unreliable.

If the answer to this really is as simple as "GDB built for Linux can't debug applications built for Windows"... I'll be very sad, and also quite annoyed that I was unable to find that info anywhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文