1 #include <darwintest.h>
3 #include <sys/kdebug.h>
5 #define TEST_EVENTID (0xfedcbb00)
12 kdebug_trace(TEST_EVENTID
, 0, 0, 0, 0);
17 #define TEST_TIMEOUT (15 * NSEC_PER_SEC)
19 T_DECL(thread_group_set
, "Checks that new threads get a THREAD_GROUP_SET tracepoint with a non-zero tid") {
21 __block
int seen_new_thread
= 0, __block seen_thread_group_set
= 0;
23 ktrace_machine_t machine
= ktrace_machine_create_current();
24 T_WITH_ERRNO
; T_ASSERT_NOTNULL(machine
, "ktrace_get_machine");
27 if (ktrace_machine_has_thread_groups(machine
, &has_tg
) || !has_tg
) {
28 T_SKIP("thread groups not supported on this system");
30 ktrace_machine_destroy(machine
);
32 ktrace_session_t session
= ktrace_session_create();
33 T_WITH_ERRNO
; T_ASSERT_NOTNULL(session
, "ktrace_session_create");
35 ktrace_set_interactive(session
);
37 ktrace_set_completion_handler(session
, ^{
38 ktrace_session_destroy(session
);
39 T_ASSERT_TRUE(seen_new_thread
, "seen new thread tracepoint");
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");
46 ktrace_end(session
, 1);
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;
54 dispatch_after(dispatch_time(DISPATCH_TIME_NOW
, TEST_TIMEOUT
), dispatch_get_main_queue(), ^{
55 ktrace_end(session
, 0);
58 T_ASSERT_POSIX_SUCCESS(ktrace_start(session
, dispatch_get_main_queue()), "ktrace_start");
60 T_EXPECT_POSIX_SUCCESS(pthread_create(&thread
, NULL
, newthread
, NULL
), "pthread_create");
61 T_EXPECT_POSIX_SUCCESS(pthread_detach(thread
), "pthread_detach");