xref: /xnu-10002.1.13/tools/trace/kqtrace.lua (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions#!/usr/local/bin/luatrace -s
2*1031c584SApple OSS Distributions
3*1031c584SApple OSS Distributionstrace_eventname = function(codename, callback)
4*1031c584SApple OSS Distributions	local debugid = trace.debugid(codename)
5*1031c584SApple OSS Distributions	if debugid ~= 0 then
6*1031c584SApple OSS Distributions		trace.single(debugid,callback)
7*1031c584SApple OSS Distributions	else
8*1031c584SApple OSS Distributions		printf("WARNING: Cannot locate debugid for '%s'\n", codename)
9*1031c584SApple OSS Distributions	end
10*1031c584SApple OSS Distributionsend
11*1031c584SApple OSS Distributions
12*1031c584SApple OSS Distributionsinitial_timestamp = 0
13*1031c584SApple OSS Distributions
14*1031c584SApple OSS Distributionsfunction event_prefix_string(buf, workq)
15*1031c584SApple OSS Distributions	if initial_timestamp == 0 then
16*1031c584SApple OSS Distributions		initial_timestamp = buf.timestamp
17*1031c584SApple OSS Distributions	end
18*1031c584SApple OSS Distributions	local secs = trace.convert_timestamp_to_nanoseconds(buf.timestamp - initial_timestamp) / 1000000000
19*1031c584SApple OSS Distributions
20*1031c584SApple OSS Distributions	local type
21*1031c584SApple OSS Distributions	if trace.debugid_is_start(buf.debugid) then
22*1031c584SApple OSS Distributions		type = "→"
23*1031c584SApple OSS Distributions	elseif trace.debugid_is_end(buf.debugid) then
24*1031c584SApple OSS Distributions		type = "←"
25*1031c584SApple OSS Distributions	else
26*1031c584SApple OSS Distributions		type = "↔"
27*1031c584SApple OSS Distributions	end
28*1031c584SApple OSS Distributions
29*1031c584SApple OSS Distributions	proc = buf.command
30*1031c584SApple OSS Distributions
31*1031c584SApple OSS Distributions	local prefix = string.format("%s %6.9f %-17s [%05d.%06x] %-28s\t",
32*1031c584SApple OSS Distributions		type, secs, proc, buf.pid, buf.threadid, buf.debugname)
33*1031c584SApple OSS Distributions	if not workq then
34*1031c584SApple OSS Distributions		prefix = prefix .. string.format(" 0x%16x", buf.arg1)
35*1031c584SApple OSS Distributions	end
36*1031c584SApple OSS Distributions
37*1031c584SApple OSS Distributions	return prefix
38*1031c584SApple OSS Distributionsend
39*1031c584SApple OSS Distributions
40*1031c584SApple OSS Distributionsfunction qos_string(qos)
41*1031c584SApple OSS Distributions	if qos == 0 then
42*1031c584SApple OSS Distributions		return "--"
43*1031c584SApple OSS Distributions	elseif qos == 1 then
44*1031c584SApple OSS Distributions		return "MT"
45*1031c584SApple OSS Distributions	elseif qos == 2 then
46*1031c584SApple OSS Distributions		return "BG"
47*1031c584SApple OSS Distributions	elseif qos == 3 then
48*1031c584SApple OSS Distributions		return "UT"
49*1031c584SApple OSS Distributions	elseif qos == 4 then
50*1031c584SApple OSS Distributions		return "DF"
51*1031c584SApple OSS Distributions	elseif qos == 5 then
52*1031c584SApple OSS Distributions		return "IN"
53*1031c584SApple OSS Distributions	elseif qos == 6 then
54*1031c584SApple OSS Distributions		return "UI"
55*1031c584SApple OSS Distributions	elseif qos == 7 then
56*1031c584SApple OSS Distributions		return "MG"
57*1031c584SApple OSS Distributions	else
58*1031c584SApple OSS Distributions		return string.format("??[0x%x]", qos)
59*1031c584SApple OSS Distributions	end
60*1031c584SApple OSS Distributionsend
61*1031c584SApple OSS Distributions
62*1031c584SApple OSS Distributionsfunction state_string(strings, state)
63*1031c584SApple OSS Distributions	local str = ''
64*1031c584SApple OSS Distributions	local first = true
65*1031c584SApple OSS Distributions	for name, bit in pairs(strings) do
66*1031c584SApple OSS Distributions		if (state & bit) == bit then
67*1031c584SApple OSS Distributions			if not first then
68*1031c584SApple OSS Distributions				str = str .. ' '
69*1031c584SApple OSS Distributions			end
70*1031c584SApple OSS Distributions			str = str .. name
71*1031c584SApple OSS Distributions			first = false
72*1031c584SApple OSS Distributions		end
73*1031c584SApple OSS Distributions	end
74*1031c584SApple OSS Distributions	return str
75*1031c584SApple OSS Distributionsend
76*1031c584SApple OSS Distributions
77*1031c584SApple OSS Distributionskqrequest_state_strings = {
78*1031c584SApple OSS Distributions	['WAKEUP'] = 1,
79*1031c584SApple OSS Distributions}
80*1031c584SApple OSS Distributions
81*1031c584SApple OSS Distributionskqueue_state_strings = {
82*1031c584SApple OSS Distributions	['SEL'] = 0x001,
83*1031c584SApple OSS Distributions	['SLEEP'] = 0x002,
84*1031c584SApple OSS Distributions	['PROCWAIT'] = 0x004,
85*1031c584SApple OSS Distributions	['KEV32'] = 0x008,
86*1031c584SApple OSS Distributions	['KEV64'] = 0x010,
87*1031c584SApple OSS Distributions	['KEV_QOS'] = 0x020,
88*1031c584SApple OSS Distributions	['WORKQ'] = 0x040,
89*1031c584SApple OSS Distributions	['WORKLOOP'] = 0x080,
90*1031c584SApple OSS Distributions	['PROCESSING'] = 0x100,
91*1031c584SApple OSS Distributions	['DRAIN'] = 0x200,
92*1031c584SApple OSS Distributions	['WAKEUP'] = 0x400,
93*1031c584SApple OSS Distributions	['DYNAMIC'] = 0x800,
94*1031c584SApple OSS Distributions	['R2K'] = 0x1000,
95*1031c584SApple OSS Distributions	['TURNSTILE'] = 0x2000,
96*1031c584SApple OSS Distributions}
97*1031c584SApple OSS Distributions
98*1031c584SApple OSS Distributionsknote_state_strings = {
99*1031c584SApple OSS Distributions	['ACTIVE'] = 0x0001,
100*1031c584SApple OSS Distributions	['QUEUED'] = 0x0002,
101*1031c584SApple OSS Distributions	['DISABLED'] = 0x0004,
102*1031c584SApple OSS Distributions	['DROPPING'] = 0x0008,
103*1031c584SApple OSS Distributions	['LOCKED'] = 0x0010,
104*1031c584SApple OSS Distributions	['POSTING'] = 0x0020,
105*1031c584SApple OSS Distributions	['STAYACTIVE'] = 0x0040,
106*1031c584SApple OSS Distributions	['DEFERDELETE'] = 0x0080,
107*1031c584SApple OSS Distributions	['MERGE_QOS'] = 0x0100,
108*1031c584SApple OSS Distributions	['REQVANISH'] = 0x0200,
109*1031c584SApple OSS Distributions	['VANISHED'] = 0x0400,
110*1031c584SApple OSS Distributions	['SUPPRESSED'] = 0x0800,
111*1031c584SApple OSS Distributions}
112*1031c584SApple OSS Distributions
113*1031c584SApple OSS Distributionskevent_flags_strings = {
114*1031c584SApple OSS Distributions	['ADD'] = 0x0001,
115*1031c584SApple OSS Distributions	['DELETE'] = 0x0002,
116*1031c584SApple OSS Distributions	['ENABLE'] = 0x0004,
117*1031c584SApple OSS Distributions	['DISABLE'] = 0x0008,
118*1031c584SApple OSS Distributions	['ONESHOT'] = 0x0010,
119*1031c584SApple OSS Distributions	['CLEAR'] = 0x0020,
120*1031c584SApple OSS Distributions	['RECEIPT'] = 0x0040,
121*1031c584SApple OSS Distributions	['DISPATCH'] = 0x0080,
122*1031c584SApple OSS Distributions	['UDATA_SPECIFIC'] = 0x0100,
123*1031c584SApple OSS Distributions	['VANISHED'] = 0x0200,
124*1031c584SApple OSS Distributions	['FLAG0'] = 0x1000,
125*1031c584SApple OSS Distributions	['FLAG1'] = 0x2000,
126*1031c584SApple OSS Distributions	['EOF'] = 0x8000,
127*1031c584SApple OSS Distributions	['ERROR'] = 0x4000,
128*1031c584SApple OSS Distributions}
129*1031c584SApple OSS Distributions
130*1031c584SApple OSS Distributionsfunction kevent_filter_string(filt)
131*1031c584SApple OSS Distributions	if filt == -1 then
132*1031c584SApple OSS Distributions		return 'READ'
133*1031c584SApple OSS Distributions	elseif filt == -2 then
134*1031c584SApple OSS Distributions		return 'WRITE'
135*1031c584SApple OSS Distributions	elseif filt == -3 then
136*1031c584SApple OSS Distributions		return 'AIO'
137*1031c584SApple OSS Distributions	elseif filt == -4 then
138*1031c584SApple OSS Distributions		return 'VNODE'
139*1031c584SApple OSS Distributions	elseif filt == -5 then
140*1031c584SApple OSS Distributions		return 'PROC'
141*1031c584SApple OSS Distributions	elseif filt == -6 then
142*1031c584SApple OSS Distributions		return 'SIGNAL'
143*1031c584SApple OSS Distributions	elseif filt == -7 then
144*1031c584SApple OSS Distributions		return 'TIMER'
145*1031c584SApple OSS Distributions	elseif filt == -8 then
146*1031c584SApple OSS Distributions		return 'MACHPORT'
147*1031c584SApple OSS Distributions	elseif filt == -9 then
148*1031c584SApple OSS Distributions		return 'FS'
149*1031c584SApple OSS Distributions	elseif filt == -10 then
150*1031c584SApple OSS Distributions		return 'USER'
151*1031c584SApple OSS Distributions	-- -11 unused
152*1031c584SApple OSS Distributions	elseif filt == -12 then
153*1031c584SApple OSS Distributions		return 'VM'
154*1031c584SApple OSS Distributions	elseif filt == -13 then
155*1031c584SApple OSS Distributions		return 'SOCK'
156*1031c584SApple OSS Distributions	elseif filt == -14 then
157*1031c584SApple OSS Distributions		return 'MEMORYSTATUS'
158*1031c584SApple OSS Distributions	elseif filt == -15 then
159*1031c584SApple OSS Distributions		return 'EXCEPT'
160*1031c584SApple OSS Distributions	elseif filt == -16 then
161*1031c584SApple OSS Distributions		return 'NW_CHANNEL'
162*1031c584SApple OSS Distributions	elseif filt == -17 then
163*1031c584SApple OSS Distributions		return 'WORKLOOP'
164*1031c584SApple OSS Distributions
165*1031c584SApple OSS Distributions	elseif filt == 17 then
166*1031c584SApple OSS Distributions		return 'KQREAD'
167*1031c584SApple OSS Distributions	elseif filt == 18 then
168*1031c584SApple OSS Distributions		return 'PIPE_N'
169*1031c584SApple OSS Distributions	elseif filt == 19 then
170*1031c584SApple OSS Distributions		return 'PIPE_R'
171*1031c584SApple OSS Distributions	elseif filt == 20 then
172*1031c584SApple OSS Distributions		return 'PIPE_W'
173*1031c584SApple OSS Distributions	elseif filt == 21 then
174*1031c584SApple OSS Distributions		return 'PTSD'
175*1031c584SApple OSS Distributions	elseif filt == 22 then
176*1031c584SApple OSS Distributions		return 'SOREAD'
177*1031c584SApple OSS Distributions	elseif filt == 23 then
178*1031c584SApple OSS Distributions		return 'SOWRITE'
179*1031c584SApple OSS Distributions	elseif filt == 24 then
180*1031c584SApple OSS Distributions		return 'SCK'
181*1031c584SApple OSS Distributions	elseif filt == 25 then
182*1031c584SApple OSS Distributions		return 'SOEXCEPT'
183*1031c584SApple OSS Distributions	elseif filt == 26 then
184*1031c584SApple OSS Distributions		return 'SPEC'
185*1031c584SApple OSS Distributions	elseif filt == 27 then
186*1031c584SApple OSS Distributions		return 'BPFREAD'
187*1031c584SApple OSS Distributions	elseif filt == 28 then
188*1031c584SApple OSS Distributions		return 'NECP_FD'
189*1031c584SApple OSS Distributions	elseif filt == 29 then
190*1031c584SApple OSS Distributions		return 'SKYWALK_CHANNEL_W'
191*1031c584SApple OSS Distributions	elseif filt == 30 then
192*1031c584SApple OSS Distributions		return 'SKYWALK_CHANNEL_R'
193*1031c584SApple OSS Distributions	elseif filt == 31 then
194*1031c584SApple OSS Distributions		return 'SKYWALK_CHANNEL_E'
195*1031c584SApple OSS Distributions	elseif filt == 32 then
196*1031c584SApple OSS Distributions		return 'FSEVENT'
197*1031c584SApple OSS Distributions	elseif filt == 33 then
198*1031c584SApple OSS Distributions		return 'VN'
199*1031c584SApple OSS Distributions	elseif filt == 34 then
200*1031c584SApple OSS Distributions		return 'TTY'
201*1031c584SApple OSS Distributions	elseif filt == 35 then
202*1031c584SApple OSS Distributions		return 'PTMX'
203*1031c584SApple OSS Distributions	elseif filt == 36 then
204*1031c584SApple OSS Distributions		return 'DETACHED'
205*1031c584SApple OSS Distributions	else
206*1031c584SApple OSS Distributions		return string.format('[%d]', filt)
207*1031c584SApple OSS Distributions	end
208*1031c584SApple OSS Distributionsend
209*1031c584SApple OSS Distributions
210*1031c584SApple OSS Distributions-- kqueue lifecycle
211*1031c584SApple OSS Distributions
212*1031c584SApple OSS Distributionsfunction processing_begin(workq)
213*1031c584SApple OSS Distributions	return function(buf)
214*1031c584SApple OSS Distributions		local prefix = event_prefix_string(buf, workq)
215*1031c584SApple OSS Distributions		if trace.debugid_is_start(buf.debugid) then
216*1031c584SApple OSS Distributions			local qos
217*1031c584SApple OSS Distributions			if workq then
218*1031c584SApple OSS Distributions				qos = buf.arg2
219*1031c584SApple OSS Distributions			else
220*1031c584SApple OSS Distributions				qos = buf.arg3
221*1031c584SApple OSS Distributions			end
222*1031c584SApple OSS Distributions			printf("%s QoS = %s\n", prefix, qos_string(qos))
223*1031c584SApple OSS Distributions		else
224*1031c584SApple OSS Distributions			printf("%s request thread = 0x%x, kqrequest state = %s\n", prefix,
225*1031c584SApple OSS Distributions					buf.arg1, state_string(kqrequest_state_strings, buf.arg2))
226*1031c584SApple OSS Distributions		end
227*1031c584SApple OSS Distributions	end
228*1031c584SApple OSS Distributionsend
229*1031c584SApple OSS Distributions
230*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kq_processing_begin", processing_begin(false))
231*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwq_processing_begin", processing_begin(true))
232*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwl_processing_begin", processing_begin(false))
233*1031c584SApple OSS Distributions
234*1031c584SApple OSS Distributionsfunction processing_end(workq)
235*1031c584SApple OSS Distributions	return function(buf)
236*1031c584SApple OSS Distributions		local qos
237*1031c584SApple OSS Distributions		if workq then
238*1031c584SApple OSS Distributions			qos = buf.arg2
239*1031c584SApple OSS Distributions		else
240*1031c584SApple OSS Distributions			qos = buf.arg3
241*1031c584SApple OSS Distributions		end
242*1031c584SApple OSS Distributions		printf("%s QoS = %s\n", event_prefix_string(buf, workq), qos_string(qos))
243*1031c584SApple OSS Distributions	end
244*1031c584SApple OSS Distributionsend
245*1031c584SApple OSS Distributions
246*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kq_processing_end", processing_end(false))
247*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwq_processing_end", processing_end(true))
248*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwl_processing_end", processing_end(false))
249*1031c584SApple OSS Distributions
250*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwq_bind", function(buf)
251*1031c584SApple OSS Distributions	printf("%s thread = 0x%x, QoS = %s, kqrequest state = %s\n",
252*1031c584SApple OSS Distributions			event_prefix_string(buf, true), buf.arg1, qos_string(buf.arg3),
253*1031c584SApple OSS Distributions			state_string(kqrequest_state_strings, buf.arg4))
254*1031c584SApple OSS Distributionsend)
255*1031c584SApple OSS Distributions
256*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwq_unbind", function(buf)
257*1031c584SApple OSS Distributions	printf("%s thread = 0x%x, QoS = %s\n", event_prefix_string(buf, true),
258*1031c584SApple OSS Distributions			buf.arg1, qos_string(buf.arg3))
259*1031c584SApple OSS Distributionsend)
260*1031c584SApple OSS Distributions
261*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwl_bind", function(buf)
262*1031c584SApple OSS Distributions	qos = buf.arg3 & 0xff
263*1031c584SApple OSS Distributions	duplicate = buf.arg3 & (1 << 8)
264*1031c584SApple OSS Distributions	kqr_override_qos_delta = buf.arg4 >> 8
265*1031c584SApple OSS Distributions	kqr_state = buf.arg4 & 0xff
266*1031c584SApple OSS Distributions
267*1031c584SApple OSS Distributions	printf("%s thread = 0x%x, QoS = %s, override QoS delta = %d, kqrequest state = %s%s\n",
268*1031c584SApple OSS Distributions			event_prefix_string(buf, false), buf.arg2, qos_string(qos),
269*1031c584SApple OSS Distributions			kqr_override_qos_delta,
270*1031c584SApple OSS Distributions			state_string(kqrequest_state_strings, kqr_state),
271*1031c584SApple OSS Distributions			duplicate and ", duplicate" or "")
272*1031c584SApple OSS Distributionsend)
273*1031c584SApple OSS Distributions
274*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwl_unbind", function(buf)
275*1031c584SApple OSS Distributions	flags = buf.arg3
276*1031c584SApple OSS Distributions	qos = buf.arg4
277*1031c584SApple OSS Distributions
278*1031c584SApple OSS Distributions	printf("%s thread = 0x%x, QoS = %s, flags = 0x%x\n", event_prefix_string(buf, false),
279*1031c584SApple OSS Distributions			buf.arg2, qos_string(qos), flags)
280*1031c584SApple OSS Distributionsend)
281*1031c584SApple OSS Distributions
282*1031c584SApple OSS Distributionsfunction thread_request(workq)
283*1031c584SApple OSS Distributions	return function(buf)
284*1031c584SApple OSS Distributions		printf("%s QoS = %s, kqrequest state = %s, override QoS delta = %d\n",
285*1031c584SApple OSS Distributions				event_prefix_string(buf, workq), qos_string(buf.arg2),
286*1031c584SApple OSS Distributions				state_string(kqrequest_state_strings, buf.arg3), buf.arg3 >> 8)
287*1031c584SApple OSS Distributions	end
288*1031c584SApple OSS Distributionsend
289*1031c584SApple OSS Distributions
290*1031c584SApple OSS Distributionsfunction thread_adjust(buf)
291*1031c584SApple OSS Distributions	tid = buf.arg2
292*1031c584SApple OSS Distributions	kqr_qos = buf.arg3 >> 8
293*1031c584SApple OSS Distributions	new_qos = buf.arg3 & 0xff
294*1031c584SApple OSS Distributions	kqr_qos_override = buf.arg4 >> 8
295*1031c584SApple OSS Distributions	kqr_state = buf.arg4 & 0xff
296*1031c584SApple OSS Distributions
297*1031c584SApple OSS Distributions	printf("%s thread = 0x%x, old/new QoS = %s/%s, old/new override QoS delta = %d/%d, kqrequest state = %s\n",
298*1031c584SApple OSS Distributions			event_prefix_string(buf, false),
299*1031c584SApple OSS Distributions			tid,
300*1031c584SApple OSS Distributions			qos_string(kqr_qos),
301*1031c584SApple OSS Distributions			qos_string(new_qos),
302*1031c584SApple OSS Distributions			kqr_qos_override,
303*1031c584SApple OSS Distributions			new_qos - kqr_qos,
304*1031c584SApple OSS Distributions			state_string(kqrequest_state_strings, kqr_state))
305*1031c584SApple OSS Distributionsend
306*1031c584SApple OSS Distributions
307*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwq_thread_request", thread_request(true))
308*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwl_thread_request", thread_request(false))
309*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwl_thread_adjust", thread_adjust)
310*1031c584SApple OSS Distributions
311*1031c584SApple OSS Distributionsfunction kevent_register(workq)
312*1031c584SApple OSS Distributions	return function(buf)
313*1031c584SApple OSS Distributions		printf("%s kevent udata = 0x%x, kevent filter = %s, kevent flags = %s\n",
314*1031c584SApple OSS Distributions				event_prefix_string(buf, workq), buf.arg2,
315*1031c584SApple OSS Distributions				kevent_filter_string(buf.arg4),
316*1031c584SApple OSS Distributions				state_string(kevent_flags_strings, buf.arg3))
317*1031c584SApple OSS Distributions	end
318*1031c584SApple OSS Distributionsend
319*1031c584SApple OSS Distributions
320*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kq_register", kevent_register(false))
321*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwq_register", kevent_register(true))
322*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwl_register", kevent_register(false))
323*1031c584SApple OSS Distributions
324*1031c584SApple OSS Distributionsfunction kevent_process(workq)
325*1031c584SApple OSS Distributions	return function(buf)
326*1031c584SApple OSS Distributions		printf("%s kevent ident = 0x%x, udata = 0x%x, kevent filter = %s, knote status = %s\n",
327*1031c584SApple OSS Distributions				event_prefix_string(buf, workq), buf.arg3 >> 32, buf.arg2,
328*1031c584SApple OSS Distributions				kevent_filter_string(buf.arg4),
329*1031c584SApple OSS Distributions				state_string(knote_state_strings, buf.arg3 & 0xffffffff))
330*1031c584SApple OSS Distributions	end
331*1031c584SApple OSS Distributionsend
332*1031c584SApple OSS Distributions
333*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kq_process", kevent_process(false))
334*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwq_process", kevent_process(true))
335*1031c584SApple OSS Distributionstrace_eventname("KEVENT_kqwl_process", kevent_process(false))
336