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