Skip to content

Instantly share code, notes, and snippets.

@msakamoto-sf
Created February 15, 2014 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msakamoto-sf/9014695 to your computer and use it in GitHub Desktop.
Save msakamoto-sf/9014695 to your computer and use it in GitHub Desktop.
JNA(Java Native Access) and Linux 2.6 (x86_64) system call example #1 using Groovy.
/*
* JNA and Linux 2.6 (x86_64) system call example #1.
* (no copyright, license-free, AS-IS, for any commercial or oss or free source code)
*/
@Grapes(
@Grab(group='net.java.dev.jna', module='jna', version='4.0.0')
)
import com.sun.jna.*
/** @see /usr/include/asm/unistd_64.h */
interface SYSCALL64 {
final int getpid = 39;
final int getppid = 110;
final int gettid = 186;
}
interface GLIBC extends Library {
void printf(String format, Object... args);
int getpid();
int getppid();
int syscall(int number, Object... args);
}
class Syscall {
final GLIBC glibc
Syscall(GLIBC glibc) {
this.glibc = glibc
}
int getpid() {
return this.glibc.syscall(SYSCALL64.getpid)
}
int getppid() {
return this.glibc.syscall(SYSCALL64.getppid)
}
int gettid() {
return this.glibc.syscall(SYSCALL64.gettid)
}
}
GLIBC glibc = (GLIBC)Native.loadLibrary('c', GLIBC.class)
println glibc.dump();
println glibc.inspect();
glibc.printf("Hello, %s World!\n", "JNA")
Syscall sc = new Syscall(glibc)
int current_ntid = sc.gettid()
println 'gettid() = ' + current_ntid
println 'now, try : strace -e trace=getpid,getppid -p ' + current_ntid
def c = System.console()
c.readLine('continue?')
println 'getpid() = ' + sc.getpid()
println 'getppid() = ' + sc.getppid()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment