Getting closer. Sorting stuff for easier comparison against previous version.

This commit is contained in:
Steve Nygard 2003-12-10 06:21:50 +00:00
parent 9b46f2ecd8
commit 8577cb94e5
14 changed files with 188 additions and 76 deletions

View File

@ -125,4 +125,7 @@ struct cd_objc_protocol_method
- (void)processProtocolSection;
- (void)checkUnreferencedProtocols;
- (NSString *)formattedStringByModule;
- (NSString *)formattedStringByClass;
@end

View File

@ -39,8 +39,6 @@
- (void)doSomething;
{
CDOCProtocol *aProtocol;
NSLog(@" > %s", _cmd);
#if 0
@ -61,8 +59,6 @@
const struct cd_objc_module *ptr;
int count, index;
NSLog(@" > %s", _cmd);
objcSegment = [machOFile segmentWithName:@"__OBJC"];
moduleSection = [objcSegment sectionWithName:@"__module_info"];
@ -78,10 +74,9 @@
aModule = [[CDOCModule alloc] init];
[aModule setVersion:ptr->version];
[aModule setName:[machOFile stringFromVMAddr:ptr->name]];
NSLog(@"----------------------------------------------------------------------");
[aModule setSymtab:[self processSymtab:ptr->symtab]];
NSLog(@"aModule: %@", aModule);
[modules addObject:aModule];
[aModule release];
}
}
@ -96,12 +91,10 @@
int index, defIndex;
NSMutableArray *classes, *categories;
// class pointer: 0xa2df7fdc
// TODO: Should we convert to pointer here or in caller?
ptr = [machOFile pointerFromVMAddr:symtab segmentName:@"__OBJC"];
if (ptr == NULL) {
NSLog(@"Skipping this symtab.");
//NSLog(@"Skipping this symtab.");
return nil;
}
@ -110,41 +103,35 @@
classes = [[NSMutableArray alloc] init];
categories = [[NSMutableArray alloc] init];
NSLog(@"%s, symtab: %p, ptr: %p", _cmd, symtab, ptr);
NSLog(@"sel_ref_cnt: %p, refs: %p, cls_def_count: %d, cat_def_count: %d", ptr->sel_ref_cnt, ptr->refs, ptr->cls_def_count, ptr->cat_def_count);
//NSLog(@"%s, symtab: %p, ptr: %p", _cmd, symtab, ptr);
//NSLog(@"sel_ref_cnt: %p, refs: %p, cls_def_count: %d, cat_def_count: %d", ptr->sel_ref_cnt, ptr->refs, ptr->cls_def_count, ptr->cat_def_count);
//defs = &ptr->class_pointer;
defs = (unsigned long *)(ptr + 1);
defIndex = 0;
if (ptr->cls_def_count > 0) {
NSLog(@"%d classes:", ptr->cls_def_count);
for (index = 0; index < ptr->cls_def_count; index++, defs++, defIndex++) {
CDOCClass *aClass;
NSLog(@"defs[%d]: %p", index, *defs);
//NSLog(@"defs[%d]: %p", index, *defs);
aClass = [self processClassDefinition:*defs];
//NSLog(@"aClass: %@", aClass);
//NSLog(@"%@", [aClass formattedString]);
[classes addObject:aClass];
}
}
if (ptr->cat_def_count > 0) {
NSLog(@"%d categories:", ptr->cat_def_count);
//NSLog(@"Later.");
#if 1
//NSLog(@"%d categories:", ptr->cat_def_count);
for (index = 0; index < ptr->cat_def_count; index++, defs++, defIndex++) {
NSLog(@"defs[%d]: %p", index, *defs);
//NSLog(@"defs[%d]: %p", index, *defs);
[self processCategoryDefinition:*defs];
}
#endif
}
[aSymtab setClasses:[NSArray arrayWithArray:classes]];
NSLog(@"\n\n\n%@\n\n\n", [[classes arrayByMappingSelector:@selector(formattedString)] componentsJoinedByString:@"\n"]);
//NSLog(@"Classes:\n%@\n", [[classes arrayByMappingSelector:@selector(formattedString)] componentsJoinedByString:@"\n"]);
[classes release];
[categories release];
@ -165,7 +152,6 @@
[aClass setSuperClassName:[machOFile stringFromVMAddr:classPtr->super_class]];
// Process ivars
NSLog(@"classPtr->ivars: %p", classPtr->ivars);
if (classPtr->ivars != 0) {
const struct cd_objc_ivars *ivarsPtr;
const struct cd_objc_ivar *ivarPtr;
@ -185,12 +171,12 @@
[anIvar release];
}
[aClass setIvars:[ivars reversedArray]];
//[aClass setIvars:[ivars reversedArray]];
[aClass setIvars:[NSArray arrayWithArray:ivars]];
[ivars release];
}
// Process methods
NSLog(@"classPtr->methods: %p", classPtr->methods);
[aClass setInstanceMethods:[self processMethods:classPtr->methods]];
// Process meta class
@ -205,9 +191,7 @@
}
// Process protocols
NSLog(@"Protocols start (%@) ************************************************************", [aClass name]);
[self processProtocolList:classPtr->protocols];
NSLog(@"Protocols end (%@) ************************************************************", [aClass name]);
[aClass setProtocols:[self processProtocolList:classPtr->protocols]];
return aClass;
}
@ -224,12 +208,10 @@
if (protocolListAddr == 0)
return protocols;
//NSLog(@"protocolListAddr: %p", protocolListAddr);
protocolList = [machOFile pointerFromVMAddr:protocolListAddr];
// Compiler doesn't like the double star cast.
protocolPtrs = (void *)(protocolList + 1);
//protocolPtrs = (unsigned long **)(protocolList + 1);
NSLog(@"%d protocols:", protocolList->count);
for (index = 0; index < protocolList->count; index++, protocolPtrs++) {
[protocols addObject:[self processProtocol:*protocolPtrs]];
}
@ -244,15 +226,11 @@
NSString *name;
NSArray *protocols;
//NSLog(@" > %s (%p)", _cmd, protocolAddr);
//NSLog(@"%s, protocolAddr: %p", _cmd, protocolAddr);
protocolPtr = [machOFile pointerFromVMAddr:protocolAddr];
name = [machOFile stringFromVMAddr:protocolPtr->protocol_name];
protocols = [self processProtocolList:protocolPtr->protocol_list];
//NSLog(@"Processing protocol (%p) %@, adopts %@", protocolAddr, name, [[protocols arrayByMappingSelector:@selector(name)] description]);
aProtocol = [protocolsByName objectForKey:name];
if (aProtocol == nil) {
aProtocol = [[[CDOCProtocol alloc] init] autorelease];
@ -269,9 +247,6 @@
// TODO (2003-12-09): Maybe we should add any missing methods. But then we'd lose the original order.
//NSLog(@"aProtocol: %@", aProtocol);
//NSLog(@"formatted protocol: %@", [aProtocol formattedString]);
return aProtocol;
}
@ -333,12 +308,12 @@
{
//const struct cd_objc_class *ptr;
NSLog(@" > %s", _cmd);
//NSLog(@" > %s", _cmd);
//ptr = [machOFile pointerFromVMAddr:defRef];
//NSLog(@"isa: %p", ptr->isa);
NSLog(@"< %s", _cmd);
//NSLog(@"< %s", _cmd);
}
// Protocol reference other protocols, so we can't try to create them
@ -353,40 +328,66 @@
CDOCProtocol *aProtocol;
int count, index;
NSLog(@"\n\n\n\n\n\n\n\n\n\n");
NSLog(@" > %s", _cmd);
objcSegment = [machOFile segmentWithName:@"__OBJC"];
protocolSection = [objcSegment sectionWithName:@"__protocol"];
NSLog(@"protocolSection: %@", protocolSection);
addr = [protocolSection addr];
NSLog(@"[protocolSection size]: %d", [protocolSection size]);
NSLog(@"sizeof(struct cd_objc_protocol): %d", sizeof(struct cd_objc_protocol));
count = [protocolSection size] / sizeof(struct cd_objc_protocol);
NSLog(@"%d protocols in __protocol section", count);
for (index = 0; index < count; index++, addr += sizeof(struct cd_objc_protocol)) {
//NSLog(@"%d: addr = %p", index, addr);
for (index = 0; index < count; index++, addr += sizeof(struct cd_objc_protocol))
aProtocol = [self processProtocol:addr];
//NSLog(@"%d: aProtocol: %@", index, aProtocol);
}
{
NSLog(@"unique protocols: %@", [protocolsByName allValues]);
NSLog(@"\n\n\n\n\n\n\n\n\n\n");
NSLog(@"protocols in order: \n%@",
[[[protocolsByName allValues] arrayByMappingSelector:@selector(formattedString)] componentsJoinedByString:@"\n\n"]);
}
NSLog(@"< %s", _cmd);
}
- (void)checkUnreferencedProtocols;
{
NSLog(@" > %s", _cmd);
NSLog(@"protocolNames: %@", protocolNames);
NSLog(@"< %s", _cmd);
}
- (NSString *)formattedStringByModule;
{
NSMutableString *resultString;
int count, index;
resultString = [NSMutableString string];
// TODO: Show protocols
count = [modules count];
for (index = 0; index < count; index++)
[[modules objectAtIndex:index] appendToString:resultString];
return resultString;
}
- (NSString *)formattedStringByClass;
{
NSMutableString *resultString;
int count, index;
NSMutableArray *allClasses;
resultString = [NSMutableString string];
allClasses = [[NSMutableArray alloc] init];
// TODO: Show protocols
count = [modules count];
for (index = 0; index < count; index++) {
NSArray *moduleClasses;
moduleClasses = [[[modules objectAtIndex:index] symtab] classes];
if (moduleClasses != nil)
[allClasses addObjectsFromArray:moduleClasses];
}
[allClasses sortUsingSelector:@selector(ascendingCompareByName:)];
count = [allClasses count];
for (index = 0; index < count; index++)
[[allClasses objectAtIndex:index] appendToString:resultString];
[allClasses release];
return resultString;
}
@end

View File

@ -15,9 +15,7 @@
data = [[NSData alloc] initWithContentsOfMappedFile:filename];
header = [data bytes];
if (header->magic == MH_MAGIC)
NSLog(@"MH_MAGIC");
else {
if (header->magic != MH_MAGIC) {
if (header->magic == MH_CIGAM)
NSLog(@"MH_CIGAM");
else
@ -181,7 +179,7 @@
}
//NSLog(@"[segment name]: %@", [segment name]);
if (aSegmentName != nil && [[segment name] isEqual:aSegmentName] == NO) {
[self showWarning:[NSString stringWithFormat:@"addr %p in segment %@, required segment is %@", vmaddr, [segment name], aSegmentName]];
//[self showWarning:[NSString stringWithFormat:@"addr %p in segment %@, required segment is %@", vmaddr, [segment name], aSegmentName]];
return NULL;
}
#if 0

View File

@ -1,11 +1,12 @@
#import <Foundation/NSObject.h>
@class NSArray, NSString;
@class NSArray, NSMutableString, NSString;
@interface CDOCClass : NSObject
{
NSString *name;
NSString *superClassName; // TODO (2003-12-17): Have CDClassDump2 keep track of the name and build the tree, linking directly to an appropriate class
NSArray *protocols;
NSArray *ivars;
NSArray *classMethods;
NSArray *instanceMethods;
@ -20,6 +21,9 @@
- (NSString *)superClassName;
- (void)setSuperClassName:(NSString *)newSuperClassName;
- (NSArray *)protocols;
- (void)setProtocols:(NSArray *)newProtocols;
- (NSArray *)ivars;
- (void)setIvars:(NSArray *)newIvars;
@ -32,5 +36,8 @@
- (NSString *)description;
- (NSString *)formattedString;
- (void)appendToString:(NSMutableString *)resultString;
- (NSComparisonResult)ascendingCompareByName:(CDOCClass *)otherIvar;
@end

View File

@ -17,6 +17,7 @@
{
[name release];
[superClassName release];
[protocols release];
[ivars release];
[classMethods release];
[instanceMethods release];
@ -52,6 +53,20 @@
superClassName = [newSuperClassName retain];
}
- (NSArray *)protocols;
{
return protocols;
}
- (void)setProtocols:(NSArray *)newProtocols;
{
if (newProtocols == protocols)
return;
[protocols release];
protocols = [newProtocols retain];
}
- (NSArray *)ivars;
{
return ivars;
@ -110,9 +125,12 @@
// Need to handle adopted protocols
[result appendString:@"\n{\n"];
if ([ivars count] > 0)
if ([ivars count] > 0) {
[result appendString:[[ivars arrayByMappingSelector:@selector(formattedString)] componentsJoinedByString:@"\n"]];
[result appendString:@"\n}\n\n"];
[result appendString:@"\n"];
}
[result appendString:@"}\n\n"];
if ([instanceMethods count] > 0) {
[result appendString:[[instanceMethods arrayByMappingSelector:@selector(formattedString)] componentsJoinedByString:@"\n"]];
[result appendString:@"\n\n"];
@ -122,4 +140,44 @@
return result;
}
- (void)appendToString:(NSMutableString *)resultString;
{
int count, index;
NSArray *sortedMethods;
[resultString appendFormat:@"@interface %@", name];
if (superClassName != nil)
[resultString appendFormat:@":%@", superClassName]; // Add space later, keep this way for backwards compatability
// TODO: Need to handle adopted protocols
if ([protocols count] > 0)
[resultString appendFormat:@" <%@>", [[protocols arrayByMappingSelector:@selector(name)] componentsJoinedByString:@", "]];
[resultString appendString:@"\n{\n"];
count = [ivars count];
if (count > 0) {
for (index = 0; index < count; index++) {
[[ivars objectAtIndex:index] appendToString:resultString];
[resultString appendString:@"\n"];
}
}
[resultString appendString:@"}\n\n"];
sortedMethods = [instanceMethods sortedArrayUsingSelector:@selector(ascendingCompareByName:)];
count = [sortedMethods count];
if (count > 0) {
for (index = 0; index < count; index++) {
[[sortedMethods objectAtIndex:index] appendToString:resultString];
[resultString appendString:@"\n"];
}
[resultString appendString:@"\n"];
}
[resultString appendString:@"@end\n\n"];
}
- (NSComparisonResult)ascendingCompareByName:(CDOCClass *)otherClass;
{
return [name compare:[otherClass name]];
}
@end

View File

@ -1,6 +1,6 @@
#import <Foundation/NSObject.h>
@class NSString;
@class NSMutableString, NSString;
@interface CDOCIvar : NSObject
{
@ -18,5 +18,6 @@
- (NSString *)description;
- (NSString *)formattedString;
- (void)appendToString:(NSMutableString *)resultString;
@end

View File

@ -50,4 +50,9 @@
return [NSString stringWithFormat:@"\t%@", name];
}
- (void)appendToString:(NSMutableString *)resultString;
{
[resultString appendFormat:@"\t%@", name];
}
@end

View File

@ -1,6 +1,6 @@
#import <Foundation/NSObject.h>
@class NSString;
@class NSMutableString, NSString;
@interface CDOCMethod : NSObject
{
@ -18,5 +18,8 @@
- (NSString *)description;
- (NSString *)formattedString;
- (void)appendToString:(NSMutableString *)resultString;
- (NSComparisonResult)ascendingCompareByName:(CDOCMethod *)otherMethod;
@end

View File

@ -53,4 +53,14 @@
return [NSString stringWithFormat:@"- %@", name];
}
- (void)appendToString:(NSMutableString *)resultString;
{
[resultString appendFormat:@"- %@", name];
}
- (NSComparisonResult)ascendingCompareByName:(CDOCMethod *)otherMethod;
{
return [name compare:[otherMethod name]];
}
@end

View File

@ -1,5 +1,6 @@
#import <Foundation/NSObject.h>
@class NSMutableString;
@class CDOCSymtab;
@interface CDOCModule : NSObject
@ -23,5 +24,8 @@
- (void)setSymtab:(CDOCSymtab *)newSymtab;
- (NSString *)description;
- (NSString *)formattedString;
- (void)appendToString:(NSMutableString *)resultString;
@end

View File

@ -68,4 +68,15 @@
return [NSString stringWithFormat:@"[%@] name: %@, version: %d, symtab: %@", NSStringFromClass([self class]), name, version, symtab];
}
- (NSString *)formattedString;
{
return [NSString stringWithFormat:@"/*\n * %@\n */\n", name];
}
- (void)appendToString:(NSMutableString *)resultString;
{
[resultString appendFormat:@"/*\n * %@\n */\n\n", name];
[symtab appendToString:resultString];
}
@end

View File

@ -1,6 +1,6 @@
#import <Foundation/NSObject.h>
@class NSArray;
@class NSArray, NSMutableString;
@interface CDOCSymtab : NSObject
{
@ -19,4 +19,6 @@
- (NSString *)description;
- (void)appendToString:(NSMutableString *)resultString;
@end

View File

@ -53,4 +53,15 @@
return [NSString stringWithFormat:@"[%@] classes: %@, categories: %@", NSStringFromClass([self class]), classes, categories];
}
- (void)appendToString:(NSMutableString *)resultString;
{
int count, index;
count = [classes count];
for (index = 0; index < count; index++)
[[classes objectAtIndex:index] appendToString:resultString];
// TODO: And categories.
}
@end

View File

@ -1,5 +1,5 @@
//
// $Id: class-dump.m,v 1.38 2003/12/10 01:43:19 nygard Exp $
// $Id: class-dump.m,v 1.39 2003/12/10 06:21:50 nygard Exp $
//
//
@ -1243,18 +1243,16 @@ int main(int argc, char *argv[])
str = argv[optind];
path = [[NSString alloc] initWithBytes:str length:strlen(str) encoding:NSASCIIStringEncoding];
//NSLog(@"path: '%@'", path);
machOFile = [[CDMachOFile alloc] initWithFilename:path];
//NSLog(@"machOFile: %@", machOFile);
//NSLog(@"[machOFile description]: %@", [machOFile description]);
NSLog(@"[machOFile loadCommands]: %@", [machOFile loadCommands]);
//NSLog(@"[machOFile loadCommands]: %@", [machOFile loadCommands]);
classDump = [[CDClassDump2 alloc] initWithMachOFile:machOFile];
[classDump doSomething];
//[classDump doSomething];
[classDump processProtocolSection];
[classDump processModules];
//[classDump checkUnreferencedProtocols];
NSLog(@"Formatted result:\n%@", [classDump formattedStringByClass]);
[classDump release];
[machOFile release];