1/* Copyright (c) 1997,2011,2014 Apple Inc.
2 *
3 * comDebug.h
4 */
5
6#ifndef	_COM_DEBUG_H_
7#define _COM_DEBUG_H_
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13/*
14 * enable general debugging printfs and error checking.
15 */
16#define COM_DEBUG				0
17#if		COM_DEBUG
18#include <stdio.h>
19
20#define	ddprintf(x)				printf x
21#else
22#define	ddprintf(x)
23#endif
24
25/*
26 * block parsing debug
27 */
28#define COM_SCAN_DEBUG			0
29#if		COM_SCAN_DEBUG
30#define	scprintf(x)				printf x
31#else
32#define	scprintf(x)
33#endif
34
35/*
36 * 2nd-level comcrypt debug
37 */
38#define LEVEL2_DEBUG			0
39#if		LEVEL2_DEBUG
40#include <stdio.h>
41
42#define	l2printf(x)				printf x
43#else
44#define	l2printf(x)
45#endif
46
47/*
48 * lookahead queue debug
49 */
50#define COM_LA_DEBUG				0
51#define COM_LA_PRINTF				0
52#if		COM_LA_PRINTF
53#define	laprintf(x)					printf x
54#else
55#define	laprintf(x)
56#endif
57
58/*
59 * Statistics measurements. This is a private API.
60 */
61#if		COM_DEBUG
62#define	COM_STATS		0
63#else
64#define	COM_STATS		0
65#endif
66
67#if		COM_STATS
68
69/*
70 * Info obtained via a call to getComStats()
71 */
72typedef struct {
73	unsigned	level1blocks;
74	unsigned	plaintextBytes;
75	unsigned	ciphertextBytes;
76	unsigned	oneByteFrags;			// 1st level only
77	unsigned	twoByteFrags;			// ditto
78	unsigned	level2oneByteFrags;		// second level only
79	unsigned	level2twoByteFrags;		// ditto
80	unsigned	level2byteCode;			// bytes, pre-encrypted
81	unsigned	level2cipherText;		// bytes, post-encrypt
82	unsigned	level2blocks;			// 2nd-level blocks
83	unsigned 	level2jmatch;			// total jmatch (at first level) of
84										//     2nd level blocks
85} comStats;
86
87extern comStats _comStats;
88#define	incrComStat(stat, num)	_comStats.stat += num;
89
90#define incr1byteFrags(recursLevel)		{		\
91	if(recursLevel == 1) {						\
92		incrComStat(level2oneByteFrags, 1);		\
93	}											\
94	else {										\
95		incrComStat(oneByteFrags, 1);			\
96	}											\
97}
98#define incr2byteFrags(recursLevel)		{		\
99	if(recursLevel == 1) {						\
100		incrComStat(level2twoByteFrags, 1);		\
101	}											\
102	else {										\
103		incrComStat(twoByteFrags, 1);			\
104	}											\
105}
106
107extern void resetComStats();
108extern void getComStats(comStats *stats);
109
110#else
111#define	incrComStat(stat, num)
112#define incr1byteFrags(recursLevel)
113#define incr2byteFrags(recursLevel)
114#endif
115
116/*
117 * Profiling measurement. A private API when enabled.
118 */
119#if		COM_DEBUG
120#define COM_PROFILE			0
121#else
122#define	COM_PROFILE			0
123#endif
124
125#if		COM_PROFILE
126
127#include <kern/time_stamp.h>
128
129/*
130 * Global profiling enable. It turns out the the cost of doing the
131 * kern_timestamp() call twice per codeword is way more expensive
132 * than the actual comcryption code. Setting this variable to zero
133 * avoids the cost of all the timestamps for reference without
134 * rebuilding. Also, the cmcPerWordOhead calibrates the actual
135 * cost of the two kern_timestamp() calls per word.
136 */
137extern unsigned comProfEnable;
138
139/*
140 * Profiling accumulators.
141 */
142typedef	unsigned comprof_t;
143
144extern comprof_t cmcTotal;
145extern comprof_t cmcQueSearch;
146extern comprof_t cmcQueMatchMove;
147extern comprof_t cmcQueMissMove;
148extern comprof_t cmcPerWordOhead;
149extern comprof_t cmcLevel2;
150
151
152/*
153 * Place one of these in the local variable declaration list of each routine
154 * which will do profiling.
155 */
156#define COMPROF_LOCALS					\
157	struct tsval _profStartTime;		\
158	struct tsval _profEndTime;
159
160/*
161 * Start the clock.
162 */
163#define COMPROF_START 						\
164	if(comProfEnable) {						\
165		kern_timestamp(&_profStartTime);	\
166	}
167
168/*
169 * Stop the clock and gather elapsed time to specified accumulator.
170 */
171#define COMPROF_END(accum)											\
172	if(comProfEnable) {												\
173		kern_timestamp(&_profEndTime);								\
174		accum += (_profEndTime.low_val - _profStartTime.low_val);	\
175	}
176
177
178#else
179
180#define	COMPROF_LOCALS
181#define COMPROF_START
182#define COMPROF_END(accum)
183
184#endif	/* COM_PROFILE */
185
186/*
187 * Get/set parameter API, private, for debug only.
188 */
189#if		COM_DEBUG
190#define	COM_PARAM_ENABLE	1
191#else
192#define	COM_PARAM_ENABLE	0
193#endif	/*COM_DEBUG*/
194
195#if		COM_PARAM_ENABLE
196
197extern unsigned	getF1(comcryptObj cobj);
198extern void	setF1(comcryptObj cobj, unsigned f1);
199extern unsigned	getF2(comcryptObj cobj);
200extern void	setF2(comcryptObj cobj, unsigned f2);
201extern unsigned	getJmatchThresh(comcryptObj cobj);
202extern void	setJmatchThresh(comcryptObj cobj, unsigned jmatchThresh);
203extern unsigned	getMinByteCode(comcryptObj cobj);
204extern void	setMinByteCode(comcryptObj cobj, unsigned minByteCode);
205
206#endif	/*COM_PARAM_ENABLE*/
207
208#ifdef __cplusplus
209}
210#endif
211
212#endif	/*_COM_DEBUG_H_*/
213