覆盖只读变量lldb swift

sba*_*row 11 lldb swift

在lldb中是否有一种方法可以覆盖只读变量.

例如,如果你有一个结构

struct Object {
    let name: String
}
Run Code Online (Sandbox Code Playgroud)

使用lldb在Xcode的断点处执行以下操作

(lldb) expression object.name = "Tom"
Run Code Online (Sandbox Code Playgroud)

会导致

error: <EXPR>:2:19: error: cannot assign to property: 'name' is a get-only property
Run Code Online (Sandbox Code Playgroud)

我完全理解为什么会发生这种情况,只是想知道在调试过程中是否有一种简单的方法可以解决这个问题?

请注意,这是在Swift&NOT Objective-C中

Aus*_*tin 5

您可以使用memory write {address}lldb 命令覆盖内存并更改字符串值。我设法一次完成一个地址,但似乎memory write可以一次性完成。

(lldb) help memory write
     Write to the memory of the process being debugged.

Syntax: memory write <cmd-options> <address> <value> [<value> [...]]

Command Options Usage:
  memory write [-f <format>] [-s <byte-size>] <address> <value> [<value> [...]]
  memory write -i <filename> [-s <byte-size>] [-o <offset>] <address> <value> [<value> [...]]

       -f <format> ( --format <format> )
            Specify a format to be used for display.

       -i <filename> ( --infile <filename> )
            Write memory using the contents of a file.

       -o <offset> ( --offset <offset> )
            Start writing bytes from an offset within the input file.

       -s <byte-size> ( --size <byte-size> )
            The size in bytes to use when displaying with the selected format.

     This command takes options and free-form arguments.  If your arguments
     resemble option specifiers (i.e., they start with a - or --), you must use
     ' -- ' between the end of the command options and the beginning of the
     arguments.
Run Code Online (Sandbox Code Playgroud)

这是一个示例(希望对 lldb 和 Swift 内部结构有更多了解的人可以提供更好的方法):

使用内存写入的示例

这显示了一次一个字节地覆盖内存。po "Tom".dataUsingEncoding(NSUTF8StringEncoding)!获取十六进制表示,用于单步遍历并覆盖 object.name 的内存。我确信有一种更简单的方法来做到这一点(在一个命令中),但我无法找出正确的参数值来实现它。

  • 一次替换所有字节的语法是`memory write &lt;address&gt; &lt;size&gt; &lt;byte&gt; &lt;byte&gt; ...`,例如`memory write 0x1000022c0 -s 1 66 6F 6F 62 61 72`... (2认同)