libtracefs: Facilitate adding and removing kprobes
v4 cover libtracefs: Facilitate adding and removing kprobes
From: "Steven Rostedt (VMware)" <[email protected]>
Add a tracefs_kprobe_raw() to facilitate adding kprobe events. It's denoted as "raw" because it still requires knowing the complex format of a kprobe, but at least it helps with other formats:
p:[[system/]event] addr fmt
The user only needs to know the "fmt" part above, and not worry about what file to open, or how to open it.
Also add a tracefs_kprobe_clear() to clear all kprobes and a tracefs_kprobe_clear_probe() to clear an individual kprobe. Both have a "force" parameter, that if set, will then try to disable the kprobe in all instances (including the top) and then remove the kprobe(s).
Changes since v3: https://lore.kernel.org/linux-trace-devel/[email protected]/
- Silly me used "stat(KPROBE_EVENTS)" to see if the kprobe event file existed, but KPROBE_EVENTS is not a full path. Should have used tracefs_file_exists() since that's what it's made for. My testing missed this, because I forgot to push the changes to my test machine, and I tested the code with old logic.
Steven Rostedt (VMware) (7):
libtracefs: Implement tracefs_instances()
libtracefs: Implement tracefs_kprobe_raw()
libtracefs: Implement tracefs_kretprobe_raw()
libtracefs: Implement tracefs_get_kprobes()
libtracefs: Implement tracefs_kprobe_clear_all() to remove all kprobes
libtracefs: Implement tracefs_kprobe_clear_probe()
libtracefs: Add man pages for kprobe functions
Documentation/libtracefs-kprobes.txt | 275 +++++++++++++++++++++ include/tracefs.h | 9 + src/Makefile | 1 + src/tracefs-instance.c | 78 ++++++ src/tracefs-kprobes.c | 349 +++++++++++++++++++++++++++ 5 files changed, 712 insertions(+) create mode 100644 Documentation/libtracefs-kprobes.txt create mode 100644 src/tracefs-kprobes.c
Signature · 1 line
2.30.2
PATCH v4 1/7 libtracefs: Implement tracefs_instances()
From: "Steven Rostedt (VMware)" <[email protected]>
Implement tracefs_instances() that will take a regex (or NULL for all) and return a list of instances in the system.
--- include/tracefs.h | 1 + src/tracefs-instance.c | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+)
include/tracefs.h +1 -0
@@ -44,6 +44,7 @@ int tracefs_instance_file_read_number(struct tracefs_instance *instance,int tracefs_instance_file_open (struct tracefs_instance *instance,const char *file,int mode);int tracefs_instances_walk (int (*callback)(const char *,void *),void *context); +char **tracefs_instances (const char *regex);bool tracefs_instance_exists (const char *name);bool tracefs_file_exists (struct tracefs_instance *instance,const char *name);
src/tracefs-instance.c +78 -0
90 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH v4 1/7] libtracefs: Implement tracefs_instances()
Quoted 57 lines · Steven Rostedt
From: "Steven Rostedt (VMware)" <[email protected]>
Implement
tracefs_instances()that will take a regex (or NULL for all) and return a list of instances in the system.Signed-off-by Steven Rostedt (VMware) <[email protected]>--- include/tracefs.h | 1 + src/tracefs-instance.c | 78 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+)include/tracefs.h +1
-0@@ -44,6 +44,7 @@ int tracefs_instance_file_read_number(struct tracefs_instance *instance,int tracefs_instance_file_open (struct tracefs_instance *instance,const char *file,int mode);int tracefs_instances_walk (int (*callback)(const char *,void *),void *context); +char **tracefs_instances (const char *regex);bool tracefs_instance_exists (const char *name);bool tracefs_file_exists (struct tracefs_instance *instance,const char *name);src/tracefs-instance.c +19
-0@@ -14,6 +14,7 @@#include <sys/stat.h> #include <fcntl.h> #include <dirent.h> +#include <regex.h> #include <limits.h> #include <pthread.h> #include "tracefs.h" @@ -630,6 +631,83 @@ out:return fret; } +static inline bool match (const char *str,regex_t *re) +{ +if (!re) +return true; +return regexec (re, str,0 ,NULL ,0 ) ==0 ; +} + +struct instance_list { +regex_t *re; +char **list; +int size; +int failed; +}; + +static int build_list (const char *name,void *data) +{ +struct instance_list *list = data; +char **instances;
src/tracefs-instance.c out: · line 661
Quoted 10 lines
src/tracefs-instance.c +10
-0+int ret =-1 ; + +if (!match (name, list->re)) +return 0 ; + + instances =realloc (list->list, list->size +2 ); +if (!instances) + goto out; + + list->list = instances;
Hi Steven,
I am not sure what was your original intention here, but this doesn't seem to work properly. Maybe we need something like this:
int size = sizeof(*instances) * (list->size + 2); instances = realloc(list->list, size); if (!instances) goto out; instances[list->size + 1] = NULL;
Quoted 35 lines
src/tracefs-instance.c +35
-0+ list->list[list->size] =strdup (name); +if (!list->list[list->size]) + goto out; + + list->size++; + ret =0 ; + + out: + list->failed = ret; +return ret; +} + +/** +* tracefs_instances - return a list of instance names +* @regex: A regex of instances to filter on (NULL to match all) +* +* Returns a list of names of existing instances, that must be +* freed with tracefs_list_free(). Note, if there are no matches +* then an empty list will be returned (not NULL). +* NULL on error. +*/ +char **tracefs_instances (const char *regex) +{ +struct instance_list list = { .re =NULL , .list =NULL }; +regex_t re; +int ret; + +if (regex) { + ret =regcomp (&re, regex,REG_ICASE |REG_NOSUB ); +if (ret <0 ) +return NULL ; + list.re = &re; + } + + ret =tracefs_instances_walk (build_list, &list);
src/tracefs-instance.c out: · line 706
Quoted 10 lines
src/tracefs-instance.c +10
-0+if (ret <0 || list.failed) { +tracefs_list_free (list.list); + list.list =NULL ; + }else { +if (!list.list) { +/* No matches should produce an empty list */ + list.list =malloc (sizeof (*list.list)); +if (list.list) + list.list[0 ] =NULL ; + }
Or you can just do:
return calloc(1, sizeof(*list.list));
Thanks!
Yordan
Quoted 8 lines
src/tracefs-instance.c +4
-0+ } +return list.list; +} +/** * tracefs_get_clock - Get the current trace clock * @instance: ftrace instance, can be NULL for the top instance
Re: [PATCH v4 1/7] libtracefs: Implement tracefs_instances()
Quoted 16 lines · On Fri, 2 Jul 2021 14:10:46 +0300 "Yordan Karadzhov (VMware)"…
Quoted 4 lines
src/tracefs-instance.c +4
-0+static int build_list (const char *name,void *data) +{ +struct instance_list *list = data; +char **instances;src/tracefs-instance.c out: · line 661
Quoted 10 lines
src/tracefs-instance.c +10
-0+int ret =-1 ; + +if (!match (name, list->re)) +return 0 ; + + instances =realloc (list->list, list->size +2 ); +if (!instances) + goto out; + + list->list = instances;Hi Steven,
Quoted 5 lines
I am not sure what was your original intention here, but this doesn't seem to work properly. Maybe we need something like this:
int size = sizeof(*instances) * (list->size + 2); instances = realloc(list->list, size);
Bah, that's what I get for trying to "multitask" while coding :-p That was suppose to be:
instances = realloc(list->list, sizeof(*instances) * (list->size + 2);
as you stated, but I'll keep it one line.
Quoted 55 lines
if (!instances) goto out; instances[list->size + 1] = NULL;Quoted 35 lines
src/tracefs-instance.c +35
-0+ list->list[list->size] =strdup (name); +if (!list->list[list->size]) + goto out; + + list->size++; + ret =0 ; + + out: + list->failed = ret; +return ret; +} + +/** +* tracefs_instances - return a list of instance names +* @regex: A regex of instances to filter on (NULL to match all) +* +* Returns a list of names of existing instances, that must be +* freed with tracefs_list_free(). Note, if there are no matches +* then an empty list will be returned (not NULL). +* NULL on error. +*/ +char **tracefs_instances (const char *regex) +{ +struct instance_list list = { .re =NULL , .list =NULL }; +regex_t re; +int ret; + +if (regex) { + ret =regcomp (&re, regex,REG_ICASE |REG_NOSUB ); +if (ret <0 ) +return NULL ; + list.re = &re; + } + + ret =tracefs_instances_walk (build_list, &list);src/tracefs-instance.c out: · line 706
Quoted 10 lines
src/tracefs-instance.c +10
-0+if (ret <0 || list.failed) { +tracefs_list_free (list.list); + list.list =NULL ; + }else { +if (!list.list) { +/* No matches should produce an empty list */ + list.list =malloc (sizeof (*list.list)); +if (list.list) + list.list[0 ] =NULL ; + }Or you can just do:
Quoted 1 line
return calloc(1, sizeof(*list.list));
Which I do in tracefs_get_probes(), must have missed this one.
Will update, thanks for the review.
-- Steve
Quoted 7 lines
Thanks!
YordanQuoted 3 lines
src/tracefs-instance.c +3
-0+ } +return list.list; +}
Re: [PATCH v4 1/7] libtracefs: Implement tracefs_instances()
Quoted 25 lines · Steven Rostedt
Quoted 16 lines · On Fri, 2 Jul 2021 14:10:46 +0300 "Yordan Karadzhov (VMware)"…
Quoted 4 lines
src/tracefs-instance.c +4
-0+static int build_list (const char *name,void *data) +{ +struct instance_list *list = data; +char **instances;src/tracefs-instance.c out: · line 661
Quoted 10 lines
src/tracefs-instance.c +10
-0+int ret =-1 ; + +if (!match (name, list->re)) +return 0 ; + + instances =realloc (list->list, list->size +2 ); +if (!instances) + goto out; + + list->list = instances;Hi Steven,
Quoted 5 lines
I am not sure what was your original intention here, but this doesn't seem to work properly. Maybe we need something like this:
int size = sizeof(*instances) * (list->size + 2); instances = realloc(list->list, size);
Quoted 11 lines
Bah, that's what I get for trying to "multitask" while coding :-p That was suppose to be:
instances = realloc(list->list, sizeof(*instances) * (list->size + 2);as you stated, but I'll keep it one line.
Quoted 4 lines
if (!instances) goto out; instances[list->size + 1] = NULL;
but you still need to make sure it is NULL terminated. Y.
Quoted 68 lines
Quoted 51 lines
Quoted 39 lines
src/tracefs-instance.c +35
-0+ list->list[list->size] =strdup (name); +if (!list->list[list->size]) + goto out; + + list->size++; + ret =0 ; + + out: + list->failed = ret; +return ret; +} + +/** +* tracefs_instances - return a list of instance names +* @regex: A regex of instances to filter on (NULL to match all) +* +* Returns a list of names of existing instances, that must be +* freed with tracefs_list_free(). Note, if there are no matches +* then an empty list will be returned (not NULL). +* NULL on error. +*/ +char **tracefs_instances (const char *regex) +{ +struct instance_list list = { .re =NULL , .list =NULL }; +regex_t re; +int ret; + +if (regex) { + ret =regcomp (&re, regex,REG_ICASE |REG_NOSUB ); +if (ret <0 ) +return NULL ; + list.re = &re; + } + + ret =tracefs_instances_walk (build_list, &list);src/tracefs-instance.c out: · line 706
Quoted 10 lines
src/tracefs-instance.c +10
-0+if (ret <0 || list.failed) { +tracefs_list_free (list.list); + list.list =NULL ; + }else { +if (!list.list) { +/* No matches should produce an empty list */ + list.list =malloc (sizeof (*list.list)); +if (list.list) + list.list[0 ] =NULL ; + }Or you can just do:
Quoted 1 line
return calloc(1, sizeof(*list.list));Which I do in
tracefs_get_probes(), must have missed this one.Will update, thanks for the review.
-- Steve
Quoted 7 lines
Thanks!
YordanQuoted 3 lines
src/tracefs-instance.c +3
-0+ } +return list.list; +}
Re: [PATCH v4 1/7] libtracefs: Implement tracefs_instances()
Quoted 8 lines · On Fri, 2 Jul 2021 14:39:48 +0300 "Yordan Karadzhov (VMware)"…
Quoted 6 lines
as you stated, but I'll keep it one line.
Quoted 4 lines
if (!instances) goto out; instances[list->size + 1] = NULL;but you still need to make sure it is NULL terminated.
Ah I missed that part of your comment (I'm still sleeping), will send a v2 ;-)
-- Steve
PATCH v4 2/7 libtracefs: Implement tracefs_kprobe_raw()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add a function to facilitate creating a kprobe event. It has the "raw" in its name because it still requires knowing the format of the kprobe. But does handle the kprobe naming better and writing to the kprobe_event file.
--- include/tracefs.h | 4 +++ src/Makefile | 1 + src/tracefs-kprobes.c | 72 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 src/tracefs-kprobes.c
include/tracefs.h +4 -0
@@ -214,4 +214,8 @@ int tracefs_tracer_clear(struct tracefs_instance *instance);ssize_t tracefs_trace_pipe_stream (int fd,struct tracefs_instance *instance,int flags);ssize_t tracefs_trace_pipe_print (struct tracefs_instance *instance,int flags);void tracefs_trace_pipe_stop (struct tracefs_instance *instance); + +int tracefs_kprobe_raw (const char *system,const char *event, +const char *addr,const char *format); +#endif /* _TRACE_FS_H */
src/Makefile +1 -0
@@ -8,6 +8,7 @@ OBJS += tracefs-instance.o OBJS += tracefs-events.o OBJS += tracefs-tools.o OBJS += tracefs-marker.o +OBJS += tracefs-kprobes.o OBJS := $(OBJS:%.o=$(bdir)/%.o) DEPS := $(OBJS:$(bdir)/%.o=$(bdir)/.%.d)
src/tracefs-kprobes.c new file +72 -0
72 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v4 3/7 libtracefs: Implement tracefs_kretprobe_raw()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add an interface that is similar to tracefs_kprobe_raw() but creates a "kretprobe". The difference between a kprobe and a kretprobe is that a kretprobe comes at the end of a function.
See Documentation/trace/kprobetrace.rst in the Linux kernel source code for more information. Note, kprobes are started with "p" and kretprobe lines start with an "r".
--- include/tracefs.h | 3 +- src/tracefs-kprobes.c | 85 ++++++++++++++++++++++++++++++------------- 2 files changed, 61 insertions(+), 27 deletions(-)
include/tracefs.h +2 -1
@@ -217,5 +217,6 @@ void tracefs_trace_pipe_stop(struct tracefs_instance *instance);int tracefs_kprobe_raw (const char *system,const char *event,const char *addr,const char *format); - +int tracefs_kretprobe_raw (const char *system,const char *event, +const char *addr,const char *format);#endif /* _TRACE_FS_H */
src/tracefs-kprobes.c +59 -26
101 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v4 4/7 libtracefs: Implement tracefs_get_kprobes()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add the function tracefs_get_kprobes() that returns a list of kprobes that are registered. The list contains strings that are of the format "group/event" (i.e. "kprobes/open"). The last element in the list is a NULL pointer. In the case that there are no kprobes, the list will return a single entry of a NULL pointer.
NULL is returned in the case of error (mainly a memory issue, or bad parsing).
--- include/tracefs.h | 1 + src/tracefs-kprobes.c | 66 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)
include/tracefs.h +1 -0
@@ -219,4 +219,5 @@ int tracefs_kprobe_raw(const char *system, const char *event, const char *addr, const char *format);int tracefs_kretprobe_raw (const char *system,const char *event,const char *addr,const char *format); +char **tracefs_get_kprobes (void );#endif /* _TRACE_FS_H */
src/tracefs-kprobes.c +66 -0
69 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v4 5/7 libtracefs: Implement tracefs_kprobe_clear_all() to remove all kprobes
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
A call to tracefs_kprobe_clear_all() will attempt to disable all kprobes. If any kprobe is set, and the @force parameter is set, it will fail with errno set to EBUSY. If @force is set, then it will attempt to disable all the defined kprobe events and then clear it.
--- include/tracefs.h | 1 + src/tracefs-kprobes.c | 103 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+)
include/tracefs.h +1 -0
@@ -220,4 +220,5 @@ int tracefs_kprobe_raw(const char *system, const char *event,int tracefs_kretprobe_raw (const char *system,const char *event,const char *addr,const char *format);char **tracefs_get_kprobes (void ); +int tracefs_kprobe_clear_all (bool force);#endif /* _TRACE_FS_H */
src/tracefs-kprobes.c +103 -0
106 lines not on this page · raw message
Signature · 1 line
2.30.2
Re: [PATCH v4 5/7] libtracefs: Implement tracefs_kprobe_clear_all() to remove all kprobes
Quoted 28 lines · Steven Rostedt
From: "Steven Rostedt (VMware)" <[email protected]>
A call to
tracefs_kprobe_clear_all()will attempt to disable all kprobes. If any kprobe is set, and the @force parameter is set, it will fail with errno set to EBUSY. If @force is set, then it will attempt to disable all the defined kprobe events and then clear it.Signed-off-by Steven Rostedt (VMware) <[email protected]>--- include/tracefs.h | 1 + src/tracefs-kprobes.c | 103 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+)include/tracefs.h +1
-0@@ -220,4 +220,5 @@ int tracefs_kprobe_raw(const char *system, const char *event,int tracefs_kretprobe_raw (const char *system,const char *event,const char *addr,const char *format);char **tracefs_get_kprobes (void ); +int tracefs_kprobe_clear_all (bool force);#endif /* _TRACE_FS_H */ src/tracefs-kprobes.c +0
-0
src/tracefs-kprobes.c char **tracefs_get_kprobes(void) · line 176
Quoted 8 lines
src/tracefs-kprobes.c +5
-0@@ -169,3 +169,106 @@ char **tracefs_get_kprobes(void) list =NULL ; goto out; } + +struct instance_list { +struct instance_list *next; +struct tracefs_instance *instance; +};
This is not used in this source file. I see that it was moved to tracefs-instance.c
Thanks!
Y.
Quoted 99 lines
src/tracefs-kprobes.c +98
-0+ +static void disable_events (const char *system,const char *event, +char **list) +{ +struct tracefs_instance *instance; +int i; + +/* +* Note, this will not fail even on error. +* That is because even if something fails, it may still +* work enough to clear the kprobes. If that's the case +* the clearing after the loop will succeed and the function +* is a success, even though other parts had failed. If +* one of the kprobe events is enabled in one of the +* instances that fail, then the clearing will fail too +* and the function will return an error. +*/ + +tracefs_event_disable (NULL , system, event); +/* No need to test results */ + +if (!list) +return ; + +for (i =0 ; list[i]; i++) { + instance =tracefs_instance_alloc (NULL , list[i]); +/* If this fails, try the next one */ +if (!instance) +continue ; +tracefs_event_disable (instance, system, event); +tracefs_instance_free (instance); + } +return ; +} + +/** +* tracefs_kprobe_clear_all - clear kprobe events +* @force: Will attempt to disable all kprobe events and clear them +* +* Will remove all defined kprobe events. If any of them are enabled, +* and @force is not set, then it will error with -1 and errno to be +* EBUSY. If @force is set, then it will attempt to disable all the kprobe +* events in all instances, and try again. +* +* Returns zero on success, -1 otherwise. +*/ +int tracefs_kprobe_clear_all (bool force) +{ +char **instance_list; +char **kprobe_list; +char *saveptr; +char *system; +char *kprobe; +char *event; +int ret; +int i; + + ret =tracefs_instance_file_clear (NULL ,KPROBE_EVENTS ); +if (!ret) +return 0 ; + +if (!force) +return -1 ; + + kprobe_list =tracefs_get_kprobes (); +if (!kprobe_list) +return -1 ; + + instance_list =tracefs_instances (NULL ); +/* +* Even if the above failed and instance_list is NULL, +* keep going, as the enabled event may simply be in the +* top level. +*/ + +for (i =0 ; kprobe_list[i]; i++) { + kprobe = kprobe_list[i]; + + system =strtok_r (kprobe,"/" , &saveptr); +if (!system) + goto out; + + event =strtok_r (NULL ," " , &saveptr); +if (!event) + goto out; + +disable_events (system, event, instance_list); + + ret =tracefs_instance_file_clear (NULL ,KPROBE_EVENTS ); +/* On success stop the loop */ +if (!ret) + goto out; + } + out: +tracefs_list_free (instance_list); +tracefs_list_free (kprobe_list); +return ret; +}
Re: [PATCH v4 5/7] libtracefs: Implement tracefs_kprobe_clear_all() to remove all kprobes
Quoted 8 lines · On Fri, 2 Jul 2021 14:15:44 +0300 "Yordan Karadzhov (VMware)"…
Quoted 8 lines
src/tracefs-kprobes.c +0
-0@@ -169,3 +169,106 @@ char **tracefs_get_kprobes(void) list =NULL ; goto out; }
Quoted 8 lines
src/tracefs-kprobes.c char **tracefs_get_kprobes(void) · line 176
Quoted 5 lines
src/tracefs-kprobes.c +5
-0+ +struct instance_list { +struct instance_list *next; +struct tracefs_instance *instance; +};This is not used in this source file. I see that it was moved to
tracefs-instance.c
Internally, this was my fourth iteration between v2 and v3, and I kept finding "better" ways to write this code. This was left over from one of those iterations.
I'll nuke it. Thanks!
-- Steve
PATCH v4 6/7 libtracefs: Implement tracefs_kprobe_clear_probe()
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add the function tracefs_kprobe_clear_probe() that will remove a single kprobe. If the @force parameter is set, it will disable that probe in all instances (including the top level instance) before removing it.
If the @event parameter is NULL, then it will clear all events that are defined by the @system parameter. If the @system parameter is NULL, then it will use the default "kprobes" group.
--- include/tracefs.h | 1 + src/tracefs-kprobes.c | 123 +++++++++++++++++++++++++++++++++--------- 2 files changed, 100 insertions(+), 24 deletions(-)
include/tracefs.h +1 -0
@@ -221,4 +221,5 @@ int tracefs_kretprobe_raw(const char *system, const char *event, const char *addr, const char *format);char **tracefs_get_kprobes (void );int tracefs_kprobe_clear_all (bool force); +int tracefs_kprobe_clear_probe (const char *system,const char *event,bool force);#endif /* _TRACE_FS_H */
src/tracefs-kprobes.c +99 -24
151 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v4 7/7 libtracefs: Add man pages for kprobe functions
In reply to Steven RostedtFrom: "Steven Rostedt (VMware)" <[email protected]>
Add man pages that describe the following functions:
tracefs_kprobe_raw
tracefs_kretprobe_raw
tracefs_get_kprobes
tracefs_kprobe_clear_all
tracefs_kprobe_clear_probe
--- Documentation/libtracefs-kprobes.txt | 275 +++++++++++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 Documentation/libtracefs-kprobes.txt
Documentation/libtracefs-kprobes.txt new file +275 -0
@@ -0,0 +1,275 @@ +libtracefs(3) +============= + +NAME +---- +tracefs_kprobe_raw, tracefs_kretprobe_raw, tracefs_get_kprobes, tracefs_kprobe_clear_all, tracefs_kprobe_clear_probe - Create, list, and destroy kprobes + +SYNOPSIS +-------- +[verse] +-- +*#include <tracefs.h>* + +int tracefs_kprobe_raw(const char pass:[*]system, const char pass:[*]event, const char pass:[*]addr, const char pass:[*]format); +int tracefs_kretprobe_raw(const char pass:[*]system, const char pass:[*]event, const char pass:[*]addr, const char pass:[*]format); +char pass:[*]pass:[*]tracefs_get_kprobes(void); +int tracefs_kprobe_clear_all(bool force); +int tracefs_kprobe_clear_probe(const char pass:[*]system, const char pass:[*]event, bool force); +-- + +DESCRIPTION +----------- +*tracefs_kprobe_raw*() will create a kprobe event. If _system_ is NULL, then +the default "kprobes" is used for the group (event system). Otherwise if _system_ +is specified then the kprobe will be created under the group by that name. The +_event_ is the name of the kprobe event to create. The _addr_ can be a function, +a function and offset, or a kernel address. This is where the location of the +kprobe will be inserted in the kernel. The _format_ is the kprobe format as +specified as FETCHARGS in the Linux kernel source in the Documentation/trace/kprobetrace.rst +document. + +*tracefs_kretprobe_raw*() is the same as *tracefs_kprobe_raw()*, except that it +creates a kretprobe instead of a kprobe. The difference is also described +in the Linux kernel source in the Documentation/trace/kprobetrace.rst file. + +*tracefs_get_kprobes*() returns an array of strings (char pass:[*]) that contain +the registered kprobes and kretprobes. The names are in the "system/event" format. +That is, one string holds both the kprobe's name as well as the group it is +defined under. These strings are allocated and may be modified with the +*strtok*(3) and *strtok_r*(3) functions. The string returned must be freed with +*tracefs_list_free*(3). + +*tracefs_kprobe_clear_all*() will try to remove all kprobes that have been +registered. If the @force flag is set, it will then disable those kprobe events +if they are enabled and then try to clear the kprobes. + +*tracefs_kprobe_clear_probe*() will try to clear specified kprobes. If _system_ +is NULL, then it will only clear the default kprobes under the "kprobes" group. +If _event_ is NULL, it will clear all kprobes under the given _system_. If the +_force_ flag is set, then it will disable the given kprobe events before clearing +them. + +RETURN VALUE +------------ + +*tracefs_kprobe_raw*(), *tracefs_kretprobe_raw*(), *tracefs_kprobe_clear_all*(), +and *tracefs_kprobe_clear_probe*() return 0 on success, or -1 on error. + +If a parsing error occurs on *tracefs_kprobe_raw*() or *tracefs_kretprobe_raw*() +then *tracefs_error_last*(3) may be used to retrieve the error message explaining +the parsing issue. + +*tracefs_get_kprobes*() returns an allocate string list of allocated strings +on success that must be freed with *tracefs_list_free*(3) and returns +NULL on error. + +ERRORS +------ +The following errors are for all the above calls: + +*EPERM* Not run as root user + +*ENODEV* Kprobe events are not configured for the running kernel. + +*ENOMEM* Memory allocation error. + +*tracefs_kprobe_raw*(), *tracefs_kretprobe_raw*() can fail with the following errors: + +*EBADMSG* Either _addr_ or _format_ are NULL. + +*EINVAL* Most likely a parsing error occurred (use *tracefs_error_last*(3) to possibly + see what that error was). + +Other errors may also happen caused by internal system calls. + +EXAMPLE +------- +[source,c] +-- +#include <stdlib.h> +#include <unistd.h> +#include <sys/wait.h> + +#include <tracefs/tracefs.h> + +static struct tep_event *open_event; +static struct tep_format_field *file_field; + +static struct tep_event *openret_event; +static struct tep_format_field *ret_field; + +static int callback(struct tep_event *event, struct tep_record *record, + int cpu, void *data) +{ + struct trace_seq seq; + + trace_seq_init(&seq); + tep_print_event(event->tep, &seq, record, "%d-%s: ", TEP_PRINT_PID, TEP_PRINT_COMM); + + if (event->id == open_event->id) { + trace_seq_puts(&seq, "open file='"); + tep_print_field(&seq, record->data, file_field); + trace_seq_puts(&seq, "'\n"); + } else if (event->id == openret_event->id) { + unsigned long long ret; + tep_read_number_field(ret_field, record->data, &ret); + trace_seq_printf(&seq, "open ret=%lld\n", ret); + } else { + goto out; + } + + trace_seq_terminate(&seq); + trace_seq_do_printf(&seq); +out: + trace_seq_destroy(&seq); + + return 0; +} + +static pid_t run_exec(char **argv, char **env) +{ + pid_t pid; + + pid = fork(); + if (pid) + return pid; + + execve(argv[0], argv, env); + perror("exec"); + exit(-1); +} + +const char *mykprobe = "my_kprobes"; + +enum kprobe_type { + KPROBE, + KRETPROBE, +}; + +static void __kprobe_create(enum kprobe_type type, const char *event, + const char *addr, const char *fmt) +{ + char *err; + int r; + + if (type == KPROBE) + r = tracefs_kprobe_raw(mykprobe, event, addr, fmt); + else + r = tracefs_kretprobe_raw(mykprobe, event, addr, fmt); + if (r < 0) { + err = tracefs_error_last(NULL); + perror("Failed to create kprobe:"); + if (err && strlen(err)) + fprintf(stderr, "%s\n", err); + } +} + +static void kprobe_create(const char *event, const char *addr, + const char *fmt) +{ + __kprobe_create(KPROBE, event, addr, fmt); +} + +static void kretprobe_create(const char *event, const char *addr, + const char *fmt) +{ + __kprobe_create(KRETPROBE, event, addr, fmt); +} + +int main (int argc, char **argv, char **env) +{ + struct tracefs_instance *instance; + struct tep_handle *tep; + const char *sysnames[] = { mykprobe, NULL }; + pid_t pid; + + if (argc < 2) { + printf("usage: %s command\n", argv[0]); + exit(-1); + } + + instance = tracefs_instance_create("exec_open"); + if (!instance) { + perror("creating instance"); + exit(-1); + } + + tracefs_kprobe_clear_probe(mykprobe, NULL, true); + + kprobe_create("open", "do_sys_openat2", + "file=+0($arg2):ustring flags=+0($arg3):x64 mode=+8($arg3):x64\n"); + + kretprobe_create("openret", "do_sys_openat2", "ret=%ax"); + + tep = tracefs_local_events_system(NULL, sysnames); + if (!tep) { + perror("reading events"); + exit(-1); + } + open_event = tep_find_event_by_name(tep, mykprobe, "open"); + file_field = tep_find_field(open_event, "file"); + + openret_event = tep_find_event_by_name(tep, mykprobe, "openret"); + ret_field = tep_find_field(openret_event, "ret"); + + tracefs_event_enable(instance, mykprobe, NULL); + pid = run_exec(&argv[1], env); + + /* Let the child start to run */ + sched_yield(); + + do { + tracefs_load_cmdlines(NULL, tep); + tracefs_iterate_raw_events(tep, instance, NULL, 0, callback, NULL); + } while (waitpid(pid, NULL, WNOHANG) != pid); + + /* Will disable the events */ + tracefs_kprobe_clear_probe(mykprobe, NULL, true); + tracefs_instance_destroy(instance); + tep_free(tep); + + return 0; +} +-- + +FILES +----- +[verse] +-- +*tracefs.h* + Header file to include in order to have access to the library APIs. +*-ltracefs* + Linker switch to add when building a program that uses the library. +-- + +SEE ALSO +-------- +_libtracefs(3)_, +_libtraceevent(3)_, +_trace-cmd(1)_ + +AUTHOR +------ +[verse] +-- +*Steven Rostedt* <[email protected]> +*Tzvetomir Stoyanov* <[email protected]> +*sameeruddin shaik* <[email protected]> +-- +REPORTING BUGS +-------------- +Report bugs to <[email protected]> + +LICENSE +------- +libtracefs is Free Software licensed under the GNU LGPL 2.1 + +RESOURCES +--------- +https://git.kernel.org/pub/scm/libs/libtrace/libtracefs.git/ + +COPYING +------- +Copyright \(C) 2020 VMware, Inc. Free use of this software is granted under +the terms of the GNU Public License (GPL).
Signature · 1 line
2.30.2