Page 1 of 1

The fun in bug tracking!

Posted: Sun Apr 27, 2008 9:40 pm
by joshw
Hello everyone, I'm Josh, I'm 16, and I just started in OS development a few months ago. My OS is going great, working pretty smoothly (a few bugs pop up here and there). Now, let's be honest, no matter how good your OS is, you probably have some strange bugs that were caused by an error in your logic show up that may take a long time to track down. I've
become good at that. It's usually caused by using a wrong variable or bit or something, and it takes forever to track down, right? And it doesn't show up until later... well I thought it would be interesting to log these things so I can look back at them, and I'll post the list here. I'm sure this happens to you alot, right? Please share your experiences. How much of your development time is spent debugging? How often do you find the problem? I find the problem to 100% of them, and it usually takes me a couple of hours to a couple of days to find these things. Well, here is my
list, I hope this becomes an interesting conversation?

The list is ordered from when I started making my OS.

./16bit = my old (newbie) version (dos-like)
./32bit = current progressive version
16bit/osldr/int21.inc: function Int21
The function tested a wrong register for an error,
exiting randomly!

something else interesting in osldr? (Can't remember)

32bit/kernel/fat.c: function read_sector
Wrong pointer arithmetic! *((uint*)start+offset)
instead of *(uint*)(start+offset)

32bit/kernel/kenrel.c: interrupts
Interrupts were enabled after setting up the IDT. It worked fine like that
for a while, but later, only in bochs, a triple fault started occuring
on the instruction after sti. This wasn't obvious, because it was when I
started testing file streams. I think this problem was also occuring earlier,
and I just thought it had to do with a high ch0 PIT frequency? I may have moved
the PIC initialization code right after the IDT init code and just didn't
test it in bochs for a while. It works fine now, so this is my guess for
what the problem was.

4-23-08
32bit/kernel/fat.c: functions pop_cluster, push_cluster: Memory allocation bug
Wouldn't update clusterstacksize, so would always allocate insufficient memory
(clusterstacksize*2*sizeof(unsigned) is always 32, because clusterstacksize itself was never updated)
Took a few days to track down!

32bit/kernel/kernel.c: function page_map
Just remember to invalidate new mapped page tables, along with the new page.

32bit/kernel/kernel.c: function kfindrange
I changed the initialization routines to non-static to make it easy to trace the bug in
bochs, because the compiler doesn't optimize nonstatic functions a certain way. The
bug was then easy to find: kfindrange looked through the page tables without checking
if they existed first.

32bit/osldr/init.inc: function load_kernel
when later modifying this code to calculate the number of pages, I forgot all
about my ES register being based at 0x0, so I forgot to use ES! Segmentation...

32bit/kernel/kernel.c: function page_unmap
Tried to free a physical page using its logical address (when freeing page tables)...

Posted: Sun Apr 27, 2008 10:00 pm
by piranha
Well, I haven't found all my bugs, but they are little ones that don't really matter for now, so I'm not trying.

When there is a bug, it depends on the bug. Simple typos/logic problems I can find fairly quickly, however if it's a specification it could take longer. Or maybe some code I don't quite understand. Luckily, I only have 3 little bugs in my kernel.

I do disagree with your subject, if it's not sarcastic.

-JL

Posted: Sun Apr 27, 2008 10:05 pm
by pcmattman
At the moment I just have one really big bug in my kernel. Anything else is badly implemented algorithms or spaghetti code, but it works (I'm going through once I get GCC ported and cleaning up a lot).

One thing I strongly suggest is setting up a backtrace in your fault handlers. All it takes is unwinding the stack (and some symbol finding code) and it helps to no end when trying to figure out where the bugs are.

I'd also suggest getting a "dprintf" (in C) that writes to a serial port, and redirect that serial port into a file (QEMU is good with this) OR use the port E9 hack in Bochs. It means you can get post-mortem information about the crash and what happened in the lead-up to it, which is invaluable.

Posted: Mon Apr 28, 2008 1:21 am
by JamesM
My time spent debugging has gone through the floor since I implemented our project's kernel debugger.

Being able to trace through the execution of the kernel instruction-by-instruction, seeing a disassembly, stacktrace including parameters and the contents of all registers is a real bonus. Not to mention being able to inspect memory before any instruction is executed to check if the stack is destroyed, etc.

Kernel debuggers are absolutely invaluable, I don't know how I survived for so long without one!

As an example I implemented multithreading in our OS in less than a day - without the debugger it would have been at least 3 days as there were some real hum-dinger bugs in there!

Posted: Mon Apr 28, 2008 5:24 am
by jal
JamesM wrote:My time spent debugging has gone through the floor since I implemented our project's kernel debugger.
How did you go about making a kernel debugger? Sooner or later I'll need one too, but I have honestly not a clue where to start and how to do it.


JAL

Posted: Mon Apr 28, 2008 5:39 am
by JamesM
jal wrote:
JamesM wrote:My time spent debugging has gone through the floor since I implemented our project's kernel debugger.
How did you go about making a kernel debugger? Sooner or later I'll need one too, but I have honestly not a clue where to start and how to do it.


JAL
Someone else mentioned this to me too, and asked for a tutorial/article on how to go about doing it.

The thing is - I can't really see what people don't get about building a kernel debugger. Please don't take this to seem arrogant, It's not meant that way in the slightest, I just see it as a set of simple steps and don't actually understand whereabouts along the way people get lost. I'd really appreciate your help in telling me exactly where you "lose the trail", as it were. It would really help me in designing this article!

I deliberately didn't mention what I did above - I'd really like to get your feedback first before I list stuff and possibly sway your thoughts/decisions!

Cheers,

James

Posted: Mon Apr 28, 2008 8:06 am
by jal
JamesM wrote:I'd really appreciate your help in telling me exactly where you "lose the trail", as it were
Just some topics that I would investigate before starting:
- disassembler: make one or use/adept an existing one
- how to do the debugging on a low level, use int 3, debug registers, or both
- how to integrate in the kernel and how to mix it in with regular scheduling
- how to handle hardware interrupts while debugging (especially when they are or can be part of the normal task switching mechanism)
- how to display debug symbols (parse the dreadful DWARF?)
- how to invoke the debugger? some magic key sequence?

I can think of more given the time, but these are the most important of the top of my head. I think if you would list the 'simple steps' for most of us that would be enough. No need for a fully fledged tutorial (at least not for me), but just some pointers and tips. (Note that I'm not asking you to do so right know, but some article in the near future would benefit many here, I think.)


JAL

Posted: Mon Apr 28, 2008 8:24 am
by AJ
I agree it's not difficult - it just seems time consuming.

I currently have a 'modules' class which tells you the function and module where an exception occurred. The next step is using JamesM's name demangling code (to make it look pretty :) ) and then disassembling. Not having done anything like that before, this sounds like a lot of work - converting opcodes to mnemonics and so on...

Cheers,
Adam

Posted: Mon Apr 28, 2008 8:31 am
by JamesM
Hi, thanks for the response.

I'll answer your points now with what I did personally.
jal wrote:
JamesM wrote:I'd really appreciate your help in telling me exactly where you "lose the trail", as it were
Just some topics that I would investigate before starting:
- disassembler: make one or use/adept an existing one
For MIPS and ARM I made our own (actually pcmattman is working on the ARM one), -x86 is so complex that I decided to port an existing one for the moment: libudis86, which can compile down to a set of seven object files, requiring only sprintf and strlen/strcpy.
- how to do the debugging on a low level, use int 3, debug registers, or both
I use both. We have a function: Processor::breakpoint(); which inlines down to "int3" in x86 or "break" in mips etc etc. That fires up the debugger. Inside the debugger is the option to single step, which is accomplished using the debug registers. I have yet to add breakpoints -it's on the todo list.
- how to integrate in the kernel and how to mix it in with regular scheduling
The regular scheduling part is what we're just coming to now. My design is to, when the debugger is entered, stop all other processors (using an IPI, for example), stop interrupts, and continue like that until the debugger is exited. Because of the way I've designed my threading subsystem, context switches are possible inside the debugger so any thread can be inspected.
- how to handle hardware interrupts while debugging (especially when they are or can be part of the normal task switching mechanism)
Disable interrupts. Remember that IRQs are asserted until EOI'd or masked, so you shouldn't lose anything.
- how to display debug symbols (parse the dreadful DWARF?)
I use DWARF, but not for debug symbols.

I get grub to load our kernel symbol table in (the ELF section) and run through that. We use C++, so I have written my own symbol demangler (it's posted in the "General Programming" subsection, open sourced under the BSD licence).

I use DWARF to accomplish backtraces when base pointers are not available. This can happen when you're not on x86 (MIPS and ARM don't have high-water-mark base pointers) or when -fomit-frame-pointer is enabled. With the DWARF CFI unwind information you can unwind the stack from any location (most notably between the CALL and PUSH EBP instructions in x86, when most backtracers would bork), and it also has the added advantage that it can restore the state of any/all registers as they were at the start of the function.

This is up to the compiler, and I've created a GCC patch which forces debug information to be emitted whenever a function call takes place for any register that gets saved onto the stack.

The upshot of this is that my backtracer is capable of restoring the state of all registers that were saved to make way for function parameters (for parameters which are passed in registers - @see X86_64's ABI and also MIPS and ARM), so I can actually find out what the function's arguments were.

This coupled with the parameter type information I glean from my demangler means that my backtracer is able to give the parameters any function in a backtrace was called with, which is pretty useful!
- how to invoke the debugger? some magic key sequence?
Currently it's done via Processor::breakpoint() statements or whenever an exception occurs, but I have plans to implement a magic key sequence so it can be entered if the system hangs at bootup to inspect things.
I can think of more given the time, but these are the most important of the top of my head. I think if you would list the 'simple steps' for most of us that would be enough. No need for a fully fledged tutorial (at least not for me), but just some pointers and tips. (Note that I'm not asking you to do so right know, but some article in the near future would benefit many here, I think.)


JAL
Cheers,

James

EDIT: Pretty pictures: http://www.jamesmolloy.co.uk/rubbish-bin

Posted: Mon Apr 28, 2008 5:55 pm
by tadada
I track bugs the hard/stupid way. :oops:

I started about 2 weeks ago...
since then I've had about 5 bugs that actually took me a bit to find.

The most recent one was where it would print "loading" and then would stop. I made it print "no bug" as many times as possible between the "loading" message and the next message. Then knowing how many times it should print "no bug" if everything goes correctly I can count my way to the bug. Then I try and comment out the line that I found. (If your wondering indeed commenting it out did fix it. apparently it didn't like setting register dx to 0x10h :? ) To find the error it took me about... 5-10 mins... After sputsing around with virtualPC to make sure that indeed it is my OSes problem. :?

Posted: Tue Apr 29, 2008 5:25 pm
by iammisc
Well it really depends on what type of bug it is. Ironically, the bugs that take me the longest to find are those caused by mistyping(however, these bugs are rare). The ones that take me the second most time to find are synchronization/threading related bugs. Also, bugs in the IPC implementation fit into this category. The simplest bugs are logic errors.

Re: The fun in bug tracking!

Posted: Thu Sep 18, 2008 10:08 am
by joshw
Wow, I made this post when I used to hace internet access, now I have it again, so hooray! Anyway, thanks for all the replies!

OK, so I wouldn't be surprised if you guys forgot about this topic already... LOL

well, yes, the title is sarcastic.

Most of what I listed was when I had only just begun OS development, and it was still very basic. It still kind of is, because I took a long break over the summer :)

I am not ready to implement a kernel debugger, but some of your other suggestions sound good, for example, I already have code to read ther kernel's symbols. I would assume that it would be pretty easy to unwind the stack, but how do you know when you get to the top, do you keep track of the entry point for each thread?

Re: The fun in bug tracking!

Posted: Thu Sep 18, 2008 12:05 pm
by jgraef
I had really bad bugs a few times. It took me a couple of weeks to fix them.

One bug was extremely hard: I rewrote my memory management and something in my paging code was wrong. The error only popped up when I enabled paging, so I couldn't just look for EIP and so on. There wasn't really a way of debugging. So by testing random parts of my code I found out that I just set one bit wrong. The present flag of all pages :oops:

At the moment I have such a bug too. I want to create a new process from an ELF file. The ELF file is mapped into kernel space and removed from the calling process's address space (so the calling process can't free the pages later). Somewhere in the code where it synchronizes the pagedirs the pagefault happens. But I just can't find it and I already work a few weeks on it.

Re: The fun in bug tracking!

Posted: Sat Sep 20, 2008 1:53 am
by Adek336
I just spent 3 weeks debugging a Haiku network driver to find out it was supplied with buffers that didn't have the packet length attribute set.