如何在IntelliJ调试器中为Kotlin的require函数设置断点?
Debugging Kotlin's
require Function to Capture Full Call Stack If you want to set a breakpoint right before Kotlin's require function throws an IllegalArgumentException (to get the full call stack during debugging), here's how to do it smoothly—especially in IntelliJ IDEA, the most common IDE for Kotlin development:
Step 1: Navigate to the require function source code
- In your project, find any usage of
require(e.g.,require(someCondition) { "Invalid input" }). - Hold
Ctrl(Windows/Linux) orCmd(Mac) and click on therequirekeyword—this will take you to the standard library's implementation of the function.- If you see decompiled code instead of the actual source, look for the "Download Sources" button at the top of the editor window. Click it to fetch the official Kotlin standard library source code (you'll need an internet connection for this).
Step 2: Set the breakpoint at the right line
Once you're viewing the source code of require:
@kotlin.internal.InlineOnly public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit { contract { returns() implies value } if (!value) { val message = lazyMessage() // Set your breakpoint here, on the line below throw IllegalArgumentException(message.toString()) } }
- Place a breakpoint on the
throwline (or immediately afterval message = lazyMessage()—either works, but thethrowline is where the exception is about to be raised).
Step 3: Debug and inspect the call stack
- Start your application in debug mode (click the bug icon in the IDE toolbar).
- When the
requirecondition fails, the debugger will pause at your breakpoint. - Look for the "Frames" panel (usually in the bottom-left or bottom-right of the IDE) to see the full call stack. This panel shows every layer of code that led to the
requirecall, from your application's entry point down to therequirefunction itself.
Note about inline functions
Since require is marked as inline, it gets expanded at compile time into the code that calls it. Don't worry though—modern IDEs like IntelliJ IDEA handle inline functions seamlessly during debugging, so your call stack will still show the full context of where require was invoked.
内容的提问来源于stack exchange,提问作者Martin Drozdik
相关产品推荐
相关产品推荐

