अंतिम अद्यतन:2026-10-12
LLDB से C++ प्रोग्राम डीबग करना¶
इस लेख में बताया जाएगा कि LLDB debugger से एक सरल C++ प्रोग्राम कैसे debug करें। हमारा example code test.cpp नाम से है।
#include <iostream>
#include <string>
struct MyStruct {
int myInt;
std::string myString;
};
int main() {
MyStruct myStruct;
myStruct.myInt = 42;
myStruct.myString = "Hello, world!";
int sum = 0;
for (int i = 1; i <= 5; ++i) {
sum += i;
}
return 0;
}
Compilation¶
सबसे पहले, हमें अतिरिक्त debug information enable करने के लिए -g flag से code compile करना होगा।
डीबगिंग शुरू करना¶
इसके बाद, हम debugger attach करते हैं।
Breakpoints¶
हम breakpoint को main() function पर सेट करते हैं।
आउटपुट:
Breakpoint 1: where = test`main + 19 at test.cpp:10:14, address = 0x0000000100003be3
चलाना¶
फिर, हम process शुरू करते हैं।
आउटपुट:
Process 11333 launched: '/Users/light/test' (x86_64)
Process 11333 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
frame #0: 0x0000000100003be3 test`main at test.cpp:10:14
7 };
8
9 int main() {
-> 10 MyStruct myStruct;
11 myStruct.myInt = 42;
12 myStruct.myString = "Hello, world!";
13
Breakpoints देखना¶
अब हम main() function की शुरुआत पर रुक चुके हैं। हम किसी विशेष line पर breakpoint सेट कर सकते हैं।
आउटपुट: Breakpoint 2: where = test`main + 76 at test.cpp:16:16, address = 0x0000000100003c1c
वर्तमान में सेट किए गए breakpoints की सूची देखने के लिए निम्न command का उपयोग करें।
आउटपुट:
(lldb) breakpoint list
Current breakpoints:
1: name = 'main', locations = 1, resolved = 1, hit count = 1
1.1: where = test`main + 19 at test.cpp:10:14, address = 0x0000000100003be3, resolved, hit count = 1
2: file = '/Users/zhangxing/test.cpp', line = 16, exact_match = 0, locations = 1, resolved = 1, hit count = 0
2.1: where = test`main + 76 at test.cpp:16:16, address = 0x0000000100003c1c, resolved, hit count = 0
प्रोग्राम का निष्पादन जारी रखना¶
हम प्रोग्राम को अगले breakpoint तक चलाना जारी रख सकते हैं।
आउटपुट:
Process 11823 resuming
Process 11823 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
frame #0: 0x0000000100003c1c test`main at test.cpp:16:16
13
14 int sum = 0;
15 for (int i = 1; i <= 5; ++i) {
-> 16 sum += i;
17 }
18
19 return 0;
Variables print करना¶
current local variables के values पाने के लिए निम्न command का उपयोग किया जा सकता है:
आउटपुट:
Watchpoints¶
किसी विशेष variable के value में बदलाव monitor करने के लिए हम watchpoint का उपयोग कर सकते हैं।
आउटपुट:
Watchpoint created: Watchpoint 1: addr = 0x7ffeefbff1e8 size = 4 state = enabled type = w
declare @ '/Users/zhangxing/test.cpp:14'
watchpoint spec = 'sum'
new value: 0
next चलाने के बाद, Watchpoint में बदलाव दिखेगा।
(lldb) n
Watchpoint 1 hit:
old value: 3
new value: 6
Process 11823 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 1
frame #0: 0x0000000100003c25 test`main at test.cpp:15:29
12 myStruct.myString = "Hello, world!";
13
14 int sum = 0;
-> 15 for (int i = 1; i <= 5; ++i) {
16 sum += i;
17 }
18
अगर किसी condition के आधार पर निष्पादन रोकना चाहते हैं, तो stop condition सेट किया जा सकता है।
आउटपुट:
Watchpoint 1 hit:
new value: 6
Process 12848 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = watchpoint 1
frame #0: 0x0000000100003c25 test`main at test.cpp:15:29
12 myStruct.myString = "Hello, world!";
13
14 int sum = 0;
-> 15 for (int i = 1; i <= 5; ++i) {
16 sum += i;
17 }
18
डीबगिंग पूरी होने पर, प्रोग्राम का निष्पादन जारी रखा जा सकता है।
अधिक जानकारी के लिए LLDB documentation देखें:
इन चरणों के ज़रिए आप LLDB से अपने C++ प्रोग्राम को आसानी से debug कर सकते हैं, गलतियाँ ढूंढ सकते हैं और समस्याएँ हल कर सकते हैं।
संबंधित विषय¶
- Bazel से C++ प्रोजेक्ट बिल्ड करना | C++那些事
- Bazel और LLDB संयुक्त डिबगिंग | C++那些事
- CMake की शुरुआत और व्यवहार | C++那些事
- Conan C++ पैकेज मैनेजर की शुरुआत | C++那些事
- C++ Insights कंपाइल-टाइम अवलोकन | C++那些事
- C++ Jupyter Notebook इंटरैक्शन | C++那些事
- C++ string को int में बदलना | C++那些事
- C++ dbg-macro डीबगिंग मैक्रो | C++那些事
- GCC कंपाइलेशन विकल्पों की विस्तृत जानकारी | C++那些事
- GDB से C++ प्रोग्राम डीबग करना | C++那些事