]>
git.saurik.com Git - apple/xnu.git/blob - tests/ktrace_helpers.h
1 #ifndef KTRACE_HELPERS_H
2 #define KTRACE_HELPERS_H
4 #include <darwintest.h>
5 #include <ktrace/ktrace.h>
7 #include <sys/sysctl.h>
8 #include <sys/kdebug.h>
13 (void)sysctl((int[]){ CTL_KERN
, KERN_KDEBUG
, KERN_KDREMOVE
}, 3,
19 start_controlling_ktrace(void)
24 size_t statesz
= sizeof(state
);
25 int ret
= sysctlbyname("ktrace.state", &state
, &statesz
, NULL
, 0);
26 T_QUIET
; T_ASSERT_POSIX_SUCCESS(ret
, "getting ktrace state");
30 size_t pidsz
= sizeof(ownerpid
);
31 ret
= sysctlbyname("ktrace.owning_pid", &ownerpid
, &pidsz
, NULL
, 0);
32 T_QUIET
; T_ASSERT_POSIX_SUCCESS(ret
, "getting owning pid");
35 T_LOG("ktrace is in foreground, but no owner");
40 ret
= proc_name(ownerpid
, ownername
, sizeof(ownername
));
42 T_LOG("ktrace is in foreground, but owner (%d) has no name", ownerpid
);
46 T_LOG("ktrace is in foreground, owned by %s, sending SIGKILL", ownername
);
47 kill(ownerpid
, SIGKILL
);
50 ret
= proc_name(ownerpid
, ownername
, sizeof(ownername
));
51 T_QUIET
; T_ASSERT_EQ(ret
, 0, "should have killed ktrace owner");
56 T_ATEND(reset_ktrace
);
60 static inline uint64_t
61 ns_from_abs(ktrace_session_t s
, uint64_t abstime
)
64 int error
= ktrace_convert_timestamp_to_nanoseconds(s
, abstime
, &ns
);
65 T_QUIET
; T_ASSERT_POSIX_ZERO(error
, "convert abstime to nanoseconds");
69 static inline uint64_t
70 relns_from_abs(ktrace_session_t s
, uint64_t abstime
)
72 return ns_from_abs(s
, abstime
- ktrace_get_earliest_timestamp(s
));
75 #endif /* !defined(KTRACE_HELPERS_H) */