From 8fd3d49beeccf8b49f10cc3462c1df6cab46ebd7 Mon Sep 17 00:00:00 2001 From: ortem Date: Mon, 29 Jul 2019 12:23:52 +0300 Subject: [PATCH] DBG: Load Rust pretty-printers via RsDebugProcessConfigurator --- .../debugger/runconfig/RsLocalDebugProcess.kt | 23 +++ .../META-INF/platform-debugger-only.xml | 2 + .../runconfig/RsDebugProcessConfigurator.kt | 17 +++ .../debugger/runconfig/RsLocalDebugProcess.kt | 22 +++ .../META-INF/platform-debugger-only.xml | 5 + .../rust/debugger/runconfig/RsDebugRunner.kt | 10 +- .../runconfig/RsExtraDebugDataLoader.kt | 137 +++++++++++++++++ .../debugger/runconfig/RsLocalDebugProcess.kt | 141 ------------------ .../main/resources/META-INF/debugger-only.xml | 4 +- .../rust/cargo/runconfig/CargoRunStateBase.kt | 2 - 10 files changed, 211 insertions(+), 152 deletions(-) create mode 100644 debugger/src/191/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt create mode 100644 debugger/src/191/main/resources/META-INF/platform-debugger-only.xml create mode 100644 debugger/src/192/main/kotlin/org/rust/debugger/runconfig/RsDebugProcessConfigurator.kt create mode 100644 debugger/src/192/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt create mode 100644 debugger/src/192/main/resources/META-INF/platform-debugger-only.xml create mode 100644 debugger/src/main/kotlin/org/rust/debugger/runconfig/RsExtraDebugDataLoader.kt delete mode 100644 debugger/src/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt diff --git a/debugger/src/191/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt b/debugger/src/191/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt new file mode 100644 index 00000000000..09ab62eb534 --- /dev/null +++ b/debugger/src/191/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt @@ -0,0 +1,23 @@ +/* + * Use of this source code is governed by the MIT license that can be + * found in the LICENSE file. + */ + +package org.rust.debugger.runconfig + +import com.intellij.execution.filters.TextConsoleBuilder +import com.intellij.xdebugger.XDebugSession +import com.jetbrains.cidr.execution.RunParameters +import com.jetbrains.cidr.execution.debugger.CidrLocalDebugProcess +import org.rust.cargo.runconfig.CargoRunStateBase + +class RsLocalDebugProcess( + parameters: RunParameters, + debugSession: XDebugSession, + consoleBuilder: TextConsoleBuilder +) : CidrLocalDebugProcess(parameters, debugSession, consoleBuilder) { + + fun setupDebugSession(state: CargoRunStateBase) { + RsExtraDebugDataLoader(this, state.cargoProject).load() + } +} diff --git a/debugger/src/191/main/resources/META-INF/platform-debugger-only.xml b/debugger/src/191/main/resources/META-INF/platform-debugger-only.xml new file mode 100644 index 00000000000..164f46cfcfb --- /dev/null +++ b/debugger/src/191/main/resources/META-INF/platform-debugger-only.xml @@ -0,0 +1,2 @@ + + diff --git a/debugger/src/192/main/kotlin/org/rust/debugger/runconfig/RsDebugProcessConfigurator.kt b/debugger/src/192/main/kotlin/org/rust/debugger/runconfig/RsDebugProcessConfigurator.kt new file mode 100644 index 00000000000..6edfd0d2f41 --- /dev/null +++ b/debugger/src/192/main/kotlin/org/rust/debugger/runconfig/RsDebugProcessConfigurator.kt @@ -0,0 +1,17 @@ +/* + * Use of this source code is governed by the MIT license that can be + * found in the LICENSE file. + */ + +package org.rust.debugger.runconfig + +import com.jetbrains.cidr.execution.debugger.CidrDebugProcess +import com.jetbrains.cidr.execution.debugger.CidrDebugProcessConfigurator +import org.rust.cargo.project.model.cargoProjects + +class RsDebugProcessConfigurator : CidrDebugProcessConfigurator { + override fun configure(process: CidrDebugProcess) { + val cargoProject = process.project.cargoProjects.allProjects.firstOrNull() + RsExtraDebugDataLoader(process, cargoProject).load() + } +} diff --git a/debugger/src/192/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt b/debugger/src/192/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt new file mode 100644 index 00000000000..c866a33f5a8 --- /dev/null +++ b/debugger/src/192/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt @@ -0,0 +1,22 @@ +/* + * Use of this source code is governed by the MIT license that can be + * found in the LICENSE file. + */ + +package org.rust.debugger.runconfig + +import com.intellij.execution.filters.TextConsoleBuilder +import com.intellij.execution.runners.ExecutionEnvironment +import com.intellij.xdebugger.XDebugSession +import com.jetbrains.cidr.execution.RunParameters +import com.jetbrains.cidr.execution.debugger.CidrLocalDebugProcess +import org.rust.cargo.runconfig.CargoRunStateBase + +class RsLocalDebugProcess( + parameters: RunParameters, + debugSession: XDebugSession, + consoleBuilder: TextConsoleBuilder +) : CidrLocalDebugProcess(parameters, debugSession, consoleBuilder) { + + fun setupDebugSession(state: CargoRunStateBase) {} +} diff --git a/debugger/src/192/main/resources/META-INF/platform-debugger-only.xml b/debugger/src/192/main/resources/META-INF/platform-debugger-only.xml new file mode 100644 index 00000000000..c58a256fff5 --- /dev/null +++ b/debugger/src/192/main/resources/META-INF/platform-debugger-only.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt b/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt index eb25092c2fc..95187d2cc1d 100644 --- a/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt +++ b/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsDebugRunner.kt @@ -24,7 +24,6 @@ import com.jetbrains.cidr.toolchains.OSType import org.jetbrains.concurrency.AsyncPromise import org.rust.cargo.runconfig.CargoRunStateBase import org.rust.cargo.runconfig.RsAsyncRunner -import org.rust.debugger.settings.RsDebuggerSettings const val ERROR_MESSAGE_TITLE: String = "Debugging is not possible" @@ -41,14 +40,9 @@ class RsDebugRunner : RsAsyncRunner(DefaultDebugExecutor.EXECUTOR_ID, ERROR_MESS return XDebuggerManager.getInstance(environment.project) .startSession(environment, object : XDebugProcessConfiguratorStarter() { override fun start(session: XDebugSession): XDebugProcess = - RsLocalDebugProcess(runParameters, session, state.consoleBuilder, state::computeSysroot).apply { + RsLocalDebugProcess(runParameters, session, state.consoleBuilder).apply { ProcessTerminatedListener.attach(processHandler, environment.project) - val settings = RsDebuggerSettings.getInstance() - loadPrettyPrinters(settings.lldbRenderers, settings.gdbRenderers) - val commitHash = state.cargoProject?.rustcInfo?.version?.commitHash - if (commitHash != null) { - loadRustcSources(commitHash) - } + setupDebugSession(state) start() } diff --git a/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsExtraDebugDataLoader.kt b/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsExtraDebugDataLoader.kt new file mode 100644 index 00000000000..ed6b6f8c017 --- /dev/null +++ b/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsExtraDebugDataLoader.kt @@ -0,0 +1,137 @@ +/* + * Use of this source code is governed by the MIT license that can be + * found in the LICENSE file. + */ + +package org.rust.debugger.runconfig + +import com.intellij.notification.NotificationType +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.util.io.FileUtil +import com.intellij.openapi.util.text.StringUtil +import com.jetbrains.cidr.execution.debugger.CidrDebugProcess +import com.jetbrains.cidr.execution.debugger.backend.DebuggerCommandException +import com.jetbrains.cidr.execution.debugger.backend.DebuggerDriver +import com.jetbrains.cidr.execution.debugger.backend.gdb.GDBDriver +import com.jetbrains.cidr.execution.debugger.backend.lldb.LLDBDriver +import org.rust.cargo.project.model.CargoProject +import org.rust.cargo.project.settings.toolchain +import org.rust.cargo.runconfig.command.workingDirectory +import org.rust.debugger.* +import org.rust.debugger.settings.RsDebuggerSettings +import org.rust.ide.notifications.showBalloon +import java.nio.file.InvalidPathException + +class RsExtraDebugDataLoader(private val process: CidrDebugProcess, cargoProject: CargoProject?) { + private val settings = RsDebuggerSettings.getInstance() + private val project = process.project + private val toolchain = process.project.toolchain + private val threadId = process.currentThreadId + private val frameIndex = process.currentFrameIndex + + private val commitHash = cargoProject?.rustcInfo?.version?.commitHash + + private val sysroot: String? by lazy { + cargoProject?.workingDirectory?.let { toolchain?.getSysroot(it) } + } + + fun load() { + process.postCommand { driver -> + try { + driver.loadRustcSources() + driver.loadPrettyPrinters() + } catch (e: DebuggerCommandException) { + process.printlnToConsole(e.message) + LOG.warn(e) + } catch (e: InvalidPathException) { + LOG.warn(e) + } + } + } + + private fun DebuggerDriver.loadRustcSources() { + if (commitHash == null) return + + val sysroot = checkSysroot(sysroot, "Cannot load rustc sources") ?: return + val sourceMapCommand = when (this) { + is LLDBDriver -> "settings set target.source-map" + is GDBDriver -> "set substitute-path" + else -> return + } + val rustcHash = "/rustc/$commitHash/".systemDependentAndEscaped() + val rustcSources = "$sysroot/lib/rustlib/src/rust/".systemDependentAndEscaped() + val fullCommand = """$sourceMapCommand "$rustcHash" "$rustcSources" """ + executeConsoleCommand(threadId, frameIndex, fullCommand) + } + + private fun DebuggerDriver.loadPrettyPrinters() { + when (this) { + is LLDBDriver -> loadPrettyPrinters() + is GDBDriver -> loadPrettyPrinters() + } + } + + private fun LLDBDriver.loadPrettyPrinters() { + when (settings.lldbRenderers) { + LLDBRenderers.COMPILER -> { + val sysroot = checkSysroot(sysroot, "Cannot load rustc renderers") ?: return + val path = "$sysroot/lib/rustlib/etc/lldb_rust_formatters.py".systemDependentAndEscaped() + executeConsoleCommand(threadId, frameIndex, """command script import "$path" """) + executeConsoleCommand(threadId, frameIndex, """type summary add --no-value --python-function lldb_rust_formatters.print_val -x ".*" --category Rust""") + executeConsoleCommand(threadId, frameIndex, """type category enable Rust""") + } + + LLDBRenderers.BUNDLED -> { + val path = PP_PATH.systemDependentAndEscaped() + executeConsoleCommand(threadId, frameIndex, """command script import "$path/$LLDB_LOOKUP.py" """) + executeConsoleCommand(threadId, frameIndex, """type synthetic add -l $LLDB_LOOKUP.synthetic_lookup -x ".*" --category Rust""") + executeConsoleCommand(threadId, frameIndex, """type summary add -F $LLDB_LOOKUP.summary_lookup -e -x -h ".*" --category Rust""") + executeConsoleCommand(threadId, frameIndex, """type category enable Rust""") + } + + LLDBRenderers.NONE -> { + } + } + } + + private fun GDBDriver.loadPrettyPrinters() { + when (settings.gdbRenderers) { + GDBRenderers.COMPILER -> { + val sysroot = checkSysroot(sysroot, "Cannot load rustc renderers") ?: return + val path = "$sysroot/lib/rustlib/etc".systemDependentAndEscaped() + // Avoid multiline Python scripts due to https://youtrack.jetbrains.com/issue/CPP-9090 + val command = """python """ + + """sys.path.insert(0, "$path"); """ + + """import gdb_rust_pretty_printing; """ + + """gdb_rust_pretty_printing.register_printers(gdb); """ + executeConsoleCommand(threadId, frameIndex, command) + } + + GDBRenderers.BUNDLED -> { + val path = PP_PATH.systemDependentAndEscaped() + val command = """python """ + + """sys.path.insert(0, "$path"); """ + + """import $GDB_LOOKUP; """ + + """$GDB_LOOKUP.register_printers(gdb); """ + executeConsoleCommand(threadId, frameIndex, command) + } + + GDBRenderers.NONE -> { + } + } + } + + private fun checkSysroot(sysroot: String?, message: String): String? { + if (sysroot == null) { + project.showBalloon(message, NotificationType.WARNING) + } + return sysroot + } + + private fun String.systemDependentAndEscaped(): String = + StringUtil.escapeStringCharacters(FileUtil.toSystemDependentName(this)) + + companion object { + private val LOG: Logger = Logger.getInstance(RsExtraDebugDataLoader::class.java) + } +} diff --git a/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt b/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt deleted file mode 100644 index 43d2e8e49d8..00000000000 --- a/debugger/src/main/kotlin/org/rust/debugger/runconfig/RsLocalDebugProcess.kt +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Use of this source code is governed by the MIT license that can be - * found in the LICENSE file. - */ - -package org.rust.debugger.runconfig - -import com.intellij.execution.filters.TextConsoleBuilder -import com.intellij.notification.NotificationType -import com.intellij.openapi.diagnostic.Logger -import com.intellij.openapi.util.io.FileUtil -import com.intellij.openapi.util.text.StringUtil -import com.intellij.xdebugger.XDebugSession -import com.jetbrains.cidr.execution.RunParameters -import com.jetbrains.cidr.execution.debugger.CidrLocalDebugProcess -import com.jetbrains.cidr.execution.debugger.backend.DebuggerCommandException -import com.jetbrains.cidr.execution.debugger.backend.gdb.GDBDriver -import com.jetbrains.cidr.execution.debugger.backend.lldb.LLDBDriver -import org.rust.debugger.* -import org.rust.ide.notifications.showBalloon -import java.nio.file.InvalidPathException - -class RsLocalDebugProcess( - parameters: RunParameters, - debugSession: XDebugSession, - consoleBuilder: TextConsoleBuilder, - computeSysroot: () -> String? -) : CidrLocalDebugProcess(parameters, debugSession, consoleBuilder) { - - private val sysroot: String? by lazy(computeSysroot) - - private fun checkSysroot(sysroot: String?, message: String): String? { - if (sysroot == null) { - project.showBalloon(message, NotificationType.WARNING) - } - return sysroot - } - - fun loadPrettyPrinters(lldbRenderers: LLDBRenderers, gdbRenderers: GDBRenderers) { - postCommand { driver -> - when (driver) { - is LLDBDriver -> driver.loadPrettyPrinters(currentThreadId, currentFrameIndex, lldbRenderers) - is GDBDriver -> driver.loadPrettyPrinters(currentThreadId, currentFrameIndex, gdbRenderers) - } - } - } - - fun loadRustcSources(commitHash: String) { - postCommand { driver -> - val sysroot = checkSysroot(sysroot, "Cannot load rustc sources") ?: return@postCommand - val sourceMapCommand = when (driver) { - is LLDBDriver -> "settings set target.source-map" - is GDBDriver -> "set substitute-path" - else -> return@postCommand - } - val rustcHash = "/rustc/$commitHash/".systemDependentAndEscaped() - val rustcSources = "$sysroot/lib/rustlib/src/rust/".systemDependentAndEscaped() - val fullCommand = """$sourceMapCommand "$rustcHash" "$rustcSources" """ - driver.executeConsoleCommand(currentThreadId, currentFrameIndex, fullCommand) - } - } - - private fun LLDBDriver.loadPrettyPrinters(threadId: Long, frameIndex: Int, renderers: LLDBRenderers) { - when (renderers) { - LLDBRenderers.COMPILER -> { - val sysroot = checkSysroot(sysroot, "Cannot load rustc renderers") ?: return - val path = "$sysroot/lib/rustlib/etc/lldb_rust_formatters.py".systemDependentAndEscaped() - try { - executeConsoleCommand(threadId, frameIndex, """command script import "$path" """) - executeConsoleCommand(threadId, frameIndex, """type summary add --no-value --python-function lldb_rust_formatters.print_val -x ".*" --category Rust""") - executeConsoleCommand(threadId, frameIndex, """type category enable Rust""") - } catch (e: DebuggerCommandException) { - printlnToConsole(e.message) - LOG.warn(e) - } - } - - LLDBRenderers.BUNDLED -> { - val path = PP_PATH.systemDependentAndEscaped() - try { - executeConsoleCommand(threadId, frameIndex, """command script import "$path/$LLDB_LOOKUP.py" """) - executeConsoleCommand(threadId, frameIndex, """type synthetic add -l $LLDB_LOOKUP.synthetic_lookup -x ".*" --category Rust""") - executeConsoleCommand(threadId, frameIndex, """type summary add -F $LLDB_LOOKUP.summary_lookup -e -x -h ".*" --category Rust""") - executeConsoleCommand(threadId, frameIndex, """type category enable Rust""") - } catch (e: DebuggerCommandException) { - printlnToConsole(e.message) - LOG.warn(e) - } catch (e: InvalidPathException) { - LOG.warn(e) - } - } - - LLDBRenderers.NONE -> { - } - } - } - - private fun GDBDriver.loadPrettyPrinters(threadId: Long, frameIndex: Int, renderers: GDBRenderers) { - when (renderers) { - GDBRenderers.COMPILER -> { - val sysroot = checkSysroot(sysroot, "Cannot load rustc renderers") ?: return - val path = "$sysroot/lib/rustlib/etc".systemDependentAndEscaped() - // Avoid multiline Python scripts due to https://youtrack.jetbrains.com/issue/CPP-9090 - val command = """python """ + - """sys.path.insert(0, "$path"); """ + - """import gdb_rust_pretty_printing; """ + - """gdb_rust_pretty_printing.register_printers(gdb); """ - try { - executeConsoleCommand(threadId, frameIndex, command) - } catch (e: DebuggerCommandException) { - printlnToConsole(e.message) - LOG.warn(e) - } - } - - GDBRenderers.BUNDLED -> { - val path = PP_PATH.systemDependentAndEscaped() - val command = """python """ + - """sys.path.insert(0, "$path"); """ + - """import $GDB_LOOKUP; """ + - """$GDB_LOOKUP.register_printers(gdb); """ - try { - executeConsoleCommand(threadId, frameIndex, command) - } catch (e: DebuggerCommandException) { - printlnToConsole(e.message) - LOG.warn(e) - } - } - - GDBRenderers.NONE -> { - } - } - } - - private fun String.systemDependentAndEscaped(): String = - StringUtil.escapeStringCharacters(FileUtil.toSystemDependentName(this)) - - companion object { - private val LOG: Logger = Logger.getInstance(RsLocalDebugProcess::class.java) - } -} diff --git a/debugger/src/main/resources/META-INF/debugger-only.xml b/debugger/src/main/resources/META-INF/debugger-only.xml index 8891425ff15..6e3fef98515 100644 --- a/debugger/src/main/resources/META-INF/debugger-only.xml +++ b/debugger/src/main/resources/META-INF/debugger-only.xml @@ -1,4 +1,6 @@ - + + + diff --git a/src/main/kotlin/org/rust/cargo/runconfig/CargoRunStateBase.kt b/src/main/kotlin/org/rust/cargo/runconfig/CargoRunStateBase.kt index bf065f531c9..9d3fb89ad46 100644 --- a/src/main/kotlin/org/rust/cargo/runconfig/CargoRunStateBase.kt +++ b/src/main/kotlin/org/rust/cargo/runconfig/CargoRunStateBase.kt @@ -56,8 +56,6 @@ abstract class CargoRunStateBase( fun cargo(): Cargo = toolchain.cargoOrWrapper(workingDirectory) - fun computeSysroot(): String? = workingDirectory?.let { toolchain.getSysroot(it) } - fun rustVersion(): RustToolchain.VersionInfo = toolchain.queryVersions() fun prepareCommandLine(): CargoCommandLine {