microsoft / microsoft/STL

`<stacktrace>` fails to load `.pdb` of shared library

Open
#4,746 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

I'm going to use std::stacktrace in a .dll whitch is loaded by LoadLibrary.
While I have followed the instruction from a previous issue #2779 and passed /PDBALTPATH:%_PDB% to link.exe for two binaries, the .pdb file fails to load correctly.
The current directory structure is:

C:.
│  test.exe
│  test.pdb
│
└─so
        dll.dll
        dll.pdb

sources of the two binaries is:
dll.cpp

#include "head.h"
#include <stacktrace>
#include <iostream>

DLL_API void ps() {
    std::cout << std::stacktrace::current() << std::endl;
}

test.cpp

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include "head.h"
int main() {
    auto hModule = LoadLibrary("./so/dll.dll");
    if (!hModule) {
        std::cerr << "Failed to load library" << std::endl;
        return 1;
    }
    auto func = reinterpret_cast<decltype(&ps)>(GetProcAddress(hModule, "ps"));
    if (!func) {
        std::cerr << "Failed to get function" << std::endl;
        return 1;
    }
    func();
    FreeLibrary(hModule);
    return 0;
}

head.h

extern "C" {
#ifdef _EXPORT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif

DLL_API void ps();
}

And when I run test.exe, it prints something like:

0> dll!ps+0x4A
1> C:\space\projects\cpp\st\test.cpp(18): test!main+0x7E
2> D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl(288): test!__scrt_common_main_seh+0x10C
3> KERNEL32!BaseThreadInitThunk+0x1D
4> ntdll!RtlUserThreadStart+0x28

, indicating that dll.pdb is not loaded correctly (otherwise it should print the directory of dll.cpp).

Since the document of std::stacktrace is missing, is there any solution?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided dll.cpp, test.cpp, and head.h reproduction, including the LoadLibrary call and /PDBALTPATH:%_PDB% linker option. Build and run test.exe with dll.dll and dll.pdb in the shown so directory, then inspect how std::stacktrace resolves the shared-library frame. Done means the frame for dll!ps resolves to the directory and line in dll.cpp.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools, operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.