Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine a process's architecture

Is there a programmatic way to find out what architecture another process is running as on Mac OS X 10.5 and later?

Examining the process's image file is not a solution, as the image is likely to contain multiple architectures, and between arch(1) and the “Open in Rosetta” and “Open in 32-bit mode” checkboxes, there's no way to tell from the image alone which architecture is actually running.

like image 403
Peter Hosey Avatar asked Aug 29 '09 01:08

Peter Hosey


2 Answers

Can you use NSRunningApplication on OSes where it is available, and fall back to sysctl stuff when it isn't? I don't think sysctl stuff is supportable API the way most stuff is, but if you're only using it on old OSes you should be okay.

Try this to get the CPU type of the process:

   cpu_type_t  cpuType
   size_t      cpuTypeSize;
   int         mib[CTL_MAXNAME];
   size_t      mibLen;
      mibLen  = CTL_MAXNAME;
   err = sysctlnametomib("sysctl.proc_cputype", mib, &mibLen);
   if (err == -1) {
       err = errno;
   }
   if (err == 0) {
       assert(mibLen < CTL_MAXNAME);
       mib[mibLen] = pid;
       mibLen += 1;

       cpuTypeSize = sizeof(cpuType);
       err = sysctl(mib, mibLen, &cpuType, &cpuTypeSize, 0, 0);
       if (err == -1) {
           err = errno;
       }
   }

And test CPU_ARCH_ABI64 to check for 64-bit.

like image 141
Ken Avatar answered Oct 04 '22 00:10

Ken


You don't say what your requirements are, but the NSRunningApplication class introduced in 10.6 offers a really easy interface for this. The docs are currently a little off, but it is there.

like image 45
Chuck Avatar answered Oct 04 '22 00:10

Chuck