Search code examples
clldb

How to input value when use lldb debug my code


When I debug my program with lldb, I set a breakpoint in the main function, why did it end directly? In addition, the terminal should wait for my input ...

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);

You can see the image , i want to figure out this,pls help me , thanks guys


Solution

  • In the example shown above, first of all, it looks like you didn't build your code with debug information (pass -g to your compiler invocations, and make sure you aren't stripping your binary). That's why when you hit your breakpoint at main, you only see some disassembly and not your source code.

    If you had debug info, then when your program hit the breakpoint at main, lldb would show you that you are stopped at the beginning of main, before your program has called scanf to query for input. You should just be able to issue the continue command in lldb, and your program will proceed to the scanf call and wait for you input.

    For instance, this (admittedly horrible code) works under 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)
    

    So that correctly fetched the input from the terminal while the program was running and provided it to the scanf call.

    From what I can see, the cause of your confusion is that you didn't build your program with debug information, so when you stopped at your initial breakpoint, you didn't realize you just hadn't gotten to the call to scanf yet.