当我使用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);
您可以看到图像,我想弄清楚,请帮助我,谢谢大家
答案 0 :(得分:2)
首先,在上面的示例中,您似乎没有使用调试信息来构建代码(将-g
传递给编译器调用,并确保未剥离二进制文件)。这就是为什么当您在主要位置遇到断点时,您只会看到一些反汇编而不是源代码的原因。
如果有调试信息,则当程序在main处遇到断点时,lldb会告诉您在程序调用{{1}}来查询输入之前,main处已停止。您应该只能够在lldb中发出scanf
命令,并且程序将继续进行scanf调用并等待您输入。
例如,此(令人恐惧的代码)在lldb下工作:
continue
以便在程序运行时从终端正确提取输入,并将其提供给> 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)
调用。
据我所知,造成混乱的原因是您没有使用调试信息来构建程序,因此当您在初始断点处停止时,您并没有意识到自己只是没有接到电话进行扫描。
答案 1 :(得分:0)
对于lldb ./test
,根据@JimIngham的出色评论,lldb可以在程序执行时捕获用户输入(例如,在断点处停止时,则不是)。
对于带有终端UI的更复杂的程序,单独的终端窗口(一个用于lldb,一个用于您的程序)可能更方便。
要使用后一种方法,请先在终端中运行./test
程序,然后在程序中等待通过scanf
的用户输入。
运行另一个终端窗口并启动
lldb -n "test"
这将根据其名称附加到正在运行的进程。
或者您也可以在进程启动时附加
lldb -n "test" --wait-for
and in another terminal window
./test
这使您可以调试main
并对程序执行任何所需的操作(包括提供用户输入)。