bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* How to get the updated content of an argument which is updated in a kernel function by kprobe
@ 2021-06-08 11:01 rainkin
  2021-06-08 18:29 ` Andrii Nakryiko
  0 siblings, 1 reply; 2+ messages in thread
From: rainkin @ 2021-06-08 11:01 UTC (permalink / raw)
  To: bpf

Hi,
Assume that a kernel function has an input argument (i.e., a pointer),
and the function will update the content pointed by the pointer during
execution. My question is how to get the updated content using kprobe.

Take the kernel function path_lookupat as example:
static int path_lookupat(struct nameidata *nd, unsigned flags, struct
path *path)
It lookup the path according to a given file name and store the
founded path in the third input arguments (i.e., struct path *path).

My goal is to get the founded path from the third input argument.

I attach my ebpf program to this kernel function using kprobe, and try
to print the content of the path argument. However, the content is
empty, which is reasonable because the function has not beed executed.
The following is the code:

SEC("kprobe/path_lookupat")
int BPF_KPROBE(path_lookupat, struct nameidata *nd, unsigned flags,
struct path *path)
{
    char fn[127];
    const unsigned char *fn_ptr = BPF_CORE_READ(path, dentry, d_name.name);
     bpf_core_read_str(fn, sizeof(fn), fn_ptr);
     bpf_printk("path_lookupat: %s\n", fn);
     return 0;
}

Then I try to do that by kretprobe where the function has been
executed, but it seems that I cannot get the input arguments in
kretprobe.

Do you have any ideas or suggestions to do that?
Thanks,
rainkin

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How to get the updated content of an argument which is updated in a kernel function by kprobe
  2021-06-08 11:01 How to get the updated content of an argument which is updated in a kernel function by kprobe rainkin
@ 2021-06-08 18:29 ` Andrii Nakryiko
  0 siblings, 0 replies; 2+ messages in thread
From: Andrii Nakryiko @ 2021-06-08 18:29 UTC (permalink / raw)
  To: rainkin; +Cc: bpf

On Tue, Jun 8, 2021 at 4:07 AM rainkin <rainkin1993@gmail.com> wrote:
>
> Hi,
> Assume that a kernel function has an input argument (i.e., a pointer),
> and the function will update the content pointed by the pointer during
> execution. My question is how to get the updated content using kprobe.
>
> Take the kernel function path_lookupat as example:
> static int path_lookupat(struct nameidata *nd, unsigned flags, struct
> path *path)
> It lookup the path according to a given file name and store the
> founded path in the third input arguments (i.e., struct path *path).
>
> My goal is to get the founded path from the third input argument.
>
> I attach my ebpf program to this kernel function using kprobe, and try
> to print the content of the path argument. However, the content is
> empty, which is reasonable because the function has not beed executed.
> The following is the code:
>
> SEC("kprobe/path_lookupat")
> int BPF_KPROBE(path_lookupat, struct nameidata *nd, unsigned flags,
> struct path *path)
> {
>     char fn[127];
>     const unsigned char *fn_ptr = BPF_CORE_READ(path, dentry, d_name.name);
>      bpf_core_read_str(fn, sizeof(fn), fn_ptr);
>      bpf_printk("path_lookupat: %s\n", fn);
>      return 0;
> }
>
> Then I try to do that by kretprobe where the function has been
> executed, but it seems that I cannot get the input arguments in
> kretprobe.
>

Yes, you can't access input arguments from kretprobe. What you can do
is either use kprobe to remember the pointer and then read contents in
kretprobe. Or better yet is to use fexit program that has access to
input arguments and just do that in one place.

> Do you have any ideas or suggestions to do that?
> Thanks,
> rainkin

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-06-08 18:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-08 11:01 How to get the updated content of an argument which is updated in a kernel function by kprobe rainkin
2021-06-08 18:29 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).