Section 16/161 menit
16. Ringkasan & Decision Guide
16. Ringkasan & Decision Guide
Pemilihan Teknik Debugging
| Situasi | Teknik LLDB yang Direkomendasikan |
|---|---|
| Crash sekali terjadi | bt, frame variable, po self di frame crash |
| Bug di dalam loop besar | Conditional breakpoint dengan --condition |
| "Siapa yang mengubah nilai ini?" | Watchpoint: watchpoint set variable |
| Test hipotesis tanpa recompile | expr variable = newValue, expr functionCall() |
| Memory leak | Breakpoint di deinit, cek retain count, Instruments Leaks |
| Race condition / data race | Thread Sanitizer + bt all saat TSan pause |
| Debug kode async/await | bt all, async backtrace di Xcode, breakpoint di task switch |
| Debug concurrency deadlock | Pause app, bt all, cari circular waiting |
| Production crash | dSYM + atos atau Xcode crash log symbolication |
| Profiling performa | Instruments (bukan LLDB) |
Perintah yang Paling Sering Dipakai
swift
bt # backtrace — pertama kali saat debugging apapun
fr v # frame variable — lihat semua variabel lokal
po <expr> # inspect object dengan description-nya
expr <code> # modifikasi state atau panggil fungsi
bt all # semua thread — untuk concurrency dan deadlock
watchpoint set variable <var> # siapa yang mengubah nilai ini?
b <file>:<line> # breakpoint cepat
b <funcname> # breakpoint di nama fungsi
Workflow Debugging yang Disarankan
swift
1. Repro → pastikan bisa reproduce crash/bug secara konsisten
2. Hypothesize → buat hipotesis tentang root cause
3. Observe → gunakan breakpoint/watchpoint untuk konfirmasi/bantah hipotesis
4. Modify → gunakan expr untuk test fix tanpa recompile
5. Fix → tulis fix di kode, verifikasi dengan breakpoint dihapus
6. Test → tambah unit test untuk prevent regresi
Setup LLDB yang Disarankan
Pasang ini di ~/.lldbinit untuk meningkatkan produktivitas:
swift
# ~/.lldbinit — konfigurasi global LLDB
# Alias berguna
command alias bfl breakpoint set --file %1 --line %2
command alias poc po self
command alias fv frame variable
# Import script Python kustom
# command script import ~/lldb_scripts/swift_helpers.py
# Tampilkan source context lebih banyak saat berhenti di breakpoint
settings set stop-line-count-before 5
settings set stop-line-count-after 5
# Nonaktifkan konfirmasi untuk perintah destruktif
settings set auto-confirm true
LLDB adalah investasi skill yang sangat worthwhile — sekali menguasai perintah dasar, debugging yang butuh berjam-jam dengan print statements bisa diselesaikan dalam hitungan menit. Langkah selanjutnya: pelajari Instruments untuk memory dan performance profiling, Address Sanitizer untuk memory corruption, dan Thread Sanitizer untuk data race detection.