llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.98k stars 11.94k forks source link

Need more powerful configuration file support #70608

Open JetForMe opened 1 year ago

JetForMe commented 1 year ago

This may be more than one request, but I'm trying to solve this problem: I wanted to be able to invoke some Swift code to operate on a variable while stopped in Swift code. I ended up writing this in mh ~/.lldbinit file, because it doesn't support any form of line continuation or multiline command.

command regex dump 's#(.+)#expr -l Swift -- print("\(dump(data: %1))"); func dump(data inData: Data, bytesPerLine inBPL: Int = 16) -> String { let conv = String("................................ !\"\u{23}$%&\u{27}()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); var dump = ""; let maxCount = Swift.min(inData.count, 256); let bpl = inBPL > 0 ? inBPL : 8; let lines = maxCount / bpl + (maxCount % bpl > 0 ? 1 : 0); for line in 0 ..< lines { let start = line * bpl; let count = Swift.min(maxCount - start, bpl); let end = start + count; var dumpLine = String(format: "\u{25}08llx: ", start); for c in start ..< start + bpl { if c < end { dumpLine += String(format: "\u{25}02x ", inData[c]) } else { dumpLine += "   " }; if (c+1) % 8 == 0 { dumpLine += "  " } ;}; for c in start ..< end { let ch: Character; if c < conv.count { ch = conv[conv.index(conv.startIndex, offsetBy: c)] } else { ch = "." }; dumpLine.append(ch); if (c+1) % 8 == 0 { dumpLine += " " } ;}; dump += dumpLine + "\n" ;}; if inData.count > maxCount { dump += "…\(inData.count - maxCount) bytes omitted…\n" }; return dump }#'

At the very least, LLDB, lldb should support some form of line continuation character, perhaps a \ at the end of the line, or (better) something like the Swift (and Python?) multiline string literal:

"""
as many
lines
as you
need
"""

A nice addition would be to import a file into command regex, perhaps like this:

(lldb) command regex import <command name>  <regex> <path-to-file>
(lldb) command regex import dump '(.+)' /path/to/file

Where /path/to/file contains

expr -l Swift --
print("\(dump(data: %1))")
func dump(data: Data, bytesPerLine: Int = 16) -> String {
    <more Swift code>
}

The result of that would be

Best would be to directly support Swift for LLDB scripting. I realize that’s hard to do because it’s so hard to embed Swift.

llvmbot commented 1 year ago

@llvm/issue-subscribers-lldb

Author: Rick M (JetForMe)

This may be more than one request, but I'm trying to solve this problem: I wanted to be able to invoke some Swift code to operate on a variable while stopped in Swift code. I ended up writing this in mh ~/.lldbinit file, because it doesn't support any form of line continuation or multiline command. ``` command regex dump 's#(.+)#expr -l Swift -- print("\(dump(data: %1))"); func dump(data inData: Data, bytesPerLine inBPL: Int = 16) -> String { let conv = String("................................ !\"\u{23}$%&\u{27}()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); var dump = ""; let maxCount = Swift.min(inData.count, 256); let bpl = inBPL > 0 ? inBPL : 8; let lines = maxCount / bpl + (maxCount % bpl > 0 ? 1 : 0); for line in 0 ..< lines { let start = line * bpl; let count = Swift.min(maxCount - start, bpl); let end = start + count; var dumpLine = String(format: "\u{25}08llx: ", start); for c in start ..< start + bpl { if c < end { dumpLine += String(format: "\u{25}02x ", inData[c]) } else { dumpLine += " " }; if (c+1) % 8 == 0 { dumpLine += " " } ;}; for c in start ..< end { let ch: Character; if c < conv.count { ch = conv[conv.index(conv.startIndex, offsetBy: c)] } else { ch = "." }; dumpLine.append(ch); if (c+1) % 8 == 0 { dumpLine += " " } ;}; dump += dumpLine + "\n" ;}; if inData.count > maxCount { dump += "…\(inData.count - maxCount) bytes omitted…\n" }; return dump }#' ``` At the very least, LLDB, lldb should support some form of line continuation character, perhaps a `\` at the end of the line, or (better) something like the Swift (and Python?) multiline string literal: ``` """ as many lines as you need """ ``` A nice addition would be to import a file into `command regex`, perhaps like this: ``` (lldb) command regex import <command name> <regex> <path-to-file> (lldb) command regex import dump '(.+)' /path/to/file ``` Where `/path/to/file` contains ``` expr -l Swift -- print("\(dump(data: %1))") func dump(data: Data, bytesPerLine: Int = 16) -> String { <more Swift code> } ``` The result of that would be Best would be to directly support Swift for LLDB scripting. I realize that’s hard to do because it’s so hard to embed Swift.