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