Run lldb command in the running lldb instance

Answered

I want to implement a CLion plugin that runs a custom command in the current lldb instance. How can I access the running lldb instance from Kotlin?

0
1 comment

Hi!

First of all, please keep in mind that there's no official public API for CLion-specific parts. While this should work, things can change under the hood in a way that could make third-party integrations break in the future. We try not to break things without a real need, but can't guarantee that it won't happen, alas.

Having said that, and given that you want to implement an action, or you have other means to retrieve the current debug session, this could look roughly like this:

// I assume you have a service bound to your plugin's lifecycle already defined somewhere
@Service(Service.Level.PROJECT)
class MyAwesomePluginService(val project: Project,
                             internal val coroutineScope: CoroutineScope) {
  companion object {
    @JvmStatic
    fun getInstance(project: Project): MyAwesomePluginService = project.service()
  }
}

class ExecuteCustomNativeDebuggerConsoleAction : DumbAwareAction() {
  override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT

  override fun update(e: AnActionEvent) {
    super.update(e)
    val debugProcess = DebuggerUIUtil.getSession(e)?.debugProcess as? CidrDebugProcess
    e.presentation.isVisible = (debugProcess != null)
    e.presentation.isEnabled = debugProcess?.driverName == "LLDB"
  }

  override fun actionPerformed(e: AnActionEvent) {
    val session = DebuggerUIUtil.getSession(e) ?: return
    val debugProcess = session.debugProcess as? CidrDebugProcess ?: return
    val threadId = (session.currentStackFrame as? CidrStackFrame)?.threadId ?: return
    val frameIndex = (session.currentStackFrame as? CidrStackFrame)?.frameIndex ?: return

    MyAwesomePluginService.getInstance(session.project).coroutineScope.launch {
      @NonNls val output = debugProcess.debuggerCommandExecutor.executeCommand(canExecuteWhileRunning = false) { driver ->
        driver.executeInterpreterCommand(threadId, frameIndex, "script print \"hello\"")
      }
      withContext(Dispatchers.EDT + ModalityState.any().asContextElement()) {
        session.reportMessage(output, MessageType.INFO)
      }
    }
  }
}
0

Please sign in to leave a comment.