Przejdลบ do treล›ci

๐Ÿ macOS Debugging Tools

macOS provides a rich set of debugging and diagnostic tools inherited from its BSD roots while also offering unique Darwin-specific utilities. These tools help developers and administrators monitor system performance, trace application behavior, and resolve issues efficiently.


๐Ÿ” Core Debugging Utilities

dtruss โ€“ System Call Tracing

macOS equivalent of strace/truss, built on DTrace for tracing system calls.

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

# Trace specific process
sudo dtruss -p 1234

# Count syscalls by type
sudo dtruss -c ./my_program

๐Ÿ’ก Tip: Requires sudo privileges due to DTrace restrictions.


sample โ€“ Process Sampling

Samples running processes to determine where they spend time, similar to a lightweight profiler.

1
2
3
4
5
# Sample process for 10 seconds
sample 1234 10

# Output call tree to file
sample 1234 -file /tmp/sample.txt

lldb โ€“ LLVM Debugger

Modern replacement for GDB, Apple's preferred debugger for native applications.

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

# Run with arguments
(lldb) run arg1 arg2

# Set breakpoint at main
(lldb) breakpoint set --name main

# Show backtrace
(lldb) thread backtrace

๐Ÿ“Š Performance Analysis Tools

top โ€“ Process Activity Monitor

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

1
2
3
4
5
6
7
8
# Interactive top view
top

# Sort by CPU usage
top -o cpu

# Show specific number of processes
top -n 20

vm_stat โ€“ Virtual Memory Statistics

Reports virtual memory statistics including pageins/pageouts and free pages.

1
2
3
4
5
# One-shot statistics
vm_stat

# Continuous updates every 2 seconds
vm_stat 2

pmset โ€“ Power Management Settings

Monitor and configure power management behaviors affecting performance.

1
2
3
4
5
6
7
8
# View current power settings
pmset -g

# Show power event history
pmset -g log

# Check thermally limited processes
pmset -g therm

๐Ÿ—ƒ๏ธ Filesystem & Storage Diagnostics

lsof โ€“ List Open Files

Identifies 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

fs_usage โ€“ Filesystem Activity Monitor

Monitors filesystem activity in real-time with detailed timestamps.

1
2
3
4
5
6
7
8
# Monitor all filesystem activity
sudo fs_usage

# Filter by process
sudo fs_usage -w -f filesys Safari

# Show specific file operations
sudo fs_usage -f filesys

diskutil โ€“ Disk Utility CLI

Manage disks, partitions, and volumes from the command line.

1
2
3
4
5
6
7
8
# List all disks
diskutil list

# Get detailed info about volume
diskutil info /Volumes/MyDrive

# Repair disk permissions
diskutil repairPermissions /

๐ŸŒ Network Troubleshooting

netstat โ€“ Network Statistics

Displays network connections, routing tables, and interface statistics.

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

# Listening ports
netstat -an | grep LISTEN

# Interface statistics
netstat -i

tcpdump โ€“ Packet Sniffer

Captures and analyzes network traffic packets.

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

# Filter HTTP traffic
sudo tcpdump port 80

# Write capture to file
sudo tcpdump -w capture.pcap

networksetup โ€“ Network Configuration

Configure network interfaces, proxies, and services.

1
2
3
4
5
6
7
8
# List network services
networksetup -listallnetworkservices

# Get IP address of interface
networksetup -getinfo Wi-Fi

# Set DNS servers
networksetup -setdnsservers Wi-Fi 8.8.8.8 8.8.4.4

๐Ÿง  Memory Debugging

leaks โ€“ Memory Leak Detection

Apple's tool for detecting memory leaks in applications.

1
2
3
4
5
6
7
8
# Check for leaks in running app
leaks MyApp

# Analyze specific process
leaks 1234

# Generate detailed report
leaks -quiet MyApp > leak_report.txt

vmmap โ€“ Virtual Memory Map

Shows detailed virtual memory layout of a process.

1
2
3
4
5
6
7
8
# Memory map of process
vmmap 1234

# Show regions with specific permissions
vmmap -resident 1234

# Focus on stack regions
vmmap -stack 1234

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

log โ€“ Unified Logging System

Modern macOS logging system replacing traditional syslog.

1
2
3
4
5
6
7
8
# Stream live logs
log stream

# Show last hour of logs
log show --last 1h

# Filter by subsystem
log show --predicate 'subsystem == "com.apple.network"' --last 30m

syslog โ€“ Traditional System Logger

Legacy interface to system logging facilities.

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

# View recent logs
tail /var/log/system.log

๐Ÿงฐ Advanced Diagnostic Tools

Instruments โ€“ GUI Performance Analyzer

Xcode's powerful profiling tool with various templates: - Time Profiler - Allocations - Leaks - Energy Log - Network

Accessible via:

1
2
# Open Instruments from terminal
open -a Instruments


spindump โ€“ System Spin Dump

Generates detailed diagnostic reports when system appears unresponsive.

1
2
3
4
5
6
7
8
# Create spin dump
sudo spindump

# Target specific process
sudo spindump 1234

# Save to custom location
sudo spindump -file /tmp/spindump.txt

sc_usage โ€“ System Call Usage

Monitors system call usage across the entire system.

1
2
3
4
5
# Monitor system-wide syscall usage
sudo sc_usage

# Focus on specific process
sudo sc_usage -p 1234

๐Ÿงพ Summary Table

Tool Purpose
dtruss System call tracing
sample Process sampling/profiling
lldb Modern source-level debugger
vm_stat Virtual memory statistics
lsof Open file descriptor tracking
fs_usage Real-time filesystem monitoring
tcpdump Network packet capture
leaks Memory leak detection
log Unified logging system
Instruments GUI performance analysis toolkit

๐Ÿง  Best Practices

โœ… Use dtruss for syscall-level debugging (with sudo) โœ… Leverage log command for modern unified logging โœ… Combine sample and Instruments for performance profiling โœ… Monitor thermal throttling with pmset โœ… Use leaks regularly during development cycle โœ… Store logs systematically for post-mortem analysis


๐Ÿงพ See Also