Hata Ayıklama
BERK, modern debugging araçları ile güçlü hata ayıklama desteği sunar. DWARF/PDB debug sembolleri, GDB/LLDB entegrasyonu ve VS Code debugging ile profesyonel geliştirme deneyimi.
Debug Build
Debug Sembollerini Aktifleştirme
# Debug modda derle (optimizasyon yok, debug sembolleri var)
berk-lang build --debug
# Veya
berk-lang build -g
Debug vs Release
| Özellik | Debug (-g) | Release (-O2) |
|---|---|---|
| Debug sembolleri | ✅ Var | ❌ Yok |
| Optimizasyon | ❌ Yok (-O0) | ✅ Var (-O2) |
| Derleme hızı | 🚀 Hızlı | 🐌 Yavaş |
| Çalışma hızı | 🐌 Yavaş | 🚀 Hızlı (10-50x) |
| Binary boyutu | 📦 Büyük | 📦 Küçük |
| Stack trace | ✅ Detaylı | ⚠️ Sınırlı |
GDB Debugger
Temel Kullanım
# Program GDB ile başlat
gdb ./output
# GDB içinde
(gdb) run # Programı çalıştır
(gdb) break main.berk:10 # 10. satıra breakpoint
(gdb) continue # Devam et
(gdb) next # Sonraki satır (step over)
(gdb) step # Fonksiyon içine gir (step into)
(gdb) print x # Değişken değeri
(gdb) backtrace # Call stack
(gdb) quit # Çık
Breakpoint Türleri
# Satır breakpoint
(gdb) break main.berk:25
# Fonksiyon breakpoint
(gdb) break hesapla
# Koşullu breakpoint
(gdb) break main.berk:30 if x > 10
# Watchpoint (değişken değiştiğinde dur)
(gdb) watch x
# Tüm breakpointleri listele
(gdb) info breakpoints
# Breakpoint sil
(gdb) delete 1
Değişken İnceleme
# Basit değişken
(gdb) print x
$1 = 42
# Struct
(gdb) print kisi
$2 = {isim = \"Ali\", yaş = 25}
# Array
(gdb) print sayılar[0]@5 # İlk 5 eleman
# Pointer
(gdb) print *ptr
# Format belirle
(gdb) print/x x # Hex
(gdb) print/t x # Binary
(gdb) print/f x # Float
LLDB Debugger
Temel Komutlar
# LLDB başlat
lldb ./output
# LLDB içinde
(lldb) run
(lldb) breakpoint set --file main.berk --line 10
(lldb) breakpoint set --name hesapla
(lldb) continue
(lldb) next
(lldb) step
(lldb) frame variable x
(lldb) bt # Backtrace
(lldb) quit
VS Code Debugging
launch.json Yapılandırma
.vscode/launch.json:
{
\"version\": \"0.2.0\",
\"configurations\": [
{
\"name\": \"Debug BERK Program\",
\"type\": \"cppdbg\",
\"request\": \"launch\",
\"program\": \"${workspaceFolder}/output.exe\",
\"args\": [],
\"stopAtEntry\": false,
\"cwd\": \"${workspaceFolder}\",
\"environment\": [],
\"externalConsole\": false,
\"MIMode\": \"gdb\",
\"miDebuggerPath\": \"gdb.exe\",
\"setupCommands\": [
{
\"description\": \"Enable pretty-printing\",
\"text\": \"-enable-pretty-printing\",
\"ignoreFailures\": true
}
],
\"preLaunchTask\": \"build-debug\"
}
]
}
tasks.json (Debug Build)
.vscode/tasks.json:
{
\"version\": \"2.0.0\",
\"tasks\": [
{
\"label\": \"build-debug\",
\"type\": \"shell\",
\"command\": \"berk-lang\",
\"args\": [\"build\", \"--debug\", \"-o\", \"output.exe\"],
\"group\": {
\"kind\": \"build\",
\"isDefault\": true
},
\"problemMatcher\": []
}
]
}
VS Code Debugging Özellikleri
1. Breakpointlar
- ✅ Line breakpoints (F9)
- ✅ Conditional breakpoints (sağ tık)
- ✅ Logpoints (çıktı yazdir, durmaz)
- ✅ Function breakpoints
2. Stepping
| Kısayol | Aksiyon |
|---|---|
| F5 | Continue |
| F10 | Step Over (fonksiyonun üzerinden geç) |
| F11 | Step Into (fonksiyonun içine gir) |
| Shift+F11 | Step Out (fonksiyondan çık) |
| Ctrl+Shift+F5 | Restart |
| Shift+F5 | Stop |
3. Variables Panel
Sol taraftaki "Variables" panelinde:
- Local değişkenler
- Global değişkenler
- Değişken değerlerini düzenleme
4. Watch Expressions
"Watch" paneline ekleyin:
x * 2
kisi.isim
sayılar.uzunluk()
5. Call Stack
Fonksiyon çağrı geçmişini gösterir - herhangi bir frame'e tıklayın
6. Debug Console
Canlı ifade değerlendirme:
// Debug Console'da
x + 10
kisi.isim
hesapla(5, 10)
Koşullu Breakpoint Örnekleri
// Sadece x > 100 olduğunda dur
fonksiyon işle(x: sayı) yap
eğer x < 0 yap // Breakpoint burada: x < 0
dön 0
son
dön x * 2
son
Logpoint Kullanımı
Satır numarasına sağ tık → "Add Logpoint":
x değeri: {x}, y değeri: {y}
Program durmaz, sadece log yazdırır
Embedded Debugging
ESP32 JTAG Debugging
# OpenOCD başlat
openocd -f board/esp32.cfg
# GDB bağlan
xtensa-esp32-elf-gdb output.elf
(gdb) target remote :3333
(gdb) monitor reset halt
(gdb) break main
(gdb) continue
STM32 SWD Debugging
# ST-Link ile
st-util
# GDB
arm-none-eabi-gdb output.elf
(gdb) target remote :4242
(gdb) load
(gdb) break main
(gdb) continue
Performance Profiling
Linux (perf)
# Profiling verisi topla
perf record -g ./output
# Analiz et
perf report
# Flame graph
perf script | flamegraph.pl > flame.svg
Windows (Performance Analyzer)
# Visual Studio Profiler
devenv /DebugExe output.exe
Memory Debugging
Valgrind (Linux)
# Memory leak kontrolü
valgrind --leak-check=full ./output
# Cache profiling
valgrind --tool=cachegrind ./output
Address Sanitizer
# LLVM AddressSanitizer ile derle
berk-lang build --sanitizer=address
# Çalıştır - memory hataları otomatik yakalanır
./output
Debugging Best Practices
-
✅ Debug build kullanın:
berk-lang build --debug -
✅ Logpoint'leri tercih edin (program durmaz)
-
✅ Conditional breakpoints kullanın (döngülerde)
-
✅ Watch expressions ekleyin (karmaşık ifadeler)
-
⚠️ Optimizasyon kapalı olmalı debug modda
-
⚠️ Stack overflow kontrolü:
ulimit -s unlimited # Linux
Debugging Teknikleri
1. Binary Search Debugging
Kodun yarısına breakpoint koy - hata hangi yarıda?
2. Rubber Duck Debugging
Kodu satır satır açıkla (kendin veya birine)
3. Print Debugging
yazdir(\"x değeri: {}\", x)
yazdir(\"Buraya geldi mi?\")
4. Assertion Kullanımı
iddia_et(x > 0, \"x pozitif olmalı\")
Sorun Giderme
Debug Sembolleri Yok
# Kontrol et
file output.exe | grep \"not stripped\"
# Strip edilmişse yeniden derle
berk-lang build --debug
Breakpoint Durmuyor
- Debug modda derlendiğinden emin olun
- Optimizasyon kapalı olmalı (-O0)
- Satır numarası doğru mu?
Değişken Değerleri Gösterilmiyor
Optimizasyon değişkeni kaldırmış olabilir - debug build kullanın
Özet
- ✅ Debug build (--debug)
- ✅ GDB/LLDB entegrasyonu
- ✅ VS Code debugging
- ✅ Breakpoints (line, conditional, log)
- ✅ Watch expressions
- ✅ Call stack
- ✅ Embedded debugging
- ✅ Performance profiling
- ✅ Memory debugging (Valgrind, ASan)