Page 1 of 1
Help debugging with bochs
Posted: Thu Apr 21, 2005 11:04 am
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. (:
Re:Help debugging with bochs
Posted: Thu Apr 21, 2005 11:12 am
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.
Re:Help debugging with bochs
Posted: Thu Apr 21, 2005 11:32 am
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:
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
Re:Help debugging with bochs
Posted: Fri Apr 22, 2005 3:02 am
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
Re:Help debugging with bochs
Posted: Fri Apr 22, 2005 4:34 am
by bubach
oh... I ndisasm's my kernel to get the address.. :-[
Re:Help debugging with bochs
Posted: Fri Apr 22, 2005 5:45 am
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...