Debugging Code
Posted: Thu Apr 19, 2012 2:06 am
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.
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.