-#include <sys/file_internal.h>
-#include <sys/namei.h>
-#include <sys/vnode_internal.h>
-#if KTRACE
-#include <sys/ktrace.h>
-#endif
-#include <sys/malloc.h>
-#include <sys/syslog.h>
-#include <sys/sysproto.h>
-#include <sys/uio_internal.h>
-
-#include <bsm/audit_kernel.h>
-
-#if KTRACE
-static struct ktr_header *ktrgetheader(int type);
-static void ktrwrite(struct vnode *, struct ktr_header *, struct uio *);
-static int ktrcanset(struct proc *,struct proc *);
-static int ktrsetchildren(struct proc *,struct proc *,
- int, int, struct vnode *);
-static int ktrops(struct proc *,struct proc *,int,int,struct vnode *);
-
-
-static struct ktr_header *
-ktrgetheader(type)
- int type;
+#include <sys/priv.h>
+#include <sys/proc.h>
+char *proc_name_address(void *p);
+#include <sys/sysctl.h>
+#include <sys/vm.h>
+
+#include <kern/locks.h>
+#include <kern/assert.h>
+
+#include <sys/kdebug.h>
+#include <kperf/kperf.h>
+
+#include <kern/host.h>
+
+kern_return_t ktrace_background_available_notify_user(void);
+
+lck_mtx_t *ktrace_lock;
+
+/*
+ * The overall state of ktrace, whether it is unconfigured, in foreground mode,
+ * or in background mode. The state determines which processes can configure
+ * ktrace.
+ */
+static enum ktrace_state ktrace_state = KTRACE_STATE_OFF;
+
+/* The true owner of ktrace, checked by ktrace_access_check(). */
+static uint64_t ktrace_owning_unique_id = 0;
+static pid_t ktrace_owning_pid = 0;
+
+/*
+ * The background pid of ktrace, automatically made the owner when
+ * transitioning to background mode.
+ */
+static uint64_t ktrace_bg_unique_id = 0;
+static pid_t ktrace_bg_pid = 0;
+
+/* The name of the last process to configure ktrace. */
+static char ktrace_last_owner_execname[MAXCOMLEN + 1] = { 0 };
+
+/*
+ * Which subsystems of ktrace (currently kdebug and kperf) are active.
+ */
+static uint32_t ktrace_active_mask = 0;
+
+/*
+ * At boot or when a daemon has been newly loaded, it's necessary to bootstrap
+ * user space background tools by sending a background available notification
+ * when the init_background sysctl is made.
+ *
+ * Background tools must be RunAtLoad daemons.
+ */
+static boolean_t should_notify_on_init = TRUE;
+
+/* Set the owning process of ktrace. */
+static void ktrace_set_owning_proc(proc_t p);
+
+/* Reset ktrace ownership back to unowned. */
+static void ktrace_release_ownership(void);
+
+/* Make the background tool the owner of ktrace. */
+static void ktrace_promote_background(void);
+
+/*
+ * If user space sets a pid manually (through kperf "blessing"), ktrace should
+ * not treat resets as releasing ownership. At that point, ownership is only
+ * released when the owner is set to an invalid pid.
+ *
+ * This is managed by the user space-oriented function ktrace_set_owning_pid
+ * and ktrace_unset_owning_pid.
+ */
+boolean_t ktrace_keep_ownership_on_reset = FALSE;
+
+/* Allow user space to unset the owning pid and potentially reset ktrace. */
+static void ktrace_set_invalid_owning_pid(void);
+
+/*
+ * This flag allows any root process to set a new ktrace owner. It is
+ * currently used by Instruments.
+ */
+int ktrace_root_set_owner_allowed = 0;
+
+static void
+ktrace_reset_internal(uint32_t reset_mask)