嵌入式开发调试工具
概述
嵌入式开发需要多种调试工具,包括串口调试、网络调试、逻辑分析等。
串口调试工具
minicom
| Bash |
|---|
| # 安装
sudo apt install minicom
# 配置
sudo minicom -s
# 连接
minicom -D /dev/ttyUSB0
# 常用快捷键
Ctrl+A, Z # 帮助菜单
Ctrl+A, X # 退出
Ctrl+A, S # 发送文件
Ctrl+A, R # 接收文件
Ctrl+A, C # 清屏
|
screen
| Bash |
|---|
| # 安装
sudo apt install screen
# 连接串口
screen /dev/ttyUSB0 115200
# 退出
Ctrl+A, K
# 记录输出
screen -L /dev/ttyUSB0 115200
|
picocom
| Bash |
|---|
| # 安装
sudo apt install picocom
# 连接
picocom -b 115200 /dev/ttyUSB0
# 退出
Ctrl+A, Ctrl+X
# 带自动换行
picocom -b 115200 --omap crcrlf /dev/ttyUSB0
|
cu
| Bash |
|---|
| # 安装
sudo apt install cu
# 连接
cu -l /dev/ttyUSB0 -s 115200
# 退出
~.
|
kermit
| Bash |
|---|
| # 安装
sudo apt install ckermit
# 配置 ~/.kermrc
set line /dev/ttyUSB0
set speed 115200
set carrier-watch off
set flow-control none
# 运行
kermit
C-Kermit> connect
|
串口调试助手(GUI)
CuteCom
| Bash |
|---|
| # 安装
sudo apt install cutecom
# 运行
cutecom
|
| Bash |
|---|
| # Python串口工具
pip install pyserial
python -m serial.tools.miniterm /dev/ttyUSB0 115200
|
自定义Python串口工具
| Python |
|---|
| import serial
import threading
def serial_monitor(port, baudrate):
ser = serial.Serial(port, baudrate, timeout=1)
def read_thread():
while True:
if ser.in_waiting:
data = ser.read(ser.in_waiting)
print(data.decode('utf-8', errors='ignore'), end='')
thread = threading.Thread(target=read_thread, daemon=True)
thread.start()
while True:
cmd = input()
ser.write((cmd + '\r\n').encode())
ser.close()
serial_monitor('/dev/ttyUSB0', 115200)
|
网络调试工具
netcat (nc)
| Bash |
|---|
| # TCP服务端
nc -l -p 8080
# TCP客户端
nc 192.168.1.1 8080
# UDP服务端
nc -u -l -p 8080
# UDP客户端
nc -u 192.168.1.1 8080
# 传输文件
# 发送端
nc -l -p 8080 < file.txt
# 接收端
nc 192.168.1.1 8080 > file.txt
|
socat
| Bash |
|---|
| # TCP转发
socat TCP-LISTEN:8080,fork TCP:192.168.1.1:80
# 串口转TCP
socat TCP-LISTEN:8080,fork FILE:/dev/ttyUSB0,rawer
# UDP转发
socat UDP-LISTEN:8080,fork UDP:192.168.1.1:8080
|
telnet
| Bash |
|---|
| # 连接
telnet 192.168.1.1 80
# 发送HTTP请求
GET / HTTP/1.1
Host: 192.168.1.1
|
curl
| Bash |
|---|
| # GET请求
curl http://192.168.1.1/api/data
# POST请求
curl -X POST -d "key=value" http://192.168.1.1/api
# 带认证
curl -u user:pass http://192.168.1.1/api
# 下载文件
curl -O http://192.168.1.1/file.bin
# 查看响应头
curl -I http://192.168.1.1
# 调试模式
curl -v http://192.168.1.1
|
wget
| Bash |
|---|
| # 下载文件
wget http://192.168.1.1/file.bin
# 后台下载
wget -b http://192.168.1.1/file.bin
# 断点续传
wget -c http://192.168.1.1/file.bin
# 递归下载
wget -r http://192.168.1.1/
|
协议调试
Modbus调试
| Bash |
|---|
| # 使用mbpoll
mbpoll -a 1 -r 0 -c 10 -t 3 /dev/ttyUSB0
# 使用pymodbus
pip install pymodbus
|
MQTT调试
| Bash |
|---|
| # 安装mosquitto客户端
sudo apt install mosquitto-clients
# 订阅
mosquitto_sub -h 192.168.1.1 -t "sensor/#" -v
# 发布
mosquitto_pub -h 192.168.1.1 -t "sensor/temp" -m "25.5"
|
HTTP调试
| Bash |
|---|
| # 使用httpie
pip install httpie
# GET请求
http GET http://192.168.1.1/api/data
# POST请求
http POST http://192.168.1.1/api key=value
|
系统调试
strace
| Bash |
|---|
| # 跟踪系统调用
strace ./myapp
# 跟踪特定系统调用
strace -e open,read,write ./myapp
# 附加到运行中的进程
strace -p <pid>
# 统计系统调用
strace -c ./myapp
|
ltrace
| Bash |
|---|
| # 跟踪库函数调用
ltrace ./myapp
# 跟踪特定函数
ltrace -e malloc,free ./myapp
|
ftrace
| Bash |
|---|
| # 内核函数跟踪
echo function > /sys/kernel/tracing/current_tracer
echo do_sys_open > /sys/kernel/tracing/set_ftrace_filter
cat /sys/kernel/tracing/trace
|
perf
| Bash |
|---|
| # 性能分析
perf record ./myapp
perf report
# 实时查看
perf top
# 统计事件
perf stat ./myapp
|
存储调试
dd
| Bash |
|---|
| # 读取设备
dd if=/dev/mmcblk0 of=backup.img bs=1M count=100
# 写入设备
dd if=image.bin of=/dev/mmcblk0 bs=1M
# 查看进度
dd if=/dev/zero of=test.bin bs=1M count=100 status=progress
|
hexdump
| Bash |
|---|
| # 十六进制查看
hexdump -C file.bin
# 指定偏移和长度
hexdump -C -s 0x100 -n 256 file.bin
|
fdisk/parted
| Bash |
|---|
| # 查看分区
fdisk -l /dev/mmcblk0
# 分区操作
fdisk /dev/mmcblk0
# GPT分区
parted /dev/mmcblk0
|
日志分析
journalctl
| Bash |
|---|
| # 查看日志
journalctl
# 实时查看
journalctl -f
# 过滤单元
journalctl -u myservice
# 过滤时间
journalctl --since "1 hour ago"
# 过滤优先级
journalctl -p err
|
dmesg
| Bash |
|---|
| # 查看内核消息
dmesg
# 实时查看
dmesg -w
# 清空
dmesg -c
# 过滤
dmesg | grep -i error
|
远程调试
SSH
| Bash |
|---|
| # 连接
ssh user@192.168.1.1
# 端口转发
ssh -L 8080:localhost:80 user@192.168.1.1
# 反向端口转发
ssh -R 8080:localhost:80 user@192.168.1.1
# 文件传输
scp file.txt user@192.168.1.1:/home/user/
|
rsync
| Bash |
|---|
| # 同步文件
rsync -avz src/ user@192.168.1.1:/dst/
# 增量同步
rsync -avz --progress src/ user@192.168.1.1:/dst/
# 排除文件
rsync -avz --exclude '*.log' src/ user@192.168.1.1:/dst/
|
参考资料