野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9383|回复: 1

实战9 KGDB调试内核

[复制链接]
发表于 2016-4-3 20:09:30 | 显示全部楼层 |阅读模式
本帖最后由 xcoder 于 2016-4-3 20:17 编辑

实战9 KGDB调试内核


KGDB是一个内核调试工具,它能够进行源码级别的调试,能够修改内存、变量以及查看函数调用栈,与使用GDB调试应用程序十分的类似,还能设置断点、单步运行等功能。使用KGDB需要两台机器,一个是目标机,运行待调试的内核镜像,另一个是主机运行GDB客户端程序,GDB通过串口连接到目标机。对于我们的开发环境,目标机就是pcDuino板,主机是开发用的虚拟机。这一章就来介绍一下KGDB的使用方法。

9.1配置linux内核支持KGDB
Linux内核已经支持了KGDB,只需要进行配置重新编译内核即可。修改kernel配置文件,清单如下:
Kernel  hacking--->
    Compile-time checks and compiler  options  --->
     [ * ] Compile the kernel with debug  info
  
    -*- Magic SysRq key
        (0x1) Enable magic SysRq key  functions by default
     [ * ] Kernel debugging
  
    [ * ] KGDB: kernel debugger  --->
         --- KGDB: kernel debugger
         <*>   KGDB: use kgdb over the serial console

保存配置,重现编译将生成uImage镜像。

9.2 配置虚拟机串口
将串口连接到主机,在设备管理中查看串口名称。如图8.1,本例中使用串口COM1。
image001.png

图9.1 串口设备
VirtualBox中配置串口。如图9.2所示,Port Number选择COM1,这个是虚拟机使用的串口,Port/File Path中填入COM1,即当前主机正在使用的串口设备。
image003.png
图9.2 VirtualBox中设置串口设备

启动虚拟机,安装minicom工具
  # sudo  apt-get install minicom

使用minicom设置串口
  # sudo  minicom -s

出现设置界面,选择Serial portsetup并回车。
image005.png
图9.3 minicom设置界面
按A或a,修改Serial Device为/dev/ttyS0,回车。
按E或e,修改Bps/Par/Bits为115200 8N1,回车。
image006.png
图9.4设置串口属性

再次回车,选择Save setup asdfl回车保存设置。
image008.png
图9.5保存串口属性

选择Exit回车退出minicom工具。
image009.png
图9.6退出minicom

9.3 修改内核启动命令行
将支持KGDB的uImage镜像拷贝到TF卡的启动分区。修改文件uEnv.txt文件,将下面一行
  bootargs=console=ttyS0,115200  earlyprintk root=/dev/mmcblk0p2 rw rootwait


修改为
  bootargs=console=ttyS0,115200 kgdboc=ttyS0,115200 kgdbwait earlyprintk root=/dev/mmcblk0p2 rw rootwait
保存修改。增加的参数告诉内核启用KGDB功能,并从ttyS0(串口)接受GDB客户端的连接。

9.4 GDB调试内核测试
启动pcDuino板,Linux启动后会等待GDB的连接,提示信息如下:
  Serial:  8250/16550 driver, 8 ports, IRQ sharing disabled
  console  [ttyS0] disabled
  1c28000.serial:  ttyS0 at MMIO 0x1c28000 (irq = 33, base_baud = 1500000) is a U6_16550A
  console  [ttyS0] enabled
  kgdb:  Registered I/O driver kgdboc.
  kgdb: Waiting for connection from remote  gdb...

在虚拟机运行GDB客户端连接pcDuino板
  # cd  /home/ubuntu/pcduino/linux
  # sudo  /home/ubuntu/pcduino/gdb-7.6.2/gdb/gdb vmlinux

GDB启动,如下打印
  GNU gdb (GDB)  7.6.2
  Copyright (C)  2013 Free Software Foundation, Inc.
  License GPLv3+:  GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  This is free  software: you are free to change and redistribute it.
  There is NO  WARRANTY, to the extent permitted by law.   Type "show copying"
  and "show  warranty" for details.
  This GDB was  configured as "--host=i686-pc-linux-gnu  --target=arm-linux-gnueabihf".
  For bug reporting  instructions, please see:
  <http://www.gnu.org/software/gdb/bugs/>...
  
Reading symbols from  /home/ubuntu/pcduino/linux/vmlinux...done.
  (gdb)

GDB命令行中设置串口
  (gdb)  set remotebaud 115200
  (gdb)  target remote /dev/ttyS0
  

内核停止运行,在函数arch_kgdb_breakpoint处停下
  Remote debugging using /dev/ttyS0
  kgdb_breakpoint  () at ../kernel/debug/debug_core.c:1050
  1050        arch_kgdb_breakpoint();
  (gdb)  list
  1045     */
  1046    noinline void kgdb_breakpoint(void)
  1047    {
  1048        atomic_inc(&kgdb_setting_breakpoint);
  1049        wmb(); /* Sync point before breakpoint  */
  1050        arch_kgdb_breakpoint();
  1051        wmb(); /* Sync point after breakpoint  */
  1052        atomic_dec(&kgdb_setting_breakpoint);
  1053    }
  1054    EXPORT_SYMBOL_GPL(kgdb_breakpoint);
  (gdb)
  

现在就可以使用GDB命令对内核进行调试了,GDB的使用与调试应用程序类似。



回复

使用道具 举报

发表于 2016-4-5 17:09:19 | 显示全部楼层
帮顶         
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-3-29 05:25 , Processed in 0.030117 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表