Mac下 KGDB连接Linux

kgdb按照资料说可以使用网络(kgdboe)和串口连接(kgdboc)等方式连接。

这里使用后者,资料比较多,下面是自己在Mac上进行配置连接的过程。

先说一下环境:

主机:Mac OSX 10.10.3

虚拟:Parallels Desktop 10.1.2 里面装 Ubuntu Server 14.14.02 (kernel 3.16)

下载kernel源码版本:3.16.7

下载gdb源码版本:7.9

虚拟机上的准备工作

1. 下载kernel源码(kernel.org)

2. apt-get install libncurses5-dev,不装这个下面的menuconfig会提示没有相关头文件

3. make menuconfig进行配置,打开KGDB相关的设置,网上比较多

4. make, make modules_install, make install

(make之前修改源码根目录下的Makefile文件,将编译优化选项O2改为Og,直接O0的话编译会报错)

5. 重启之后原来的PD共享文件夹可能看不到了,这时候可以重新装一遍Parallel Tools(Actions->Reinstall Parallels Tools)

6. 将编译目录tar一下,通过共享文件夹拷到mac主机上便于以后gdb使用(或许只把vmlinux拷到别的源码目录就可以?)

 

Mac上的准备工作

1. 在Parallels Desktop中为Linux虚拟机添加一个串口

注意:source中选择new socket,然后填一个有权限的路径,一般可以放在/tmp目录下,名称任意。Mode选择Server

2. 安装socat

虚拟机机内部是一个串口设备在外部系统中表示为一个unix domain socket(可以进行输入输出),而使用终端模式调试的话,还是要用相应的终端软件去连接到一个终端。socat就是把这个unix domain socket转接到一个虚拟终端。如果是物理机子的话就是直接可以用终端软件通过串口线连接了。

brew install socat

当然这要求电脑上已经装了brew

3. 编译安装gdb

这个不能用brew直接装,也不能用xcode带的lldb,一定要手工编译源码。解压后进入源码目录执行

./configure --target=x86_64-linux-gnu

target表示要调试的目标代码是x86 64 Linux平台上的,如果不指定的话会生成mac平台上的这样是没有办法载入vmlinux映像进行调试的。其实还有个host参数就是gdb运行的平台,这里默认会选择mac平台,因此不用修改。

make
make install

这样得到的可执行文件是x86_64-linux-gnu-gdb,相应的命令行不能直接输入gdb,而要打x86_64-linux-gnu-gdb,程序输出如下:

hgf-mbpr:gdb-7.9 hegaofeng$ x86_64-linux-gnu-gdb 
GNU gdb (GDB) 7.9
Copyright (C) 2015 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=x86_64-apple-darwin14.3.0 --target=x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) 

注意查看‘This GDB was configured as --host...’这一行,应该和上述的配置一样。

 调试连接

1. 设置kgdboc的参数

# echo ttyS0 > /sys/module/kgdboc/parameters/kgdboc

即kgdboc通过虚拟机的ttyS0来连接,就是我们在PD中添加的那个虚拟串口

2. 中断内核

echo g > /proc/sysrq-trigger

3. 在mac中用socat建一个串口设备ttyS0

$ socat UNIX-CONNECT:/tmp/linuxserver_ttys0 pty,link=/tmp/ttyS0

这样就可以通过这个设备和虚拟机内部的串口通信了(PD里添加的串口设备使用socket模式,文件名为/tmp/linuxserver_ttys0)

4. 在mac中打开x86_64-linux-gnu-gdb 参数是编译生成的vmlinux映像

hgf-mbpr:linux-3.16.7 hegaofeng$ x86_64-linux-gnu-gdb vmlinux
GNU gdb (GDB) 7.9
Copyright (C) 2015 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=x86_64-apple-darwin14.3.0 --target=x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from vmlinux...done.

最后表示vmlinux载入成功。

5. 连接虚拟机机内核,输入target remote /tmp/ttyS0, /tmp/ttyS0就是我们用socat建立的

(gdb) target remote /tmp/ttyS0
Remote debugging using /tmp/ttyS0
kgdb_breakpoint () at kernel/debug/debug_core.c:1051
1051        wmb(); /* Sync point after breakpoint */
(gdb)

此时gdb已经连接到了内核,可以输入命令进行调试了。

posted @ 2015-05-15 02:12  卖程序的小歪  阅读(858)  评论(0编辑  收藏  举报