Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse /proc/pid/cmdline

Tags:

I'm trying to split the cmdline of a process on Linux but it seems I cannot rely on it to be separated by '\0' characters. Do you know why sometimes the '\0' character is used as separator and sometimes it is a regular space?

Do you know any other ways of retrieving the executable name and the path to it? I have been trying to get this information with 'ps' but it always returns the full command line and the executable name is truncated.

Thanks.

like image 280
ryotakatsuki Avatar asked Oct 18 '09 20:10

ryotakatsuki


People also ask

What is proc PID Cmdline?

In linux, /proc includes a directory for each running process, including kernel processes, in directories named /proc/PID, these are the directories present: directory. description. /proc/PID/cmdline. Command line arguments.

What is proc Cmdline in Linux?

This file shows the parameters passed to the kernel at the time it is started. A sample /proc/cmdline file looks like the following: ro root=/dev/VolGroup00/LogVol00 rhgb quiet 3. This output tells us the following: ro.

Can you see why we say proc is a pseudo filesystem which allows access to kernel data structures?

The proc file system is a pseudo-file system which is used as an interface to kernel data structures. It is commonly mounted at /proc. Most of it is read-only, but some files allow kernel variables to be changed. The following outline gives a quick tour through the /proc hierarchy.

What is proc Statm?

/proc/[pid]/statm Provides information about memory usage, measured in pages.


2 Answers

use strings

$ cat /proc/self/cmdline | strings -1 cat /proc/self/cmdline 
like image 128
riywo Avatar answered Sep 21 '22 19:09

riywo


The /proc/PID/cmdline is always separated by NUL characters.

To understand spaces, execute this command:

cat -v /proc/self/cmdline "a b" "c d e" 

EDIT: If you really see spaces where there shouldn't be any, perhaps your executable (intentionally or inadvertently) writes to argv[], or is using setproctitle()?

When the process is started by the kernel, cmdline is NUL-separated, and the kernel code simply copies the range of memory where argv[] was at process startup into the output buffer when you read /proc/PID/cmdline.

like image 30
Employed Russian Avatar answered Sep 19 '22 19:09

Employed Russian