30
30
#include < unistd.h>
31
31
#endif
32
32
#include < stdarg.h>
33
+ #include " ImageInspection.h"
33
34
#include " swift/Runtime/Debug.h"
34
35
#include " swift/Runtime/Mutex.h"
35
36
#include " swift/Basic/Demangle.h"
38
39
#if !defined(_MSC_VER)
39
40
#include < cxxabi.h>
40
41
#endif
41
- #if SWIFT_SUPPORTS_BACKTRACE_REPORTING
42
42
43
+ #if SWIFT_SUPPORTS_BACKTRACE_REPORTING
43
44
// execinfo.h is not available on Android. Checks in this file ensure that
44
45
// fatalError behaves as expected, but without stack traces.
45
46
#include < execinfo.h>
46
- // We are only using dlfcn.h in code that is invoked on non cygwin/android
47
- // platforms. So I am putting it here.
48
- #include < dlfcn.h>
49
47
#endif
50
48
51
49
#ifdef __APPLE__
@@ -62,26 +60,26 @@ using namespace swift;
62
60
63
61
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
64
62
65
- static bool getSymbolNameAddr (llvm::StringRef libraryName, Dl_info dlinfo ,
63
+ static bool getSymbolNameAddr (llvm::StringRef libraryName, SymbolInfo syminfo ,
66
64
std::string &symbolName, uintptr_t &addrOut) {
67
65
68
66
// If we failed to find a symbol and thus dlinfo->dli_sname is nullptr, we
69
67
// need to use the hex address.
70
- bool hasUnavailableAddress = dlinfo. dli_sname == nullptr ;
68
+ bool hasUnavailableAddress = syminfo. symbolName == nullptr ;
71
69
72
70
if (hasUnavailableAddress) {
73
71
return false ;
74
72
}
75
73
76
74
// Ok, now we know that we have some sort of "real" name. Set the outAddr.
77
- addrOut = uintptr_t (dlinfo. dli_saddr );
75
+ addrOut = uintptr_t (syminfo. symbolAddress );
78
76
79
77
// First lets try to demangle using cxxabi. If this fails, we will try to
80
78
// demangle with swift. We are taking advantage of __cxa_demangle actually
81
79
// providing failure status instead of just returning the original string like
82
80
// swift demangle.
83
81
int status;
84
- char *demangled = abi::__cxa_demangle (dlinfo. dli_sname , 0 , 0 , &status);
82
+ char *demangled = abi::__cxa_demangle (syminfo. symbolName , 0 , 0 , &status);
85
83
if (status == 0 ) {
86
84
assert (demangled != nullptr && " If __cxa_demangle succeeds, demangled "
87
85
" should never be nullptr" );
@@ -95,27 +93,24 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName, Dl_info dlinfo,
95
93
// Otherwise, try to demangle with swift. If swift fails to demangle, it will
96
94
// just pass through the original output.
97
95
symbolName = demangleSymbolAsString (
98
- dlinfo. dli_sname , strlen (dlinfo. dli_sname ),
96
+ syminfo. symbolName , strlen (syminfo. symbolName ),
99
97
Demangle::DemangleOptions::SimplifiedUIDemangleOptions ());
100
98
return true ;
101
99
}
102
100
103
101
// / This function dumps one line of a stack trace. It is assumed that \p address
104
102
// / is the address of the stack frame at index \p index.
105
103
static void dumpStackTraceEntry (unsigned index, void *framePC) {
106
- Dl_info dlinfo ;
104
+ SymbolInfo syminfo ;
107
105
108
- // 0 is failure for dladdr. We do not use nullptr since it is an int
109
- // argument. This violates normal unix patterns. See man page for dladdr on OS
110
- // X.
111
- if (0 == dladdr (framePC, &dlinfo)) {
106
+ // 0 is failure for lookupSymbol
107
+ if (0 == lookupSymbol (framePC, &syminfo)) {
112
108
return ;
113
109
}
114
110
115
- // According to the man page of dladdr, if dladdr returns non-zero, then we
116
- // know that it must have fname, fbase set. Thus, we find the library name
117
- // here.
118
- StringRef libraryName = StringRef (dlinfo.dli_fname ).rsplit (' /' ).second ;
111
+ // If lookupSymbol succeeded then fileName is non-null. Thus, we find the
112
+ // library name here.
113
+ StringRef libraryName = StringRef (syminfo.fileName ).rsplit (' /' ).second ;
119
114
120
115
// Next we get the symbol name that we are going to use in our backtrace.
121
116
std::string symbolName;
@@ -124,7 +119,7 @@ static void dumpStackTraceEntry(unsigned index, void *framePC) {
124
119
// we just get HexAddr + 0.
125
120
uintptr_t symbolAddr = uintptr_t (framePC);
126
121
bool foundSymbol =
127
- getSymbolNameAddr (libraryName, dlinfo , symbolName, symbolAddr);
122
+ getSymbolNameAddr (libraryName, syminfo , symbolName, symbolAddr);
128
123
129
124
// We do not use %p here for our pointers since the format is implementation
130
125
// defined. This makes it logically impossible to check the output. Forcing
@@ -142,7 +137,7 @@ static void dumpStackTraceEntry(unsigned index, void *framePC) {
142
137
" <unavailable> + %td\n " ;
143
138
fprintf (stderr, backtraceEntryFormat, index, libraryName.data (),
144
139
uintptr_t (framePC),
145
- ptrdiff_t (uintptr_t (framePC) - uintptr_t (dlinfo. dli_fbase )));
140
+ ptrdiff_t (uintptr_t (framePC) - uintptr_t (syminfo. baseAddress )));
146
141
}
147
142
}
148
143
0 commit comments