]> git.saurik.com Git - apple/xnu.git/blob - tests/thread_group_set_32261625.c
xnu-6153.11.26.tar.gz
[apple/xnu.git] / tests / thread_group_set_32261625.c
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*
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 pthread_t thread;
23 __block int seen_new_thread = 0, __block seen_thread_group_set = 0;
24
25 ktrace_machine_t machine = ktrace_machine_create_current();
26 T_WITH_ERRNO; T_ASSERT_NOTNULL(machine, "ktrace_get_machine");
27
28 bool has_tg = false;
29 if (ktrace_machine_has_thread_groups(machine, &has_tg) || !has_tg) {
30 T_SKIP("thread groups not supported on this system");
31 }
32 ktrace_machine_destroy(machine);
33
34 ktrace_session_t session = ktrace_session_create();
35 T_WITH_ERRNO; T_ASSERT_NOTNULL(session, "ktrace_session_create");
36
37 ktrace_set_interactive(session);
38
39 ktrace_set_completion_handler(session, ^{
40 ktrace_session_destroy(session);
41 T_ASSERT_TRUE(seen_new_thread, "seen new thread tracepoint");
42 T_END;
43 });
44
45 ktrace_events_single(session, TEST_EVENTID, ^(__unused ktrace_event_t e) {
46 T_EXPECT_TRUE(seen_thread_group_set, "seen THREAD_GROUP_SET tracepoint");
47 seen_new_thread = 1;
48 ktrace_end(session, 1);
49 });
50
51 ktrace_events_single(session, MACHDBG_CODE(DBG_MACH_THREAD_GROUP, MACH_THREAD_GROUP_SET), ^(ktrace_event_t e) {
52 T_EXPECT_GT(e->arg3, (uintptr_t)0, "tid on THREAD_GROUP_SET");
53 seen_thread_group_set = 1;
54 });
55
56 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT), dispatch_get_main_queue(), ^{
57 ktrace_end(session, 0);
58 });
59
60 T_ASSERT_POSIX_SUCCESS(ktrace_start(session, dispatch_get_main_queue()), "ktrace_start");
61
62 T_EXPECT_POSIX_SUCCESS(pthread_create(&thread, NULL, newthread, NULL), "pthread_create");
63 T_EXPECT_POSIX_SUCCESS(pthread_detach(thread), "pthread_detach");
64
65 dispatch_main();
66 }