2 * Copyright (c) 2009-2010,2012-2015 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #import <Foundation/Foundation.h>
25 #import <Security/Security.h>
27 #import <CKBridge/SOSCloudKeychainClient.h>
29 #import <dispatch/dispatch.h>
31 #import <utilities/debugging.h>
32 #import <utilities/SecCFWrappers.h>
34 #import <Security/SecureObjectSync/SOSInternal.h>
36 #include <Security/SecureObjectSync/SOSCloudCircle.h>
38 #include "secToolFileIO.h"
39 #include "accountCirclesViewsPrint.h"
44 @interface NSString (FileOutput)
45 - (void) writeTo: (FILE*) file;
46 - (void) writeToStdOut;
47 - (void) writeToStdErr;
50 @implementation NSString (FileOutput)
52 - (void) writeTo: (FILE*) file {
53 CFStringPerformWithCString((__bridge CFStringRef) self, ^(const char *utf8String) { fputs(utf8String, file); });
56 - (void) writeToStdOut {
57 [self writeTo: stdout];
59 - (void) writeToStdErr {
60 [self writeTo: stderr];
65 @interface NSData (Hexinization)
67 - (NSString*) asHexString;
71 @implementation NSData (Hexinization)
73 - (NSString*) asHexString {
74 return (__bridge_transfer NSString*) CFDataCopyHexString((__bridge CFDataRef)self);
81 circle_sysdiagnose(void)
83 SOSLogSetOutputTo(NULL,NULL);
84 SOSCCDumpCircleInformation();
88 engine_sysdiagnose(void)
90 [@"Engine state:\n" writeToStdOut];
92 CFErrorRef error = NULL;
94 if (!SOSCCForEachEngineStateAsString(&error, ^(CFStringRef oneStateString) {
95 [(__bridge NSString*) oneStateString writeToStdOut];
96 [@"\n" writeToStdOut];
98 [[NSString stringWithFormat: @"No engine state, got error: %@", error] writeToStdOut];
103 homekit_sysdiagnose(void)
108 unlock_sysdiagnose(void)
112 static void idsproxy_print_message(CFDictionaryRef messages)
114 NSDictionary<NSString*, NSDictionary*> *idsMessages = (__bridge NSDictionary *)messages;
116 printf("IDS messages in flight: %d\n", (int)[idsMessages count]);
118 [idsMessages enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull identifier, NSDictionary* _Nonnull messageDictionary, BOOL * _Nonnull stop) {
119 printf("message identifier: %s\n", [identifier cStringUsingEncoding:NSUTF8StringEncoding]);
121 NSDictionary *messageDataAndPeerID = [messageDictionary valueForKey:(__bridge NSString*)kIDSMessageToSendKey];
122 [messageDataAndPeerID enumerateKeysAndObjectsUsingBlock:^(NSString* _Nonnull peerID, NSData* _Nonnull messageData, BOOL * _Nonnull stop1) {
124 printf("size of message to recipient: %lu\n", (unsigned long)[messageData length]);
127 NSString *deviceID = [messageDictionary valueForKey:(__bridge NSString*)kIDSMessageRecipientDeviceID];
129 printf("recipient device id: %s\n", [deviceID cStringUsingEncoding:NSUTF8StringEncoding]);
135 idsproxy_sysdiagnose(void)
138 dispatch_semaphore_t wait_for = dispatch_semaphore_create(0);
139 __block CFDictionaryRef returned = NULL;
141 dispatch_queue_t processQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
142 SOSCloudKeychainRetrievePendingMessageFromProxy(processQueue, ^(CFDictionaryRef returnedValues, CFErrorRef error) {
143 secdebug("SOSCloudKeychainRetrievePendingMessageFromProxy", "returned: %@", returnedValues);
144 CFRetainAssign(returned, returnedValues);
145 dispatch_semaphore_signal(wait_for);
148 dispatch_semaphore_wait(wait_for, dispatch_time(DISPATCH_TIME_NOW, 2ull * NSEC_PER_SEC));
149 secdebug("idsproxy sysdiagnose", "messages: %@", returned);
151 idsproxy_print_message(returned);
155 kvs_sysdiagnose(void) {
156 SOSLogSetOutputTo(NULL,NULL);
157 SOSCCDumpCircleKVSInformation(NULL);
162 main(int argc, const char ** argv)
165 printf("sysdiagnose keychain\n");
167 circle_sysdiagnose();
168 engine_sysdiagnose();
169 homekit_sysdiagnose();
170 unlock_sysdiagnose();
171 idsproxy_sysdiagnose();
172 // Keep this one last