21
21
22
22
#include " swift/shims/Visibility.h"
23
23
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
+
24
32
#include < wchar.h>
25
33
26
34
// / Convert a wide string to UTF-8.
@@ -45,6 +53,81 @@ char *_swift_win32_copyUTF8FromWide(const wchar_t *str);
45
53
SWIFT_RUNTIME_STDLIB_INTERNAL
46
54
wchar_t *_swift_win32_copyWideFromUTF8 (const char *str);
47
55
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
+
48
131
#endif // defined(_WIN32)
49
132
50
133
#endif // SWIFT_RUNTIME_WIN32_H
0 commit comments