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:
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.
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.
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:A nice addition would be to import a file into
command regex
, perhaps like this:Where
/path/to/file
containsThe 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.