issue 286 - clean up some demangling code in dump_syms.mm. r=chris rogers

git-svn-id: http://google-breakpad.googlecode.com/svn/trunk@301 4c0a9323-5329-0410-9bdc-e9ce6186880e
This commit is contained in:
ted.mielczarek 2008-12-03 18:23:09 +00:00
parent e5c401467b
commit 7837cb7236
2 changed files with 16 additions and 85 deletions

View File

@ -46,7 +46,6 @@ typedef hash_map<string, dwarf2reader::SectionMap *> ArchSectionMap;
NSMutableDictionary *addresses_; // Addresses and symbols (STRONG) NSMutableDictionary *addresses_; // Addresses and symbols (STRONG)
NSMutableSet *functionAddresses_; // Function addresses (STRONG) NSMutableSet *functionAddresses_; // Function addresses (STRONG)
NSMutableDictionary *sources_; // Address and Source file paths (STRONG) NSMutableDictionary *sources_; // Address and Source file paths (STRONG)
NSMutableArray *cppAddresses_; // Addresses of C++ symbols (STRONG)
NSMutableDictionary *headers_; // Mach-o header information (STRONG) NSMutableDictionary *headers_; // Mach-o header information (STRONG)
NSMutableDictionary *sectionData_; // Keyed by seg/sect name (STRONG) NSMutableDictionary *sectionData_; // Keyed by seg/sect name (STRONG)
uint32_t lastStartAddress_; uint32_t lastStartAddress_;

View File

@ -104,8 +104,7 @@ void DumpFunctionMap(const dwarf2reader::FunctionMap function_map) {
@interface DumpSymbols(PrivateMethods) @interface DumpSymbols(PrivateMethods)
- (NSArray *)convertCPlusPlusSymbols:(NSArray *)symbols; - (NSString *)convertCPlusPlusSymbol:(NSString *)symbol;
- (void)convertSymbols;
- (void)addFunction:(NSString *)name line:(int)line address:(uint64_t)address section:(int)section; - (void)addFunction:(NSString *)name line:(int)line address:(uint64_t)address section:(int)section;
- (BOOL)processSymbolItem:(struct nlist_64 *)list stringTable:(char *)table; - (BOOL)processSymbolItem:(struct nlist_64 *)list stringTable:(char *)table;
- (BOOL)loadSymbolInfo:(void *)base offset:(uint32_t)offset; - (BOOL)loadSymbolInfo:(void *)base offset:(uint32_t)offset;
@ -126,86 +125,20 @@ void DumpFunctionMap(const dwarf2reader::FunctionMap function_map) {
@implementation DumpSymbols @implementation DumpSymbols
//============================================================================= //=============================================================================
- (NSArray *)convertCPlusPlusSymbols:(NSArray *)symbols { - (NSString *)convertCPlusPlusSymbol:(NSString *)symbol {
NSMutableArray *symbols_demangled = [[NSMutableArray alloc]
initWithCapacity:[symbols count]];
// __cxa_demangle will realloc this if needed // __cxa_demangle will realloc this if needed
char *buffer = (char *)malloc(1024); char *buffer = (char *)malloc(1024);
size_t buffer_size = 1024; size_t buffer_size = 1024;
int result; int result;
NSEnumerator *enumerator = [symbols objectEnumerator]; const char *sym = [symbol UTF8String];
id symbolObject; NSString *demangled = nil;
while ((symbolObject = [enumerator nextObject])) { buffer = abi::__cxa_demangle(sym, buffer, &buffer_size, &result);
const char *symbol = [symbolObject UTF8String]; if (result == 0) {
buffer = abi::__cxa_demangle(symbol, buffer, &buffer_size, &result); demangled = [NSString stringWithUTF8String:buffer];
if (result == 0) {
[symbols_demangled addObject:[NSString stringWithUTF8String:buffer]];
} else {
// unable to demangle - use mangled name instead
[symbols_demangled addObject:symbolObject];
}
} }
free(buffer); free(buffer);
return demangled;
return symbols_demangled;
}
//=============================================================================
- (void)convertSymbols {
unsigned int count = [cppAddresses_ count];
NSMutableArray *symbols = [[NSMutableArray alloc] initWithCapacity:count];
// Sort addresses for processing
NSArray *addresses = [cppAddresses_ sortedArrayUsingSelector:
@selector(compare:)];
for (unsigned int i = 0; i < count; ++i) {
NSMutableDictionary *dict = [addresses_ objectForKey:
[addresses objectAtIndex:i]];
NSString *symbol = [dict objectForKey:kAddressSymbolKey];
// Make sure that the symbol is valid
if ([symbol length] < 1)
symbol = kUnknownSymbol;
[symbols addObject:symbol];
}
// In order to deal with crashing problems in c++filt, we setup
// a while loop to handle the case where convertCPlusPlusSymbols
// only returns partial results.
// We then attempt to continue from the point where c++filt failed
// and add the partial results to the total results until we're
// completely done.
unsigned int totalIndex = 0;
unsigned int totalCount = count;
while (totalIndex < totalCount) {
NSRange range = NSMakeRange(totalIndex, totalCount - totalIndex);
NSArray *subarray = [symbols subarrayWithRange:range];
NSArray *converted = [self convertCPlusPlusSymbols:subarray];
unsigned int convertedCount = [converted count];
if (convertedCount == 0) {
break; // we give up at this point
}
for (unsigned int convertedIndex = 0;
convertedIndex < convertedCount && totalIndex < totalCount;
++totalIndex, ++convertedIndex) {
NSMutableDictionary *dict = [addresses_ objectForKey:
[addresses objectAtIndex:totalIndex]];
NSString *symbol = [converted objectAtIndex:convertedIndex];
// Only add if this is a non-zero length symbol
if ([symbol length])
[dict setObject:symbol forKey:kAddressConvertedSymbolKey];
}
}
[symbols release];
} }
//============================================================================= //=============================================================================
@ -215,8 +148,7 @@ void DumpFunctionMap(const dwarf2reader::FunctionMap function_map) {
if (!address) if (!address)
return; return;
// If the function starts with "_Z" or "__Z" then add it to the list of // If the function starts with "_Z" or "__Z" then demangle it.
// addresses to run through the c++filt
BOOL isCPP = NO; BOOL isCPP = NO;
if ([name hasPrefix:@"__Z"]) { if ([name hasPrefix:@"__Z"]) {
@ -255,12 +187,6 @@ void DumpFunctionMap(const dwarf2reader::FunctionMap function_map) {
NSRange emptyRange = { NSNotFound, 0 }; NSRange emptyRange = { NSNotFound, 0 };
NSRange objcppRange = [name rangeOfCharacterFromSet:objcppCharSet]; NSRange objcppRange = [name rangeOfCharacterFromSet:objcppCharSet];
isCPP = NSEqualRanges(objcppRange, emptyRange); isCPP = NSEqualRanges(objcppRange, emptyRange);
}
if (isCPP) {
if (!cppAddresses_)
cppAddresses_ = [[NSMutableArray alloc] init];
[cppAddresses_ addObject:addressNum];
} else if ([name characterAtIndex:0] == '_') { } else if ([name characterAtIndex:0] == '_') {
// Remove the leading underscore // Remove the leading underscore
name = [name substringFromIndex:1]; name = [name substringFromIndex:1];
@ -283,6 +209,13 @@ void DumpFunctionMap(const dwarf2reader::FunctionMap function_map) {
[functionAddresses_ addObject:addressNum]; [functionAddresses_ addObject:addressNum];
} }
if (isCPP) {
// try demangling
NSString *demangled = [self convertCPlusPlusSymbol:name];
if (demangled != nil)
[dict setObject:demangled forKey:kAddressConvertedSymbolKey];
}
if (line && ![dict objectForKey:kAddressSourceLineKey]) if (line && ![dict objectForKey:kAddressSourceLineKey])
[dict setObject:[NSNumber numberWithUnsignedInt:line] [dict setObject:[NSNumber numberWithUnsignedInt:line]
forKey:kAddressSourceLineKey]; forKey:kAddressSourceLineKey];
@ -920,7 +853,6 @@ static BOOL WriteFormat(int fd, const char *fmt, ...) {
// Gather the information // Gather the information
[self loadSymbolInfoForArchitecture]; [self loadSymbolInfoForArchitecture];
[self convertSymbols];
NSArray *sortedAddresses = [[addresses_ allKeys] NSArray *sortedAddresses = [[addresses_ allKeys]
sortedArrayUsingSelector:@selector(compare:)]; sortedArrayUsingSelector:@selector(compare:)];