]>
Commit | Line | Data |
---|---|---|
cb323159 A |
1 | #ifndef KTRACE_HELPERS_H |
2 | #define KTRACE_HELPERS_H | |
3 | ||
4 | #include <darwintest.h> | |
f427ee49 | 5 | #include <ktrace/ktrace.h> |
cb323159 A |
6 | #include <libproc.h> |
7 | #include <sys/sysctl.h> | |
f427ee49 | 8 | #include <sys/kdebug.h> |
cb323159 A |
9 | |
10 | static inline void | |
11 | reset_ktrace(void) | |
12 | { | |
13 | (void)sysctl((int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDREMOVE }, 3, | |
14 | NULL, 0, NULL, 0); | |
15 | kperf_reset(); | |
16 | } | |
17 | ||
18 | static inline void | |
19 | start_controlling_ktrace(void) | |
20 | { | |
21 | T_SETUPBEGIN; | |
22 | ||
23 | int state = 0; | |
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"); | |
27 | ||
28 | if (state == 1) { | |
29 | int ownerpid = 0; | |
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"); | |
33 | ||
34 | if (ownerpid <= 0) { | |
35 | T_LOG("ktrace is in foreground, but no owner"); | |
36 | goto out; | |
37 | } | |
38 | ||
39 | char ownername[1024]; | |
40 | ret = proc_name(ownerpid, ownername, sizeof(ownername)); | |
41 | if (ret == 0) { | |
42 | T_LOG("ktrace is in foreground, but owner (%d) has no name", ownerpid); | |
43 | goto out; | |
44 | } | |
45 | ||
46 | T_LOG("ktrace is in foreground, owned by %s, sending SIGKILL", ownername); | |
47 | kill(ownerpid, SIGKILL); | |
48 | usleep(500000); | |
49 | ||
50 | ret = proc_name(ownerpid, ownername, sizeof(ownername)); | |
51 | T_QUIET; T_ASSERT_EQ(ret, 0, "should have killed ktrace owner"); | |
52 | } | |
53 | ||
54 | out: | |
55 | reset_ktrace(); | |
56 | T_ATEND(reset_ktrace); | |
57 | T_SETUPEND; | |
58 | } | |
59 | ||
f427ee49 A |
60 | static inline uint64_t |
61 | ns_from_abs(ktrace_session_t s, uint64_t abstime) | |
62 | { | |
63 | uint64_t ns = 0; | |
64 | int error = ktrace_convert_timestamp_to_nanoseconds(s, abstime, &ns); | |
65 | T_QUIET; T_ASSERT_POSIX_ZERO(error, "convert abstime to nanoseconds"); | |
66 | return ns; | |
67 | } | |
68 | ||
69 | static inline uint64_t | |
70 | relns_from_abs(ktrace_session_t s, uint64_t abstime) | |
71 | { | |
72 | return ns_from_abs(s, abstime - ktrace_get_earliest_timestamp(s)); | |
73 | } | |
74 | ||
cb323159 | 75 | #endif /* !defined(KTRACE_HELPERS_H) */ |