Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seeing all the system calls that were made by a Java program

How can I see which system calls my Java program is making? Is there a tool that will do this on Linux?

like image 987
Ragini Avatar asked Apr 30 '12 13:04

Ragini


People also ask

How can I trace system calls?

-e trace=setTrace only the specified set of system calls. The -c option is useful for determining which system calls might be useful to trace. For example, trace=open,close,read,write means to only trace those four system calls.

How can I see all processes in Java?

You can use the ps command to view running Java processes on a system also by piping output to grep . OpenJDK, however, has its very own specific process monitor. The Java Virtual Machine Process Status (jps) tool allows you to scan for each running instance of the Java Virtual Machine (JVM) on your system.

Can Java make system calls?

Yes. How does it work internally to achieve its activities? The typical pattern is that some of the methods in a Java class are labelled as native . When the JVM encounters a call to a native method it makes a call into C or C++ code that is part of the JVM executable.

What is a system call in Java?

System call provides the services of the operating system to the user programs via Application Program Interface(API). It provides an interface between a process and operating system to allow user-level processes to request services of the operating system. System calls are the only entry points into the kernel system.


2 Answers

Use strace:

strace -f java your_program

or

strace -f -p <pid of your java program>
like image 51
chrisaycock Avatar answered Sep 30 '22 21:09

chrisaycock


Use strace. But there is is trick for my case. Option -f is needed and is the same as --follow-forks. For example, the following code:

public class Foo {
    public static void main (String [] args) {
        System.out.println("XXX");    
    }
}

After running javac Foo.java to compile it, strace java Foo 2>&1 | grep write print nothing. But strace -f java Foo 2>&1 | grep write prints:

[pid 11655] write(3, "0x63", 4)         = 4
[pid 11655] write(3, "\0", 1)           = 1
[pid 11655] write(3, "\0", 1)           = 1
[pid 11655] write(3, "\0", 1)           = 1
[pid 11655] write(3, "\0", 1)           = 1
[pid 11655] write(3, "\0", 1)           = 1
[pid 11655] write(3, "\0", 1)           = 1
[pid 11655] write(3, "\0", 1)           = 1
[pid 11655] write(3, "\0", 1)           = 1
[pid 11655] write(1, "XXX", 3XXX)          = 3
[pid 11655] write(1, "\n", 1

[pid 11655] write(1, "XXX", 3XXX) = 3 shows the system call made for System.out.println("XXX").

like image 36
Jingguo Yao Avatar answered Sep 30 '22 22:09

Jingguo Yao