Skip to contentSkip to conversation

Sample module for Kernel access to Ftrace instances.

RFC In review Divya Indi 2 participants 4 messages Sep 21, 2019 – Oct 15, 2019

lore
Share & tools
lore

Divya Indi

author
#0 lore raw

[PATCH] tracing: Sample module to demonstrate kernel access to Ftrace

Hi,

This patch is for a sample module to demonstrate the use of APIs that were introduced/exported in order to access Ftrace instances from within the kernel.

Please Note: This module is dependent on -
- commit: f45d122 tracing: Kernel access to Ftrace instances
- Patches pending review: https://lore.kernel.org/lkml/[email protected]/

The sample module creates/lookup a trace array called sample-instance on module load time.
We then start a kernel thread(simple-thread) to -
1) Enable tracing for event "sample_event" to buffer associated with the trace array - "sample-instance".
2) Start a timer that will disable tracing to this buffer after 5 sec. (Tracing disabled after 5 sec ie at count=4)
3) Write to the buffer using trace_array_printk()
4) Stop the kernel thread and destroy the buffer during module unload.

A sample output for the same -

# tracer: nop
#
# entries-in-buffer/entries-written: 16/16 #P:4
#
# _-----=> irqs-off
# / _----=> need-resched

#                            | / _---=> hardirq/softirq
#                            || / _--=> preempt-depth
#                            ||| /     delay
#           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
#              | |       |   ||||       |         |

sample-instance-26797 [003] .... 955180.489833: simple_thread: trace_array_printk: count=0 sample-instance-26797 [003] .... 955180.489836: sample_event: count value=0 at jiffies=5249940864 sample-instance-26797 [003] .... 955181.513722: simple_thread: trace_array_printk: count=1 sample-instance-26797 [003] .... 955181.513724: sample_event: count value=1 at jiffies=5249941888 sample-instance-26797 [003] .... 955182.537629: simple_thread: trace_array_printk: count=2 sample-instance-26797 [003] .... 955182.537631: sample_event: count value=2 at jiffies=5249942912 sample-instance-26797 [003] .... 955183.561516: simple_thread: trace_array_printk: count=3 sample-instance-26797 [003] .... 955183.561518: sample_event: count value=3 at jiffies=5249943936 sample-instance-26797 [003] .... 955184.585423: simple_thread: trace_array_printk: count=4 sample-instance-26797 [003] .... 955184.585427: sample_event: count value=4 at jiffies=5249944960 sample-instance-26797 [003] .... 955185.609344: simple_thread: trace_array_printk: count=5 sample-instance-26797 [003] .... 955186.633241: simple_thread: trace_array_printk: count=6 sample-instance-26797 [003] .... 955187.657157: simple_thread: trace_array_printk: count=7 sample-instance-26797 [003] .... 955188.681039: simple_thread: trace_array_printk: count=8 sample-instance-26797 [003] .... 955189.704937: simple_thread: trace_array_printk: count=9 sample-instance-26797 [003] .... 955190.728840: simple_thread: trace_array_printk: count=10

Let me know if you have any questions.

Thanks,
Divya

author
#1 lore raw

PATCH 1/1 tracing: Sample module to demonstrate kernel access to Ftrace instances.

This is a sample module to demostrate the use of the newly introduced and exported APIs to access Ftrace instances from within the kernel.

Newly introduced APIs used here -

1. Create a new trace array if it does not exist. struct trace_array *trace_array_create(const char *name)

2. Destroy/Remove a trace array.
int trace_array_destroy(struct trace_array *tr)

3. Lookup a trace array, given its name.
struct trace_array *trace_array_lookup(const char *name)

4. Enable/Disable trace events:
int trace_array_set_clr_event(struct trace_array *tr, const char *system,

        const char *event, int set);

Exported APIs -
1. trace_printk equivalent for instances.
int trace_array_printk(struct trace_array *tr,

               unsigned long ip, const char *fmt, ...);

2. Helper function.
void trace_printk_init_buffers(void);

3. To decrement the reference counter.
void trace_array_put(struct trace_array *tr)

---
 samples/Kconfig                              |   7 ++
 samples/Makefile                             |   1 +
 samples/ftrace_instance/Makefile             |   6 ++
 samples/ftrace_instance/sample-trace-array.c | 134 +++++++++++++++++++++++++++
 samples/ftrace_instance/sample-trace-array.h |  84 +++++++++++++++++
 5 files changed, 232 insertions(+)
 create mode 100644 samples/ftrace_instance/Makefile
 create mode 100644 samples/ftrace_instance/sample-trace-array.c
 create mode 100644 samples/ftrace_instance/sample-trace-array.h
samples/Kconfig +7 -0
index d63cc8a..1c7864b 100644
@@ -20,6 +20,13 @@ config SAMPLE_TRACE_PRINTK
 	 This builds a module that calls trace_printk() and can be used to
 	 test various trace_printk() calls from a module.
 
+config SAMPLE_TRACE_ARRAY
+        tristate "Build sample module for kernel access to Ftrace instancess"
+	depends on EVENT_TRACING && m
+	help
+	 This builds a module that demonstrates the use of various APIs to
+	 access Ftrace instances from within the kernel.
+
 config SAMPLE_KOBJECT
 	tristate "Build kobject examples"
 	help
samples/Makefile +1 -0
index debf892..02c444e 100644
@@ -17,6 +17,7 @@ obj-$(CONFIG_SAMPLE_RPMSG_CLIENT)	+= rpmsg/
 subdir-$(CONFIG_SAMPLE_SECCOMP)		+= seccomp
 obj-$(CONFIG_SAMPLE_TRACE_EVENTS)	+= trace_events/
 obj-$(CONFIG_SAMPLE_TRACE_PRINTK)	+= trace_printk/
+obj-$(CONFIG_SAMPLE_TRACE_ARRAY)	+= ftrace_instance/
 obj-$(CONFIG_VIDEO_PCI_SKELETON)	+= v4l/
 obj-y					+= vfio-mdev/
 subdir-$(CONFIG_SAMPLE_VFS)		+= vfs
samples/ftrace_instance/Makefile new file +6 -0
new file mode 100644
index 0000000..3603b13
@@ -0,0 +1,6 @@
+# Builds a module that calls various routines to access Ftrace instances.
+# To use(as root):  insmod sample-trace-array.ko
+
+CFLAGS_sample-trace-array.o := -I$(src)
+
+obj-$(CONFIG_SAMPLE_TRACE_ARRAY) += sample-trace-array.o
samples/ftrace_instance/sample-trace-array.c new file +134 -0

134 lines not on this page · raw message

samples/ftrace_instance/sample-trace-array.h new file +84 -0

84 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH] tracing: Sample module to demonstrate kernel access to Ftrace instances.

Quoted 32 lines · On Fri, 20 Sep 2019 16:59:26 -0700 Divya Indi <[email protected]> wrote:

This is a sample module to demostrate the use of the newly introduced and exported APIs to access Ftrace instances from within the kernel.

Newly introduced APIs used here -

1. Create a new trace array if it does not exist. struct trace_array *trace_array_create(const char *name)

2. Destroy/Remove a trace array.
int trace_array_destroy(struct trace_array *tr)

3. Lookup a trace array, given its name.
struct trace_array *trace_array_lookup(const char *name)

4. Enable/Disable trace events:
int trace_array_set_clr_event(struct trace_array *tr, const char *system,

        const char *event, int set);

Exported APIs -
1. trace_printk equivalent for instances.
int trace_array_printk(struct trace_array *tr,

               unsigned long ip, const char *fmt, ...);

2. Helper function.
void trace_printk_init_buffers(void);

3. To decrement the reference counter.
void trace_array_put(struct trace_array *tr)

Quoted 11 lines
---
 samples/Kconfig                              |   7 ++
 samples/Makefile                             |   1 +
 samples/ftrace_instance/Makefile             |   6 ++
 samples/ftrace_instance/sample-trace-array.c | 134 +++++++++++++++++++++++++++
 samples/ftrace_instance/sample-trace-array.h |  84 +++++++++++++++++
 5 files changed, 232 insertions(+)
 create mode 100644 samples/ftrace_instance/Makefile
 create mode 100644 samples/ftrace_instance/sample-trace-array.c
 create mode 100644 samples/ftrace_instance/sample-trace-array.h

I applied this patch but get this:

/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘mytimer_handler’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:35:2: error: implicit declaration of function ‘trace_array_set_clr_event’; did you mean ‘trace_set_clr_event’? [-Werror=implicit-function-declaration]
trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", 0);
^~~~~~~~~~~~~~~~~~~~~~~~~
trace_set_clr_event
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘simple_thread_func’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:47:2: error: implicit declaration of function ‘trace_array_printk’; did you mean ‘trace_seq_printf’? [-Werror=implicit-function-declaration]
trace_array_printk(tr, _THIS_IP_, "trace_array_printk: count=%d\n",
^~~~~~~~~~~~~~~~~~
trace_seq_printf
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘simple_thread’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:85:2: error: implicit declaration of function ‘trace_array_put’; did you mean ‘trace_seq_putc’? [-Werror=implicit-function-declaration]
trace_array_put(tr);
^~~~~~~~~~~~~~~
trace_seq_putc
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘sample_trace_array_init’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:100:7: error: implicit declaration of function ‘trace_array_lookup’; did you mean ‘radix_tree_lookup’? [-Werror=implicit-function-declaration]

  tr = trace_array_lookup("sample-instance");
       ^~~~~~~~~~~~~~~~~~
       radix_tree_lookup
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:100:5: warning: assignment to ‘struct trace_array *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
  tr = trace_array_lookup("sample-instance");
     ^
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:102:8: error: implicit declaration of function ‘trace_array_create’; did you mean ‘ftrace_force_update’? [-Werror=implicit-function-declaration]
   tr = trace_array_create("sample-instance");
        ^~~~~~~~~~~~~~~~~~
        ftrace_force_update
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:102:6: warning: assignment to ‘struct trace_array *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   tr = trace_array_create("sample-instance");
      ^

/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:110:2: error: implicit declaration of function ‘trace_printk_init_buffers’; did you mean ‘trace_event_get_offsets’? [-Werror=implicit-function-declaration]
trace_printk_init_buffers();
^~~~~~~~~~~~~~~~~~~~~~~~~
trace_event_get_offsets
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘sample_trace_array_exit’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:126:2: error: implicit declaration of function ‘trace_array_destroy’; did you mean ‘assoc_array_destroy’? [-Werror=implicit-function-declaration]
trace_array_destroy(tr);
^~~~~~~~~~~~~~~~~~~
assoc_array_destroy
cc1: some warnings being treated as errors
make[3]: *** [/work/git/linux-trace.git/scripts/Makefile.build:266: samples/ftrace_instance/sample-trace-array.o] Error 1
make[2]: *** [/work/git/linux-trace.git/scripts/Makefile.build:509: samples/ftrace_instance] Error 2
make[1]: *** [/work/git/linux-trace.git/Makefile:1650: samples] Error 2
make[1]: *** Waiting for unfinished jobs....

-- Steve

author
#3 lore raw

Re: [PATCH] tracing: Sample module to demonstrate kernel access to Ftrace instances.

[Apologies for multiple resends, formatting issue]

Hi Steven,

Thanks for taking a look.

This sample module is dependent on -

- commit: f45d122 tracing: Kernel access to Ftrace instances
- Patches pending review: https://lore.kernel.org/lkml/[email protected]/

I mentioned it in the cover page, but will add to this patch as well to avoid confusion.

So, I think the messages you are seeing is due to the absence of

Patches pending review: https://lore.kernel.org/lkml/[email protected]/

Let me know if you prefer to have the sample module and this patch-set (pending review) as part of one patch set.

For reference, adding the cover page details here -

------------------------------------------------------------------------------------------------------------------
This patch is for a sample module to demonstrate the use of APIs that were introduced/exported in order to access Ftrace instances from within the kernel.

Please Note: This module is dependent on -
- commit: f45d122 tracing: Kernel access to Ftrace instances
- Patches pending review: https://lore.kernel.org/lkml/[email protected]/

The sample module creates/lookup a trace array called sample-instance on module load time.
We then start a kernel thread(simple-thread) to -
1) Enable tracing for event "sample_event" to buffer associated with the trace array - "sample-instance".
2) Start a timer that will disable tracing to this buffer after 5 sec. (Tracing disabled after 5 sec ie at count=4)
3) Write to the buffer using trace_array_printk()
4) Stop the kernel thread and destroy the buffer during module unload.

A sample output for the same -

# tracer: nop
#
# entries-in-buffer/entries-written: 16/16 #P:4
#
# _-----=> irqs-off
# / _----=> need-resched

#                            | / _---=> hardirq/softirq
#                            || / _--=> preempt-depth
#                            ||| /     delay
#           TASK-PID   CPU#  ||||    TIMESTAMP  FUNCTION
#              | |       |   ||||       |         |

sample-instance-26797 [003] .... 955180.489833: simple_thread: trace_array_printk: count=0 sample-instance-26797 [003] .... 955180.489836: sample_event: count value=0 at jiffies=5249940864 sample-instance-26797 [003] .... 955181.513722: simple_thread: trace_array_printk: count=1 sample-instance-26797 [003] .... 955181.513724: sample_event: count value=1 at jiffies=5249941888 sample-instance-26797 [003] .... 955182.537629: simple_thread: trace_array_printk: count=2 sample-instance-26797 [003] .... 955182.537631: sample_event: count value=2 at jiffies=5249942912 sample-instance-26797 [003] .... 955183.561516: simple_thread: trace_array_printk: count=3 sample-instance-26797 [003] .... 955183.561518: sample_event: count value=3 at jiffies=5249943936 sample-instance-26797 [003] .... 955184.585423: simple_thread: trace_array_printk: count=4 sample-instance-26797 [003] .... 955184.585427: sample_event: count value=4 at jiffies=5249944960 sample-instance-26797 [003] .... 955185.609344: simple_thread: trace_array_printk: count=5 sample-instance-26797 [003] .... 955186.633241: simple_thread: trace_array_printk: count=6 sample-instance-26797 [003] .... 955187.657157: simple_thread: trace_array_printk: count=7 sample-instance-26797 [003] .... 955188.681039: simple_thread: trace_array_printk: count=8 sample-instance-26797 [003] .... 955189.704937: simple_thread: trace_array_printk: count=9 sample-instance-26797 [003] .... 955190.728840: simple_thread: trace_array_printk: count=10

--------------------------------------------------------------------------------------------------------------------

Thanks,
Divya

Quoted 95 lines · Steven Rostedt
Quoted 39 lines · On Fri, 20 Sep 2019 16:59:26 -0700 Divya Indi <[email protected]> wrote:

This is a sample module to demostrate the use of the newly introduced and exported APIs to access Ftrace instances from within the kernel.

Newly introduced APIs used here -

1. Create a new trace array if it does not exist. struct trace_array *trace_array_create(const char *name)

2. Destroy/Remove a trace array.
int trace_array_destroy(struct trace_array *tr)

3. Lookup a trace array, given its name.
struct trace_array *trace_array_lookup(const char *name)

4. Enable/Disable trace events:
int trace_array_set_clr_event(struct trace_array *tr, const char *system,

         const char *event, int set);

Exported APIs -
1. trace_printk equivalent for instances.
int trace_array_printk(struct trace_array *tr,

                unsigned long ip, const char *fmt, ...);

2. Helper function.
void trace_printk_init_buffers(void);

3. To decrement the reference counter.
void trace_array_put(struct trace_array *tr)

---
  samples/Kconfig                              |   7 ++
  samples/Makefile                             |   1 +
  samples/ftrace_instance/Makefile             |   6 ++
  samples/ftrace_instance/sample-trace-array.c | 134 +++++++++++++++++++++++++++
  samples/ftrace_instance/sample-trace-array.h |  84 +++++++++++++++++
  5 files changed, 232 insertions(+)
Quoted 4 lines

create mode 100644 samples/ftrace_instance/Makefile create mode 100644 samples/ftrace_instance/sample-trace-array.c create mode 100644 samples/ftrace_instance/sample-trace-array.h

I applied this patch but get this:

/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘mytimer_handler’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:35:2: error: implicit declaration of function ‘trace_array_set_clr_event’; did you mean ‘trace_set_clr_event’? [-Werror=implicit-function-declaration]

   trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", 0);
   ^~~~~~~~~~~~~~~~~~~~~~~~~
   trace_set_clr_event

/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘simple_thread_func’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:47:2: error: implicit declaration of function ‘trace_array_printk’; did you mean ‘trace_seq_printf’? [-Werror=implicit-function-declaration]
trace_array_printk(tr, _THIS_IP_, "trace_array_printk: count=%d\n",
^~~~~~~~~~~~~~~~~~
trace_seq_printf
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘simple_thread’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:85:2: error: implicit declaration of function ‘trace_array_put’; did you mean ‘trace_seq_putc’? [-Werror=implicit-function-declaration]

   trace_array_put(tr);
   ^~~~~~~~~~~~~~~
   trace_seq_putc
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘sample_trace_array_init’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:100:7: error: implicit declaration of function ‘trace_array_lookup’; did you mean ‘radix_tree_lookup’? [-Werror=implicit-function-declaration]
   tr = trace_array_lookup("sample-instance");
        ^~~~~~~~~~~~~~~~~~
        radix_tree_lookup
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:100:5: warning: assignment to ‘struct trace_array *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   tr = trace_array_lookup("sample-instance");
      ^
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:102:8: error: implicit declaration of function ‘trace_array_create’; did you mean ‘ftrace_force_update’? [-Werror=implicit-function-declaration]
    tr = trace_array_create("sample-instance");
         ^~~~~~~~~~~~~~~~~~
         ftrace_force_update
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:102:6: warning: assignment to ‘struct trace_array *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
    tr = trace_array_create("sample-instance");
       ^
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:110:2: error: implicit declaration of function ‘trace_printk_init_buffers’; did you mean ‘trace_event_get_offsets’? [-Werror=implicit-function-declaration]
   trace_printk_init_buffers();
   ^~~~~~~~~~~~~~~~~~~~~~~~~
   trace_event_get_offsets
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c: In function ‘sample_trace_array_exit’:
/work/git/linux-trace.git/samples/ftrace_instance/sample-trace-array.c:126:2: error: implicit declaration of function ‘trace_array_destroy’; did you mean ‘assoc_array_destroy’? [-Werror=implicit-function-declaration]
   trace_array_destroy(tr);
   ^~~~~~~~~~~~~~~~~~~
   assoc_array_destroy

cc1: some warnings being treated as errors
make[3]: *** [/work/git/linux-trace.git/scripts/Makefile.build:266: samples/ftrace_instance/sample-trace-array.o] Error 1 make[2]: *** [/work/git/linux-trace.git/scripts/Makefile.build:509: samples/ftrace_instance] Error 2 make[1]: *** [/work/git/linux-trace.git/Makefile:1650: samples] Error 2
make[1]: *** Waiting for unfinished jobs....

-- Steve