]> git.saurik.com Git - apple/xnu.git/blame - tests/ktrace_helpers.h
xnu-6153.41.3.tar.gz
[apple/xnu.git] / tests / ktrace_helpers.h
CommitLineData
cb323159
A
1#ifndef KTRACE_HELPERS_H
2#define KTRACE_HELPERS_H
3
4#include <darwintest.h>
5#include <libproc.h>
6#include <sys/sysctl.h>
7#include <System/sys/kdebug.h>
8
9static inline void
10reset_ktrace(void)
11{
12 (void)sysctl((int[]){ CTL_KERN, KERN_KDEBUG, KERN_KDREMOVE }, 3,
13 NULL, 0, NULL, 0);
14 kperf_reset();
15}
16
17static inline void
18start_controlling_ktrace(void)
19{
20 T_SETUPBEGIN;
21
22 int state = 0;
23 size_t statesz = sizeof(state);
24 int ret = sysctlbyname("ktrace.state", &state, &statesz, NULL, 0);
25 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "getting ktrace state");
26
27 if (state == 1) {
28 int ownerpid = 0;
29 size_t pidsz = sizeof(ownerpid);
30 ret = sysctlbyname("ktrace.owning_pid", &ownerpid, &pidsz, NULL, 0);
31 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "getting owning pid");
32
33 if (ownerpid <= 0) {
34 T_LOG("ktrace is in foreground, but no owner");
35 goto out;
36 }
37
38 char ownername[1024];
39 ret = proc_name(ownerpid, ownername, sizeof(ownername));
40 if (ret == 0) {
41 T_LOG("ktrace is in foreground, but owner (%d) has no name", ownerpid);
42 goto out;
43 }
44
45 T_LOG("ktrace is in foreground, owned by %s, sending SIGKILL", ownername);
46 kill(ownerpid, SIGKILL);
47 usleep(500000);
48
49 ret = proc_name(ownerpid, ownername, sizeof(ownername));
50 T_QUIET; T_ASSERT_EQ(ret, 0, "should have killed ktrace owner");
51 }
52
53out:
54 reset_ktrace();
55 T_ATEND(reset_ktrace);
56 T_SETUPEND;
57}
58
59#endif /* !defined(KTRACE_HELPERS_H) */