1. 程式人生 > 實用技巧 >lldb除錯C++總結(3)

lldb除錯C++總結(3)

note

本文將彌補之前的遺漏部分。

continue

前面提到,當設定斷點後,使用stepnextfinish,程式會停下來,需要程式繼續執行,鍵入continue, 程式可自動繼續向下執行.

設定斷點

(lldb) breakpoint set --line 14
Breakpoint 1: where = demo`main + 151 at demo.cpp:14:9, address = 0x0000000000401277

執行程式

(lldb) run  
Process 82513 launched: '/home/xx/demo/libevent_demo/demo' (x86_64)

程式停下來了

Process 82513 stopped
* thread #1, name = 'demo', stop reason = breakpoint 1.1

繼續執行

(lldb) continue

完整輸出

(lldb) breakpoint set --line 14
Breakpoint 1: where = demo`main + 151 at demo.cpp:14:9, address = 0x0000000000401277
(lldb) run  
Process 82513 launched: '/home/xx/demo/libevent_demo/demo' (x86_64)
ABCDEF

var_a=1

the print following is from call's output

----------------------------

Process 82513 stopped
* thread #1, name = 'demo', stop reason = breakpoint 1.1
    frame #0: 0x0000000000401277 demo`main(argc=1, argv=0x00007fffffffde88, env=0x00007fffffffde98) at demo.cpp:14:9
   11  	    std::cout << "var_a=" << var_a << "\n\n";
   12  	
   13  	    std::cout << "the print following is from call's output\n\n----------------------------\n\n";
-> 14  	    car new_car;
   15  	    new_car.print_name();
   16  	
   17  	
(lldb) continue
Process 82513 resuming
my name is october

Process 82513 exited with status = 0 (0x00000000) 

程式無異常, 退出返回值為0

Process 82513 exited with status = 0 (0x00000000) 

類成員函式設定斷點

準備

這裡準備了另一個檔案 car.hpp, 原始碼如下

#ifndef _car_h
#define _car_h
#include <iostream>
class car
{
public:
    car(const car& instance) = delete;
    car(const car&& instance) = delete;
    car& operator = (const car& instance) = delete;
    car& operator = (const car&& instance) = delete;
    
    car(){}
    ~car(){}

    void print_name()
    {
        std::cout << "my name is october\n\n"; 
    }
};

#endif //! _car_h

呼叫程式碼如下:

    car new_car;
    new_car.print_name();

指定檔案設定設定斷點

使用breakpoint set --file [檔名] --line [行號] 可指定檔案設定斷點。

(lldb) breakpoint set --file car.hpp --line 18
Breakpoint 2: where = demo`car::print_name() + 12 at car.hpp:18:19, address = 0x00000000004012ec