Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - How to get argument list of running process from a program.: (8 Items)
   
How to get argument list of running process from a program.  
I want to get command line argument list of running process, i got process name, process ID from proc file system. But i
 am not sure is there any way to get this info.

executing pidin ar would give you that so believe this info is stored somewhere.

Can anybody point me to that direction?
Re: How to get argument list of running process from a program.  
You need to communicate with /proc/<pid>/as

http://www.qnx.com/developers/docs/6.4.1/neutrino/prog/process.html

On 13/12/2010 5:32 PM, arminder singh wrote:
> I want to get command line argument list of running process, i got process name, process ID from proc file system. But
 i am not sure is there any way to get this info.
>
> executing pidin ar would give you that so believe this info is stored somewhere.
>
> Can anybody point me to that direction?
>
>
>
> _______________________________________________
>
> OSTech
> http://community.qnx.com/sf/go/post78552
>
Re: How to get argument list of running process from a program.  
Hi, you can refrence to following source code, maybe it's usful for you.

pid_t GetProcessPid(const char *process ){
    DIR *dirp;
    struct dirent *dire;
    char buffer[32];
    int fd=0, status;
    pid_t pid;
    struct dinfo_s {
        procfs_debuginfo info;
        char pathbuffer[PATH_MAX];
    } dinfo;

    printf("GetProcessId(%s)\r\n", process) ;

    if ((dirp = opendir( "/proc" )) == NULL)
    {
        closedir( dirp );
        printf( "Could not open '/proc'\n" );
        return -1;
    }

    while (1)
    {
        if ((dire = readdir( dirp )) == NULL) break;
        if (isdigit( dire->d_name[0] ))
        {
            pid = strtoul( dire->d_name, NULL, 0 );
            sprintf( buffer, "/proc/%d/as", pid );
            if (fd>0) close( fd );
            fd = open( buffer, O_RDONLY ) ;
            if (fd>0)
            {
                status = devctl( fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo), NULL ) ;
                if (status == EOK)
                {

                    printf("[%s]\r\n", basename(dinfo.info.path)) ;

                    if (!strcmp(process, basename(dinfo.info.path)))
                    {
                        if (fd>0) close( fd );
                        closedir( dirp );
                        return pid;
                    }
                }
                if (fd>0) close( fd );
            }
        }
    }
    if (fd>0) close( fd );
    closedir( dirp );
    return 0;
}
Re: How to get argument list of running process from a program.  
Since the argument list is stored in a process' stack you need to figure out where that processes stack is. You query 
the process as shown here:

http://www.qnx.com/developers/docs/6.4.1/neutrino/prog/process.html#DCMD_PROC_INFO

The stack offset is stored in my_info.initial_stack which you use to lseek() to the position in the fd (used to devctl()
 above). If you then read() 4 bytes from that position (in the same fd), you will get the equivalent of argc (so the 
argument count). There will also be that many offset values indicating where the arguments are stored - so you lseek() 
to each offset, and read() at most the length of the longest argument. The offset values are the equivalent of char **
argv (the argument list).

Say you want to get the list from pid 1234, you fd=open("/proc/1234/as"...). You then use the fd as described above 
(devctl+read+lseek...etc).

I hope that helps.
Re: How to get argument list of running process from a program.  
"There will also be that many offset values indicating where the arguments are stored - so you lseek() to each offset"

I am not finding any field corresponding to these in procfs struture.

Does any body has any idea how pidin is getting that info?  
Re: How to get argument list of running process from a program.  
Thanks Gervais Mulongoy i got that by reading from process stack
Re: How to get argument list of running process from a program.  
That is exactly how pidin does it and how I tried to describe it. I am glad you were able to get it sorted out!
Re: How to get argument list of running process from a program.  
Hi, you can refrence to following source code, maybe it's usful for you.

pid_t GetProcessPid(const char *process ){
    DIR *dirp;
    struct dirent *dire;
    char buffer[32];
    int fd=0, status;
    pid_t pid;
    struct dinfo_s {
        procfs_debuginfo info;
        char pathbuffer[PATH_MAX];
    } dinfo;

    printf("GetProcessId(%s)\r\n", process) ;

    if ((dirp = opendir( "/proc" )) == NULL)
    {
        closedir( dirp );
        printf( "Could not open '/proc'\n" );
        return -1;
    }

    while (1)
    {
        if ((dire = readdir( dirp )) == NULL) break;
        if (isdigit( dire->d_name[0] ))
        {
            pid = strtoul( dire->d_name, NULL, 0 );
            sprintf( buffer, "/proc/%d/as", pid );
            if (fd>0) close( fd );
            fd = open( buffer, O_RDONLY ) ;
            if (fd>0)
            {
                status = devctl( fd, DCMD_PROC_MAPDEBUG_BASE, &dinfo, sizeof(dinfo), NULL ) ;
                if (status == EOK)
                {

                    printf("[%s]\r\n", basename(dinfo.info.path)) ;

                    if (!strcmp(process, basename(dinfo.info.path)))
                    {
                        if (fd>0) close( fd );
                        closedir( dirp );
                        return pid;
                    }
                }
                if (fd>0) close( fd );
            }
        }
    }
    if (fd>0) close( fd );
    closedir( dirp );
    return 0;
}