]> git.saurik.com Git - apple/xnu.git/blame - tests/thread_group_set_32261625.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / tests / thread_group_set_32261625.c
CommitLineData
5ba3f43e
A
1#include <darwintest.h>
2#include <ktrace.h>
3#include <sys/kdebug.h>
4
cb323159
A
5T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
6
5ba3f43e
A
7#define TEST_EVENTID (0xfedcbb00)
8
9static void*
10newthread(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
f427ee49
A
21T_DECL(thread_group_set, "Checks that new threads get a THREAD_GROUP_SET tracepoint with a non-zero tid",
22 T_META_ASROOT(true)) {
5ba3f43e
A
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();
a39ff7e2 27 T_WITH_ERRNO; T_ASSERT_NOTNULL(machine, "ktrace_get_machine");
5ba3f43e
A
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 }
a39ff7e2 33 ktrace_machine_destroy(machine);
5ba3f43e
A
34
35 ktrace_session_t session = ktrace_session_create();
a39ff7e2 36 T_WITH_ERRNO; T_ASSERT_NOTNULL(session, "ktrace_session_create");
5ba3f43e
A
37
38 ktrace_set_interactive(session);
39
40 ktrace_set_completion_handler(session, ^{
a39ff7e2 41 ktrace_session_destroy(session);
5ba3f43e
A
42 T_ASSERT_TRUE(seen_new_thread, "seen new thread tracepoint");
43 T_END;
44 });
45
5ba3f43e
A
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) {
a39ff7e2 53 T_EXPECT_GT(e->arg3, (uintptr_t)0, "tid on THREAD_GROUP_SET");
5ba3f43e
A
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
a39ff7e2
A
63 T_EXPECT_POSIX_SUCCESS(pthread_create(&thread, NULL, newthread, NULL), "pthread_create");
64 T_EXPECT_POSIX_SUCCESS(pthread_detach(thread), "pthread_detach");
65
5ba3f43e
A
66 dispatch_main();
67}