Help debugging with bochs

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
Anthony M.

Help debugging with bochs

Post by Anthony M. »

I am curious how i locate addresses of the lines i want the debugger to break out. so far the only thing i have done is 'break 0x100000' and break right at the beginning of my kernel. but then when i hit loops that interated for say 1024 times (paging stuff), its impractical to keeping stepping forever. and there is some stuff in the middle of my kernel i'd like to check. but don't know the break point address to get there. hope this isn't too dumb of a question. (:
AR

Re:Help debugging with bochs

Post by AR »

My favourite trick is CLI, HLT.

Code: Select all

int main()
{
    //Lots of code
    
    __asm__ ("hlt");
    //Code to check
}
When it encounters the halt it will obviously stop then in the terminal you do Ctrl+C which will bring up the debug prompt for you to step and/or trace.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:Help debugging with bochs

Post by Brendan »

Hi,
Anthony M. wrote: I am curious how i locate addresses of the lines i want the debugger to break out. so far the only thing i have done is 'break 0x100000' and break right at the beginning of my kernel. but then when i hit loops that interated for say 1024 times (paging stuff), its impractical to keeping stepping forever. and there is some stuff in the middle of my kernel i'd like to check. but don't know the break point address to get there. hope this isn't too dumb of a question. (:
There's a few methods. The first is to find a better address to break at. How to do this depends on what language, etc you're using (for e.g. with LD you could do "ld **** -M > map.txt").

Another way is to put instructions in the source code to stop the CPU at the right spot. Two common methods are:

Code: Select all

  pushfd
  cli
  hlt
  nop
  popfd
And:

Code: Select all

  push ecx
  mov ecx,0
.zz:
  jecxz .zz
  pop ecx
For the first version you'd stop Bochs (control+C) and then step past the HLT. For Bochs-2.2 pre-releases this generates a PANIC (as it impossible for the CPU to do anything else), where you'd tell it to enter the debugger (rather than 'die'), then step past the HLT.

For the second method you'd stop Bochs with control+C, then type in "set $ecx=1" and start stepping through from there. This works for SMP IFF you're stopping the first CPU (as "set $ecx" only works for the first CPU).

Also, when you are stepping through you can use the 'p' command, which executes up to the instruction following the current instruction (instead of 's' which does a single instruction only). This means if you've got "call somewhere" you can skip past the code that's called, or if you've got "loop .next" you can skip doing the loop ECX times. The 'p' command is also useful when you've got multi-threading happening as it skips IRQs (for e.g. if you do a thread switch during the timer IRQ Bochs would end up skipping the IRQ and everything done by the other threads).

The other thing is you can do use the "s" command to do more than one instruction (e.g. "s 1000" would skip 1000 instructions). This might be useful if you want to skip ahead a bit.


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.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Help debugging with bochs

Post by Pype.Clicker »

well, one possible alternative is to use [tt]objdump -drS <file> | less[/tt] in addition to bochs so that you can see how your code has been assembled. Once you get that, you can set a new "break 0x15004" and then "c"ontinue bochs 'til that point.

Another trick i use in Clicker is to have the system wait for keypresses at various points (e.g. by issueing "anykey()" in the sourcecode). In addition, i have a control interface that allows me to tell what 'kind' of messages are interresting and which are not, and how deep debug display should go (in terms of nested calls).

If you're interested in that technique, you can get a look at Clicker CVS
User avatar
bubach
Member
Member
Posts: 1224
Joined: Sat Oct 23, 2004 11:00 pm
Location: Sweden
Contact:

Re:Help debugging with bochs

Post by bubach »

oh... I ndisasm's my kernel to get the address.. :-[
"Simplicity is the ultimate sophistication."
http://bos.asmhackers.net/ - GitHub
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Help debugging with bochs

Post by Pype.Clicker »

you may also have such a large kernel that it's not feasible to have it fully disassembled. In which case, you might like my krash perl script which checks the .map file generated by the linker to locate the closest .o file and then disassemble _taht_ .o file (adjusting the offset) and search for the precise line that'll match the given address...
Post Reply