Skip to content

Commit b03888e

Browse files
committed
---
yaml --- r: 347078 b: refs/heads/master c: af7f34e h: refs/heads/master
1 parent 1815294 commit b03888e

File tree

7 files changed

+71
-10
lines changed

7 files changed

+71
-10
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: fa6e3abc5fe95adc7938a28a0482db7c404781a5
2+
refs/heads/master: af7f34e6811aede14377e82c9c0ef36aed828a4b
33
refs/heads/master-next: 203b3026584ecad859eb328b2e12490099409cd5
44
refs/tags/osx-passed: b6b74147ef8a386f532cf9357a1bde006e552c54
55
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-a: 6bb18e013c2284f2b45f5f84f2df2887dc0f7dea

trunk/stdlib/public/runtime/Errors.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ enum: uint32_t {
6767
using namespace swift;
6868

6969
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
70-
static bool getSymbolNameAddr(llvm::StringRef libraryName, SymbolInfo syminfo,
70+
static bool getSymbolNameAddr(llvm::StringRef libraryName,
71+
const SymbolInfo &syminfo,
7172
std::string &symbolName, uintptr_t &addrOut) {
7273
// If we failed to find a symbol and thus dlinfo->dli_sname is nullptr, we
7374
// need to use the hex address.
@@ -96,7 +97,7 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName, SymbolInfo syminfo,
9697

9798
{
9899
std::lock_guard<std::mutex> lock(mutex);
99-
dwResult = UnDecorateSymbolName(syminfo.symbolName, szUndName,
100+
dwResult = UnDecorateSymbolName(syminfo.symbolName.get(), szUndName,
100101
sizeof(szUndName), dwFlags);
101102
}
102103

@@ -106,7 +107,8 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName, SymbolInfo syminfo,
106107
}
107108
#else
108109
int status;
109-
char *demangled = abi::__cxa_demangle(syminfo.symbolName, 0, 0, &status);
110+
char *demangled =
111+
abi::__cxa_demangle(syminfo.symbolName.get(), 0, 0, &status);
110112
if (status == 0) {
111113
assert(demangled != nullptr &&
112114
"If __cxa_demangle succeeds, demangled should never be nullptr");
@@ -121,7 +123,7 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName, SymbolInfo syminfo,
121123
// Otherwise, try to demangle with swift. If swift fails to demangle, it will
122124
// just pass through the original output.
123125
symbolName = demangleSymbolAsString(
124-
syminfo.symbolName, strlen(syminfo.symbolName),
126+
syminfo.symbolName.get(), strlen(syminfo.symbolName.get()),
125127
Demangle::DemangleOptions::SimplifiedUIDemangleOptions());
126128
return true;
127129
}

trunk/stdlib/public/runtime/ImageInspection.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,42 @@
2424
#include "ImageInspectionELF.h"
2525
#include <cstdint>
2626
#include <cstddef>
27+
#if defined(__cplusplus)
28+
#include <memory>
29+
#endif
2730

2831
namespace swift {
2932

3033
/// This is a platform independent version of Dl_info from dlfcn.h
34+
#if defined(__cplusplus)
35+
36+
template <typename T>
37+
struct null_deleter {
38+
void operator()(T *) const {}
39+
void operator()(typename std::remove_cv<T>::type *value) const {}
40+
};
41+
42+
template <typename T>
43+
struct free_deleter {
44+
void operator()(T *value) const {
45+
free(const_cast<typename std::remove_cv<T>::type *>(value));
46+
}
47+
void operator()(typename std::remove_cv<T>::type *value) const {
48+
free(value);
49+
}
50+
};
51+
3152
struct SymbolInfo {
3253
const char *fileName;
3354
void *baseAddress;
34-
const char *symbolName;
55+
#if defined(_WIN32)
56+
std::unique_ptr<const char, free_deleter<const char>> symbolName;
57+
#else
58+
std::unique_ptr<const char, null_deleter<const char>> symbolName;
59+
#endif
3560
void *symbolAddress;
3661
};
62+
#endif
3763

3864
/// Load the metadata from the image necessary to find protocols by name.
3965
void initializeProtocolLookup();

trunk/stdlib/public/runtime/ImageInspectionCOFF.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
#if defined(__CYGWIN__)
1919
#include <dlfcn.h>
20+
#elif defined(_WIN32)
21+
#define WIN32_LEAN_AND_MEAN
22+
#define NOMINMAX
23+
#include <Windows.h>
24+
#include <DbgHelp.h>
2025
#endif
2126

2227
using namespace swift;
@@ -129,10 +134,38 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
129134
info->baseAddress = dlinfo.dli_fbase;
130135
info->symbolName = dli_info.dli_sname;
131136
info->symbolAddress = dli_saddr;
137+
return 1;
138+
#elif defined(_WIN32)
139+
static const constexpr size_t kSymbolMaxNameLen = 1024;
140+
static bool bInitialized = false;
141+
142+
if (bInitialized == false) {
143+
if (SymInitialize(GetCurrentProcess(), /*UserSearchPath=*/NULL,
144+
/*fInvadeProcess=*/TRUE) == FALSE)
145+
return 0;
146+
bInitialized = true;
147+
}
148+
149+
char buffer[sizeof(SYMBOL_INFO) + kSymbolMaxNameLen];
150+
PSYMBOL_INFO pSymbol = reinterpret_cast<PSYMBOL_INFO>(buffer);
151+
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
152+
pSymbol->MaxNameLen = kSymbolMaxNameLen;
153+
154+
DWORD64 dwDisplacement = 0;
155+
156+
if (SymFromAddr(GetCurrentProcess(), reinterpret_cast<const DWORD64>(address),
157+
&dwDisplacement, pSymbol) == FALSE)
158+
return 0;
159+
160+
info->fileName = NULL;
161+
info->baseAddress = reinterpret_cast<void *>(pSymbol->ModBase);
162+
info->symbolName.reset(_strdup(pSymbol->Name));
163+
info->symbolAddress = reinterpret_cast<void *>(pSymbol->Address);
164+
132165
return 1;
133166
#else
134167
return 0;
135-
#endif // __CYGWIN__
168+
#endif // defined(__CYGWIN__) || defined(_WIN32)
136169
}
137170

138171
// This is only used for backward deployment hooks, which we currently only support for

trunk/stdlib/public/runtime/ImageInspectionELF.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
134134

135135
info->fileName = dlinfo.dli_fname;
136136
info->baseAddress = dlinfo.dli_fbase;
137-
info->symbolName = dlinfo.dli_sname;
137+
info->symbolName.reset(dlinfo.dli_sname);
138138
info->symbolAddress = dlinfo.dli_saddr;
139139
return 1;
140140
}

trunk/stdlib/public/runtime/ImageInspectionMachO.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int swift::lookupSymbol(const void *address, SymbolInfo *info) {
105105

106106
info->fileName = dlinfo.dli_fname;
107107
info->baseAddress = dlinfo.dli_fbase;
108-
info->symbolName = dlinfo.dli_sname;
108+
info->symbolName.reset(dlinfo.dli_sname);
109109
info->symbolAddress = dlinfo.dli_saddr;
110110
return 1;
111111
}

trunk/stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ template<> void ProtocolConformanceDescriptor::dump() const {
6767
int ok = lookupSymbol(addr, &info);
6868
if (!ok)
6969
return "<unknown addr>";
70-
return info.symbolName;
70+
return info.symbolName.get();
7171
};
7272

7373
switch (auto kind = getTypeKind()) {

0 commit comments

Comments
 (0)