10

I am trying to find process type (32 bit/ 64bit) from process pid?

I get the process information and process list from using GetBSDProcessList method described here.

how can we get the process type information? Any Ideas?

I can use defined(i386) or defined(x86_64) but only if we are in process. I am out of the process.

RLT
  • 4,219
  • 4
  • 37
  • 91

3 Answers3

14

GetBSDProcessList returns a kinfo_proc. The kinfo_proc has a kp_proc member which is of type extern_proc. The extern_proc has a p_flag member, which one of the flags is P_LP64, indicating "Process is LP64"). So you should be able to check with:

int is64bit = proc->kp_proc.p_flags & P_LP64;

(Note: As shown in the comment, you need to use the B_get_process_info found in Link:

static int
B_get_process_info(pid_t pid, struct kinfo_proc *kp)
{
    size_t bufsize      = 0;
    size_t orig_bufsize = 0;
    int    retry_count  = 0;
    int    local_error  = 0;
    int    mib[4]       = { CTL_KERN, KERN_PROC, KERN_PROC_PID, 0 };

    mib[3] = pid;
    orig_bufsize = bufsize = sizeof(struct kinfo_proc);

    for (retry_count = 0; ; retry_count++) {
        local_error = 0;
        bufsize = orig_bufsize;
        if ((local_error = sysctl(mib, 4, kp, &bufsize, NULL, 0)) < 0) {
            if (retry_count < 1000) {
                sleep(1);
                continue;
            }
            return local_error;
        } else if (local_error == 0) {
            break;
        }
        sleep(1);
    }

    return local_error;
}

)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • I tested the code, but it does not give the desired result. It is always 0. – RLT Nov 02 '11 at 17:06
  • 1
    @Rahul: What if you use the B_get_process_info found in http://osxbook.com/book/bonus/chapter8/core/download/gcore.c to get the `kinfo_proc`? (And it seems you need to run it with `sudo`.) – kennytm Nov 02 '11 at 17:09
  • 1
    Thanks, this works as expected, though the structure to proc->kp_proc.p_flags is now proc->kp_proc.p_flag as I mention here: http://stackoverflow.com/questions/19138043/check-if-any-running-binary-is-32-or-64-bit/19139034#19139034 – TheDarkKnight Oct 02 '13 at 14:17
1

Okay so I did a lot of research and figured out a better solution. Although the sysctl approach works, the documentation states it should be avoided. The method below uses libproc.h's proc_pidinfo function and works similarly to sysctl. This is obviously for Apple's platforms.

bool Is64Bit (int pid)
{
    proc_bsdshortinfo info;
    if (proc_pidinfo (pid, PROC_PIDT_SHORTBSDINFO,
        0, &info, PROC_PIDT_SHORTBSDINFO_SIZE))
        return info.pbsi_flags & PROC_FLAG_LP64;

    return false;
}
Dave
  • 7,283
  • 12
  • 55
  • 101
0

If you want to find on the terminal the processes that are 32 bit run

ps aux -oflags | grep '[01238ab]$'

All the others are 64 bit, but you could run

ps aux -oflags | grep '[4567cdef]$'