I'm trying to split the cmdline of a process on Linux but it seems I cannot rely on it to be separated by '
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?
我试图在Linux上拆分进程的cmdline,但似乎我不能依赖它来用'\ 0'字符分隔。你知道为什么有时'\ 0'字符被用作分隔符,有时它是一个常规空间?
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.
您是否知道检索可执行文件名称及其路径的其他方法?我一直试图用'ps'来获取这些信息,但它总是返回完整的命令行,并且可执行文件名被截断。
Thanks.
谢谢。
6 个解决方案
#1
14
use strings
使用字符串
$ cat /proc/self/cmdline | strings -1
cat
/proc/self/cmdline
#2
11
The /proc/PID/cmdline is always separated by NUL characters.
/ proc / PID / cmdline始终用NUL字符分隔。
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()?
编辑:如果你真的看到不应该有的空格,也许你的可执行文件(有意或无意)写入argv [],或使用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.
当内核启动进程时,cmdline是NUL分隔的,当你读取/ proc / PID / cmdline时,内核代码只是将进程启动时argv []的内存范围复制到输出缓冲区中。
#3
8
Use
使用
cat /proc/2634/cmdline | tr "\0" " "
to get the args separated by blanks, as you would see it on a command line.
用空格分隔args,就像你在命令行中看到的一样。
#4
4
A shot in the dark, but is it possible that \0 is separating terms and spaces are separating words within a term? For example,
在黑暗中拍摄,但是\ 0是否可以分隔术语和空格分隔术语中的单词?例如,
myprog "foo bar" baz
might appear in /proc/pid/cmdline as...
可能会出现在/ proc / pid / cmdline中...
/usr/bin/myprog\0foo bar\0baz
Complete guess here, I can't seem to find any spaces on one of my Linux boxes.
在这里完成猜测,我似乎无法在我的一个Linux机器上找到任何空格。
#5
4
The command line arguments in /proc/PID/cmdline are separated by null bytes. You can use tr to replace them by new lines:
/ proc / PID / cmdline中的命令行参数由空字节分隔。您可以使用tr替换新行:
tr '\0' '\n' < /proc/"$PID"/cmdline
#6
2
Have a look at my answer here. It covers what I found when trying to do this myself.
看看我的答案。它涵盖了我自己尝试这样做时发现的内容。
Edit: Have a look at this thread on debian-user for a bash script that tries its best to do what you want (look for version 3 of the script in that thread).
编辑:看看debian-user上的这个线程,找一个bash脚本,尽力做你想做的事情(在该线程中查找脚本的第3版)。
' characters. Do you know why sometimes the '
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?
我试图在Linux上拆分进程的cmdline,但似乎我不能依赖它来用'\ 0'字符分隔。你知道为什么有时'\ 0'字符被用作分隔符,有时它是一个常规空间?
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.
您是否知道检索可执行文件名称及其路径的其他方法?我一直试图用'ps'来获取这些信息,但它总是返回完整的命令行,并且可执行文件名被截断。
Thanks.
谢谢。
6 个解决方案
#1
14
use strings
使用字符串
$ cat /proc/self/cmdline | strings -1
cat
/proc/self/cmdline
#2
11
The /proc/PID/cmdline is always separated by NUL characters.
/ proc / PID / cmdline始终用NUL字符分隔。
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()?
编辑:如果你真的看到不应该有的空格,也许你的可执行文件(有意或无意)写入argv [],或使用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.
当内核启动进程时,cmdline是NUL分隔的,当你读取/ proc / PID / cmdline时,内核代码只是将进程启动时argv []的内存范围复制到输出缓冲区中。
#3
8
Use
使用
cat /proc/2634/cmdline | tr "\0" " "
to get the args separated by blanks, as you would see it on a command line.
用空格分隔args,就像你在命令行中看到的一样。
#4
4
A shot in the dark, but is it possible that \0 is separating terms and spaces are separating words within a term? For example,
在黑暗中拍摄,但是\ 0是否可以分隔术语和空格分隔术语中的单词?例如,
myprog "foo bar" baz
might appear in /proc/pid/cmdline as...
可能会出现在/ proc / pid / cmdline中...
/usr/bin/myprog\0foo bar\0baz
Complete guess here, I can't seem to find any spaces on one of my Linux boxes.
在这里完成猜测,我似乎无法在我的一个Linux机器上找到任何空格。
#5
4
The command line arguments in /proc/PID/cmdline are separated by null bytes. You can use tr to replace them by new lines:
/ proc / PID / cmdline中的命令行参数由空字节分隔。您可以使用tr替换新行:
tr '\0' '\n' < /proc/"$PID"/cmdline
#6
2
Have a look at my answer here. It covers what I found when trying to do this myself.
看看我的答案。它涵盖了我自己尝试这样做时发现的内容。
Edit: Have a look at this thread on debian-user for a bash script that tries its best to do what you want (look for version 3 of the script in that thread).
编辑:看看debian-user上的这个线程,找一个bash脚本,尽力做你想做的事情(在该线程中查找脚本的第3版)。
' character is used as separator and sometimes it is a regular space?I'm trying to split the cmdline of a process on