1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2007-2008 John Birrell <jb@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_kdb.h" 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/eventhandler.h> 37 #include <sys/kdb.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/proc.h> 41 #include <sys/dtrace_bsd.h> 42 #include <sys/sysctl.h> 43 #include <sys/sysent.h> 44 45 #define KDTRACE_PROC_SIZE 64 46 #define KDTRACE_THREAD_SIZE 256 47 48 FEATURE(kdtrace_hooks, 49 "Kernel DTrace hooks which are required to load DTrace kernel modules"); 50 51 static MALLOC_DEFINE(M_KDTRACE, "kdtrace", "DTrace hooks"); 52 53 /* Hooks used in the machine-dependent trap handlers. */ 54 dtrace_trap_func_t dtrace_trap_func; 55 dtrace_doubletrap_func_t dtrace_doubletrap_func; 56 dtrace_pid_probe_ptr_t dtrace_pid_probe_ptr; 57 dtrace_return_probe_ptr_t dtrace_return_probe_ptr; 58 59 systrace_probe_func_t __read_frequently systrace_probe_func; 60 61 /* Return the DTrace process data size compiled in the kernel hooks. */ 62 size_t 63 kdtrace_proc_size() 64 { 65 66 return (KDTRACE_PROC_SIZE); 67 } 68 69 static void 70 kdtrace_proc_ctor(void *arg __unused, struct proc *p) 71 { 72 73 p->p_dtrace = malloc(KDTRACE_PROC_SIZE, M_KDTRACE, M_WAITOK|M_ZERO); 74 } 75 76 static void 77 kdtrace_proc_dtor(void *arg __unused, struct proc *p) 78 { 79 80 if (p->p_dtrace != NULL) { 81 free(p->p_dtrace, M_KDTRACE); 82 p->p_dtrace = NULL; 83 } 84 } 85 86 /* Return the DTrace thread data size compiled in the kernel hooks. */ 87 size_t 88 kdtrace_thread_size() 89 { 90 91 return (KDTRACE_THREAD_SIZE); 92 } 93 94 static void 95 kdtrace_thread_ctor(void *arg __unused, struct thread *td) 96 { 97 98 td->td_dtrace = malloc(KDTRACE_THREAD_SIZE, M_KDTRACE, M_WAITOK|M_ZERO); 99 } 100 101 static void 102 kdtrace_thread_dtor(void *arg __unused, struct thread *td) 103 { 104 105 if (td->td_dtrace != NULL) { 106 free(td->td_dtrace, M_KDTRACE); 107 td->td_dtrace = NULL; 108 } 109 } 110 111 /* 112 * Initialise the kernel DTrace hooks. 113 */ 114 static void 115 init_dtrace(void *dummy __unused) 116 { 117 118 EVENTHANDLER_REGISTER(process_ctor, kdtrace_proc_ctor, NULL, 119 EVENTHANDLER_PRI_ANY); 120 EVENTHANDLER_REGISTER(process_dtor, kdtrace_proc_dtor, NULL, 121 EVENTHANDLER_PRI_ANY); 122 EVENTHANDLER_REGISTER(thread_ctor, kdtrace_thread_ctor, NULL, 123 EVENTHANDLER_PRI_ANY); 124 EVENTHANDLER_REGISTER(thread_dtor, kdtrace_thread_dtor, NULL, 125 EVENTHANDLER_PRI_ANY); 126 } 127 128 SYSINIT(kdtrace, SI_SUB_KDTRACE, SI_ORDER_FIRST, init_dtrace, NULL); 129