xref: /xnu-11215/iokit/Kernel/IOKitDebug.cpp (revision a3bb9fcc)
1 /*
2  * Copyright (c) 1998-2010 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 <libkern/c++/OSContainers.h>
32 #include <libkern/c++/OSCPPDebug.h>
33 
34 #include <IOKit/IOKitDebug.h>
35 #include <IOKit/IOLib.h>
36 #include <IOKit/assert.h>
37 #include <IOKit/IODeviceTreeSupport.h>
38 #include <IOKit/IOService.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 = 0;
48 
49 #if DEVELOPMENT || DEBUG
50 #define IODEBUG_CTLFLAGS	CTLFLAG_RW
51 #else
52 #define IODEBUG_CTLFLAGS	CTLFLAG_RD
53 #endif
54 
55 SYSCTL_QUAD(_debug, OID_AUTO, iokit, IODEBUG_CTLFLAGS | CTLFLAG_LOCKED, &gIOKitDebug, "boot_arg io");
56 SYSCTL_QUAD(_debug, OID_AUTO, iotrace, CTLFLAG_RW | CTLFLAG_LOCKED, &gIOKitTrace, "trace io");
57 
58 
59 int 		debug_malloc_size;
60 int		debug_iomalloc_size;
61 
62 vm_size_t	debug_iomallocpageable_size;
63 int 		debug_container_malloc_size;
64 // int 		debug_ivars_size; // in OSObject.cpp
65 
66 extern "C" {
67 
68 #if 0
69 #define DEBG(fmt, args...)   { kprintf(fmt, ## args); }
70 #else
71 #define DEBG(fmt, args...)   { IOLog(fmt, ## args); }
72 #endif
73 
74 void IOPrintPlane( const IORegistryPlane * plane )
75 {
76     IORegistryEntry *		next;
77     IORegistryIterator * 	iter;
78     OSOrderedSet *		all;
79     char			format[] = "%xxxs";
80     IOService *			service;
81 
82     iter = IORegistryIterator::iterateOver( plane );
83     assert( iter );
84     all = iter->iterateAll();
85     if( all) {
86         DEBG("Count %d\n", all->getCount() );
87         all->release();
88     } else
89 	DEBG("Empty\n");
90 
91     iter->reset();
92     while( (next = iter->getNextObjectRecursive())) {
93 	snprintf(format + 1, sizeof(format) - 1, "%ds", 2 * next->getDepth( plane ));
94 	DEBG( format, "");
95 	DEBG( "\033[33m%s", next->getName( plane ));
96 	if( (next->getLocation( plane )))
97             DEBG("@%s", next->getLocation( plane ));
98 	DEBG("\033[0m <class %s", next->getMetaClass()->getClassName());
99         if( (service = OSDynamicCast(IOService, next)))
100             DEBG(", busy %ld", (long) service->getBusyState());
101 	DEBG( ">\n");
102 //	IOSleep(250);
103     }
104     iter->release();
105 }
106 
107 void db_piokjunk(void)
108 {
109 }
110 
111 void db_dumpiojunk( const IORegistryPlane * plane __unused )
112 {
113 }
114 
115 void IOPrintMemory( void )
116 {
117 
118 //    OSMetaClass::printInstanceCounts();
119 
120     IOLog("\n"
121 	    "ivar kalloc()       0x%08x\n"
122 	    "malloc()            0x%08x\n"
123             "containers kalloc() 0x%08x\n"
124 	    "IOMalloc()          0x%08x\n"
125             "----------------------------------------\n",
126 	    debug_ivars_size,
127             debug_malloc_size,
128             debug_container_malloc_size,
129             debug_iomalloc_size
130             );
131 }
132 
133 } /* extern "C" */
134 
135 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
136 
137 #define super OSObject
138 OSDefineMetaClassAndStructors(IOKitDiagnostics, OSObject)
139 
140 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
141 
142 OSObject * IOKitDiagnostics::diagnostics( void )
143 {
144     IOKitDiagnostics * diags;
145 
146     diags = new IOKitDiagnostics;
147     if( diags && !diags->init()) {
148 	diags->release();
149 	diags = 0;
150     }
151 
152     return( diags );
153 }
154 
155 void IOKitDiagnostics::updateOffset( OSDictionary * dict,
156 			UInt32 value, const char * name )
157 {
158     OSNumber * off;
159 
160     off = OSNumber::withNumber( value, 32 );
161     if( !off)
162 	return;
163 
164     dict->setObject( name, off );
165     off->release();
166 }
167 
168 bool IOKitDiagnostics::serialize(OSSerialize *s) const
169 {
170     OSDictionary * 	dict;
171     bool		ok;
172 
173     dict = OSDictionary::withCapacity( 5 );
174     if( !dict)
175 	return( false );
176 
177     updateOffset( dict, debug_ivars_size, "Instance allocation" );
178     updateOffset( dict, debug_container_malloc_size, "Container allocation" );
179     updateOffset( dict, debug_iomalloc_size, "IOMalloc allocation" );
180     updateOffset( dict, debug_iomallocpageable_size, "Pageable allocation" );
181 
182     OSMetaClass::serializeClassDictionary(dict);
183 
184     ok = dict->serialize( s );
185 
186     dict->release();
187 
188     return( ok );
189 }
190 
191 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
192