-
Notifications
You must be signed in to change notification settings - Fork 379
/
RsLocalDebugProcess.kt
141 lines (126 loc) · 6.16 KB
/
RsLocalDebugProcess.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* 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)
}
}