Page 1 of 1

Debugging Code

Posted: Thu Apr 19, 2012 2:06 am
by iansjack
A query about the BabySteps tutorial (in another section of the forums) prompts me to make a recommendation to those learning coding. (Apologies to old hands, to whom this is trivially obvious.)

If you don't understand what your code is doing, there is no substitute for going through it step-by-step, examining what is happening to variables (or memory and registers at the machine level) with each instruction. Once you do this it normally becomes blindingly obvious what is happening (and why it may not be working as you intend). With simple programs you can do this with pencil and paper, but for anything more complicated you need a good debugger.

Visual Studio, for Windows, contains excellent debugging facilities. On Linux you can use gdb (or a GUI front end such as ddd). If you want to debug at a very low level (for example debugging a boot loader or the early stages of OS initialization) you cannot do better than SimNow from AMD (http://developer.amd.com/tools/simnow/P ... fault.aspx), available for both Linux and Windows. This will let you set breakpoints and then single-step through code with a real-time display of memory and register contents.

You can also debug programs running under qemu using gdb. The details are a little too much to list here, but Google will help if you want to do this.

Whatever platform you are developing on, learn how to use the appropriate debugger; it will repay you many-fold in time saved.

Re: Debugging Code

Posted: Thu Apr 19, 2012 2:54 am
by Solar
+{infinity}.

Skill with your editor, the language, and the compiler is nothing without being able to dance the debugger dance.

If you are in any position where getting involved with any kind of cross-platform work is a posibility, do make yourself familiar with GDB. Like Vim, it does seem awkward and antique at first glance, but like Vim it is one of the most powerful tools available once you got the hang of it - and available virtually everywhere.

Re: Debugging Code

Posted: Thu Apr 19, 2012 12:24 pm
by NickJohnson
I agree, but as someone who spent years debugging an OS using only hexadecimal register dumps and disassembly from objdump, I have to say that being able to debug without adequate tooling does come in handy sometimes. At the very least, it is a good mental exercise.

But yeah, GDB is very useful.