Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging principles/core topics in C/C++ [closed]

I am interested in devoting a good amount of time to improving my debugging ability and am looking for a list of core topics that I need to cover in order to be versed in the principles of commonly used and advanced debugging/testing techniques.

Initially, I figured I would just read through the gdb documentation and glean debugging techniques from its functionality; however, other than jumping into it to get the line number of a segfault and maybe running bt, months later I am still resorting to mass printf's as my default strategy. I feel this is because I don't have any well defined strategies that I could effect through more sophisticated means.

Although my question is in relation to C/C++, and although I operate within a UNIX environment, I would be willing to look at generalized material, or even topics covered in other languages if they will improve my understanding of key concepts.

like image 623
bqui56 Avatar asked Sep 01 '12 16:09

bqui56


People also ask

What are the 4 stages of debugging?

Isolate the source of the bug. Identify the cause of the bug. Determine a fix for the bug. Apply the fix and test it.

What is debugging in C?

Debugging is a methodical process of finding and reducing the number of bugs (or defects) in a computer program, thus making it behave as originally expected.

What is debugging explain its phases?

Debugging, in computer programming and engineering, is a multistep process that involves identifying a problem, isolating the source of the problem, and then either correcting the problem or determining a way to work around it. The final step of debugging is to test the correction or workaround and make sure it works.


1 Answers

You have multiple direct strategies which you should consider:

  • Mass printfs cry for a logging solution. You have many options here, but logging extensively isn't particularly a bad strategy, it is in fact vital for any form of client-side debugging.
  • Extensively use assertions (and never disable them, even in "release" code). Always write checks for all potential errors and fail as soon as possible (use exceptions in C++ -- always throw, never catch).
  • Learning to master gdb within emacs is useful. Learning how to step the program, how to set up breakpoints and how to inspect local variables is usually more than enough.
  • Unit testing is something to consider too. Especially since small tests are easier to debug, because they are not surrounded by the noise of a fully featured program. Write tests before the code, or better, have someone else write the tests.

More generally, the following points, although not directly related to debugging, will benefit you:

  • Learning how a program is executed (learn eg. about stack frames and a small intro to assembly) may prove useful in certain situations where a bug is corrupting memory. More generally, never stop learning stuff about your environment.
  • In C++, use good practices: RAII, the standard library, failing as soon as possible, etc. This has a strong tendency to reduce debugging effort, esp. since debuggers can pretty print stuff from the standard library. Also, coding a simply as possible has a positive effect on debugging time.
  • Use (distributed) version control. Always. You'll see the benefits once you'll get accustomed to it (eg. in combination with unit tests, you have the almighty git bisect available to you).
like image 200
Alexandre C. Avatar answered Oct 08 '22 18:10

Alexandre C.