xref: /xnu-10063.141.1/iokit/Kernel/IOReportLegend.cpp (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions /*
2*d8b80295SApple OSS Distributions  * Copyright (c) 2012-2013 Apple Computer, Inc.  All Rights Reserved.
3*d8b80295SApple OSS Distributions  *
4*d8b80295SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*d8b80295SApple OSS Distributions  *
6*d8b80295SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*d8b80295SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*d8b80295SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*d8b80295SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*d8b80295SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*d8b80295SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*d8b80295SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*d8b80295SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*d8b80295SApple OSS Distributions  *
15*d8b80295SApple OSS Distributions  * Please obtain a copy of the License at
16*d8b80295SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*d8b80295SApple OSS Distributions  *
18*d8b80295SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*d8b80295SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*d8b80295SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*d8b80295SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*d8b80295SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*d8b80295SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*d8b80295SApple OSS Distributions  * limitations under the License.
25*d8b80295SApple OSS Distributions  *
26*d8b80295SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*d8b80295SApple OSS Distributions  */
28*d8b80295SApple OSS Distributions 
29*d8b80295SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30*d8b80295SApple OSS Distributions 
31*d8b80295SApple OSS Distributions #include <IOKit/IOKernelReportStructs.h>
32*d8b80295SApple OSS Distributions #include <IOKit/IOKernelReporters.h>
33*d8b80295SApple OSS Distributions 
34*d8b80295SApple OSS Distributions 
35*d8b80295SApple OSS Distributions //#define IORDEBUG_LEGEND 1
36*d8b80295SApple OSS Distributions 
37*d8b80295SApple OSS Distributions #ifdef IORDEBUG_LEGEND
38*d8b80295SApple OSS Distributions     #define IORLEGENDLOG(fmt, args...)      \
39*d8b80295SApple OSS Distributions     do {                                    \
40*d8b80295SApple OSS Distributions 	IOLog("IOReportLegend | ");         \
41*d8b80295SApple OSS Distributions 	IOLog(fmt, ##args);                 \
42*d8b80295SApple OSS Distributions 	IOLog("\n");                        \
43*d8b80295SApple OSS Distributions     } while(0)
44*d8b80295SApple OSS Distributions #else
45*d8b80295SApple OSS Distributions     #define IORLEGENDLOG(fmt, args...)
46*d8b80295SApple OSS Distributions #endif
47*d8b80295SApple OSS Distributions 
48*d8b80295SApple OSS Distributions 
49*d8b80295SApple OSS Distributions #define super OSObject
50*d8b80295SApple OSS Distributions OSDefineMetaClassAndStructors(IOReportLegend, OSObject);
51*d8b80295SApple OSS Distributions 
52*d8b80295SApple OSS Distributions OSSharedPtr<IOReportLegend>
with(OSArray * legend)53*d8b80295SApple OSS Distributions IOReportLegend::with(OSArray *legend)
54*d8b80295SApple OSS Distributions {
55*d8b80295SApple OSS Distributions 	OSSharedPtr<IOReportLegend> iorLegend = OSMakeShared<IOReportLegend>();
56*d8b80295SApple OSS Distributions 
57*d8b80295SApple OSS Distributions 	if (iorLegend) {
58*d8b80295SApple OSS Distributions 		if (legend != NULL) {
59*d8b80295SApple OSS Distributions 			if (iorLegend->initWith(legend) != kIOReturnSuccess) {
60*d8b80295SApple OSS Distributions 				return nullptr;
61*d8b80295SApple OSS Distributions 			}
62*d8b80295SApple OSS Distributions 		}
63*d8b80295SApple OSS Distributions 
64*d8b80295SApple OSS Distributions 		return iorLegend;
65*d8b80295SApple OSS Distributions 	} else {
66*d8b80295SApple OSS Distributions 		return nullptr;
67*d8b80295SApple OSS Distributions 	}
68*d8b80295SApple OSS Distributions }
69*d8b80295SApple OSS Distributions 
70*d8b80295SApple OSS Distributions /* must clean up everything if it fails */
71*d8b80295SApple OSS Distributions IOReturn
initWith(OSArray * legend)72*d8b80295SApple OSS Distributions IOReportLegend::initWith(OSArray *legend)
73*d8b80295SApple OSS Distributions {
74*d8b80295SApple OSS Distributions 	if (legend) {
75*d8b80295SApple OSS Distributions 		_reportLegend = OSArray::withArray(legend);
76*d8b80295SApple OSS Distributions 	}
77*d8b80295SApple OSS Distributions 
78*d8b80295SApple OSS Distributions 	if (_reportLegend == NULL) {
79*d8b80295SApple OSS Distributions 		return kIOReturnError;
80*d8b80295SApple OSS Distributions 	} else {
81*d8b80295SApple OSS Distributions 		return kIOReturnSuccess;
82*d8b80295SApple OSS Distributions 	}
83*d8b80295SApple OSS Distributions }
84*d8b80295SApple OSS Distributions 
85*d8b80295SApple OSS Distributions 
86*d8b80295SApple OSS Distributions void
free(void)87*d8b80295SApple OSS Distributions IOReportLegend::free(void)
88*d8b80295SApple OSS Distributions {
89*d8b80295SApple OSS Distributions 	super::free();
90*d8b80295SApple OSS Distributions }
91*d8b80295SApple OSS Distributions 
92*d8b80295SApple OSS Distributions 
93*d8b80295SApple OSS Distributions OSArray*
getLegend(void)94*d8b80295SApple OSS Distributions IOReportLegend::getLegend(void)
95*d8b80295SApple OSS Distributions {
96*d8b80295SApple OSS Distributions 	return _reportLegend.get();
97*d8b80295SApple OSS Distributions }
98*d8b80295SApple OSS Distributions 
99*d8b80295SApple OSS Distributions IOReturn
addReporterLegend(IOService * reportingService,IOReporter * reporter,const char * groupName,const char * subGroupName)100*d8b80295SApple OSS Distributions IOReportLegend::addReporterLegend(IOService *reportingService,
101*d8b80295SApple OSS Distributions     IOReporter *reporter,
102*d8b80295SApple OSS Distributions     const char *groupName,
103*d8b80295SApple OSS Distributions     const char *subGroupName)
104*d8b80295SApple OSS Distributions {
105*d8b80295SApple OSS Distributions 	IOReturn res = kIOReturnError;
106*d8b80295SApple OSS Distributions 	OSSharedPtr<IOReportLegend> legend;
107*d8b80295SApple OSS Distributions 	OSSharedPtr<OSObject> curLegend;
108*d8b80295SApple OSS Distributions 
109*d8b80295SApple OSS Distributions 	// No need to check groupName and subGroupName because optional params
110*d8b80295SApple OSS Distributions 	if (!reportingService || !reporter) {
111*d8b80295SApple OSS Distributions 		goto finish;
112*d8b80295SApple OSS Distributions 	}
113*d8b80295SApple OSS Distributions 
114*d8b80295SApple OSS Distributions 	// It's fine if the legend doesn't exist (IOReportLegend::with(NULL)
115*d8b80295SApple OSS Distributions 	// is how you make an empty legend).  If it's not an array, then
116*d8b80295SApple OSS Distributions 	// we're just going to replace it.
117*d8b80295SApple OSS Distributions 	curLegend = reportingService->copyProperty(kIOReportLegendKey);
118*d8b80295SApple OSS Distributions 	legend = IOReportLegend::with(OSDynamicCast(OSArray, curLegend.get()));
119*d8b80295SApple OSS Distributions 	if (!legend) {
120*d8b80295SApple OSS Distributions 		goto finish;
121*d8b80295SApple OSS Distributions 	}
122*d8b80295SApple OSS Distributions 
123*d8b80295SApple OSS Distributions 	// Add the reporter's entries and update the service property.
124*d8b80295SApple OSS Distributions 	// The overwrite triggers a release of the old legend array.
125*d8b80295SApple OSS Distributions 	legend->addReporterLegend(reporter, groupName, subGroupName);
126*d8b80295SApple OSS Distributions 	reportingService->setProperty(kIOReportLegendKey, legend->getLegend());
127*d8b80295SApple OSS Distributions 	reportingService->setProperty(kIOReportLegendPublicKey, true);
128*d8b80295SApple OSS Distributions 
129*d8b80295SApple OSS Distributions 	res = kIOReturnSuccess;
130*d8b80295SApple OSS Distributions 
131*d8b80295SApple OSS Distributions finish:
132*d8b80295SApple OSS Distributions 	return res;
133*d8b80295SApple OSS Distributions }
134*d8b80295SApple OSS Distributions 
135*d8b80295SApple OSS Distributions 
136*d8b80295SApple OSS Distributions IOReturn
addLegendEntry(IOReportLegendEntry * legendEntry,const char * groupName,const char * subGroupName)137*d8b80295SApple OSS Distributions IOReportLegend::addLegendEntry(IOReportLegendEntry *legendEntry,
138*d8b80295SApple OSS Distributions     const char *groupName,
139*d8b80295SApple OSS Distributions     const char *subGroupName)
140*d8b80295SApple OSS Distributions {
141*d8b80295SApple OSS Distributions 	kern_return_t res = kIOReturnError;
142*d8b80295SApple OSS Distributions 	OSSharedPtr<const OSSymbol> tmpGroupName;
143*d8b80295SApple OSS Distributions 	OSSharedPtr<const OSSymbol> tmpSubGroupName;
144*d8b80295SApple OSS Distributions 
145*d8b80295SApple OSS Distributions 	if (!legendEntry) {
146*d8b80295SApple OSS Distributions 		return res;
147*d8b80295SApple OSS Distributions 	}
148*d8b80295SApple OSS Distributions 
149*d8b80295SApple OSS Distributions 	if (groupName) {
150*d8b80295SApple OSS Distributions 		tmpGroupName = OSSymbol::withCString(groupName);
151*d8b80295SApple OSS Distributions 	}
152*d8b80295SApple OSS Distributions 
153*d8b80295SApple OSS Distributions 	if (subGroupName) {
154*d8b80295SApple OSS Distributions 		tmpSubGroupName = OSSymbol::withCString(subGroupName);
155*d8b80295SApple OSS Distributions 	}
156*d8b80295SApple OSS Distributions 
157*d8b80295SApple OSS Distributions 	// It is ok to call appendLegendWith() if tmpGroups are NULL
158*d8b80295SApple OSS Distributions 	res = organizeLegend(legendEntry, tmpGroupName.get(), tmpSubGroupName.get());
159*d8b80295SApple OSS Distributions 
160*d8b80295SApple OSS Distributions 	return res;
161*d8b80295SApple OSS Distributions }
162*d8b80295SApple OSS Distributions 
163*d8b80295SApple OSS Distributions 
164*d8b80295SApple OSS Distributions IOReturn
addReporterLegend(IOReporter * reporter,const char * groupName,const char * subGroupName)165*d8b80295SApple OSS Distributions IOReportLegend::addReporterLegend(IOReporter *reporter,
166*d8b80295SApple OSS Distributions     const char *groupName,
167*d8b80295SApple OSS Distributions     const char *subGroupName)
168*d8b80295SApple OSS Distributions {
169*d8b80295SApple OSS Distributions 	IOReturn res = kIOReturnError;
170*d8b80295SApple OSS Distributions 	OSSharedPtr<IOReportLegendEntry> legendEntry;
171*d8b80295SApple OSS Distributions 
172*d8b80295SApple OSS Distributions 	if (reporter) {
173*d8b80295SApple OSS Distributions 		legendEntry = reporter->createLegend();
174*d8b80295SApple OSS Distributions 
175*d8b80295SApple OSS Distributions 		if (legendEntry) {
176*d8b80295SApple OSS Distributions 			res = addLegendEntry(legendEntry.get(), groupName, subGroupName);
177*d8b80295SApple OSS Distributions 		}
178*d8b80295SApple OSS Distributions 	}
179*d8b80295SApple OSS Distributions 
180*d8b80295SApple OSS Distributions 	return res;
181*d8b80295SApple OSS Distributions }
182*d8b80295SApple OSS Distributions 
183*d8b80295SApple OSS Distributions 
184*d8b80295SApple OSS Distributions IOReturn
organizeLegend(IOReportLegendEntry * legendEntry,const OSSymbol * groupName,const OSSymbol * subGroupName)185*d8b80295SApple OSS Distributions IOReportLegend::organizeLegend(IOReportLegendEntry *legendEntry,
186*d8b80295SApple OSS Distributions     const OSSymbol *groupName,
187*d8b80295SApple OSS Distributions     const OSSymbol *subGroupName)
188*d8b80295SApple OSS Distributions {
189*d8b80295SApple OSS Distributions 	if (!legendEntry) {
190*d8b80295SApple OSS Distributions 		return kIOReturnBadArgument;
191*d8b80295SApple OSS Distributions 	}
192*d8b80295SApple OSS Distributions 
193*d8b80295SApple OSS Distributions 	if (!groupName && subGroupName) {
194*d8b80295SApple OSS Distributions 		return kIOReturnBadArgument;
195*d8b80295SApple OSS Distributions 	}
196*d8b80295SApple OSS Distributions 
197*d8b80295SApple OSS Distributions 	IORLEGENDLOG("IOReportLegend::organizeLegend");
198*d8b80295SApple OSS Distributions 	// Legend is empty, enter first node
199*d8b80295SApple OSS Distributions 	if (_reportLegend == NULL) {
200*d8b80295SApple OSS Distributions 		IORLEGENDLOG("IOReportLegend::new legend creation");
201*d8b80295SApple OSS Distributions 		_reportLegend = OSArray::withCapacity(1);
202*d8b80295SApple OSS Distributions 
203*d8b80295SApple OSS Distributions 		if (!_reportLegend) {
204*d8b80295SApple OSS Distributions 			return kIOReturnNoMemory;
205*d8b80295SApple OSS Distributions 		}
206*d8b80295SApple OSS Distributions 	}
207*d8b80295SApple OSS Distributions 
208*d8b80295SApple OSS Distributions 	if (groupName) {
209*d8b80295SApple OSS Distributions 		legendEntry->setObject(kIOReportLegendGroupNameKey, groupName);
210*d8b80295SApple OSS Distributions 	}
211*d8b80295SApple OSS Distributions 
212*d8b80295SApple OSS Distributions 	if (subGroupName) {
213*d8b80295SApple OSS Distributions 		legendEntry->setObject(kIOReportLegendSubGroupNameKey, subGroupName);
214*d8b80295SApple OSS Distributions 	}
215*d8b80295SApple OSS Distributions 
216*d8b80295SApple OSS Distributions 	_reportLegend->setObject(legendEntry);
217*d8b80295SApple OSS Distributions 
218*d8b80295SApple OSS Distributions 	// callers can now safely release legendEntry (it is part of _reportLegend)
219*d8b80295SApple OSS Distributions 
220*d8b80295SApple OSS Distributions 	return kIOReturnSuccess;
221*d8b80295SApple OSS Distributions }
222