Skip to content

Commit 84ab54a

Browse files
committed
Implement SymbolInfo::getFilename() on Win32
1 parent 8c6fe1c commit 84ab54a

File tree

13 files changed

+235
-174
lines changed

13 files changed

+235
-174
lines changed

include/swift/Runtime/Win32.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@
2121

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

24+
#include <cwchar>
2425
#include <functional>
2526
#include <type_traits>
2627

2728
// For HANDLE
2829
#define WIN32_LEAN_AND_MEAN
2930
#define NOMINMAX
30-
#include <windows.h>
31+
#include <Windows.h>
3132

32-
#include <wchar.h>
33+
#pragma mark - Wide-character string conversion
3334

3435
/// Convert a wide string to UTF-8.
3536
///
@@ -53,6 +54,8 @@ char *_swift_win32_copyUTF8FromWide(const wchar_t *str);
5354
SWIFT_RUNTIME_STDLIB_SPI
5455
wchar_t *_swift_win32_copyWideFromUTF8(const char *str);
5556

57+
#pragma mark - DbgHelp library thread-safety
58+
5659
/// Configure the environment to allow calling into the Debug Help library.
5760
///
5861
/// \param body A function to invoke. This function attempts to first initialize

stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,15 @@ internal func _getMetadataSection(_ index: UInt) -> UnsafeRawPointer?
138138
@_silgen_name("swift_getMetadataSectionCount")
139139
internal func _getMetadataSectionCount() -> UInt
140140

141-
@_silgen_name("swift_getMetadataSectionName")
142-
internal func _getMetadataSectionName(
141+
@_silgen_name("swift_copyMetadataSectionName")
142+
internal func _copyMetadataSectionName(
143143
_ metadata_section: UnsafeRawPointer
144-
) -> UnsafePointer<CChar>
144+
) -> UnsafeMutablePointer<CChar>?
145+
146+
@_silgen_name("swift_freeMetadataSectionName")
147+
internal func _freeMetadataSectionName(
148+
_ name: UnsafeMutablePointer<CChar>
149+
) -> Void
145150
#endif
146151

147152
extension Section {
@@ -154,9 +159,13 @@ extension Section {
154159
internal func getReflectionInfoForImage(atIndex i: UInt32) -> ReflectionInfo? {
155160
#if INTERNAL_CHECKS_ENABLED
156161
return _getMetadataSection(UInt(i)).map { rawPointer in
157-
let name = _getMetadataSectionName(rawPointer)
162+
let cName = _copyMetadataSectionName(rawPointer)
163+
defer {
164+
_freeMetadataSectionName(cName)
165+
}
166+
let name = cName.flatMap { String(validatingUTF8: $0) } ?? "<unavailable>"
158167
let metadataSection = rawPointer.bindMemory(to: MetadataSections.self, capacity: 1).pointee
159-
return ReflectionInfo(imageName: String(validatingUTF8: name)!,
168+
return ReflectionInfo(imageName: name,
160169
fieldmd: Section(range: metadataSection.swift5_fieldmd),
161170
assocty: Section(range: metadataSection.swift5_assocty),
162171
builtin: Section(range: metadataSection.swift5_builtin),

stdlib/public/runtime/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ set(swift_runtime_sources
5050
HeapObject.cpp
5151
ImageInspectionCommon.cpp
5252
ImageInspectionMachO.cpp
53-
ImageInspectionELF.cpp
54-
ImageInspectionCOFF.cpp
5553
ImageInspectionStatic.cpp
5654
ImageInspectionWasm.cpp
5755
SymbolInfo.cpp

stdlib/public/runtime/Errors.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ enum: uint32_t {
7979
using namespace swift;
8080

8181
#if SWIFT_STDLIB_SUPPORTS_BACKTRACE_REPORTING && SWIFT_STDLIB_HAS_DLADDR
82-
static bool getSymbolNameAddr(llvm::StringRef libraryName,
83-
const SymbolInfo &syminfo,
82+
static bool getSymbolNameAddr(const SymbolInfo &syminfo,
8483
std::string &symbolName, uintptr_t &addrOut) {
8584
// If we failed to find a symbol and thus dlinfo->dli_sname is nullptr, we
8685
// need to use the hex address.
@@ -150,12 +149,14 @@ void swift::dumpStackTraceEntry(unsigned index, void *framePC,
150149
return;
151150
}
152151

153-
// If SymbolInfo:lookup succeeded then fileName is non-null. Thus, we find the
152+
// If SymbolInfo:lookup succeeded and fileName is non-null, we can find the
154153
// library name here. Avoid using StringRef::rsplit because its definition
155154
// is not provided in the header so that it requires linking with
156155
// libSupport.a.
157-
llvm::StringRef libraryName{syminfo->getFilename()};
158-
libraryName = libraryName.substr(libraryName.rfind('/')).substr(1);
156+
const char *libraryName = syminfo->getImageFilename();
157+
if (!libraryName) {
158+
libraryName = "<unavailable>";
159+
}
159160

160161
// Next we get the symbol name that we are going to use in our backtrace.
161162
std::string symbolName;
@@ -164,12 +165,12 @@ void swift::dumpStackTraceEntry(unsigned index, void *framePC,
164165
// we just get HexAddr + 0.
165166
uintptr_t symbolAddr = uintptr_t(framePC);
166167
bool foundSymbol =
167-
getSymbolNameAddr(libraryName, syminfo.value(), symbolName, symbolAddr);
168+
getSymbolNameAddr(syminfo.value(), symbolName, symbolAddr);
168169
ptrdiff_t offset = 0;
169170
if (foundSymbol) {
170171
offset = ptrdiff_t(uintptr_t(framePC) - symbolAddr);
171172
} else {
172-
auto baseAddress = syminfo->getBaseAddress();
173+
const void *baseAddress = syminfo->getImageBaseAddress();
173174
offset = ptrdiff_t(uintptr_t(framePC) - uintptr_t(baseAddress));
174175
symbolAddr = uintptr_t(framePC);
175176
symbolName = "<unavailable>";
@@ -183,12 +184,11 @@ void swift::dumpStackTraceEntry(unsigned index, void *framePC,
183184
// This gives enough info to reconstruct identical debugging target after
184185
// this process terminates.
185186
if (shortOutput) {
186-
fprintf(stderr, "%s`%s + %td", libraryName.data(), symbolName.c_str(),
187-
offset);
187+
fprintf(stderr, "%s`%s + %td", libraryName, symbolName.c_str(), offset);
188188
} else {
189189
constexpr const char *format = "%-4u %-34s 0x%0.16" PRIxPTR " %s + %td\n";
190-
fprintf(stderr, format, index, libraryName.data(), symbolAddr,
191-
symbolName.c_str(), offset);
190+
fprintf(stderr, format, index, libraryName, symbolAddr, symbolName.c_str(),
191+
offset);
192192
}
193193
#else
194194
if (shortOutput) {

stdlib/public/runtime/ImageInspectionCOFF.cpp

Lines changed: 0 additions & 89 deletions
This file was deleted.

stdlib/public/runtime/ImageInspectionCommon.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ static void fixupMetadataSectionBaseAddress(swift::MetadataSections *sections) {
6464
if (fixupNeeded) {
6565
// We need to fix up the base address. We'll need a known-good address in
6666
// the same image: `sections` itself will work nicely.
67-
auto symbolInfo = SymbolInfo::lookup(sections);
68-
if (symbolInfo.has_value() && symbolInfo->getBaseAddress()) {
69-
sections->baseAddress.store(symbolInfo->getBaseAddress(),
70-
std::memory_order_relaxed);
67+
if (auto symbolInfo = SymbolInfo::lookup(sections)) {
68+
if (const void *baseAddress = symbolInfo->getImageBaseAddress()) {
69+
sections->baseAddress.store(baseAddress, std::memory_order_relaxed);
70+
}
7171
}
7272
}
7373
}
@@ -200,22 +200,26 @@ const swift::MetadataSections *swift_getMetadataSection(size_t index) {
200200
}
201201

202202
SWIFT_RUNTIME_EXPORT
203-
const char *
204-
swift_getMetadataSectionName(const swift::MetadataSections *section) {
203+
char *swift_copyMetadataSectionName(const swift::MetadataSections *section) {
205204
if (auto info = swift::SymbolInfo::lookup(section)) {
206-
if (info->getFilename()) {
207-
return info->getFilename();
205+
if (const char *imagePath = info->getImagePath()) {
206+
return strdup(imagePath);
208207
}
209208
}
210-
return "";
209+
return nullptr;
210+
}
211+
212+
SWIFT_RUNTIME_EXPORT
213+
void swift_freeMetadataSectionName(char *name) {
214+
free(name);
211215
}
212216

213217
SWIFT_RUNTIME_EXPORT
214218
void swift_getMetadataSectionBaseAddress(const swift::MetadataSections *section,
215219
void const **out_actual,
216220
void const **out_expected) {
217221
if (auto info = swift::SymbolInfo::lookup(section)) {
218-
*out_actual = info->getBaseAddress();
222+
*out_actual = info->getImageBaseAddress();
219223
} else {
220224
*out_actual = nullptr;
221225
}

stdlib/public/runtime/ImageInspectionCommon.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,11 @@ void swift_enumerateAllMetadataSections(
8989
#ifndef NDEBUG
9090

9191
SWIFT_RUNTIME_EXPORT
92-
const char *
93-
swift_getMetadataSectionName(const struct swift::MetadataSections *section);
92+
char *
93+
swift_copyMetadataSectionName(const struct swift::MetadataSections *section);
94+
95+
SWIFT_RUNTIME_EXPORT
96+
void swift_freeMetadataSectionName(char *name);
9497

9598
SWIFT_RUNTIME_EXPORT
9699
void swift_getMetadataSectionBaseAddress(

stdlib/public/runtime/ImageInspectionELF.cpp

Lines changed: 0 additions & 25 deletions
This file was deleted.

stdlib/public/runtime/ReflectionMirror.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,8 +1117,8 @@ id swift_reflectionMirror_quickLookObject(OpaqueValue *value, const Metadata *T)
11171117
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERNAL
11181118
const char *swift_keyPath_copySymbolName(void *address) {
11191119
if (auto info = SymbolInfo::lookup(address)) {
1120-
if (info->getSymbolName()) {
1121-
return strdup(info->getSymbolName());
1120+
if (const char *symbolName = info->getSymbolName()) {
1121+
return strdup(symbolName);
11221122
}
11231123
}
11241124
return nullptr;

0 commit comments

Comments
 (0)