Skip to content

Commit bc3dcb3

Browse files
committed
Adopt feedback from PR, make lookupSymbol() a to-be-deprecated function that calls SymbolInfo:lookup() with a better signature
1 parent 00a08a5 commit bc3dcb3

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

stdlib/public/runtime/Errors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void swift::dumpStackTraceEntry(unsigned index, void *framePC,
155155
// library name here. Avoid using StringRef::rsplit because its definition
156156
// is not provided in the header so that it requires linking with
157157
// libSupport.a.
158-
llvm::StringRef libraryName{syminfo.getFileName()};
158+
llvm::StringRef libraryName{syminfo.getFilename()};
159159
libraryName = libraryName.substr(libraryName.rfind('/')).substr(1);
160160

161161
// Next we get the symbol name that we are going to use in our backtrace.

stdlib/public/runtime/ImageInspectionCommon.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ const char *
192192
swift_getMetadataSectionName(const swift::MetadataSections *section) {
193193
swift::SymbolInfo info;
194194
if (lookupSymbol(section, &info)) {
195-
if (info.getFileName()) {
196-
return info.getFileName();
195+
if (info.getFilename()) {
196+
return info.getFilename();
197197
}
198198
}
199199
return "";

stdlib/public/runtime/SymbolInfo.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
#include <dlfcn.h>
2222
#endif
2323

24+
#include <utility>
25+
2426
#include "ImageInspection.h"
2527

2628
using namespace swift;
2729

28-
const char *swift::SymbolInfo::getFileName() const {
30+
const char *swift::SymbolInfo::getFilename() const {
2931
#if defined(_WIN32) && !defined(__CYGWIN__)
3032
return nullptr;
3133
#elif SWIFT_STDLIB_HAS_DLADDR
@@ -65,31 +67,32 @@ const void *swift::SymbolInfo::getSymbolAddress() const {
6567
#endif
6668
}
6769

68-
int swift::SymbolInfo::lookup(const void *address, SymbolInfo *outInfo) {
69-
int result = 0;
70+
llvm::Optional<SymbolInfo> swift::SymbolInfo::lookup(const void *address) {
71+
llvm::Optional<SymbolInfo> result;
7072

7173
#if defined(__wasm__)
7274
// Currently, Wasm doesn't have a standard stable ABI for exporting address <->
7375
// symbol table, it's work in progress. Also, there is no API to access such
7476
// information from Wasm binary side. It's accessible only from host VM.
7577
// See https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md
76-
7778
#elif defined(_WIN32) && !defined(__CYGWIN__)
7879
_swift_win32_withDbgHelpLibrary([&] (HANDLE hProcess) {
7980
if (!hProcess) {
80-
return;
81+
return 0;
8182
}
8283

83-
outInfo->_package.si.SizeOfStruct = sizeof(SYMBOL_INFO);
84-
outInfo->_package.si.MaxNameLen = MAX_SYM_NAME;
84+
SymbolInfo info;
85+
info._package.si.SizeOfStruct = sizeof(SYMBOL_INFO);
86+
info._package.si.MaxNameLen = MAX_SYM_NAME;
8587
if (SymFromAddr(hProcess, reinterpret_cast<const DWORD64>(address),
86-
nullptr, &outInfo->_package.si)) {
87-
result = 1;
88+
nullptr, &info._package.si)) {
89+
result = std::move(info);
8890
}
8991
});
9092
#elif SWIFT_STDLIB_HAS_DLADDR
91-
if (dladdr(address, &outInfo->_info)) {
92-
result = 1;
93+
SymbolInfo info;
94+
if (dladdr(address, &info._info)) {
95+
result = std::move(info);
9396
}
9497
#endif
9598

stdlib/public/runtime/SymbolInfo.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#include <dlfcn.h>
3838
#endif
3939

40+
#include "llvm/ADT/Optional.h"
41+
4042
namespace swift {
4143
struct SymbolInfo {
4244
private:
@@ -50,22 +52,49 @@ struct SymbolInfo {
5052
SymbolInfo() {}
5153

5254
/// Get the file name of the image where the symbol was found.
53-
const char *getFileName() const;
55+
///
56+
/// The resulting C string is only valid for the lifetime of \c this.
57+
const char *getFilename() const;
5458

5559
/// Get the base address of the image where the symbol was found.
5660
const void *getBaseAddress() const;
5761

5862
/// Get the name of the symbol.
63+
///
64+
/// The resulting C string is only valid for the lifetime of \c this.
5965
const char *getSymbolName() const;
6066

6167
/// Get the address of the symbol.
6268
const void *getSymbolAddress() const;
6369

64-
static int lookup(const void *address, SymbolInfo *outInfo);
70+
/// Look up a symbol by address.
71+
///
72+
/// \param address The address where the symbol is located.
73+
///
74+
/// \returns On success, an instance of \c SymbolInfo containing information
75+
/// about the symbol at \a address. On failure, \c llvm::None.
76+
static llvm::Optional<SymbolInfo> lookup(const void *address);
6577
};
6678

79+
/// Look up a symbol by address and store the result in a locally-declared
80+
/// \c SymbolInfo value.
81+
///
82+
/// \param address The address where the symbol is located.
83+
/// \param outInfo On successful return, populated with information about the
84+
/// symbol at \a address. On failure, unspecified.
85+
///
86+
/// \returns On success, a non-zero integer. On failure, zero.
87+
///
88+
/// \note This function will be replaced with \c SymbolInfo::lookup() in a
89+
/// future update.
6790
static inline int lookupSymbol(const void *address, SymbolInfo *outInfo) {
68-
return SymbolInfo::lookup(address, outInfo);
91+
auto info = SymbolInfo::lookup(address);
92+
if (info.has_value()) {
93+
*outInfo = info.value();
94+
return 1;
95+
} else {
96+
return 0;
97+
}
6998
}
7099

71100
} // end namespace swift

0 commit comments

Comments
 (0)