xref: /xnu-8796.121.2/tests/ktrace/kdebug_tests.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions // Copyright (c) 2020-2022 Apple Inc.  All rights reserved.
2*c54f35caSApple OSS Distributions 
3*c54f35caSApple OSS Distributions #include <darwintest.h>
4*c54f35caSApple OSS Distributions #include <darwintest_utils.h>
5*c54f35caSApple OSS Distributions #include <dispatch/dispatch.h>
6*c54f35caSApple OSS Distributions #include <inttypes.h>
7*c54f35caSApple OSS Distributions #include <ktrace/session.h>
8*c54f35caSApple OSS Distributions #include <ktrace/private.h>
9*c54f35caSApple OSS Distributions #include <kperf/kperf.h>
10*c54f35caSApple OSS Distributions #include <mach/clock_types.h>
11*c54f35caSApple OSS Distributions #include <mach/dyld_kernel.h>
12*c54f35caSApple OSS Distributions #include <mach/host_info.h>
13*c54f35caSApple OSS Distributions #include <mach/mach.h>
14*c54f35caSApple OSS Distributions #include <mach/mach_init.h>
15*c54f35caSApple OSS Distributions #include <mach/task.h>
16*c54f35caSApple OSS Distributions #include <os/assumes.h>
17*c54f35caSApple OSS Distributions #include <stdlib.h>
18*c54f35caSApple OSS Distributions #include <sys/kdebug.h>
19*c54f35caSApple OSS Distributions #include <sys/kdebug_signpost.h>
20*c54f35caSApple OSS Distributions #include <sys/resource_private.h>
21*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
22*c54f35caSApple OSS Distributions #include <stdint.h>
23*c54f35caSApple OSS Distributions #include <TargetConditionals.h>
24*c54f35caSApple OSS Distributions 
25*c54f35caSApple OSS Distributions #include "ktrace_helpers.h"
26*c54f35caSApple OSS Distributions #include "test_utils.h"
27*c54f35caSApple OSS Distributions #include "ktrace_meta.h"
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions #define KDBG_TEST_MACROS         1
30*c54f35caSApple OSS Distributions #define KDBG_TEST_OLD_TIMES      2
31*c54f35caSApple OSS Distributions #define KDBG_TEST_FUTURE_TIMES   3
32*c54f35caSApple OSS Distributions #define KDBG_TEST_IOP_SYNC_FLUSH 4
33*c54f35caSApple OSS Distributions 
34*c54f35caSApple OSS Distributions #pragma mark kdebug syscalls
35*c54f35caSApple OSS Distributions 
36*c54f35caSApple OSS Distributions #define TRACE_DEBUGID (0xfedfed00U)
37*c54f35caSApple OSS Distributions 
38*c54f35caSApple OSS Distributions T_DECL(kdebug_trace_syscall, "test that kdebug_trace(2) emits correct events")
39*c54f35caSApple OSS Distributions {
40*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
41*c54f35caSApple OSS Distributions 
42*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
43*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
44*c54f35caSApple OSS Distributions 	ktrace_set_collection_interval(s, 200);
45*c54f35caSApple OSS Distributions 
46*c54f35caSApple OSS Distributions 	__block int events_seen = 0;
47*c54f35caSApple OSS Distributions 	ktrace_events_single(s, TRACE_DEBUGID, ^void (struct trace_point *tp) {
48*c54f35caSApple OSS Distributions 		events_seen++;
49*c54f35caSApple OSS Distributions 		T_PASS("saw traced event");
50*c54f35caSApple OSS Distributions 
51*c54f35caSApple OSS Distributions 		if (ktrace_is_kernel_64_bit(s)) {
52*c54f35caSApple OSS Distributions 			T_EXPECT_EQ(tp->arg1, UINT64_C(0xfeedfacefeedface),
53*c54f35caSApple OSS Distributions 					"argument 1 of traced event is correct");
54*c54f35caSApple OSS Distributions 		} else {
55*c54f35caSApple OSS Distributions 			T_EXPECT_EQ(tp->arg1, UINT64_C(0xfeedface),
56*c54f35caSApple OSS Distributions 					"argument 1 of traced event is correct");
57*c54f35caSApple OSS Distributions 		}
58*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(tp->arg2, 2ULL, "argument 2 of traced event is correct");
59*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(tp->arg3, 3ULL, "argument 3 of traced event is correct");
60*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(tp->arg4, 4ULL, "argument 4 of traced event is correct");
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions 		ktrace_end(s, 1);
63*c54f35caSApple OSS Distributions 	});
64*c54f35caSApple OSS Distributions 
65*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
66*c54f35caSApple OSS Distributions 		T_EXPECT_GE(events_seen, 1, NULL);
67*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
68*c54f35caSApple OSS Distributions 		T_END;
69*c54f35caSApple OSS Distributions 	});
70*c54f35caSApple OSS Distributions 
71*c54f35caSApple OSS Distributions 	ktrace_filter_pid(s, getpid());
72*c54f35caSApple OSS Distributions 
73*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL);
74*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(kdebug_trace(TRACE_DEBUGID, 0xfeedfacefeedface, 2,
75*c54f35caSApple OSS Distributions 			3, 4), NULL);
76*c54f35caSApple OSS Distributions 	ktrace_end(s, 0);
77*c54f35caSApple OSS Distributions 
78*c54f35caSApple OSS Distributions 	dispatch_main();
79*c54f35caSApple OSS Distributions }
80*c54f35caSApple OSS Distributions 
81*c54f35caSApple OSS Distributions #if __LP64__
82*c54f35caSApple OSS Distributions #define IS_64BIT true
83*c54f35caSApple OSS Distributions #else // __LP64__
84*c54f35caSApple OSS Distributions #define IS_64BIT false
85*c54f35caSApple OSS Distributions #endif // !__LP64__
86*c54f35caSApple OSS Distributions 
87*c54f35caSApple OSS Distributions #define STRING_SIZE (1024)
88*c54f35caSApple OSS Distributions 
89*c54f35caSApple OSS Distributions T_DECL(kdebug_trace_string_syscall,
90*c54f35caSApple OSS Distributions 		"test that kdebug_trace_string(2) emits correct events",
91*c54f35caSApple OSS Distributions 		T_META_ENABLED(IS_64BIT))
92*c54f35caSApple OSS Distributions {
93*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
94*c54f35caSApple OSS Distributions 
95*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
96*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
97*c54f35caSApple OSS Distributions 	ktrace_set_collection_interval(s, 200);
98*c54f35caSApple OSS Distributions 	ktrace_filter_pid(s, getpid());
99*c54f35caSApple OSS Distributions 
100*c54f35caSApple OSS Distributions 	char *traced_string = calloc(1, STRING_SIZE);
101*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO;
102*c54f35caSApple OSS Distributions 	T_ASSERT_NOTNULL(traced_string, "allocated memory for string");
103*c54f35caSApple OSS Distributions 	for (size_t i = 0; i < sizeof(traced_string); i++) {
104*c54f35caSApple OSS Distributions 		traced_string[i] = 'a' + (i % 26);
105*c54f35caSApple OSS Distributions 	}
106*c54f35caSApple OSS Distributions 	traced_string[sizeof(traced_string) - 1] = '\0';
107*c54f35caSApple OSS Distributions 	size_t traced_len = strlen(traced_string);
108*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_EQ(traced_len, sizeof(traced_string) - 1,
109*c54f35caSApple OSS Distributions 			"traced string should be filled");
110*c54f35caSApple OSS Distributions 
111*c54f35caSApple OSS Distributions 	ktrace_events_single(s, TRACE_DEBUGID,
112*c54f35caSApple OSS Distributions 			^void (struct trace_point * __unused tp) {
113*c54f35caSApple OSS Distributions 		// Do nothing -- just ensure the event is filtered in.
114*c54f35caSApple OSS Distributions 	});
115*c54f35caSApple OSS Distributions 
116*c54f35caSApple OSS Distributions 	__block unsigned int string_cpu = 0;
117*c54f35caSApple OSS Distributions 	__block bool tracing_string = false;
118*c54f35caSApple OSS Distributions 	char *observed_string = calloc(1, PATH_MAX);
119*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO;
120*c54f35caSApple OSS Distributions 	T_ASSERT_NOTNULL(observed_string, "allocated memory for observed string");
121*c54f35caSApple OSS Distributions 	__block size_t string_offset = 0;
122*c54f35caSApple OSS Distributions 	ktrace_events_single(s, TRACE_STRING_GLOBAL, ^(struct trace_point *tp){
123*c54f35caSApple OSS Distributions 		if (tp->debugid & DBG_FUNC_START && tp->arg1 == TRACE_DEBUGID) {
124*c54f35caSApple OSS Distributions 			tracing_string = true;
125*c54f35caSApple OSS Distributions 			string_cpu = tp->cpuid;
126*c54f35caSApple OSS Distributions 			memcpy(observed_string + string_offset, &tp->arg3,
127*c54f35caSApple OSS Distributions 					sizeof(uint64_t) * 2);
128*c54f35caSApple OSS Distributions 			string_offset += sizeof(uint64_t) * 2;
129*c54f35caSApple OSS Distributions 		} else if (tracing_string && string_cpu == tp->cpuid) {
130*c54f35caSApple OSS Distributions 			memcpy(observed_string + string_offset, &tp->arg1,
131*c54f35caSApple OSS Distributions 					sizeof(uint64_t) * 4);
132*c54f35caSApple OSS Distributions 			string_offset += sizeof(uint64_t) * 4;
133*c54f35caSApple OSS Distributions 			if (tp->debugid & DBG_FUNC_END) {
134*c54f35caSApple OSS Distributions 				ktrace_end(s, 1);
135*c54f35caSApple OSS Distributions 			}
136*c54f35caSApple OSS Distributions 		}
137*c54f35caSApple OSS Distributions 	});
138*c54f35caSApple OSS Distributions 
139*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
140*c54f35caSApple OSS Distributions 		T_EXPECT_TRUE(tracing_string, "found string in trace");
141*c54f35caSApple OSS Distributions 		size_t observed_len = strlen(observed_string);
142*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(traced_len, observed_len, "string lengths should be equal");
143*c54f35caSApple OSS Distributions 		if (traced_len == observed_len) {
144*c54f35caSApple OSS Distributions 			T_EXPECT_EQ_STR(traced_string, observed_string,
145*c54f35caSApple OSS Distributions 					"observed correct string");
146*c54f35caSApple OSS Distributions 		}
147*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
148*c54f35caSApple OSS Distributions 		T_END;
149*c54f35caSApple OSS Distributions 	});
150*c54f35caSApple OSS Distributions 
151*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL);
152*c54f35caSApple OSS Distributions 	uint64_t str_id = kdebug_trace_string(TRACE_DEBUGID, 0, traced_string);
153*c54f35caSApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_NE(str_id, (uint64_t)0, "kdebug_trace_string(2)");
154*c54f35caSApple OSS Distributions 	ktrace_end(s, 0);
155*c54f35caSApple OSS Distributions 
156*c54f35caSApple OSS Distributions 	dispatch_main();
157*c54f35caSApple OSS Distributions }
158*c54f35caSApple OSS Distributions 
159*c54f35caSApple OSS Distributions #define SIGNPOST_SINGLE_CODE (0x10U)
160*c54f35caSApple OSS Distributions #define SIGNPOST_PAIRED_CODE (0x20U)
161*c54f35caSApple OSS Distributions 
162*c54f35caSApple OSS Distributions T_DECL(kdebug_signpost_syscall,
163*c54f35caSApple OSS Distributions 		"test that kdebug_signpost(2) emits correct events")
164*c54f35caSApple OSS Distributions {
165*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
166*c54f35caSApple OSS Distributions 
167*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
168*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
169*c54f35caSApple OSS Distributions 
170*c54f35caSApple OSS Distributions 	__block int single_seen = 0;
171*c54f35caSApple OSS Distributions 	__block int paired_seen = 0;
172*c54f35caSApple OSS Distributions 
173*c54f35caSApple OSS Distributions 	/* make sure to get enough events for the KDBUFWAIT to trigger */
174*c54f35caSApple OSS Distributions 	// ktrace_events_class(s, DBG_MACH, ^(__unused struct trace_point *tp){});
175*c54f35caSApple OSS Distributions 	ktrace_events_single(s,
176*c54f35caSApple OSS Distributions 	    APPSDBG_CODE(DBG_APP_SIGNPOST, SIGNPOST_SINGLE_CODE),
177*c54f35caSApple OSS Distributions 	    ^(struct trace_point *tp) {
178*c54f35caSApple OSS Distributions 		single_seen++;
179*c54f35caSApple OSS Distributions 		T_PASS("single signpost is traced");
180*c54f35caSApple OSS Distributions 
181*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(tp->arg1, 1ULL, "argument 1 of single signpost is correct");
182*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(tp->arg2, 2ULL, "argument 2 of single signpost is correct");
183*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(tp->arg3, 3ULL, "argument 3 of single signpost is correct");
184*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(tp->arg4, 4ULL, "argument 4 of single signpost is correct");
185*c54f35caSApple OSS Distributions 	});
186*c54f35caSApple OSS Distributions 
187*c54f35caSApple OSS Distributions 	ktrace_events_single_paired(s,
188*c54f35caSApple OSS Distributions 	    APPSDBG_CODE(DBG_APP_SIGNPOST, SIGNPOST_PAIRED_CODE),
189*c54f35caSApple OSS Distributions 	    ^(struct trace_point *start, struct trace_point *end) {
190*c54f35caSApple OSS Distributions 		paired_seen++;
191*c54f35caSApple OSS Distributions 		T_PASS("paired signposts are traced");
192*c54f35caSApple OSS Distributions 
193*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(start->arg1, 5ULL, "argument 1 of start signpost is correct");
194*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(start->arg2, 6ULL, "argument 2 of start signpost is correct");
195*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(start->arg3, 7ULL, "argument 3 of start signpost is correct");
196*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(start->arg4, 8ULL, "argument 4 of start signpost is correct");
197*c54f35caSApple OSS Distributions 
198*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(end->arg1, 9ULL, "argument 1 of end signpost is correct");
199*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(end->arg2, 10ULL, "argument 2 of end signpost is correct");
200*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(end->arg3, 11ULL, "argument 3 of end signpost is correct");
201*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(end->arg4, 12ULL, "argument 4 of end signpost is correct");
202*c54f35caSApple OSS Distributions 
203*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(single_seen, 1, "signposts are traced in the correct order");
204*c54f35caSApple OSS Distributions 
205*c54f35caSApple OSS Distributions 		ktrace_end(s, 1);
206*c54f35caSApple OSS Distributions 	});
207*c54f35caSApple OSS Distributions 
208*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^(void) {
209*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_NE(single_seen, 0,
210*c54f35caSApple OSS Distributions 		"did not see single tracepoint before timeout");
211*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_NE(paired_seen, 0,
212*c54f35caSApple OSS Distributions 		"did not see single tracepoint before timeout");
213*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
214*c54f35caSApple OSS Distributions 		T_END;
215*c54f35caSApple OSS Distributions 	});
216*c54f35caSApple OSS Distributions 
217*c54f35caSApple OSS Distributions 	ktrace_filter_pid(s, getpid());
218*c54f35caSApple OSS Distributions 
219*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()),
220*c54f35caSApple OSS Distributions 	    "started tracing");
221*c54f35caSApple OSS Distributions 
222*c54f35caSApple OSS Distributions #pragma clang diagnostic push
223*c54f35caSApple OSS Distributions #pragma clang diagnostic ignored "-Wdeprecated-declarations"
224*c54f35caSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(kdebug_signpost(SIGNPOST_SINGLE_CODE, 1, 2, 3, 4),
225*c54f35caSApple OSS Distributions 	    "emitted single signpost");
226*c54f35caSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(
227*c54f35caSApple OSS Distributions 		kdebug_signpost_start(SIGNPOST_PAIRED_CODE, 5, 6, 7, 8),
228*c54f35caSApple OSS Distributions 		"emitted start signpost");
229*c54f35caSApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(
230*c54f35caSApple OSS Distributions 		kdebug_signpost_end(SIGNPOST_PAIRED_CODE, 9, 10, 11, 12),
231*c54f35caSApple OSS Distributions 		"emitted end signpost");
232*c54f35caSApple OSS Distributions #pragma clang diagnostic pop
233*c54f35caSApple OSS Distributions 	ktrace_end(s, 0);
234*c54f35caSApple OSS Distributions 
235*c54f35caSApple OSS Distributions 	dispatch_main();
236*c54f35caSApple OSS Distributions }
237*c54f35caSApple OSS Distributions 
238*c54f35caSApple OSS Distributions T_DECL(syscall_tracing,
239*c54f35caSApple OSS Distributions 		"ensure that syscall arguments are traced propertly")
240*c54f35caSApple OSS Distributions {
241*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
242*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
243*c54f35caSApple OSS Distributions 
244*c54f35caSApple OSS Distributions 	__block bool seen = 0;
245*c54f35caSApple OSS Distributions 
246*c54f35caSApple OSS Distributions 	ktrace_filter_pid(s, getpid());
247*c54f35caSApple OSS Distributions 
248*c54f35caSApple OSS Distributions 	static const int telemetry_syscall_no = 451;
249*c54f35caSApple OSS Distributions 	static const uint64_t arg1 = 0xfeedfacefeedface;
250*c54f35caSApple OSS Distributions 
251*c54f35caSApple OSS Distributions 	ktrace_events_single(s, BSDDBG_CODE(DBG_BSD_EXCP_SC, telemetry_syscall_no),
252*c54f35caSApple OSS Distributions 			^(struct trace_point *evt){
253*c54f35caSApple OSS Distributions 		if (KDBG_EXTRACT_CODE(evt->debugid) != telemetry_syscall_no || seen) {
254*c54f35caSApple OSS Distributions 			return;
255*c54f35caSApple OSS Distributions 		}
256*c54f35caSApple OSS Distributions 
257*c54f35caSApple OSS Distributions 		seen = true;
258*c54f35caSApple OSS Distributions 		if (ktrace_is_kernel_64_bit(s)) {
259*c54f35caSApple OSS Distributions 			T_EXPECT_EQ(evt->arg1, arg1,
260*c54f35caSApple OSS Distributions 					"argument 1 of syscall event is correct");
261*c54f35caSApple OSS Distributions 		} else {
262*c54f35caSApple OSS Distributions 			T_EXPECT_EQ(evt->arg1, (uint64_t)(uint32_t)(arg1),
263*c54f35caSApple OSS Distributions 					"argument 1 of syscall event is correct");
264*c54f35caSApple OSS Distributions 		}
265*c54f35caSApple OSS Distributions 
266*c54f35caSApple OSS Distributions 		ktrace_end(s, 1);
267*c54f35caSApple OSS Distributions 	});
268*c54f35caSApple OSS Distributions 
269*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
270*c54f35caSApple OSS Distributions 		T_ASSERT_TRUE(seen,
271*c54f35caSApple OSS Distributions 				"should have seen a syscall event for kevent_id(2)");
272*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
273*c54f35caSApple OSS Distributions 		T_END;
274*c54f35caSApple OSS Distributions 	});
275*c54f35caSApple OSS Distributions 
276*c54f35caSApple OSS Distributions 	int error = ktrace_start(s, dispatch_get_main_queue());
277*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started tracing");
278*c54f35caSApple OSS Distributions 
279*c54f35caSApple OSS Distributions 	/*
280*c54f35caSApple OSS Distributions 	 * telemetry(2) has a 64-bit argument that will definitely be traced, and
281*c54f35caSApple OSS Distributions 	 * is unlikely to be used elsewhere by this process.
282*c54f35caSApple OSS Distributions 	 */
283*c54f35caSApple OSS Distributions 	extern int __telemetry(uint64_t cmd, uint64_t deadline, uint64_t interval,
284*c54f35caSApple OSS Distributions 			uint64_t leeway, uint64_t arg4, uint64_t arg5);
285*c54f35caSApple OSS Distributions 	(void)__telemetry(arg1, 0, 0, 0, 0, 0);
286*c54f35caSApple OSS Distributions 
287*c54f35caSApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC),
288*c54f35caSApple OSS Distributions 			dispatch_get_main_queue(), ^{
289*c54f35caSApple OSS Distributions 		T_LOG("ending test due to timeout");
290*c54f35caSApple OSS Distributions 		ktrace_end(s, 0);
291*c54f35caSApple OSS Distributions 	});
292*c54f35caSApple OSS Distributions 
293*c54f35caSApple OSS Distributions 	dispatch_main();
294*c54f35caSApple OSS Distributions }
295*c54f35caSApple OSS Distributions 
296*c54f35caSApple OSS Distributions #pragma mark kdebug behaviors
297*c54f35caSApple OSS Distributions 
298*c54f35caSApple OSS Distributions #define WRAPPING_EVENTS_COUNT     (150000)
299*c54f35caSApple OSS Distributions #define TRACE_ITERATIONS          (5000)
300*c54f35caSApple OSS Distributions #define WRAPPING_EVENTS_THRESHOLD (100)
301*c54f35caSApple OSS Distributions 
302*c54f35caSApple OSS Distributions T_DECL(wrapping,
303*c54f35caSApple OSS Distributions     "ensure that wrapping traces lost events and no events prior to the wrap",
304*c54f35caSApple OSS Distributions     T_META_CHECK_LEAKS(false))
305*c54f35caSApple OSS Distributions {
306*c54f35caSApple OSS Distributions 	kbufinfo_t buf_info;
307*c54f35caSApple OSS Distributions 	int wait_wrapping_secs = (WRAPPING_EVENTS_COUNT / TRACE_ITERATIONS) + 5;
308*c54f35caSApple OSS Distributions 	int current_secs = wait_wrapping_secs;
309*c54f35caSApple OSS Distributions 
310*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
311*c54f35caSApple OSS Distributions 
312*c54f35caSApple OSS Distributions 	/* use sysctls manually to bypass libktrace assumptions */
313*c54f35caSApple OSS Distributions 
314*c54f35caSApple OSS Distributions 	int mib[4] = { CTL_KERN, KERN_KDEBUG };
315*c54f35caSApple OSS Distributions 	mib[2] = KERN_KDSETBUF; mib[3] = WRAPPING_EVENTS_COUNT;
316*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(mib, 4, NULL, 0, NULL, 0), "KERN_KDSETBUF");
317*c54f35caSApple OSS Distributions 
318*c54f35caSApple OSS Distributions 	mib[2] = KERN_KDSETUP; mib[3] = 0;
319*c54f35caSApple OSS Distributions 	size_t needed = 0;
320*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(mib, 3, NULL, &needed, NULL, 0),
321*c54f35caSApple OSS Distributions 	    "KERN_KDSETUP");
322*c54f35caSApple OSS Distributions 
323*c54f35caSApple OSS Distributions 	mib[2] = KERN_KDENABLE; mib[3] = 1;
324*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(mib, 4, NULL, 0, NULL, 0), "KERN_KDENABLE");
325*c54f35caSApple OSS Distributions 
326*c54f35caSApple OSS Distributions 	/* wrapping is on by default */
327*c54f35caSApple OSS Distributions 
328*c54f35caSApple OSS Distributions 	/* wait until wrapped */
329*c54f35caSApple OSS Distributions 	T_LOG("waiting for trace to wrap");
330*c54f35caSApple OSS Distributions 	mib[2] = KERN_KDGETBUF;
331*c54f35caSApple OSS Distributions 	needed = sizeof(buf_info);
332*c54f35caSApple OSS Distributions 	do {
333*c54f35caSApple OSS Distributions 		sleep(1);
334*c54f35caSApple OSS Distributions 		for (int i = 0; i < TRACE_ITERATIONS; i++) {
335*c54f35caSApple OSS Distributions 			T_QUIET;
336*c54f35caSApple OSS Distributions 			T_ASSERT_POSIX_SUCCESS(kdebug_trace(0xfefe0000, 0, 0, 0, 0), NULL);
337*c54f35caSApple OSS Distributions 		}
338*c54f35caSApple OSS Distributions 		T_QUIET;
339*c54f35caSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(sysctl(mib, 3, &buf_info, &needed, NULL, 0),
340*c54f35caSApple OSS Distributions 		    NULL);
341*c54f35caSApple OSS Distributions 	} while (!(buf_info.flags & KDBG_WRAPPED) && --current_secs > 0);
342*c54f35caSApple OSS Distributions 
343*c54f35caSApple OSS Distributions 	T_ASSERT_TRUE(buf_info.flags & KDBG_WRAPPED,
344*c54f35caSApple OSS Distributions 	    "trace wrapped (after %d seconds within %d second timeout)",
345*c54f35caSApple OSS Distributions 	    wait_wrapping_secs - current_secs, wait_wrapping_secs);
346*c54f35caSApple OSS Distributions 
347*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
348*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_NOTNULL(s, NULL);
349*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ktrace_set_use_existing(s), NULL);
350*c54f35caSApple OSS Distributions 
351*c54f35caSApple OSS Distributions 	__block int events = 0;
352*c54f35caSApple OSS Distributions 
353*c54f35caSApple OSS Distributions 	ktrace_events_all(s, ^(struct trace_point *tp) {
354*c54f35caSApple OSS Distributions 		if (events == 0) {
355*c54f35caSApple OSS Distributions 		        T_EXPECT_EQ(tp->debugid, (unsigned int)TRACE_LOST_EVENTS,
356*c54f35caSApple OSS Distributions 		        "first event's debugid 0x%08x (%s) should be TRACE_LOST_EVENTS",
357*c54f35caSApple OSS Distributions 		        tp->debugid,
358*c54f35caSApple OSS Distributions 		        ktrace_name_for_eventid(s, tp->debugid & KDBG_EVENTID_MASK));
359*c54f35caSApple OSS Distributions 		} else {
360*c54f35caSApple OSS Distributions 		        T_QUIET;
361*c54f35caSApple OSS Distributions 		        T_EXPECT_NE(tp->debugid, (unsigned int)TRACE_LOST_EVENTS,
362*c54f35caSApple OSS Distributions 		        "event debugid 0x%08x (%s) should not be TRACE_LOST_EVENTS",
363*c54f35caSApple OSS Distributions 		        tp->debugid,
364*c54f35caSApple OSS Distributions 		        ktrace_name_for_eventid(s, tp->debugid & KDBG_EVENTID_MASK));
365*c54f35caSApple OSS Distributions 		}
366*c54f35caSApple OSS Distributions 
367*c54f35caSApple OSS Distributions 		events++;
368*c54f35caSApple OSS Distributions 		if (events > WRAPPING_EVENTS_THRESHOLD) {
369*c54f35caSApple OSS Distributions 		        ktrace_end(s, 1);
370*c54f35caSApple OSS Distributions 		}
371*c54f35caSApple OSS Distributions 	});
372*c54f35caSApple OSS Distributions 
373*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
374*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
375*c54f35caSApple OSS Distributions 		T_END;
376*c54f35caSApple OSS Distributions 	});
377*c54f35caSApple OSS Distributions 
378*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()),
379*c54f35caSApple OSS Distributions 	    "started tracing");
380*c54f35caSApple OSS Distributions 
381*c54f35caSApple OSS Distributions 	dispatch_main();
382*c54f35caSApple OSS Distributions }
383*c54f35caSApple OSS Distributions 
384*c54f35caSApple OSS Distributions static void
_assert_tracing_state(bool enable,const char * msg)385*c54f35caSApple OSS Distributions _assert_tracing_state(bool enable, const char *msg)
386*c54f35caSApple OSS Distributions {
387*c54f35caSApple OSS Distributions 	kbufinfo_t bufinfo = { 0 };
388*c54f35caSApple OSS Distributions 	T_QUIET;
389*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(
390*c54f35caSApple OSS Distributions 		    (int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDGETBUF }, 3,
391*c54f35caSApple OSS Distributions 		    &bufinfo, &(size_t){ sizeof(bufinfo) }, NULL, 0),
392*c54f35caSApple OSS Distributions 	    "get kdebug buffer info");
393*c54f35caSApple OSS Distributions 	T_QUIET;
394*c54f35caSApple OSS Distributions 	T_ASSERT_NE(bufinfo.nkdbufs, 0, "tracing should be configured");
395*c54f35caSApple OSS Distributions 	T_ASSERT_NE(bufinfo.nolog, enable, "%s: tracing should%s be enabled",
396*c54f35caSApple OSS Distributions 			msg, enable ? "" : "n't");
397*c54f35caSApple OSS Distributions }
398*c54f35caSApple OSS Distributions 
399*c54f35caSApple OSS Distributions #define DRAIN_TIMEOUT_NS (1 * NSEC_PER_SEC)
400*c54f35caSApple OSS Distributions 
401*c54f35caSApple OSS Distributions static void
_drain_until_event(uint32_t debugid)402*c54f35caSApple OSS Distributions _drain_until_event(uint32_t debugid)
403*c54f35caSApple OSS Distributions {
404*c54f35caSApple OSS Distributions 	static kd_buf events[256] = { 0 };
405*c54f35caSApple OSS Distributions 	size_t events_size = sizeof(events);
406*c54f35caSApple OSS Distributions 	uint64_t start_time_ns = clock_gettime_nsec_np(CLOCK_MONOTONIC);
407*c54f35caSApple OSS Distributions 	unsigned int reads = 0;
408*c54f35caSApple OSS Distributions 	while (true) {
409*c54f35caSApple OSS Distributions 		T_QUIET;
410*c54f35caSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(sysctl(
411*c54f35caSApple OSS Distributions 				(int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDREADTR, }, 3,
412*c54f35caSApple OSS Distributions 				events, &events_size, NULL, 0), "reading trace data");
413*c54f35caSApple OSS Distributions 		reads += 1;
414*c54f35caSApple OSS Distributions 		size_t events_count = events_size;
415*c54f35caSApple OSS Distributions 		for (size_t i = 0; i < events_count; i++) {
416*c54f35caSApple OSS Distributions 			if (events[i].debugid == debugid) {
417*c54f35caSApple OSS Distributions 				T_LOG("draining found event 0x%x", debugid);
418*c54f35caSApple OSS Distributions 				return;
419*c54f35caSApple OSS Distributions 			}
420*c54f35caSApple OSS Distributions 		}
421*c54f35caSApple OSS Distributions 		uint64_t cur_time_ns = clock_gettime_nsec_np(CLOCK_MONOTONIC);
422*c54f35caSApple OSS Distributions 		if (cur_time_ns - start_time_ns > DRAIN_TIMEOUT_NS) {
423*c54f35caSApple OSS Distributions 			T_ASSERT_FAIL("timed out after %f seconds waiting for 0x%x,"
424*c54f35caSApple OSS Distributions 					" after %u reads",
425*c54f35caSApple OSS Distributions 					(double)(cur_time_ns - start_time_ns) / 1e9, debugid,
426*c54f35caSApple OSS Distributions 					reads);
427*c54f35caSApple OSS Distributions 		}
428*c54f35caSApple OSS Distributions 	}
429*c54f35caSApple OSS Distributions }
430*c54f35caSApple OSS Distributions 
431*c54f35caSApple OSS Distributions T_DECL(disabling_event_match,
432*c54f35caSApple OSS Distributions     "ensure that ktrace is disabled when an event disable matcher fires")
433*c54f35caSApple OSS Distributions {
434*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
435*c54f35caSApple OSS Distributions 
436*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
437*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
438*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
439*c54f35caSApple OSS Distributions 	ktrace_events_single(s, TRACE_DEBUGID,
440*c54f35caSApple OSS Distributions 			^(struct trace_point *tp __unused) {});
441*c54f35caSApple OSS Distributions 	int error = ktrace_configure(s);
442*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(error, "configured session");
443*c54f35caSApple OSS Distributions 	kd_event_matcher matchers[2] = { {
444*c54f35caSApple OSS Distributions 		.kem_debugid = TRACE_DEBUGID,
445*c54f35caSApple OSS Distributions 		.kem_args[0] = 0xff,
446*c54f35caSApple OSS Distributions 	}, {
447*c54f35caSApple OSS Distributions 		.kem_debugid = UINT32_MAX,
448*c54f35caSApple OSS Distributions 		.kem_args[0] = 0xfff,
449*c54f35caSApple OSS Distributions 	} };
450*c54f35caSApple OSS Distributions 	size_t matchers_size = sizeof(matchers);
451*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(
452*c54f35caSApple OSS Distributions 		(int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDSET_EDM, }, 3,
453*c54f35caSApple OSS Distributions 		&matchers, &matchers_size, NULL, 0), "set event disable matcher");
454*c54f35caSApple OSS Distributions 	size_t size = 0;
455*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(
456*c54f35caSApple OSS Distributions 		(int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDEFLAGS, KDBG_MATCH_DISABLE, }, 4,
457*c54f35caSApple OSS Distributions 		NULL, &size, NULL, 0), "enabled event disable matching");
458*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(
459*c54f35caSApple OSS Distributions 		(int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDENABLE, 1, }, 4,
460*c54f35caSApple OSS Distributions 		NULL, NULL, NULL, 0), "enabled tracing");
461*c54f35caSApple OSS Distributions 	_assert_tracing_state(true, "after enabling trace");
462*c54f35caSApple OSS Distributions 	T_SETUPEND;
463*c54f35caSApple OSS Distributions 
464*c54f35caSApple OSS Distributions 	kdebug_trace(TRACE_DEBUGID + 8, 0xff, 0, 0, 0);
465*c54f35caSApple OSS Distributions 	_drain_until_event(TRACE_DEBUGID + 8);
466*c54f35caSApple OSS Distributions 	_assert_tracing_state(true, "with wrong debugid");
467*c54f35caSApple OSS Distributions 	kdebug_trace(TRACE_DEBUGID, 0, 0, 0, 0);
468*c54f35caSApple OSS Distributions 	_drain_until_event(TRACE_DEBUGID);
469*c54f35caSApple OSS Distributions 	_assert_tracing_state(true, "with wrong argument");
470*c54f35caSApple OSS Distributions 	kdebug_trace(TRACE_DEBUGID, 0xff, 0, 0, 0);
471*c54f35caSApple OSS Distributions 	_drain_until_event(TRACE_DEBUGID);
472*c54f35caSApple OSS Distributions 	_assert_tracing_state(false, "after disabling event");
473*c54f35caSApple OSS Distributions }
474*c54f35caSApple OSS Distributions 
475*c54f35caSApple OSS Distributions T_DECL(reject_old_events,
476*c54f35caSApple OSS Distributions     "ensure that kdebug rejects events from before tracing began",
477*c54f35caSApple OSS Distributions     T_META_CHECK_LEAKS(false))
478*c54f35caSApple OSS Distributions {
479*c54f35caSApple OSS Distributions 	__block uint64_t event_horizon_ts;
480*c54f35caSApple OSS Distributions 
481*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
482*c54f35caSApple OSS Distributions 
483*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
484*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
485*c54f35caSApple OSS Distributions 	ktrace_set_collection_interval(s, 100);
486*c54f35caSApple OSS Distributions 
487*c54f35caSApple OSS Distributions 	__block int events = 0;
488*c54f35caSApple OSS Distributions 	ktrace_events_single(s, KDBG_EVENTID(DBG_BSD, DBG_BSD_KDEBUG_TEST, 1),
489*c54f35caSApple OSS Distributions 	    ^(struct trace_point *tp) {
490*c54f35caSApple OSS Distributions 		events++;
491*c54f35caSApple OSS Distributions 		T_EXPECT_GT(tp->timestamp, event_horizon_ts,
492*c54f35caSApple OSS Distributions 		"events in trace should be from after tracing began");
493*c54f35caSApple OSS Distributions 	});
494*c54f35caSApple OSS Distributions 
495*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
496*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(events, 2, "should see only two events");
497*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
498*c54f35caSApple OSS Distributions 		T_END;
499*c54f35caSApple OSS Distributions 	});
500*c54f35caSApple OSS Distributions 
501*c54f35caSApple OSS Distributions 	event_horizon_ts = mach_absolute_time();
502*c54f35caSApple OSS Distributions 
503*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL);
504*c54f35caSApple OSS Distributions 	/* first, try an old event at the beginning of trace */
505*c54f35caSApple OSS Distributions 	assert_kdebug_test(KDBG_TEST_OLD_TIMES, "induce old event at beginning");
506*c54f35caSApple OSS Distributions 	/* after a good event has been traced, old events should be rejected */
507*c54f35caSApple OSS Distributions 	assert_kdebug_test(KDBG_TEST_OLD_TIMES, "induce old event to be rejected");
508*c54f35caSApple OSS Distributions 	ktrace_end(s, 0);
509*c54f35caSApple OSS Distributions 
510*c54f35caSApple OSS Distributions 	dispatch_main();
511*c54f35caSApple OSS Distributions }
512*c54f35caSApple OSS Distributions 
513*c54f35caSApple OSS Distributions #define ORDERING_TIMEOUT_SEC 5
514*c54f35caSApple OSS Distributions 
515*c54f35caSApple OSS Distributions T_DECL(ascending_time_order,
516*c54f35caSApple OSS Distributions     "ensure that kdebug events are in ascending order based on time",
517*c54f35caSApple OSS Distributions     T_META_CHECK_LEAKS(false), XNU_T_META_SOC_SPECIFIC)
518*c54f35caSApple OSS Distributions {
519*c54f35caSApple OSS Distributions 	__block uint64_t prev_ts = 0;
520*c54f35caSApple OSS Distributions 	__block uint32_t prev_debugid = 0;
521*c54f35caSApple OSS Distributions 	__block unsigned int prev_cpu = 0;
522*c54f35caSApple OSS Distributions 	__block bool in_order = true;
523*c54f35caSApple OSS Distributions 
524*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
525*c54f35caSApple OSS Distributions 
526*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
527*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
528*c54f35caSApple OSS Distributions 
529*c54f35caSApple OSS Distributions 	ktrace_events_all(s, ^(struct trace_point *tp) {
530*c54f35caSApple OSS Distributions 		if (tp->timestamp < prev_ts) {
531*c54f35caSApple OSS Distributions 		        in_order = false;
532*c54f35caSApple OSS Distributions 		        T_LOG("%" PRIu64 ": %#" PRIx32 " (cpu %d)",
533*c54f35caSApple OSS Distributions 		        prev_ts, prev_debugid, prev_cpu);
534*c54f35caSApple OSS Distributions 		        T_LOG("%" PRIu64 ": %#" PRIx32 " (cpu %d)",
535*c54f35caSApple OSS Distributions 		        tp->timestamp, tp->debugid, tp->cpuid);
536*c54f35caSApple OSS Distributions 		        ktrace_end(s, 1);
537*c54f35caSApple OSS Distributions 		}
538*c54f35caSApple OSS Distributions 	});
539*c54f35caSApple OSS Distributions 
540*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
541*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
542*c54f35caSApple OSS Distributions 		T_EXPECT_TRUE(in_order, "event timestamps were in-order");
543*c54f35caSApple OSS Distributions 		T_END;
544*c54f35caSApple OSS Distributions 	});
545*c54f35caSApple OSS Distributions 
546*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()),
547*c54f35caSApple OSS Distributions 	    "started tracing");
548*c54f35caSApple OSS Distributions 
549*c54f35caSApple OSS Distributions 	/* try to inject old timestamps into trace */
550*c54f35caSApple OSS Distributions 	assert_kdebug_test(KDBG_TEST_OLD_TIMES, "inject old time");
551*c54f35caSApple OSS Distributions 
552*c54f35caSApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, ORDERING_TIMEOUT_SEC * NSEC_PER_SEC),
553*c54f35caSApple OSS Distributions 	    dispatch_get_main_queue(), ^{
554*c54f35caSApple OSS Distributions 		T_LOG("ending test after timeout");
555*c54f35caSApple OSS Distributions 		ktrace_end(s, 1);
556*c54f35caSApple OSS Distributions 	});
557*c54f35caSApple OSS Distributions 
558*c54f35caSApple OSS Distributions 	dispatch_main();
559*c54f35caSApple OSS Distributions }
560*c54f35caSApple OSS Distributions 
561*c54f35caSApple OSS Distributions #pragma mark dyld tracing
562*c54f35caSApple OSS Distributions 
563*c54f35caSApple OSS Distributions __attribute__((aligned(8)))
564*c54f35caSApple OSS Distributions static const char map_uuid[16] = "map UUID";
565*c54f35caSApple OSS Distributions 
566*c54f35caSApple OSS Distributions __attribute__((aligned(8)))
567*c54f35caSApple OSS Distributions static const char unmap_uuid[16] = "unmap UUID";
568*c54f35caSApple OSS Distributions 
569*c54f35caSApple OSS Distributions __attribute__((aligned(8)))
570*c54f35caSApple OSS Distributions static const char sc_uuid[16] = "shared UUID";
571*c54f35caSApple OSS Distributions 
572*c54f35caSApple OSS Distributions static fsid_t map_fsid = { .val = { 42, 43 } };
573*c54f35caSApple OSS Distributions static fsid_t unmap_fsid = { .val = { 44, 45 } };
574*c54f35caSApple OSS Distributions static fsid_t sc_fsid = { .val = { 46, 47 } };
575*c54f35caSApple OSS Distributions 
576*c54f35caSApple OSS Distributions static fsobj_id_t map_fsobjid = { .fid_objno = 42, .fid_generation = 43 };
577*c54f35caSApple OSS Distributions static fsobj_id_t unmap_fsobjid = { .fid_objno = 44, .fid_generation = 45 };
578*c54f35caSApple OSS Distributions static fsobj_id_t sc_fsobjid = { .fid_objno = 46, .fid_generation = 47 };
579*c54f35caSApple OSS Distributions 
580*c54f35caSApple OSS Distributions #define MAP_LOAD_ADDR   0xabadcafe
581*c54f35caSApple OSS Distributions #define UNMAP_LOAD_ADDR 0xfeedface
582*c54f35caSApple OSS Distributions #define SC_LOAD_ADDR    0xfedfaced
583*c54f35caSApple OSS Distributions 
584*c54f35caSApple OSS Distributions __unused
585*c54f35caSApple OSS Distributions static void
expect_dyld_image_info(struct trace_point * tp,const uint64_t * exp_uuid,uint64_t exp_load_addr,fsid_t * exp_fsid,fsobj_id_t * exp_fsobjid,int order)586*c54f35caSApple OSS Distributions expect_dyld_image_info(struct trace_point *tp, const uint64_t *exp_uuid,
587*c54f35caSApple OSS Distributions     uint64_t exp_load_addr, fsid_t *exp_fsid, fsobj_id_t *exp_fsobjid,
588*c54f35caSApple OSS Distributions     int order)
589*c54f35caSApple OSS Distributions {
590*c54f35caSApple OSS Distributions #if defined(__LP64__) || defined(__arm64__)
591*c54f35caSApple OSS Distributions 	if (order == 0) {
592*c54f35caSApple OSS Distributions 		uint64_t uuid[2];
593*c54f35caSApple OSS Distributions 		uint64_t load_addr;
594*c54f35caSApple OSS Distributions 		fsid_t fsid;
595*c54f35caSApple OSS Distributions 
596*c54f35caSApple OSS Distributions 		uuid[0] = (uint64_t)tp->arg1;
597*c54f35caSApple OSS Distributions 		uuid[1] = (uint64_t)tp->arg2;
598*c54f35caSApple OSS Distributions 		load_addr = (uint64_t)tp->arg3;
599*c54f35caSApple OSS Distributions 		fsid.val[0] = (int32_t)(tp->arg4 & UINT32_MAX);
600*c54f35caSApple OSS Distributions 		fsid.val[1] = (int32_t)((uint64_t)tp->arg4 >> 32);
601*c54f35caSApple OSS Distributions 
602*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(uuid[0], exp_uuid[0], NULL);
603*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(uuid[1], exp_uuid[1], NULL);
604*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(load_addr, exp_load_addr, NULL);
605*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(fsid.val[0], exp_fsid->val[0], NULL);
606*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(fsid.val[1], exp_fsid->val[1], NULL);
607*c54f35caSApple OSS Distributions 	} else if (order == 1) {
608*c54f35caSApple OSS Distributions 		fsobj_id_t fsobjid;
609*c54f35caSApple OSS Distributions 
610*c54f35caSApple OSS Distributions 		fsobjid.fid_objno = (uint32_t)(tp->arg1 & UINT32_MAX);
611*c54f35caSApple OSS Distributions 		fsobjid.fid_generation = (uint32_t)((uint64_t)tp->arg1 >> 32);
612*c54f35caSApple OSS Distributions 
613*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(fsobjid.fid_objno, exp_fsobjid->fid_objno, NULL);
614*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(fsobjid.fid_generation,
615*c54f35caSApple OSS Distributions 		    exp_fsobjid->fid_generation, NULL);
616*c54f35caSApple OSS Distributions 	} else {
617*c54f35caSApple OSS Distributions 		T_ASSERT_FAIL("unrecognized order of events %d", order);
618*c54f35caSApple OSS Distributions 	}
619*c54f35caSApple OSS Distributions #else /* defined(__LP64__) */
620*c54f35caSApple OSS Distributions 	if (order == 0) {
621*c54f35caSApple OSS Distributions 		uint32_t uuid[4];
622*c54f35caSApple OSS Distributions 
623*c54f35caSApple OSS Distributions 		uuid[0] = (uint32_t)tp->arg1;
624*c54f35caSApple OSS Distributions 		uuid[1] = (uint32_t)tp->arg2;
625*c54f35caSApple OSS Distributions 		uuid[2] = (uint32_t)tp->arg3;
626*c54f35caSApple OSS Distributions 		uuid[3] = (uint32_t)tp->arg4;
627*c54f35caSApple OSS Distributions 
628*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(uuid[0], (uint32_t)exp_uuid[0], NULL);
629*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(uuid[1], (uint32_t)(exp_uuid[0] >> 32), NULL);
630*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(uuid[2], (uint32_t)exp_uuid[1], NULL);
631*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(uuid[3], (uint32_t)(exp_uuid[1] >> 32), NULL);
632*c54f35caSApple OSS Distributions 	} else if (order == 1) {
633*c54f35caSApple OSS Distributions 		uint32_t load_addr;
634*c54f35caSApple OSS Distributions 		fsid_t fsid;
635*c54f35caSApple OSS Distributions 		fsobj_id_t fsobjid;
636*c54f35caSApple OSS Distributions 
637*c54f35caSApple OSS Distributions 		load_addr = (uint32_t)tp->arg1;
638*c54f35caSApple OSS Distributions 		fsid.val[0] = (int32_t)tp->arg2;
639*c54f35caSApple OSS Distributions 		fsid.val[1] = (int32_t)tp->arg3;
640*c54f35caSApple OSS Distributions 		fsobjid.fid_objno = (uint32_t)tp->arg4;
641*c54f35caSApple OSS Distributions 
642*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(load_addr, (uint32_t)exp_load_addr, NULL);
643*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(fsid.val[0], exp_fsid->val[0], NULL);
644*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(fsid.val[1], exp_fsid->val[1], NULL);
645*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(fsobjid.fid_objno, exp_fsobjid->fid_objno, NULL);
646*c54f35caSApple OSS Distributions 	} else if (order == 2) {
647*c54f35caSApple OSS Distributions 		fsobj_id_t fsobjid;
648*c54f35caSApple OSS Distributions 
649*c54f35caSApple OSS Distributions 		fsobjid.fid_generation = tp->arg1;
650*c54f35caSApple OSS Distributions 
651*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(fsobjid.fid_generation,
652*c54f35caSApple OSS Distributions 		    exp_fsobjid->fid_generation, NULL);
653*c54f35caSApple OSS Distributions 	} else {
654*c54f35caSApple OSS Distributions 		T_ASSERT_FAIL("unrecognized order of events %d", order);
655*c54f35caSApple OSS Distributions 	}
656*c54f35caSApple OSS Distributions #endif /* defined(__LP64__) */
657*c54f35caSApple OSS Distributions }
658*c54f35caSApple OSS Distributions 
659*c54f35caSApple OSS Distributions #if defined(__LP64__) || defined(__arm64__)
660*c54f35caSApple OSS Distributions #define DYLD_CODE_OFFSET (0)
661*c54f35caSApple OSS Distributions #define DYLD_EVENTS      (2)
662*c54f35caSApple OSS Distributions #else
663*c54f35caSApple OSS Distributions #define DYLD_CODE_OFFSET (2)
664*c54f35caSApple OSS Distributions #define DYLD_EVENTS      (3)
665*c54f35caSApple OSS Distributions #endif
666*c54f35caSApple OSS Distributions 
667*c54f35caSApple OSS Distributions static void
expect_dyld_events(ktrace_session_t s,const char * name,uint32_t base_code,const char * exp_uuid,uint64_t exp_load_addr,fsid_t * exp_fsid,fsobj_id_t * exp_fsobjid,uint8_t * saw_events)668*c54f35caSApple OSS Distributions expect_dyld_events(ktrace_session_t s, const char *name, uint32_t base_code,
669*c54f35caSApple OSS Distributions     const char *exp_uuid, uint64_t exp_load_addr, fsid_t *exp_fsid,
670*c54f35caSApple OSS Distributions     fsobj_id_t *exp_fsobjid, uint8_t *saw_events)
671*c54f35caSApple OSS Distributions {
672*c54f35caSApple OSS Distributions 	for (int i = 0; i < DYLD_EVENTS; i++) {
673*c54f35caSApple OSS Distributions 		ktrace_events_single(s, KDBG_EVENTID(DBG_DYLD, DBG_DYLD_UUID,
674*c54f35caSApple OSS Distributions 		    base_code + DYLD_CODE_OFFSET + (unsigned int)i),
675*c54f35caSApple OSS Distributions 		    ^(struct trace_point *tp) {
676*c54f35caSApple OSS Distributions 			T_LOG("checking %s event %c", name, 'A' + i);
677*c54f35caSApple OSS Distributions 			expect_dyld_image_info(tp, (const void *)exp_uuid, exp_load_addr,
678*c54f35caSApple OSS Distributions 			exp_fsid, exp_fsobjid, i);
679*c54f35caSApple OSS Distributions 			*saw_events |= (1U << i);
680*c54f35caSApple OSS Distributions 		});
681*c54f35caSApple OSS Distributions 	}
682*c54f35caSApple OSS Distributions }
683*c54f35caSApple OSS Distributions 
684*c54f35caSApple OSS Distributions T_DECL(dyld_events, "test that dyld registering libraries emits events")
685*c54f35caSApple OSS Distributions {
686*c54f35caSApple OSS Distributions 	dyld_kernel_image_info_t info;
687*c54f35caSApple OSS Distributions 
688*c54f35caSApple OSS Distributions 	/*
689*c54f35caSApple OSS Distributions 	 * Use pointers instead of __block variables in order to use these variables
690*c54f35caSApple OSS Distributions 	 * in the completion block below _and_ pass pointers to them to the
691*c54f35caSApple OSS Distributions 	 * expect_dyld_events function.
692*c54f35caSApple OSS Distributions 	 */
693*c54f35caSApple OSS Distributions 	uint8_t saw_events[3] = { 0 };
694*c54f35caSApple OSS Distributions 	uint8_t *saw_mapping = &(saw_events[0]);
695*c54f35caSApple OSS Distributions 	uint8_t *saw_unmapping = &(saw_events[1]);
696*c54f35caSApple OSS Distributions 	uint8_t *saw_shared_cache = &(saw_events[2]);
697*c54f35caSApple OSS Distributions 
698*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
699*c54f35caSApple OSS Distributions 
700*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
701*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
702*c54f35caSApple OSS Distributions 
703*c54f35caSApple OSS Distributions 	T_QUIET;
704*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_filter_pid(s, getpid()),
705*c54f35caSApple OSS Distributions 	    "filtered to current process");
706*c54f35caSApple OSS Distributions 
707*c54f35caSApple OSS Distributions 	expect_dyld_events(s, "mapping", DBG_DYLD_UUID_MAP_A, map_uuid,
708*c54f35caSApple OSS Distributions 	    MAP_LOAD_ADDR, &map_fsid, &map_fsobjid, saw_mapping);
709*c54f35caSApple OSS Distributions 	expect_dyld_events(s, "unmapping", DBG_DYLD_UUID_UNMAP_A, unmap_uuid,
710*c54f35caSApple OSS Distributions 	    UNMAP_LOAD_ADDR, &unmap_fsid, &unmap_fsobjid, saw_unmapping);
711*c54f35caSApple OSS Distributions 	expect_dyld_events(s, "shared cache", DBG_DYLD_UUID_SHARED_CACHE_A,
712*c54f35caSApple OSS Distributions 	    sc_uuid, SC_LOAD_ADDR, &sc_fsid, &sc_fsobjid, saw_shared_cache);
713*c54f35caSApple OSS Distributions 
714*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
715*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
716*c54f35caSApple OSS Distributions 
717*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(__builtin_popcount(*saw_mapping), DYLD_EVENTS, NULL);
718*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(__builtin_popcount(*saw_unmapping), DYLD_EVENTS, NULL);
719*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(__builtin_popcount(*saw_shared_cache), DYLD_EVENTS, NULL);
720*c54f35caSApple OSS Distributions 		T_END;
721*c54f35caSApple OSS Distributions 	});
722*c54f35caSApple OSS Distributions 
723*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL);
724*c54f35caSApple OSS Distributions 
725*c54f35caSApple OSS Distributions 	info.load_addr = MAP_LOAD_ADDR;
726*c54f35caSApple OSS Distributions 	memcpy(info.uuid, map_uuid, sizeof(info.uuid));
727*c54f35caSApple OSS Distributions 	info.fsid = map_fsid;
728*c54f35caSApple OSS Distributions 	info.fsobjid = map_fsobjid;
729*c54f35caSApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_register_dyld_image_infos(mach_task_self(),
730*c54f35caSApple OSS Distributions 	    &info, 1), "registered dyld image info");
731*c54f35caSApple OSS Distributions 
732*c54f35caSApple OSS Distributions 	info.load_addr = UNMAP_LOAD_ADDR;
733*c54f35caSApple OSS Distributions 	memcpy(info.uuid, unmap_uuid, sizeof(info.uuid));
734*c54f35caSApple OSS Distributions 	info.fsid = unmap_fsid;
735*c54f35caSApple OSS Distributions 	info.fsobjid = unmap_fsobjid;
736*c54f35caSApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_unregister_dyld_image_infos(mach_task_self(),
737*c54f35caSApple OSS Distributions 	    &info, 1), "unregistered dyld image info");
738*c54f35caSApple OSS Distributions 
739*c54f35caSApple OSS Distributions 	info.load_addr = SC_LOAD_ADDR;
740*c54f35caSApple OSS Distributions 	memcpy(info.uuid, sc_uuid, sizeof(info.uuid));
741*c54f35caSApple OSS Distributions 	info.fsid = sc_fsid;
742*c54f35caSApple OSS Distributions 	info.fsobjid = sc_fsobjid;
743*c54f35caSApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(task_register_dyld_shared_cache_image_info(
744*c54f35caSApple OSS Distributions 		    mach_task_self(), info, FALSE, FALSE),
745*c54f35caSApple OSS Distributions 	    "registered dyld shared cache image info");
746*c54f35caSApple OSS Distributions 
747*c54f35caSApple OSS Distributions 	ktrace_end(s, 0);
748*c54f35caSApple OSS Distributions 
749*c54f35caSApple OSS Distributions 	dispatch_main();
750*c54f35caSApple OSS Distributions }
751*c54f35caSApple OSS Distributions 
752*c54f35caSApple OSS Distributions #pragma mark kdebug kernel macros
753*c54f35caSApple OSS Distributions 
754*c54f35caSApple OSS Distributions #define EXP_KERNEL_EVENTS 5U
755*c54f35caSApple OSS Distributions 
756*c54f35caSApple OSS Distributions static const uint32_t dev_evts[EXP_KERNEL_EVENTS] = {
757*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 0),
758*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 1),
759*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 2),
760*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 3),
761*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 4),
762*c54f35caSApple OSS Distributions };
763*c54f35caSApple OSS Distributions 
764*c54f35caSApple OSS Distributions static const uint32_t rel_evts[EXP_KERNEL_EVENTS] = {
765*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 5),
766*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 6),
767*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 7),
768*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 8),
769*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 9),
770*c54f35caSApple OSS Distributions };
771*c54f35caSApple OSS Distributions 
772*c54f35caSApple OSS Distributions static const uint32_t filt_evts[EXP_KERNEL_EVENTS] = {
773*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 10),
774*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 11),
775*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 12),
776*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 13),
777*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 14),
778*c54f35caSApple OSS Distributions };
779*c54f35caSApple OSS Distributions 
780*c54f35caSApple OSS Distributions static const uint32_t noprocfilt_evts[EXP_KERNEL_EVENTS] = {
781*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 15),
782*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 16),
783*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 17),
784*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 18),
785*c54f35caSApple OSS Distributions 	BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, 19),
786*c54f35caSApple OSS Distributions };
787*c54f35caSApple OSS Distributions 
788*c54f35caSApple OSS Distributions static void
expect_event(struct trace_point * tp,const char * name,unsigned int * events,const uint32_t * event_ids,size_t event_ids_len)789*c54f35caSApple OSS Distributions expect_event(struct trace_point *tp, const char *name, unsigned int *events,
790*c54f35caSApple OSS Distributions     const uint32_t *event_ids, size_t event_ids_len)
791*c54f35caSApple OSS Distributions {
792*c54f35caSApple OSS Distributions 	unsigned int event_idx = *events;
793*c54f35caSApple OSS Distributions 	bool event_found = false;
794*c54f35caSApple OSS Distributions 	size_t i;
795*c54f35caSApple OSS Distributions 	for (i = 0; i < event_ids_len; i++) {
796*c54f35caSApple OSS Distributions 		if (event_ids[i] == (tp->debugid & KDBG_EVENTID_MASK)) {
797*c54f35caSApple OSS Distributions 			T_LOG("found %s event 0x%x", name, tp->debugid);
798*c54f35caSApple OSS Distributions 			event_found = true;
799*c54f35caSApple OSS Distributions 		}
800*c54f35caSApple OSS Distributions 	}
801*c54f35caSApple OSS Distributions 
802*c54f35caSApple OSS Distributions 	if (!event_found) {
803*c54f35caSApple OSS Distributions 		return;
804*c54f35caSApple OSS Distributions 	}
805*c54f35caSApple OSS Distributions 
806*c54f35caSApple OSS Distributions 	*events += 1;
807*c54f35caSApple OSS Distributions 	for (i = 0; i < event_idx; i++) {
808*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(((uint64_t *)&tp->arg1)[i], (uint64_t)i + 1,
809*c54f35caSApple OSS Distributions 		    NULL);
810*c54f35caSApple OSS Distributions 	}
811*c54f35caSApple OSS Distributions 	for (; i < 4; i++) {
812*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_EQ(((uint64_t *)&tp->arg1)[i], (uint64_t)0, NULL);
813*c54f35caSApple OSS Distributions 	}
814*c54f35caSApple OSS Distributions }
815*c54f35caSApple OSS Distributions 
816*c54f35caSApple OSS Distributions static void
expect_release_event(struct trace_point * tp,unsigned int * events)817*c54f35caSApple OSS Distributions expect_release_event(struct trace_point *tp, unsigned int *events)
818*c54f35caSApple OSS Distributions {
819*c54f35caSApple OSS Distributions 	expect_event(tp, "release", events, rel_evts,
820*c54f35caSApple OSS Distributions 	    sizeof(rel_evts) / sizeof(rel_evts[0]));
821*c54f35caSApple OSS Distributions }
822*c54f35caSApple OSS Distributions 
823*c54f35caSApple OSS Distributions static void
expect_development_event(struct trace_point * tp,unsigned int * events)824*c54f35caSApple OSS Distributions expect_development_event(struct trace_point *tp, unsigned int *events)
825*c54f35caSApple OSS Distributions {
826*c54f35caSApple OSS Distributions 	expect_event(tp, "dev", events, dev_evts, sizeof(dev_evts) / sizeof(dev_evts[0]));
827*c54f35caSApple OSS Distributions }
828*c54f35caSApple OSS Distributions 
829*c54f35caSApple OSS Distributions static void
expect_filtered_event(struct trace_point * tp,unsigned int * events)830*c54f35caSApple OSS Distributions expect_filtered_event(struct trace_point *tp, unsigned int *events)
831*c54f35caSApple OSS Distributions {
832*c54f35caSApple OSS Distributions 	expect_event(tp, "filtered", events, filt_evts,
833*c54f35caSApple OSS Distributions 	    sizeof(filt_evts) / sizeof(filt_evts[0]));
834*c54f35caSApple OSS Distributions }
835*c54f35caSApple OSS Distributions 
836*c54f35caSApple OSS Distributions static void
expect_noprocfilt_event(struct trace_point * tp,unsigned int * events)837*c54f35caSApple OSS Distributions expect_noprocfilt_event(struct trace_point *tp, unsigned int *events)
838*c54f35caSApple OSS Distributions {
839*c54f35caSApple OSS Distributions 	expect_event(tp, "noprocfilt", events, noprocfilt_evts,
840*c54f35caSApple OSS Distributions 	    sizeof(noprocfilt_evts) / sizeof(noprocfilt_evts[0]));
841*c54f35caSApple OSS Distributions }
842*c54f35caSApple OSS Distributions 
843*c54f35caSApple OSS Distributions static void
844*c54f35caSApple OSS Distributions expect_kdbg_test_events(ktrace_session_t s, bool use_all_callback,
845*c54f35caSApple OSS Distributions     void (^cb)(unsigned int dev_seen, unsigned int rel_seen,
846*c54f35caSApple OSS Distributions     unsigned int filt_seen, unsigned int noprocfilt_seen))
847*c54f35caSApple OSS Distributions {
848*c54f35caSApple OSS Distributions 	__block unsigned int dev_seen = 0;
849*c54f35caSApple OSS Distributions 	__block unsigned int rel_seen = 0;
850*c54f35caSApple OSS Distributions 	__block unsigned int filt_seen = 0;
851*c54f35caSApple OSS Distributions 	__block unsigned int noprocfilt_seen = 0;
852*c54f35caSApple OSS Distributions 
853*c54f35caSApple OSS Distributions 	void (^evtcb)(struct trace_point *tp) = ^(struct trace_point *tp) {
854*c54f35caSApple OSS Distributions 		expect_development_event(tp, &dev_seen);
855*c54f35caSApple OSS Distributions 		expect_release_event(tp, &rel_seen);
856*c54f35caSApple OSS Distributions 		expect_filtered_event(tp, &filt_seen);
857*c54f35caSApple OSS Distributions 		expect_noprocfilt_event(tp, &noprocfilt_seen);
858*c54f35caSApple OSS Distributions 	};
859*c54f35caSApple OSS Distributions 
860*c54f35caSApple OSS Distributions 	if (use_all_callback) {
861*c54f35caSApple OSS Distributions 		ktrace_events_all(s, evtcb);
862*c54f35caSApple OSS Distributions 	} else {
863*c54f35caSApple OSS Distributions 		ktrace_events_range(s, KDBG_EVENTID(DBG_BSD, DBG_BSD_KDEBUG_TEST, 0),
864*c54f35caSApple OSS Distributions 		    KDBG_EVENTID(DBG_BSD + 1, 0, 0), evtcb);
865*c54f35caSApple OSS Distributions 	}
866*c54f35caSApple OSS Distributions 
867*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
868*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
869*c54f35caSApple OSS Distributions 		cb(dev_seen, rel_seen, filt_seen, noprocfilt_seen);
870*c54f35caSApple OSS Distributions 		T_END;
871*c54f35caSApple OSS Distributions 	});
872*c54f35caSApple OSS Distributions 
873*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_start(s, dispatch_get_main_queue()), NULL);
874*c54f35caSApple OSS Distributions 	assert_kdebug_test(KDBG_TEST_MACROS, "check test macros");
875*c54f35caSApple OSS Distributions 
876*c54f35caSApple OSS Distributions 	ktrace_end(s, 0);
877*c54f35caSApple OSS Distributions }
878*c54f35caSApple OSS Distributions 
879*c54f35caSApple OSS Distributions T_DECL(kernel_events, "ensure kernel macros work")
880*c54f35caSApple OSS Distributions {
881*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
882*c54f35caSApple OSS Distributions 
883*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
884*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
885*c54f35caSApple OSS Distributions 
886*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ktrace_filter_pid(s, getpid()),
887*c54f35caSApple OSS Distributions 	    "filtered events to current process");
888*c54f35caSApple OSS Distributions 
889*c54f35caSApple OSS Distributions 	expect_kdbg_test_events(s, false,
890*c54f35caSApple OSS Distributions 	    ^(unsigned int dev_seen, unsigned int rel_seen,
891*c54f35caSApple OSS Distributions 	    unsigned int filt_seen, unsigned int noprocfilt_seen) {
892*c54f35caSApple OSS Distributions 		/*
893*c54f35caSApple OSS Distributions 		 * Development-only events are only filtered if running on an embedded
894*c54f35caSApple OSS Distributions 		 * OS.
895*c54f35caSApple OSS Distributions 		 */
896*c54f35caSApple OSS Distributions 		unsigned int dev_exp;
897*c54f35caSApple OSS Distributions #if (TARGET_OS_IPHONE && !TARGET_OS_SIMULATOR)
898*c54f35caSApple OSS Distributions 		dev_exp = is_development_kernel() ? EXP_KERNEL_EVENTS : 0U;
899*c54f35caSApple OSS Distributions #else
900*c54f35caSApple OSS Distributions 		dev_exp = EXP_KERNEL_EVENTS;
901*c54f35caSApple OSS Distributions #endif
902*c54f35caSApple OSS Distributions 
903*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(rel_seen, EXP_KERNEL_EVENTS,
904*c54f35caSApple OSS Distributions 		"release and development events seen");
905*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(dev_seen, dev_exp, "development-only events %sseen",
906*c54f35caSApple OSS Distributions 		dev_exp ? "" : "not ");
907*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(filt_seen, dev_exp, "filter-only events seen");
908*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(noprocfilt_seen, EXP_KERNEL_EVENTS,
909*c54f35caSApple OSS Distributions 		"process filter-agnostic events seen");
910*c54f35caSApple OSS Distributions 	});
911*c54f35caSApple OSS Distributions 
912*c54f35caSApple OSS Distributions 	dispatch_main();
913*c54f35caSApple OSS Distributions }
914*c54f35caSApple OSS Distributions 
915*c54f35caSApple OSS Distributions T_DECL(kernel_events_filtered, "ensure that the filtered kernel macros work")
916*c54f35caSApple OSS Distributions {
917*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
918*c54f35caSApple OSS Distributions 
919*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
920*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
921*c54f35caSApple OSS Distributions 
922*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(ktrace_filter_pid(s, getpid()),
923*c54f35caSApple OSS Distributions 	    "filtered events to current process");
924*c54f35caSApple OSS Distributions 
925*c54f35caSApple OSS Distributions 	expect_kdbg_test_events(s, true,
926*c54f35caSApple OSS Distributions 	    ^(unsigned int dev_seen, unsigned int rel_seen,
927*c54f35caSApple OSS Distributions 	    unsigned int filt_seen, unsigned int noprocfilt_seen) {
928*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(rel_seen, EXP_KERNEL_EVENTS, NULL);
929*c54f35caSApple OSS Distributions #if defined(__arm64__)
930*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(dev_seen, is_development_kernel() ? EXP_KERNEL_EVENTS : 0U,
931*c54f35caSApple OSS Distributions 		NULL);
932*c54f35caSApple OSS Distributions #else
933*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(dev_seen, EXP_KERNEL_EVENTS,
934*c54f35caSApple OSS Distributions 		"development-only events seen");
935*c54f35caSApple OSS Distributions #endif /* defined(__arm64__) */
936*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(filt_seen, 0U, "no filter-only events seen");
937*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(noprocfilt_seen, EXP_KERNEL_EVENTS,
938*c54f35caSApple OSS Distributions 		"process filter-agnostic events seen");
939*c54f35caSApple OSS Distributions 	});
940*c54f35caSApple OSS Distributions 
941*c54f35caSApple OSS Distributions 	dispatch_main();
942*c54f35caSApple OSS Distributions }
943*c54f35caSApple OSS Distributions 
944*c54f35caSApple OSS Distributions T_DECL(kernel_events_noprocfilt,
945*c54f35caSApple OSS Distributions     "ensure that the no process filter kernel macros work")
946*c54f35caSApple OSS Distributions {
947*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
948*c54f35caSApple OSS Distributions 
949*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
950*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
951*c54f35caSApple OSS Distributions 
952*c54f35caSApple OSS Distributions 	/*
953*c54f35caSApple OSS Distributions 	 * Only allow launchd events through.
954*c54f35caSApple OSS Distributions 	 */
955*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(ktrace_filter_pid(s, 1), "filtered events to launchd");
956*c54f35caSApple OSS Distributions 	for (size_t i = 0; i < sizeof(noprocfilt_evts) / sizeof(noprocfilt_evts[0]); i++) {
957*c54f35caSApple OSS Distributions 		T_QUIET;
958*c54f35caSApple OSS Distributions 		T_ASSERT_POSIX_ZERO(ktrace_ignore_process_filter_for_event(s,
959*c54f35caSApple OSS Distributions 		    noprocfilt_evts[i]),
960*c54f35caSApple OSS Distributions 		    "ignored process filter for noprocfilt event");
961*c54f35caSApple OSS Distributions 	}
962*c54f35caSApple OSS Distributions 
963*c54f35caSApple OSS Distributions 	expect_kdbg_test_events(s, false,
964*c54f35caSApple OSS Distributions 	    ^(unsigned int dev_seen, unsigned int rel_seen,
965*c54f35caSApple OSS Distributions 	    unsigned int filt_seen, unsigned int noprocfilt_seen) {
966*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(rel_seen, 0U, "release and development events not seen");
967*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(dev_seen, 0U, "development-only events not seen");
968*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(filt_seen, 0U, "filter-only events not seen");
969*c54f35caSApple OSS Distributions 
970*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(noprocfilt_seen, EXP_KERNEL_EVENTS,
971*c54f35caSApple OSS Distributions 		"process filter-agnostic events seen");
972*c54f35caSApple OSS Distributions 	});
973*c54f35caSApple OSS Distributions 
974*c54f35caSApple OSS Distributions 	dispatch_main();
975*c54f35caSApple OSS Distributions }
976*c54f35caSApple OSS Distributions 
977*c54f35caSApple OSS Distributions static volatile bool continue_abuse = true;
978*c54f35caSApple OSS Distributions 
979*c54f35caSApple OSS Distributions #define STRESS_DEBUGID (0xfeedfac0)
980*c54f35caSApple OSS Distributions #define ABUSE_SECS (2)
981*c54f35caSApple OSS Distributions #define TIMER_NS (100 * NSEC_PER_USEC)
982*c54f35caSApple OSS Distributions /*
983*c54f35caSApple OSS Distributions  * Use the quantum as the gap threshold.
984*c54f35caSApple OSS Distributions  */
985*c54f35caSApple OSS Distributions #define GAP_THRESHOLD_NS (10 * NSEC_PER_MSEC)
986*c54f35caSApple OSS Distributions 
987*c54f35caSApple OSS Distributions static void *
kdebug_abuser_thread(void * ctx)988*c54f35caSApple OSS Distributions kdebug_abuser_thread(void *ctx)
989*c54f35caSApple OSS Distributions {
990*c54f35caSApple OSS Distributions 	unsigned int id = (unsigned int)ctx;
991*c54f35caSApple OSS Distributions 	uint64_t i = 0;
992*c54f35caSApple OSS Distributions 	while (continue_abuse) {
993*c54f35caSApple OSS Distributions 		kdebug_trace(STRESS_DEBUGID, id, i, 0, 0);
994*c54f35caSApple OSS Distributions 		i++;
995*c54f35caSApple OSS Distributions 	}
996*c54f35caSApple OSS Distributions 
997*c54f35caSApple OSS Distributions 	return NULL;
998*c54f35caSApple OSS Distributions }
999*c54f35caSApple OSS Distributions 
1000*c54f35caSApple OSS Distributions T_DECL(stress, "emit events on all but one CPU with a small buffer",
1001*c54f35caSApple OSS Distributions     T_META_CHECK_LEAKS(false))
1002*c54f35caSApple OSS Distributions {
1003*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
1004*c54f35caSApple OSS Distributions 
1005*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
1006*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
1007*c54f35caSApple OSS Distributions 	T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(s, "ktrace_session_create");
1008*c54f35caSApple OSS Distributions 
1009*c54f35caSApple OSS Distributions 	/* Let's not waste any time with pleasantries. */
1010*c54f35caSApple OSS Distributions 	ktrace_set_uuid_map_enabled(s, KTRACE_FEATURE_DISABLED);
1011*c54f35caSApple OSS Distributions 
1012*c54f35caSApple OSS Distributions 	/* Ouch. */
1013*c54f35caSApple OSS Distributions 	ktrace_events_all(s, ^(__unused struct trace_point *tp) {});
1014*c54f35caSApple OSS Distributions 	ktrace_set_vnode_paths_enabled(s, KTRACE_FEATURE_ENABLED);
1015*c54f35caSApple OSS Distributions 	(void)atexit_b(^{ kperf_reset(); });
1016*c54f35caSApple OSS Distributions 	(void)kperf_action_count_set(1);
1017*c54f35caSApple OSS Distributions 	(void)kperf_timer_count_set(1);
1018*c54f35caSApple OSS Distributions 	int kperror = kperf_timer_period_set(0, kperf_ns_to_ticks(TIMER_NS));
1019*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kperror, "kperf_timer_period_set %llu ns",
1020*c54f35caSApple OSS Distributions 	    TIMER_NS);
1021*c54f35caSApple OSS Distributions 	kperror = kperf_timer_action_set(0, 1);
1022*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kperror, "kperf_timer_action_set");
1023*c54f35caSApple OSS Distributions 	kperror = kperf_action_samplers_set(1, KPERF_SAMPLER_TINFO |
1024*c54f35caSApple OSS Distributions 	    KPERF_SAMPLER_TH_SNAPSHOT | KPERF_SAMPLER_KSTACK |
1025*c54f35caSApple OSS Distributions 	    KPERF_SAMPLER_USTACK | KPERF_SAMPLER_MEMINFO |
1026*c54f35caSApple OSS Distributions 	    KPERF_SAMPLER_TINFO_SCHED | KPERF_SAMPLER_TH_DISPATCH |
1027*c54f35caSApple OSS Distributions 	    KPERF_SAMPLER_TK_SNAPSHOT | KPERF_SAMPLER_SYS_MEM |
1028*c54f35caSApple OSS Distributions 	    KPERF_SAMPLER_TH_INSTRS_CYCLES);
1029*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(kperror, "kperf_action_samplers_set");
1030*c54f35caSApple OSS Distributions 	/* You monster... */
1031*c54f35caSApple OSS Distributions 
1032*c54f35caSApple OSS Distributions 	/* The coup-de-grace. */
1033*c54f35caSApple OSS Distributions 	ktrace_set_buffer_size(s, 10);
1034*c54f35caSApple OSS Distributions 
1035*c54f35caSApple OSS Distributions 	char filepath_arr[MAXPATHLEN] = "";
1036*c54f35caSApple OSS Distributions 	strlcpy(filepath_arr, dt_tmpdir(), sizeof(filepath_arr));
1037*c54f35caSApple OSS Distributions 	strlcat(filepath_arr, "/stress.ktrace", sizeof(filepath_arr));
1038*c54f35caSApple OSS Distributions 	char *filepath = filepath_arr;
1039*c54f35caSApple OSS Distributions 
1040*c54f35caSApple OSS Distributions 	int ncpus = 0;
1041*c54f35caSApple OSS Distributions 	size_t ncpus_size = sizeof(ncpus);
1042*c54f35caSApple OSS Distributions 	int ret = sysctlbyname("hw.logicalcpu_max", &ncpus, &ncpus_size, NULL, 0);
1043*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctlbyname(\"hw.logicalcpu_max\"");
1044*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_GT(ncpus, 0, "realistic number of CPUs");
1045*c54f35caSApple OSS Distributions 
1046*c54f35caSApple OSS Distributions 	pthread_t *threads = calloc((unsigned int)ncpus - 1, sizeof(pthread_t));
1047*c54f35caSApple OSS Distributions 	T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(threads, "calloc(%d threads)",
1048*c54f35caSApple OSS Distributions 	    ncpus - 1);
1049*c54f35caSApple OSS Distributions 
1050*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
1051*c54f35caSApple OSS Distributions 		T_SETUPBEGIN;
1052*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
1053*c54f35caSApple OSS Distributions 
1054*c54f35caSApple OSS Distributions 		T_LOG("trace ended, searching for gaps");
1055*c54f35caSApple OSS Distributions 
1056*c54f35caSApple OSS Distributions 		ktrace_session_t sread = ktrace_session_create();
1057*c54f35caSApple OSS Distributions 		T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(sread, "ktrace_session_create");
1058*c54f35caSApple OSS Distributions 
1059*c54f35caSApple OSS Distributions 		int error = ktrace_set_file(sread, filepath);
1060*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(error, "ktrace_set_file %s", filepath);
1061*c54f35caSApple OSS Distributions 
1062*c54f35caSApple OSS Distributions 		ktrace_file_t f = ktrace_file_open(filepath, false);
1063*c54f35caSApple OSS Distributions 		T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(f, "ktrace_file_open %s",
1064*c54f35caSApple OSS Distributions 		filepath);
1065*c54f35caSApple OSS Distributions 		uint64_t first_timestamp = 0;
1066*c54f35caSApple OSS Distributions 		error = ktrace_file_earliest_timestamp(f, &first_timestamp);
1067*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(error, "ktrace_file_earliest_timestamp");
1068*c54f35caSApple OSS Distributions 
1069*c54f35caSApple OSS Distributions 		uint64_t last_timestamp = 0;
1070*c54f35caSApple OSS Distributions 		(void)ktrace_file_latest_timestamp(f, &last_timestamp);
1071*c54f35caSApple OSS Distributions 
1072*c54f35caSApple OSS Distributions 		__block uint64_t prev_timestamp = 0;
1073*c54f35caSApple OSS Distributions 		__block uint64_t nevents = 0;
1074*c54f35caSApple OSS Distributions 		ktrace_events_all(sread, ^(struct trace_point *tp) {
1075*c54f35caSApple OSS Distributions 			nevents++;
1076*c54f35caSApple OSS Distributions 			uint64_t delta_ns = 0;
1077*c54f35caSApple OSS Distributions 			T_QUIET; T_EXPECT_GE(tp->timestamp, prev_timestamp,
1078*c54f35caSApple OSS Distributions 			"timestamps are monotonically increasing");
1079*c54f35caSApple OSS Distributions 			int converror = ktrace_convert_timestamp_to_nanoseconds(sread,
1080*c54f35caSApple OSS Distributions 			tp->timestamp - prev_timestamp, &delta_ns);
1081*c54f35caSApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_ZERO(converror, "convert timestamp to ns");
1082*c54f35caSApple OSS Distributions 			if (prev_timestamp && delta_ns > GAP_THRESHOLD_NS) {
1083*c54f35caSApple OSS Distributions 			        if (tp->debugname) {
1084*c54f35caSApple OSS Distributions 			                T_LOG("gap: %gs at %llu - %llu on %d: %s (%#08x)",
1085*c54f35caSApple OSS Distributions 			                (double)delta_ns / 1e9, prev_timestamp,
1086*c54f35caSApple OSS Distributions 			                tp->timestamp, tp->cpuid, tp->debugname, tp->debugid);
1087*c54f35caSApple OSS Distributions 				} else {
1088*c54f35caSApple OSS Distributions 			                T_LOG("gap: %gs at %llu - %llu on %d: %#x",
1089*c54f35caSApple OSS Distributions 			                (double)delta_ns / 1e9, prev_timestamp,
1090*c54f35caSApple OSS Distributions 			                tp->timestamp, tp->cpuid, tp->debugid);
1091*c54f35caSApple OSS Distributions 				}
1092*c54f35caSApple OSS Distributions 
1093*c54f35caSApple OSS Distributions 			        /*
1094*c54f35caSApple OSS Distributions 			         * These gaps are ok -- they appear after CPUs are brought back
1095*c54f35caSApple OSS Distributions 			         * up.
1096*c54f35caSApple OSS Distributions 			         */
1097*c54f35caSApple OSS Distributions #define INTERRUPT (0x1050000)
1098*c54f35caSApple OSS Distributions #define PERF_CPU_IDLE (0x27001000)
1099*c54f35caSApple OSS Distributions #define INTC_HANDLER (0x5000004)
1100*c54f35caSApple OSS Distributions #define DECR_TRAP (0x1090000)
1101*c54f35caSApple OSS Distributions 			        uint32_t eventid = tp->debugid & KDBG_EVENTID_MASK;
1102*c54f35caSApple OSS Distributions 			        if (eventid != INTERRUPT && eventid != PERF_CPU_IDLE &&
1103*c54f35caSApple OSS Distributions 			        eventid != INTC_HANDLER && eventid != DECR_TRAP) {
1104*c54f35caSApple OSS Distributions 			                unsigned int lost_events = TRACE_LOST_EVENTS;
1105*c54f35caSApple OSS Distributions 			                T_QUIET; T_EXPECT_EQ(tp->debugid, lost_events,
1106*c54f35caSApple OSS Distributions 			                "gaps should end with lost events");
1107*c54f35caSApple OSS Distributions 				}
1108*c54f35caSApple OSS Distributions 			}
1109*c54f35caSApple OSS Distributions 
1110*c54f35caSApple OSS Distributions 			prev_timestamp = tp->timestamp;
1111*c54f35caSApple OSS Distributions 		});
1112*c54f35caSApple OSS Distributions 		ktrace_events_single(sread, TRACE_LOST_EVENTS, ^(struct trace_point *tp){
1113*c54f35caSApple OSS Distributions 			T_LOG("lost: %llu on %d (%llu)", tp->timestamp, tp->cpuid, tp->arg1);
1114*c54f35caSApple OSS Distributions 		});
1115*c54f35caSApple OSS Distributions 
1116*c54f35caSApple OSS Distributions 		__block uint64_t last_write = 0;
1117*c54f35caSApple OSS Distributions 		ktrace_events_single_paired(sread, TRACE_WRITING_EVENTS,
1118*c54f35caSApple OSS Distributions 		^(struct trace_point *start, struct trace_point *end) {
1119*c54f35caSApple OSS Distributions 			uint64_t delta_ns;
1120*c54f35caSApple OSS Distributions 			int converror = ktrace_convert_timestamp_to_nanoseconds(sread,
1121*c54f35caSApple OSS Distributions 			start->timestamp - last_write, &delta_ns);
1122*c54f35caSApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_ZERO(converror, "convert timestamp to ns");
1123*c54f35caSApple OSS Distributions 
1124*c54f35caSApple OSS Distributions 			uint64_t dur_ns;
1125*c54f35caSApple OSS Distributions 			converror = ktrace_convert_timestamp_to_nanoseconds(sread,
1126*c54f35caSApple OSS Distributions 			end->timestamp - start->timestamp, &dur_ns);
1127*c54f35caSApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_ZERO(converror, "convert timestamp to ns");
1128*c54f35caSApple OSS Distributions 
1129*c54f35caSApple OSS Distributions 			T_LOG("write: %llu (+%gs): %gus on %d: %llu events", start->timestamp,
1130*c54f35caSApple OSS Distributions 			(double)delta_ns / 1e9, (double)dur_ns / 1e3, end->cpuid, end->arg1);
1131*c54f35caSApple OSS Distributions 			last_write = end->timestamp;
1132*c54f35caSApple OSS Distributions 		});
1133*c54f35caSApple OSS Distributions 		ktrace_set_completion_handler(sread, ^{
1134*c54f35caSApple OSS Distributions 			uint64_t duration_ns = 0;
1135*c54f35caSApple OSS Distributions 			if (last_timestamp) {
1136*c54f35caSApple OSS Distributions 			        int converror = ktrace_convert_timestamp_to_nanoseconds(sread,
1137*c54f35caSApple OSS Distributions 			        last_timestamp - first_timestamp, &duration_ns);
1138*c54f35caSApple OSS Distributions 			        T_QUIET; T_ASSERT_POSIX_ZERO(converror,
1139*c54f35caSApple OSS Distributions 			        "convert timestamp to ns");
1140*c54f35caSApple OSS Distributions 			        T_LOG("file was %gs long, %llu events: %g events/msec/cpu",
1141*c54f35caSApple OSS Distributions 			        (double)duration_ns / 1e9, nevents,
1142*c54f35caSApple OSS Distributions 			        (double)nevents / ((double)duration_ns / 1e6) / ncpus);
1143*c54f35caSApple OSS Distributions 			}
1144*c54f35caSApple OSS Distributions 			(void)unlink(filepath);
1145*c54f35caSApple OSS Distributions 			ktrace_session_destroy(sread);
1146*c54f35caSApple OSS Distributions 			T_END;
1147*c54f35caSApple OSS Distributions 		});
1148*c54f35caSApple OSS Distributions 
1149*c54f35caSApple OSS Distributions 		int starterror = ktrace_start(sread, dispatch_get_main_queue());
1150*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(starterror, "ktrace_start read session");
1151*c54f35caSApple OSS Distributions 
1152*c54f35caSApple OSS Distributions 		T_SETUPEND;
1153*c54f35caSApple OSS Distributions 	});
1154*c54f35caSApple OSS Distributions 
1155*c54f35caSApple OSS Distributions /* Just kidding... for now. */
1156*c54f35caSApple OSS Distributions #if 0
1157*c54f35caSApple OSS Distributions 	kperror = kperf_sample_set(1);
1158*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(kperror,
1159*c54f35caSApple OSS Distributions 	    "started kperf timer sampling every %llu ns", TIMER_NS);
1160*c54f35caSApple OSS Distributions #endif
1161*c54f35caSApple OSS Distributions 
1162*c54f35caSApple OSS Distributions 	for (int i = 0; i < (ncpus - 1); i++) {
1163*c54f35caSApple OSS Distributions 		int error = pthread_create(&threads[i], NULL, kdebug_abuser_thread,
1164*c54f35caSApple OSS Distributions 		    (void *)(uintptr_t)i);
1165*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(error,
1166*c54f35caSApple OSS Distributions 		    "pthread_create abuser thread %d", i);
1167*c54f35caSApple OSS Distributions 	}
1168*c54f35caSApple OSS Distributions 
1169*c54f35caSApple OSS Distributions 	int error = ktrace_start_writing_file(s, filepath,
1170*c54f35caSApple OSS Distributions 	    ktrace_compression_none, NULL, NULL);
1171*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started writing ktrace to %s", filepath);
1172*c54f35caSApple OSS Distributions 
1173*c54f35caSApple OSS Distributions 	T_SETUPEND;
1174*c54f35caSApple OSS Distributions 
1175*c54f35caSApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, ABUSE_SECS * NSEC_PER_SEC),
1176*c54f35caSApple OSS Distributions 	    dispatch_get_main_queue(), ^{
1177*c54f35caSApple OSS Distributions 		T_LOG("ending trace");
1178*c54f35caSApple OSS Distributions 		ktrace_end(s, 1);
1179*c54f35caSApple OSS Distributions 
1180*c54f35caSApple OSS Distributions 		continue_abuse = false;
1181*c54f35caSApple OSS Distributions 		for (int i = 0; i < (ncpus - 1); i++) {
1182*c54f35caSApple OSS Distributions 		        int joinerror = pthread_join(threads[i], NULL);
1183*c54f35caSApple OSS Distributions 		        T_QUIET; T_EXPECT_POSIX_ZERO(joinerror, "pthread_join thread %d",
1184*c54f35caSApple OSS Distributions 		        i);
1185*c54f35caSApple OSS Distributions 		}
1186*c54f35caSApple OSS Distributions 	});
1187*c54f35caSApple OSS Distributions 
1188*c54f35caSApple OSS Distributions 	dispatch_main();
1189*c54f35caSApple OSS Distributions }
1190*c54f35caSApple OSS Distributions 
1191*c54f35caSApple OSS Distributions #define ROUND_TRIP_PERIOD UINT64_C(10 * 1000)
1192*c54f35caSApple OSS Distributions #define ROUND_TRIPS_THRESHOLD UINT64_C(25)
1193*c54f35caSApple OSS Distributions #define ROUND_TRIPS_TIMEOUT_SECS (2 * 60)
1194*c54f35caSApple OSS Distributions #define COLLECTION_INTERVAL_MS 100
1195*c54f35caSApple OSS Distributions 
1196*c54f35caSApple OSS Distributions /*
1197*c54f35caSApple OSS Distributions  * Test a sustained tracing session, involving multiple round-trips to the
1198*c54f35caSApple OSS Distributions  * kernel.
1199*c54f35caSApple OSS Distributions  *
1200*c54f35caSApple OSS Distributions  * Trace all events, and every `ROUND_TRIP_PERIOD` events, emit an event that's
1201*c54f35caSApple OSS Distributions  * unlikely to be emitted elsewhere.  Look for this event, too, and make sure we
1202*c54f35caSApple OSS Distributions  * see as many of them as we emitted.
1203*c54f35caSApple OSS Distributions  *
1204*c54f35caSApple OSS Distributions  * After seeing `ROUND_TRIPS_THRESHOLD` of the unlikely events, end tracing.
1205*c54f35caSApple OSS Distributions  * In the failure mode, we won't see any of these, so set a timeout of
1206*c54f35caSApple OSS Distributions  * `ROUND_TRIPS_TIMEOUT_SECS` to prevent hanging, waiting for events that we'll
1207*c54f35caSApple OSS Distributions  * never see.
1208*c54f35caSApple OSS Distributions  */
1209*c54f35caSApple OSS Distributions T_DECL(round_trips,
1210*c54f35caSApple OSS Distributions     "test sustained tracing with multiple round-trips through the kernel")
1211*c54f35caSApple OSS Distributions {
1212*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
1213*c54f35caSApple OSS Distributions 
1214*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
1215*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
1216*c54f35caSApple OSS Distributions 
1217*c54f35caSApple OSS Distributions 	/*
1218*c54f35caSApple OSS Distributions 	 * Set a small buffer and collection interval to increase the number of
1219*c54f35caSApple OSS Distributions 	 * round-trips.
1220*c54f35caSApple OSS Distributions 	 */
1221*c54f35caSApple OSS Distributions 	ktrace_set_buffer_size(s, 50);
1222*c54f35caSApple OSS Distributions 	ktrace_set_collection_interval(s, COLLECTION_INTERVAL_MS);
1223*c54f35caSApple OSS Distributions 
1224*c54f35caSApple OSS Distributions 	__block uint64_t events = 0;
1225*c54f35caSApple OSS Distributions 	__block uint64_t emitted = 0;
1226*c54f35caSApple OSS Distributions 	__block uint64_t seen = 0;
1227*c54f35caSApple OSS Distributions 	ktrace_events_all(s, ^(__unused struct trace_point *tp) {
1228*c54f35caSApple OSS Distributions 		events++;
1229*c54f35caSApple OSS Distributions 		if (events % ROUND_TRIP_PERIOD == 0) {
1230*c54f35caSApple OSS Distributions 		        T_LOG("emitting round-trip event %" PRIu64, emitted);
1231*c54f35caSApple OSS Distributions 		        kdebug_trace(TRACE_DEBUGID, events, 0, 0, 0);
1232*c54f35caSApple OSS Distributions 		        emitted++;
1233*c54f35caSApple OSS Distributions 		}
1234*c54f35caSApple OSS Distributions 	});
1235*c54f35caSApple OSS Distributions 
1236*c54f35caSApple OSS Distributions 	ktrace_events_single(s, TRACE_DEBUGID, ^(__unused struct trace_point *tp) {
1237*c54f35caSApple OSS Distributions 		T_LOG("saw round-trip event after %" PRIu64 " events", events);
1238*c54f35caSApple OSS Distributions 		seen++;
1239*c54f35caSApple OSS Distributions 		if (seen >= ROUND_TRIPS_THRESHOLD) {
1240*c54f35caSApple OSS Distributions 		        T_LOG("ending trace after seeing %" PRIu64 " events, "
1241*c54f35caSApple OSS Distributions 		        "emitting %" PRIu64, seen, emitted);
1242*c54f35caSApple OSS Distributions 		        ktrace_end(s, 1);
1243*c54f35caSApple OSS Distributions 		}
1244*c54f35caSApple OSS Distributions 	});
1245*c54f35caSApple OSS Distributions 
1246*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
1247*c54f35caSApple OSS Distributions 		T_EXPECT_GE(emitted, ROUND_TRIPS_THRESHOLD,
1248*c54f35caSApple OSS Distributions 		"emitted %" PRIu64 " round-trip events", emitted);
1249*c54f35caSApple OSS Distributions 		T_EXPECT_GE(seen, ROUND_TRIPS_THRESHOLD,
1250*c54f35caSApple OSS Distributions 		"saw %" PRIu64 " round-trip events", seen);
1251*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
1252*c54f35caSApple OSS Distributions 		T_END;
1253*c54f35caSApple OSS Distributions 	});
1254*c54f35caSApple OSS Distributions 
1255*c54f35caSApple OSS Distributions 	int error = ktrace_start(s, dispatch_get_main_queue());
1256*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started tracing");
1257*c54f35caSApple OSS Distributions 
1258*c54f35caSApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
1259*c54f35caSApple OSS Distributions 	    ROUND_TRIPS_TIMEOUT_SECS * NSEC_PER_SEC), dispatch_get_main_queue(),
1260*c54f35caSApple OSS Distributions 	    ^{
1261*c54f35caSApple OSS Distributions 		T_LOG("ending trace after %d seconds", ROUND_TRIPS_TIMEOUT_SECS);
1262*c54f35caSApple OSS Distributions 		ktrace_end(s, 0);
1263*c54f35caSApple OSS Distributions 	});
1264*c54f35caSApple OSS Distributions 
1265*c54f35caSApple OSS Distributions 	dispatch_main();
1266*c54f35caSApple OSS Distributions }
1267*c54f35caSApple OSS Distributions 
1268*c54f35caSApple OSS Distributions #define HEARTBEAT_INTERVAL_SECS 1
1269*c54f35caSApple OSS Distributions #define HEARTBEAT_COUNT 10
1270*c54f35caSApple OSS Distributions 
1271*c54f35caSApple OSS Distributions /*
1272*c54f35caSApple OSS Distributions  * Ensure we see events periodically, checking for recent events on a
1273*c54f35caSApple OSS Distributions  * heart-beat.
1274*c54f35caSApple OSS Distributions  */
1275*c54f35caSApple OSS Distributions T_DECL(event_coverage, "ensure events appear up to the end of tracing")
1276*c54f35caSApple OSS Distributions {
1277*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
1278*c54f35caSApple OSS Distributions 
1279*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
1280*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
1281*c54f35caSApple OSS Distributions 
1282*c54f35caSApple OSS Distributions 	__block uint64_t current_timestamp = 0;
1283*c54f35caSApple OSS Distributions 	__block uint64_t events = 0;
1284*c54f35caSApple OSS Distributions 	ktrace_events_all(s, ^(struct trace_point *tp) {
1285*c54f35caSApple OSS Distributions 		current_timestamp = tp->timestamp;
1286*c54f35caSApple OSS Distributions 		events++;
1287*c54f35caSApple OSS Distributions 	});
1288*c54f35caSApple OSS Distributions 
1289*c54f35caSApple OSS Distributions 	ktrace_set_buffer_size(s, 20);
1290*c54f35caSApple OSS Distributions 	ktrace_set_collection_interval(s, COLLECTION_INTERVAL_MS);
1291*c54f35caSApple OSS Distributions 
1292*c54f35caSApple OSS Distributions 	__block uint64_t last_timestamp = 0;
1293*c54f35caSApple OSS Distributions 	__block uint64_t last_events = 0;
1294*c54f35caSApple OSS Distributions 	__block unsigned int heartbeats = 0;
1295*c54f35caSApple OSS Distributions 
1296*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
1297*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
1298*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_GT(events, 0ULL, "should have seen some events");
1299*c54f35caSApple OSS Distributions 		T_END;
1300*c54f35caSApple OSS Distributions 	});
1301*c54f35caSApple OSS Distributions 
1302*c54f35caSApple OSS Distributions 	dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
1303*c54f35caSApple OSS Distributions 	    0, 0, dispatch_get_main_queue());
1304*c54f35caSApple OSS Distributions 	dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW,
1305*c54f35caSApple OSS Distributions 	    HEARTBEAT_INTERVAL_SECS * NSEC_PER_SEC),
1306*c54f35caSApple OSS Distributions 	    HEARTBEAT_INTERVAL_SECS * NSEC_PER_SEC, 0);
1307*c54f35caSApple OSS Distributions 	dispatch_source_set_cancel_handler(timer, ^{
1308*c54f35caSApple OSS Distributions 		dispatch_release(timer);
1309*c54f35caSApple OSS Distributions 	});
1310*c54f35caSApple OSS Distributions 
1311*c54f35caSApple OSS Distributions 	dispatch_source_set_event_handler(timer, ^{
1312*c54f35caSApple OSS Distributions 		heartbeats++;
1313*c54f35caSApple OSS Distributions 
1314*c54f35caSApple OSS Distributions 		T_LOG("heartbeat %u at time %lld, seen %" PRIu64 " events, "
1315*c54f35caSApple OSS Distributions 		"current event time %lld", heartbeats, mach_absolute_time(),
1316*c54f35caSApple OSS Distributions 		events, current_timestamp);
1317*c54f35caSApple OSS Distributions 
1318*c54f35caSApple OSS Distributions 		if (current_timestamp > 0) {
1319*c54f35caSApple OSS Distributions 		        T_EXPECT_GT(current_timestamp, last_timestamp,
1320*c54f35caSApple OSS Distributions 		        "event timestamps should be increasing");
1321*c54f35caSApple OSS Distributions 		        T_QUIET; T_EXPECT_GT(events, last_events,
1322*c54f35caSApple OSS Distributions 		        "number of events should be increasing");
1323*c54f35caSApple OSS Distributions 		}
1324*c54f35caSApple OSS Distributions 
1325*c54f35caSApple OSS Distributions 		last_timestamp = current_timestamp;
1326*c54f35caSApple OSS Distributions 		last_events = events;
1327*c54f35caSApple OSS Distributions 
1328*c54f35caSApple OSS Distributions 		kdebug_trace(TRACE_DEBUGID, 0, 0, 0, 0);
1329*c54f35caSApple OSS Distributions 
1330*c54f35caSApple OSS Distributions 		if (heartbeats >= HEARTBEAT_COUNT) {
1331*c54f35caSApple OSS Distributions 		        T_LOG("ending trace after %u heartbeats", HEARTBEAT_COUNT);
1332*c54f35caSApple OSS Distributions 		        ktrace_end(s, 0);
1333*c54f35caSApple OSS Distributions 		}
1334*c54f35caSApple OSS Distributions 	});
1335*c54f35caSApple OSS Distributions 
1336*c54f35caSApple OSS Distributions 	int error = ktrace_start(s, dispatch_get_main_queue());
1337*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started tracing");
1338*c54f35caSApple OSS Distributions 
1339*c54f35caSApple OSS Distributions 	dispatch_activate(timer);
1340*c54f35caSApple OSS Distributions 
1341*c54f35caSApple OSS Distributions 	dispatch_main();
1342*c54f35caSApple OSS Distributions }
1343*c54f35caSApple OSS Distributions 
1344*c54f35caSApple OSS Distributions static unsigned int
get_nevents(void)1345*c54f35caSApple OSS Distributions get_nevents(void)
1346*c54f35caSApple OSS Distributions {
1347*c54f35caSApple OSS Distributions 	kbufinfo_t bufinfo = { 0 };
1348*c54f35caSApple OSS Distributions 	T_QUIET;
1349*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(
1350*c54f35caSApple OSS Distributions 		    (int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDGETBUF }, 3,
1351*c54f35caSApple OSS Distributions 		    &bufinfo, &(size_t){ sizeof(bufinfo) }, NULL, 0),
1352*c54f35caSApple OSS Distributions 	    "get kdebug buffer size");
1353*c54f35caSApple OSS Distributions 
1354*c54f35caSApple OSS Distributions 	return (unsigned int)bufinfo.nkdbufs;
1355*c54f35caSApple OSS Distributions }
1356*c54f35caSApple OSS Distributions 
1357*c54f35caSApple OSS Distributions static unsigned int
set_nevents(unsigned int nevents)1358*c54f35caSApple OSS Distributions set_nevents(unsigned int nevents)
1359*c54f35caSApple OSS Distributions {
1360*c54f35caSApple OSS Distributions 	T_QUIET;
1361*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(
1362*c54f35caSApple OSS Distributions 		    (int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDSETBUF, (int)nevents }, 4,
1363*c54f35caSApple OSS Distributions 		    NULL, 0, NULL, 0), "set kdebug buffer size");
1364*c54f35caSApple OSS Distributions 
1365*c54f35caSApple OSS Distributions 	T_QUIET;
1366*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(
1367*c54f35caSApple OSS Distributions 		    (int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDSETUP, (int)nevents }, 4,
1368*c54f35caSApple OSS Distributions 		    NULL, 0, NULL, 0), "setup kdebug buffers");
1369*c54f35caSApple OSS Distributions 
1370*c54f35caSApple OSS Distributions 	unsigned int nevents_allocated = get_nevents();
1371*c54f35caSApple OSS Distributions 
1372*c54f35caSApple OSS Distributions 	T_QUIET;
1373*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(sysctl(
1374*c54f35caSApple OSS Distributions 		    (int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDREMOVE }, 3,
1375*c54f35caSApple OSS Distributions 		    NULL, 0, NULL, 0),
1376*c54f35caSApple OSS Distributions 	    "remove kdebug buffers");
1377*c54f35caSApple OSS Distributions 
1378*c54f35caSApple OSS Distributions 	return nevents_allocated;
1379*c54f35caSApple OSS Distributions }
1380*c54f35caSApple OSS Distributions 
1381*c54f35caSApple OSS Distributions T_DECL(set_buffer_size, "ensure large buffer sizes can be set",
1382*c54f35caSApple OSS Distributions 		XNU_T_META_SOC_SPECIFIC)
1383*c54f35caSApple OSS Distributions {
1384*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
1385*c54f35caSApple OSS Distributions 	uint64_t memsize = 0;
1386*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(sysctlbyname("hw.memsize", &memsize,
1387*c54f35caSApple OSS Distributions 	    &(size_t){ sizeof(memsize) }, NULL, 0), "sysctl hw.memsize");
1388*c54f35caSApple OSS Distributions 	T_SETUPEND;
1389*c54f35caSApple OSS Distributions 
1390*c54f35caSApple OSS Distributions #if TARGET_OS_IPHONE
1391*c54f35caSApple OSS Distributions 	if (memsize >= (8ULL << 30)) {
1392*c54f35caSApple OSS Distributions 		T_SKIP("skipping on iOS device with memory >= 8GB, rdar://79403304");
1393*c54f35caSApple OSS Distributions 	}
1394*c54f35caSApple OSS Distributions #endif // TARGET_OS_IPHONE
1395*c54f35caSApple OSS Distributions 
1396*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
1397*c54f35caSApple OSS Distributions 
1398*c54f35caSApple OSS Distributions 	/*
1399*c54f35caSApple OSS Distributions 	 * Try to allocate up to one-eighth of available memory towards
1400*c54f35caSApple OSS Distributions 	 * tracing.
1401*c54f35caSApple OSS Distributions 	 */
1402*c54f35caSApple OSS Distributions 	uint64_t maxevents_u64 = memsize / 8 / sizeof(kd_buf);
1403*c54f35caSApple OSS Distributions 	if (maxevents_u64 > UINT32_MAX) {
1404*c54f35caSApple OSS Distributions 		maxevents_u64 = UINT32_MAX;
1405*c54f35caSApple OSS Distributions 	}
1406*c54f35caSApple OSS Distributions 	unsigned int maxevents = (unsigned int)maxevents_u64;
1407*c54f35caSApple OSS Distributions 
1408*c54f35caSApple OSS Distributions 	unsigned int minevents = set_nevents(0);
1409*c54f35caSApple OSS Distributions 	T_ASSERT_GT(minevents, 0, "saw non-zero minimum event count of %u",
1410*c54f35caSApple OSS Distributions 	    minevents);
1411*c54f35caSApple OSS Distributions 
1412*c54f35caSApple OSS Distributions 	unsigned int step = ((maxevents - minevents - 1) / 4);
1413*c54f35caSApple OSS Distributions 	T_ASSERT_GT(step, 0, "stepping by %u events", step);
1414*c54f35caSApple OSS Distributions 
1415*c54f35caSApple OSS Distributions 	for (unsigned int i = minevents + step; i < maxevents; i += step) {
1416*c54f35caSApple OSS Distributions 		unsigned int actualevents = set_nevents(i);
1417*c54f35caSApple OSS Distributions 		T_ASSERT_GE(actualevents, i - minevents,
1418*c54f35caSApple OSS Distributions 		    "%u events in kernel when %u requested", actualevents, i);
1419*c54f35caSApple OSS Distributions 	}
1420*c54f35caSApple OSS Distributions }
1421*c54f35caSApple OSS Distributions 
1422*c54f35caSApple OSS Distributions static void *
donothing(__unused void * arg)1423*c54f35caSApple OSS Distributions donothing(__unused void *arg)
1424*c54f35caSApple OSS Distributions {
1425*c54f35caSApple OSS Distributions 	return NULL;
1426*c54f35caSApple OSS Distributions }
1427*c54f35caSApple OSS Distributions 
1428*c54f35caSApple OSS Distributions T_DECL(long_names, "ensure long command names are reported")
1429*c54f35caSApple OSS Distributions {
1430*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
1431*c54f35caSApple OSS Distributions 
1432*c54f35caSApple OSS Distributions 	char longname[] = "thisisaverylongprocessname!";
1433*c54f35caSApple OSS Distributions 	char *longname_ptr = longname;
1434*c54f35caSApple OSS Distributions 	static_assert(sizeof(longname) > 16,
1435*c54f35caSApple OSS Distributions 	    "the name should be longer than MAXCOMLEN");
1436*c54f35caSApple OSS Distributions 
1437*c54f35caSApple OSS Distributions 	int ret = sysctlbyname("kern.procname", NULL, NULL, longname,
1438*c54f35caSApple OSS Distributions 	    sizeof(longname));
1439*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ret,
1440*c54f35caSApple OSS Distributions 	    "use sysctl kern.procname to lengthen the name");
1441*c54f35caSApple OSS Distributions 
1442*c54f35caSApple OSS Distributions 	ktrace_session_t ktsess = ktrace_session_create();
1443*c54f35caSApple OSS Distributions 
1444*c54f35caSApple OSS Distributions 	/*
1445*c54f35caSApple OSS Distributions 	 * 32-bit kernels can only trace 16 bytes of the string in their event
1446*c54f35caSApple OSS Distributions 	 * arguments.
1447*c54f35caSApple OSS Distributions 	 */
1448*c54f35caSApple OSS Distributions 	if (!ktrace_is_kernel_64_bit(ktsess)) {
1449*c54f35caSApple OSS Distributions 		longname[16] = '\0';
1450*c54f35caSApple OSS Distributions 	}
1451*c54f35caSApple OSS Distributions 
1452*c54f35caSApple OSS Distributions 	ktrace_filter_pid(ktsess, getpid());
1453*c54f35caSApple OSS Distributions 
1454*c54f35caSApple OSS Distributions 	__block bool saw_newthread = false;
1455*c54f35caSApple OSS Distributions 	ktrace_events_single(ktsess, TRACE_STRING_NEWTHREAD,
1456*c54f35caSApple OSS Distributions 	    ^(struct trace_point *tp) {
1457*c54f35caSApple OSS Distributions 		if (ktrace_get_pid_for_thread(ktsess, tp->threadid) ==
1458*c54f35caSApple OSS Distributions 		    getpid()) {
1459*c54f35caSApple OSS Distributions 			saw_newthread = true;
1460*c54f35caSApple OSS Distributions 
1461*c54f35caSApple OSS Distributions 			char argname[32] = {};
1462*c54f35caSApple OSS Distributions 			strncat(argname, (char *)&tp->arg1, sizeof(tp->arg1));
1463*c54f35caSApple OSS Distributions 			strncat(argname, (char *)&tp->arg2, sizeof(tp->arg2));
1464*c54f35caSApple OSS Distributions 			strncat(argname, (char *)&tp->arg3, sizeof(tp->arg3));
1465*c54f35caSApple OSS Distributions 			strncat(argname, (char *)&tp->arg4, sizeof(tp->arg4));
1466*c54f35caSApple OSS Distributions 
1467*c54f35caSApple OSS Distributions 			T_EXPECT_EQ_STR((char *)argname, longname_ptr,
1468*c54f35caSApple OSS Distributions 			    "process name of new thread should be long");
1469*c54f35caSApple OSS Distributions 
1470*c54f35caSApple OSS Distributions 			ktrace_end(ktsess, 1);
1471*c54f35caSApple OSS Distributions 		}
1472*c54f35caSApple OSS Distributions 	});
1473*c54f35caSApple OSS Distributions 
1474*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(ktsess, ^{
1475*c54f35caSApple OSS Distributions 		ktrace_session_destroy(ktsess);
1476*c54f35caSApple OSS Distributions 		T_EXPECT_TRUE(saw_newthread,
1477*c54f35caSApple OSS Distributions 		    "should have seen the new thread");
1478*c54f35caSApple OSS Distributions 		T_END;
1479*c54f35caSApple OSS Distributions 	});
1480*c54f35caSApple OSS Distributions 
1481*c54f35caSApple OSS Distributions 	int error = ktrace_start(ktsess, dispatch_get_main_queue());
1482*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started tracing");
1483*c54f35caSApple OSS Distributions 
1484*c54f35caSApple OSS Distributions 	pthread_t thread = NULL;
1485*c54f35caSApple OSS Distributions 	error = pthread_create(&thread, NULL, donothing, NULL);
1486*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "create new thread");
1487*c54f35caSApple OSS Distributions 
1488*c54f35caSApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC),
1489*c54f35caSApple OSS Distributions 	    dispatch_get_main_queue(), ^{
1490*c54f35caSApple OSS Distributions 		ktrace_end(ktsess, 0);
1491*c54f35caSApple OSS Distributions 	});
1492*c54f35caSApple OSS Distributions 
1493*c54f35caSApple OSS Distributions 	error = pthread_join(thread, NULL);
1494*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "join to thread");
1495*c54f35caSApple OSS Distributions 
1496*c54f35caSApple OSS Distributions 	dispatch_main();
1497*c54f35caSApple OSS Distributions }
1498*c54f35caSApple OSS Distributions 
1499*c54f35caSApple OSS Distributions T_DECL(continuous_time, "make sure continuous time status can be queried",
1500*c54f35caSApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true))
1501*c54f35caSApple OSS Distributions {
1502*c54f35caSApple OSS Distributions 	bool cont_time = kdebug_using_continuous_time();
1503*c54f35caSApple OSS Distributions 	T_ASSERT_FALSE(cont_time, "should not be using continuous time yet");
1504*c54f35caSApple OSS Distributions }
1505*c54f35caSApple OSS Distributions 
1506*c54f35caSApple OSS Distributions T_DECL(lookup_long_paths, "lookup long path names")
1507*c54f35caSApple OSS Distributions {
1508*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
1509*c54f35caSApple OSS Distributions 
1510*c54f35caSApple OSS Distributions 	int ret = chdir("/tmp");
1511*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "chdir to /tmp");
1512*c54f35caSApple OSS Distributions 	const char *dir = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/";
1513*c54f35caSApple OSS Distributions 	int i = 0;
1514*c54f35caSApple OSS Distributions 	do {
1515*c54f35caSApple OSS Distributions 		ret = mkdir(dir, S_IRUSR | S_IWUSR | S_IXUSR);
1516*c54f35caSApple OSS Distributions 		if (ret >= 0 || errno != EEXIST) {
1517*c54f35caSApple OSS Distributions 			T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mkdir of %d nested directory",
1518*c54f35caSApple OSS Distributions 			    i);
1519*c54f35caSApple OSS Distributions 		}
1520*c54f35caSApple OSS Distributions 		ret = chdir(dir);
1521*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "chdir to %d nested directory", i);
1522*c54f35caSApple OSS Distributions 	} while (i++ < 40);
1523*c54f35caSApple OSS Distributions 
1524*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
1525*c54f35caSApple OSS Distributions 	ktrace_set_collection_interval(s, 250);
1526*c54f35caSApple OSS Distributions 	ktrace_filter_pid(s, getpid());
1527*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "created session");
1528*c54f35caSApple OSS Distributions 	ktrace_events_single(s, VFS_LOOKUP, ^(struct trace_point *tp __unused){});
1529*c54f35caSApple OSS Distributions 	ktrace_set_vnode_paths_enabled(s, KTRACE_FEATURE_ENABLED);
1530*c54f35caSApple OSS Distributions 
1531*c54f35caSApple OSS Distributions 	dispatch_queue_t q = dispatch_queue_create("com.apple.kdebug-test", 0);
1532*c54f35caSApple OSS Distributions 
1533*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
1534*c54f35caSApple OSS Distributions 		dispatch_release(q);
1535*c54f35caSApple OSS Distributions 		T_END;
1536*c54f35caSApple OSS Distributions 	});
1537*c54f35caSApple OSS Distributions 
1538*c54f35caSApple OSS Distributions 	int error = ktrace_start(s, q);
1539*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started tracing");
1540*c54f35caSApple OSS Distributions 
1541*c54f35caSApple OSS Distributions 	int fd = open("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", O_RDWR | O_CREAT);
1542*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "opened file at %d directories deep", i);
1543*c54f35caSApple OSS Distributions 
1544*c54f35caSApple OSS Distributions 	sleep(5);
1545*c54f35caSApple OSS Distributions 
1546*c54f35caSApple OSS Distributions 	T_LOG("ending tracing");
1547*c54f35caSApple OSS Distributions 	ktrace_end(s, 0);
1548*c54f35caSApple OSS Distributions 	dispatch_main();
1549*c54f35caSApple OSS Distributions }
1550*c54f35caSApple OSS Distributions 
1551*c54f35caSApple OSS Distributions #pragma mark - boot tracing
1552*c54f35caSApple OSS Distributions 
1553*c54f35caSApple OSS Distributions static void
expect_kernel_task_tracing(void)1554*c54f35caSApple OSS Distributions expect_kernel_task_tracing(void)
1555*c54f35caSApple OSS Distributions {
1556*c54f35caSApple OSS Distributions 	unsigned int state = 0;
1557*c54f35caSApple OSS Distributions 	size_t state_size = sizeof(state);
1558*c54f35caSApple OSS Distributions 	int ret = sysctlbyname("ktrace.state", &state, &state_size, NULL, 0);
1559*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(ktrace.state)");
1560*c54f35caSApple OSS Distributions 	T_ASSERT_EQ(state, 1, "state is foreground");
1561*c54f35caSApple OSS Distributions 
1562*c54f35caSApple OSS Distributions 	char configured_by[1024] = "";
1563*c54f35caSApple OSS Distributions 	size_t configured_by_size = sizeof(configured_by);
1564*c54f35caSApple OSS Distributions 	ret = sysctlbyname("ktrace.configured_by", &configured_by,
1565*c54f35caSApple OSS Distributions 	    &configured_by_size, NULL, 0);
1566*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "sysctl(ktrace.configured_by)");
1567*c54f35caSApple OSS Distributions 	T_ASSERT_EQ_STR(configured_by, "kernel_task", "configured by kernel_task");
1568*c54f35caSApple OSS Distributions }
1569*c54f35caSApple OSS Distributions 
1570*c54f35caSApple OSS Distributions static const char *expected_subsystems[] = {
1571*c54f35caSApple OSS Distributions 	"tunables", "locks", "kprintf", "pmap_steal", "kmem", "zalloc",
1572*c54f35caSApple OSS Distributions 	/* "percpu", only has a startup phase on Intel */
1573*c54f35caSApple OSS Distributions 	"codesigning", "oslog", "early_boot",
1574*c54f35caSApple OSS Distributions };
1575*c54f35caSApple OSS Distributions #define EXPECTED_SUBSYSTEMS_LEN \
1576*c54f35caSApple OSS Distributions 		(sizeof(expected_subsystems) / sizeof(expected_subsystems[0]))
1577*c54f35caSApple OSS Distributions 
1578*c54f35caSApple OSS Distributions T_DECL(early_boot_tracing, "ensure early boot strings are present",
1579*c54f35caSApple OSS Distributions 	T_META_BOOTARGS_SET("trace=1000000"), XNU_T_META_SOC_SPECIFIC)
1580*c54f35caSApple OSS Distributions {
1581*c54f35caSApple OSS Distributions 	T_ATEND(reset_ktrace);
1582*c54f35caSApple OSS Distributions 
1583*c54f35caSApple OSS Distributions 	expect_kernel_task_tracing();
1584*c54f35caSApple OSS Distributions 
1585*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
1586*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
1587*c54f35caSApple OSS Distributions 	ktrace_set_collection_interval(s, 250);
1588*c54f35caSApple OSS Distributions 	int error = ktrace_set_use_existing(s);
1589*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "use existing trace buffer");
1590*c54f35caSApple OSS Distributions 
1591*c54f35caSApple OSS Distributions #if defined(__x86_64__)
1592*c54f35caSApple OSS Distributions #define FIRST_EVENT_STRING "i386_init"
1593*c54f35caSApple OSS Distributions #else /* defined(__x86_64__) */
1594*c54f35caSApple OSS Distributions #define FIRST_EVENT_STRING "kernel_startup_bootstrap"
1595*c54f35caSApple OSS Distributions #endif /* !defined(__x86_64__) */
1596*c54f35caSApple OSS Distributions 
1597*c54f35caSApple OSS Distributions 	__block bool seen_event = false;
1598*c54f35caSApple OSS Distributions 	__block size_t cur_subsystem = 0;
1599*c54f35caSApple OSS Distributions 	ktrace_events_single(s, TRACE_INFO_STRING, ^(struct trace_point *tp) {
1600*c54f35caSApple OSS Distributions 		char early_str[33] = "";
1601*c54f35caSApple OSS Distributions 		size_t argsize = ktrace_is_kernel_64_bit(s) ? 8 : 4;
1602*c54f35caSApple OSS Distributions 		memcpy(early_str, &tp->arg1, argsize);
1603*c54f35caSApple OSS Distributions 		memcpy(early_str + argsize, &tp->arg2, argsize);
1604*c54f35caSApple OSS Distributions 		memcpy(early_str + argsize * 2, &tp->arg3, argsize);
1605*c54f35caSApple OSS Distributions 		memcpy(early_str + argsize * 3, &tp->arg4, argsize);
1606*c54f35caSApple OSS Distributions 
1607*c54f35caSApple OSS Distributions 		if (!seen_event) {
1608*c54f35caSApple OSS Distributions 			T_LOG("found first string event with args: "
1609*c54f35caSApple OSS Distributions 			    "0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRIx64,
1610*c54f35caSApple OSS Distributions 			    tp->arg1, tp->arg2, tp->arg3, tp->arg4);
1611*c54f35caSApple OSS Distributions 			char expect_str[33] = FIRST_EVENT_STRING;
1612*c54f35caSApple OSS Distributions 			if (!ktrace_is_kernel_64_bit(s)) {
1613*c54f35caSApple OSS Distributions 				// Only the first 16 bytes of the string will be traced.
1614*c54f35caSApple OSS Distributions 				expect_str[16] = '\0';
1615*c54f35caSApple OSS Distributions 			}
1616*c54f35caSApple OSS Distributions 
1617*c54f35caSApple OSS Distributions 			T_EXPECT_EQ_STR(early_str, expect_str,
1618*c54f35caSApple OSS Distributions 			    "first event in boot trace should be the bootstrap message");
1619*c54f35caSApple OSS Distributions 		}
1620*c54f35caSApple OSS Distributions 		seen_event = true;
1621*c54f35caSApple OSS Distributions 
1622*c54f35caSApple OSS Distributions 		if (strcmp(early_str, expected_subsystems[cur_subsystem]) == 0) {
1623*c54f35caSApple OSS Distributions 			T_LOG("found log for subsystem `%s'",
1624*c54f35caSApple OSS Distributions 					expected_subsystems[cur_subsystem]);
1625*c54f35caSApple OSS Distributions 			cur_subsystem++;
1626*c54f35caSApple OSS Distributions 		} else {
1627*c54f35caSApple OSS Distributions 			T_LOG("saw extra log for subsystem `%s'", early_str);
1628*c54f35caSApple OSS Distributions 		}
1629*c54f35caSApple OSS Distributions 
1630*c54f35caSApple OSS Distributions 		if (cur_subsystem == EXPECTED_SUBSYSTEMS_LEN) {
1631*c54f35caSApple OSS Distributions 			T_LOG("ending after seeing all expected logs");
1632*c54f35caSApple OSS Distributions 			ktrace_end(s, 1);
1633*c54f35caSApple OSS Distributions 		}
1634*c54f35caSApple OSS Distributions 	});
1635*c54f35caSApple OSS Distributions 
1636*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
1637*c54f35caSApple OSS Distributions 		T_EXPECT_TRUE(seen_event, "should see an early boot string event");
1638*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(cur_subsystem, EXPECTED_SUBSYSTEMS_LEN,
1639*c54f35caSApple OSS Distributions 				"should see logs from all subsystems");
1640*c54f35caSApple OSS Distributions 		if (cur_subsystem != EXPECTED_SUBSYSTEMS_LEN) {
1641*c54f35caSApple OSS Distributions 			T_LOG("missing log for %s", expected_subsystems[cur_subsystem]);
1642*c54f35caSApple OSS Distributions 		}
1643*c54f35caSApple OSS Distributions 		T_END;
1644*c54f35caSApple OSS Distributions 	});
1645*c54f35caSApple OSS Distributions 
1646*c54f35caSApple OSS Distributions 	error = ktrace_start(s, dispatch_get_main_queue());
1647*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "started tracing");
1648*c54f35caSApple OSS Distributions 
1649*c54f35caSApple OSS Distributions 	T_SETUPEND;
1650*c54f35caSApple OSS Distributions 
1651*c54f35caSApple OSS Distributions 	dispatch_main();
1652*c54f35caSApple OSS Distributions }
1653*c54f35caSApple OSS Distributions 
1654*c54f35caSApple OSS Distributions // Allocating ~4TB should be clamped to some lower number.
1655*c54f35caSApple OSS Distributions T_DECL(early_boot_tracing_too_large,
1656*c54f35caSApple OSS Distributions     "ensure early boot tracing can allocate up to a clamped size",
1657*c54f35caSApple OSS Distributions 	T_META_BOOTARGS_SET("trace=64000000000"), XNU_T_META_SOC_SPECIFIC)
1658*c54f35caSApple OSS Distributions {
1659*c54f35caSApple OSS Distributions 	expect_kernel_task_tracing();
1660*c54f35caSApple OSS Distributions 	T_EXPECT_NE(get_nevents(), 0, "allocated some events");
1661*c54f35caSApple OSS Distributions }
1662*c54f35caSApple OSS Distributions 
1663*c54f35caSApple OSS Distributions // Not SoC-specific because the typefilter parsing logic is generic.
1664*c54f35caSApple OSS Distributions T_DECL(typefilter_boot_arg, "ensure typefilter is set up correctly at boot",
1665*c54f35caSApple OSS Distributions 	T_META_BOOTARGS_SET("trace=100000 trace_typefilter=S0x0c00,C0xfe"))
1666*c54f35caSApple OSS Distributions {
1667*c54f35caSApple OSS Distributions 	T_ATEND(reset_ktrace);
1668*c54f35caSApple OSS Distributions 
1669*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
1670*c54f35caSApple OSS Distributions 	ktrace_config_t config = ktrace_config_create_current();
1671*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO;
1672*c54f35caSApple OSS Distributions 	T_ASSERT_NOTNULL(config, "create config from current system");
1673*c54f35caSApple OSS Distributions 	T_SETUPEND;
1674*c54f35caSApple OSS Distributions 
1675*c54f35caSApple OSS Distributions 	T_LOG("ktrace configuration:");
1676*c54f35caSApple OSS Distributions 	ktrace_config_print_description(config, stdout);
1677*c54f35caSApple OSS Distributions 
1678*c54f35caSApple OSS Distributions 	uint8_t *typefilt = ktrace_config_kdebug_get_typefilter(config);
1679*c54f35caSApple OSS Distributions 	T_ASSERT_NOTNULL(typefilt, "typefilter is active");
1680*c54f35caSApple OSS Distributions 	T_EXPECT_TRUE(typefilt[0x0c00 / 8],
1681*c54f35caSApple OSS Distributions 			"specified subclass is set in typefilter");
1682*c54f35caSApple OSS Distributions 	T_MAYFAIL; // rdar://63625062 (UTD converts commas in boot-args to spaces)
1683*c54f35caSApple OSS Distributions 	T_EXPECT_TRUE(typefilt[0xfeed / 8],
1684*c54f35caSApple OSS Distributions 			"specified class is set in typefilter");
1685*c54f35caSApple OSS Distributions 
1686*c54f35caSApple OSS Distributions 	ktrace_config_destroy(config);
1687*c54f35caSApple OSS Distributions }
1688*c54f35caSApple OSS Distributions 
1689*c54f35caSApple OSS Distributions #pragma mark - events present
1690*c54f35caSApple OSS Distributions 
1691*c54f35caSApple OSS Distributions static int recvd_sigchild = 0;
1692*c54f35caSApple OSS Distributions static void
sighandler(int sig)1693*c54f35caSApple OSS Distributions sighandler(int sig)
1694*c54f35caSApple OSS Distributions {
1695*c54f35caSApple OSS Distributions 	if (sig != SIGCHLD) {
1696*c54f35caSApple OSS Distributions 		T_ASSERT_FAIL("unexpected signal: %d", sig);
1697*c54f35caSApple OSS Distributions 	}
1698*c54f35caSApple OSS Distributions 	recvd_sigchild = 1;
1699*c54f35caSApple OSS Distributions }
1700*c54f35caSApple OSS Distributions 
1701*c54f35caSApple OSS Distributions #define END_EVENT (0xfeedfac0)
1702*c54f35caSApple OSS Distributions 
1703*c54f35caSApple OSS Distributions T_DECL(instrs_and_cycles_on_proc_exit,
1704*c54f35caSApple OSS Distributions 		"instructions and cycles should be traced on thread exit",
1705*c54f35caSApple OSS Distributions 		T_META_REQUIRES_SYSCTL_EQ("kern.monotonic.supported", 1))
1706*c54f35caSApple OSS Distributions {
1707*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
1708*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
1709*c54f35caSApple OSS Distributions 	int error;
1710*c54f35caSApple OSS Distributions 	struct rusage_info_v4 *rusage = calloc(1, sizeof(*rusage));
1711*c54f35caSApple OSS Distributions 	char *args[] = { "ls", "-l", NULL, };
1712*c54f35caSApple OSS Distributions 	int status;
1713*c54f35caSApple OSS Distributions 	dispatch_queue_t q = dispatch_queue_create("com.apple.kdebug-test",
1714*c54f35caSApple OSS Distributions 			DISPATCH_QUEUE_SERIAL);
1715*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(signal(SIGCHLD, sighandler),
1716*c54f35caSApple OSS Distributions 			"register signal handler");
1717*c54f35caSApple OSS Distributions 
1718*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
1719*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "ktrace_session_create");
1720*c54f35caSApple OSS Distributions 	ktrace_set_collection_interval(s, 100);
1721*c54f35caSApple OSS Distributions 
1722*c54f35caSApple OSS Distributions 	__block pid_t pid;
1723*c54f35caSApple OSS Distributions 	__block bool seen_event = false;
1724*c54f35caSApple OSS Distributions 	__block uint64_t proc_instrs = 0;
1725*c54f35caSApple OSS Distributions 	__block uint64_t proc_cycles = 0;
1726*c54f35caSApple OSS Distributions 	__block uint64_t proc_sys_time = 0;
1727*c54f35caSApple OSS Distributions 	__block uint64_t proc_usr_time = 0;
1728*c54f35caSApple OSS Distributions 	error = ktrace_events_single(s, DBG_MT_INSTRS_CYCLES_PROC_EXIT,
1729*c54f35caSApple OSS Distributions 			^(ktrace_event_t tp){
1730*c54f35caSApple OSS Distributions 		if (tp->pid == pid) {
1731*c54f35caSApple OSS Distributions 			seen_event = true;
1732*c54f35caSApple OSS Distributions 			proc_instrs = tp->arg1;
1733*c54f35caSApple OSS Distributions 			proc_cycles = tp->arg2;
1734*c54f35caSApple OSS Distributions 			proc_sys_time = tp->arg3;
1735*c54f35caSApple OSS Distributions 			proc_usr_time = tp->arg4;
1736*c54f35caSApple OSS Distributions 			ktrace_end(s, 1);
1737*c54f35caSApple OSS Distributions 		}
1738*c54f35caSApple OSS Distributions 	});
1739*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_POSIX_ZERO(error, "trace single event");
1740*c54f35caSApple OSS Distributions 	error = ktrace_events_single(s, END_EVENT, ^(ktrace_event_t __unused tp){
1741*c54f35caSApple OSS Distributions 		T_LOG("saw ending event, stopping trace session");
1742*c54f35caSApple OSS Distributions 		ktrace_end(s, 0);
1743*c54f35caSApple OSS Distributions 	});
1744*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_POSIX_ZERO(error, "trace single event");
1745*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
1746*c54f35caSApple OSS Distributions 		// TODO Check for equality once rdar://61948669 is fixed.
1747*c54f35caSApple OSS Distributions 		T_ASSERT_GE(proc_instrs, rusage->ri_instructions,
1748*c54f35caSApple OSS Distributions 				"trace event instrs are >= to rusage instrs");
1749*c54f35caSApple OSS Distributions 		T_ASSERT_GE(proc_cycles, rusage->ri_cycles,
1750*c54f35caSApple OSS Distributions 				"trace event cycles are >= to rusage cycles");
1751*c54f35caSApple OSS Distributions 		T_ASSERT_GE(proc_sys_time, rusage->ri_system_time,
1752*c54f35caSApple OSS Distributions 				"trace event sys time is >= rusage sys time");
1753*c54f35caSApple OSS Distributions 		T_ASSERT_GE(proc_usr_time, rusage->ri_user_time,
1754*c54f35caSApple OSS Distributions 				"trace event usr time >= rusage usr time");
1755*c54f35caSApple OSS Distributions 		T_EXPECT_TRUE(seen_event, "should see the proc exit trace event");
1756*c54f35caSApple OSS Distributions 
1757*c54f35caSApple OSS Distributions 		free(rusage);
1758*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
1759*c54f35caSApple OSS Distributions 		dispatch_release(q);
1760*c54f35caSApple OSS Distributions 		T_END;
1761*c54f35caSApple OSS Distributions 	});
1762*c54f35caSApple OSS Distributions 	error = ktrace_start(s, q);
1763*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "start tracing");
1764*c54f35caSApple OSS Distributions 	T_SETUPEND;
1765*c54f35caSApple OSS Distributions 
1766*c54f35caSApple OSS Distributions 	extern char **environ;
1767*c54f35caSApple OSS Distributions 	status = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);
1768*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(status, "spawn process");
1769*c54f35caSApple OSS Distributions 	if (status == 0) {
1770*c54f35caSApple OSS Distributions 		while (!recvd_sigchild) {
1771*c54f35caSApple OSS Distributions 			pause();
1772*c54f35caSApple OSS Distributions 		}
1773*c54f35caSApple OSS Distributions 		error = proc_pid_rusage(pid, RUSAGE_INFO_V4, (rusage_info_t)rusage);
1774*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(error, "rusage");
1775*c54f35caSApple OSS Distributions 		error = waitpid(pid, &status, 0);
1776*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_SUCCESS(error, "waitpid");
1777*c54f35caSApple OSS Distributions 		kdebug_trace(END_EVENT, 0, 0, 0, 0);
1778*c54f35caSApple OSS Distributions 	}
1779*c54f35caSApple OSS Distributions 	dispatch_main();
1780*c54f35caSApple OSS Distributions }
1781*c54f35caSApple OSS Distributions 
1782*c54f35caSApple OSS Distributions #define NO_OF_THREADS 2
1783*c54f35caSApple OSS Distributions 
1784*c54f35caSApple OSS Distributions struct thread_counters_info {
1785*c54f35caSApple OSS Distributions 	struct thsc_cpi counts;
1786*c54f35caSApple OSS Distributions 	uint64_t cpu_time;
1787*c54f35caSApple OSS Distributions 	uint64_t thread_id;
1788*c54f35caSApple OSS Distributions };
1789*c54f35caSApple OSS Distributions typedef struct thread_counters_info *tc_info_t;
1790*c54f35caSApple OSS Distributions 
1791*c54f35caSApple OSS Distributions static void*
get_thread_counters(void * ptr)1792*c54f35caSApple OSS Distributions get_thread_counters(void* ptr)
1793*c54f35caSApple OSS Distributions {
1794*c54f35caSApple OSS Distributions 	extern uint64_t __thread_selfusage(void);
1795*c54f35caSApple OSS Distributions 	extern uint64_t __thread_selfid(void);
1796*c54f35caSApple OSS Distributions 	tc_info_t tc_info = (tc_info_t) ptr;
1797*c54f35caSApple OSS Distributions 	tc_info->thread_id = __thread_selfid();
1798*c54f35caSApple OSS Distributions 	// Just to increase the instr, cycle count
1799*c54f35caSApple OSS Distributions 	T_LOG("printing %llu\n", tc_info->thread_id);
1800*c54f35caSApple OSS Distributions 	tc_info->cpu_time = __thread_selfusage();
1801*c54f35caSApple OSS Distributions 	(void)thread_selfcounts(THSC_CPI, &tc_info->counts, sizeof(tc_info->counts));
1802*c54f35caSApple OSS Distributions 	return NULL;
1803*c54f35caSApple OSS Distributions }
1804*c54f35caSApple OSS Distributions 
1805*c54f35caSApple OSS Distributions T_DECL(instrs_and_cycles_on_thread_exit,
1806*c54f35caSApple OSS Distributions 		"instructions and cycles should be traced on thread exit",
1807*c54f35caSApple OSS Distributions 		T_META_REQUIRES_SYSCTL_EQ("kern.monotonic.supported", 1))
1808*c54f35caSApple OSS Distributions {
1809*c54f35caSApple OSS Distributions 	T_SETUPBEGIN;
1810*c54f35caSApple OSS Distributions 	start_controlling_ktrace();
1811*c54f35caSApple OSS Distributions 
1812*c54f35caSApple OSS Distributions 	int error;
1813*c54f35caSApple OSS Distributions 	pthread_t *threads = calloc((unsigned int)(NO_OF_THREADS),
1814*c54f35caSApple OSS Distributions 			sizeof(pthread_t));
1815*c54f35caSApple OSS Distributions 	 T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(threads, "calloc(%d threads)",
1816*c54f35caSApple OSS Distributions 	    NO_OF_THREADS);
1817*c54f35caSApple OSS Distributions 	tc_info_t tc_infos = calloc((unsigned int) (NO_OF_THREADS),
1818*c54f35caSApple OSS Distributions 			sizeof(struct thread_counters_info));
1819*c54f35caSApple OSS Distributions 	T_WITH_ERRNO; T_QUIET; T_ASSERT_NOTNULL(tc_infos,
1820*c54f35caSApple OSS Distributions 			"calloc(%d thread counters)", NO_OF_THREADS);
1821*c54f35caSApple OSS Distributions 
1822*c54f35caSApple OSS Distributions 	ktrace_session_t s = ktrace_session_create();
1823*c54f35caSApple OSS Distributions 	T_QUIET; T_WITH_ERRNO; T_ASSERT_NOTNULL(s, "ktrace_session_create");
1824*c54f35caSApple OSS Distributions 	ktrace_filter_pid(s, getpid());
1825*c54f35caSApple OSS Distributions 
1826*c54f35caSApple OSS Distributions 	__block int nevents = 0;
1827*c54f35caSApple OSS Distributions 	error = ktrace_events_single(s, DBG_MT_INSTRS_CYCLES_THR_EXIT,
1828*c54f35caSApple OSS Distributions 			^(ktrace_event_t tp) {
1829*c54f35caSApple OSS Distributions 		for (int i = 0; i < NO_OF_THREADS; i++) {
1830*c54f35caSApple OSS Distributions 			if (tp->threadid == tc_infos[i].thread_id) {
1831*c54f35caSApple OSS Distributions 				nevents++;
1832*c54f35caSApple OSS Distributions 				uint64_t cpu_time = tp->arg3 + tp->arg4;
1833*c54f35caSApple OSS Distributions 				/*
1834*c54f35caSApple OSS Distributions 				 * as we are getting counts before thread exit,
1835*c54f35caSApple OSS Distributions 				 * the counts at thread exit should be greater than
1836*c54f35caSApple OSS Distributions 				 * thread_selfcounts
1837*c54f35caSApple OSS Distributions 				 */
1838*c54f35caSApple OSS Distributions 				T_ASSERT_GE(tp->arg1, tc_infos[i].counts.tcpi_instructions,
1839*c54f35caSApple OSS Distributions 					"trace event instrs are >= to thread's instrs");
1840*c54f35caSApple OSS Distributions 				T_ASSERT_GE(tp->arg2, tc_infos[i].counts.tcpi_cycles,
1841*c54f35caSApple OSS Distributions 					"trace event cycles are >= to thread's cycles");
1842*c54f35caSApple OSS Distributions 				T_ASSERT_GE(cpu_time, tc_infos[i].cpu_time,
1843*c54f35caSApple OSS Distributions 					"trace event cpu time is >= thread's cpu time");
1844*c54f35caSApple OSS Distributions 			}
1845*c54f35caSApple OSS Distributions 			if (nevents == NO_OF_THREADS) {
1846*c54f35caSApple OSS Distributions 				ktrace_end(s, 1);
1847*c54f35caSApple OSS Distributions 			}
1848*c54f35caSApple OSS Distributions 		}
1849*c54f35caSApple OSS Distributions 	});
1850*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_ZERO(error, "trace single event");
1851*c54f35caSApple OSS Distributions 	ktrace_set_completion_handler(s, ^{
1852*c54f35caSApple OSS Distributions 		T_EXPECT_EQ(NO_OF_THREADS, nevents, "seen %d thread exit trace events",
1853*c54f35caSApple OSS Distributions 				NO_OF_THREADS);
1854*c54f35caSApple OSS Distributions 		free(tc_infos);
1855*c54f35caSApple OSS Distributions 		ktrace_session_destroy(s);
1856*c54f35caSApple OSS Distributions 		T_END;
1857*c54f35caSApple OSS Distributions 	});
1858*c54f35caSApple OSS Distributions 	error = ktrace_start(s, dispatch_get_main_queue());
1859*c54f35caSApple OSS Distributions 	T_ASSERT_POSIX_ZERO(error, "start tracing");
1860*c54f35caSApple OSS Distributions 
1861*c54f35caSApple OSS Distributions 	for (int i = 0; i < NO_OF_THREADS; i++) {
1862*c54f35caSApple OSS Distributions 		error = pthread_create(&threads[i], NULL, get_thread_counters,
1863*c54f35caSApple OSS Distributions 				(void *)&tc_infos[i]);
1864*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_POSIX_ZERO(error, "pthread_create thread %d", i);
1865*c54f35caSApple OSS Distributions 	}
1866*c54f35caSApple OSS Distributions 	T_SETUPEND;
1867*c54f35caSApple OSS Distributions 	for (int i = 0; i < NO_OF_THREADS; i++) {
1868*c54f35caSApple OSS Distributions 		error = pthread_join(threads[i], NULL);
1869*c54f35caSApple OSS Distributions 		T_QUIET; T_EXPECT_POSIX_ZERO(error, "pthread_join thread %d", i);
1870*c54f35caSApple OSS Distributions 	}
1871*c54f35caSApple OSS Distributions 
1872*c54f35caSApple OSS Distributions 	dispatch_main();
1873*c54f35caSApple OSS Distributions }
1874