diff options
Diffstat (limited to 'samples')
-rw-r--r-- | samples/Kconfig | 6 | ||||
-rw-r--r-- | samples/Makefile | 2 | ||||
-rw-r--r-- | samples/trace_events/Makefile | 6 | ||||
-rw-r--r-- | samples/trace_events/trace-events-sample.c | 52 | ||||
-rw-r--r-- | samples/trace_events/trace-events-sample.h | 129 |
5 files changed, 194 insertions, 1 deletions
diff --git a/samples/Kconfig b/samples/Kconfig index 4b02f5a0e656..b75d28cba3f7 100644 --- a/samples/Kconfig +++ b/samples/Kconfig | |||
@@ -19,6 +19,12 @@ config SAMPLE_TRACEPOINTS | |||
19 | help | 19 | help |
20 | This build tracepoints example modules. | 20 | This build tracepoints example modules. |
21 | 21 | ||
22 | config SAMPLE_TRACE_EVENTS | ||
23 | tristate "Build trace_events examples -- loadable modules only" | ||
24 | depends on EVENT_TRACING && m | ||
25 | help | ||
26 | This build trace event example modules. | ||
27 | |||
22 | config SAMPLE_KOBJECT | 28 | config SAMPLE_KOBJECT |
23 | tristate "Build kobject examples" | 29 | tristate "Build kobject examples" |
24 | help | 30 | help |
diff --git a/samples/Makefile b/samples/Makefile index 10eaca89fe17..13e4b470b539 100644 --- a/samples/Makefile +++ b/samples/Makefile | |||
@@ -1,3 +1,3 @@ | |||
1 | # Makefile for Linux samples code | 1 | # Makefile for Linux samples code |
2 | 2 | ||
3 | obj-$(CONFIG_SAMPLES) += markers/ kobject/ kprobes/ tracepoints/ | 3 | obj-$(CONFIG_SAMPLES) += markers/ kobject/ kprobes/ tracepoints/ trace_events/ |
diff --git a/samples/trace_events/Makefile b/samples/trace_events/Makefile new file mode 100644 index 000000000000..0d428dc67283 --- /dev/null +++ b/samples/trace_events/Makefile | |||
@@ -0,0 +1,6 @@ | |||
1 | # builds the trace events example kernel modules; | ||
2 | # then to use one (as root): insmod <module_name.ko> | ||
3 | |||
4 | CFLAGS_trace-events-sample.o := -I$(src) | ||
5 | |||
6 | obj-$(CONFIG_SAMPLE_TRACE_EVENTS) += trace-events-sample.o | ||
diff --git a/samples/trace_events/trace-events-sample.c b/samples/trace_events/trace-events-sample.c new file mode 100644 index 000000000000..aabc4e970911 --- /dev/null +++ b/samples/trace_events/trace-events-sample.c | |||
@@ -0,0 +1,52 @@ | |||
1 | #include <linux/module.h> | ||
2 | #include <linux/kthread.h> | ||
3 | |||
4 | /* | ||
5 | * Any file that uses trace points, must include the header. | ||
6 | * But only one file, must include the header by defining | ||
7 | * CREATE_TRACE_POINTS first. This will make the C code that | ||
8 | * creates the handles for the trace points. | ||
9 | */ | ||
10 | #define CREATE_TRACE_POINTS | ||
11 | #include "trace-events-sample.h" | ||
12 | |||
13 | |||
14 | static void simple_thread_func(int cnt) | ||
15 | { | ||
16 | set_current_state(TASK_INTERRUPTIBLE); | ||
17 | schedule_timeout(HZ); | ||
18 | trace_foo_bar("hello", cnt); | ||
19 | } | ||
20 | |||
21 | static int simple_thread(void *arg) | ||
22 | { | ||
23 | int cnt = 0; | ||
24 | |||
25 | while (!kthread_should_stop()) | ||
26 | simple_thread_func(cnt++); | ||
27 | |||
28 | return 0; | ||
29 | } | ||
30 | |||
31 | static struct task_struct *simple_tsk; | ||
32 | |||
33 | static int __init trace_event_init(void) | ||
34 | { | ||
35 | simple_tsk = kthread_run(simple_thread, NULL, "event-sample"); | ||
36 | if (IS_ERR(simple_tsk)) | ||
37 | return -1; | ||
38 | |||
39 | return 0; | ||
40 | } | ||
41 | |||
42 | static void __exit trace_event_exit(void) | ||
43 | { | ||
44 | kthread_stop(simple_tsk); | ||
45 | } | ||
46 | |||
47 | module_init(trace_event_init); | ||
48 | module_exit(trace_event_exit); | ||
49 | |||
50 | MODULE_AUTHOR("Steven Rostedt"); | ||
51 | MODULE_DESCRIPTION("trace-events-sample"); | ||
52 | MODULE_LICENSE("GPL"); | ||
diff --git a/samples/trace_events/trace-events-sample.h b/samples/trace_events/trace-events-sample.h new file mode 100644 index 000000000000..128a897687c5 --- /dev/null +++ b/samples/trace_events/trace-events-sample.h | |||
@@ -0,0 +1,129 @@ | |||
1 | /* | ||
2 | * Notice that this file is not protected like a normal header. | ||
3 | * We also must allow for rereading of this file. The | ||
4 | * | ||
5 | * || defined(TRACE_HEADER_MULTI_READ) | ||
6 | * | ||
7 | * serves this purpose. | ||
8 | */ | ||
9 | #if !defined(_TRACE_EVENT_SAMPLE_H) || defined(TRACE_HEADER_MULTI_READ) | ||
10 | #define _TRACE_EVENT_SAMPLE_H | ||
11 | |||
12 | /* | ||
13 | * All trace headers should include tracepoint.h, until we finally | ||
14 | * make it into a standard header. | ||
15 | */ | ||
16 | #include <linux/tracepoint.h> | ||
17 | |||
18 | /* | ||
19 | * If TRACE_SYSTEM is defined, that will be the directory created | ||
20 | * in the ftrace directory under /debugfs/tracing/events/<system> | ||
21 | * | ||
22 | * The define_trace.h belowe will also look for a file name of | ||
23 | * TRACE_SYSTEM.h where TRACE_SYSTEM is what is defined here. | ||
24 | * | ||
25 | * If you want a different system than file name, you can override | ||
26 | * the header name by defining TRACE_INCLUDE_FILE | ||
27 | * | ||
28 | * If this file was called, goofy.h, then we would define: | ||
29 | * | ||
30 | * #define TRACE_INCLUDE_FILE goofy | ||
31 | * | ||
32 | */ | ||
33 | #undef TRACE_SYSTEM | ||
34 | #define TRACE_SYSTEM sample | ||
35 | |||
36 | /* | ||
37 | * The TRACE_EVENT macro is broken up into 5 parts. | ||
38 | * | ||
39 | * name: name of the trace point. This is also how to enable the tracepoint. | ||
40 | * A function called trace_foo_bar() will be created. | ||
41 | * | ||
42 | * proto: the prototype of the function trace_foo_bar() | ||
43 | * Here it is trace_foo_bar(char *foo, int bar). | ||
44 | * | ||
45 | * args: must match the arguments in the prototype. | ||
46 | * Here it is simply "foo, bar". | ||
47 | * | ||
48 | * struct: This defines the way the data will be stored in the ring buffer. | ||
49 | * There are currently two types of elements. __field and __array. | ||
50 | * a __field is broken up into (type, name). Where type can be any | ||
51 | * type but an array. | ||
52 | * For an array. there are three fields. (type, name, size). The | ||
53 | * type of elements in the array, the name of the field and the size | ||
54 | * of the array. | ||
55 | * | ||
56 | * __array( char, foo, 10) is the same as saying char foo[10]. | ||
57 | * | ||
58 | * fast_assign: This is a C like function that is used to store the items | ||
59 | * into the ring buffer. | ||
60 | * | ||
61 | * printk: This is a way to print out the data in pretty print. This is | ||
62 | * useful if the system crashes and you are logging via a serial line, | ||
63 | * the data can be printed to the console using this "printk" method. | ||
64 | * | ||
65 | * Note, that for both the assign and the printk, __entry is the handler | ||
66 | * to the data structure in the ring buffer, and is defined by the | ||
67 | * TP_STRUCT__entry. | ||
68 | */ | ||
69 | TRACE_EVENT(foo_bar, | ||
70 | |||
71 | TP_PROTO(char *foo, int bar), | ||
72 | |||
73 | TP_ARGS(foo, bar), | ||
74 | |||
75 | TP_STRUCT__entry( | ||
76 | __array( char, foo, 10 ) | ||
77 | __field( int, bar ) | ||
78 | ), | ||
79 | |||
80 | TP_fast_assign( | ||
81 | strncpy(__entry->foo, foo, 10); | ||
82 | __entry->bar = bar; | ||
83 | ), | ||
84 | |||
85 | TP_printk("foo %s %d", __entry->foo, __entry->bar) | ||
86 | ); | ||
87 | #endif | ||
88 | |||
89 | /***** NOTICE! The #if protection ends here. *****/ | ||
90 | |||
91 | |||
92 | /* | ||
93 | * There are several ways I could have done this. If I left out the | ||
94 | * TRACE_INCLUDE_PATH, then it would default to the kernel source | ||
95 | * include/trace/events directory. | ||
96 | * | ||
97 | * I could specify a path from the define_trace.h file back to this | ||
98 | * file. | ||
99 | * | ||
100 | * #define TRACE_INCLUDE_PATH ../../samples/trace_events | ||
101 | * | ||
102 | * But I chose to simply make it use the current directory and then in | ||
103 | * the Makefile I added: | ||
104 | * | ||
105 | * CFLAGS_trace-events-sample.o := -I$(PWD)/samples/trace_events/ | ||
106 | * | ||
107 | * This will make sure the current path is part of the include | ||
108 | * structure for our file so that we can find it. | ||
109 | * | ||
110 | * I could have made only the top level directory the include: | ||
111 | * | ||
112 | * CFLAGS_trace-events-sample.o := -I$(PWD) | ||
113 | * | ||
114 | * And then let the path to this directory be the TRACE_INCLUDE_PATH: | ||
115 | * | ||
116 | * #define TRACE_INCLUDE_PATH samples/trace_events | ||
117 | * | ||
118 | * But then if something defines "samples" or "trace_events" then we | ||
119 | * could risk that being converted too, and give us an unexpected | ||
120 | * result. | ||
121 | */ | ||
122 | #undef TRACE_INCLUDE_PATH | ||
123 | #undef TRACE_INCLUDE_FILE | ||
124 | #define TRACE_INCLUDE_PATH . | ||
125 | /* | ||
126 | * TRACE_INCLUDE_FILE is not needed if the filename and TRACE_SYSTEM are equal | ||
127 | */ | ||
128 | #define TRACE_INCLUDE_FILE trace-events-sample | ||
129 | #include <trace/define_trace.h> | ||