1 year ago

#380837

test-img

lexical

Visual Studio Code c++ gdb debugging a DLL when attaching to a non-debug-enabled host process

I'm creating a windows DLL using gcc that is loaded via LoadLibrary in a third party program (I don't have access to it's code). When I try to debug my DLL in Visual Studio Code using gdb with an attach request, I'm unable to get any breakpoints to hit. It works fine if I use a launch request. Loading the host program takes enough time as it is, and it takes even longer when gdb and VSCode are launching it directly.

I created a test EXE in gcc/VSC and compiled it with and without debugging information. I then provided a message box in test prior to loading the dll to give me time to attach the debugger. The breakpoints where hit just fine when test.exe was compiled with debugging info. However, I was able to recreate the issue when the test was compiled without debugging info, which tells me it has something to do with the EXE not having debugging symbols available.

Is there a way to load symbols for a debug-enabled DLL when the host program doesn't provide debugging symbols while attaching to the process with gdb/VSC?

dllmain.cpp


#define WIN32_LEAN_AND_MEAN
#include <windows.h>

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
    switch(fdwReason) { 
        case DLL_PROCESS_ATTACH:
            MessageBoxA(NULL, "We've loaded the library", "Title", 0); // message box displays
            break;
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
    }
    
    return TRUE; // set breakpoint here, doesn't hit if test.exe is compiled without -g
}

gcc -g -shared dllmain.cpp -o dllmain.dll

test.cpp


#define WIN32_LEAN_AND_MEAN
#include <windows.h>

int main() {
    MessageBoxA(NULL, "Attach the debugger now, then click OK", "Attach debugger", 0);
    HMODULE h = LoadLibraryA("path/to/dllmain.dll"); // dll breakpoint should hit
    if (h == 0) return GetLastError();

    FreeLibrary(h); // dll breakpoint should hit
    return 0;

gcc test.cpp -o test.exe

gcc -g test.cpp -o test.exe

launch.json

{
    "name":"Attach to Process",
    "type":"cppdbg",
    "miDebuggerPath":"path/to/gdb.exe",
    "MIMode":"gdb",
    "request":"attach",
    "program":"path/to/test.exe",
    "processId":"${command:pickProcess}",
    "cwd":"path/to/source files",
}

Tools: Msys2, MinGW-w64, gcc@11.2.0, gdb@11.2, Visual Studio Code v1.66

c++

visual-studio-code

gcc

gdb

breakpoints

0 Answers

Your Answer

Accepted video resources