Debugging Polyglot Applications on GraalVM in VS Code

Martin Balín
graalvm
Published in
3 min readJun 23, 2021

GraalVM provides users with the unique ability to mix languages and create polyglot solutions. However, it can be a challenge to debug such programs using traditional IDEs. To support this, GraalVM offers true Polyglot debugging capabilities in its own GraalVM Extension Pack for VS Code. After installing it, you can debug not only GraalVM supported guest languages using the Debug Adapter protocol or Chrome Inspector, but also Java programs combined with GraalVM guest languages.

Get Ready

Assuming you have VSCode installed, go to the Extensions activity panel, search for “GraalVM Extension Pack”. Installing it will automatically download and install all three of the extensions it includes.

GraalVM Extension Pack

Polyglot debugging only works with GraalVM, so make sure a GraalVM install is configured. It can be installed using the VSCode Gr activity panel either by selecting Download or by choosing the existing GraalVM installation from your disk. Set just installed GraalVM as “Active GraalVM installation” when asked. This way VSCode will use this GraalVM installation as its Java platform internally.

GraalVM “Gr” activity panel in VSCode.

To test it, navigate to GraalVM Demos on GitHub, and clone the sample polyglot-debug. This sample is Java & JavaScript, but any other scripting language supported by GraalVM will work as well.

Debugging

After cloning the sample project, open the polyglot-debug folder using File | Open Folder.

Open the main class Java file PolyglotDebug.javaand/or the fib.js file located in resources/org/graalvm/demos.

Place the breakpoint either in the Java file or JS file.

Go to Run and Debug or simply press F5. Choose “Launch Java 8+ underneath “powered by Apache NetBeans Language Server,” as this is the Polyglot debugger provided by GraalVM extensions. You need to run your polyglot program with this launch configuration since other Java launch configs don’t support polyglot debugging.

GraalVMs “Launch Java 8+ App” configuration for polyglot debugging.

It is possible to step through Java code as well as JS code (in this sample), watch variables in both languages, place breakpoints, etc. It works as expected, like any real debugger.

Project Structure

In this case there is one Java file which calls a function from JavaScript. The JS file itself is placed in the resources folder so it is packaged by Maven and available for GraalVM to be executed at any time. This approach, when JS is in a separate file, offers better clarity when debugging JS. It is possible to place JS code inline inside Java, but then it is harder to place a breakpoint or step into such code.

Inlining JS could look like this:

Context ctx = Context.create();ctx.eval("js","function fib(x) {\n" +                   "if (x <= 2) {\n" +                       "return 1;\n" +                   "}\n" +                   "let fibX1 = fib(x - 1);\n" +                   "let fibX2 = fib(x - 2);\n" +                   "return fibX1 + fibX2;\n" +               "}");

The advantage of \nis that Truffle then displays the script with newlines, making debugging easier than in the case of inlined script.

GraalVM VSCode extensions work with Maven and Gradle projects.

Conclusion

Our goal is to make complex things simple using GraalVM and the VSCode extension. Give it a try: https://marketplace.visualstudio.com/items?itemName=oracle-labs-graalvm.graalvm-pack

We always welcome feedback and feature requests on Github, Slack, or Twitter. Stay tuned for the next GraalVM debugger story and more!

--

--

Martin Balín
graalvm
Writer for

GraalVM Tools manager with a long experience in IDE development, namely NetBeans.