在 lldb 中将寄存器值设置为 NSString

Setting a register value to NSString in lldb

我正在尝试将寄存器值从一个 NSString 更改为另一个,当我查看寄存器时 - 在本例中 "rdx" 指向一个 NSString 对象:

(lldb) register read
General Purpose Registers:
       rax = 0x0000000000000000
       rbx = 0x0000000000000000
       rcx = 0x0000000000007d0b
       rdx = 0x00007fffb6e40860  @"Helvetica"

现在我尝试以多种方式更改它的值 - 我尝试在 "expression" 中创建一个 NSString 对象并将其设置为 $rdx:

(lldb) expression
Enter expressions, then terminate with an empty line to evaluate:
  1: NSString *abc=@"Times"; 
  2: $rdx=abc;
error: assigning to 'unsigned long' from incompatible type 'NSString *'

现在我记起 rdx 只是一个指向 NSString 对象的指针,所以我尝试获取指针值(unsigned long)并将其设置为 rdx:

(lldb) expression
Enter expressions, then terminate with an empty line to evaluate:
  1: NSString *abc=@"Times"; 
  2: $rdx=(unsigned long)&abc; 
(unsigned long) 0 = 123145406123008

现在寄存器读取只显示指针值而不是 NSString 值

(lldb) register read 
General Purpose Registers:
       rax = 0x0000000000000000
       rbx = 0x0000000000000000
       rcx = 0x0000000000007d0b
       rdx = 0x000070000e275d00

当我让程序继续运行时 - 它崩溃了...

现在我知道我遗漏了一些东西,因为 rdx 的初始值指向 NSString (0x00007fffb6e40860) - 从内存读取它时它指向另一个指针 (7fffb3d721b8,它指向垃圾数据... )

(lldb) memory read 0x00007fffb6e40860
0x7fffb6e40860: b8 21 d7 b3 ff 7f 00 00 c8 07 00 00 00 00 00 00  ?!׳?...?.......

并且在将 rdx 分配给我的 NSString 指针后读取值时 - 它确实显示了字符串值:

(lldb) memory read 0x000070000f8a3d00
0x70000f8a3d00: 55 54 69 6d 65 73 00 00 00 00 00 00 00 00 00 00  UTimes..........

如果还不够清楚 - 我的目标是设置 rdx,使其显示如下内容:

(lldb) register read
General Purpose Registers:
       rax = 0x0000000000000000
       rbx = 0x0000000000000000
       rcx = 0x0000000000007d0b
       rdx = 0x00007fffb6e40860  @"Times"

您有 2 个选择:

创建字符串,获取对它的引用,然后使用 register write

设置它
(lldb) p/x @"hello world"
(__NSCFString *) [=10=] = 0x00006000002232a0 @"hello world"
(lldb) register write rdx 0x00006000002232a0
(lldb) p/x $rdx
(unsigned long)  = 0x00006000002232a0
(lldb) po $rdx 
hello world

或者,您也可以使用表达式命令:

(lldb) p/x @"goodbye world"
(__NSCFString *)  = 0x0000604000423000 @"goodbye world"
(lldb) po $rdx = 0x0000600000223220
goodbye world

(lldb) po $rdx
goodbye world