Section 8/161 menit

8. Inspeksi Thread dan Stack Frame

8. Inspeksi Thread dan Stack Frame

Debugging Multi-Thread

swift
# Lihat semua threads
(lldb) thread list
* thread #1: tid = 0x1234, 0x00000001 MyApp`main, queue = 'com.apple.main-thread'
  thread #2: tid = 0x5678, 0x00000002 MyApp`URLSession worker thread
  thread #3: tid = 0x9abc, 0x00000003 libswift_Concurrency.dylib`swift_task_switch

# Pindah ke thread tertentu
(lldb) thread select 2

# Lihat backtrace thread tertentu
(lldb) thread backtrace 2

# Lihat backtrace SEMUA threads (sangat berguna untuk deadlock)
(lldb) bt all

# Hentikan thread tertentu (bukan kill, hanya pause)
(lldb) thread suspend 2

# Lanjutkan thread tertentu saja
(lldb) thread resume 2
swift
# Lihat call stack (backtrace) thread saat ini
(lldb) bt
frame #0: 0x00000001 MyApp`NetworkManager.fetchUser(id:) at NetworkManager.swift:45
frame #1: 0x00000002 MyApp`UserInteractor.loadProfile() at UserInteractor.swift:23
frame #2: 0x00000003 MyApp`ProfileViewController.viewDidLoad() at ProfileViewController.swift:18

# Pindah ke frame (angka = nomor frame dari bt)
(lldb) frame select 2
(lldb) f 2

# Di frame 2 (ProfileViewController.viewDidLoad), lihat variabel lokal
(lldb) frame variable
(lldb) fr v

# Lihat source context di frame ini
(lldb) frame info

Register Inspection

swift
# Lihat semua register (ARM64)
(lldb) register read

# Lihat register tertentu
(lldb) register read x0 x1 x2  # argument registers
(lldb) register read sp        # stack pointer
(lldb) register read pc        # program counter (current instruction)
(lldb) register read lr        # link register (return address)

# Sangat berguna saat debugging crash tanpa simbol

Memory Reading

swift
# Baca memori di alamat tertentu (hex dump)
(lldb) memory read 0x00000001006a4000
(lldb) x 0x00000001006a4000      # singkatan

# Baca sebagai tipe tertentu
(lldb) memory read --type int 0x00000001
(lldb) memory read --size 8 --format hex 0x00000001006a4000

# Baca variabel sebagai raw memory
(lldb) memory read &myVariable