Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-21-2009, 08:41 AM   #1
Ajit Gunge
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Rep: Reputation: 21
Effective debugging or improving ones debugging skills

Hi Does anyone know how can one improve his debugging skills or how can one narrow down the problem and try to get to the solution ASAP.Or rather how once should approach a problem.I know this is a bit vague question but I want to know how you people approach?
Old 05-21-2009, 09:10 AM   #2
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
I always develop using and IDE and I always figure out how to run the software using the debugger in the IDE. I rarely add console logging statements just to help troubleshoot. I set breakpoints just before the trouble starts and step through the code. It often takes several runs so I'm always updating the breakpoint locations. Debugging someone else's code is harder since its often unclear where to start (or stop).

Not sure what your language is, but Eclipse has support for C/C++ and integrates with the GNU debugger on Linux.
Old 05-21-2009, 09:36 AM   #3
Senior Member
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Originally Posted by Ajit Gunge View Post
Hi Does anyone know how can one improve his debugging skills or how can one narrow down the problem and try to get to the solution ASAP.
You always want to divide and conquer when debugging. If you do not have access to a debugger, you want to put bread crumbs in your code to show you the execution path up to the point of failure.

I usually define a macro like this:
#define BREADCRUMB() \
    fprintf(stderr, "%s:%n: bread crumb\n", __FILE__, __LINE__)
Then you can just copy and paste BREADCRUMB through your your entire code path. When you run your code, you will see the last bread crumb before it crashes or reports an error.

Then you can add more bread crumbs until you have bracketed the problem down to the exact line.

This method is very useful in debugging on embedded targets. On a desktop, you will almost always have access to a source level debugger which will allow you to put a break point before the code that fails. Then you just single step until you find the cause of the problem.

Fixing the actual error is a science unto itself. You need to have a very good understanding of pointers and how stacks work. Most bugs are caused by bad pointer arithmetic or stack overflow. If your bug is not one of those, it will usually be a bad argument to a function.

If you are using GCC, you can detect most errors before you even run your program by passing the "-Wall -Werror" options to GCC. That will tell GCC to report all warnings and treat them as errors. If you fix all the warnings, you will remove 90% of the errors in your code.

I also found a new trick yesterday. If you pass "-Os" to GCC, it will crank up the warning level as it will no longer auto-initialize stack variables for you. So you will see more errors where you failed to initialized a stack variable before use.

If you are using GCC and a debugger, don't forget to pass "-g" to GCC when compiling and when linking. If you forget the latter, the debugging information will not be added to the executable during the link phase.

Last edited by David1357; 05-23-2009 at 09:46 AM. Reason: Fixed broken emphasis
Old 05-22-2009, 09:29 AM   #4
Registered: Apr 2009
Posts: 107

Rep: Reputation: 19
Run valgrind on your compiled code to see if it finds dangling pointer problems. Run your code in a debugger like ddd or whatever you have available, and also use the bread crumbs as mentioned in one of the previous postings. There are many possible problem, so you should train your skills for every possible method of bug tracing.

Linux Archive

Last edited by soleilarw; 06-18-2009 at 04:05 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
debugging a WM? kalleanka Programming 5 12-01-2007 11:44 AM
Need some debugging help Sparkfist Programming 3 06-08-2007 08:41 AM
Difference between kernel - debugging and application debugging topworld Linux - Software 2 03-30-2006 12:50 AM
Improving Linux Skills DanTaylor Linux - Newbie 7 01-22-2006 06:54 AM
Visual Debugging and Linux Kernel Debugging Igor007 Programming 0 09-30-2005 10:33 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:22 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration