[英]Change value of const variables in LLDB
常量变量很好。 但是,有时我想在调试时动态更改它们的值以跟踪某些特定行为。
当我po flag = NO
我收到此错误:
错误:<用户表达式 0>:1:34: 无法使用 const 限定类型“const BOOL &”(又名“const bool &”)分配给变量“flag”
有什么解决方法吗?
您可以通过使用 const_cast 使表达式成功,但它可能不会执行您想要的操作。 例如:
(lldb) run
Process 32640 launched: '/tmp/foo' (x86_64)
Process 32640 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100003f66 foo`main at foo.c:7
4 main()
5 {
6 const int foo = 10;
-> 7 printf("%d\n", foo);
^
8 return 0;
9 }
Target 0: (foo) stopped.
(lldb) expr foo = 20
error: <user expression 0>:1:5: cannot assign to variable 'foo' with const-qualified type 'const int &'
foo = 20
~~~ ^
note: variable 'foo' declared const here
const_cast 救援:
(lldb) expr *(const_cast<int*>(&foo)) = 20
(int) $1 = 20
我们确实更改了 foo 槽中的值,如您所见:
(lldb) expr foo
(const int) $2 = 20
和:
(lldb) frame var foo
(const int) foo = 20
但是编译器可以自由地内联 const 变量的值,即使在 -O0 (*) 时它也可以自由地内联。 例如,对 printf 的调用在 x86_64 上 -O0 编译到:
-> 0x100003f66 <+22>: leaq 0x39(%rip), %rdi ; "%d\n"
0x100003f6d <+29>: movl $0xa, %esi
0x100003f72 <+34>: movb $0x0, %al
0x100003f74 <+36>: callq 0x100003f86 ; symbol stub for: printf
请注意,它不引用任何变量,它只是将 0xa 直接放入传递寄存器的第二个参数中。 所以不出所料:
(lldb) c
Process 33433 resuming
10
(*) 在更高的优化级别上,可能甚至不会在框架上为 const 变量分配变量。 该值会像上面那样立即插入,调试信息也会记录它以供调试器显示,但 memory 中不会有任何东西从旧值更改为新值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.