forked from wix-incubator/DTXObjectiveCHelpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DTXAddressInfo.mm
120 lines (98 loc) · 2.59 KB
/
DTXAddressInfo.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// DTXAddressInfo.m
// DTXObjectiveCHelpers
//
// Created by Leo Natan (Wix) on 07/07/2017.
// Copyright © 2017-2020 Wix. All rights reserved.
//
#import "DTXAddressInfo.h"
#include <dlfcn.h>
#include <cxxabi.h>
static char* (*__dtx_swift_demangle)(const char *mangledName,
size_t mangledNameLength,
char *outputBuffer,
size_t *outputBufferSize,
uint32_t flags);
@implementation DTXAddressInfo
{
Dl_info _info;
}
@synthesize image, symbol, offset, address;
+ (void)load
{
__dtx_swift_demangle = (char*(*)(const char *, size_t, char *, size_t *, uint32_t))dlsym(RTLD_DEFAULT, "swift_demangle");
}
- (instancetype)initWithAddress:(NSUInteger)_address
{
self = [super init];
if(self)
{
address = _address;
dladdr((void*)address, &_info);
}
return self;
}
- (NSString *)image
{
if(_info.dli_fname != NULL)
{
NSString* potentialImage = [NSString stringWithUTF8String:_info.dli_fname];
if([potentialImage containsString:@"/"])
{
return potentialImage.lastPathComponent;
}
}
return @"???";
}
- (NSString *)symbol
{
if(_info.dli_sname != NULL)
{
int status = -1;
char* demangled = nullptr;
BOOL shouldFree = NO;
if((demangled = abi::__cxa_demangle(_info.dli_sname, nullptr, nullptr, &status)) != nullptr ||
(__dtx_swift_demangle && (demangled = __dtx_swift_demangle(_info.dli_sname, strlen(_info.dli_sname), nullptr, nullptr, 0)) != nullptr))
{
shouldFree = YES;
}
if(demangled == nullptr)
{
demangled = (char *)_info.dli_sname;
shouldFree = NO;
}
NSString* tmpSymbol = [NSString stringWithUTF8String:demangled];
if(shouldFree)
{
free(demangled);
}
return tmpSymbol;
}
else if(_info.dli_fname != NULL)
{
return self.image;
}
return [NSString stringWithFormat:@"0x%1lx", (unsigned long)_info.dli_saddr];
}
- (NSUInteger)offset
{
NSString* str = nil;
if(_info.dli_sname != NULL && (str = [NSString stringWithUTF8String:_info.dli_sname]) != nil)
{
return address - (NSUInteger)_info.dli_saddr;
}
else if(_info.dli_fname != NULL && (str = [NSString stringWithUTF8String:_info.dli_fname]) != nil)
{
return address - (NSUInteger)_info.dli_fbase;
}
return address - (NSUInteger)_info.dli_saddr;
}
- (NSString*)formattedDescriptionForIndex:(NSUInteger)index;
{
#if __LP64__
return [NSString stringWithFormat:@"%-4ld%-35s 0x%016llx %@ + %ld", index, self.image.UTF8String, (uint64_t)address, self.symbol, self.offset];
#else
return [NSString stringWithFormat:@"%-4d%-35s 0x%08lx %@ + %d", index, self.image.UTF8String, (unsigned long)address, self.symbol, self.offset];
#endif
}
@end