Search code examples
javamacoskerneljna

How to access Mac OS X Kernel framework (mach) using JNA


I am attempting to write a Java class to access some Mac OS X memory statistics. I have a working implementation parsing the string returned from vm_stat. However, I am trying to use JNA to access the native code.

I've dug through the vm_stat source code enough to identify host_statistics64() as the function I need to call, but I can't seem to get to the library/framework which executes that. The header files are located under /System/Library/Frameworks/Kernel.framework. However, unlike just about every other framework in that directory, there is no executable file, so the JNA code to access that framework throws a UnsatisfiedLinkError (image not found).

This is the code I'm using to attempt to access the library:

public interface Kernel extends Library {

    Kernel INSTANCE = (Kernel) Native.loadLibrary("Kernel", Kernel.class);

    // ... other code ...
}

The code works fine for a random selection of other frameworks in the directory (e.g., ApplicationServices.framework, etc.) as all of these appear to have a binary executable inside their directory structure. Except for Kernel.

I've spent a few hours on Google trying to hunt down a solution, without success. Any advice?


Solution

  • I was able to find out a command to run which tells me which libraries are loaded. For Mac OS X the command I used was:

    otool -L /usr/bin/vm_stat
    

    This produced the following output:

    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)

    So this worked like a charm:

    SystemB INSTANCE = (SystemB) Native.loadLibrary("System.B",SystemB.class);
    

    (The equivalent linux command is ldd.)