Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing proc/pid/cmdline to get function parameters

Tags:

c++

linux

procfs

I'm trying to extract the parameter with which an app was called by using the data inside cmdline.

If I start an application instance like this:

myapp 1 2

and then cat the cmdline of myapp I will see something like myapp12.

I needed to extract these values and I used this piece of code to do it


pid_t proc_id = getpid();

sprintf(buf,"/proc/%i/cmdline",proc_id);

FILE * pFile;
pFile = fopen (buf,"r");
if (pFile!=NULL)
{
    fread(buf,100,100,pFile);
    cout << "PID " << proc_id << endl;
    string str = buf;
    cout << buf << endl;
    size_t found=str.find_last_of("/\\");
    cout << " file: " << str.substr(found+1) << endl;

    fclose (pFile);
}

But what I am getting is only the app name and no parameters...


Update coppied from answer:

well, my question now seems to be how do I read the cmdline file without it stopping at the first NULL character...

fopen(cmdline, "rb")

doesn't do anything else so...

like image 278
André Moreira Avatar asked Dec 07 '22 06:12

André Moreira


1 Answers

/usr/bin/strings /proc/1337/cmdline usually do the job for me.

like image 120
PypeBros Avatar answered Dec 19 '22 18:12

PypeBros