Department of Computer Science
JVMViewer
Version 3.2.0, January, 2010
This
version
uses
Java
1.6.
If you are still using Java 1.5, download Version 3.1 .
|
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
Jasmin Assembler
to show what actually is happening. It also shows the execution
of (most) methods of a compiled Java program.
Contents
- Installing and running the JVMViewer
- Loading a class file
- Selecting a method
- Selecting parameters
- Stepping through
- Caveats
- References, source code
- Changes
Installing and running the JVMViewer
- Download jvmem.jar and save it in the directory
where you normally keep your .class
files.
Be sure that file is saved as jvmem.jar. Some
browsers/OS's will want to save it as jvmem.zip.
If that happens, just rename it to jvmem.jar.
- 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
-jar
jvmem.jar
or
java
-cp
jvmem.jar
jvmem.JVMViewer
The JVMViewer is found in the jvmem
package contained in the jvmem.jar
file.
The window should look like
Problems starting up?
This program needs Java 1.6.0. If you are still using 1.5.0,
download
version 3.1.
If
you
are
using
1.4,
download
version 2.0.1.
- You can start the JVMViewer in other ways, depending upon your
mood.
- For the impatient, you can load a class file from the
command line by, for example, java -jar jvmem.jar -f
c:\PathToJavaClass\AJavaClass. (In Unix, the slashes are
forward.)
- For the person who is lazy: In the file listing window,
double-click on the
icon. However, you will not be able to see the output to the
console.
- 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
java
jvmem.JVMViewer
to start up the JVMViewer.
- You can change the size of the display font by selecting Zoom
Text or Unzoom Text from the Help submenu.
Loading a class
file
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
can type in a class file name with or without the .class suffix.
The
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 mypackage.MyClass for the file
name while in the
directory myprogs.
(For the Mac, you'll have to load the file using the -f
command line option
mentioned above;
for example: java -jar jvmem.jar -f mypackage.MyClass.)
You can see all the methods, the constant pool, and a hexadecimal dump
of the class by selecting Show Class from the Method submenu.
Selecting a
method
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
show
the object code, the stack size, and the maximum number of local
variables. This is shown in the beginning of the first
figure.
Selecting
Parameters
- 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
parameter.
- 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. If you want to
input null, just type null.
(But
expect
exceptions
to
occur
in
your
methods.)
- 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
this version).
- 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. If you want to input null, just type null.
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 or stack value that is either undefined or contains null is shown as --.
The JVMViewer will complain if it cannot pass a particular
parameter type.
Stepping Through
- 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
stop execution.
- You can set a breakpoint by entering -> <n>
where <n> is the location of an instruction. For
example, if you want to execute a program up to the instruction at
location 100, enter -> 100. After pressing Step, the
JVMViewer will execute instructions up to the breakpoint. Though you
can set many breakpoints, only the one that is displayed will be active.
- If a method calls another method in the same class, you can Step
into the called method. Another window will open with that method.
(This can be fun when doing recursion.)
- When the method completes by one of the returns, the interpreter will
show the value returned, if any. The null value is shown as --
Caveats
- Most, but not all, instructions can be interpreted.
To see which ones that aren't, run
java
-cp
jvmem.jar
jvmem.Instructions
none
This will list the unimplemented instructions, such as the ever popular
instruction monitorenter.
If
you
want
to
see
the
list
of
instructions
that
are
implemented,
run
java -cp
jvmem.jar jvmem.Instructions
- If a method needs an object, say MyObject, then MyObject
must have the null (MyObject()) constructor.
- Some "unusual" cases are not yet interpreted.
For example, calling a static method with a short parameter.
The JVMViewer will let you know about this.
- Interpreting the constructors (init methods) may be
quirky.
References and
source
code
- 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-1.1.zip here.
- An
overview of the machine instructions from the Jasmin site. Detailed
explanations of the machine instructions from Sun's site. The original
(now archived) Jasmin reference has a better interface.
- 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.
All
files are in the package jvmem,
a
subdirectory
of
the
project
directory
jvmming.
Unzip,
compile
and
create javadocs, if you wish. You'll find interesting
comments. (You can see them in an older Version 2.1.1.)
Changes
and corrections
Carl E. Bredlau
Department of
Computer Science
College of Science and
Mathematics
Montclair State
University
1 Normal Avenue
Montclair, NJ 07043