views:

226

answers:

2

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.

+2  A: 

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.

Chuck
Looks nice. Unfortunately, I'm targeting Leopard. I've edited my question to include this.
Peter Hosey
+2  A: 

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.

Ken
`sysctl` is supported API. That looks promising; I'll let you know how it goes once I have a chance to try it out (busy with more pressing things at the moment).
Peter Hosey
The `sysctl` function is not going anywhere. The stuff in the man page isn't going anywhere. But if you do `sysctl -a`, you see a whole bunch of other stuff, including this. These are not necessarily supported.
Ken