LLDB 玩樂筆記

Dboy Liao
4 min readMar 18, 2019

--

最近在寫 uTensor 的 CNN Demo 文章,code 是可以 compile 也可以 run ,但是總在個奇怪的地方掛掉 (Segmentation Fault 看到飽),然後 gdb 搞 Apple 憑證之類的搞好久搞不定,只好玩玩 lldb 。寫個筆記記錄一下今晚學到的指令,畢竟 lldb 相對不熟悉 (說得好像跟 gdb 就熟一樣….)

寫個極度簡單的 C 程式來當範例:

#include <stdio.h>int add(int a, int b)
{
return a + b;
}
int main(int argc, char *argv[])
{
for (int i = 0; i < 5; ++i)
{
printf("%i\n", add(i, 2 * i));
}

for (int i = 1; i < argc; ++i)
{
printf("%s\n", argv[i]);
}
return 0;
}

起手式

  1. % gcc -g main.c -o main: 毫無懸念,先 compile
  2. % lldb: 進入 lldb

進入 lldb 之後

  • (lldb) file ./main: 載入可執行檔
  • (lldb) breakpoint set -f main.c -l 10 or b main.c:10: 設中斷點
  • (lldb) run [arg1,[arg2,...]]: 執行可執行檔,這時可以帶入參數,舉例來說, run -a -b dd --try ee 相當於執行 ./main -a -b dd --try ee
  • (lldb) frame info : 顯示當前 stack frame 的資訊,像是下面這樣
  • (lldb) f [<index>]: 依照 index 顯示 stack frame 的資訊,還會連帶顯示相關程式碼:
  • (lldb) c or continue: 顧名思義,繼續執行到下一個中斷點
  • (lldb) dis: 對 C debugger 熟悉的朋友應該不會陌生,就是反組譯。譬如我們在 add 下個中斷點,然後執行 dis 會看到:
  • (lldb) po or (lldb) p: 基本上就是 print ,只是用的 format 不一樣,譬如在 add 裡,我們可以執行 (lldb) po a(lldb) p a 會看到:
  • (lldb) env: 顯示環境變數,會看到所有當下的環境變數,我的環境變數太多了,但大致長這樣:
  • (lldb) kill: 終止目前 debugging process
  • (lldb) quit or ^D: 跳出 lldb

結語

這邊就列出一晚上我用了一下的幾個基本指令的基本用法,還有一些進階技巧還用不上,以後再說 XDDDD。

之前朋友有示範給我看用 gdb 去 run python 然後跑起來去對 C/C++ 的 .so 來 debug,我想 lldb 應該也可以做一樣的事,試出來就下一篇再寫囉。

References

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Dboy Liao
Dboy Liao

Written by Dboy Liao

Code Writer, Math Enthusiast and Data Scientist, yet.

No responses yet

Write a response