Debugging Code

Programming, for all ages and all languages.
Post Reply
User avatar
iansjack
Member
Member
Posts: 4687
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Debugging Code

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Debugging Code

Post 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.
Every good solution is obvious once you've found it.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Debugging Code

Post 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.
Post Reply