kern_dtrace.c revision 211613
1173143Srwatson/*- 2159248Srwatson * Copyright (c) 2007-2008 John Birrell <jb@FreeBSD.org> 3159248Srwatson * All rights reserved. 4159248Srwatson * 5173143Srwatson * Redistribution and use in source and binary forms, with or without 6159248Srwatson * modification, are permitted provided that the following conditions 7159248Srwatson * are met: 8159248Srwatson * 1. Redistributions of source code must retain the above copyright 9159248Srwatson * notice, this list of conditions and the following disclaimer. 10159248Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11159248Srwatson * notice, this list of conditions and the following disclaimer in the 12159248Srwatson * documentation and/or other materials provided with the distribution. 13159248Srwatson * 14159248Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15159248Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16159248Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17159248Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18185573Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19159248Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20159248Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21159248Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22159248Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23159248Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24159248Srwatson * SUCH DAMAGE. 25159248Srwatson */ 26159248Srwatson 27159248Srwatson#include <sys/cdefs.h> 28159248Srwatson__FBSDID("$FreeBSD: head/sys/kern/kern_dtrace.c 211613 2010-08-22 11:08:18Z rpaulo $"); 29159248Srwatson 30159248Srwatson#include "opt_kdb.h" 31159248Srwatson 32159248Srwatson#include <sys/param.h> 33159248Srwatson#include <sys/systm.h> 34159248Srwatson#include <sys/eventhandler.h> 35159248Srwatson#include <sys/kdb.h> 36159248Srwatson#include <sys/kernel.h> 37159248Srwatson#include <sys/malloc.h> 38159248Srwatson#include <sys/proc.h> 39159248Srwatson#include <sys/dtrace_bsd.h> 40159248Srwatson 41159248Srwatson#define KDTRACE_PROC_SIZE 64 42159248Srwatson#define KDTRACE_PROC_ZERO 8 43159248Srwatson#define KDTRACE_THREAD_SIZE 256 44159248Srwatson#define KDTRACE_THREAD_ZERO 64 45159248Srwatson 46159248SrwatsonMALLOC_DEFINE(M_KDTRACE, "kdtrace", "DTrace hooks"); 47159248Srwatson 48159248Srwatson/* Return the DTrace process data size compiled in the kernel hooks. */ 49159248Srwatsonsize_t 50159248Srwatsonkdtrace_proc_size() 51159248Srwatson{ 52159248Srwatson 53159248Srwatson return (KDTRACE_PROC_SIZE); 54159248Srwatson} 55173143Srwatson 56159248Srwatsonstatic void 57159248Srwatsonkdtrace_proc_ctor(void *arg __unused, struct proc *p) 58159248Srwatson{ 59159248Srwatson p->p_dtrace = malloc(KDTRACE_PROC_SIZE, M_KDTRACE, M_WAITOK); 60173143Srwatson 61173143Srwatson bzero(p->p_dtrace, KDTRACE_PROC_ZERO); 62173143Srwatson} 63159248Srwatson 64173143Srwatsonstatic void 65173143Srwatsonkdtrace_proc_dtor(void *arg __unused, struct proc *p) 66173143Srwatson{ 67159248Srwatson 68159248Srwatson if (p->p_dtrace != NULL) { 69159248Srwatson free(p->p_dtrace, M_KDTRACE); 70159248Srwatson p->p_dtrace = NULL; 71159248Srwatson } 72159248Srwatson} 73159248Srwatson 74159248Srwatson/* Return the DTrace thread data size compiled in the kernel hooks. */ 75159248Srwatsonsize_t 76159248Srwatsonkdtrace_thread_size() 77159248Srwatson{ 78159248Srwatson return (KDTRACE_THREAD_SIZE); 79159248Srwatson} 80159248Srwatson 81159248Srwatsonstatic void 82159248Srwatsonkdtrace_thread_ctor(void *arg __unused, struct thread *td) 83159248Srwatson{ 84159248Srwatson td->td_dtrace = malloc(KDTRACE_THREAD_SIZE, M_KDTRACE, M_WAITOK); 85159248Srwatson 86159248Srwatson bzero(td->td_dtrace, KDTRACE_THREAD_ZERO); 87159248Srwatson} 88159248Srwatson 89159248Srwatsonstatic void 90159248Srwatsonkdtrace_thread_dtor(void *arg __unused, struct thread *td) 91159248Srwatson{ 92159248Srwatson 93159248Srwatson if (td->td_dtrace != NULL) { 94159248Srwatson free(td->td_dtrace, M_KDTRACE); 95159248Srwatson td->td_dtrace = NULL; 96159248Srwatson } 97159248Srwatson} 98159248Srwatson 99173143Srwatson/* 100173143Srwatson * Initialise the kernel DTrace hooks. 101159248Srwatson */ 102159248Srwatsonstatic void 103159248Srwatsoninit_dtrace(void *dummy __unused) 104159248Srwatson{ 105159248Srwatson 106159248Srwatson EVENTHANDLER_REGISTER(process_ctor, kdtrace_proc_ctor, NULL, 107159248Srwatson EVENTHANDLER_PRI_ANY); 108159248Srwatson EVENTHANDLER_REGISTER(process_dtor, kdtrace_proc_dtor, NULL, 109159248Srwatson EVENTHANDLER_PRI_ANY); 110159248Srwatson EVENTHANDLER_REGISTER(thread_ctor, kdtrace_thread_ctor, NULL, 111159248Srwatson EVENTHANDLER_PRI_ANY); 112159248Srwatson EVENTHANDLER_REGISTER(thread_dtor, kdtrace_thread_dtor, NULL, 113185573Srwatson EVENTHANDLER_PRI_ANY); 114173143Srwatson} 115159248Srwatson 116159248SrwatsonSYSINIT(kdtrace, SI_SUB_KDTRACE, SI_ORDER_FIRST, init_dtrace, NULL); 117159248Srwatson