void printKext( OSKextRef theKext, PathSpec pathSpec, Boolean extra_info, char lineEnd) { CFStringRef bundleID = NULL; // do NOT release CFStringRef bundleVersion = NULL; // do NOT release CFStringRef kextPath = NULL; // must release char * kext_path = NULL; // must free char * bundle_id = NULL; // must free char * bundle_version = NULL; // must free kextPath = copyPathForKext(theKext, pathSpec); if (!kextPath) { OSKextLogMemError(); goto finish; } kext_path = createUTF8CStringForCFString(kextPath); if (!kext_path) { OSKextLogMemError(); goto finish; } if (extra_info) { bundleID = OSKextGetIdentifier(theKext); bundleVersion = OSKextGetValueForInfoDictionaryKey(theKext, kCFBundleVersionKey); bundle_id = createUTF8CStringForCFString(bundleID); bundle_version = createUTF8CStringForCFString(bundleVersion); if (!bundle_id || !bundle_version) { OSKextLogMemError(); goto finish; } fprintf(stdout, "%s\t%s\t%s%c", kext_path, bundle_id, bundle_version, lineEnd); } else { fprintf(stdout, "%s%c", kext_path, lineEnd); } finish: SAFE_RELEASE(kextPath); SAFE_FREE(kext_path); SAFE_FREE(bundle_id); SAFE_FREE(bundle_version); return; }
/******************************************************************************* * XXX: I'm really not sure this is completely reliable for getting a relative * XXX: path. *******************************************************************************/ void printKextExecutable( OSKextRef theKext, PathSpec pathSpec, char lineEnd) { CFStringRef executablePath = NULL; // must release char * executablePathCString = NULL; // must free executablePath = copyKextExecutablePath(theKext, pathSpec); if (!executablePath) { OSKextLogMemError(); goto finish; } executablePathCString = createUTF8CStringForCFString(executablePath); if (!executablePathCString) { OSKextLogMemError(); goto finish; } printf("%s%c", executablePathCString, lineEnd); finish: SAFE_FREE(executablePathCString); SAFE_RELEASE(executablePath); return; }
void printKextInfoDictionary( OSKextRef theKext, PathSpec pathSpec, char lineEnd) { CFStringRef infoDictPath = NULL; // must release char * infoDictPathCString = NULL; // must free infoDictPath = copyKextInfoDictionaryPath(theKext, pathSpec); if (!infoDictPath) { OSKextLogMemError(); goto finish; } infoDictPathCString = createUTF8CStringForCFString(infoDictPath); if (!infoDictPathCString) { OSKextLogMemError(); goto finish; } printf("%s%c", infoDictPathCString, lineEnd); finish: SAFE_FREE(infoDictPathCString); SAFE_RELEASE(infoDictPath); return; }
void printProperty( CFStringRef label, CFStringRef propKey, CFTypeRef value, char lineEnd) { CFTypeID type = CFGetTypeID(value); CFStringRef propString = NULL; // must release CFStringRef labeledString = NULL; // must release CFStringRef outputString = NULL; // do not release char * allocString = NULL; // must free char * dataString = NULL; // must free if (type == CFStringGetTypeID()) { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ = \"%@\"%c"), propKey, value, lineEnd); } else if (type == CFNumberGetTypeID()) { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ = %@%c"), propKey, value, lineEnd); } else if (type == CFBooleanGetTypeID()) { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ = %@%c"), propKey, value, lineEnd); } else if (type == CFDataGetTypeID()) { CFIndex length = 0; length = CFDataGetLength(value); const UInt8 * data = CFDataGetBytePtr(value); if (!data) { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@[%zd] = <null data pointer>%c"), propKey, length, lineEnd); } else { int numBytes = (int)MIN(length, kMaxPrintableCFDataLength); dataString = stringForData(data, MIN(numBytes, kMaxPrintableCFDataLength)); if (length > kMaxPrintableCFDataLength) { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ = <data (%zd bytes): %s...>%c"), propKey, length, dataString, lineEnd); } else { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ = <data (%zd bytes): %s>%c"), propKey, length, dataString, lineEnd); } } } else if (type == CFDictionaryGetTypeID()) { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ = <dictionary of %zd items>%c"), propKey, CFDictionaryGetCount(value), lineEnd); } else if (type == CFArrayGetTypeID()) { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ = <array of %zd items>%c"), propKey, CFArrayGetCount(value), lineEnd); } else { propString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@ = <value of unknown type>%c"), propKey, lineEnd); } if (!propString) { goto finish; } if (label) { labeledString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@: %@"), label, propString); outputString = labeledString; } else { labeledString = CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR("%@"), propString); outputString = labeledString; } if (!outputString) { goto finish; } allocString = createUTF8CStringForCFString(outputString); if (!allocString) { goto finish; } fprintf(stdout, "%s", allocString); finish: if (propString) CFRelease(propString); if (labeledString) CFRelease(labeledString); if (allocString) free(allocString); if (dataString) free(dataString); return; }
void printKextInfo(CFDictionaryRef kextInfo, KextstatArgs * toolArgs) { CFBooleanRef isKernelComponent = NULL; // do not release CFNumberRef loadTag = NULL; // do not release CFNumberRef retainCount = NULL; // do not release CFNumberRef loadAddress = NULL; // do not release CFNumberRef loadSize = NULL; // do not release CFNumberRef wiredSize = NULL; // do not release CFStringRef bundleID = NULL; // do not release CFStringRef bundleVersion = NULL; // do not release CFArrayRef dependencyLoadTags = NULL; // do not release CFMutableArrayRef sortedLoadTags = NULL; // must release uint32_t loadTagValue = kOSKextInvalidLoadTag; uint32_t retainCountValue = (uint32_t)-1; uint64_t loadAddressValue = (uint64_t)-1; uint32_t loadSizeValue = (uint32_t)-1; uint32_t wiredSizeValue = (uint32_t)-1; uint32_t cpuTypeValue = (uint32_t)-1; uint32_t cpuSubTypeValue = (uint32_t)-1; char * bundleIDCString = NULL; // must free char * bundleVersionCString = NULL; // must free CFIndex count, i; loadTag = (CFNumberRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSBundleLoadTagKey)); retainCount = (CFNumberRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSBundleRetainCountKey)); loadAddress = (CFNumberRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSBundleLoadAddressKey)); loadSize = (CFNumberRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSBundleLoadSizeKey)); wiredSize = (CFNumberRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSBundleWiredSizeKey)); bundleID = (CFStringRef)CFDictionaryGetValue(kextInfo, kCFBundleIdentifierKey); bundleVersion = (CFStringRef)CFDictionaryGetValue(kextInfo, kCFBundleVersionKey); dependencyLoadTags = (CFArrayRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSBundleDependenciesKey)); /* If the -k flag was given, skip any kernel components unless * they are explicitly requested. */ if (toolArgs->flagNoKernelComponents) { isKernelComponent = (CFBooleanRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSKernelResourceKey)); if (isKernelComponent && CFBooleanGetValue(isKernelComponent)) { if (bundleID && kCFNotFound == CFArrayGetFirstIndexOfValue(toolArgs->bundleIDs, RANGE_ALL(toolArgs->bundleIDs), bundleID)) { goto finish; } } } if (!getNumValue(loadTag, kCFNumberSInt32Type, &loadTagValue)) { loadTagValue = kOSKextInvalidLoadTag; } /* Never print the info for the kernel (loadTag 0, id __kernel__). */ if (loadTagValue == 0) { goto finish; } if (!getNumValue(retainCount, kCFNumberSInt32Type, &retainCountValue)) { retainCountValue = (uint32_t)-1; } if (!getNumValue(loadAddress, kCFNumberSInt64Type, &loadAddressValue)) { loadAddressValue = (uint64_t)-1; } if (!getNumValue(loadSize, kCFNumberSInt32Type, &loadSizeValue)) { loadSizeValue = (uint32_t)-1; } if (!getNumValue(wiredSize, kCFNumberSInt32Type, &wiredSizeValue)) { wiredSizeValue = (uint32_t)-1; } if (!getNumValue(((CFNumberRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSBundleCPUTypeKey))), kCFNumberSInt32Type, &cpuTypeValue)) { cpuTypeValue = (uint32_t)-1; } if (!getNumValue(((CFNumberRef)CFDictionaryGetValue(kextInfo, CFSTR(kOSBundleCPUSubtypeKey))), kCFNumberSInt32Type, &cpuSubTypeValue)) { cpuSubTypeValue = (uint32_t)-1; } bundleIDCString = createUTF8CStringForCFString(bundleID); bundleVersionCString = createUTF8CStringForCFString(bundleVersion); /* First column has no leading space. * * These field widths are from the old kextstat, may want to change them. */ if (loadTagValue == kOSKextInvalidLoadTag) { fprintf(stdout, "%5s", kStringInvalidShort); } else { fprintf(stdout, "%5d", loadTagValue); } if (retainCountValue == (uint32_t)-1) { fprintf(stdout, " %4s", kStringInvalidShort); } else { fprintf(stdout, " %4d", retainCountValue); } if (toolArgs->runningKernelArch->cputype & CPU_ARCH_ABI64) { if (loadAddressValue == (uint64_t)-1) { fprintf(stdout, " %-18s", kStringInvalidLong); } else { fprintf(stdout, " %#-18llx", (uint64_t)loadAddressValue); } } else { if (loadAddressValue == (uint64_t)-1) { fprintf(stdout, " %-10s", kStringInvalidLong); } else { fprintf(stdout, " %#-10x", (uint32_t)loadAddressValue); } } if (loadSizeValue == (uint32_t)-1) { fprintf(stdout, " %-10s", kStringInvalidLong); } else { fprintf(stdout, " %#-10x", loadSizeValue); } if (wiredSizeValue == (uint32_t)-1) { fprintf(stdout, " %-10s", kStringInvalidLong); } else { fprintf(stdout, " %#-10x", wiredSizeValue); } if (toolArgs->flagShowArchitecture) { // include kext cputype/cpusubtype info if (cpuTypeValue == (uint32_t) -1) { fprintf(stdout, " %10s/%-7s", kStringInvalidLong, kStringInvalidLong); } else { const NXArchInfo * archName = NXGetArchInfoFromCpuType(cpuTypeValue, cpuSubTypeValue); if (archName != NULL) { fprintf(stdout, " %-18s", archName->name); } else { fprintf(stdout, " %#010x/%#-7x", cpuTypeValue, cpuSubTypeValue); } } } fprintf(stdout, " %s", bundleIDCString ? bundleIDCString : kStringInvalidLong); fprintf(stdout, " (%s)", bundleVersionCString ? bundleVersionCString : kStringInvalidLong); if (dependencyLoadTags && CFArrayGetCount(dependencyLoadTags)) { sortedLoadTags = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, dependencyLoadTags); if (!sortedLoadTags) { OSKextLogMemError(); goto finish; } CFArraySortValues(sortedLoadTags, RANGE_ALL(sortedLoadTags), &compareNumbers, /* context */ NULL); fprintf(stdout, " <"); count = CFArrayGetCount(sortedLoadTags); for (i = 0; i < count; i++) { loadTag = (CFNumberRef)CFArrayGetValueAtIndex(sortedLoadTags, i); if (!getNumValue(loadTag, kCFNumberSInt32Type, &loadTagValue)) { loadTagValue = kOSKextInvalidLoadTag; } if (loadTagValue == kOSKextInvalidLoadTag) { fprintf(stdout, "%s%s", i == 0 ? "" : " ", kStringInvalidShort); } else { fprintf(stdout, "%s%d", i == 0 ? "" : " ", loadTagValue); } } fprintf(stdout, ">"); } fprintf(stdout, "\n"); finish: SAFE_RELEASE(sortedLoadTags); SAFE_FREE(bundleIDCString); SAFE_FREE(bundleVersionCString); return; }