1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 bool __read_frequently systrace_enabled; 60 systrace_probe_func_t systrace_probe_func; 61 62 /* Return the DTrace process data size compiled in the kernel hooks. */ 63 size_t 64 kdtrace_proc_size(void) 65 { 66 67 return (KDTRACE_PROC_SIZE); 68 } 69 70 void 71 kdtrace_proc_ctor(struct proc *p) 72 { 73 74 p->p_dtrace = malloc(KDTRACE_PROC_SIZE, M_KDTRACE, M_WAITOK|M_ZERO); 75 } 76 77 void 78 kdtrace_proc_dtor(struct proc *p) 79 { 80 81 free(p->p_dtrace, M_KDTRACE); 82 p->p_dtrace = NULL; 83 } 84 85 /* Return the DTrace thread data size compiled in the kernel hooks. */ 86 size_t 87 kdtrace_thread_size(void) 88 { 89 90 return (KDTRACE_THREAD_SIZE); 91 } 92 93 void 94 kdtrace_thread_ctor(struct thread *td) 95 { 96 97 td->td_dtrace = malloc(KDTRACE_THREAD_SIZE, M_KDTRACE, M_WAITOK|M_ZERO); 98 } 99 100 void 101 kdtrace_thread_dtor(struct thread *td) 102 { 103 104 free(td->td_dtrace, M_KDTRACE); 105 td->td_dtrace = NULL; 106 } 107