Martin Drozdik
Martin Drozdik

Reputation: 13303

How to set a breakpoint in Kotlin require with IntelliJ debugger?

The Kotlin standard library has a neat function require which is something like a runtime assert:

@kotlin.internal.InlineOnly
public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit {
    contract {
        returns() implies value
    }
    if (!value) {
        val message = lazyMessage()
        throw IllegalArgumentException(message.toString())
    }
}

When I am debugging, I would like to be able to set a breakpoint in this function, just before the exception is thrown. Like this, I would have the entire stacktrace and local variables visible in the debugger once a requirement is broken. However, this doesn't seem to work:

enter image description here

At first I thought that this is because require is an inline function. I made an experiment with one of my inline functions and the debugger halts as expected.

As a workaround I tried to set the debugger to break on exceptions, but the framework I am working with (Spring) throws a barrage of exceptions on each application start, making it extremely tedious to ignore the irrelevant exceptions.

I would like to know how to make it work, but I am also interested about the why of "it doesn't work".

Upvotes: 2

Views: 2299

Answers (1)

goodwinnk
goodwinnk

Reputation: 3837

It's currently not possible to set breakpoints in Kotlin for functions marked with a InlineOnly annotation, and require is one of such functions. Inline functions marked with this annotation don't provide additional debug information in order to save a line from the call site untouched in stack-traces, but it also ruins setting breakpoints inside (https://youtrack.jetbrains.com/issue/KT-24306).

You have spotted one workaround - using exception breakpoints (https://www.jetbrains.com/help/idea/creating-exception-breakpoints.html). IllegalArgumentException would be the best class in this case.

If there're calls in your code that don't work, they might be replaced to custom function as another workaround.

(The answer was updated. The previous version erroneously claimed that breakpoints in require might work for some calls.)

Upvotes: 2

Related Questions