内核准备
内核配置
建议从源代码重新编译内核,这样可以更方便地进行源代码级调试;当然自己从发行版官网下载相应内核的调试符号亦可以,通常情况下只用于应急分析,比如crash dump分析等。
自己编译Linux内核的话,最好打开两个选项:
CONFIG_DEBUG_INFO=y
CONFIG_GDB_SCRIPTS=y
可通过make menuconfig来设置内核配置项,分别以5.12及6.1.0为例:
5.12:
6.1.0:

禁止编译优化
无法针对Linux内核禁止全局优化,但可以针对特定的源代码文件做禁止编译优化的处理,可通过以下方式达成:
在需要被调试的源代码文件头部增加下面一行代码:
#pragma GCC optimize ("O0")
或者修改其对应的Makefile,以文件名core.c为例:
CFLAGS_REMOVE_core.o := -O2
CFLAGS_core.o := -O0
镜像准备
建议通过libvirt来管理本地KVM虚拟机,这样使用起来非常方便,如果条件不允许的话亦可用qemu命令行方式来运行。
添加 gdb stub
下面的示例是以命令行方式来运行调试目标:
qemu-system-x86_64 -m 4096 -nographic -net user,hostfwd=tcp::44334-:22 -net nic -hda \
debian-10.7.qcow2 -machine type=pc,accel=kvm -cpu host -smp 4 -gdb tcp:127.0.0.1:44333
“-gdb tcp:127.0.0.1:44333” 就是gdb stub的命令行参数,即gdb可连接localhost:44333来调试目标系统。
如果使用libvirt的话则需要修改QEMU配置,可通过命令virsh edit来进行修改,需要在末尾加上中间4行:
</devices>
<qemu:commandline>
<qemu:arg value='-gdb'/>
<qemu:arg value='tcp:localhost:44333'/>
</qemu:commandline>
</domain>
另外还需要更改虚拟机配置文件的domain type,即将第一行从
<domain type='kvm'>
改为:
<domain type='kvm' xmlns:qemu='http://libvirt.org/schemas/domain/qemu/1.0'>
修改内核启动项
本步骤主要为了禁止内核地址随机化,不然内核符号无法定位并正确绑定。可通过修改grub项完成,在安装新内核过程中会自加增加nokaslr及nopti,当然在编译内核时亦可直接禁止相应功能亦可。
root@T490:~# cat /etc/default/grub | grep nokaslr
GRUB_CMDLINE_LINUX_DEFAULT="noquiet nopti nokaslr console=ttyS0"
最直接的方式就是直接修改/boot/grub/grub.cfg文件,或者在系统启动时手工编译grub启动项亦可:
root@T490:~# cat /boot/grub/grub.cfg | grep nokaslr
linux /boot/vmlinuz-5.10.14 root=UUID=c4072cd3-9ed9-4376-b85c-95d1306e817b ro noquiet nopti nokaslr console=ttyS0,115200
host端准备
前面我们就完成了guest端即调试目标机的配置,然后就是准备host端,可以用gdb,亦可以用vscode等gui工具,甚至可以用Windows Visual Studio配合VisualKernel工具来调试Linux系统。
源代码及调试符号vmlinux
将相应源代码及调试符号vmlinux复制到host端即可,复制时注意软链接问题,比如:
vmlinux-gdb.py -> /BUILD/linux-5.12/scripts/gdb/vmlinux-gdb.py
设置gdb的auto-load路径
建议直接修改 ~/.gdbinit文件,当然手动执行亦可,当处理多个调试机时最好通过.gdbinit来保存配置:
matt@T490 /W/K/debian-10.7> cat ~/.gdbinit
add-auto-load-safe-path /usr/share/gdb/python/gdb/
add-auto-load-safe-path /usr/share/gdb/python/
add-auto-load-safe-path /usr/share/gdb/
add-auto-load-safe-path /BUILD/linux-5.12
set auto-load python-scripts on
source /BUILD/pwndbg/gdbinit.py
我本地的调试环境里安装的是pwndbg,已配置好了python组件
启动gdb
cd /BUILD/linux-5.12; gdb ./vmlinux
在gdb中执行以下执行开启内核调试:
target remote :44333
然后gdb会自动加载pwndbg并中断目标机:

在内核gdb scripts加载成功的情况下可以执行lx系列扩展命令以获取内核信息:

可通过apropos lx查询相应的gdb scripts扩展命令,如lx-lsmod, 变量:$lx_current():
gdb> apropos lx
function lx_clk_core_lookup -- Find struct clk_core by name
function lx_current -- Return current task.
function lx_device_find_by_bus_name -- Find struct device by bus and name (both strings)
function lx_device_find_by_class_name -- Find struct device by class and name (both strings)
function lx_module -- Find module by name and return the module variable.
function lx_per_cpu -- Return per-cpu variable.
function lx_rb_first -- Lookup and return a node from an RBTree
function lx_rb_last -- Lookup and return a node from an RBTree.
function lx_rb_next -- Lookup and return a node from an RBTree.
function lx_rb_prev -- Lookup and return a node from an RBTree.
function lx_task_by_pid -- Find Linux task by PID and return the task_struct variable.
function lx_thread_info -- Calculate Linux thread_info from task variable.
function lx_thread_info_by_pid -- Calculate Linux thread_info from task variable found by pid
lx-clk-summary -- Print clk tree summary
lx-cmdline -- Report the Linux Commandline used in the current kernel.
lx-configdump -- Output kernel config to the filename specified as the command
lx-cpus -- List CPU status arrays
lx-device-list-bus -- Print devices on a bus (or all buses if not specified)
lx-device-list-class -- Print devices in a class (or all classes if not specified)
lx-device-list-tree -- Print a device and its children recursively
lx-dmesg -- Print Linux kernel log buffer.
lx-fdtdump -- Output Flattened Device Tree header and dump FDT blob to the filename
lx-genpd-summary -- Print genpd summary
lx-iomem -- Identify the IO memory resource locations defined by the kernel
lx-ioports -- Identify the IO port resource locations defined by the kernel
lx-list-check -- Verify a list consistency
lx-lsmod -- List currently loaded modules.
lx-mounts -- Report the VFS mounts of the current process namespace.
lx-ps -- Dump Linux tasks.
lx-symbols -- (Re-)load symbols of Linux kernel and currently loaded modules.
lx-timerlist -- Print /proc/timer_list
lx-version -- Report the Linux Version of the current kernel.

gdb常用命令及调试技巧
常用命令
常用gdb指令:bt, disass, step, next, break/delete, print, x/[i/g/d]等,具体可查询gdb命令手册
断点设置
使用break命令可设置断点,支持以下几种命令格式:
- break 函数名: break dump_stack
- break source_file:line_number: break smith_hook.c:70
- break 逻辑地址:break 0xffffffff81003010,可以强制break跳过符号/函数名识别
针对模块的调试,可以考虑在模块中增加不常用内核函数的调用,如dump_stack;模块加载前是无法设置断点的,但内核函数的调用是可以的,这样就可以在模块调用指定内核函数时触发断点,然后再通过add-symbol-file /path_to/ko_file base_address,base_address或通过lx-lsmod查询得到,即模块在内核中的内存位置,之后就可以访问模块内的符号了,如全局变量或内部函数
X86_64位环境调试32位虚拟机
需要在attach前设置目标及当前系统架构,如 set architecture i386:x86-64。建议安装gdb-multiarch,以免系统自带gdb不支持多种架构:
pwndbg> target remote :44323
Remote debugging using :44323
warning: Selected architecture i386 is not compatible with reported target architecture i386:x86-64
warning: Architecture rejected target-supplied description
Remote 'g' packet reply is too long (expected 312 bytes, got 608 bytes): 102b91c10......0000
pwndbg> set architecture i386:x86-64
The target architecture is set to "i386:x86-64".
pwndbg> target remote :44323
Remote debugging using :44323
0x00000000c1912b23 in default_idle () at arch/x86/kernel
мелбет кз бонус на ставки мелбет кз бонус на ставки
This is the perfect website for anybody who wishes to understand this topic. You know so much its almost hard to argue with you (not that I really would want to…HaHa). You certainly put a brand new spin on a topic that has been discussed for years. Great stuff, just wonderful!
https://teletype.in/@avtobloggerua/UJyFYOkOA7I
mostbet stavka limit mostbet47183.help
мелбет купон ставок http://melbet95634.help
1win dice game http://www.1win5529.ru
мостбет пополнить сомами https://mostbet80295.help/
pin-up convertir bono a saldo http://pinup62718.help
pin-up hisob to‘ldirish http://pinup08694.help/
1win error 403 1win error 403
mines melbet http://www.melbet15709.help
мелбет кз купон ставок http://www.melbet15709.help
mostbet email tasdiqlash https://mostbet47183.help
pin-up ilova xato kodi http://pinup08694.help
depositar con Plin en 1win depositar con Plin en 1win
Melalui 9lib, pengguna dapat menemukan pilihan terbaik situs slot gacor yang menawarkan RTP tinggi serta transaksi terpercaya. Situs ini membantu pemain menemukan platform terbaik, mengulas bonus menarik, serta memberikan informasi tentang berbagai jenis permainan. Dikembangkan khusus untuk pengguna Indonesia, situs ini berfokus pada keamanan, fair play, dan potensi kemenangan optimal.
DETIK365
melbet kz трансляции http://www.melbet15709.help
Hello! This post could not be written any better! Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this page to him. Pretty sure he will have a good read. Many thanks for sharing!
Betify Connexion
mostbet refund https://mostbet47183.help/
Why viewers still use to read news papers when in this technological globe all is available on web?
Paris sportif crypto
mostbet dollar https://www.mostbet47183.help
pin-up android app rasmiy pin-up android app rasmiy
Hi there, for all time i used to check web site posts here early in the daylight, because i like to find out more and more.
Tower Rush
pin-up saytına daxil ol http://pinup48127.help
cómo recuperar mi cuenta 1win cómo recuperar mi cuenta 1win
pin-up turnir jadvali pinup08694.help
1win deposit not working 1win deposit not working
I got this web page from my pal who told me on the topic of this website and at the moment this time I am visiting this web site and reading very informative content at this place.
adult xxx video porn site xxx sex video
juego lucky jet pin up http://www.pinup62718.help
мостбет-kg http://mostbet80295.help
1win withdrawal https://www.1win5529.ru
игра краш melbet игра краш melbet
pin up cl http://pinup62718.help
Hi Dear, are you truly visiting this web page on a regular basis, if so after that you will without doubt take pleasant know-how.
Buy viagra here buy cialis on this site
мостбет скачать 2026 http://www.mostbet80295.help
мелбет статистика https://www.melbet95634.help
1win join now http://www.1win5529.ru
1win apuesta gratis http://www.1win05634.help
crear cuenta en pin up crear cuenta en pin up
мостбет официальный сайт Кыргызстан мостбет официальный сайт Кыргызстан
melbet plinko играть melbet plinko играть
mostbet rasmiy promo kod http://www.mostbet47183.help
pin-up iphone uchun app http://pinup08694.help/
pin-up ilova yuklab olish bepul http://pinup08694.help/
aviator 1win retiros aviator 1win retiros
mines en 1win http://1win05634.help/
1win esports http://1win5529.ru
мостбет пополнение mastercard http://mostbet65920.help
pinup móvil https://pinup62718.help
мостбет промокод для Кыргызстана мостбет промокод для Кыргызстана
melbet visa пополнение melbet visa пополнение