Determine a process's architecture

architecture, macos, process

Solution

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.

Problem

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.

Original source