|
| 1 | + |
| 2 | + |
| 3 | +//-------------------------------------------------------------------------------------------------- |
| 4 | +// |
| 5 | +// BNCDebug.m |
| 6 | +// Branch.framework |
| 7 | +// |
| 8 | +// Debugging Support |
| 9 | +// Edward Smith, October 2016 |
| 10 | +// |
| 11 | +// -©- Copyright © 2016 Branch, all rights reserved. -©- |
| 12 | +// |
| 13 | +//-------------------------------------------------------------------------------------------------- |
| 14 | + |
| 15 | + |
| 16 | +#import "BNCDebug.h" |
| 17 | +#import <sys/sysctl.h> |
| 18 | +#import <objc/runtime.h> |
| 19 | + |
| 20 | + |
| 21 | +BOOL BNCDebuggerIsAttached() { |
| 22 | + // From an Apple tech note that I've lost --EB Smith |
| 23 | + |
| 24 | + // Returns true if the current process is being debugged (either |
| 25 | + // running under the debugger or has a debugger attached post facto). |
| 26 | + |
| 27 | + int junk; |
| 28 | + int mib[4]; |
| 29 | + struct kinfo_proc info; |
| 30 | + size_t size; |
| 31 | + |
| 32 | + // Initialize the flags so that, if sysctl fails for some bizarre |
| 33 | + // reason, we get a predictable result. |
| 34 | + |
| 35 | + info.kp_proc.p_flag = 0; |
| 36 | + |
| 37 | + // Initialize mib, which tells sysctl the info we want, in this case |
| 38 | + // we're looking for information about a specific process ID. |
| 39 | + |
| 40 | + mib[0] = CTL_KERN; |
| 41 | + mib[1] = KERN_PROC; |
| 42 | + mib[2] = KERN_PROC_PID; |
| 43 | + mib[3] = getpid(); |
| 44 | + |
| 45 | + // Call sysctl. |
| 46 | + |
| 47 | + size = sizeof(info); |
| 48 | + junk = sysctl(mib, sizeof(mib) / sizeof(*mib), &info, &size, NULL, 0); |
| 49 | + if (junk != 0) { |
| 50 | + NSLog(@"Program error in BNCDebuggerIsAttached. Junk != 0!"); |
| 51 | + } |
| 52 | + |
| 53 | + // We're being debugged if the P_TRACED flag is set. |
| 54 | + |
| 55 | + return ( (info.kp_proc.p_flag & P_TRACED) != 0 ); |
| 56 | +} |
| 57 | + |
| 58 | +NSString * _Nonnull BNCDebugStringFromObject(id _Nullable instance) { |
| 59 | + // Dump the class -- |
| 60 | + |
| 61 | + if (!instance) return @"Object is nil.\n"; |
| 62 | + |
| 63 | + const char* superclassname = "nil"; |
| 64 | + Class class = object_getClass(instance); |
| 65 | + Class superclass = class_getSuperclass(class); |
| 66 | + if (superclass) superclassname = class_getName(superclass); |
| 67 | + if (!superclassname) superclassname = "<nil>"; |
| 68 | + |
| 69 | + NSMutableString *result = [NSMutableString stringWithCapacity:512]; |
| 70 | + if (class_isMetaClass(class)) { |
| 71 | + [result appendFormat:@"\nClass %p is class '%s' of class '%s':\n", |
| 72 | + instance, class_getName(class), superclassname]; |
| 73 | + class = instance; |
| 74 | + } else { |
| 75 | + [result appendFormat:@"\nInstance %p is of class '%s' of class '%s':\n", |
| 76 | + instance, class_getName(class), superclassname]; |
| 77 | + } |
| 78 | + |
| 79 | + // Ivars -- |
| 80 | + |
| 81 | + #define isTypeOf(encoding, type) \ |
| 82 | + (strncmp(encoding, @encode(type), strlen(encoding)) == 0) |
| 83 | + |
| 84 | + #define AppendValueOfType(type, format) \ |
| 85 | + if (isTypeOf(encoding, type)) { \ |
| 86 | + if (ivarPtr) { \ |
| 87 | + [result appendFormat:@"\tIvar '%s' type '%s' value '"format"'.\n", \ |
| 88 | + ivarName, #type, *((type*)ivarPtr)]; \ |
| 89 | + } else { \ |
| 90 | + [result appendFormat:@"\tIvar '%s' type '%s'.\n", \ |
| 91 | + ivarName, #type]; \ |
| 92 | + } \ |
| 93 | + } else |
| 94 | + |
| 95 | + uint count = 0; |
| 96 | + Ivar *ivars = class_copyIvarList(class, &count); |
| 97 | + for (int i = 0; i < count; ++i) { |
| 98 | + const char* encoding = ivar_getTypeEncoding(ivars[i]); |
| 99 | + const char* ivarName = ivar_getName(ivars[i]); |
| 100 | + const void* ivarPtr = nil; |
| 101 | + if (class == instance) { |
| 102 | + // instance is a class, so there aren't any ivar values. |
| 103 | + } else if (encoding[0] == '@' || encoding[0] == '#') { |
| 104 | + ivarPtr = (__bridge void*) object_getIvar(instance, ivars[i]); |
| 105 | + } else { |
| 106 | + ivarPtr = (void*) (((__bridge void*)instance) + ivar_getOffset(ivars[i])); |
| 107 | + } |
| 108 | + |
| 109 | + if (encoding[0] == '@') { |
| 110 | + if (ivarPtr) |
| 111 | + [result appendFormat:@"\tIvar '%s' type '%@' value '%@'.\n", |
| 112 | + ivarName, NSStringFromClass(((__bridge id<NSObject>)ivarPtr).class), ivarPtr]; |
| 113 | + else { |
| 114 | + NSString *className = [NSString stringWithFormat:@"%s", encoding]; |
| 115 | + if ([className hasPrefix:@"@\""]) |
| 116 | + className = [className substringFromIndex:2]; |
| 117 | + if ([className hasSuffix:@"\""]) |
| 118 | + className = [className substringToIndex:className.length-1]; |
| 119 | + [result appendFormat:@"\tIvar '%s' type '%@'.\n", ivarName, className]; |
| 120 | + } |
| 121 | + } else if (isTypeOf(encoding, Class)) { |
| 122 | + if (ivarPtr) |
| 123 | + [result appendFormat:@"\tIvar '%s' type 'class' value '%@'.\n", |
| 124 | + ivarName, NSStringFromClass((__bridge Class _Nonnull)(ivarPtr))]; |
| 125 | + else |
| 126 | + [result appendFormat:@"\tIvar '%s' type 'class'.\n", |
| 127 | + ivarName]; |
| 128 | + } else if (isTypeOf(encoding, char*)) { |
| 129 | + if (ivarPtr) |
| 130 | + [result appendFormat:@"\tIvar '%s' type 'char*' value '%s'.\n", |
| 131 | + ivarName, *(char**)ivarPtr]; |
| 132 | + else |
| 133 | + [result appendFormat:@"\tIvar '%s' type 'char*'.\n", |
| 134 | + ivarName]; |
| 135 | + } else if (isTypeOf(encoding, BOOL)) { |
| 136 | + if (ivarPtr) |
| 137 | + [result appendFormat:@"\tIvar '%s' type 'BOOL' value '%s'.\n", |
| 138 | + ivarName, (*(BOOL*)ivarPtr)?"YES":"NO"]; |
| 139 | + else |
| 140 | + [result appendFormat:@"\tIvar '%s' type 'BOOL'.\n", |
| 141 | + ivarName]; |
| 142 | + } else if (isTypeOf(encoding, int)) { |
| 143 | + if (ivarPtr) |
| 144 | + [result appendFormat:@"\tIvar '%s' type '%s' value '%d'.\n", |
| 145 | + ivarName, "int", *((int*)ivarPtr)]; |
| 146 | + else |
| 147 | + [result appendFormat:@"\tIvar '%s' type '%s'.\n", |
| 148 | + ivarName, "int"]; |
| 149 | + } else |
| 150 | + // clang-format off |
| 151 | + AppendValueOfType(float, "%f") |
| 152 | + AppendValueOfType(double, "%f") |
| 153 | + AppendValueOfType(long double, "%Lf") |
| 154 | + AppendValueOfType(char, "%c") |
| 155 | + AppendValueOfType(int, "%d") |
| 156 | + AppendValueOfType(short, "%hd") |
| 157 | + AppendValueOfType(long, "%ld") |
| 158 | + AppendValueOfType(long long, "%lld") |
| 159 | + AppendValueOfType(unsigned char, "%c") |
| 160 | + AppendValueOfType(unsigned int, "%u") |
| 161 | + AppendValueOfType(unsigned short, "%hu") |
| 162 | + AppendValueOfType(unsigned long, "%lu") |
| 163 | + AppendValueOfType(unsigned long long, "%llu") |
| 164 | + [result appendFormat:@"\tIvar '%s' type '%s' (un-handled type).\n", |
| 165 | + ivarName, |
| 166 | + encoding]; |
| 167 | + // clang-format off |
| 168 | + } |
| 169 | + if (ivars) free(ivars); |
| 170 | + |
| 171 | + #undef AppendValueOfType |
| 172 | + #undef isTypeOf |
| 173 | + |
| 174 | + // Properties -- |
| 175 | + |
| 176 | + count = 0; |
| 177 | + objc_property_t *properties = class_copyPropertyList(class, &count); |
| 178 | + for (int i = 0; i < count; ++i) |
| 179 | + [result appendFormat:@"\tProperty name: '%s'.\n", property_getName(properties[i])]; |
| 180 | + if (properties) free(properties); |
| 181 | + |
| 182 | + // Class methods -- |
| 183 | + |
| 184 | + count = 0; |
| 185 | + Method *methods = class_copyMethodList(object_getClass(class), &count); |
| 186 | + for (int i = 0; i < count; ++i) |
| 187 | + [result appendFormat:@"\tClass method name: '%s'.\n", |
| 188 | + sel_getName(method_getName(methods[i]))]; |
| 189 | + if (methods) free(methods); |
| 190 | + |
| 191 | + // Instance methods -- |
| 192 | + |
| 193 | + count = 0; |
| 194 | + methods = class_copyMethodList(class, &count); |
| 195 | + for (int i = 0; i < count; ++i) |
| 196 | + [result appendFormat:@"\tMethod name: '%s'.\n", |
| 197 | + sel_getName(method_getName(methods[i]))]; |
| 198 | + if (methods) free(methods); |
| 199 | + |
| 200 | + return result; |
| 201 | +} |
| 202 | + |
| 203 | +NSArray<NSString*> * _Nonnull BNCDebugArrayOfReqisteredClasses() { |
| 204 | + // Add all loaded classes to the NSString array: |
| 205 | + |
| 206 | + int numClasses = 0; |
| 207 | + Class * classes = NULL; |
| 208 | + |
| 209 | + numClasses = objc_getClassList(NULL, 0); |
| 210 | + if (numClasses <= 0) return @[]; |
| 211 | + |
| 212 | + classes = (__unsafe_unretained Class*) malloc(sizeof(Class) * numClasses); |
| 213 | + numClasses = objc_getClassList(classes, numClasses); |
| 214 | + |
| 215 | + NSMutableArray<NSString*> *result = [[NSMutableArray alloc] initWithCapacity:numClasses]; |
| 216 | + |
| 217 | + Class *class = classes; |
| 218 | + for (int i = 0; i < numClasses; ++i) { |
| 219 | + NSString* s = NSStringFromClass(*class); |
| 220 | + if (s) [result addObject:s]; |
| 221 | + ++class; |
| 222 | + } |
| 223 | + |
| 224 | + free(classes); |
| 225 | + return result; |
| 226 | +} |
0 commit comments