How to Trace Qemu

810阅读 0评论2014-04-21 embeddedlwp
分类:LINUX

在Qemu中安装一个系统来查看Qemu对设备的emulate情况,这里不要安装一个发行版之类的,直接跑kernel,如果自己做rootfs和磁盘就太麻烦了,直接从网上下一个linux-0.2.img,这里的rootfs已经做好了。

wget

bun2 linux-0.2.img.bz2
写一个脚本来运行qemu,qemu.sh:
#!/bin/bash
qemu-system-i386 -s -kernel $1 -inid $2 -hda ./linux-0.2.img -append root=/dev/sda -boot c -monitor stdio -trace events=/tmp/events
对一些参数的解释:
-kernel bzImage use 'bzImage' as kernel image
-append cmdline use 'cmdline' as kernel command line
-initrd    use 'file' as initial ram disk
-monitor dev    redirect the monitor to char device 'dev'
-boot [order=drives][,oe=drives][,menu=on|off]??  
                'drives': floppy (a), hard disk (c), CD-ROM (d), network (n)
-trace [events=][,file=]
当然了,这些参数的信息可以通过qemu-system-i386 --help来查看,下面来运行一下Qemu:
root@hacker:/opt/qemu/bin# ./qemu.sh vmlinuz-3.0.0-12-generic initrd.img-3.0.0-12-generic 
qemu-system-i386: pci_a_option_rom: failed to romfile "pxe-rtl8139.bin"
这里说找不到pxe-rtl8139.bin,我们安装一下kvm-pxe就好了。
apt-get install kvm-pxe
再运行一次:
root@hacker:/opt/qemu/bin# ./qemu.sh vmlinuz-3.0.0-12-generic initrd.img-3.0.0-12-generic 
QEMU 0.14.1 monitor - type 'help' for infoation

(qemu) 

注意qemu有一个build in monitor,可以在启动参数中用-monitor stdio把它重定向出来,也可以在运行的时候使用ctrl-alt-2.
(qemu) info pci
  Bus  0, device   0, function 0:
    Host brge: PCI device 8086:1237
      id ""
  Bus  0, device   1, function 0:
    ISA bridge: PCI device 8086:7000
      id ""
  Bus  0, device   1, function 1:
    IDE controller: PCI device 8086:7010
      BAR4: I/O at 0xc000 [0xc00f].
      id ""
  Bus  0, device   1, function 3:
    Bridge: PCI device 8086:7113
      IRQ 9.
      id ""
  Bus  0, device   2, function 0:
    VGA controller: PCI device 1013:00b8
      BAR0: 32 bit prefetchable memory at 0xf0000000 [0xf1ffffff].
      BAR1: 32 bit memory at 0xf2000000 [0xf2000fff].
      BAR6: 32 bit memory at 0xffffffffffffffff [0x0000fffe].
      id ""
  Bus  0, device   3, function 0:
    Ethernet controller: PCI device 10ec:8139
      IRQ 11.
      BAR0: I/O at 0xc100 [0xc1ff].
      BAR1: 32 bit memory at 0xf2020000 [0xf20200ff].
      BAR6: 32 bit memory at 0xffffffffffffffff [0x00007ffe].

      id ""

在guest os中查看可以看到结果是一样的:

如果想查看更多信息直接输入info,此时会提示可以查看那些。

在源码的文档中docs/tracing.txt讲述了如何trace qemu。

1)在配置的时候需要添加trace的功能:

./configure --trace-backend=simple

make

2)创建一个文件添加你想trace的events:

echo pci_default__config > /tmp/events

echo pci_default_read_config > /tmp/events

看一下我的那个文件:

3)运行qemu并添加上选项

qemu -trace events=/tmp/events

4)如果想对事件进行查看,使用qemu源码中的一个脚本:
./simpletrace.py trace-events trace-*

当然了,上述方法是trace已经添加到trace-events这个文件中的events,如果想添加其他events我们需要修改源码,在源码中添加trace_****,然后在trace-events文件中添加你trace的函数。

在要trace的函数中添加trace_****:

在trace-events函数中添加要trace的函数和要trace的参数,注意格式:

运行脚本进行查看events:

[root@chinaltragon scripts]# ./simpletrace.py ../trace-events ../../Attack/bin/trace-7381 | kvm_handle_io

在qemu启动之前最好将需要trace的函数名添加到/tmp/events文件中,当然在qemu运行的时候也可以:

(qemu) trace-event u_out on

(qemu) trace-event cpu_in on

开启对event的trace

trace-event  **** on

关闭对event的trace

trace-event **** off

查看一下:

[root@chinaltcdragon scripts]# ./simpletrace.py ../trace-events ../../Attack/bin/trace-7381 | grep cpu_out

上一篇:QEMU/KVM中磁盘cache关闭(cache=off/none)
下一篇:qemu-kvm-0.12.3主要函数路径