views:

66

answers:

3

I know that I can use the terminal and the system-profiler command to determine the current bitness of the kernel but I am trying to determine if there is a way to get that same information programmatically in objective-c.

I have looked through Carbon's gestalt, but haven't seen anything that would tell me bitness of the kernel.

Does anyone have any suggestions as to how I could do this? I need this info for a debugging report that gives a snapshot of the system at the time of the report.

Thanks!

Update: One thing I have tried that is a trick I learned from the Windows world is to check the size of an int like:

sizeof(int*); //(4 = x86 8 = x64)

but I don't think this is a resolution because I think this will only give me an idea of what the actual program itself is running at and not the actual OS kernel. My understanding is that even though the OS kernel is running at 32 bit your program can still run at 64bit.

I have run across other forum posting similar to this one but none of them seem to come up with an answer other then using system_profiler.

A: 

You should be able to read the system profiler information in from command line like this:

sys_profile = popen("system_profiler -xml", "r");

See the ProfileSystem example in Apple's documentation for how to parse it.

Software/System Software Overview/64-bit Kernel and Extensions is probably the key you want.

Peter DeWeese
Peter,I'm beginning to think you are correct. I have not been able to find another way to get the information that is provided by '64-bit Kernel and Extensions'.
+2  A: 

See man 3 uname: It fills a utsname structure which includes a member machine, which is "x86_64" or "i386" on Intel platforms:

struct utsname un;
int res = uname(&un);
if (res >= 0) {
    NSLog(@"%s", un.machine);
}
Georg Fritzsche
Thanks Georg,I tried your example but was unable to get it to work. I'm sure it is a PEBKAP on my part but I can't get it to recognize the struct utsname. My feeling is that this would only give me an idea of the cpu type where what I'm looking for is the actual bitness of the current OS kernel, not the cpu capability.
@user: Use `struct utsname un;` - i'm still often thinking in C++ in that regard. And this gives you the machine type the kernel was built for which is what you are looking for.
Georg Fritzsche
Georg,I got it working and you are the winner this does give me the information I'm looking for!Thanks for your help!
+1  A: 

You could use sysctlbyname. Dig around mach/machine.h to get possible values.

#include <mach/machine.h>
#include <sys/sysctl.h>

void example() 
{
   unsigned int cpuType;   
   size_t size = sizeof(cpuType);
   sysctlbyname("hw.cputype", &cpuType, &size, NULL, 0);

   bool is64 = cpuType & CPU_ARCH_ABI64;

   const char *cpu;

   switch (cpuType) {
      case CPU_TYPE_X86:
         cpu = "x86";
         break;
      case CPU_TYPE_X86_64:
         cpu = "x86_64";
         break;
      case CPU_TYPE_POWERPC:
         cpu = "ppc";
         break;
      case CPU_TYPE_POWERPC64:
         cpu = "ppc_64";
         break;
      case CPU_TYPE_SPARC:
         cpu = "sparc";
         break;
      default:
         cpu = "unknown";
         break;
   }
}
Eric Rahm
Eric,I tried setting the bool of is64 as you describe but It always returns false. If I am reading the code correctly, this will only tell me if the physical cpu is 64 bit capable. What I would really like to know is, what bitness is the OS kernel currently running at. Is it running x86 (the default) or is it running x64?