/*
- * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
+ * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
*
* @Apple_LICENSE_HEADER_START@
*
#include <mach/mach_types.h>
#include <mach/mach_time.h>
#include <mach/mach_vm.h>
+#include <machine/atomic.h>
#include <machine/machine_routines.h>
#include <mach/machine.h>
#include <kern/task.h>
#include <kern/debug.h>
#include <kern/kalloc.h>
+#include <kern/cpu_number.h>
#include <kern/cpu_data.h>
#include <kern/assert.h>
#include <kern/telemetry.h>
#include <machine/pal_routines.h>
#include <machine/atomic.h>
+extern unsigned int wake_nkdbufs;
+extern unsigned int trace_wrap;
+
/*
* IOP(s)
*
- * https://coreoswiki.apple.com/wiki/pages/U6z3i0q9/Consistent_Logging_Implementers_Guide.html
- *
* IOP(s) are auxiliary cores that want to participate in kdebug event logging.
* They are registered dynamically. Each is assigned a cpu_id at registration.
*
*/
void task_act_iterate_wth_args(task_t, void (*)(thread_t, void *), void *);
-int cpu_number(void); /* XXX <machine/...> include path broken */
void commpage_update_kdebug_state(void); /* XXX sign */
extern int log_leaks;
static int kdbg_debug = 0;
-#if KDEBUG_MOJO_TRACE
-#include <sys/kdebugevents.h>
-static void kdebug_serial_print( /* forward */
- uint32_t, uint32_t, uint64_t,
- uintptr_t, uintptr_t, uintptr_t, uintptr_t, uintptr_t);
-#endif
-
int kdbg_control(int *, u_int, user_addr_t, size_t *);
static int kdbg_read(user_addr_t, size_t *, vnode_t, vfs_context_t, uint32_t);
static int kdbg_setpidex(kd_regtype *);
static int kdbg_setpid(kd_regtype *);
static void kdbg_thrmap_init(void);
-static int kdbg_reinit(boolean_t);
-static int kdbg_bootstrap(boolean_t);
+static int kdbg_reinit(bool);
+static int kdbg_bootstrap(bool);
static int kdbg_test(size_t flavor);
-static int kdbg_write_v1_header(boolean_t write_thread_map, vnode_t vp, vfs_context_t ctx);
+static int kdbg_write_v1_header(bool write_thread_map, vnode_t vp, vfs_context_t ctx);
static int kdbg_write_thread_map(vnode_t vp, vfs_context_t ctx);
static int kdbg_copyout_thread_map(user_addr_t buffer, size_t *buffer_size);
static void kdbg_clear_thread_map(void);
-static boolean_t kdbg_wait(uint64_t timeout_ms, boolean_t locked_wait);
+static bool kdbg_wait(uint64_t timeout_ms, bool locked_wait);
static void kdbg_wakeup(void);
int kdbg_cpumap_init_internal(kd_iop_t* iops, uint32_t cpu_count,
uint8_t** cpumap, uint32_t* cpumap_size);
-static kd_threadmap *kdbg_thrmap_init_internal(unsigned int count,
- unsigned int *mapsize,
- unsigned int *mapcount);
+static kd_threadmap *kdbg_thrmap_init_internal(size_t max_count,
+ vm_size_t *map_size, vm_size_t *map_count);
-static boolean_t kdebug_current_proc_enabled(uint32_t debugid);
+static bool kdebug_current_proc_enabled(uint32_t debugid);
static errno_t kdebug_check_trace_string(uint32_t debugid, uint64_t str_id);
int kdbg_write_v3_header(user_addr_t, size_t *, int);
// Helper functions
-static int create_buffers(boolean_t);
+static int create_buffers(bool);
static void delete_buffers(void);
extern int tasks_count;
#define KD_EARLY_BUFFER_SIZE (16 * 1024)
#define KD_EARLY_BUFFER_NBUFS (KD_EARLY_BUFFER_SIZE / sizeof(kd_buf))
-#if CONFIG_EMBEDDED
+#if defined(__x86_64__)
+__attribute__((aligned(KD_EARLY_BUFFER_SIZE)))
+static kd_buf kd_early_buffer[KD_EARLY_BUFFER_NBUFS];
+#else /* defined(__x86_64__) */
/*
- * On embedded, the space for this is carved out by osfmk/arm/data.s -- clang
+ * On ARM, the space for this is carved out by osfmk/arm/data.s -- clang
* has problems aligning to greater than 4K.
*/
extern kd_buf kd_early_buffer[KD_EARLY_BUFFER_NBUFS];
-#else /* CONFIG_EMBEDDED */
-__attribute__((aligned(KD_EARLY_BUFFER_SIZE)))
-static kd_buf kd_early_buffer[KD_EARLY_BUFFER_NBUFS];
-#endif /* !CONFIG_EMBEDDED */
+#endif /* !defined(__x86_64__) */
static unsigned int kd_early_index = 0;
static bool kd_early_overflow = false;
uint32_t kds_bufindx;
uint32_t kds_bufcnt;
uint32_t kds_readlast;
- boolean_t kds_lostevents;
+ bool kds_lostevents;
uint64_t kds_timestamp;
kd_buf kds_records[EVENTS_PER_STORAGE_UNIT];
struct kd_bufinfo {
union kds_ptr kd_list_head;
union kds_ptr kd_list_tail;
- boolean_t kd_lostevents;
+ bool kd_lostevents;
uint32_t _pad;
uint64_t kd_prev_timebase;
uint32_t num_bufs;
unsigned int kdlog_value3 = 0;
unsigned int kdlog_value4 = 0;
-static lck_spin_t * kdw_spin_lock;
-static lck_spin_t * kds_spin_lock;
+static LCK_GRP_DECLARE(kdebug_lck_grp, "kdebug");
+static LCK_SPIN_DECLARE(kdw_spin_lock, &kdebug_lck_grp);
+static LCK_SPIN_DECLARE(kds_spin_lock, &kdebug_lck_grp);
kd_threadmap *kd_mapptr = 0;
-unsigned int kd_mapsize = 0;
-unsigned int kd_mapcount = 0;
+vm_size_t kd_mapsize = 0;
+vm_size_t kd_mapcount = 0;
off_t RAW_file_offset = 0;
int RAW_file_written = 0;
#define BSC_SysCall 0x040c0000
#define MACH_SysCall 0x010c0000
-/* task to string structure */
-struct tts {
- task_t task; /* from procs task */
- pid_t pid; /* from procs p_pid */
- char task_comm[20];/* from procs p_comm */
+struct kd_task_name {
+ task_t ktn_task;
+ pid_t ktn_pid;
+ char ktn_name[20];
};
-typedef struct tts tts_t;
-
-struct krt {
- kd_threadmap *map; /* pointer to the map buffer */
- int count;
- int maxcount;
- struct tts *atts;
+struct kd_resolver {
+ kd_threadmap *krs_map;
+ vm_size_t krs_count;
+ vm_size_t krs_maxcount;
+ struct kd_task_name *krs_task;
};
+/*
+ * TRACE file formats...
+ *
+ * RAW_VERSION0
+ *
+ * uint32_t #threadmaps
+ * kd_threadmap[]
+ * kd_buf[]
+ *
+ * RAW_VERSION1
+ *
+ * RAW_header, with version_no set to RAW_VERSION1
+ * kd_threadmap[]
+ * Empty space to pad alignment to the nearest page boundary.
+ * kd_buf[]
+ *
+ * RAW_VERSION1+
+ *
+ * RAW_header, with version_no set to RAW_VERSION1
+ * kd_threadmap[]
+ * kd_cpumap_header, with version_no set to RAW_VERSION1
+ * kd_cpumap[]
+ * Empty space to pad alignment to the nearest page boundary.
+ * kd_buf[]
+ *
+ * V1+ implementation details...
+ *
+ * It would have been nice to add the cpumap data "correctly", but there were
+ * several obstacles. Existing code attempts to parse both V1 and V0 files.
+ * Due to the fact that V0 has no versioning or header, the test looks like
+ * this:
+ *
+ * // Read header
+ * if (header.version_no != RAW_VERSION1) { // Assume V0 }
+ *
+ * If we add a VERSION2 file format, all existing code is going to treat that
+ * as a VERSION0 file when reading it, and crash terribly when trying to read
+ * RAW_VERSION2 threadmap entries.
+ *
+ * To differentiate between a V1 and V1+ file, read as V1 until you reach
+ * the padding bytes. Then:
+ *
+ * boolean_t is_v1plus = FALSE;
+ * if (padding_bytes >= sizeof(kd_cpumap_header)) {
+ * kd_cpumap_header header = // read header;
+ * if (header.version_no == RAW_VERSION1) {
+ * is_v1plus = TRUE;
+ * }
+ * }
+ *
+ */
+
+#define RAW_VERSION3 0x00001000
+
+// Version 3 header
+// The header chunk has the tag 0x00001000 which also serves as a magic word
+// that identifies the file as a version 3 trace file. The header payload is
+// a set of fixed fields followed by a variable number of sub-chunks:
+/*
+ * ____________________________________________________________________________
+ | Offset | Size | Field |
+ | ----------------------------------------------------------------------------
+ | 0 | 4 | Tag (0x00001000) |
+ | 4 | 4 | Sub-tag. Represents the version of the header. |
+ | 8 | 8 | Length of header payload (40+8x) |
+ | 16 | 8 | Time base info. Two 32-bit numbers, numer/denom, |
+ | | | for converting timestamps to nanoseconds. |
+ | 24 | 8 | Timestamp of trace start. |
+ | 32 | 8 | Wall time seconds since Unix epoch. |
+ | | | As returned by gettimeofday(). |
+ | 40 | 4 | Wall time microseconds. As returned by gettimeofday(). |
+ | 44 | 4 | Local time zone offset in minutes. ( " ) |
+ | 48 | 4 | Type of daylight savings time correction to apply. ( " ) |
+ | 52 | 4 | Flags. 1 = 64-bit. Remaining bits should be written |
+ | | | as 0 and ignored when reading. |
+ | 56 | 8x | Variable number of sub-chunks. None are required. |
+ | | | Ignore unknown chunks. |
+ | ----------------------------------------------------------------------------
+ */
+// NOTE: The header sub-chunks are considered part of the header chunk,
+// so they must be included in the header chunk’s length field.
+// The CPU map is an optional sub-chunk of the header chunk. It provides
+// information about the CPUs that are referenced from the trace events.
+typedef struct {
+ uint32_t tag;
+ uint32_t sub_tag;
+ uint64_t length;
+ uint32_t timebase_numer;
+ uint32_t timebase_denom;
+ uint64_t timestamp;
+ uint64_t walltime_secs;
+ uint32_t walltime_usecs;
+ uint32_t timezone_minuteswest;
+ uint32_t timezone_dst;
+ uint32_t flags;
+} __attribute__((packed)) kd_header_v3;
+
+typedef struct {
+ uint32_t tag;
+ uint32_t sub_tag;
+ uint64_t length;
+} __attribute__((packed)) kd_chunk_header_v3;
+
+#define V3_CONFIG 0x00001b00
+#define V3_CPU_MAP 0x00001c00
+#define V3_THREAD_MAP 0x00001d00
+#define V3_RAW_EVENTS 0x00001e00
+#define V3_NULL_CHUNK 0x00002000
+
+// The current version of all kernel managed chunks is 1. The
+// V3_CURRENT_CHUNK_VERSION is added to ease the simple case
+// when most/all the kernel managed chunks have the same version.
+
+#define V3_CURRENT_CHUNK_VERSION 1
+#define V3_HEADER_VERSION V3_CURRENT_CHUNK_VERSION
+#define V3_CPUMAP_VERSION V3_CURRENT_CHUNK_VERSION
+#define V3_THRMAP_VERSION V3_CURRENT_CHUNK_VERSION
+#define V3_EVENT_DATA_VERSION V3_CURRENT_CHUNK_VERSION
+
typedef struct krt krt_t;
static uint32_t
-kdbg_cpu_count(boolean_t early_trace)
+kdbg_cpu_count(bool early_trace)
{
if (early_trace) {
-#if CONFIG_EMBEDDED
- return ml_get_cpu_count();
-#else
+#if defined(__x86_64__)
return max_ncpus;
-#endif
+#else /* defined(__x86_64__) */
+ return ml_get_cpu_count();
+#endif /* !defined(__x86_64__) */
}
+#if defined(__x86_64__)
host_basic_info_data_t hinfo;
mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
host_info((host_t)1 /* BSD_HOST */, HOST_BASIC_INFO, (host_info_t)&hinfo, &count);
assert(hinfo.logical_cpu_max > 0);
return hinfo.logical_cpu_max;
+#else /* defined(__x86_64__) */
+ return ml_get_topology_info()->max_cpu_id + 1;
+#endif /* !defined(__x86_64__) */
}
#if MACH_ASSERT
-#if CONFIG_EMBEDDED
-static boolean_t
+
+static bool
kdbg_iop_list_is_valid(kd_iop_t* iop)
{
if (iop) {
kd_iop_t* temp = iop;
do {
assert(!temp->next || temp->next->cpu_id == temp->cpu_id - 1);
- assert(temp->next || (temp->cpu_id == kdbg_cpu_count(FALSE) || temp->cpu_id == kdbg_cpu_count(TRUE)));
+ assert(temp->next || (temp->cpu_id == kdbg_cpu_count(false) || temp->cpu_id == kdbg_cpu_count(true)));
} while ((temp = temp->next));
/* Does each entry have a function and a name? */
} while ((temp = temp->next));
}
- return TRUE;
+ return true;
}
-static boolean_t
-kdbg_iop_list_contains_cpu_id(kd_iop_t* list, uint32_t cpu_id)
-{
- while (list) {
- if (list->cpu_id == cpu_id) {
- return TRUE;
- }
- list = list->next;
- }
-
- return FALSE;
-}
-#endif /* CONFIG_EMBEDDED */
#endif /* MACH_ASSERT */
static void
}
}
-static lck_grp_t *kdebug_lck_grp = NULL;
-
static void
-kdbg_set_tracing_enabled(boolean_t enabled, uint32_t trace_type)
+kdbg_set_tracing_enabled(bool enabled, uint32_t trace_type)
{
- int s = ml_set_interrupts_enabled(FALSE);
- lck_spin_lock_grp(kds_spin_lock, kdebug_lck_grp);
+ /*
+ * Drain any events from IOPs before making the state change. On
+ * enabling, this removes any stale events from before tracing. On
+ * disabling, this saves any events up to the point tracing is disabled.
+ */
+ kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops, KD_CALLBACK_SYNC_FLUSH,
+ NULL);
+
+ int s = ml_set_interrupts_enabled(false);
+ lck_spin_lock_grp(&kds_spin_lock, &kdebug_lck_grp);
+
if (enabled) {
/*
- * The oldest valid time is now; reject old events from IOPs.
+ * The oldest valid time is now; reject past events from IOPs.
*/
kd_ctrl_page.oldest_time = kdbg_timestamp();
kdebug_enable |= trace_type;
kd_ctrl_page.enabled = 0;
commpage_update_kdebug_state();
}
- lck_spin_unlock(kds_spin_lock);
+ lck_spin_unlock(&kds_spin_lock);
ml_set_interrupts_enabled(s);
if (enabled) {
- kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops, KD_CALLBACK_KDEBUG_ENABLED, NULL);
+ kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops,
+ KD_CALLBACK_KDEBUG_ENABLED, NULL);
} else {
- /*
- * If you do not flush the IOP trace buffers, they can linger
- * for a considerable period; consider code which disables and
- * deallocates without a final sync flush.
- */
- kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops, KD_CALLBACK_KDEBUG_DISABLED, NULL);
- kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops, KD_CALLBACK_SYNC_FLUSH, NULL);
+ kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops,
+ KD_CALLBACK_KDEBUG_DISABLED, NULL);
}
}
static void
-kdbg_set_flags(int slowflag, int enableflag, boolean_t enabled)
+kdbg_set_flags(int slowflag, int enableflag, bool enabled)
{
- int s = ml_set_interrupts_enabled(FALSE);
- lck_spin_lock_grp(kds_spin_lock, kdebug_lck_grp);
+ int s = ml_set_interrupts_enabled(false);
+ lck_spin_lock_grp(&kds_spin_lock, &kdebug_lck_grp);
if (enabled) {
kd_ctrl_page.kdebug_slowcheck |= slowflag;
kdebug_enable &= ~enableflag;
}
- lck_spin_unlock(kds_spin_lock);
+ lck_spin_unlock(&kds_spin_lock);
ml_set_interrupts_enabled(s);
}
/*
* Disable wrapping and return true if trace wrapped, false otherwise.
*/
-static boolean_t
+static bool
disable_wrap(uint32_t *old_slowcheck, uint32_t *old_flags)
{
- boolean_t wrapped;
- int s = ml_set_interrupts_enabled(FALSE);
- lck_spin_lock_grp(kds_spin_lock, kdebug_lck_grp);
+ bool wrapped;
+ int s = ml_set_interrupts_enabled(false);
+ lck_spin_lock_grp(&kds_spin_lock, &kdebug_lck_grp);
*old_slowcheck = kd_ctrl_page.kdebug_slowcheck;
*old_flags = kd_ctrl_page.kdebug_flags;
kd_ctrl_page.kdebug_flags &= ~KDBG_WRAPPED;
kd_ctrl_page.kdebug_flags |= KDBG_NOWRAP;
- lck_spin_unlock(kds_spin_lock);
+ lck_spin_unlock(&kds_spin_lock);
ml_set_interrupts_enabled(s);
return wrapped;
static void
enable_wrap(uint32_t old_slowcheck)
{
- int s = ml_set_interrupts_enabled(FALSE);
- lck_spin_lock_grp(kds_spin_lock, kdebug_lck_grp);
+ int s = ml_set_interrupts_enabled(false);
+ lck_spin_lock_grp(&kds_spin_lock, &kdebug_lck_grp);
kd_ctrl_page.kdebug_flags &= ~KDBG_NOWRAP;
kd_ctrl_page.kdebug_slowcheck &= ~SLOW_NOLOG;
}
- lck_spin_unlock(kds_spin_lock);
+ lck_spin_unlock(&kds_spin_lock);
ml_set_interrupts_enabled(s);
}
static int
-create_buffers(boolean_t early_trace)
+create_buffers(bool early_trace)
{
unsigned int i;
unsigned int p_buffer_size;
*/
kd_ctrl_page.kdebug_iops = kd_iops;
-#if CONFIG_EMBEDDED
assert(kdbg_iop_list_is_valid(kd_ctrl_page.kdebug_iops));
-#endif
/*
* If the list is valid, it is sorted, newest -> oldest. Each iop entry
for (i = 0; i < n_storage_buffers; i++) {
struct kd_storage *kds;
- int n_elements;
- int n;
+ uint16_t n_elements;
+ static_assert(N_STORAGE_UNITS_PER_BUFFER <= UINT16_MAX);
+ assert(kd_bufs[i].kdsb_size <= N_STORAGE_UNITS_PER_BUFFER *
+ sizeof(struct kd_storage));
n_elements = kd_bufs[i].kdsb_size / sizeof(struct kd_storage);
kds = kd_bufs[i].kdsb_addr;
- for (n = 0; n < n_elements; n++) {
+ for (uint16_t n = 0; n < n_elements; n++) {
kds[n].kds_next.buffer_index = kd_ctrl_page.kds_free_list.buffer_index;
kds[n].kds_next.offset = kd_ctrl_page.kds_free_list.offset;
for (i = 0; i < kd_ctrl_page.kdebug_cpus; i++) {
kdbip[i].kd_list_head.raw = KDS_PTR_NULL;
kdbip[i].kd_list_tail.raw = KDS_PTR_NULL;
- kdbip[i].kd_lostevents = FALSE;
+ kdbip[i].kd_lostevents = false;
kdbip[i].num_bufs = 0;
}
kdsp.raw = kdsp_raw;
- s = ml_set_interrupts_enabled(FALSE);
- lck_spin_lock_grp(kds_spin_lock, kdebug_lck_grp);
+ s = ml_set_interrupts_enabled(false);
+ lck_spin_lock_grp(&kds_spin_lock, &kdebug_lck_grp);
kdbp = &kdbip[cpu];
kd_ctrl_page.kds_inuse_count--;
}
- lck_spin_unlock(kds_spin_lock);
+ lck_spin_unlock(&kds_spin_lock);
ml_set_interrupts_enabled(s);
}
-
-boolean_t
+bool
allocate_storage_unit(int cpu)
{
union kds_ptr kdsp;
struct kd_storage *kdsp_actual, *kdsp_next_actual;
struct kd_bufinfo *kdbp, *kdbp_vict, *kdbp_try;
uint64_t oldest_ts, ts;
- boolean_t retval = TRUE;
+ bool retval = true;
int s = 0;
- s = ml_set_interrupts_enabled(FALSE);
- lck_spin_lock_grp(kds_spin_lock, kdebug_lck_grp);
+ s = ml_set_interrupts_enabled(false);
+ lck_spin_lock_grp(&kds_spin_lock, &kdebug_lck_grp);
kdbp = &kdbip[cpu];
*/
if (kd_ctrl_page.kdebug_flags & KDBG_NOWRAP) {
kd_ctrl_page.kdebug_slowcheck |= SLOW_NOLOG;
- kdbp->kd_lostevents = TRUE;
- retval = FALSE;
+ kdbp->kd_lostevents = true;
+ retval = false;
goto out;
}
kdbp_vict = NULL;
kdebug_enable = 0;
kd_ctrl_page.enabled = 0;
commpage_update_kdebug_state();
- retval = FALSE;
+ retval = false;
goto out;
}
kdsp = kdbp_vict->kd_list_head;
if (kdbp_vict->kd_list_head.raw != KDS_PTR_NULL) {
kdsp_next_actual = POINTER_FROM_KDS_PTR(kdbp_vict->kd_list_head);
- kdsp_next_actual->kds_lostevents = TRUE;
+ kdsp_next_actual->kds_lostevents = true;
} else {
- kdbp_vict->kd_lostevents = TRUE;
+ kdbp_vict->kd_lostevents = true;
}
if (kd_ctrl_page.oldest_time < oldest_ts) {
kdsp_actual->kds_readlast = 0;
kdsp_actual->kds_lostevents = kdbp->kd_lostevents;
- kdbp->kd_lostevents = FALSE;
+ kdbp->kd_lostevents = false;
kdsp_actual->kds_bufindx = 0;
if (kdbp->kd_list_head.raw == KDS_PTR_NULL) {
}
kdbp->kd_list_tail = kdsp;
out:
- lck_spin_unlock(kds_spin_lock);
+ lck_spin_unlock(&kds_spin_lock);
ml_set_interrupts_enabled(s);
return retval;
* Remove when fixed.
*/
{
- boolean_t is_valid_name = FALSE;
+ bool is_valid_name = false;
for (uint32_t length = 0; length < sizeof(callback.iop_name); ++length) {
/* This is roughly isprintable(c) */
if (callback.iop_name[length] > 0x20 && callback.iop_name[length] < 0x7F) {
}
if (callback.iop_name[length] == 0) {
if (length) {
- is_valid_name = TRUE;
+ is_valid_name = true;
}
break;
}
* TLDR; Must not read kd_iops more than once per loop.
*/
iop->next = kd_iops;
- iop->cpu_id = iop->next ? (iop->next->cpu_id + 1) : kdbg_cpu_count(FALSE);
+ iop->cpu_id = iop->next ? (iop->next->cpu_id + 1) : kdbg_cpu_count(false);
/*
* Header says OSCompareAndSwapPtr has a memory barrier
goto out1;
}
-#if CONFIG_EMBEDDED
- /*
- * When start_kern_tracing is called by the kernel to trace very
- * early kernel events, it saves data to a secondary buffer until
- * it is possible to initialize ktrace, and then dumps the events
- * into the ktrace buffer using this method. In this case, iops will
- * be NULL, and the coreid will be zero. It is not possible to have
- * a valid IOP coreid of zero, so pass if both iops is NULL and coreid
- * is zero.
- */
- assert(kdbg_iop_list_contains_cpu_id(kd_ctrl_page.kdebug_iops, coreid) || (kd_ctrl_page.kdebug_iops == NULL && coreid == 0));
-#endif
-
disable_preemption();
if (kd_ctrl_page.enabled == 0) {
kdbp = &kdbip[coreid];
timestamp &= KDBG_TIMESTAMP_MASK;
-#if KDEBUG_MOJO_TRACE
- if (kdebug_enable & KDEBUG_ENABLE_SERIAL) {
- kdebug_serial_print(coreid, debugid, timestamp,
- arg1, arg2, arg3, arg4, threadid);
- }
-#endif
-
retry_q:
kds_raw = kdbp->kd_list_tail;
}
if (kdsp_actual == NULL || bindx >= EVENTS_PER_STORAGE_UNIT) {
- if (allocate_storage_unit(coreid) == FALSE) {
+ if (allocate_storage_unit(coreid) == false) {
/*
* this can only happen if wrapping
* has been disabled
cpu = cpu_number();
kdbp = &kdbip[cpu];
-#if KDEBUG_MOJO_TRACE
- if (kdebug_enable & KDEBUG_ENABLE_SERIAL) {
- kdebug_serial_print(cpu, debugid,
- kdbg_timestamp() & KDBG_TIMESTAMP_MASK,
- arg1, arg2, arg3, arg4, arg5);
- }
-#endif
-
retry_q:
kds_raw = kdbp->kd_list_tail;
}
if (kdsp_actual == NULL || bindx >= EVENTS_PER_STORAGE_UNIT) {
- if (allocate_storage_unit(cpu) == FALSE) {
+ if (allocate_storage_unit(cpu) == false) {
/*
* this can only happen if wrapping
* has been disabled
}
}
+__attribute__((noinline))
void
kernel_debug(
uint32_t debugid,
(uintptr_t)thread_tid(current_thread()), 0);
}
+__attribute__((noinline))
void
kernel_debug1(
uint32_t debugid,
kernel_debug_internal(debugid, arg1, arg2, arg3, arg4, arg5, 0);
}
+__attribute__((noinline))
void
kernel_debug_flags(
uint32_t debugid,
(uintptr_t)thread_tid(current_thread()), flags);
}
+__attribute__((noinline))
void
kernel_debug_filtered(
uint32_t debugid,
/* If early tracing is over, use the normal path. */
if (kd_early_done) {
- KERNEL_DEBUG_CONSTANT(debugid, arg1, arg2, arg3, arg4, 0);
+ KDBG_RELEASE(debugid, arg1, arg2, arg3, arg4);
return;
}
/* reset the current oldest time to allow early events */
kd_ctrl_page.oldest_time = 0;
-#if !CONFIG_EMBEDDED
+#if defined(__x86_64__)
/* Fake sentinel marking the start of kernel time relative to TSC */
- kernel_debug_enter(0,
- TRACE_TIMESTAMPS,
- 0,
- (uint32_t)(tsc_rebase_abs_time >> 32),
- (uint32_t)tsc_rebase_abs_time,
- tsc_at_boot,
- 0,
- 0);
-#endif
+ kernel_debug_enter(0, TRACE_TIMESTAMPS, 0,
+ (uint32_t)(tsc_rebase_abs_time >> 32), (uint32_t)tsc_rebase_abs_time,
+ tsc_at_boot, 0, 0);
+#endif /* defined(__x86_64__) */
for (unsigned int i = 0; i < kd_early_index; i++) {
kernel_debug_enter(0,
kd_early_buffer[i].debugid,
kernel_debug_disable(void)
{
if (kdebug_enable) {
- kdbg_set_tracing_enabled(FALSE, 0);
+ kdbg_set_tracing_enabled(false, 0);
}
}
* the first atomic load test of Global Typefilter Ptr, this function
* can then safely use the remaining global state without atomic checks.
*/
- if (!__c11_atomic_load((_Atomic typefilter_t *)&kdbg_typefilter, memory_order_acquire)) {
+ if (!os_atomic_load(&kdbg_typefilter, acquire)) {
return EINVAL;
}
VM_KERN_MEMORY_NONE,
kdbg_typefilter_memory_entry, // port (memory entry!)
0, // offset (in memory entry)
- FALSE, // should copy
+ false, // should copy
VM_PROT_READ, // cur_prot
VM_PROT_READ, // max_prot
VM_INHERIT_SHARE)); // inherit behavior on fork
* Trace system and scheduling events circumvent this check, as do events
* emitted in interrupt context.
*/
-static boolean_t
+static bool
kdebug_current_proc_enabled(uint32_t debugid)
{
/* can't determine current process in interrupt context */
if (ml_at_interrupt_context()) {
- return TRUE;
+ return true;
}
/* always emit trace system and scheduling events */
if ((KDBG_EXTRACT_CLASS(debugid) == DBG_TRACE ||
(debugid & KDBG_CSC_MASK) == MACHDBG_CODE(DBG_MACH_SCHED, 0))) {
- return TRUE;
+ return true;
}
if (kd_ctrl_page.kdebug_flags & KDBG_PIDCHECK) {
/* only the process with the kdebug bit set is allowed */
if (cur_proc && !(cur_proc->p_kdebug)) {
- return FALSE;
+ return false;
}
} else if (kd_ctrl_page.kdebug_flags & KDBG_PIDEXCLUDE) {
proc_t cur_proc = current_proc();
/* every process except the one with the kdebug bit set is allowed */
if (cur_proc && cur_proc->p_kdebug) {
- return FALSE;
+ return false;
}
}
- return TRUE;
+ return true;
}
-boolean_t
+bool
kdebug_debugid_enabled(uint32_t debugid)
{
/* if no filtering is enabled */
if (!kd_ctrl_page.kdebug_slowcheck) {
- return TRUE;
+ return true;
}
return kdebug_debugid_explicitly_enabled(debugid);
}
-boolean_t
+bool
kdebug_debugid_explicitly_enabled(uint32_t debugid)
{
if (kd_ctrl_page.kdebug_flags & KDBG_TYPEFILTER_CHECK) {
return typefilter_is_debugid_allowed(kdbg_typefilter, debugid);
} else if (KDBG_EXTRACT_CLASS(debugid) == DBG_TRACE) {
- return TRUE;
+ return true;
} else if (kd_ctrl_page.kdebug_flags & KDBG_RANGECHECK) {
if (debugid < kdlog_beg || debugid > kdlog_end) {
- return FALSE;
+ return false;
}
} else if (kd_ctrl_page.kdebug_flags & KDBG_VALCHECK) {
if ((debugid & KDBG_EVENTID_MASK) != kdlog_value1 &&
(debugid & KDBG_EVENTID_MASK) != kdlog_value2 &&
(debugid & KDBG_EVENTID_MASK) != kdlog_value3 &&
(debugid & KDBG_EVENTID_MASK) != kdlog_value4) {
- return FALSE;
+ return false;
}
}
- return TRUE;
+ return true;
+}
+
+bool
+kdebug_using_continuous_time(void)
+{
+ return kdebug_enable & KDEBUG_ENABLE_CONT_TIME;
}
/*
return 0;
}
-static void
-kdbg_lock_init(void)
-{
- static lck_grp_attr_t *kdebug_lck_grp_attr = NULL;
- static lck_attr_t *kdebug_lck_attr = NULL;
-
- if (kd_ctrl_page.kdebug_flags & KDBG_LOCKINIT) {
- return;
- }
-
- assert(kdebug_lck_grp_attr == NULL);
- kdebug_lck_grp_attr = lck_grp_attr_alloc_init();
- kdebug_lck_grp = lck_grp_alloc_init("kdebug", kdebug_lck_grp_attr);
- kdebug_lck_attr = lck_attr_alloc_init();
-
- kds_spin_lock = lck_spin_alloc_init(kdebug_lck_grp, kdebug_lck_attr);
- kdw_spin_lock = lck_spin_alloc_init(kdebug_lck_grp, kdebug_lck_attr);
-
- kd_ctrl_page.kdebug_flags |= KDBG_LOCKINIT;
-}
-
int
-kdbg_bootstrap(boolean_t early_trace)
+kdbg_bootstrap(bool early_trace)
{
kd_ctrl_page.kdebug_flags &= ~KDBG_WRAPPED;
}
int
-kdbg_reinit(boolean_t early_trace)
+kdbg_reinit(bool early_trace)
{
int ret = 0;
*arg_uniqueid = 0;
} else {
*arg_pid = proc->p_pid;
- *arg_uniqueid = proc->p_uniqueid;
+ /* Fit in a trace point */
+ *arg_uniqueid = (long)proc->p_uniqueid;
if ((uint64_t) *arg_uniqueid != proc->p_uniqueid) {
*arg_uniqueid = 0;
}
void
-kdbg_trace_string(struct proc *proc, long *arg1, long *arg2, long *arg3, long *arg4)
+kdbg_trace_string(struct proc *proc, long *arg1, long *arg2, long *arg3,
+ long *arg4)
{
- char *dbg_nameptr;
- int dbg_namelen;
- long dbg_parms[4];
-
if (!proc) {
*arg1 = 0;
*arg2 = 0;
*arg4 = 0;
return;
}
- /*
- * Collect the pathname for tracing
- */
- dbg_nameptr = proc->p_comm;
- dbg_namelen = (int)strlen(proc->p_comm);
- dbg_parms[0] = 0L;
- dbg_parms[1] = 0L;
- dbg_parms[2] = 0L;
- dbg_parms[3] = 0L;
- if (dbg_namelen > (int)sizeof(dbg_parms)) {
- dbg_namelen = (int)sizeof(dbg_parms);
- }
-
- strncpy((char *)dbg_parms, dbg_nameptr, dbg_namelen);
+ const char *procname = proc_best_name(proc);
+ size_t namelen = strlen(procname);
- *arg1 = dbg_parms[0];
- *arg2 = dbg_parms[1];
- *arg3 = dbg_parms[2];
- *arg4 = dbg_parms[3];
-}
+ long args[4] = { 0 };
-static void
-kdbg_resolve_map(thread_t th_act, void *opaque)
-{
- kd_threadmap *mapptr;
- krt_t *t = (krt_t *)opaque;
+ if (namelen > sizeof(args)) {
+ namelen = sizeof(args);
+ }
- if (t->count < t->maxcount) {
- mapptr = &t->map[t->count];
- mapptr->thread = (uintptr_t)thread_tid(th_act);
+ strncpy((char *)args, procname, namelen);
- (void) strlcpy(mapptr->command, t->atts->task_comm,
- sizeof(t->atts->task_comm));
- /*
- * Some kernel threads have no associated pid.
- * We still need to mark the entry as valid.
- */
- if (t->atts->pid) {
- mapptr->valid = t->atts->pid;
- } else {
- mapptr->valid = 1;
- }
-
- t->count++;
- }
+ *arg1 = args[0];
+ *arg2 = args[1];
+ *arg3 = args[2];
+ *arg4 = args[3];
}
/*
}
}
-static kd_threadmap *
-kdbg_thrmap_init_internal(unsigned int count, unsigned int *mapsize, unsigned int *mapcount)
+static void
+kd_resolve_map(thread_t thread, void *opaque)
{
- kd_threadmap *mapptr;
- proc_t p;
- struct krt akrt;
- int tts_count = 0; /* number of task-to-string structures */
- struct tts *tts_mapptr;
- unsigned int tts_mapsize = 0;
- vm_offset_t kaddr;
-
- assert(mapsize != NULL);
- assert(mapcount != NULL);
-
- *mapcount = threads_count;
- tts_count = tasks_count;
-
- /*
- * The proc count could change during buffer allocation,
- * so introduce a small fudge factor to bump up the
- * buffer sizes. This gives new tasks some chance of
- * making into the tables. Bump up by 25%.
- */
- *mapcount += *mapcount / 4;
- tts_count += tts_count / 4;
-
- *mapsize = *mapcount * sizeof(kd_threadmap);
+ struct kd_resolver *resolve = opaque;
- if (count && count < *mapcount) {
- return 0;
- }
+ if (resolve->krs_count < resolve->krs_maxcount) {
+ kd_threadmap *map = &resolve->krs_map[resolve->krs_count];
+ struct kd_task_name *task_name = resolve->krs_task;
+ map->thread = (uintptr_t)thread_tid(thread);
- if ((kmem_alloc(kernel_map, &kaddr, (vm_size_t)*mapsize, VM_KERN_MEMORY_DIAG) == KERN_SUCCESS)) {
- bzero((void *)kaddr, *mapsize);
- mapptr = (kd_threadmap *)kaddr;
- } else {
- return 0;
+ (void)strlcpy(map->command, task_name->ktn_name, sizeof(map->command));
+ /*
+ * Kernel threads should still be marked with non-zero valid bit.
+ */
+ pid_t pid = resolve->krs_task->ktn_pid;
+ map->valid = pid == 0 ? 1 : pid;
+ resolve->krs_count++;
}
+}
- tts_mapsize = tts_count * sizeof(struct tts);
-
- if ((kmem_alloc(kernel_map, &kaddr, (vm_size_t)tts_mapsize, VM_KERN_MEMORY_DIAG) == KERN_SUCCESS)) {
- bzero((void *)kaddr, tts_mapsize);
- tts_mapptr = (struct tts *)kaddr;
- } else {
- kmem_free(kernel_map, (vm_offset_t)mapptr, *mapsize);
-
- return 0;
- }
+static vm_size_t
+kd_resolve_tasks(struct kd_task_name *task_names, vm_size_t ntasks)
+{
+ vm_size_t i = 0;
+ proc_t p = PROC_NULL;
- /*
- * Save the proc's name and take a reference for each task associated
- * with a valid process.
- */
proc_list_lock();
-
- int i = 0;
ALLPROC_FOREACH(p) {
- if (i >= tts_count) {
+ if (i >= ntasks) {
break;
}
- if (p->p_lflag & P_LEXIT) {
- continue;
- }
- if (p->task) {
+ /*
+ * Only record processes that can be referenced and are not exiting.
+ */
+ if (p->task && (p->p_lflag & P_LEXIT) == 0) {
task_reference(p->task);
- tts_mapptr[i].task = p->task;
- tts_mapptr[i].pid = p->p_pid;
- (void)strlcpy(tts_mapptr[i].task_comm, proc_best_name(p), sizeof(tts_mapptr[i].task_comm));
+ task_names[i].ktn_task = p->task;
+ task_names[i].ktn_pid = p->p_pid;
+ (void)strlcpy(task_names[i].ktn_name, proc_best_name(p),
+ sizeof(task_names[i].ktn_name));
i++;
}
}
- tts_count = i;
-
proc_list_unlock();
+ return i;
+}
+
+static vm_size_t
+kd_resolve_threads(kd_threadmap *map, struct kd_task_name *task_names,
+ vm_size_t ntasks, vm_size_t nthreads)
+{
+ struct kd_resolver resolver = {
+ .krs_map = map, .krs_count = 0, .krs_maxcount = nthreads,
+ };
+
+ for (int i = 0; i < ntasks; i++) {
+ struct kd_task_name *cur_task = &task_names[i];
+ resolver.krs_task = cur_task;
+ task_act_iterate_wth_args(cur_task->ktn_task, kd_resolve_map,
+ &resolver);
+ task_deallocate(cur_task->ktn_task);
+ }
+
+ return resolver.krs_count;
+}
+
+static kd_threadmap *
+kdbg_thrmap_init_internal(size_t maxthreads, vm_size_t *mapsize,
+ vm_size_t *mapcount)
+{
+ kd_threadmap *thread_map = NULL;
+ struct kd_task_name *task_names;
+ vm_size_t names_size = 0;
+
+ assert(mapsize != NULL);
+ assert(mapcount != NULL);
+
+ vm_size_t nthreads = threads_count;
+ vm_size_t ntasks = tasks_count;
+
/*
- * Initialize thread map data
+ * Allow 25% more threads and tasks to be created between now and taking the
+ * proc_list_lock.
*/
- akrt.map = mapptr;
- akrt.count = 0;
- akrt.maxcount = *mapcount;
+ if (os_add_overflow(nthreads, nthreads / 4, &nthreads) ||
+ os_add_overflow(ntasks, ntasks / 4, &ntasks)) {
+ return NULL;
+ }
- for (i = 0; i < tts_count; i++) {
- akrt.atts = &tts_mapptr[i];
- task_act_iterate_wth_args(tts_mapptr[i].task, kdbg_resolve_map, &akrt);
- task_deallocate((task_t)tts_mapptr[i].task);
+ *mapcount = nthreads;
+ if (os_mul_overflow(nthreads, sizeof(kd_threadmap), mapsize)) {
+ return NULL;
+ }
+ if (os_mul_overflow(ntasks, sizeof(task_names[0]), &names_size)) {
+ return NULL;
}
- kmem_free(kernel_map, (vm_offset_t)tts_mapptr, tts_mapsize);
- *mapcount = akrt.count;
+ /*
+ * Wait until the out-parameters have been filled with the needed size to
+ * do the bounds checking on the provided maximum.
+ */
+ if (maxthreads != 0 && maxthreads < nthreads) {
+ return NULL;
+ }
- return mapptr;
+ thread_map = kalloc_tag(*mapsize, VM_KERN_MEMORY_DIAG);
+ bzero(thread_map, *mapsize);
+ task_names = kheap_alloc(KHEAP_TEMP, names_size, Z_WAITOK | Z_ZERO);
+ ntasks = kd_resolve_tasks(task_names, ntasks);
+ *mapcount = kd_resolve_threads(thread_map, task_names, ntasks, nthreads);
+ kheap_free(KHEAP_TEMP, task_names, names_size);
+ return thread_map;
}
static void
{
ktrace_assert_lock_held();
- kdbg_lock_init();
-
kdbg_clear();
if (kdbg_typefilter) {
typefilter_reject_all(kdbg_typefilter);
void
kdebug_free_early_buf(void)
{
-#if !CONFIG_EMBEDDED
- /* Must be done with the buffer, so release it back to the VM.
- * On embedded targets this buffer is freed when the BOOTDATA segment is freed. */
+#if defined(__x86_64__)
+ /*
+ * Make Intel aware that the early buffer is no longer being used. ARM
+ * handles this as part of the BOOTDATA segment.
+ */
ml_static_mfree((vm_offset_t)&kd_early_buffer, sizeof(kd_early_buffer));
-#endif
+#endif /* defined(__x86_64__) */
}
int
*/
kd_ctrl_page.kdebug_flags |= KDBG_PIDCHECK;
kd_ctrl_page.kdebug_flags &= ~KDBG_PIDEXCLUDE;
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
p->p_kdebug = 1;
} else {
*/
kd_ctrl_page.kdebug_flags |= KDBG_PIDEXCLUDE;
kd_ctrl_page.kdebug_flags &= ~KDBG_PIDCHECK;
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
p->p_kdebug = 1;
} else {
* that any non-null kdbg_typefilter means a
* valid memory_entry is available.
*/
- __c11_atomic_store(((_Atomic typefilter_t*)&kdbg_typefilter), tf, memory_order_release);
+ os_atomic_store(&kdbg_typefilter, tf, release);
return KERN_SUCCESS;
}
assert(kdbg_typefilter);
kd_ctrl_page.kdebug_flags &= ~(KDBG_RANGECHECK | KDBG_VALCHECK);
kd_ctrl_page.kdebug_flags |= KDBG_TYPEFILTER_CHECK;
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
commpage_update_kdebug_state();
}
kd_ctrl_page.kdebug_flags &= ~KDBG_TYPEFILTER_CHECK;
if ((kd_ctrl_page.kdebug_flags & (KDBG_PIDCHECK | KDBG_PIDEXCLUDE))) {
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
} else {
- kdbg_set_flags(SLOW_CHECKS, 0, FALSE);
+ kdbg_set_flags(SLOW_CHECKS, 0, false);
}
commpage_update_kdebug_state();
kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
kd_ctrl_page.kdebug_flags &= ~KDBG_VALCHECK; /* Turn off specific value check */
kd_ctrl_page.kdebug_flags |= (KDBG_RANGECHECK | KDBG_CLASSTYPE);
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
break;
case KDBG_SUBCLSTYPE:
val_1 = (kdr->value1 & 0xff);
kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
kd_ctrl_page.kdebug_flags &= ~KDBG_VALCHECK; /* Turn off specific value check */
kd_ctrl_page.kdebug_flags |= (KDBG_RANGECHECK | KDBG_SUBCLSTYPE);
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
break;
case KDBG_RANGETYPE:
kdlog_beg = (kdr->value1);
kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
kd_ctrl_page.kdebug_flags &= ~KDBG_VALCHECK; /* Turn off specific value check */
kd_ctrl_page.kdebug_flags |= (KDBG_RANGECHECK | KDBG_RANGETYPE);
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
break;
case KDBG_VALCHECK:
kdlog_value1 = (kdr->value1);
kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
kd_ctrl_page.kdebug_flags &= ~KDBG_RANGECHECK; /* Turn off range check */
kd_ctrl_page.kdebug_flags |= KDBG_VALCHECK; /* Turn on specific value check */
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
break;
case KDBG_TYPENONE:
kd_ctrl_page.kdebug_flags &= (unsigned int)~KDBG_CKTYPES;
if ((kd_ctrl_page.kdebug_flags & (KDBG_RANGECHECK | KDBG_VALCHECK |
KDBG_PIDCHECK | KDBG_PIDEXCLUDE |
KDBG_TYPEFILTER_CHECK))) {
- kdbg_set_flags(SLOW_CHECKS, 0, TRUE);
+ kdbg_set_flags(SLOW_CHECKS, 0, true);
} else {
- kdbg_set_flags(SLOW_CHECKS, 0, FALSE);
+ kdbg_set_flags(SLOW_CHECKS, 0, false);
}
kdlog_beg = 0;
static int
kdbg_write_to_vnode(caddr_t buffer, size_t size, vnode_t vp, vfs_context_t ctx, off_t file_offset)
{
- return vn_rdwr(UIO_WRITE, vp, buffer, size, file_offset, UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT,
+ assert(size < INT_MAX);
+ return vn_rdwr(UIO_WRITE, vp, buffer, (int)size, file_offset, UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT,
vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
}
return ret;
}
-int
-kdbg_write_v3_chunk_header_to_buffer(void * buffer, uint32_t tag, uint32_t sub_tag, uint64_t length)
-{
- kd_chunk_header_v3 header = {
- .tag = tag,
- .sub_tag = sub_tag,
- .length = length,
- };
-
- if (!buffer) {
- return 0;
- }
-
- memcpy(buffer, &header, sizeof(kd_chunk_header_v3));
-
- return sizeof(kd_chunk_header_v3);
-}
-
-int
+static int
kdbg_write_v3_chunk_to_fd(uint32_t tag, uint32_t sub_tag, uint64_t length, void *payload, uint64_t payload_size, int fd)
{
proc_t p;
vnode_t vp;
p = current_proc();
- proc_fdlock(p);
- if ((fp_lookup(p, fd, &fp, 1))) {
- proc_fdunlock(p);
- return EFAULT;
+ if (fp_get_ftype(p, fd, DTYPE_VNODE, EBADF, &fp)) {
+ return EBADF;
}
+ vp = fp->fp_glob->fg_data;
context.vc_thread = current_thread();
- context.vc_ucred = fp->f_fglob->fg_cred;
-
- if (FILEGLOB_DTYPE(fp->f_fglob) != DTYPE_VNODE) {
- fp_drop(p, fd, fp, 1);
- proc_fdunlock(p);
- return EBADF;
- }
- vp = (struct vnode *) fp->f_fglob->fg_data;
- proc_fdunlock(p);
+ context.vc_ucred = fp->fp_glob->fg_cred;
if ((vnode_getwithref(vp)) == 0) {
- RAW_file_offset = fp->f_fglob->fg_offset;
+ RAW_file_offset = fp->fp_glob->fg_offset;
kd_chunk_header_v3 chunk_header = {
.tag = tag,
RAW_file_offset += payload_size;
}
- fp->f_fglob->fg_offset = RAW_file_offset;
+ fp->fp_glob->fg_offset = RAW_file_offset;
vnode_put(vp);
}
ret = EINVAL;
goto bail;
}
- thrmap_size = kd_mapcount * sizeof(kd_threadmap);
+ if (os_mul_overflow(kd_mapcount, sizeof(kd_threadmap), &thrmap_size)) {
+ ret = ERANGE;
+ goto bail;
+ }
mach_timebase_info_data_t timebase = {0, 0};
clock_timebase_info(&timebase);
// If its a buffer, check if we have enough space to copy the header and the maps.
if (user_header) {
- bytes_needed = header.length + thrmap_size + (2 * sizeof(kd_chunk_header_v3));
+ bytes_needed = (size_t)header.length + thrmap_size + (2 * sizeof(kd_chunk_header_v3));
if (*user_header_size < bytes_needed) {
ret = EINVAL;
goto bail;
kdbg_readcurthrmap(user_addr_t buffer, size_t *bufsize)
{
kd_threadmap *mapptr;
- unsigned int mapsize;
- unsigned int mapcount;
- unsigned int count = 0;
+ vm_size_t mapsize;
+ vm_size_t mapcount;
int ret = 0;
+ size_t count = *bufsize / sizeof(kd_threadmap);
- count = *bufsize / sizeof(kd_threadmap);
*bufsize = 0;
if ((mapptr = kdbg_thrmap_init_internal(count, &mapsize, &mapcount))) {
*bufsize = (mapcount * sizeof(kd_threadmap));
}
- kmem_free(kernel_map, (vm_offset_t)mapptr, mapsize);
+ kfree(mapptr, mapsize);
} else {
ret = EINVAL;
}
}
static int
-kdbg_write_v1_header(boolean_t write_thread_map, vnode_t vp, vfs_context_t ctx)
+kdbg_write_v1_header(bool write_thread_map, vnode_t vp, vfs_context_t ctx)
{
int ret = 0;
RAW_header header;
uint32_t extra_thread_count = 0;
uint32_t cpumap_size;
size_t map_size = 0;
- size_t map_count = 0;
+ uint32_t map_count = 0;
if (write_thread_map) {
assert(kd_ctrl_page.kdebug_flags & KDBG_MAPINIT);
- map_count = kd_mapcount;
- map_size = map_count * sizeof(kd_threadmap);
+ if (kd_mapcount > UINT32_MAX) {
+ return ERANGE;
+ }
+ map_count = (uint32_t)kd_mapcount;
+ if (os_mul_overflow(map_count, sizeof(kd_threadmap), &map_size)) {
+ return ERANGE;
+ }
+ if (map_size >= INT_MAX) {
+ return ERANGE;
+ }
}
/*
assert(vp);
assert(ctx);
- pad_size = PAGE_16KB - ((sizeof(RAW_header) + map_size) & PAGE_MASK_64);
+ pad_size = PAGE_16KB - ((sizeof(RAW_header) + map_size) & PAGE_MASK);
cpumap_size = sizeof(kd_cpumap_header) + kd_ctrl_page.kdebug_cpus * sizeof(kd_cpumap);
if (cpumap_size > pad_size) {
header.TOD_secs = secs;
header.TOD_usecs = usecs;
- ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)&header, sizeof(RAW_header), RAW_file_offset,
+ ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)&header, (int)sizeof(RAW_header), RAW_file_offset,
UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
if (ret) {
goto write_error;
RAW_file_written += sizeof(RAW_header);
if (write_thread_map) {
- ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)kd_mapptr, map_size, RAW_file_offset,
+ assert(map_size < INT_MAX);
+ ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)kd_mapptr, (int)map_size, RAW_file_offset,
UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
if (ret) {
goto write_error;
if (extra_thread_count) {
pad_size = extra_thread_count * sizeof(kd_threadmap);
- pad_buf = kalloc(pad_size);
+ pad_buf = kheap_alloc(KHEAP_TEMP, pad_size, Z_WAITOK | Z_ZERO);
if (!pad_buf) {
ret = ENOMEM;
goto write_error;
}
- memset(pad_buf, 0, pad_size);
- ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)pad_buf, pad_size, RAW_file_offset,
+ assert(pad_size < INT_MAX);
+ ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)pad_buf, (int)pad_size, RAW_file_offset,
UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
- kfree(pad_buf, pad_size);
+ kheap_free(KHEAP_TEMP, pad_buf, pad_size);
if (ret) {
goto write_error;
}
RAW_file_written += pad_size;
}
- pad_size = PAGE_SIZE - (RAW_file_offset & PAGE_MASK_64);
+ pad_size = PAGE_SIZE - (RAW_file_offset & PAGE_MASK);
if (pad_size) {
- pad_buf = (char *)kalloc(pad_size);
+ pad_buf = (char *)kheap_alloc(KHEAP_TEMP, pad_size, Z_WAITOK | Z_ZERO);
if (!pad_buf) {
ret = ENOMEM;
goto write_error;
}
- memset(pad_buf, 0, pad_size);
/*
* embed a cpumap in the padding bytes.
memset(pad_buf, 0, pad_size);
}
- ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)pad_buf, pad_size, RAW_file_offset,
+ assert(pad_size < INT_MAX);
+ ret = vn_rdwr(UIO_WRITE, vp, (caddr_t)pad_buf, (int)pad_size, RAW_file_offset,
UIO_SYSSPACE, IO_NODELOCKED | IO_UNIT, vfs_context_ucred(ctx), (int *) 0, vfs_context_proc(ctx));
- kfree(pad_buf, pad_size);
+ kheap_free(KHEAP_TEMP, pad_buf, pad_size);
if (ret) {
goto write_error;
}
if (kd_ctrl_page.kdebug_flags & KDBG_MAPINIT) {
assert(kd_mapptr != NULL);
- kmem_free(kernel_map, (vm_offset_t)kd_mapptr, kd_mapsize);
+ kfree(kd_mapptr, kd_mapsize);
kd_mapptr = NULL;
kd_mapsize = 0;
kd_mapcount = 0;
kdbg_write_thread_map(vnode_t vp, vfs_context_t ctx)
{
int ret = 0;
- boolean_t map_initialized;
+ bool map_initialized;
ktrace_assert_lock_held();
assert(ctx != NULL);
static int
kdbg_copyout_thread_map(user_addr_t buffer, size_t *buffer_size)
{
- boolean_t map_initialized;
+ bool map_initialized;
size_t map_size;
int ret = 0;
kdbg_readthrmap_v3(user_addr_t buffer, size_t buffer_size, int fd)
{
int ret = 0;
- boolean_t map_initialized;
+ bool map_initialized;
size_t map_size;
ktrace_assert_lock_held();
* Only allow allocation up to half the available memory (sane_size).
*/
uint64_t max_nkdbufs = (sane_size / 2) / sizeof(kd_buf);
- nkdbufs = (req_nkdbufs > max_nkdbufs) ? max_nkdbufs : req_nkdbufs;
+ nkdbufs = (req_nkdbufs > max_nkdbufs) ? (unsigned int)max_nkdbufs :
+ req_nkdbufs;
}
/*
*
* Called with `ktrace_lock` locked and interrupts enabled.
*/
-static boolean_t
-kdbg_wait(uint64_t timeout_ms, boolean_t locked_wait)
+static bool
+kdbg_wait(uint64_t timeout_ms, bool locked_wait)
{
int wait_result = THREAD_AWAKENED;
uint64_t abstime = 0;
clock_absolutetime_interval_to_deadline(abstime, &abstime);
}
- boolean_t s = ml_set_interrupts_enabled(FALSE);
+ bool s = ml_set_interrupts_enabled(false);
if (!s) {
panic("kdbg_wait() called with interrupts disabled");
}
- lck_spin_lock_grp(kdw_spin_lock, kdebug_lck_grp);
+ lck_spin_lock_grp(&kdw_spin_lock, &kdebug_lck_grp);
if (!locked_wait) {
/* drop the mutex to allow others to access trace */
kds_waiter = 1;
if (abstime) {
- wait_result = lck_spin_sleep_deadline(kdw_spin_lock, 0, &kds_waiter, THREAD_ABORTSAFE, abstime);
+ wait_result = lck_spin_sleep_deadline(&kdw_spin_lock, 0, &kds_waiter, THREAD_ABORTSAFE, abstime);
} else {
- wait_result = lck_spin_sleep(kdw_spin_lock, 0, &kds_waiter, THREAD_ABORTSAFE);
+ wait_result = lck_spin_sleep(&kdw_spin_lock, 0, &kds_waiter, THREAD_ABORTSAFE);
}
kds_waiter = 0;
}
/* check the count under the spinlock */
- boolean_t threshold_exceeded = (kd_ctrl_page.kds_inuse_count >= n_storage_threshold);
+ bool threshold_exceeded = (kd_ctrl_page.kds_inuse_count >= n_storage_threshold);
- lck_spin_unlock(kdw_spin_lock);
+ lck_spin_unlock(&kdw_spin_lock);
ml_set_interrupts_enabled(s);
if (!locked_wait) {
static void
kdbg_wakeup(void)
{
- boolean_t need_kds_wakeup = FALSE;
+ bool need_kds_wakeup = false;
/*
* Try to take the lock here to synchronize with the waiter entering
* conditions. No problem if we fail, there will be lots of additional
* events coming in that will eventually succeed in grabbing this lock.
*/
- boolean_t s = ml_set_interrupts_enabled(FALSE);
+ bool s = ml_set_interrupts_enabled(false);
- if (lck_spin_try_lock(kdw_spin_lock)) {
+ if (lck_spin_try_lock(&kdw_spin_lock)) {
if (kds_waiter &&
(kd_ctrl_page.kds_inuse_count >= n_storage_threshold)) {
kds_waiter = 0;
- need_kds_wakeup = TRUE;
+ need_kds_wakeup = true;
}
- lck_spin_unlock(kdw_spin_lock);
+ lck_spin_unlock(&kdw_spin_lock);
}
ml_set_interrupts_enabled(s);
- if (need_kds_wakeup == TRUE) {
+ if (need_kds_wakeup == true) {
wakeup(&kds_waiter);
}
}
value = name[1];
}
- kdbg_lock_init();
- assert(kd_ctrl_page.kdebug_flags & KDBG_LOCKINIT);
-
ktrace_lock();
/*
memset(&kd_bufinfo, 0, sizeof(kd_bufinfo));
kd_bufinfo.nkdbufs = nkdbufs;
- kd_bufinfo.nkdthreads = kd_mapcount;
-
+ kd_bufinfo.nkdthreads = kd_mapcount < INT_MAX ? (int)kd_mapcount :
+ INT_MAX;
if ((kd_ctrl_page.kdebug_slowcheck & SLOW_NOLOG)) {
kd_bufinfo.nolog = 1;
} else {
}
kdbg_thrmap_init();
- kdbg_set_tracing_enabled(TRUE, value);
+ kdbg_set_tracing_enabled(true, value);
} else {
if (!kdebug_enable) {
break;
break;
case KERN_KDSETUP:
- ret = kdbg_reinit(FALSE);
+ ret = kdbg_reinit(false);
break;
case KERN_KDREMOVE:
int fd;
if (name[0] == KERN_KDWRITETR || name[0] == KERN_KDWRITETR_V3) {
- (void)kdbg_wait(size, TRUE);
+ (void)kdbg_wait(size, true);
}
p = current_proc();
fd = value;
- proc_fdlock(p);
- if ((ret = fp_lookup(p, fd, &fp, 1))) {
- proc_fdunlock(p);
- break;
- }
- context.vc_thread = current_thread();
- context.vc_ucred = fp->f_fglob->fg_cred;
-
- if (FILEGLOB_DTYPE(fp->f_fglob) != DTYPE_VNODE) {
- fp_drop(p, fd, fp, 1);
- proc_fdunlock(p);
+ if (fp_get_ftype(p, fd, DTYPE_VNODE, EBADF, &fp)) {
ret = EBADF;
break;
}
- vp = (struct vnode *)fp->f_fglob->fg_data;
- proc_fdunlock(p);
+
+ vp = fp->fp_glob->fg_data;
+ context.vc_thread = current_thread();
+ context.vc_ucred = fp->fp_glob->fg_cred;
if ((ret = vnode_getwithref(vp)) == 0) {
- RAW_file_offset = fp->f_fglob->fg_offset;
+ RAW_file_offset = fp->fp_glob->fg_offset;
if (name[0] == KERN_KDWRITETR || name[0] == KERN_KDWRITETR_V3) {
number = nkdbufs * sizeof(kd_buf);
ret = kdbg_write_thread_map(vp, &context);
}
}
- fp->f_fglob->fg_offset = RAW_file_offset;
+ fp->fp_glob->fg_offset = RAW_file_offset;
vnode_put(vp);
}
fp_drop(p, fd, fp, 0);
break;
}
case KERN_KDBUFWAIT:
- *sizep = kdbg_wait(size, FALSE);
+ *sizep = kdbg_wait(size, false);
break;
case KERN_KDPIDTR:
int
kdbg_read(user_addr_t buffer, size_t *number, vnode_t vp, vfs_context_t ctx, uint32_t file_version)
{
- unsigned int count;
+ size_t count;
unsigned int cpu, min_cpu;
uint64_t barrier_min = 0, barrier_max = 0, t, earliest_time;
int error = 0;
struct kd_storage *kdsp_actual;
struct kd_bufinfo *kdbp;
struct kd_bufinfo *min_kdbp;
- uint32_t tempbuf_count;
+ size_t tempbuf_count;
uint32_t tempbuf_number;
uint32_t old_kdebug_flags;
uint32_t old_kdebug_slowcheck;
- boolean_t out_of_events = FALSE;
- boolean_t wrapped = FALSE;
+ bool out_of_events = false;
+ bool wrapped = false;
- assert(number);
+ assert(number != NULL);
count = *number / sizeof(kd_buf);
*number = 0;
memset(&lostevent, 0, sizeof(lostevent));
lostevent.debugid = TRACE_LOST_EVENTS;
- /*
- * Capture the current time. Only sort events that have occured
- * before now. Since the IOPs are being flushed here, it is possible
- * that events occur on the AP while running live tracing. If we are
- * disabled, no new events should occur on the AP.
- */
- if (kd_ctrl_page.enabled) {
- barrier_max = kdbg_timestamp() & KDBG_TIMESTAMP_MASK;
- }
-
/*
* Request each IOP to provide us with up to date entries before merging
* buffers together.
*/
kdbg_iop_list_callback(kd_ctrl_page.kdebug_iops, KD_CALLBACK_SYNC_FLUSH, NULL);
+ /*
+ * Capture the current time. Only sort events that have occured
+ * before now. Since the IOPs are being flushed here, it is possible
+ * that events occur on the AP while running live tracing.
+ */
+ barrier_max = kdbg_timestamp() & KDBG_TIMESTAMP_MASK;
+
/*
* Disable wrap so storage units cannot be stolen out from underneath us
* while merging events.
continue;
}
kdsp_actual = POINTER_FROM_KDS_PTR(kdsp);
- kdsp_actual->kds_lostevents = FALSE;
+ kdsp_actual->kds_lostevents = false;
}
}
/*
*/
kdbg_set_timestamp_and_cpu(&lostevent, barrier_min, 0);
*tempbuf = lostevent;
- wrapped = FALSE;
+ wrapped = false;
goto nextevent;
}
*/
if (kdsp_actual->kds_lostevents) {
lostevents = true;
- kdsp_actual->kds_lostevents = FALSE;
+ kdsp_actual->kds_lostevents = false;
/*
* The earliest event we can trust is the first one in this
t = kdbg_get_timestamp(&kdsp_actual->kds_records[rcursor]);
- if ((t > barrier_max) && (barrier_max > 0)) {
+ if (t > barrier_max) {
if (kdbg_debug) {
printf("kdebug: FUTURE EVENT: debugid %#8x: "
"time %lld from CPU %u "
kdsp_actual->kds_records[rcursor].debugid,
t, cpu, barrier_max, *number + tempbuf_number);
}
- /*
- * Need to flush IOPs again before we can sort any more
- * data from the buffers.
- */
- out_of_events = TRUE;
- break;
+ goto next_cpu;
}
if (t < kdsp_actual->kds_timestamp) {
/*
* Bail out so we don't get out-of-order events by
* continuing to read events from other CPUs' events.
*/
- out_of_events = TRUE;
+ out_of_events = true;
break;
}
*/
if (t < barrier_min) {
kdsp_actual->kds_readlast++;
+ if (kdbg_debug) {
+ printf("kdebug: PAST EVENT: debugid %#8x: "
+ "time %lld from CPU %u "
+ "(barrier at time %lld)\n",
+ kdsp_actual->kds_records[rcursor].debugid,
+ t, cpu, barrier_min);
+ }
if (kdsp_actual->kds_readlast >= EVENTS_PER_STORAGE_UNIT) {
release_storage_unit(cpu, kdsp.raw);
}
if (min_kdbp == NULL) {
/* All buffers ran empty. */
- out_of_events = TRUE;
+ out_of_events = true;
}
if (out_of_events) {
break;
if (traced_retrograde) {
continue;
}
+ if (kdbg_debug) {
+ printf("kdebug: RETRO EVENT: debugid %#8x: "
+ "time %lld from CPU %u "
+ "(barrier at time %lld)\n",
+ kdsp_actual->kds_records[rcursor].debugid,
+ t, cpu, barrier_min);
+ }
kdbg_set_timestamp_and_cpu(tempbuf, min_kdbp->kd_prev_timebase, kdbg_get_cpu(tempbuf));
tempbuf->arg1 = tempbuf->debugid;
- tempbuf->arg2 = earliest_time;
+ tempbuf->arg2 = (kd_buf_argtype)earliest_time;
tempbuf->arg3 = 0;
tempbuf->arg4 = 0;
tempbuf->debugid = TRACE_RETROGRADE_EVENTS;
count -= tempbuf_number;
*number += tempbuf_number;
}
- if (out_of_events == TRUE) {
+ if (out_of_events == true) {
/*
* all trace buffers are empty
*/
return error;
}
+#define KDEBUG_TEST_CODE(code) BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, (code))
+
+/*
+ * A test IOP for the SYNC_FLUSH callback.
+ */
+
+static int sync_flush_iop = 0;
+
+static void
+sync_flush_callback(void * __unused context, kd_callback_type reason,
+ void * __unused arg)
+{
+ assert(sync_flush_iop > 0);
+
+ if (reason == KD_CALLBACK_SYNC_FLUSH) {
+ kernel_debug_enter(sync_flush_iop, KDEBUG_TEST_CODE(0xff),
+ kdbg_timestamp(), 0, 0, 0, 0, 0);
+ }
+}
+
+static struct kd_callback sync_flush_kdcb = {
+ .func = sync_flush_callback,
+ .iop_name = "test_sf",
+};
+
static int
kdbg_test(size_t flavor)
{
int code = 0;
int dummy_iop = 0;
-#define KDEBUG_TEST_CODE(code) BSDDBG_CODE(DBG_BSD_KDEBUG_TEST, (code))
switch (flavor) {
case 1:
/* try each macro */
/* ensure old timestamps are not emitted from kernel_debug_enter */
kernel_debug_enter(dummy_iop, KDEBUG_TEST_CODE(code),
- 100 /* very old timestamp */, 0, 0, 0,
- 0, (uintptr_t)thread_tid(current_thread()));
+ 100 /* very old timestamp */, 0, 0, 0, 0, 0);
code++;
kernel_debug_enter(dummy_iop, KDEBUG_TEST_CODE(code),
- kdbg_timestamp(), 0, 0, 0, 0,
- (uintptr_t)thread_tid(current_thread()));
+ kdbg_timestamp(), 0, 0, 0, 0, 0);
code++;
break;
+ case 3:
+ if (kd_ctrl_page.kdebug_iops) {
+ dummy_iop = kd_ctrl_page.kdebug_iops[0].cpu_id;
+ }
+ kernel_debug_enter(dummy_iop, KDEBUG_TEST_CODE(code),
+ kdbg_timestamp() * 2 /* !!! */, 0, 0, 0, 0, 0);
+ break;
+
+ case 4:
+ if (!sync_flush_iop) {
+ sync_flush_iop = kernel_debug_register_callback(
+ sync_flush_kdcb);
+ assert(sync_flush_iop > 0);
+ }
+ break;
+
default:
return ENOTSUP;
}
-#undef KDEBUG_TEST_CODE
return 0;
}
+#undef KDEBUG_TEST_CODE
+
void
-kdebug_init(unsigned int n_events, char *filter_desc, boolean_t wrapping)
+kdebug_init(unsigned int n_events, char *filter_desc, enum kdebug_opts opts)
{
assert(filter_desc != NULL);
-#if defined(__x86_64__)
- /* only trace MACH events when outputting kdebug to serial */
- if (kdebug_serial) {
- n_events = 1;
- if (filter_desc[0] == '\0') {
- filter_desc[0] = 'C';
- filter_desc[1] = '1';
- filter_desc[2] = '\0';
- }
- }
-#endif /* defined(__x86_64__) */
-
if (log_leaks && n_events == 0) {
n_events = 200000;
}
- kdebug_trace_start(n_events, filter_desc, wrapping, FALSE);
+ kdebug_trace_start(n_events, filter_desc, opts);
}
static void
if (filter_desc[0] >= '0' && filter_desc[0] <= '9') {
unsigned long csc = strtoul(filter_desc, NULL, 0);
if (filter_desc != end && csc <= KDBG_CSC_MAX) {
- typefilter_allow_csc(kdbg_typefilter, csc);
+ typefilter_allow_csc(kdbg_typefilter, (uint16_t)csc);
}
return;
}
char filter_type = filter_desc[0];
if (filter_type != 'C' && filter_type != 'S') {
+ printf("kdebug: unexpected filter type `%c'\n", filter_type);
return;
}
filter_desc++;
allow_value = strtoul(filter_desc, &end, 0);
if (filter_desc == end) {
- /* cannot parse as integer */
+ printf("kdebug: cannot parse `%s' as integer\n", filter_desc);
return;
}
switch (filter_type) {
case 'C':
- if (allow_value <= KDBG_CLASS_MAX) {
- typefilter_allow_class(kdbg_typefilter, allow_value);
- } else {
- /* illegal class */
+ if (allow_value > KDBG_CLASS_MAX) {
+ printf("kdebug: class 0x%lx is invalid\n", allow_value);
return;
}
+ printf("kdebug: C 0x%lx\n", allow_value);
+ typefilter_allow_class(kdbg_typefilter, (uint8_t)allow_value);
break;
case 'S':
- if (allow_value <= KDBG_CSC_MAX) {
- typefilter_allow_csc(kdbg_typefilter, allow_value);
- } else {
- /* illegal class subclass */
+ if (allow_value > KDBG_CSC_MAX) {
+ printf("kdebug: class-subclass 0x%lx is invalid\n", allow_value);
return;
}
+ printf("kdebug: S 0x%lx\n", allow_value);
+ typefilter_allow_csc(kdbg_typefilter, (uint16_t)allow_value);
break;
default:
- return;
+ __builtin_unreachable();
}
/* advance to next filter entry */
}
}
+uint64_t
+kdebug_wake(void)
+{
+ if (!wake_nkdbufs) {
+ return 0;
+ }
+ uint64_t start = mach_absolute_time();
+ kdebug_trace_start(wake_nkdbufs, NULL, trace_wrap ? KDOPT_WRAPPING : 0);
+ return mach_absolute_time() - start;
+}
+
/*
- * This function is meant to be called from the bootstrap thread or coming out
- * of acpi_idle_kernel.
+ * This function is meant to be called from the bootstrap thread or kdebug_wake.
*/
void
kdebug_trace_start(unsigned int n_events, const char *filter_desc,
- boolean_t wrapping, boolean_t at_wake)
+ enum kdebug_opts opts)
{
if (!n_events) {
kd_early_done = true;
ktrace_start_single_threaded();
- kdbg_lock_init();
-
ktrace_kernel_configure(KTRACE_KDEBUG);
kdbg_set_nkdbufs(n_events);
kernel_debug_string_early("start_kern_tracing");
- if (kdbg_reinit(TRUE)) {
+ if (kdbg_reinit((opts & KDOPT_ATBOOT))) {
printf("error from kdbg_reinit, kernel tracing not started\n");
goto out;
}
* Wrapping is disabled because boot and wake tracing is interested in
* the earliest events, at the expense of later ones.
*/
- if (!wrapping) {
+ if (!(opts & KDOPT_WRAPPING)) {
uint32_t old1, old2;
(void)disable_wrap(&old1, &old2);
}
* Hold off interrupts between getting a thread map and enabling trace
* and until the early traces are recorded.
*/
- boolean_t s = ml_set_interrupts_enabled(FALSE);
+ bool s = ml_set_interrupts_enabled(false);
- if (at_wake) {
+ if (!(opts & KDOPT_ATBOOT)) {
kdbg_thrmap_init();
}
- kdbg_set_tracing_enabled(TRUE, KDEBUG_ENABLE_TRACE | (kdebug_serial ?
- KDEBUG_ENABLE_SERIAL : 0));
+ kdbg_set_tracing_enabled(true, KDEBUG_ENABLE_TRACE);
- if (!at_wake) {
+ if ((opts & KDOPT_ATBOOT)) {
/*
* Transfer all very early events from the static buffer into the real
* buffers.
ml_set_interrupts_enabled(s);
- printf("kernel tracing started with %u events\n", n_events);
-
-#if KDEBUG_MOJO_TRACE
- if (kdebug_serial) {
- printf("serial output enabled with %lu named events\n",
- sizeof(kd_events) / sizeof(kd_event_t));
- }
-#endif /* KDEBUG_MOJO_TRACE */
+ printf("kernel tracing started with %u events, filter = %s\n", n_events,
+ filter_desc ?: "none");
out:
ktrace_end_single_threaded();
.debugid = TRACE_WRITING_EVENTS | DBG_FUNC_END,
.arg1 = write_size,
.arg2 = ret,
- .arg5 = thread_tid(current_thread()),
+ .arg5 = (kd_buf_argtype)thread_tid(current_thread()),
};
kdbg_set_timestamp_and_cpu(&end_event, kdbg_timestamp(),
cpu_number());
CTLTYPE_QUAD | CTLFLAG_RD | CTLFLAG_LOCKED,
&kd_ctrl_page.oldest_time,
"Find the oldest timestamp still in trace");
-
-#if KDEBUG_MOJO_TRACE
-static kd_event_t *
-binary_search(uint32_t id)
-{
- int low, high, mid;
-
- low = 0;
- high = (int)(sizeof(kd_events) / sizeof(kd_event_t)) - 1;
-
- while (TRUE) {
- mid = (low + high) / 2;
-
- if (low > high) {
- return NULL; /* failed */
- } else if (low + 1 >= high) {
- /* We have a match */
- if (kd_events[high].id == id) {
- return &kd_events[high];
- } else if (kd_events[low].id == id) {
- return &kd_events[low];
- } else {
- return NULL; /* search failed */
- }
- } else if (id < kd_events[mid].id) {
- high = mid;
- } else {
- low = mid;
- }
- }
-}
-
-/*
- * Look up event id to get name string.
- * Using a per-cpu cache of a single entry
- * before resorting to a binary search of the full table.
- */
-#define NCACHE 1
-static kd_event_t *last_hit[MAX_CPUS];
-static kd_event_t *
-event_lookup_cache(uint32_t cpu, uint32_t id)
-{
- if (last_hit[cpu] == NULL || last_hit[cpu]->id != id) {
- last_hit[cpu] = binary_search(id);
- }
- return last_hit[cpu];
-}
-
-static uint64_t kd_last_timstamp;
-
-static void
-kdebug_serial_print(
- uint32_t cpunum,
- uint32_t debugid,
- uint64_t timestamp,
- uintptr_t arg1,
- uintptr_t arg2,
- uintptr_t arg3,
- uintptr_t arg4,
- uintptr_t threadid
- )
-{
- char kprintf_line[192];
- char event[40];
- uint64_t us = timestamp / NSEC_PER_USEC;
- uint64_t us_tenth = (timestamp % NSEC_PER_USEC) / 100;
- uint64_t delta = timestamp - kd_last_timstamp;
- uint64_t delta_us = delta / NSEC_PER_USEC;
- uint64_t delta_us_tenth = (delta % NSEC_PER_USEC) / 100;
- uint32_t event_id = debugid & KDBG_EVENTID_MASK;
- const char *command;
- const char *bra;
- const char *ket;
- kd_event_t *ep;
-
- /* event time and delta from last */
- snprintf(kprintf_line, sizeof(kprintf_line),
- "%11llu.%1llu %8llu.%1llu ",
- us, us_tenth, delta_us, delta_us_tenth);
-
-
- /* event (id or name) - start prefixed by "[", end postfixed by "]" */
- bra = (debugid & DBG_FUNC_START) ? "[" : " ";
- ket = (debugid & DBG_FUNC_END) ? "]" : " ";
- ep = event_lookup_cache(cpunum, event_id);
- if (ep) {
- if (strlen(ep->name) < sizeof(event) - 3) {
- snprintf(event, sizeof(event), "%s%s%s",
- bra, ep->name, ket);
- } else {
- snprintf(event, sizeof(event), "%s%x(name too long)%s",
- bra, event_id, ket);
- }
- } else {
- snprintf(event, sizeof(event), "%s%x%s",
- bra, event_id, ket);
- }
- snprintf(kprintf_line + strlen(kprintf_line),
- sizeof(kprintf_line) - strlen(kprintf_line),
- "%-40s ", event);
-
- /* arg1 .. arg4 with special cases for strings */
- switch (event_id) {
- case VFS_LOOKUP:
- case VFS_LOOKUP_DONE:
- if (debugid & DBG_FUNC_START) {
- /* arg1 hex then arg2..arg4 chars */
- snprintf(kprintf_line + strlen(kprintf_line),
- sizeof(kprintf_line) - strlen(kprintf_line),
- "%-16lx %-8s%-8s%-8s ",
- arg1, (char*)&arg2, (char*)&arg3, (char*)&arg4);
- break;
- }
- /* else fall through for arg1..arg4 chars */
- case TRACE_STRING_EXEC:
- case TRACE_STRING_NEWTHREAD:
- case TRACE_INFO_STRING:
- snprintf(kprintf_line + strlen(kprintf_line),
- sizeof(kprintf_line) - strlen(kprintf_line),
- "%-8s%-8s%-8s%-8s ",
- (char*)&arg1, (char*)&arg2, (char*)&arg3, (char*)&arg4);
- break;
- default:
- snprintf(kprintf_line + strlen(kprintf_line),
- sizeof(kprintf_line) - strlen(kprintf_line),
- "%-16lx %-16lx %-16lx %-16lx",
- arg1, arg2, arg3, arg4);
- }
-
- /* threadid, cpu and command name */
- if (threadid == (uintptr_t)thread_tid(current_thread()) &&
- current_proc() &&
- current_proc()->p_comm[0]) {
- command = current_proc()->p_comm;
- } else {
- command = "-";
- }
- snprintf(kprintf_line + strlen(kprintf_line),
- sizeof(kprintf_line) - strlen(kprintf_line),
- " %-16lx %-2d %s\n",
- threadid, cpunum, command);
-
- kprintf("%s", kprintf_line);
- kd_last_timstamp = timestamp;
-}
-
-#endif