QEMU risc-v , log all memory operations

Step 1:

./configure --target-list=riscv64-softmmu --enable-plugins

Step 2: modify tests/plugin/mem.c , add this code in vcpu_mem()

    struct qemu_plugin_hwaddr *hwaddr2 = qemu_plugin_get_hwaddr(meminfo, vaddr);
    const char *name = qemu_plugin_hwaddr_device_name(hwaddr2);
    uint64_t addr = qemu_plugin_hwaddr_phys_addr(hwaddr2);
    g_autoptr(GString) out = g_string_new("");
    
    uint64_t temp=0;
	//unsigned int size=8;
	unsigned int size=qemu_plugin_mem_size_shift(meminfo);  // get the accessed memory size
	if (size==0){
	     size=1;
	}else if (size==1){
	     size=2;
	}else if (size==2){
	     size=4;
	}else{
	     size=8;
	}
    qemu_plugin_read_guest_virt_mem(vaddr, (char *)&temp, size);
    
    if (qemu_plugin_mem_is_store(meminfo)) {
                    g_string_printf(out, "> mem store (%s), 0x%lx, 0x%lx, 0x%lx, %d\n", name, (long unsigned int)vaddr, (long unsigned int)addr, (long unsigned int)temp, size);
    } else {
                    g_string_printf(out, "> mem load(%s), 0x%lx, 0x%lx, 0x%lx, %d\n", name, (long unsigned int)vaddr, (long unsigned int)addr, (long unsigned int)temp, size);
    }
    qemu_plugin_outs(out->str);

Step 3: in xv6-riscv

change this

qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 3M -smp 1 -nographic -global virtio-mmio.force-legacy=false -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 -singlestep -d exec,cpu,nochain,in_asm,int,trace:memory_region_ops_read,trace:memory_region_ops_write -D qemu.log

to

qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 3M -smp 1 -nographic -global virtio-mmio.force-legacy=false -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 -accel tcg,one-insn-per-tb=on -d exec,cpu,nochain,in_asm,int,plugin -plugin ~/workspace/qemu/build/tests/plugin/libmem.so,callback=true -D qemu.log

Then you see this

References

  1. https://blog.csdn.net/JaCenz/article/details/125302647
  2. https://www.qemu.org/docs/master/devel/tcg-plugins.html

Leave a Reply

Your email address will not be published. Required fields are marked *