jrfonseca / drmingw

Postmortem debugging tools for MinGW.
GNU Lesser General Public License v2.1
273 stars 53 forks source link

catchsegv not resolving gnu_debuglink correctly #65

Closed alvinhochun closed 2 years ago

alvinhochun commented 2 years ago

Filing a new issue from https://github.com/jrfonseca/drmingw/issues/64#issuecomment-1094932631

The ImageName argument seems wrong? You can also see that https://github.com/jrfonseca/drmingw/blob/c03af4c377410745751498a2b83bb33ab16fbc91/src/mgwhelp/dwarf_pe.cpp#L249 is printing a path with the wrong directory.

Z:\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin>d:\dev\toolchain\mingw\x86_64-11.2.0-release-posix-seh-rt_v9-rev1\bin\gdb --args catchsegv -m krita.com
d:\dev\toolchain\mingw\x86_64-11.2.0-release-posix-seh-rt_v9-rev1\bin\gdborig.exe: warning: Couldn't determine a path for the index cache directory.
GNU gdb (GDB) 10.2
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
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-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from catchsegv...
(gdb) b mgwhelp.cpp:240
No source file named mgwhelp.cpp.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (mgwhelp.cpp:240) pending.
(gdb) r
Starting program: Z:\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\catchsegv.exe -v -m krita.com
[New Thread 14264.0x52b4]
[New Thread 14264.0x5908]
[New Thread 14264.0x1610]
CREATE_PROCESS PID=13776 TID=22388 lpBaseOfImage=00007FF7E4C40000

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x178, ImageName=0x5fdd4ff2d0 "\a", Base=140702671699968) at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb) c
Continuing.
[New Thread 14264.0x5d6c]
warning: MGWHELP: krita.com.debug - not found
LOAD_DLL PID=13776 TID=22388 lpBaseOfDll=00007FFAA4D50000 ntdll.dll

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x238, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ntdll.dll", Base=140714483974144)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb) c
Continuing.
LOAD_DLL PID=13776 TID=22388 lpBaseOfDll=00007FFAA3020000 kernel32.dll

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x240, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\kernel32.dll", Base=140714453368832)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb)
Continuing.
LOAD_DLL PID=13776 TID=22388 lpBaseOfDll=00007FFAA24F0000 KernelBase.dll

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x248, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\KernelBase.dll", Base=140714441637888)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb)
Continuing.
CREATE_THREAD PID=13776 TID=24588
LOAD_DLL PID=13776 TID=22388 lpBaseOfDll=00007FFAA2B80000 ucrtbase.dll

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x250, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", Base=140714448519168)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb)
Continuing.
CREATE_THREAD PID=13776 TID=19544
LOAD_DLL PID=13776 TID=24588 lpBaseOfDll=00007FFA4F440000 ucrtbase.dll

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x25c, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", Base=140713048408064)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb)
Continuing.
warning: MGWHELP: \\?\C:\Windows\System32\krita.dll.debug - not found
warning: MGWHELP: \\?\C:\Windows\System32\.debug\krita.dll.debug - not found
CREATE_THREAD PID=13776 TID=19372
LOAD_DLL PID=13776 TID=24588 lpBaseOfDll=00007FFA4EE20000 ucrtbase.dll

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x264, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", Base=140713041985536)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb)
Continuing.
warning: MGWHELP: \\?\C:\Windows\System32\libkritaimage.dll.debug - not found
warning: MGWHELP: \\?\C:\Windows\System32\.debug\libkritaimage.dll.debug - not found
LOAD_DLL PID=13776 TID=19372 lpBaseOfDll=00007FFA67C90000 ucrtbase.dll

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x26c, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", Base=140713459777536)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb)
Continuing.
warning: MGWHELP: \\?\C:\Windows\System32\libkritaresources.dll.debug - not found
warning: MGWHELP: \\?\C:\Windows\System32\.debug\libkritaresources.dll.debug - not found
LOAD_DLL PID=13776 TID=19544 lpBaseOfDll=00007FFA29D70000 ucrtbase.dll

Thread 1 hit Breakpoint 1, mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x274, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", Base=140712420507648)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
240         module->Base = Base;
(gdb) bt
#0  mgwhelp_module_create (process=0x1b0a4b68a10, hFile=0x274, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", Base=140712420507648)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:240
#1  mgwhelp_module_lookup (hProcess=hProcess@entry=0x174, hFile=hFile@entry=0x274, ImageName=ImageName@entry=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", Base=Base@entry=140712420507648)
    at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:361
#2  0x00007ffa606530d3 in MgwSymLoadModuleEx (hProcess=0x174, hFile=0x274, ImageName=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", ModuleName=<optimized out>, BaseOfDll=140712420507648,
    DllSize=262287360, Data=0x0, Flags=0) at D:\dev\krita\drmingw\drmingw\src\mgwhelp/mgwhelp.cpp:472
#3  0x00007ff612883f04 in loadModule (hProcess=0x174, hFile=hFile@entry=0x274, pszImageName=pszImageName@entry=0x5fdd4ff2d0 "\\\\?\\C:\\Windows\\System32\\ucrtbase.dll", lpBaseOfDll=0x7ffa29d70000)
    at D:/dev/krita/drmingw/drmingw/src/common/debugger.cpp:237
#4  0x00007ff612883526 in DebugMainLoop () at D:/dev/krita/drmingw/drmingw/src/common/debugger.cpp:694
#5  0x00007ff6128818d3 in main (argc=4, argv=0x1b0a4b4b810) at D:\dev\krita\drmingw\drmingw\src\catchsegv/catchsegv.cpp:411
(gdb)
jrfonseca commented 2 years ago

It shouldn't be difficult to reproduce this, but it would help me to understand where exactly are the binaries. For example, what's the krita.dll and krita.dll.debug's full path?

jrfonseca commented 2 years ago

Nevermind, I downloaded krita from https://binary-factory.kde.org/job/Krita_Nightly_Windows_Build/ so I can see all binaries.

However I can't reproduce this. mgwhelp_module_create's ImageName parameter is always correct for me.

It might be something more subtle going on here. Please double check this happens with the released catchsegv.exe or catchsegv.exe built with gcc.

jrfonseca commented 2 years ago

@alvinhochun, please rerun catchsegv with -v option, ie, catchsegv.exe -v -m krita.com.

I want to see what's logged on https://github.com/jrfonseca/drmingw/blob/0.9.5/src/common/debugger.cpp#L676-L698

My guess is that GetFileNameFromHandle is somehow returning garbage. Why is not clear, and I can't investigate it furher without reprocuding locally. You'll need to step through GetFileNameFromHandle and see why it's failing.

One potential problem is my abuse of MAX_PATH all over the place. I've been lazy, as I really should start using variable length vectors everywhere for paths, preferably Unicode. But it's time consuming grunt work..

jrfonseca commented 2 years ago

Sorry, I see you already run with -v... The relevant message is

 CREATE_PROCESS PID=13776 TID=22388 lpBaseOfImage=00007FF7E4C40000

 ...

the expectation would be

 CREATE_PROCESS PID=13776 TID=22388 lpBaseOfImage=00007FF7E4C40000 krita.com
 ...

so this is consistent with GetFileNameFromHandle returning garbage. Why I don't know...

jrfonseca commented 2 years ago

@alvinhochun , ignore all my previous rambles. I think I nailed it in https://github.com/jrfonseca/drmingw/commit/af8adbfd6387744c85311fc2634a5d6b57e1a809. Let me know if the issue persists.

alvinhochun commented 2 years ago

Ah, thanks for looking into this. I always forgot to mention that I am using a ramdisk drive created with ImDisk, which often seems to expose some edge cases in applications.

In my case it is still failing to load. A snippet of the debug output:

[19300] GetFinalPathNameByHandle failed with 0x00000001
[19300] MGWHELP: \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\krita.com.debug - not found
[19300] MGWHELP: \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\.debug\krita.com.debug - not found
[19300] GetFinalPathNameByHandle failed with 0x00000001
[19300] MGWHELP: \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\krita.dll.debug - not found
[19300] MGWHELP: \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\.debug\krita.dll.debug - not found

And a snippet of the catchsegv -v output:

CREATE_PROCESS PID=18276 TID=23856 lpBaseOfImage=00007FF7E4C40000 \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\krita.com
LOAD_DLL PID=18276 TID=23856 lpBaseOfDll=00007FFAA4D50000 \\?\C:\Windows\System32\ntdll.dll
LOAD_DLL PID=18276 TID=23856 lpBaseOfDll=00007FFAA3020000 \\?\C:\Windows\System32\kernel32.dll
LOAD_DLL PID=18276 TID=23856 lpBaseOfDll=00007FFAA24F0000 \\?\C:\Windows\System32\KernelBase.dll
CREATE_THREAD PID=18276 TID=10200
LOAD_DLL PID=18276 TID=23856 lpBaseOfDll=00007FFAA2B80000 \\?\C:\Windows\System32\ucrtbase.dll
CREATE_THREAD PID=18276 TID=11020
LOAD_DLL PID=18276 TID=10200 lpBaseOfDll=00007FFA4F440000 \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\krita.dll
CREATE_THREAD PID=18276 TID=24568
LOAD_DLL PID=18276 TID=24568 lpBaseOfDll=00007FFA64150000 \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\libkritaresources.dll
LOAD_DLL PID=18276 TID=10200 lpBaseOfDll=00007FFA4EE20000 \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\libkritaimage.dll
LOAD_DLL PID=18276 TID=24568 lpBaseOfDll=00007FFA62D70000 \Device\ImDisk20\krita-v5.1.0-prealpha-1799-g386dee827c-dirty\bin\libkritaglobal.dll

I think in the case of the path format like \Device\ImDisk20\... it needs some kind of a prefix to get win32 APIs to open it correctly? Maybe something like \\.\? IIRC the same thing also applies to a hard disk volume mounted to a folder mountpoint instead of a drive letter...

jrfonseca commented 2 years ago

I think in the case of the path format like \Device\ImDisk20\... it needs some kind of a prefix to get win32 APIs to open it correctly? Maybe something like \\.\?

Yep, GetFileNameFromHandle body needs the rest of the sample code on https://msdn.microsoft.com/en-us/library/aa366789.aspx . I thought it wasn't necessary, but it unmistakably makes a diference on this case.

I'll push a fix after cleaning up the code somewhat.

alvinhochun commented 2 years ago

Yay it works! 😄