Przejdลบ do treล›ci

๐Ÿฆ‰ FreeBSD Debugging Tools

FreeBSD provides a comprehensive suite of debugging and diagnostic tools that allow administrators and developers to inspect system behavior, troubleshoot performance issues, and analyze application problems effectively.


๐Ÿ” Core Debugging Utilities

truss โ€“ System Call Tracing

FreeBSD's equivalent of Linux's strace, used for tracing system calls made by processes.

1
2
3
4
5
6
7
8
# Trace system calls of a command
truss ls

# Trace a running process
truss -p 1234

# Save output to file
truss -o trace.log ./my_program

๐Ÿ’ก Tip: Use -f to follow child processes and -D to show syscall durations.


ktrace / kdump โ€“ Kernel-Level Tracing

More detailed than truss, allows tracing multiple processes and kernel activities.

1
2
3
4
5
6
7
8
# Start tracing a process
ktrace ./my_app

# View trace results
kdump

# Trace specific system calls only
ktrace -t c ./network_program

gdb โ€“ GNU Debugger

Same as on Linux, used for debugging compiled programs with debug symbols.

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

# Run with arguments
(gdb) run arg1 arg2

# Set breakpoint at main
(gdb) break main

# Show backtrace
(gdb) bt

๐Ÿ› ๏ธ Ensure binary was compiled with -g flag for debug info.


๐Ÿ“Š Performance Analysis Tools

top โ€“ Process Activity Monitor

Built-in tool showing real-time system resource usage.

1
2
3
4
5
# Interactive top view
top

# Batch mode (for scripting)
top -b -n 1

procstat โ€“ Process Information Utility

Provides detailed statistics about running processes including memory maps, file descriptors, and threads.

1
2
3
4
5
6
7
8
# Show basic info for PID
procstat 1234

# List open files
procstat -f 1234

# Memory mapping details
procstat -v 1234

vmstat โ€“ Virtual Memory Statistics

Reports virtual memory statistics, process scheduling, disk I/O, and more.

1
2
3
4
5
# Display every 2 seconds
vmstat 2

# Show disk-specific stats
vmstat -i

๐Ÿ—ƒ๏ธ Filesystem & Storage Diagnostics

fstat โ€“ File Status Information

Shows which files are currently open by processes.

1
2
3
4
5
6
7
8
# Show all open files
fstat

# Filter by process ID
fstat -p 1234

# Show network connections
fstat -n

iostat โ€“ I/O Statistics

Monitors disk I/O performance and utilization.

1
2
3
4
5
# Extended device statistics
iostat -x

# Update every 5 seconds
iostat 5

geom โ€“ GEOM Subsystem Control

Manages and queries the GEOM (Geometry) subsystem for disk devices.

1
2
3
4
5
6
7
8
# List all disks and partitions
geom disk list

# Show disk labels
geom label status

# View RAID configurations
geom mirror status

๐ŸŒ Network Troubleshooting

sockstat โ€“ Socket Statistics

Lists active sockets and associated processes.

1
2
3
4
5
6
7
8
# Show listening sockets
sockstat -l

# All IPv4 connections
sockstat -4

# Specific protocol (e.g., TCP)
sockstat -P tcp

tcpdump โ€“ Packet Sniffer

Captures and analyzes network traffic packets.

1
2
3
4
5
6
7
8
# Capture on default interface
tcpdump

# Filter HTTP traffic
tcpdump port 80

# Write capture to file
tcpdump -w capture.pcap

netstat โ€“ Network Statistics

Displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

1
2
3
4
5
6
7
8
# Active connections
netstat -an

# Listening ports
netstat -an | grep LISTEN

# Interface statistics
netstat -i

๐Ÿง  Memory Debugging

valgrind โ€“ Memory Error Detectorยน

Available via Ports or Packages, detects memory leaks and invalid accesses.

1
2
3
4
5
# Check for memory errors
valgrind --tool=memcheck ./my_program

# Generate suppressions
valgrind --gen-suppressions=all ./my_program

ยน Note: Must be installed separately using pkg install valgrind.


/proc/*/status โ€“ Process Memory Info

Access process memory information directly from procfs (if mounted).

1
2
# Check process memory usage
cat /proc/1234/status | grep VmRSS

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

dmesg โ€“ Kernel Ring Buffer

Views boot-time messages and runtime kernel diagnostics.

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

# Highlight errors/warnings
dmesg -T | grep -E "(error|warning)"

syslogd + logger โ€“ System Logging

Standard Unix logging facilities supported in FreeBSD.

1
2
3
4
5
# Send custom log message
logger "Test log entry"

# View recent logs
tail /var/log/messages

๐Ÿงฐ Advanced Diagnostic Tools

DTrace โ€“ Dynamic Tracing Frameworkยฒ

Powerful instrumentation framework for examining live systems.

Example probe:

1
2
3
4
syscall::open:entry
{
    printf("Opening file: %s\n", copyinstr(arg0));
}

Run with:

1
sudo dtrace -s script.d

ยฒ Available on most architectures except some ARM variants.


kgdb โ€“ Kernel Debuggerยณ

Used for debugging the running kernel or crash dumps.

1
2
# Analyze core dump
kgdb /boot/kernel/kernel /var/crash/vmcore.0

ยณ Requires kernel debugging enabled and proper setup.


๐Ÿงพ Summary Table

Tool Purpose
truss System call tracing
ktrace Kernel-level process tracing
gdb Source-level debugging
procstat Detailed process information
fstat Open file descriptor tracking
sockstat Socket and connection monitoring
tcpdump Network packet capture
valgrind Memory error detection
dmesg Kernel diagnostic messages
DTrace Dynamic system-wide tracing

๐Ÿง  Best Practices

โœ… Use truss for quick syscall troubleshooting โœ… Leverage procstat for deep process introspection โœ… Enable DTrace for production-safe observability โœ… Regularly review dmesg for hardware or driver issues โœ… Combine tools for layered diagnostics โœ… Store logs centrally for audit trails


๐Ÿงพ See Also