简体   繁体   English

如何从JDK(SocketChannelImpl和SocketDispatcher)更改Java本机代码(C实现)并重新编译以与JDK一起使用?

[英]How to change Java native code (C implementation) from the JDK (SocketChannelImpl and SocketDispatcher) and recompile to use with the JDK?

I need to locate, recompile and deploy on my JDK running on Linux the following native methods below, from SocketDispatcher.java : (Native methods are on the bottom of the code below, so scroll it down please) 我需要从SocketDispatcher.java以下本地方法,在Linux上运行的JDK上进行定位,重新编译和部署SocketDispatcher.java本地方法位于下面代码的底部,请向下滚动)

package sun.nio.ch;

import java.io.*;

/**
 * Allows different platforms to call different native methods
 * for read and write operations.
 */

class SocketDispatcher extends NativeDispatcher
{

    static {
        IOUtil.load();
    }

    int read(FileDescriptor fd, long address, int len) throws IOException {
        return read0(fd, address, len);
    }

    long readv(FileDescriptor fd, long address, int len) throws IOException {
        return readv0(fd, address, len);
    }

    int write(FileDescriptor fd, long address, int len) throws IOException {
        return write0(fd, address, len);
    }

    long writev(FileDescriptor fd, long address, int len) throws IOException {
        return writev0(fd, address, len);
    }

    void preClose(FileDescriptor fd) throws IOException {
        preClose0(fd);
    }

    void close(FileDescriptor fd) throws IOException {
        close0(fd);
    }

    //-- Native methods
    static native int read0(FileDescriptor fd, long address, int len)
        throws IOException;

    static native long readv0(FileDescriptor fd, long address, int len)
        throws IOException;

    static native int write0(FileDescriptor fd, long address, int len)
        throws IOException;

    static native long writev0(FileDescriptor fd, long address, int len)
        throws IOException;

    static native void preClose0(FileDescriptor fd) throws IOException;

    static native void close0(FileDescriptor fd) throws IOException;
}

Source: http://grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/nio/ch/SocketDispatcher.java/?v=source 来源: http : //grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/sun/nio/ch/SocketDispatcher.java/?v= source

So basically I want to make the native method write0 print "Hello from native write0!". 因此,基本上我想使本机方法write0打印“ Hello from native write0!”。

Questions: 问题:

  • Where is the C/C++ code for write0 for Linux? Linux的write0的C / C ++代码在哪里?

  • How do I recompile (gcc command-line?) the C/C++ code for write0 on Linux? 如何在Linux上重新编译(gcc命令行?) write0的C / C ++代码?

  • How do I start my JVM and make it use my new compiled native code for write0 on Linux? 如何在Linux上启动JVM并使它使用我的新编write0机代码进行write0

There is no native SocketDispatcher implementation on Linux. Linux上没有本地SocketDispatcher实现。 It is Windows-specific class; 它是Windows特定的类; on Linux it just shares the implementation with FileDispatcherImpl . 在Linux上,它仅与FileDispatcherImpl 共享实现

The native code for FileDispatcherImpl is in src/solaris/native/sun/nio/ch/FileDispatcherImpl.c (in OpenJDK solaris directory stands for POSIX code, so Linux implementation is also there). FileDispatcherImpl的本地代码在src / solaris / native / sun / nio / ch / FileDispatcherImpl.c中 (在OpenJDK solaris目录中代表POSIX代码,因此也有Linux实现)。

If you want to override native methods, you have to create a shared library with your own implementation. 如果要覆盖本机方法,则必须使用自己的实现创建一个共享库。 Native method names follow JNI conventions , eg the native function for FileDispatherImpl.write0 should have the following signature: 本机方法名称遵循JNI约定 ,例如FileDispatherImpl.write0的本机函数应具有以下签名:

JNIEXPORT jint JNICALL
Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz,
                              jobject fdo, jlong address, jint len)

Once you've built your own shared library with one or more functions overriden, preload it with LD_PRELOAD , and JVM will link your functions before any native code from JDK. 构建覆盖了一个或多个函数的共享库后,请使用LD_PRELOAD对其进行预加载,然后JVM将在JDK的任何本机代码之前链接您的函数。 By the way, JDK natives for FileDispatcherImpl and other java.nio stuff comes in libnio.so . 顺便说一句, FileDispatcherImpl和其他java.nio东西的JDK本机出现在libnio.so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM