Sign in Join
Wiki copytemporary

FPC JVM/Debugging

Debugging help

The compiler does not catch all errors in the source code that may result in invalid JVM bytecode. Examples include uninitialized variables and some invalid typecasts. The consequence is that you will get verification errors from the JVM at run time.

If the source code is compiled with the -g option, all exception backtraces will contain source line information (except in case of include files, as explained earlier).

Additionally, even if the bytecode passes the verifier, there can still be logic bugs in your program.

Bytecode verification

The Apache Commons BCEL project includes a very handy JVM byte code verifier. Unfortunately, the current release version (5.2) is quite old and does not support all JDK 1.5 features used by FPC. You have to use the svn version instead.

Download BCEL snapshot

A prebuilt version of the patched BCEL 6.0 snapshot is included in the snapshots and can also be downloaded as part of the Java-specific utilities.

Compilation instructions and a required patch can be found at FPC JVM/Building.

Running the verifier

To run the verifier on, e.g., the system unit class org.freepascal.rtl.system

text
 java -Xmx1000000000 -cp /full/path/to/bcel/target/bcel-6.0-SNAPSHOT.jar:.:/full/path/to/fpc/rtl/units/jvm-java org.apache.bcel.verifier.Verifier org.freepascal.rtl.System

(that is for Unix systems, on Windows replace the forward slashes with backslashes, and separate directories with ; instead of with :).

On Unix systems, you can create a shell script called jverify.sh with the following contents:

text
 #!/bin/sh
 java -Xmx1000000000 -cp /full/path/to/bcel/target/bcel-6.0-SNAPSHOT.jar:.:/full/path/to/fpc/rtl/units/jvm-java org.apache.bcel.verifier.Verifier $@

This makes it possible to run it as e.g. jverify.sh org.freepascal.rtl.System. The reason for the large maximum heap size is because the verifier otherwise can run out of memory while verifying more complex classes.

The verifier will report byte code offsets rather than source code line numbers when it detects an error. To obtain the corresponding source code line number, run javap -l on the same class. It will list the translation table from byte code offsets to source code line numbers for every function in that class.

Java debuggers

Source code searching logic

All Java debuggers will look for source code in the same way as they look for classes. This means that if your class is in a package called org.freepascal.rtl, then the debugger will look for the source file containing that class in a directory structure org/freepascal/rtl that is located somewhere in the class or source directory path.

I.e., if you specify /home/me/fpc/rtl/java as a source directory, the debugger will look in /home/me/fpc/rtl/java/org/freepascal/rtl for the sources of any class in the org.freepascal.rtl package rather than in the top level directory you specified. One way to deal with this is to create symbolic links to the source code next to the class files, if your operating system supports that, or to copy the source files next to the class files.

jdb

jdb is the standard command line debugger included with the JDK. It is very primitive and requires lots of typing, but other than that works fine. Some useful commands:

Eclipse

It is undoubtedly possible to set up Eclipse so that it will debug FPC-generated Java class files. Anyone who figures out the details, feel free to describe them here.


From the Free Pascal wiki, page "FPC JVM/Debugging", as it stood on 2022-01-22 after a change by Jonas, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 2, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.