psinfo_tsolaris的字段中不包含完整的进程名称

psinfo_t solaris does not contain full process name in its field

本文关键字:包含完 进程名 tsolaris 字段 psinfo      更新时间:2023-10-16

从solaris中的procfs.h将进程信息从psinfo数据文件(/proc/%d/psinfo(读取到结构体psinfo_t时,未在psinfo_tstruct的字段pr_fname中获取完整进程名称。

完整的psinfo_t结构定义存在于以下站点:

http://docs.oracle.com/cd/E19253-01/816-5174/6mbb98ui2/index.html

只有当进程名称小于15个字符时,我才会得到完整的进程名称。如果进程名称超过15个字符,则我只得到进程名称的前15个字符。其余字符将被截断。

我使用的代码如下:

#include <iostream>
#include <cstdlib>
#include <procfs.h>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
    // get the pid from command line
    int pid  = atoi(argv[1]);
    // create the pstatus struct from procfs
    psinfo_t info;
    char file[100];
    sprintf(file, "/proc/%d/psinfo", pid);
    ifstream in(file);
    if (in)
    {
        in.read((char*)&info, sizeof(psinfo_t));
        in.close();
        cout << "My Name: " << info.pr_fname << endl;
    }
    else
    {
        cout << "Process Not Exists!" << endl;
    }
return 0;
}

我必须从procfs文件系统中读取一些其他文件(除了psinfo(才能获得完整的进程名称吗。此外,如果我从命令行使用belwo-ps命令,那么我可以获得完整的进程名称:

ps -p 4970 -o comm

但我不想通过在代码中执行ps命令来获取进程名称。我很好奇ps二进制文件是从哪里获得进程名称的。

根据源代码:,psinfo_t结构的pr_fname字段长16字节

#define PRFNSZ      16  /* Maximum size of execed filename */

因此,它实际上将被截断为最多15个字符。

您可以从/proc/PID/map中获取实际二进制文件的名称。对于32位进程,exec'd二进制文件将映射到地址0x00010000

您可以在以下位置浏览ps的源代码:http://src.illumos.org/source/xref/illumos-gate/usr/src/cmd/ps/如果你想看看它在哪里找到数据。

Solaris 11.3 SRU 5引入了包含完整命令名的/proc/<pid>/execname,因此您可以检查该文件是否存在并在存在时使用它,否则请返回到有限的pr_fname

请参阅Solaris 11.3 SRU 5.6:ps(1(和/proc/<pid>/{cmdline,environ,execname}获取详细信息。