C 使用lldb调试我的代码时如何输入值

C 使用lldb调试我的代码时如何输入值,c,lldb,C,Lldb,当我用lldb调试我的程序时,我在主函数中设置了一个断点,为什么它会直接结束?此外,终端应等待我的输入 函数c #包括“func.h” 空心插入头(pnode*phead,pnode*ptail,int i){ pnode pnew=(pnode)calloc(1,sizeof(node)); pnew->num=i; 如果(phead==NULL){ *phead=pnew; *ptail=pnew; }否则{ pnew->pnext=*phead; *phead=pnew; } } 无效打印

当我用lldb调试我的程序时,我在主函数中设置了一个断点,为什么它会直接结束?此外,终端应等待我的输入

函数c

#包括“func.h”
空心插入头(pnode*phead,pnode*ptail,int i){
pnode pnew=(pnode)calloc(1,sizeof(node));
pnew->num=i;
如果(phead==NULL){
*phead=pnew;
*ptail=pnew;
}否则{
pnew->pnext=*phead;
*phead=pnew;
}
}
无效打印列表(pnode phead){
while(phead){
printf(“%d”,phead->num);
phead=phead->pnext;
}
}
main.cpp

#包括“func.h”
int main()
{
pnode phead,ptail;//创建新的头部起始点,指向sturct
phead=ptail=NULL;
int i;
while(scanf(“%d”,&i)!=EOF){
插入_头(&phead,&ptail,i);
}
打印列表(phead);
返回0;
}
函数h

#pragma一次
#包括
#包括
//定义结构体
类型定义结构节点{
int-num;
结构节点*pnext;
}节点,*pnode;
//头插法
空心插入头(pnode*phead,pnode*ptail,int i);
无效打印列表(pnode phead);
你可以看到图片,我想弄清楚这个,请帮助我,谢谢大家

对于您的
lldb./test
,根据卓越的@JimIngham备注,lldb可以在程序执行时捕获用户输入(而不是在断点处停止)

对于具有终端UI的更复杂程序,单独的终端窗口(一个用于lldb,一个用于您的程序)可能更方便

要使用后一种方法,请首先在终端中运行
/test
程序,然后等待用户通过
scanf
输入

运行另一个终端窗口并启动

lldb -n "test"
将根据其名称附加到正在运行的进程

或者,您也可以在流程启动时附加

lldb -n "test" --wait-for

and in another terminal window
./test

这允许你调试你的
main
,并对你的程序做任何你想做的事情(包括提供用户输入)。

对于你的
lldb./test
,按照卓越的@JimIngham备注,lldb可以在程序执行时捕获用户输入(而不是在断点处停止)

对于具有终端UI的更复杂程序,单独的终端窗口(一个用于lldb,一个用于您的程序)可能更方便

要使用后一种方法,请首先在终端中运行
/test
程序,然后等待用户通过
scanf
输入

运行另一个终端窗口并启动

lldb -n "test"
将根据其名称附加到正在运行的进程

或者,您也可以在流程启动时附加

lldb -n "test" --wait-for

and in another terminal window
./test

这允许您调试
main
,并对程序执行任何您想要的操作(包括提供用户输入)。

在上面的示例中,首先,看起来您没有使用调试信息构建代码(将
-g
传递给编译器调用,并确保没有剥离二进制文件)。这就是为什么当您在main中点击断点时,您只看到一些反汇编,而没有看到源代码

如果您有调试信息,那么当您的程序到达main的断点时,lldb将显示您在main的开始处停止,在您的程序调用
scanf
查询输入之前。您应该能够在lldb中发出
continue
命令,程序将继续执行scanf调用并等待您的输入

例如,这个(公认的可怕代码)在lldb下工作:

> cat scant.c
#include <stdio.h>

int
main()
{
  int i;
  int buffer[2000];
  int idx = 0;
  while(scanf("%d", &i) != EOF) {
    buffer[idx++] = i;
  }
  for(i = 0; i < idx; i++)
    printf("%d: %d\n", i, buffer[i]);
  return 0;
}
> clang -g -O0 scanit.c -o scanit
> lldb scanit
(lldb) target create "scanit"
Current executable set to '/tmp/scanit' (x86_64).
(lldb) break set -n main
Breakpoint 1: where = scanit`main + 41 at scanit.c:8:7, address = 0x0000000100000e89
(lldb) run
Process 74926 launched: '/tmp/scanit' (x86_64)
Process 74926 stopped
* thread #1 tid = 0x71d134 , queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100000e89 scanit`main at scanit.c:8
    5       {
    6         int i;
    7         int buffer[2000];
 -> 8         int idx = 0;
                      ^
    9         while(scanf("%d", &i) != EOF) {
   10       buffer[idx++] = i;
   11     }
Target 0: (scanit) stopped.
(lldb) c
Process 74926 resuming
10 20 30 40 ^D
0: 10
1: 20
2: 30
3: 40
Process 74926 exited with status = 0 (0x00000000) 
(lldb)
>cat scant.c
#包括
int
main()
{
int i;
int缓冲区[2000];
int-idx=0;
while(scanf(“%d”,&i)!=EOF){
缓冲区[idx++]=i;
}
对于(i=0;i叮当声-g-o0scanit.c-oscanit
>lldb扫描
(lldb)目标创建“scanit”
当前可执行文件设置为“/tmp/scanit”(x86_64)。
(lldb)断开装置-n干管
断点1:where=scanit`main+41位于scanit.c:8:7,地址=0x0000000100000e89
(lldb)运行
启动进程74926:“/tmp/scanit”(x86_64)
进程74926已停止
*线程#1 tid=0x71d134,队列='com.apple.main thread',停止原因=断点1.1
帧#0:0x0000000100000e89 scanit `主扫描点位于scanit.c:8
5       {
6国际一级;
7整数缓冲区[2000];
->8 int idx=0;
^
9 while(scanf(“%d”,&i)!=EOF){
10缓冲区[idx++]=i;
11     }
目标0:(扫描)已停止。
(lldb)c
进程74926恢复
10203040^D
0: 10
1: 20
2: 30
3: 40
进程74926退出,状态为0(0x00000000)
(lldb)
这样,当程序运行时,它正确地从终端获取输入,并将其提供给
scanf
调用


从我所看到的情况来看,造成混乱的原因是您没有使用调试信息构建程序,因此当您在初始断点处停止时,您没有意识到您还没有调用scanf。

在上面所示的示例中,首先,看起来您没有使用调试信息构建代码(将
-g
传递给编译器调用,并确保您没有剥离二进制文件)。这就是为什么当您在main中点击断点时,只会看到一些反汇编,而不会看到源代码

如果你有调试信息,那么当你的程序在main上点击断点时,lldb会显示你在main的开头被停止,在你的程序调用
scanf
来查询输入之前。你应该能够在lldb中发出
continue
命令,你的程序将继续进行scanf调用并等待f或者你输入

例如,这个(公认的可怕代码)在lldb下工作:

> cat scant.c
#include <stdio.h>

int
main()
{
  int i;
  int buffer[2000];
  int idx = 0;
  while(scanf("%d", &i) != EOF) {
    buffer[idx++] = i;
  }
  for(i = 0; i < idx; i++)
    printf("%d: %d\n", i, buffer[i]);
  return 0;
}
> clang -g -O0 scanit.c -o scanit
> lldb scanit
(lldb) target create "scanit"
Current executable set to '/tmp/scanit' (x86_64).
(lldb) break set -n main
Breakpoint 1: where = scanit`main + 41 at scanit.c:8:7, address = 0x0000000100000e89
(lldb) run
Process 74926 launched: '/tmp/scanit' (x86_64)
Process 74926 stopped
* thread #1 tid = 0x71d134 , queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    frame #0: 0x0000000100000e89 scanit`main at scanit.c:8
    5       {
    6         int i;
    7         int buffer[2000];
 -> 8         int idx = 0;
                      ^
    9         while(scanf("%d", &i) != EOF) {
   10       buffer[idx++] = i;
   11     }
Target 0: (scanit) stopped.
(lldb) c
Process 74926 resuming
10 20 30 40 ^D
0: 10
1: 20
2: 30
3: 40
Process 74926 exited with status = 0 (0x00000000) 
(lldb)
>cat scant.c
#包括
int
main()
{
int i;
智力增益