xref: /xnu-8796.141.3/tests/thread_group_set_32261625.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions #include <ktrace.h>
3*1b191cb5SApple OSS Distributions #include <sys/kdebug.h>
4*1b191cb5SApple OSS Distributions 
5*1b191cb5SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions #define TEST_EVENTID (0xfedcbb00)
8*1b191cb5SApple OSS Distributions 
9*1b191cb5SApple OSS Distributions static void*
newthread(void * arg)10*1b191cb5SApple OSS Distributions newthread(void *arg)
11*1b191cb5SApple OSS Distributions {
12*1b191cb5SApple OSS Distributions #pragma unused(arg)
13*1b191cb5SApple OSS Distributions 	while (1) {
14*1b191cb5SApple OSS Distributions 		kdebug_trace(TEST_EVENTID, 0, 0, 0, 0);
15*1b191cb5SApple OSS Distributions 		sleep(1);
16*1b191cb5SApple OSS Distributions 	}
17*1b191cb5SApple OSS Distributions }
18*1b191cb5SApple OSS Distributions 
19*1b191cb5SApple OSS Distributions #define TEST_TIMEOUT (15 * NSEC_PER_SEC)
20*1b191cb5SApple OSS Distributions 
21*1b191cb5SApple OSS Distributions T_DECL(thread_group_set, "Checks that new threads get a THREAD_GROUP_SET tracepoint with a non-zero tid",
22*1b191cb5SApple OSS Distributions     T_META_ASROOT(true)) {
23*1b191cb5SApple OSS Distributions 	pthread_t thread;
24*1b191cb5SApple OSS Distributions 	__block int seen_new_thread = 0, __block seen_thread_group_set = 0;
25*1b191cb5SApple OSS Distributions 
26*1b191cb5SApple OSS Distributions 	ktrace_machine_t machine = ktrace_machine_create_current();
27*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_NOTNULL(machine, "ktrace_get_machine");
28*1b191cb5SApple OSS Distributions 
29*1b191cb5SApple OSS Distributions 	bool has_tg = false;
30*1b191cb5SApple OSS Distributions 	if (ktrace_machine_has_thread_groups(machine, &has_tg) || !has_tg) {
31*1b191cb5SApple OSS Distributions 		T_SKIP("thread groups not supported on this system");
32*1b191cb5SApple OSS Distributions 	}
33*1b191cb5SApple OSS Distributions 	ktrace_machine_destroy(machine);
34*1b191cb5SApple OSS Distributions 
35*1b191cb5SApple OSS Distributions 	ktrace_session_t session = ktrace_session_create();
36*1b191cb5SApple OSS Distributions 	T_WITH_ERRNO; T_ASSERT_NOTNULL(session, "ktrace_session_create");
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions 	ktrace_set_interactive(session);
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions 	ktrace_set_completion_handler(session, ^{
41*1b191cb5SApple OSS Distributions 		ktrace_session_destroy(session);
42*1b191cb5SApple OSS Distributions 		T_ASSERT_TRUE(seen_new_thread, "seen new thread tracepoint");
43*1b191cb5SApple OSS Distributions 		T_END;
44*1b191cb5SApple OSS Distributions 	});
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions 	ktrace_events_single(session, TEST_EVENTID, ^(__unused ktrace_event_t e) {
47*1b191cb5SApple OSS Distributions 		T_EXPECT_TRUE(seen_thread_group_set, "seen THREAD_GROUP_SET tracepoint");
48*1b191cb5SApple OSS Distributions 		seen_new_thread = 1;
49*1b191cb5SApple OSS Distributions 		ktrace_end(session, 1);
50*1b191cb5SApple OSS Distributions 	});
51*1b191cb5SApple OSS Distributions 
52*1b191cb5SApple OSS Distributions 	ktrace_events_single(session, MACHDBG_CODE(DBG_MACH_THREAD_GROUP, MACH_THREAD_GROUP_SET), ^(ktrace_event_t e) {
53*1b191cb5SApple OSS Distributions 		T_EXPECT_GT(e->arg3, (uintptr_t)0, "tid on THREAD_GROUP_SET");
54*1b191cb5SApple OSS Distributions 		seen_thread_group_set = 1;
55*1b191cb5SApple OSS Distributions 	});
56*1b191cb5SApple OSS Distributions 
57*1b191cb5SApple OSS Distributions 	dispatch_after(dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT), dispatch_get_main_queue(), ^{
58*1b191cb5SApple OSS Distributions 		ktrace_end(session, 0);
59*1b191cb5SApple OSS Distributions 	});
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(ktrace_start(session, dispatch_get_main_queue()), "ktrace_start");
62*1b191cb5SApple OSS Distributions 
63*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(pthread_create(&thread, NULL, newthread, NULL), "pthread_create");
64*1b191cb5SApple OSS Distributions 	T_EXPECT_POSIX_SUCCESS(pthread_detach(thread), "pthread_detach");
65*1b191cb5SApple OSS Distributions 
66*1b191cb5SApple OSS Distributions 	dispatch_main();
67*1b191cb5SApple OSS Distributions }
68