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