set a breakpoint inside c function using bochs debugger

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
firas981

set a breakpoint inside c function using bochs debugger

Post by firas981 »

how I can set a breakpoint inside c function ?
I know how to breakpoint at a physical address using bochs debugger , but say that I want to breakpoint at the instruction x inside the c function y
How I can do so ?


thanks
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:set a breakpoint inside c function using bochs debugger

Post by Candy »

firas981 wrote: how I can set a breakpoint inside c function ?
I know how to breakpoint at a physical address using bochs debugger , but say that I want to breakpoint at the instruction x inside the c function y
How I can do so ?


thanks
A. use a debugger
B. Disassemble the instruction stream, figure out where your instruction starts & replace it with an interrupting opcode, such as INT3
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:set a breakpoint inside c function using bochs debugger

Post by Brendan »

Hi,

In some cases I find it easiest to stop Bochs with JECXZ, and then use the internal debugger of Bochs (must be enabled when Bochs is compiled).

To make this easier I've got a little C macro:

Code: Select all

#define BOCHSHALT __asm__ __volatile__ (      \
   "pushl %ecx\n\t"            \
   "xor %ecx,%ecx\n"         \
   "0:\n\t"               \
   "jecxz 0b\n\t"            \
   "popl %ecx\n\t"            \
)
When BOCHS reaches this macro you press control+c to enter the debugger, then "set $ecx = 1" to stop the loop. Now you can step through the code an instruction at a time using 's' or 'p'...

While it is possible to end up inside an IRQ handler it does save you from trying to figure out the address of the instruction.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
firas981

Re:set a breakpoint inside c function using bochs debugger

Post by firas981 »

Thank you Brendan , I got benefit of your code .
thanks.
firas981

Re:set a breakpoint inside c function using bochs debugger

Post by firas981 »

BOCHSHALT helped me stop execution at any point inside c source
???but I couldn't get into debugger , why ?
???The answer is that i haven't bochs build with internal
???debugger option .
???
???Okay , I have downloaded bochs source : "bochs-2.1.1.src.rpm" to re-build using
???" ./configure --enable-debugger --enable-disasm " , but the simplest form of rebuilding
???"rpm -rebuilddb " gave me the error :
???"error: db4 error(16) from dbenv->remove: Device or resource busy "
???
???Okay , I've tried another way :
???I have the debugger bochsdbg.exe on Windows and I run it using
???WINE bochsdbg , it is working , and the c macro did its task but here as you
???know ctrl+c stops execution at all , and i didn't find any way to return to
???bochsdebugger command prompt to issue commands .
???
???This is the situation , probably you have a suggestion ..
???
???Thanks
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:set a breakpoint inside c function using bochs debugger

Post by Brendan »

Hi,

I downloaded the Bochs 2.1.1 source as a tar.gz and compiled it on Redhat linux and windows/cygwin without any problems..

./configure --enable-debugger --enable-disasm
make bochs

I don't know what "rpm -rebuilddb" is supposed to do, or how it works...

Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Curufir

Re:set a breakpoint inside c function using bochs debugger

Post by Curufir »

Well it's not going to work in C without inline asm, but my favourite trick for 'setting a breakpoint' in asm for Bochs when playing with the kernel is to just have:

Code: Select all

cli
hlt
sti
Then let Bochs run until it hits the hlt, break out of the run into the debugger and you'll be at precisely the right location.

Once you tell Bochs to continue it will do so at the next instruction (sti).

Only impact this snippet has on machine state is the interrupt flag, and you should already know what that is.
Ytinasni

Re:set a breakpoint inside c function using bochs debugger

Post by Ytinasni »

Curufir wrote:

Code: Select all

cli
hlt
sti
Only impact this snippet has on machine state is the interrupt flag, and you should already know what that is.
Or, if you need to keep the interrupt flag how it is, use:

Code: Select all

pushfd
cli
hlt
popfd
Curufir

Re:set a breakpoint inside c function using bochs debugger

Post by Curufir »

Yup, that's a nicer way of doing it Ytinasni, and leaves the current state of #IF on the stack for you to examine.
firas981

Re:set a breakpoint inside c function using bochs debugger

Post by firas981 »

tar.gz worked nice , rpm didn't ....
I don't know the reason , at any rate bochs is now working well with internal debugger

thanks
Post Reply