Skip to content

Commit 66cd10a

Browse files
committed
Move load_symbols_for_file out of experimental:: and add a test program to simulate the scenario
1 parent 2cc71d5 commit 66cd10a

File tree

10 files changed

+76
-23
lines changed

10 files changed

+76
-23
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,19 +1207,24 @@ This section only applies to the dbghelp backend (`CPPTRACE_GET_SYMBOLS_WITH_DBG
12071207
12081208
When loading a DLL at runtime with `LoadLibrary` after a stacktrace has already been generated,
12091209
symbols from that library may not be resolved correctly for subsequent stacktraces. To fix this,
1210-
call `cpptrace::experimental::load_symbols_for_file` with the same filename that was passed to
1211-
`LoadLibrary`.
1210+
call `cpptrace::load_symbols_for_file` with the same path that was passed to `LoadLibrary`.
12121211
12131212
```cpp
12141213
HMODULE hModule = LoadLibrary("mydll.dll");
12151214
if (hModule) {
1216-
cpptrace::experimental::load_symbols_for_file("mydll.dll");
1215+
cpptrace::load_symbols_for_file("mydll.dll");
12171216
}
12181217
```
12191218

12201219
For backends other than dbghelp, `load_symbols_for_file` does nothing. For platforms other than
12211220
Windows, it is not declared.
12221221

1222+
```cpp
1223+
namespace cpptrace {
1224+
void load_symbols_for_file(const std::string& filename);
1225+
}
1226+
```
1227+
12231228
# ABI Versioning
12241229
12251230
Since cpptrace vX, the library uses an inline ABI versioning namespace and all symbols part of the public interface are

include/cpptrace/utils.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ CPPTRACE_BEGIN_NAMESPACE
6565
}
6666

6767
// dbghelp
68-
namespace experimental {
69-
CPPTRACE_EXPORT void load_symbols_for_file(const std::string& filename);
70-
}
68+
#ifdef _MSC_VER
69+
CPPTRACE_EXPORT void load_symbols_for_file(const std::string& filename);
70+
#endif
7171
CPPTRACE_END_NAMESPACE
7272

7373
#endif

src/cpptrace.cppm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ CPPTRACE_BEGIN_NAMESPACE
101101
export using cpptrace::experimental::set_cache_mode;
102102
export using cpptrace::experimental::set_dwarf_resolver_line_table_cache_size;
103103
export using cpptrace::experimental::set_dwarf_resolver_disable_aranges;
104-
export using cpptrace::experimental::load_symbols_for_file;
105104
}
105+
106+
export using cpptrace::load_symbols_for_file;
106107
CPPTRACE_END_NAMESPACE

src/symbols/symbols_core.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,3 @@ namespace detail {
154154
}
155155
}
156156
CPPTRACE_END_NAMESPACE
157-
158-
/*
159-
Fallback definition for cpptrace::experimental::load_symbols_for_file. If
160-
CPPTRACE_GET_SYMBOLS_WITH_DBGHELP is defined, this function is defined in symbols_with_dbghelp.cpp.
161-
*/
162-
#ifndef CPPTRACE_GET_SYMBOLS_WITH_DBGHELP
163-
CPPTRACE_BEGIN_NAMESPACE
164-
namespace experimental {
165-
void load_symbols_for_file(const std::string& filename) {
166-
(void)filename;
167-
}
168-
}
169-
CPPTRACE_END_NAMESPACE
170-
#endif

src/symbols/symbols_with_dbghelp.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,6 @@ namespace dbghelp {
451451
CPPTRACE_END_NAMESPACE
452452

453453
CPPTRACE_BEGIN_NAMESPACE
454-
namespace experimental {
455454
/*
456455
When a module was loaded at runtime with LoadLibrary after SymInitialize was already called,
457456
it is necessary to manually load the symbols from that module with SymLoadModuleEx.
@@ -508,7 +507,6 @@ namespace experimental {
508507
);
509508
}
510509
}
511-
}
512510
CPPTRACE_END_NAMESPACE
513511

514512
#endif

src/utils.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,10 @@ CPPTRACE_BEGIN_NAMESPACE
8989
void register_terminate_handler() {
9090
std::set_terminate(terminate_handler);
9191
}
92+
93+
#if defined(_MSC_VER) && !defined(CPPTRACE_GET_SYMBOLS_WITH_DBGHELP)
94+
void load_symbols_for_file(const std::string&) {
95+
// nop
96+
}
97+
#endif
9298
CPPTRACE_END_NAMESPACE

test/load-library/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.8)
2+
3+
project(module_load_test VERSION 0.0.1 LANGUAGES CXX)
4+
5+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../.. ${CMAKE_CURRENT_BINARY_DIR}/cpptrace)
6+
7+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
8+
9+
add_library(mydll SHARED mydll.cpp)
10+
target_include_directories(mydll PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
11+
12+
add_executable(main main.cpp)
13+
target_link_libraries(main PUBLIC cpptrace::cpptrace)

test/load-library/main.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "cpptrace/basic.hpp"
2+
#include "cpptrace/utils.hpp"
3+
#include "mydll.hpp"
4+
5+
#include <windows.h>
6+
7+
#include <iostream>
8+
9+
#include <cpptrace/from_current.hpp>
10+
11+
int main() {
12+
// generate a trace before LoadLibraryA to initialize dbghelp
13+
cpptrace::generate_trace().print();
14+
15+
HMODULE lib = LoadLibraryA("mydll.dll");
16+
if (!lib) {
17+
std::cerr << "Failed to load DLL\n";
18+
return 1;
19+
}
20+
21+
auto foo = reinterpret_cast<decltype(::foo)*>(GetProcAddress(lib, "foo"));
22+
if (!foo) {
23+
std::cerr << "Failed to get symbol\n";
24+
return 1;
25+
}
26+
27+
cpptrace::load_symbols_for_file("mydll.dll");
28+
29+
CPPTRACE_TRY {
30+
foo();
31+
} CPPTRACE_CATCH(...) {
32+
cpptrace::from_current_exception().print();
33+
}
34+
35+
FreeLibrary(lib);
36+
}

test/load-library/mydll.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "mydll.hpp"
2+
3+
void foo() {
4+
throw 0;
5+
}

test/load-library/mydll.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
extern "C" void foo();

0 commit comments

Comments
 (0)