Skip to content

Commit

Permalink
helper-function: fix formatting of bpf_probe_read_user_str.md
Browse files Browse the repository at this point in the history
Fix formatting of the code block and some inline codes.
  • Loading branch information
kxxt committed Oct 23, 2024
1 parent 99fe8b0 commit 5c70e53
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions docs/linux/helper-function/bpf_probe_read_user_str.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ Copy a NUL terminated string from an unsafe user address _unsafe_ptr_ to _dst_.

On success, returns the number of bytes that were written, including the terminal NUL. This makes this helper useful in tracing programs for reading strings, and more importantly to get its length at runtime. See the following snippet:

```c
SEC("kprobe/sys_open")
void bpf_sys_open(struct pt_regs ctx) {
char buf[PATHLEN]; // PATHLEN is defined to 256
int res = bpf_probe_read_user_str(buf, sizeof(buf), ctx->di);
// Consume buf, for example push it to
// userspace via bpf_perf_event_output(); we
// can use res (the string length) as event
// size, after checking its boundaries.
}
```
SEC("kprobe/sys_open") void bpf_sys_open(struct pt_regs _ctx) { char buf[PATHLEN]; // PATHLEN is defined to 256 int res = bpf_probe_read_user_str(buf, sizeof(buf),
```

         ctx->di);

        // Consume buf, for example push it to // userspace via bpf_perf_event_output(); we // can use res (the string length) as event // size, after checking its boundaries.

    }
In comparison, using **bpf_probe_read_user**() helper here instead to read the string would require to estimate the length at compile time, and would often result in copying more memory than necessary.
Another useful use case is when parsing individual process arguments or individual environment variables navigating _current_**->mm->arg_start** and _current_\ **->mm->env_start**: using this helper and the return value, one can quickly iterate at the right offset of the memory area.
Another useful use case is when parsing individual process arguments or individual environment variables navigating **current->mm->arg_start** and **current->mm->env_start**: using this helper and the return value, one can quickly iterate at the right offset of the memory area.
### Returns
Expand Down

0 comments on commit 5c70e53

Please sign in to comment.