]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/darwintests/thread_group_set_32261625.c
xnu-4570.41.2.tar.gz
[apple/xnu.git] / tools / tests / darwintests / thread_group_set_32261625.c
CommitLineData
5ba3f43e
A
1#include <darwintest.h>
2#include <ktrace.h>
3#include <sys/kdebug.h>
4
5#define TEST_EVENTID (0xfedcbb00)
6
7static void*
8newthread(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
19T_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_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
31 ktrace_session_t session = ktrace_session_create();
32 T_ASSERT_NOTNULL(session, "ktrace_session_create");
33
34 ktrace_set_interactive(session);
35
36 ktrace_set_completion_handler(session, ^{
37 T_ASSERT_TRUE(seen_new_thread, "seen new thread tracepoint");
38 T_END;
39 });
40
41 T_EXPECT_POSIX_SUCCESS(pthread_create(&thread, NULL, newthread, NULL), "pthread_create");
42 T_EXPECT_POSIX_SUCCESS(pthread_detach(thread), "pthread_detach");
43
44 ktrace_events_single(session, TEST_EVENTID, ^(__unused ktrace_event_t e) {
45 T_EXPECT_TRUE(seen_thread_group_set, "seen THREAD_GROUP_SET tracepoint");
46 seen_new_thread = 1;
47 ktrace_end(session, 1);
48 });
49
50 ktrace_events_single(session, MACHDBG_CODE(DBG_MACH_THREAD_GROUP, MACH_THREAD_GROUP_SET), ^(ktrace_event_t e) {
51 T_EXPECT_GT(e->arg3, 0, "tid on THREAD_GROUP_SET");
52 seen_thread_group_set = 1;
53 });
54
55 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, TEST_TIMEOUT), dispatch_get_main_queue(), ^{
56 ktrace_end(session, 0);
57 });
58
59 T_ASSERT_POSIX_SUCCESS(ktrace_start(session, dispatch_get_main_queue()), "ktrace_start");
60
61 dispatch_main();
62}