在以下C代码中:
#include <stdio.h>
int main(void)
{
unsigned i = 1;
while (i)
{
i <<= 1;
}
return 0;
}我首先使用调试信息编译,如下所示:gcc -g single-step.c,然后执行lldb a.out。我想看看我在做什么,所以我做了b main和run,然后我为i:watchpoint set variable i设置了我的观察点。然而,当我这样做时,lldb似乎将一个值赋值给变量i,而它不应该这样做
Watchpoint created: Watchpoint 1: addr = 0x16fdff498 size = 4 state = enabled type = w declare @ '/Users/d1ff1cult/Documents/KUL 2021-2022/IW/oefenzitting-c/les8-debugging/examples/single-step.c:5' watchpoint spec = 'i' new value: 53472
它给了我一个看似完全随机的值,那么我做错了什么呢?
我使用CLion编写我的代码,但是这种情况发生在CLion终端和正确的macOS终端中。我也在使用M1 Mac。
提前感谢!
发布于 2021-12-07 17:29:07
尤金是对的。
在我的系统上,当我运行你的程序时,我在观察点得到一个初始值0。
我更改了程序,将代码放入函数(例如) orig中。
我创建了一个函数fill,在它的堆栈帧中循环填充长度为100的int数组。
从main,我打电话给fill,然后打给orig和b orig。然后,执行观察点命令,我得到一个值100。
因此,您的观察点是显示堆栈框架中为i分配的任何内容。即未初始化的值。
下面是我创建的测试程序:
#include <stdio.h>
#include <stdlib.h>
int
orig(void)
{
unsigned i = 1;
while (i) {
i <<= 1;
}
return 0;
}
void
fill(void)
{
unsigned x[100];
for (int i = 0; i < 100; ++i)
x[i] = rand();
}
int
main(void)
{
fill();
orig();
return 0;
}以下是调试输出:
> lldb ./fix1
(lldb) target create "./fix1"
Current executable set to './fix1' (x86_64).
(lldb) b main
Breakpoint 1: where = fix1`main + 4 at fix1.c:29, address = 0x000000000040117b
(lldb) b orig
Breakpoint 2: where = fix1`orig + 4 at fix1.c:7, address = 0x000000000040112a
(lldb) run
Process 193289 launched: '/tmp/watch/fix1' (x86_64)
Process 193289 stopped
* thread #1, name = 'fix1', stop reason = breakpoint 1.1
frame #0: 0x000000000040117b fix1`main at fix1.c:29
26 main(void)
27 {
28
-> 29 fill();
30 orig();
31
32 return 0;
(lldb) c
Process 193289 resuming
Process 193289 stopped
* thread #1, name = 'fix1', stop reason = breakpoint 2.1
frame #0: 0x000000000040112a fix1`orig at fix1.c:7
4 int
5 orig(void)
6 {
-> 7 unsigned i = 1;
8
9 while (i) {
10 i <<= 1;
(lldb) watchpoint set variable i
Watchpoint created: Watchpoint 1: addr = 0x7fffffffdd6c size = 4 state = enabled type = w
declare @ '/tmp/watch/fix1.c:7'
watchpoint spec = 'i'
new value: 100https://stackoverflow.com/questions/70264131
复制相似问题