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

How to input value when use lldb debug my code

我在用lldb调试程序的时候,在main函数下了断点,为什么直接结束了?另外,终端应该等待我的输入...

func.c

#include "func.h"
        void insert_head(pnode *phead,pnode *ptail,int i){
        pnode pnew = (pnode)calloc(1,sizeof(node));
        pnew->num = i;
        if(phead == NULL){
            *phead = pnew;
            *ptail = pnew;
        }else{
             pnew->pnext = *phead;
            *phead = pnew;
        }
    }

    void print_list(pnode phead){
        while(phead){
            printf("%d",phead->num);
            phead=phead->pnext;
    }
    }

main.cpp

#include "func.h"
   int main()
   {
       pnode phead,ptail;//create new head ptial point to sturct
       phead = ptail = NULL;
       int i;
       while(scanf("%d",&i) != EOF){
           insert_head(&phead,&ptail,i);
       }
      print_list(phead);
      return 0;
  }

func.h

#pragma once
#include <cstdio>
#include <cstdlib>
//定义结构体
typedef struct Node{
    int num;
    struct Node *pnext;
}node,*pnode;

//头插法
void insert_head(pnode *phead,pnode *ptail,int i);
void print_list(pnode phead);

你可以看到图片,我想弄清楚这个,请帮助我,谢谢大家

对于你的lldb ./test,根据出色的@JimIngham 评论,lldb 可以在程序执行时捕获用户输入(而不是在例如在断点处停止时)。

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

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

运行 另一个终端 window 并启动

lldb -n "test"

它将根据其名称附加到 运行ning 进程。

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

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)

这样就可以在程序 运行 时正确地从终端获取输入并将其提供给 scanf 调用。

据我所知,造成你困惑的原因是你没有使用调试信息构建你的程序,所以当你停在初始断点时,你没有意识到你还没有到尚未调用 scanf。