Department of Computer Science
(Version 2.1.1 October,
Every other CPU has a machine level debugger/viewer. Why not one
for the Java Virtual Machine?
The JVMViewer simulates JVM (Java Virtual Machine)
instructions of a method in a class. It will show you how each
instruction in a method is interpreted. Well almost. Please
review the caveats below.
Its primary purpose is to debug simple programs written with the
to show what actually is happening. It also shows the execution
of (most) methods of a compiled Java program.
- Installing and running the JVMViewer
- Loading a class file
- Selecting a method
- Selecting parameters
- Stepping through
- References, source code, javadocs
Installing and running the JVMViewer
jvmem.jar and save it in the directory where you normally
keep your .class
- In Windows, or Unix/Linux,
bring up the Dos Command Prompt / UNIX Terminal and
change directory to the directory containing your .class files. Start the
JVMViewer by entering
java -cp jvmem.jar jvmem.JVMViewer
The JVMViewer is found in the jvmem
package contained in the jvmem.jar
The window should look like
- You can start the JVMViewer in other ways, depending upon your mood.
- For the person who is lazy: In Windows, double-click on the
icon. However, you will not be able to see the ouput to the
- For the adventuresome who does not like to type: Copy jvmem.jar
to the directoryContainingTheJRE\lib\ext
-- in Unix use the forward rather than the backslash --
subdirectory. Then all you have to do is
to start up the JVMViewer.
the impatient: dynamic loading of class files may take some time.
To speed up the process, include the class path to the directory
containing the .class
files that you want to look at. Remember to put a semicolon -- in
Unix, use a colon rather than a semicolon -- , but no spaces, between
entries. For example, to include the directory that the viewer is
in and the directories c:\java\jasmin and c:\eclipse, type
In Unix, it would look somwhat like (note the forward rather than the back slashes)
Loading a class
In the File submenu, Open the .class file that you want to
view. You can browse to any directory that
contains a .class
file. While clicking on a .class
file icon is preferred, you
of course, type in a class file name with or without the .class suffix.
class file will be verified by
the JVM loader. Since there is no guarantee that a jasmin program
is well behaved, and our interpreter assumes that it is, this step is
necessary. (Adding a float and an integer is not a good
idea.) The display will look like
If you want to to load a class in a package,
you must first get
to the directory where the package subdirectory is located. Then
type in packageName.ClassName.
For example, if from the directory myprogs you can execute java myPackage.myClass, then,
to load this class, you would enter in myPackage.myClass for the file
name while in the
From the Method submenu, select a
method. You can distinguish between static methods and instance
methods, since the latter are highlighted. The screen will then
the object code, the stack size, and the maximum number of local
variables. This is shown in the beginning of the first
- Instance methods must have an object associated with it.
The JVMViewer will create an object; it will be passed in as the first
parameter. Note: at this time, objects must have the default
constructor (the one with no parameters).
- Required data for the parameters, if any, can be passed by
selecting from the
Parameters submenu. A dialog will prompt you for the type of
- For the primitive
types int, char, float, long, double and boolean, you should have no problem passing in data. (The other
primitive types can be easily implemented. But why?)
- For the String
type, just type in the text box. Note that pressing OK without
entering any data will be the empty string "", not null. (Is there any
reason to input null?)
- For object types, a message will confirm
that the object
has been created. That is, if the class for the object has the
default constructor (the one with no parameters). It will
complain if it cannot create such an
object. Thus, we cannot interpret methods that pass an File object (at least for
- Arrays of int,
char, float, String, and AnyObject can
also be passed as parameters. An array of strings will be single
words. For an array of objects, a dialog box will ask for the
size of the array. The class for the object must have the default
constructor. Pressing OK without entering any array data will create an
array with 0 elements. You cannot pass null as a parameter.
The String parameter
allows us to execute main
methods. Try it.
- Once all the parameters have been filled in, the
interpreter will begin execution. The display will be similar to
the first figure. The program counter (pc)
will be a at location 0 of the instructions. The instruction to
be executed is displayed. The stack (any data
above ----------------) will
be empty. The values of the parameters will be shown in the first
local variables. Integers and floats are displayed as such.
Characters and booleans are converted to their integer values.
Longs and doubles take up two local variables; the higher variable
value is shown as --.
References to arrays and objects are shown as -> array/object. Any
local variable value that is either undefined or contains null is shown as --.
The JVMViewer will complain if it cannot pass a particular
- Press the Step button to execute a machine instruction.
After execution, the next instruction to be executed is shown.
The stack will be shown above above ---------------- and the values
of the local variables will be shown below.
- The default is to execute one machine instruction at a time,
though you may change that value. In particular you can choose
"one after another" in which the interpreter continuously executes an
instruction about once per second. You can stop this by changing
the step size to a number; in particular, selecting Execute no instructions will
- When the method completes by one of the return's, the interpreter will
show the value returned, if any. The null value is shown as --
- Since the JVMViewer is using Java's capability for reflection
(Java can call a method by naming it during execution), methods must be public so that the JVM can
find the method. If the method is not public, and we try to
interpret an invokestatic
instruction calling it, we'll get a message such as Internal error for invokestatic:
Simply make sure
that all the methods called are public.
- Most, but not all, instructions can be interpreted.
To see which ones
that aren't, run
-cp jvmem.jar jvmem.Instructions none
This will list the unimplemented instructions, such as the ever popular
If you want to see the list of instructions that are implemented, run
- This all started with the desire to write assembly code for the
Java Virtual Machine. Looking at architectures may be fine, but
writing code at a low level is an eye-opener.
- For the assembler, look at the jasmin site, or
download jasmin.zip here.
explanations of the machine instructions from the old Jasmin
site. If you can't get to it, try Sun's
- Learning to write simple code for the JVM is easy. Take a
look at my tutorial.
For more details, you can look at my notes
for the course that I taught at Montclair State.
- Source code, in a zip file.
files are in the package jvmem,
a subdirectory of the project directory jvmming.
- Javadocs for the source
There may be some interesting comments.
- Version 2.1.1:
- Turned off Method and Parameter Menu when File Open
- Fixed the creation of a new object by faking the object on the stack
- Catch verify error when loading
- Catch exceptions when calling methods
- Get/put (static) field can work with booleans
- Now compiled using Java 1.5. (Not using 1.5? Then use Version 2.0.1. jsut below)
- Version 2.0.1:
- The JVMViewer can load a .class
file from any directory. You no longer have to
specify a class path to directories.
- The JVMViewer can load classes found in packages. packageName.className must be
typed in at the File prompt while in the directory containing
the package/directory packageName.
- Most instructions have been implemented.
- Bug fix for fcmpg
and fcmpgl. (I
branched the wrong way.)
- A class can be dumped by selecting Methods/Class
- Version 1.0.1:
- A counter for the number of instructions executed is
Carl E. Bredlau
College of Science and
1 Normal Avenue
Montclair, NJ 07043