Przejdลบ do treล›ci

๐Ÿง Linux Debugging Tools

Linux offers a rich ecosystem of debugging tools that help diagnose and troubleshoot system issues, performance bottlenecks, and application problems. This guide covers both built-in and third-party utilities for effective Linux debugging.


๐Ÿ” Core Debugging Utilities

strace โ€“ System Call Tracing

Tracks system calls made by a process, helping identify I/O issues, hangs, or unexpected behavior.

1
2
3
4
5
6
7
8
# Trace all system calls of a program
strace ls

# Save trace to file
strace -o trace.log my_program

# Follow child processes
strace -f my_daemon

๐Ÿ’ก Tip: Use -e trace=file to focus on file-related syscalls.


ltrace โ€“ Library Call Tracing

Monitors dynamic library calls instead of kernel-level syscalls.

1
2
3
4
5
# Trace shared library calls
ltrace ./my_app

# Filter by symbol name
ltrace -e malloc ./memory_hungry_app

gdb โ€“ GNU Debugger

A powerful interactive debugger for compiled programs (especially C/C++).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Start GDB session
gdb ./program

# Run with arguments
(gdb) run arg1 arg2

# Set breakpoint
(gdb) break main

# Backtrace stack
(gdb) bt

๐Ÿ› ๏ธ Requires debug symbols (-g flag during compilation).


๐Ÿ“Š Performance Analysis Tools

top / htop โ€“ Real-Time Process Monitoring

Displays live resource usage per process.

1
2
3
4
5
# Interactive top view
htop

# Sort by CPU usage
top -o %CPU

perf โ€“ Performance Profiling

Advanced profiling tool for analyzing CPU cycles, cache misses, branch predictions.

1
2
3
4
5
6
7
8
# Record profile data
perf record -g ./my_program

# View report
perf report

# Flame graph generation (requires extra tools)
perf script | stackcollapse-perf.pl | flamegraph.pl > perf.svg

vmstat, iostat, sar โ€“ System Metrics

Part of the sysstat package, these tools provide detailed metrics over time.

1
2
3
4
5
6
7
8
# Memory and swap statistics every 2 seconds
vmstat 2

# Disk I/O stats
iostat -x 5

# Collect historical data
sar -u 1 10

๐Ÿ—ƒ๏ธ Filesystem & Storage Diagnostics

lsof โ€“ List Open Files

Identify which processes have files open or are listening on ports.

1
2
3
4
5
6
7
8
# Show open files by process
lsof -p 1234

# Find who's using port 80
lsof -i :80

# Display network connections
lsof -iTCP -sTCP:LISTEN

iotop โ€“ Per-Process I/O Monitoring

Shows real-time disk I/O per process.

1
sudo iotop

df / du โ€“ Disk Space Usage

Monitor filesystem space and directory sizes.

1
2
3
4
5
# Human-readable disk usage summary
df -h

# Size of directories recursively
du -sh /var/log/*

๐ŸŒ Network Troubleshooting

ss / netstat โ€“ Socket Statistics

Check active network connections and socket states.

1
2
3
4
5
# Show TCP sockets in listen state
ss -tuln

# Equivalent with netstat
netstat -tulnp

tcpdump โ€“ Packet Sniffing

Capture and analyze raw packets on the wire.

1
2
3
4
5
# Capture traffic on eth0
sudo tcpdump -i eth0

# Filter HTTP traffic
sudo tcpdump -i any port 80

ping / traceroute / mtr โ€“ Connectivity Testing

Basic connectivity checks and path tracing.

1
2
3
4
5
6
7
8
# Test reachability
ping google.com

# Trace hop-by-hop path
traceroute google.com

# Continuous traceroute with latency stats
mtr google.com

๐Ÿง  Memory Debugging

valgrind โ€“ Memory Error Detection

Detect memory leaks, invalid reads/writes, and other errors.

1
2
3
4
5
# Check for memory leaks
valgrind --tool=memcheck --leak-check=yes ./my_program

# Generate suppression file for false positives
valgrind --gen-suppressions=all ./my_program

/proc/meminfo โ€“ Kernel Memory Info

Access low-level memory info directly from procfs.

1
cat /proc/meminfo | grep -E "(MemTotal|MemFree|Buffers|Cached)"

๐Ÿ•ต๏ธโ€โ™‚๏ธ Logs and Journaling

journalctl โ€“ systemd Journal Viewer

Query logs managed by systemd journal.

1
2
3
4
5
6
7
8
# View recent logs
journalctl -n 100

# Tail logs like tail -f
journalctl -f

# Filter logs for service
journalctl -u nginx.service

dmesg โ€“ Kernel Ring Buffer

View kernel boot messages and hardware diagnostics.

1
2
3
4
5
# Recent kernel messages
dmesg | tail

# Highlight errors
dmesg -T --level=err,warn

๐Ÿงฐ Advanced Toolkits

BCC (BPF Compiler Collection)

Use eBPF probes for advanced tracing without modifying code.

Example: trace slow file opens:

1
opensnoop -p PID

Install via package manager:

1
sudo apt install bpfcc-tools

htop vs atop โ€“ Enhanced Monitoring

While htop gives visual clarity, atop stores historical snapshots for post-mortem analysis.

1
2
3
4
5
# Run atop interactively
atop

# Read previous day's snapshot
atop -r /var/log/atop/atop_20250404

๐Ÿงพ Summary Table

Tool Purpose
strace Syscall tracing
ltrace Library call tracing
gdb Interactive source-level debugger
perf CPU profiling
htop Live process viewer
lsof Open files and ports
tcpdump Packet capture
valgrind Memory leak detection
journalctl systemd log inspection
dmesg Kernel diagnostic output

๐Ÿง  Best Practices

โœ… Always verify your assumptions about system behavior โœ… Prefer structured logging in applications for easier diagnosis โœ… Combine multiple tools for layered insights โœ… Use containers/namespaces to isolate experiments โœ… Store logs securely and rotate regularly


๐Ÿงพ See Also