Get full command line of a pid #2116
-
I am using a bpftrace script to fetch network usage on pid level, below article has the full code. Along with pid I want to get full command line including arguments, I could only get comm/process name since it is a built in variable. I could run "ps" commands in system function to fetch full command line but i dont think it is a good idea. I am looking for a better and optimized way, please suggest. Article: https://www.gcardone.net/2020-07-31-per-process-bandwidth-monitoring-on-Linux-with-bpftrace/ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
we don't have a helper for that, nor seems there to be a BPF helper for it. What I'd do is try to redo the steps in bpf. The data you want is available in
another window:
so that seems to work. Now lets look at the function: https://elixir.bootlin.com/linux/latest/source/fs/proc/base.c#L358
So it first gets the task_struct for the pid associated with the file (remember its /proc/PID/cmdline so the pid is in there) and then calls a helper to extract the cmdline. We have Inspecting the next function:
So that seems like its using the If we now look at https://elixir.bootlin.com/linux/latest/source/fs/proc/base.c#L256 it basically loops over
Let's run it:
sadpanda.jpg No data so likely that the probe_read failed, let's try again:
Digging deeper https://elixir.bootlin.com/linux/latest/source/mm/memory.c#L5133 we see:
Now you think you can write What we can do however is use
So not great but definitely usable :) |
Beta Was this translation helpful? Give feedback.
we don't have a helper for that, nor seems there to be a BPF helper for it. What I'd do is try to redo the steps in bpf.
The data you want is available in
/proc/pid/cmdline
so a first guess would be a similar named function: