ios - 更改 LLDB 中 const 变量的值
问题描述
常量变量很好。但是,有时我想在调试时动态更改它们的值以跟踪某些特定行为。
当我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 变量分配变量。该值将像上面那样立即插入,调试信息也会记录它以供调试器显示,但内存中不会有任何东西从旧值更改为新值。
推荐阅读
- node.js - 在 Adonis.js 为什么 refreshToken 为空?
- javascript - puppeteer 打开 chrome 时无法使用 chrome 扩展
- xamarin - Xamarin 表单切换事件导致无限循环 - 组切换
- tensorflow - 如何在 Keras 中完成模型
- mysql - 用于删除重复电子邮件的数据库 SQL 查询
- powerbi - 如何通过给定参数提取列,然后在 Power BI 中汇总列总数
- java - Java中的每个请求都需要缓存能力
- swift - Swift4 中的命令行处理序列
- ios - 如何在 Swift5 上检查 App Store 中是否有我的应用程序的新版本?
- javascript - 我可以通过 Javascript 设置 HTML 视频属性吗?