1/*
2 * Copyright (c) 1998-2006 Apple Computer, 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 * Copyright (c) 1998 Apple Computer, Inc.  All rights reserved.
30 *
31 * HISTORY
32 *
33 */
34
35#include <sys/sysctl.h>
36
37#include <IOKit/IOKitDebug.h>
38#include <IOKit/IOLib.h>
39#include <IOKit/assert.h>
40#include <IOKit/IODeviceTreeSupport.h>
41#include <IOKit/IOService.h>
42
43#include <libkern/c++/OSContainers.h>
44#include <libkern/c++/OSCPPDebug.h>
45
46#ifdef IOKITDEBUG
47#define DEBUG_INIT_VALUE IOKITDEBUG
48#else
49#define DEBUG_INIT_VALUE 0
50#endif
51
52SInt64		gIOKitDebug = DEBUG_INIT_VALUE;
53SYSCTL_QUAD(_debug, OID_AUTO, iokit, CTLFLAG_RW, &gIOKitDebug, "boot_arg io");
54
55
56int 		debug_malloc_size;
57int		debug_iomalloc_size;
58
59vm_size_t	debug_iomallocpageable_size;
60int 		debug_container_malloc_size;
61// int 		debug_ivars_size; // in OSObject.cpp
62
63extern "C" {
64
65
66void IOPrintPlane( const IORegistryPlane * plane )
67{
68    IORegistryEntry *		next;
69    IORegistryIterator * 	iter;
70    OSOrderedSet *		all;
71    char			format[] = "%xxxs";
72    IOService *			service;
73
74    iter = IORegistryIterator::iterateOver( plane );
75    assert( iter );
76    all = iter->iterateAll();
77    if( all) {
78        IOLog("Count %d\n", all->getCount() );
79        all->release();
80    } else
81	IOLog("Empty\n");
82
83    iter->reset();
84    while( (next = iter->getNextObjectRecursive())) {
85	snprintf(format + 1, sizeof(format) - 1, "%ds", 2 * next->getDepth( plane ));
86	IOLog( format, "");
87	IOLog( "\033[33m%s", next->getName( plane ));
88	if( (next->getLocation( plane )))
89            IOLog("@%s", next->getLocation( plane ));
90	IOLog("\033[0m <class %s", next->getMetaClass()->getClassName());
91        if( (service = OSDynamicCast(IOService, next)))
92            IOLog(", busy %ld", service->getBusyState());
93	IOLog( ">\n");
94	IOSleep(250);
95    }
96    iter->release();
97}
98
99void dbugprintf(char *fmt, ...);
100void db_dumpiojunk( const IORegistryPlane * plane );
101
102void db_piokjunk(void) {
103
104	dbugprintf("\nDT plane:\n");
105	db_dumpiojunk( gIODTPlane );
106	dbugprintf("\n\nService plane:\n");
107	db_dumpiojunk( gIOServicePlane );
108    dbugprintf("\n\n"
109	    "ivar kalloc()       0x%08x\n"
110	    "malloc()            0x%08x\n"
111            "containers kalloc() 0x%08x\n"
112	    "IOMalloc()          0x%08x\n"
113            "----------------------------------------\n",
114	    debug_ivars_size,
115            debug_malloc_size,
116            debug_container_malloc_size,
117            debug_iomalloc_size
118            );
119
120}
121
122
123void db_dumpiojunk( const IORegistryPlane * plane )
124{
125    IORegistryEntry *		next;
126    IORegistryIterator * 	iter;
127    OSOrderedSet *		all;
128    char			format[] = "%xxxs";
129    IOService *			service;
130
131    iter = IORegistryIterator::iterateOver( plane );
132
133    all = iter->iterateAll();
134    if( all) {
135        dbugprintf("Count %d\n", all->getCount() );
136        all->release();
137    } else dbugprintf("Empty\n");
138
139    iter->reset();
140    while( (next = iter->getNextObjectRecursive())) {
141		snprintf(format + 1, sizeof(format) - 1, "%ds", 2 * next->getDepth( plane ));
142		dbugprintf( format, "");
143		dbugprintf( "%s", next->getName( plane ));
144		if( (next->getLocation( plane )))
145				dbugprintf("@%s", next->getLocation( plane ));
146		dbugprintf(" <class %s", next->getMetaClass()->getClassName());
147			if( (service = OSDynamicCast(IOService, next)))
148				dbugprintf(", busy %ld", service->getBusyState());
149		dbugprintf( ">\n");
150    }
151    iter->release();
152}
153
154void IOPrintMemory( void )
155{
156
157//    OSMetaClass::printInstanceCounts();
158
159    IOLog("\n"
160	    "ivar kalloc()       0x%08x\n"
161	    "malloc()            0x%08x\n"
162            "containers kalloc() 0x%08x\n"
163	    "IOMalloc()          0x%08x\n"
164            "----------------------------------------\n",
165	    debug_ivars_size,
166            debug_malloc_size,
167            debug_container_malloc_size,
168            debug_iomalloc_size
169            );
170}
171
172} /* extern "C" */
173
174/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
175
176#define super OSObject
177OSDefineMetaClassAndStructors(IOKitDiagnostics, OSObject)
178
179/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
180
181OSObject * IOKitDiagnostics::diagnostics( void )
182{
183    IOKitDiagnostics * diags;
184
185    diags = new IOKitDiagnostics;
186    if( diags && !diags->init()) {
187	diags->release();
188	diags = 0;
189    }
190
191    return( diags );
192}
193
194void IOKitDiagnostics::updateOffset( OSDictionary * dict,
195			UInt32 value, const char * name )
196{
197    OSNumber * off;
198
199    off = OSNumber::withNumber( value, 32 );
200    if( !off)
201	return;
202
203    dict->setObject( name, off );
204    off->release();
205}
206
207bool IOKitDiagnostics::serialize(OSSerialize *s) const
208{
209    OSDictionary * 	dict;
210    bool		ok;
211
212    dict = OSDictionary::withCapacity( 5 );
213    if( !dict)
214	return( false );
215
216    updateOffset( dict, debug_ivars_size, "Instance allocation" );
217    updateOffset( dict, debug_container_malloc_size, "Container allocation" );
218    updateOffset( dict, debug_iomalloc_size, "IOMalloc allocation" );
219    updateOffset( dict, debug_iomallocpageable_size, "Pageable allocation" );
220
221    OSMetaClass::serializeClassDictionary(dict);
222
223    ok = dict->serialize( s );
224
225    dict->release();
226
227    return( ok );
228}
229
230/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
231