gdb app core.dump
SIGUSR1
信号不停止handle SIGUSR1 nostop
target remote :port
b * 0x7c00
ni/si
info register
info register dh
info register eflags
info address check_one_fd
info symbol 0x7ffff7a2d940
info sharedlibrary
p &((struct a*)0)->b
gdb qemuxml2argvtest
(gdb) set environment VIR_TEST_DEBUG=1
(gdb) set environment VIR_TEST_RANGE=5
(gdb) r
b {symbol} 设置断点,完成后假设断点为2
commands 2 针对断点2进行设置
> p {value} 打印相关变量
> XXXX 其它操作,比如自己设置循环变量,断住50次后自动停止等
> c 让停住的断点继续运行
> end 退出断点设置
readelf -s {binary/so}
objdump -D app
/* 指定事件输出到的日志 */
/* 指定要trace的事件 */
qemu-system-x86_64 -trace ?
virsh qemu-monitor-command CentOS_Stream_8 --hmp trace-event virtio_gpu_cmd_res_create_2d on
- qemu命令行指定qmp server socket
-qmp unix:/tmp/qmp-sock,server,nowait
- qemu源码下的客户端脚本连接该socket,进入交互界面
python3.6 scripts/qmp/qmp-shell -v -p /tmp/qmp-sock
-monitor stdio
virsh qemu-monitor-command {vm} --hmp dump-guest-memory -z /path/to/crash.img
crash vmlinux /path/to/crash.img
qemu-system-x86_64 -S -no-user-config -nodefaults -nographic -machine none,accel=kvm
trace_kvm_ioapic_set_irq
cat /sys/kernel/debug/tracing/events/kvm/kvm_ioapic_set_irq/format
echo 1 > /sys/kernel/debug/tracing/events/kvm/kvm_ioapic_set_irq/enable
cat /sys/kernel/debug/tracing/trace | grep kvm_ioapic_set_irq
TRACE_EVENT(kvm_ioapic_set_irq......)
kvm_exit: reason EPT_MISCONFIG rip 0x55555596c364 info 0 0
- reason: exit_reason —— EPT_MISCONFIG
intel vmx手册中定义的VM-exit退出原因
- rip: guest_rip —— 0x55555596c364
vcpu 下一次要执行的指令地址,通过gdb可以查看,如下
- info: info1、info2 —— 0,0
VMCS中VM-exit结构体中的exit exit和exit qualification 字段
static void modern_set_status(struct virtio_crypto_hw *hw, uint8_t status)
{
rte_write8(status, &hw->common_cfg->device_status);
}
mov %sil,0x14(%rax)
指令前后virtio-pci对应的内存空间(0x7ffff6801014)/*
* Tracepoint for kvm guest exit:
*/
TRACE_EVENT(kvm_exit,
TP_PROTO(unsigned int exit_reason, struct kvm_vcpu *vcpu, u32 isa),
TP_ARGS(exit_reason, vcpu, isa),
TP_STRUCT__entry(
__field( unsigned int, exit_reason )
__field( unsigned long, guest_rip )
__field( u32, isa )
__field( u64, info1 )
__field( u64, info2 )
),
TP_fast_assign(
__entry->exit_reason = exit_reason;
__entry->guest_rip = kvm_rip_read(vcpu);
__entry->isa = isa;
kvm_x86_ops->get_exit_info(vcpu, &__entry->info1,
&__entry->info2);
),
TP_printk("reason %s rip 0x%lx info %llx %llx",
(__entry->isa == KVM_ISA_VMX) ?
__print_symbolic(__entry->exit_reason, VMX_EXIT_REASONS) :
__print_symbolic(__entry->exit_reason, SVM_EXIT_REASONS),
__entry->guest_rip, __entry->info1, __entry->info2)
);
int console_printk[4] = {
DEFAULT_CONSOLE_LOGLEVEL, /* console_loglevel */
DEFAULT_MESSAGE_LOGLEVEL, /* default_message_loglevel */
MINIMUM_CONSOLE_LOGLEVEL, /* minimum_console_loglevel */
DEFAULT_CONSOLE_LOGLEVEL, /* default_console_loglevel */
};
cat /proc/sys/kernel/printk
A B C D
A: 控制台默认等级
B: 内核日志默认等级
C: 控制台最小等级
D: 控制台默认等级
#define KERN_EMERG KERN_SOH "0" /* system is unusable */
#define KERN_ALERT KERN_SOH "1" /* action must be taken immediately */
#define KERN_CRIT KERN_SOH "2" /* critical conditions */
#define KERN_ERR KERN_SOH "3" /* error conditions */
#define KERN_WARNING KERN_SOH "4" /* warning conditions */
#define KERN_NOTICE KERN_SOH "5" /* normal but significant condition */
#define KERN_INFO KERN_SOH "6" /* informational */
#define KERN_DEBUG KERN_SOH "7" /* debug-level messages */
1. kmem -o
打印内核per-cpu变量计算时需要加上的偏移
2. p {value}
打印per-cpu变量的值
3. p (struct XXX *)
两者相加解析得到per-cpu变量
yumdownloader --source kernel
rpm -ivh --root=`pwd` kernel.src.rpm
yum-builddep kernel
rpmbuild -bb SPECS/kernel.spec --define "_topdir $(pwd)"
make menuconfig
make -jN
make modules_install
make install
注意,内核调试需要打开的配置项没有明确的说法,可以参考Documentation/dev-tools下的文档,gdb+qemu调试内核同kgdb调试内核原理类似,因此可以把KGDB的配置都使能,以下配置可以都使能:
CONFIG_DEBUG_INFO 生成内核基本调试信息,即符号表
CONFIG_DEBUG_KERNEL 包含驱动的调试信息
CONFIG_KGDB 支持KGDB
CONFIG_FRAME_POINTER 支持gdb输出堆栈信息
CONFIG_GDB_SCRIPTS 生成vmlinux-gdb.py用于帮助gdb内核调试(高版本内核支持)
位置:
/
以下配置必须关闭:
CONFIG_RANDOMIZE_BASE 内核地址空间布局随机化,如果打开,gdb调试的时候会报错访问不了符号对应的内存空间
CONFIG_DEBUG_RODATA 如果打开,内核只读地址空间无法设置断点
CONFIG_DEBUG_SET_MODULE_RONX 如果打开,内核模块只读空间无法设置断点
位置:
最后,对于一些想要调试的内核模块,也可以编译到内核,避免了在gdb调试的时候加载:
CONFIG_VIRTIO
CONFIG_VIRTIO_PCI
CONFIG_VIRTIO_PCI_LEGACY
CONFIG_VIRTIO_BALLON
CONFIG_VIRTIO_MMIO
CONFIG_VIRTIO_BLK
libvirt xml,增加如下标签,qemu命令行中,-s是-gdb tcp::1234缩写
cd /path/to/linux-build
gdb ./vmlinux
如果是高版本vmlinux,此时可以输入lx-symbols
加载符号,并使用该工具,参考内核文档Documentation/dev-tools/gdb-kernel-debugging.rst
(gdb) target remote :1234
(gdb) dir .
b start_kernel
c
console=ttyS0
参数grub2-reboot "CentOS Linux (5.13.0-rc4+) 8 (Core)"
grub2-set-default "CentOS Linux (5.13.0-rc4+) 8 (Core)"