Questions tagged [debugging]

Debugging is a methodical process of finding and fixing bugs in a computer program. **IMPORTANT NOTE:** This tag is ONLY for questions about debugging techniques or the process of debugging itself, NOT for requesting help debugging your code.

Debugging is a methodical process of finding and fixing bugs in a computer program or a piece of electronic hardware, thus making it behave as expected.

Debugging tends to be harder when various subsystems are tightly coupled, as changes in one may cause bugs to emerge in another. Many books have been written about debugging, as it involves numerous aspects, including interactive debugging, control flow, integration testing, log files, monitoring (application, system), memory dumps, profiling, Statistical Process Control, and special design tactics to improve detection while simplifying changes.

Four key techniques for debugging are syntax checking, adding comments, stepping and using breakpoints.

Syntax checking

Many good tools exist, including online-only tools, to check the syntax of your code. Checking the syntax means checking your code obeys the basic rules of the programming language or tool being used (e.g., missing end brackets, no end to an if statement). Syntax checking is done automatically in compiled languages (eg, C, C++, pascal) but not in interpreted or scripted languages (eg, javascript, perl, HTML). Some code editors include syntax highlighting or validation. Syntax checks can also be carried out for some data files or stylesheets, for example the JSON or CSS that your code uses.

Syntax checks will quickly help find spelling mistakes, missing or repeated statements, invalid expressions, and may also give warnings or suggested improvements. Syntax checkers are also known as linters, or code validators. Checking for valid syntax before running can identify errors quickly.

Stepping

Program stepping refers to using a tool to running your code line by line or a section at a time, examining the results including variables, the result of expressions, and the order that the program's steps are executed in. This is particularly useful in programs which do not give an error, or contain infinite loops.

Breakpoints

Breakpoints are particular places in your code in which you want to temporarily stop the code in order to check if it is running correctly so far, for example to check whether a value typed in was correctly stored in a variable you would add a breakpoint immediately after that line, then check the result. Using several different breakpoints allows you to very quickly find the area of the code which is causing the problem.

Breakpoints can be created using a debugging tool, or manually a very simple form of breakpoint could be adding a pop-up messages that waits for you to respond with OK, and can display a message containing program information (e.g., line number, function's name, values of variables).

Using comments

Adding comment to your code is good practice and allows you to describe the purpose of a short piece of code in human-readable form. Programming languages ignore all lines containing comments, but they can help you later to update your code or resolve problems if you add them as you first begin coding.

Applications and Tools for Debugging:

Learning Sources:

Books:

Tutorials:

47496 questions
547
votes
62 answers

"The breakpoint will not currently be hit. The source code is different from the original version." What does this mean?

When debugging in Visual Studio, sometimes I add a breakpoint but it's hollow and VS says "The breakpoint will not currently be hit. The source code is different from the original version." Obviously this prevents me from being able to debug. What…
David
  • 14,678
  • 20
  • 80
  • 145
518
votes
4 answers

What is private bytes, virtual bytes, working set?

I am trying to use the perfmon windows utility to debug memory leaks in a process. This is how perfmon explains the terms: Working Set is the current size, in bytes, of the Working Set of this process. The Working Set is the set of memory pages…
pankajt
  • 7,024
  • 11
  • 33
  • 59
501
votes
6 answers

Swift: print() vs println() vs NSLog()

What's the difference between print, NSLog and println and when should I use each? For example, in Python if I wanted to print a dictionary, I'd just print myDict, but now I have 2 other options. How and when should I use each?
User
  • 20,562
  • 35
  • 99
  • 185
491
votes
6 answers

What is the purpose of the vshost.exe file?

When I create and compile a "Hello, World!" application in C#, I get three files in the Debug folder apart from the main exe (e.g. HelloWorld.exe) HelloWorld.vshost.exe HelloWorld.pdb HelloWorld.vshost.exe.manifest What purpose do these files…
Milen
  • 5,473
  • 5
  • 19
  • 12
474
votes
15 answers

How do I print debug messages in the Google Chrome JavaScript Console?

How do I print debug messages in the Google Chrome JavaScript Console? Please note that the JavaScript Console is not the same as the JavaScript Debugger; they have different syntaxes AFAIK, so the print command in JavaScript Debugger will not work…
Tamas Czinege
  • 110,351
  • 39
  • 146
  • 173
472
votes
20 answers

Is it possible to disable the network in iOS Simulator?

I am trying to debug some inconsistent behaviour I am seeing in an application that gets its primary data from the internet. I don't see the issues in the simulator, just on the device, so I'd like to reproduce the network and connectivity…
Paul Hammond
  • 5,208
  • 3
  • 15
  • 10
470
votes
15 answers

C# if/then directives for debug vs release

In Solution properties, I have Configuration set to "release" for my one and only project. At the beginning of the main routine, I have this code, and it is showing "Mode=Debug". I also have these two lines at the very top: #define DEBUG #define…
NealWalters
  • 14,090
  • 34
  • 109
  • 199
455
votes
10 answers

What is a Python equivalent of PHP's var_dump()?

When debugging in PHP, I frequently find it useful to simply stick a var_dump() in my code to show me what a variable is, what its value is, and the same for anything that it contains. What is a good Python equivalent for this?
Zoredache
  • 31,819
  • 7
  • 40
  • 59
455
votes
8 answers

#if DEBUG vs. Conditional("DEBUG")

Which is better to use, and why, on a large project: #if DEBUG public void SetPrivateValue(int value) { ... } #endif or [System.Diagnostics.Conditional("DEBUG")] public void SetPrivateValue(int value) { ... }
Lucas B
  • 10,961
  • 5
  • 34
  • 50
445
votes
18 answers

JavaScript: How do I print a message to the error console?

How can I print a message to the error console, preferably including a variable? For example, something like: print('x=%d', x);
Mark Harrison
  • 267,774
  • 112
  • 308
  • 434
435
votes
34 answers

What does status=canceled for a resource mean in Chrome Developer Tools?

What would cause a page to be canceled? I have a screenshot of the Chrome Developer Tools. This happens often but not every time. It seems like once some other resources are cached, a page refresh will load the LeftPane.aspx. And what's really odd…
styfle
  • 16,664
  • 22
  • 73
  • 115
433
votes
6 answers

Showing all errors and warnings

UPDATE 2: I have now removed the following from the .php file: I have set display_erros in php.ini as follows: display_errors = On Error reporting is set to the following in php.ini: error_reporting = E_ALL |…
oshirowanen
  • 15,331
  • 77
  • 181
  • 330
423
votes
15 answers

How do you launch the JavaScript debugger in Google Chrome?

When using Google Chrome, I want to debug some JavaScript code. How can I do that?
Kevin Driedger
  • 44,076
  • 15
  • 45
  • 55
423
votes
17 answers

How do you get assembler output from C/C++ source in gcc?

How does one do this? If I want to analyze how something is getting compiled, how would I get the emitted assembly code?
Doug T.
  • 59,839
  • 22
  • 131
  • 193
405
votes
27 answers

How do I debug "Error: spawn ENOENT" on node.js?

When I get the following error: events.js:72 throw er; // Unhandled 'error' event ^ Error: spawn ENOENT at errnoException (child_process.js:1000:11) at Process.ChildProcess._handle.onexit (child_process.js:791:34) What…
laconbass
  • 14,410
  • 6
  • 36
  • 45