How do I track memory access in qemu?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

How do I track memory access in qemu?

Post by mrjbom »

Hi.
I recently encountered a problem with memory corruption during task switching and now I want to find a place where data corruption occurs. I suspect that the stack is "crawling" on the data.
I want to debug the kernel step by step and have qemu print data about where memory access is going. How can I do this? Can QEMU Monitor help with this?
foliagecanine
Member
Member
Posts: 148
Joined: Sun Aug 23, 2020 4:35 pm

Re: How do I track memory access in qemu?

Post by foliagecanine »

Not the QEMU monitor (that I know of).
However, you can connect GDB to it and set a watchpoint.

Code: Select all

qemu-system-i386 ... -s
Starts a GDB server on localhost:1234. You can then use

Code: Select all

(gdb) target remote localhost:1234
However, in my experience, I've found that hardware watchpoints don't always work with QEMU. Software watchpoints do, but they take forever (I once spent half a day waiting for a software watchpoint to happen).

Code: Select all

Hardware Watchpoint (default):
(gdb) watch *memory_address_here
Software Watchpoint:
(gdb) set can-use-hw-watchpoints 0
(gdb) watch *memory_address_here
Even better, if you use the GDB file command you can set watchpoints based on symbols such as

Code: Select all

(gdb) watch variable_name
When it detects a memory change, GDB will break and you can look at the next assembly instructions with either

Code: Select all

QEMU Monitor:
x /6i $eip
or GDB:
(gdb) x /6i $eip
My OS: TritiumOS
https://github.com/foliagecanine/tritium-os
void warranty(laptop_t laptop) { if (laptop.broken) return laptop; }
I don't get it: Why's the warranty void?
User avatar
mrjbom
Member
Member
Posts: 317
Joined: Sun Jul 21, 2019 7:34 am

Re: How do I track memory access in qemu?

Post by mrjbom »

foliagecanine wrote:Not the QEMU monitor (that I know of).
However, you can connect GDB to it and set a watchpoint.

Code: Select all

qemu-system-i386 ... -s
Starts a GDB server on localhost:1234. You can then use

Code: Select all

(gdb) target remote localhost:1234
However, in my experience, I've found that hardware watchpoints don't always work with QEMU. Software watchpoints do, but they take forever (I once spent half a day waiting for a software watchpoint to happen).

Code: Select all

Hardware Watchpoint (default):
(gdb) watch *memory_address_here
Software Watchpoint:
(gdb) set can-use-hw-watchpoints 0
(gdb) watch *memory_address_here
Even better, if you use the GDB file command you can set watchpoints based on symbols such as

Code: Select all

(gdb) watch variable_name
When it detects a memory change, GDB will break and you can look at the next assembly instructions with either

Code: Select all

QEMU Monitor:
x /6i $eip
or GDB:
(gdb) x /6i $eip
Thank you very much for the detailed answer, despite the fact that I didn't need it and found the error in a different way(I needed to control the memory area, and the methods you suggested allowed me to control access only at a specific address), I am sure this will be useful to me in the future.
Post Reply