> For the complete documentation index, see [llms.txt](https://richardweiyang-2.gitbook.io/understanding_qemu/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://richardweiyang-2.gitbook.io/understanding_qemu/00-as/07-witness.md).

# 眼见为实

> 纸上得来终觉浅，绝知此事须躬行

讲了这么多概念是时候实际看一看这些数据结构的样子了。这次我们借助程序员的老朋友gdb来帮助我们近距离观察qemu的地址空间。

## 修改.gdbinit

在qemu源码目录下有一个文件.gdbinit， 这是默认gdb启动时加载的脚本。添加如下内容：

```
file x86_64-softmmu/qemu-system-x86_64
set args -nographic
source gdb-script
b pc_memory_init
r
```

修改后，在源码目录下直接运行gdb就可以执行这些命令。其含义是：

* 加载指定可执行文件
* 设定可执行文件的参数
* 加载一个脚本[gdb-script](https://gist.github.com/RichardWeiYang/123ce27f686165dca9a27278384d1081)
* 设置断点在pc\_memory\_init
* 运行

在源码目录下敲入gdb开始我们的探索。

注：请自行编译qemu源码。

## 查看帮助

脚本中主要的函数有三个，分别带有简单的帮助，可以先看一下。

```
(gdb) help dump_address_spaces
Dump a AddressSpace: dump_address_spaces 0|1

Example:

dump_address_spaces 0
dump_address_spaces 1
(gdb) help dump_memory_region
Dump a MemoryRegion: dump_memory_region SYM|ADDRESS

Example:

dump_memory_region system_memory
dump_memory_region 0x5555565036a0
(gdb) help dump_flatview
Dump a FlatView: dump_flatview ADDRESS

Example:

dump_memory_region 0x555556675be0
```

这三个函数分别用来显示AddressSpace, MemoryRegion和FlatView。

## 显示AddressSpace

首先来看看当前qemu中AddressSpace的状况。

```
(gdb) dump_address_spaces 0
AddressSpace : memory(0x5555565036a0)
     Root MR : 0x55555661e300
    FlatView : 0x555556675be0
AddressSpace : I/O(0x555556503640)
     Root MR : 0x555556616800
    FlatView : 0x555556691f60
AddressSpace : cpu-memory-0(0x5555566756e0)
     Root MR : 0x55555661e300
    FlatView : 0x555556675be0
AddressSpace : cpu-smm-0(0x5555566758c0)
     Root MR : 0x555556590000
    FlatView : 0x555556675be0
```

这个命令可以带两个参数，0或1。

* 0: 显示地址空间的根MemoryRegion和FlatView
* 1: 显示地址空间的MemoryRegion树

## 显示MemoryRegion

接着我们可以查看MemoryRegion的情况。

```
(gdb) dump_memory_region 0x55555661e300
Dump MemoryRegion:system
```

这个函数可以接受两种参数，变量名或地址。

在这个例子中，我们传入的是上一步地址空间中得到的一个根MemoryRegion的地址。

## 显示FlatView

看过了MemoryRegion，我们还以看其对应的FlatView。

```
(gdb) dump_flatview 0x555556675be0
[000000000fee00000-000000000fef00000], offset_in_region 0000000000000000
```

这个就是刚才那个根MemoryRegion的一维展开了。

怎么样，这样是不是更清楚了？

最近发现在qemu monitor中已经有类似的功能了。直接输入info mtree，就可以获得内存树结构。
