]> git.saurik.com Git - apple/xnu.git/blob - iokit/Kernel/IOKitDebug.cpp
xnu-1504.15.3.tar.gz
[apple/xnu.git] / iokit / Kernel / IOKitDebug.cpp
1 /*
2 * Copyright (c) 1998-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <sys/sysctl.h>
30
31 #include <IOKit/IOKitDebug.h>
32 #include <IOKit/IOLib.h>
33 #include <IOKit/assert.h>
34 #include <IOKit/IODeviceTreeSupport.h>
35 #include <IOKit/IOService.h>
36
37 #include <libkern/c++/OSContainers.h>
38 #include <libkern/c++/OSCPPDebug.h>
39
40 #ifdef IOKITDEBUG
41 #define DEBUG_INIT_VALUE IOKITDEBUG
42 #else
43 #define DEBUG_INIT_VALUE 0
44 #endif
45
46 SInt64 gIOKitDebug = DEBUG_INIT_VALUE;
47 SInt64 gIOKitTrace = 0x3B;
48 UInt64 gIOInterruptThresholdNS = 0;
49
50 SYSCTL_QUAD(_debug, OID_AUTO, iokit, CTLFLAG_RW | CTLFLAG_LOCKED, &gIOKitDebug, "boot_arg io");
51 SYSCTL_QUAD(_debug, OID_AUTO, iotrace, CTLFLAG_RW | CTLFLAG_LOCKED, &gIOKitTrace, "trace io");
52 SYSCTL_QUAD(_debug, OID_AUTO, iointthreshold, CTLFLAG_RW | CTLFLAG_LOCKED, &gIOInterruptThresholdNS, "io interrupt threshold");
53
54
55 int debug_malloc_size;
56 int debug_iomalloc_size;
57
58 vm_size_t debug_iomallocpageable_size;
59 int debug_container_malloc_size;
60 // int debug_ivars_size; // in OSObject.cpp
61
62 extern "C" {
63
64 #if 0
65 #define DEBG(fmt, args...) { kprintf(fmt, ## args); }
66 #else
67 #define DEBG(fmt, args...) { IOLog(fmt, ## args); }
68 #endif
69
70 void IOPrintPlane( const IORegistryPlane * plane )
71 {
72 IORegistryEntry * next;
73 IORegistryIterator * iter;
74 OSOrderedSet * all;
75 char format[] = "%xxxs";
76 IOService * service;
77
78 iter = IORegistryIterator::iterateOver( plane );
79 assert( iter );
80 all = iter->iterateAll();
81 if( all) {
82 DEBG("Count %d\n", all->getCount() );
83 all->release();
84 } else
85 DEBG("Empty\n");
86
87 iter->reset();
88 while( (next = iter->getNextObjectRecursive())) {
89 snprintf(format + 1, sizeof(format) - 1, "%ds", 2 * next->getDepth( plane ));
90 DEBG( format, "");
91 DEBG( "\033[33m%s", next->getName( plane ));
92 if( (next->getLocation( plane )))
93 DEBG("@%s", next->getLocation( plane ));
94 DEBG("\033[0m <class %s", next->getMetaClass()->getClassName());
95 if( (service = OSDynamicCast(IOService, next)))
96 DEBG(", busy %ld", (long) service->getBusyState());
97 DEBG( ">\n");
98 // IOSleep(250);
99 }
100 iter->release();
101 }
102
103 void dbugprintf(char *fmt, ...);
104 void db_dumpiojunk( const IORegistryPlane * plane );
105
106 void db_piokjunk(void) {
107
108 dbugprintf("\nDT plane:\n");
109 db_dumpiojunk( gIODTPlane );
110 dbugprintf("\n\nService plane:\n");
111 db_dumpiojunk( gIOServicePlane );
112 dbugprintf("\n\n"
113 "ivar kalloc() 0x%08x\n"
114 "malloc() 0x%08x\n"
115 "containers kalloc() 0x%08x\n"
116 "IOMalloc() 0x%08x\n"
117 "----------------------------------------\n",
118 debug_ivars_size,
119 debug_malloc_size,
120 debug_container_malloc_size,
121 debug_iomalloc_size
122 );
123
124 }
125
126
127 void db_dumpiojunk( const IORegistryPlane * plane )
128 {
129 IORegistryEntry * next;
130 IORegistryIterator * iter;
131 OSOrderedSet * all;
132 char format[] = "%xxxs";
133 IOService * service;
134
135 iter = IORegistryIterator::iterateOver( plane );
136
137 all = iter->iterateAll();
138 if( all) {
139 dbugprintf("Count %d\n", all->getCount() );
140 all->release();
141 } else dbugprintf("Empty\n");
142
143 iter->reset();
144 while( (next = iter->getNextObjectRecursive())) {
145 snprintf(format + 1, sizeof(format) - 1, "%ds", 2 * next->getDepth( plane ));
146 dbugprintf( format, "");
147 dbugprintf( "%s", next->getName( plane ));
148 if( (next->getLocation( plane )))
149 dbugprintf("@%s", next->getLocation( plane ));
150 dbugprintf(" <class %s", next->getMetaClass()->getClassName());
151 if( (service = OSDynamicCast(IOService, next)))
152 dbugprintf(", busy %ld", service->getBusyState());
153 dbugprintf( ">\n");
154 }
155 iter->release();
156 }
157
158 void IOPrintMemory( void )
159 {
160
161 // OSMetaClass::printInstanceCounts();
162
163 IOLog("\n"
164 "ivar kalloc() 0x%08x\n"
165 "malloc() 0x%08x\n"
166 "containers kalloc() 0x%08x\n"
167 "IOMalloc() 0x%08x\n"
168 "----------------------------------------\n",
169 debug_ivars_size,
170 debug_malloc_size,
171 debug_container_malloc_size,
172 debug_iomalloc_size
173 );
174 }
175
176 } /* extern "C" */
177
178 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
179
180 #define super OSObject
181 OSDefineMetaClassAndStructors(IOKitDiagnostics, OSObject)
182
183 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
184
185 OSObject * IOKitDiagnostics::diagnostics( void )
186 {
187 IOKitDiagnostics * diags;
188
189 diags = new IOKitDiagnostics;
190 if( diags && !diags->init()) {
191 diags->release();
192 diags = 0;
193 }
194
195 return( diags );
196 }
197
198 void IOKitDiagnostics::updateOffset( OSDictionary * dict,
199 UInt32 value, const char * name )
200 {
201 OSNumber * off;
202
203 off = OSNumber::withNumber( value, 32 );
204 if( !off)
205 return;
206
207 dict->setObject( name, off );
208 off->release();
209 }
210
211 bool IOKitDiagnostics::serialize(OSSerialize *s) const
212 {
213 OSDictionary * dict;
214 bool ok;
215
216 dict = OSDictionary::withCapacity( 5 );
217 if( !dict)
218 return( false );
219
220 updateOffset( dict, debug_ivars_size, "Instance allocation" );
221 updateOffset( dict, debug_container_malloc_size, "Container allocation" );
222 updateOffset( dict, debug_iomalloc_size, "IOMalloc allocation" );
223 updateOffset( dict, debug_iomallocpageable_size, "Pageable allocation" );
224
225 OSMetaClass::serializeClassDictionary(dict);
226
227 ok = dict->serialize( s );
228
229 dict->release();
230
231 return( ok );
232 }
233
234 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */