Skip to content

Commit 18df4db

Browse files
committed
Move _swift_win32_withDbgHelpLibrary into the new header.
It makes sense to move this function into the new Win32 header. rdar://103397975
1 parent f38515a commit 18df4db

File tree

5 files changed

+87
-77
lines changed

5 files changed

+87
-77
lines changed

include/swift/Runtime/Win32.h

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121

2222
#include "swift/shims/Visibility.h"
2323

24+
#include <functional>
25+
#include <type_traits>
26+
27+
// For HANDLE
28+
#define WIN32_LEAN_AND_MEAN
29+
#define NOMINMAX
30+
#include <windows.h>
31+
2432
#include <wchar.h>
2533

2634
/// Convert a wide string to UTF-8.
@@ -45,6 +53,81 @@ char *_swift_win32_copyUTF8FromWide(const wchar_t *str);
4553
SWIFT_RUNTIME_STDLIB_INTERNAL
4654
wchar_t *_swift_win32_copyWideFromUTF8(const char *str);
4755

56+
/// Configure the environment to allow calling into the Debug Help library.
57+
///
58+
/// \param body A function to invoke. This function attempts to first initialize
59+
/// the Debug Help library. If it did so successfully, the handle used during
60+
/// initialization is passed to this function and should be used with
61+
/// subsequent calls to the Debug Help library. Do not close this handle.
62+
/// \param context A caller-supplied value to pass to \a body.
63+
///
64+
/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
65+
/// calls into it from the Swift runtime and stdlib should route through this
66+
/// function.
67+
///
68+
/// This function sets the Debug Help library's options by calling
69+
/// \c SymSetOptions() before \a body is invoked, and then resets them back to
70+
/// their old value before returning. \a body can also call \c SymSetOptions()
71+
/// if needed.
72+
SWIFT_RUNTIME_STDLIB_SPI
73+
void _swift_win32_withDbgHelpLibrary(
74+
void (* body)(HANDLE hProcess, void *context), void *context);
75+
76+
/// Configure the environment to allow calling into the Debug Help library.
77+
///
78+
/// \param body A function to invoke. This function attempts to first initialize
79+
/// the Debug Help library. If it did so successfully, the handle used during
80+
/// initialization is passed to this function and should be used with
81+
/// subsequent calls to the Debug Help library. Do not close this handle.
82+
///
83+
/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
84+
/// calls into it from the Swift runtime and stdlib should route through this
85+
/// function.
86+
///
87+
/// This function sets the Debug Help library's options by calling
88+
/// \c SymSetOptions() before \a body is invoked, and then resets them back to
89+
/// their old value before returning. \a body can also call \c SymSetOptions()
90+
/// if needed.
91+
static inline void _swift_win32_withDbgHelpLibrary(
92+
const std::function<void(HANDLE /*hProcess*/)> &body) {
93+
_swift_win32_withDbgHelpLibrary([](HANDLE hProcess, void *context) {
94+
auto bodyp = reinterpret_cast<std::function<void(HANDLE)> *>(context);
95+
(* bodyp)(hProcess);
96+
}, const_cast<void *>(reinterpret_cast<const void *>(&body)));
97+
}
98+
99+
/// Configure the environment to allow calling into the Debug Help library.
100+
///
101+
/// \param body A function to invoke. This function attempts to first initialize
102+
/// the Debug Help library. If it did so successfully, the handle used during
103+
/// initialization is passed to this function and should be used with
104+
/// subsequent calls to the Debug Help library. Do not close this handle.
105+
///
106+
/// \returns Whatever is returned from \a body.
107+
///
108+
/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
109+
/// calls into it from the Swift runtime and stdlib should route through this
110+
/// function.
111+
///
112+
/// This function sets the Debug Help library's options by calling
113+
/// \c SymSetOptions() before \a body is invoked, and then resets them back to
114+
/// their old value before returning. \a body can also call \c SymSetOptions()
115+
/// if needed.
116+
template <
117+
typename F,
118+
typename R = typename std::result_of_t<F&(HANDLE /*hProcess*/)>,
119+
typename = typename std::enable_if_t<!std::is_same<void, R>::value>
120+
>
121+
static inline R _swift_win32_withDbgHelpLibrary(const F& body) {
122+
R result;
123+
124+
_swift_win32_withDbgHelpLibrary([&body, &result] (HANDLE hProcess) {
125+
result = body(hProcess);
126+
});
127+
128+
return result;
129+
}
130+
48131
#endif // defined(_WIN32)
49132

50133
#endif // SWIFT_RUNTIME_WIN32_H

stdlib/public/runtime/Errors.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "swift/Demangling/Demangle.h"
3737
#include "swift/Runtime/Debug.h"
3838
#include "swift/Runtime/Portability.h"
39+
#include "swift/Runtime/Win32.h"
3940
#include "swift/Threading/Errors.h"
4041
#include "swift/Threading/Mutex.h"
4142
#include "llvm/ADT/StringRef.h"

stdlib/public/runtime/ImageInspection.h

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -83,83 +83,6 @@ void addImageAccessibleFunctionsBlockCallbackUnsafe(const void *baseAddress,
8383
const void *start,
8484
uintptr_t size);
8585

86-
#if defined(_WIN32)
87-
/// Configure the environment to allow calling into the Debug Help library.
88-
///
89-
/// \param body A function to invoke. This function attempts to first initialize
90-
/// the Debug Help library. If it did so successfully, the handle used during
91-
/// initialization is passed to this function and should be used with
92-
/// subsequent calls to the Debug Help library. Do not close this handle.
93-
/// \param context A caller-supplied value to pass to \a body.
94-
///
95-
/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
96-
/// calls into it from the Swift runtime and stdlib should route through this
97-
/// function.
98-
///
99-
/// This function sets the Debug Help library's options by calling
100-
/// \c SymSetOptions() before \a body is invoked, and then resets them back to
101-
/// their old value before returning. \a body can also call \c SymSetOptions()
102-
/// if needed.
103-
SWIFT_RUNTIME_STDLIB_SPI
104-
void _swift_win32_withDbgHelpLibrary(
105-
void (* body)(HANDLE hProcess, void *context), void *context);
106-
107-
/// Configure the environment to allow calling into the Debug Help library.
108-
///
109-
/// \param body A function to invoke. This function attempts to first initialize
110-
/// the Debug Help library. If it did so successfully, the handle used during
111-
/// initialization is passed to this function and should be used with
112-
/// subsequent calls to the Debug Help library. Do not close this handle.
113-
///
114-
/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
115-
/// calls into it from the Swift runtime and stdlib should route through this
116-
/// function.
117-
///
118-
/// This function sets the Debug Help library's options by calling
119-
/// \c SymSetOptions() before \a body is invoked, and then resets them back to
120-
/// their old value before returning. \a body can also call \c SymSetOptions()
121-
/// if needed.
122-
static inline void _swift_win32_withDbgHelpLibrary(
123-
const std::function<void(HANDLE /*hProcess*/)> &body) {
124-
_swift_win32_withDbgHelpLibrary([](HANDLE hProcess, void *context) {
125-
auto bodyp = reinterpret_cast<std::function<void(HANDLE)> *>(context);
126-
(* bodyp)(hProcess);
127-
}, const_cast<void *>(reinterpret_cast<const void *>(&body)));
128-
}
129-
130-
/// Configure the environment to allow calling into the Debug Help library.
131-
///
132-
/// \param body A function to invoke. This function attempts to first initialize
133-
/// the Debug Help library. If it did so successfully, the handle used during
134-
/// initialization is passed to this function and should be used with
135-
/// subsequent calls to the Debug Help library. Do not close this handle.
136-
///
137-
/// \returns Whatever is returned from \a body.
138-
///
139-
/// On Windows, the Debug Help library (DbgHelp.lib) is not thread-safe. All
140-
/// calls into it from the Swift runtime and stdlib should route through this
141-
/// function.
142-
///
143-
/// This function sets the Debug Help library's options by calling
144-
/// \c SymSetOptions() before \a body is invoked, and then resets them back to
145-
/// their old value before returning. \a body can also call \c SymSetOptions()
146-
/// if needed.
147-
template <
148-
typename F,
149-
typename R = typename std::result_of_t<F&(HANDLE /*hProcess*/)>,
150-
typename = typename std::enable_if_t<!std::is_same<void, R>::value>
151-
>
152-
static inline R _swift_win32_withDbgHelpLibrary(const F& body) {
153-
R result;
154-
155-
_swift_win32_withDbgHelpLibrary([&body, &result] (HANDLE hProcess) {
156-
result = body(hProcess);
157-
});
158-
159-
return result;
160-
}
161-
#endif
162-
16386
} // end namespace swift
16487

16588
#endif

stdlib/public/runtime/ImageInspectionCOFF.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <DbgHelp.h>
2424
#endif
2525

26+
#include "swift/Runtime/Win32.h"
2627
#include "swift/Threading/Mutex.h"
2728

2829
using namespace swift;

stdlib/public/runtime/SymbolInfo.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <dlfcn.h>
2222
#endif
2323

24+
#include "swift/Runtime/Win32.h"
25+
2426
#include "ImageInspection.h"
2527

2628
using namespace swift;

0 commit comments

Comments
 (0)