代码之家  ›  专栏  ›  技术社区  ›  Jonathan Beaudoin

Linux环境上的Java JNA

  •  1
  • Jonathan Beaudoin  · 技术社区  · 8 年前

    我目前正在尝试在linux系统上使用java编写内存读写器。在做了研究之后,我发现了ptrace。h具有这样的功能。

    这是我的ptrace课程

    package com.beaudoin.jmm.natives.linux;
    
    import com.sun.jna.Native;
    import com.sun.jna.NativeLibrary;
    import com.sun.jna.Pointer;
    import com.sun.jna.win32.W32APIOptions;
    
    /**
     * Created by jonathan on 06/01/16.
     */
    public class ptrace {
    
        static {
            Native.register(NativeLibrary.getInstance("/usr/include/sys/ptrace.h"));
        }
    
        public static native long ptrace (/*enum __ptrace_request*/ long request,
                                   /*pid_t*/ int pid,
                                   Pointer addr,
                                   Pointer data);
    }
    

    ptrace。h文件位于 /usr/include/sys/ptrace.h

    启动时,我收到以下错误:

    Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library '/usr/include/sys/ptrace.h': Native library (usr/include/sys/ptrace.h) not found in resource path ([file:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar, file:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar, file:/home/jonathan/ideaprojects/Java-Memory-Manipulation/build/classes/main/, file:/home/jonathan/ideaprojects/Java-Memory-Manipulation/build/resources/main/, file:/home/jonathan/.gradle/caches/modules-2/files-2.1/net.java.dev.jna/jna/4.2.1/fcc5b10cb812c41b00708e7b57baccc3aee5567c/jna-4.2.1.jar, file:/home/jonathan/.gradle/caches/modules-2/files-2.1/net.java.dev.jna/jna-platform/4.2.1/440e1b36d1b68fb0360ebf66d9544c3f9d9f15ef/jna-platform-4.2.1.jar, file:/home/jonathan/Desktop/idea-IC-143.1184.17/lib/idea_rt.jar])
    at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:277)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:403)
    at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:342)
    at com.beaudoin.jmm.natives.linux.ptrace.<clinit>(ptrace.java:14)
    at com.beaudoin.jmm.Main.main(Main.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
    

    下面是ptrace.h的教程和结构

    https://mikecvet.wordpress.com/2010/08/14/ptrace-tutorial/

    1 回复  |  直到 6 年前
        1
  •  3
  •   Bart Pelle    8 年前

    您将无法加载头文件。您需要加载一个包含函数的库文件,然后使用JNA从符号导出表加载它,并调用它。看看这是否适用于您:)

    我不确定,但我认为ptrace位于C标准库中,所以尝试从JNA加载“libc”,然后从那里解析函数。