From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25598 invoked by alias); 3 Mar 2008 23:52:37 -0000 Received: (qmail 25591 invoked by uid 22791); 3 Mar 2008 23:52:36 -0000 X-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL,BAYES_00,SPF_PASS X-Spam-Check-By: sourceware.org Received: from e5.ny.us.ibm.com (HELO e5.ny.us.ibm.com) (32.97.182.145) by sourceware.org (qpsmtpd/0.31) with ESMTP; Mon, 03 Mar 2008 23:52:12 +0000 Received: from d01relay02.pok.ibm.com (d01relay02.pok.ibm.com [9.56.227.234]) by e5.ny.us.ibm.com (8.13.8/8.13.8) with ESMTP id m23NqAr7025132 for ; Mon, 3 Mar 2008 18:52:10 -0500 Received: from d01av04.pok.ibm.com (d01av04.pok.ibm.com [9.56.224.64]) by d01relay02.pok.ibm.com (8.13.8/8.13.8/NCO v8.7) with ESMTP id m23NqAVV240616 for ; Mon, 3 Mar 2008 18:52:10 -0500 Received: from d01av04.pok.ibm.com (loopback [127.0.0.1]) by d01av04.pok.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id m23NqAhE011743 for ; Mon, 3 Mar 2008 18:52:10 -0500 Received: from sig-9-49-136-136.mts.ibm.com (sig-9-49-136-136.mts.ibm.com [9.49.136.136]) by d01av04.pok.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id m23Nq9IX011689; Mon, 3 Mar 2008 18:52:09 -0500 Subject: [patch 3/3] Trace sample From: "David J. Wilder" To: linux-kernel@vger.kernel.org, akpm@linux-foundation.org Cc: systemtap@sourceware.org Content-Type: text/plain Date: Mon, 03 Mar 2008 23:52:00 -0000 Message-Id: <1204588328.32186.9.camel@lc4eb748232119.ibm.com> Mime-Version: 1.0 X-Mailer: Evolution 2.0.2 (2.0.2-35.0.4.el4) Content-Transfer-Encoding: 7bit Mailing-List: contact systemtap-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: systemtap-owner@sourceware.org X-SW-Source: 2008-q1/txt/msg00356.txt.bz2 Trace example - Adds the trace example to samples/ Signed-off-by: David Wilder --- samples/Kconfig | 6 ++ samples/Makefile | 2 +- samples/trace/Makefile | 4 + samples/trace/fork_trace.c | 132 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 1 deletions(-) diff --git a/samples/Kconfig b/samples/Kconfig index 74d97cc..980c574 100644 --- a/samples/Kconfig +++ b/samples/Kconfig @@ -22,5 +22,11 @@ config SAMPLE_KOBJECT If in doubt, say "N" here. +config SAMPLE_TRACE + tristate "Build trace example -- loadable modules only" + depends on TRACE && KPROBES && m + help + This builds a trace example module. + endif # SAMPLES diff --git a/samples/Makefile b/samples/Makefile index 8652d0f..47db118 100644 --- a/samples/Makefile +++ b/samples/Makefile @@ -1,3 +1,3 @@ # Makefile for Linux samples code -obj-$(CONFIG_SAMPLES) += markers/ kobject/ +obj-$(CONFIG_SAMPLES) += markers/ kobject/ trace/ diff --git a/samples/trace/Makefile b/samples/trace/Makefile new file mode 100644 index 0000000..e33f276 --- /dev/null +++ b/samples/trace/Makefile @@ -0,0 +1,4 @@ +# builds the trace example kernel modules; +# then to use (as root): insmod + +obj-$(CONFIG_SAMPLE_TRACE) := fork_trace.o diff --git a/samples/trace/fork_trace.c b/samples/trace/fork_trace.c new file mode 100644 index 0000000..71c04c7 --- /dev/null +++ b/samples/trace/fork_trace.c @@ -0,0 +1,132 @@ +/* + * An example of using trace in a kprobes module + * + * Copyright (C) 2007 IBM Inc. + * + * David Wilder + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * ------- + * This module creates a trace channel and places a kprobe + * on the function do_fork(). The value of current->pid is written to + * the trace channel each time the kprobe is hit.. + * + * How to run the example: + * $ mount -t debugfs /debug + * $ insmod fork_trace.ko + * + * To view the data produced by the module: + * $ cat /debug/trace_example/do_fork/trace0 + * + */ + +#include +#include +#include +#include + +#define USE_GLOBAL_BUFFERS 1 +#define USE_FLIGHT 1 + +#define PROBE_POINT "do_fork" + +static struct kprobe kp; +static struct trace_info *kprobes_trace; + +#ifdef USE_GLOBAL_BUFFERS +static DEFINE_SPINLOCK(trace_lock); +#endif + +/* + * Send formatted trace data to trace channel. + * @note Preemption must be disabled to use this. + */ +static void trace_printf(struct trace_info *trace, const char *format, ...) +{ + va_list ap, aq; + char *record; + unsigned long flags; + int len; + + if (!trace) + return; + +#ifdef USE_GLOBAL_BUFFERS + spin_lock_irqsave(&trace_lock, flags); +#endif + if (trace_running(trace)) { + va_start(ap, format); + va_copy(aq, ap); + len = vsnprintf(NULL, 0, format, aq); + va_end(aq); + record = relay_reserve(trace->rchan, ++len); + if (record) + vsnprintf(record, len, format, ap); + va_end(ap); + } +#ifdef USE_GLOBAL_BUFFERS + spin_unlock_irqrestore(&trace_lock, flags); +#endif +} + +static int handler_pre(struct kprobe *p, struct pt_regs *regs) +{ + rcu_read_lock(); + trace_printf(kprobes_trace, "%d\n", current->pid); + rcu_read_unlock(); + return 0; +} + +int init_module(void) +{ + int ret; + u32 flags = 0; + +#ifdef USE_GLOBAL_BUFFERS + flags |= TRACE_GLOBAL_CHANNEL; +#endif + +#ifdef USE_FLIGHT + flags |= TRACE_FLIGHT_CHANNEL; +#endif + + /* setup the trace */ + kprobes_trace = trace_setup("trace_example", PROBE_POINT, + 1024, 8, flags); + if (IS_ERR(kprobes_trace)) + return PTR_ERR(kprobes_trace); + + trace_start(kprobes_trace); + + /* setup the kprobe */ + kp.pre_handler = handler_pre; + kp.post_handler = NULL; + kp.fault_handler = NULL; + kp.symbol_name = PROBE_POINT; + ret = register_kprobe(&kp); + if (ret) { + printk(KERN_ERR "fork_trace: register_kprobe failed\n"); + return ret; + } + return 0; +} + +void cleanup_module(void) +{ + unregister_kprobe(&kp); + trace_stop(kprobes_trace); + trace_cleanup(kprobes_trace); +} +MODULE_LICENSE("GPL");