code optimization

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
otenki
Posts: 1
Joined: Tue Mar 15, 2011 11:23 am

code optimization

Post by otenki »

I have been starting to write my first os in c but I have a question concerning optimisation by the compiler.
I have written an interupt handler and for testing that it works I have put a simple printk() inside which writes to the screen whenever I receive an interupt.
So my ouput would be something like:
"Interupt fired: 0x03"

When I compile my code with -O however I get a page full of: "Interupt fired:0x0d after the normal 0x3 interupt has been fired.
So I looked at the error part of the Global Protection Fault and checked the address in my kernel disasembly.
And this was somewhere in the .bss section where my GDT pointer is.

So my question is how can optimization affect this and how do I go about debugging this and fixing it?
I'm a bit stuck in this so any help would bring much rejoicing :-)

Thanks,
Otenki
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: code optimization

Post by Combuster »

Reduce the problem first. Can you do "nothing" on an int 3, and see if you still get the GPF afterwards? How much do you need to add back before the problem reappears? At least you'll know what code to investigate deeper (i.e. look at disassemblies and singlestep through it).
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: code optimization

Post by Solar »

Generally speaking, if changing the optimization settings of your compiler breaks your program, it's your code that is broken, not your compiler.

One-in-a-million exceptions to the rule notwithstanding.

Combuster already hinted at the "reduce the problem" approach. I call it Machete Debugging.
Every good solution is obvious once you've found it.
Tosi
Member
Member
Posts: 255
Joined: Tue Jun 15, 2010 9:27 am
Location: Flyover State, United States
Contact:

Re: code optimization

Post by Tosi »

I compile everything with -O2 from the beginning, it helps me catch bugs earlier and faster code is always better.
If I need to debug heavily I will remove the optimization as the optimizer moves things around and can make heavy-duty debugging difficult.
-O3 can be a good way to find bugs, but can also be a good way to create bugs ex nihil. This especially holds true in OS development if you aren't careful with multithreaded code and don't declare necessary variables volatile.

Another, better way is to catch some bugs at compile time. Turning on all compiler warnings and making it treat them as errors is a good way to spot some bugs that you could have spent an hour debugging.
Post Reply