What is a debugger and how can it help me diagnose problems?











up vote
55
down vote

favorite
20












This is intended to be a general purpose question to assist new programmers who have a problem with a program, but do not know how to use a debugger to diagnose the cause of the problem.



This question covers two classes of more specific question:




  • When I run my program, it does not produce the output I expect for the input I gave it

  • When I run my program, it crashes and gives me a stack trace. I have examined the stack trace, but I still do not know the cause of the problem because the stack trace does not provide me with enough information.










share|improve this question




















  • 2




    Nice work - it would also be good to have a related "go to" Q & A for debugging techniques, e.g. using a debugger, other debug tools (e.g. valgrind), strategic printfs, stress testing, divide and conquer, etc.
    – Paul R
    Aug 19 '14 at 13:52








  • 1




    I agree with @PaulR, the FAQ should contain stuff like this.
    – Nicu Stiurca
    Oct 29 '14 at 18:33















up vote
55
down vote

favorite
20












This is intended to be a general purpose question to assist new programmers who have a problem with a program, but do not know how to use a debugger to diagnose the cause of the problem.



This question covers two classes of more specific question:




  • When I run my program, it does not produce the output I expect for the input I gave it

  • When I run my program, it crashes and gives me a stack trace. I have examined the stack trace, but I still do not know the cause of the problem because the stack trace does not provide me with enough information.










share|improve this question




















  • 2




    Nice work - it would also be good to have a related "go to" Q & A for debugging techniques, e.g. using a debugger, other debug tools (e.g. valgrind), strategic printfs, stress testing, divide and conquer, etc.
    – Paul R
    Aug 19 '14 at 13:52








  • 1




    I agree with @PaulR, the FAQ should contain stuff like this.
    – Nicu Stiurca
    Oct 29 '14 at 18:33













up vote
55
down vote

favorite
20









up vote
55
down vote

favorite
20






20





This is intended to be a general purpose question to assist new programmers who have a problem with a program, but do not know how to use a debugger to diagnose the cause of the problem.



This question covers two classes of more specific question:




  • When I run my program, it does not produce the output I expect for the input I gave it

  • When I run my program, it crashes and gives me a stack trace. I have examined the stack trace, but I still do not know the cause of the problem because the stack trace does not provide me with enough information.










share|improve this question















This is intended to be a general purpose question to assist new programmers who have a problem with a program, but do not know how to use a debugger to diagnose the cause of the problem.



This question covers two classes of more specific question:




  • When I run my program, it does not produce the output I expect for the input I gave it

  • When I run my program, it crashes and gives me a stack trace. I have examined the stack trace, but I still do not know the cause of the problem because the stack trace does not provide me with enough information.







debugging language-agnostic






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 23 '17 at 12:34









Community

11




11










asked Aug 19 '14 at 13:49









Raedwald

25.8k2295155




25.8k2295155








  • 2




    Nice work - it would also be good to have a related "go to" Q & A for debugging techniques, e.g. using a debugger, other debug tools (e.g. valgrind), strategic printfs, stress testing, divide and conquer, etc.
    – Paul R
    Aug 19 '14 at 13:52








  • 1




    I agree with @PaulR, the FAQ should contain stuff like this.
    – Nicu Stiurca
    Oct 29 '14 at 18:33














  • 2




    Nice work - it would also be good to have a related "go to" Q & A for debugging techniques, e.g. using a debugger, other debug tools (e.g. valgrind), strategic printfs, stress testing, divide and conquer, etc.
    – Paul R
    Aug 19 '14 at 13:52








  • 1




    I agree with @PaulR, the FAQ should contain stuff like this.
    – Nicu Stiurca
    Oct 29 '14 at 18:33








2




2




Nice work - it would also be good to have a related "go to" Q & A for debugging techniques, e.g. using a debugger, other debug tools (e.g. valgrind), strategic printfs, stress testing, divide and conquer, etc.
– Paul R
Aug 19 '14 at 13:52






Nice work - it would also be good to have a related "go to" Q & A for debugging techniques, e.g. using a debugger, other debug tools (e.g. valgrind), strategic printfs, stress testing, divide and conquer, etc.
– Paul R
Aug 19 '14 at 13:52






1




1




I agree with @PaulR, the FAQ should contain stuff like this.
– Nicu Stiurca
Oct 29 '14 at 18:33




I agree with @PaulR, the FAQ should contain stuff like this.
– Nicu Stiurca
Oct 29 '14 at 18:33












2 Answers
2






active

oldest

votes

















up vote
25
down vote



accepted










A debugger is a program that can examine the state of your program while your program is running. The technical means it uses for doing this are not important for understanding the basics of how to use a debugger. You can use a debugger to halt the execution of your program when it reaches a particular place in your code, then examine the values of the variables in the program. You can use a debugger to run your program very slowly, one line of code at a time (called single stepping), while you examine the values of its variables.



By doing this you can discover whether a variable has the wrong value, and where in your program its value changed to the wrong value.



Using single stepping you can also discover whether the control flow is as you expect. For example, whether an if branch executed when you expect it ought to be.



The specifics of using a debugger depend on the debugger and, to a lesser degree, the programming language you are using.




  • You can attach a debugger to a process already running your program. You might do if your program is stuck.


  • In practice it is often easier to run your program under the control of a debugger from the very start.


  • You indicate where your program should stop executing by indicating the source-code file and line number of the line at which execution should stop, or by indicating the name of the method/function at which the program should stop (if you want to stop as soon as execution enters the method). The technical means that the debugger uses to cause your program to stop is called a breakpoint and this process is called setting a breakpoint.


  • Most modern debuggers are part of an IDE and provide you with a convenient GUI for examining the source code and variables of your program, with a point-and-click interface for setting breakpoints, running your program and single stepping it.


  • Using a debugger can be very difficult unless your program executable or bytecode files include debugging symbol information. You might have to compile (or recompile) your program slightly differently to ensure that information is present.







share|improve this answer



















  • 1




    This is incomplete as it misses the most important debugger of all, one that has the potential to reduce the number of questions on Stackoverflow very significantly (I predict by at least 20%) - javascript debuggers: firebug, Chrome, Firefox, IE9+ integrated debugger, IE8- Visual Studio, etc.
    – slebetman
    Aug 25 '14 at 2:45






  • 1




    Also for node.js - node inspector. But node.js programmers don't ask as many basic and/or fix-my-code questions as general javascript programmers.
    – slebetman
    Aug 25 '14 at 2:47




















up vote
21
down vote













I want to add that a debugger isn't always the perfect solution, and shouldn't always be the go-to solution to debugging. Here are a few cases where a debugger might not work for you:




  • The part of your program which fails is really large (poor modularization, perhaps?) and you're not exactly sure where to start stepping through the code. Stepping through all of it might be too time consuming.

  • Your program uses a lot of callbacks and other non-linear flow control methods, which makes the debugger confused when you step through it.

  • Your program is multi-threaded. Or even worse, your problem is caused by a race condition.

  • The code that has the bug in it runs many times before it bugs out. This can be particularly problematic in main loops, or worse yet, in physics engines, where the problem could be numerical. Even setting a breakpoint, in this case, would simply have you hitting it many times, with the bug not appearing.

  • Your program must run in real time. This is a big issue for programs that connect to the network. If you set up a breakpoint in your network code, the other end isn't going to wait for you to step through, it's simply going to time out. Programs which rely on the system clock, e.g. games with frame skip, aren't much better off either.

  • Your program performs some form of destructive actions, like writing to files or sending e-mails, and you'd like to limit the number of times you need to run through it.

  • You can tell that your bug is caused by incorrect values arriving at function X, but you don't know where these values come from. Having to run through the program, again and again, setting breakpoints farther and farther back, can be a huge hassle. Especially if function X is called from many places throughout the program.


In all of these cases, either having your program stop abruptly could cause the end results to differ, or stepping through manually in search of the one line where the bug is caused is too much of a hassle. This can equally happen whether your bug is incorrect behavior, or a crash. For instance, if memory corruption causes a crash, by the time the crash happens, it's too far from where the memory corruption first occurred, and no useful information is left.



So, what are the alternatives?



Simplest is simply logging and assertions. Add logs to your program at various points, and compare what you get with what you're expecting. For instance, see if the function where you think there's a bug is even called in the first place. See if the variables at a start of a method are what you think they are. Unlike breakpoints, it's okay for there to be many log lines in which nothing special happens. You can simply search through the log afterwards. Once you hit a log line that's different from what you're expecting, add more in the same area. Narrow it down farther and farther, until it's small enough to be able to log every line in the bugged area.



Assertions can be used to trap incorrect values as they occur, rather than once they have a visible effect to the end user. The quicker you catch an incorrect value, the closer you are to the line that produced it.



Refactor and unit test. If your program is too big, it might be worthwhile to test it one class or one function at a time. Give it inputs, and look at the outputs, and see which are not as you're expecting. Being able to narrow down a bug from an entire program to a single function can make a huge difference in debugging time.



In case of memory leaks or memory stomping, use appropriate tools which are able to analyze and detect these at runtime. Being able to detect where the actual corruption occurs is the first step. After this, you can use logs to work your way back to where incorrect values were introduced.



Remember that debugging is a process going backwards. You have the end result - a bug - and find the cause, which preceded it. It's about working your way backwards and, unfortunately, debuggers only step forwards. This is where good logging and postmortem analysis can give you much better results.






share|improve this answer

















  • 3




    This would be a good answer... of a different question. It is a bad answer for this question. Perhaps you should ask that question and post this as a reply to it.
    – Raedwald
    Apr 7 '15 at 6:53






  • 3




    The actual question is described as "assist new programmers who have a problem with a program", "it does not produce the output I expect" and "I have examined the stack trace, but I still do not know the cause of the problem". All of which are aided by this answer. Additionally, when explaining what a debugger does, it's equally important to explain what it doesn't do.
    – SlugFiller
    Apr 7 '15 at 14:51






  • 3




    Great answer. I always used the debugger as main tool to find bugs. But now i'm working in a project where a huge infrastructure component is using many threads and a lot of network code(client/server) and notice that the debugger is the last thing that helps me. You have mentioned a lot of things where you should really use a different tool instead of relying on your good old debugger (most important: logging).
    – Rango
    Nov 24 '17 at 10:02













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25385173%2fwhat-is-a-debugger-and-how-can-it-help-me-diagnose-problems%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
25
down vote



accepted










A debugger is a program that can examine the state of your program while your program is running. The technical means it uses for doing this are not important for understanding the basics of how to use a debugger. You can use a debugger to halt the execution of your program when it reaches a particular place in your code, then examine the values of the variables in the program. You can use a debugger to run your program very slowly, one line of code at a time (called single stepping), while you examine the values of its variables.



By doing this you can discover whether a variable has the wrong value, and where in your program its value changed to the wrong value.



Using single stepping you can also discover whether the control flow is as you expect. For example, whether an if branch executed when you expect it ought to be.



The specifics of using a debugger depend on the debugger and, to a lesser degree, the programming language you are using.




  • You can attach a debugger to a process already running your program. You might do if your program is stuck.


  • In practice it is often easier to run your program under the control of a debugger from the very start.


  • You indicate where your program should stop executing by indicating the source-code file and line number of the line at which execution should stop, or by indicating the name of the method/function at which the program should stop (if you want to stop as soon as execution enters the method). The technical means that the debugger uses to cause your program to stop is called a breakpoint and this process is called setting a breakpoint.


  • Most modern debuggers are part of an IDE and provide you with a convenient GUI for examining the source code and variables of your program, with a point-and-click interface for setting breakpoints, running your program and single stepping it.


  • Using a debugger can be very difficult unless your program executable or bytecode files include debugging symbol information. You might have to compile (or recompile) your program slightly differently to ensure that information is present.







share|improve this answer



















  • 1




    This is incomplete as it misses the most important debugger of all, one that has the potential to reduce the number of questions on Stackoverflow very significantly (I predict by at least 20%) - javascript debuggers: firebug, Chrome, Firefox, IE9+ integrated debugger, IE8- Visual Studio, etc.
    – slebetman
    Aug 25 '14 at 2:45






  • 1




    Also for node.js - node inspector. But node.js programmers don't ask as many basic and/or fix-my-code questions as general javascript programmers.
    – slebetman
    Aug 25 '14 at 2:47

















up vote
25
down vote



accepted










A debugger is a program that can examine the state of your program while your program is running. The technical means it uses for doing this are not important for understanding the basics of how to use a debugger. You can use a debugger to halt the execution of your program when it reaches a particular place in your code, then examine the values of the variables in the program. You can use a debugger to run your program very slowly, one line of code at a time (called single stepping), while you examine the values of its variables.



By doing this you can discover whether a variable has the wrong value, and where in your program its value changed to the wrong value.



Using single stepping you can also discover whether the control flow is as you expect. For example, whether an if branch executed when you expect it ought to be.



The specifics of using a debugger depend on the debugger and, to a lesser degree, the programming language you are using.




  • You can attach a debugger to a process already running your program. You might do if your program is stuck.


  • In practice it is often easier to run your program under the control of a debugger from the very start.


  • You indicate where your program should stop executing by indicating the source-code file and line number of the line at which execution should stop, or by indicating the name of the method/function at which the program should stop (if you want to stop as soon as execution enters the method). The technical means that the debugger uses to cause your program to stop is called a breakpoint and this process is called setting a breakpoint.


  • Most modern debuggers are part of an IDE and provide you with a convenient GUI for examining the source code and variables of your program, with a point-and-click interface for setting breakpoints, running your program and single stepping it.


  • Using a debugger can be very difficult unless your program executable or bytecode files include debugging symbol information. You might have to compile (or recompile) your program slightly differently to ensure that information is present.







share|improve this answer



















  • 1




    This is incomplete as it misses the most important debugger of all, one that has the potential to reduce the number of questions on Stackoverflow very significantly (I predict by at least 20%) - javascript debuggers: firebug, Chrome, Firefox, IE9+ integrated debugger, IE8- Visual Studio, etc.
    – slebetman
    Aug 25 '14 at 2:45






  • 1




    Also for node.js - node inspector. But node.js programmers don't ask as many basic and/or fix-my-code questions as general javascript programmers.
    – slebetman
    Aug 25 '14 at 2:47















up vote
25
down vote



accepted







up vote
25
down vote



accepted






A debugger is a program that can examine the state of your program while your program is running. The technical means it uses for doing this are not important for understanding the basics of how to use a debugger. You can use a debugger to halt the execution of your program when it reaches a particular place in your code, then examine the values of the variables in the program. You can use a debugger to run your program very slowly, one line of code at a time (called single stepping), while you examine the values of its variables.



By doing this you can discover whether a variable has the wrong value, and where in your program its value changed to the wrong value.



Using single stepping you can also discover whether the control flow is as you expect. For example, whether an if branch executed when you expect it ought to be.



The specifics of using a debugger depend on the debugger and, to a lesser degree, the programming language you are using.




  • You can attach a debugger to a process already running your program. You might do if your program is stuck.


  • In practice it is often easier to run your program under the control of a debugger from the very start.


  • You indicate where your program should stop executing by indicating the source-code file and line number of the line at which execution should stop, or by indicating the name of the method/function at which the program should stop (if you want to stop as soon as execution enters the method). The technical means that the debugger uses to cause your program to stop is called a breakpoint and this process is called setting a breakpoint.


  • Most modern debuggers are part of an IDE and provide you with a convenient GUI for examining the source code and variables of your program, with a point-and-click interface for setting breakpoints, running your program and single stepping it.


  • Using a debugger can be very difficult unless your program executable or bytecode files include debugging symbol information. You might have to compile (or recompile) your program slightly differently to ensure that information is present.







share|improve this answer














A debugger is a program that can examine the state of your program while your program is running. The technical means it uses for doing this are not important for understanding the basics of how to use a debugger. You can use a debugger to halt the execution of your program when it reaches a particular place in your code, then examine the values of the variables in the program. You can use a debugger to run your program very slowly, one line of code at a time (called single stepping), while you examine the values of its variables.



By doing this you can discover whether a variable has the wrong value, and where in your program its value changed to the wrong value.



Using single stepping you can also discover whether the control flow is as you expect. For example, whether an if branch executed when you expect it ought to be.



The specifics of using a debugger depend on the debugger and, to a lesser degree, the programming language you are using.




  • You can attach a debugger to a process already running your program. You might do if your program is stuck.


  • In practice it is often easier to run your program under the control of a debugger from the very start.


  • You indicate where your program should stop executing by indicating the source-code file and line number of the line at which execution should stop, or by indicating the name of the method/function at which the program should stop (if you want to stop as soon as execution enters the method). The technical means that the debugger uses to cause your program to stop is called a breakpoint and this process is called setting a breakpoint.


  • Most modern debuggers are part of an IDE and provide you with a convenient GUI for examining the source code and variables of your program, with a point-and-click interface for setting breakpoints, running your program and single stepping it.


  • Using a debugger can be very difficult unless your program executable or bytecode files include debugging symbol information. You might have to compile (or recompile) your program slightly differently to ensure that information is present.








share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 9 at 11:36

























answered Aug 19 '14 at 13:49









Raedwald

25.8k2295155




25.8k2295155








  • 1




    This is incomplete as it misses the most important debugger of all, one that has the potential to reduce the number of questions on Stackoverflow very significantly (I predict by at least 20%) - javascript debuggers: firebug, Chrome, Firefox, IE9+ integrated debugger, IE8- Visual Studio, etc.
    – slebetman
    Aug 25 '14 at 2:45






  • 1




    Also for node.js - node inspector. But node.js programmers don't ask as many basic and/or fix-my-code questions as general javascript programmers.
    – slebetman
    Aug 25 '14 at 2:47
















  • 1




    This is incomplete as it misses the most important debugger of all, one that has the potential to reduce the number of questions on Stackoverflow very significantly (I predict by at least 20%) - javascript debuggers: firebug, Chrome, Firefox, IE9+ integrated debugger, IE8- Visual Studio, etc.
    – slebetman
    Aug 25 '14 at 2:45






  • 1




    Also for node.js - node inspector. But node.js programmers don't ask as many basic and/or fix-my-code questions as general javascript programmers.
    – slebetman
    Aug 25 '14 at 2:47










1




1




This is incomplete as it misses the most important debugger of all, one that has the potential to reduce the number of questions on Stackoverflow very significantly (I predict by at least 20%) - javascript debuggers: firebug, Chrome, Firefox, IE9+ integrated debugger, IE8- Visual Studio, etc.
– slebetman
Aug 25 '14 at 2:45




This is incomplete as it misses the most important debugger of all, one that has the potential to reduce the number of questions on Stackoverflow very significantly (I predict by at least 20%) - javascript debuggers: firebug, Chrome, Firefox, IE9+ integrated debugger, IE8- Visual Studio, etc.
– slebetman
Aug 25 '14 at 2:45




1




1




Also for node.js - node inspector. But node.js programmers don't ask as many basic and/or fix-my-code questions as general javascript programmers.
– slebetman
Aug 25 '14 at 2:47






Also for node.js - node inspector. But node.js programmers don't ask as many basic and/or fix-my-code questions as general javascript programmers.
– slebetman
Aug 25 '14 at 2:47














up vote
21
down vote













I want to add that a debugger isn't always the perfect solution, and shouldn't always be the go-to solution to debugging. Here are a few cases where a debugger might not work for you:




  • The part of your program which fails is really large (poor modularization, perhaps?) and you're not exactly sure where to start stepping through the code. Stepping through all of it might be too time consuming.

  • Your program uses a lot of callbacks and other non-linear flow control methods, which makes the debugger confused when you step through it.

  • Your program is multi-threaded. Or even worse, your problem is caused by a race condition.

  • The code that has the bug in it runs many times before it bugs out. This can be particularly problematic in main loops, or worse yet, in physics engines, where the problem could be numerical. Even setting a breakpoint, in this case, would simply have you hitting it many times, with the bug not appearing.

  • Your program must run in real time. This is a big issue for programs that connect to the network. If you set up a breakpoint in your network code, the other end isn't going to wait for you to step through, it's simply going to time out. Programs which rely on the system clock, e.g. games with frame skip, aren't much better off either.

  • Your program performs some form of destructive actions, like writing to files or sending e-mails, and you'd like to limit the number of times you need to run through it.

  • You can tell that your bug is caused by incorrect values arriving at function X, but you don't know where these values come from. Having to run through the program, again and again, setting breakpoints farther and farther back, can be a huge hassle. Especially if function X is called from many places throughout the program.


In all of these cases, either having your program stop abruptly could cause the end results to differ, or stepping through manually in search of the one line where the bug is caused is too much of a hassle. This can equally happen whether your bug is incorrect behavior, or a crash. For instance, if memory corruption causes a crash, by the time the crash happens, it's too far from where the memory corruption first occurred, and no useful information is left.



So, what are the alternatives?



Simplest is simply logging and assertions. Add logs to your program at various points, and compare what you get with what you're expecting. For instance, see if the function where you think there's a bug is even called in the first place. See if the variables at a start of a method are what you think they are. Unlike breakpoints, it's okay for there to be many log lines in which nothing special happens. You can simply search through the log afterwards. Once you hit a log line that's different from what you're expecting, add more in the same area. Narrow it down farther and farther, until it's small enough to be able to log every line in the bugged area.



Assertions can be used to trap incorrect values as they occur, rather than once they have a visible effect to the end user. The quicker you catch an incorrect value, the closer you are to the line that produced it.



Refactor and unit test. If your program is too big, it might be worthwhile to test it one class or one function at a time. Give it inputs, and look at the outputs, and see which are not as you're expecting. Being able to narrow down a bug from an entire program to a single function can make a huge difference in debugging time.



In case of memory leaks or memory stomping, use appropriate tools which are able to analyze and detect these at runtime. Being able to detect where the actual corruption occurs is the first step. After this, you can use logs to work your way back to where incorrect values were introduced.



Remember that debugging is a process going backwards. You have the end result - a bug - and find the cause, which preceded it. It's about working your way backwards and, unfortunately, debuggers only step forwards. This is where good logging and postmortem analysis can give you much better results.






share|improve this answer

















  • 3




    This would be a good answer... of a different question. It is a bad answer for this question. Perhaps you should ask that question and post this as a reply to it.
    – Raedwald
    Apr 7 '15 at 6:53






  • 3




    The actual question is described as "assist new programmers who have a problem with a program", "it does not produce the output I expect" and "I have examined the stack trace, but I still do not know the cause of the problem". All of which are aided by this answer. Additionally, when explaining what a debugger does, it's equally important to explain what it doesn't do.
    – SlugFiller
    Apr 7 '15 at 14:51






  • 3




    Great answer. I always used the debugger as main tool to find bugs. But now i'm working in a project where a huge infrastructure component is using many threads and a lot of network code(client/server) and notice that the debugger is the last thing that helps me. You have mentioned a lot of things where you should really use a different tool instead of relying on your good old debugger (most important: logging).
    – Rango
    Nov 24 '17 at 10:02

















up vote
21
down vote













I want to add that a debugger isn't always the perfect solution, and shouldn't always be the go-to solution to debugging. Here are a few cases where a debugger might not work for you:




  • The part of your program which fails is really large (poor modularization, perhaps?) and you're not exactly sure where to start stepping through the code. Stepping through all of it might be too time consuming.

  • Your program uses a lot of callbacks and other non-linear flow control methods, which makes the debugger confused when you step through it.

  • Your program is multi-threaded. Or even worse, your problem is caused by a race condition.

  • The code that has the bug in it runs many times before it bugs out. This can be particularly problematic in main loops, or worse yet, in physics engines, where the problem could be numerical. Even setting a breakpoint, in this case, would simply have you hitting it many times, with the bug not appearing.

  • Your program must run in real time. This is a big issue for programs that connect to the network. If you set up a breakpoint in your network code, the other end isn't going to wait for you to step through, it's simply going to time out. Programs which rely on the system clock, e.g. games with frame skip, aren't much better off either.

  • Your program performs some form of destructive actions, like writing to files or sending e-mails, and you'd like to limit the number of times you need to run through it.

  • You can tell that your bug is caused by incorrect values arriving at function X, but you don't know where these values come from. Having to run through the program, again and again, setting breakpoints farther and farther back, can be a huge hassle. Especially if function X is called from many places throughout the program.


In all of these cases, either having your program stop abruptly could cause the end results to differ, or stepping through manually in search of the one line where the bug is caused is too much of a hassle. This can equally happen whether your bug is incorrect behavior, or a crash. For instance, if memory corruption causes a crash, by the time the crash happens, it's too far from where the memory corruption first occurred, and no useful information is left.



So, what are the alternatives?



Simplest is simply logging and assertions. Add logs to your program at various points, and compare what you get with what you're expecting. For instance, see if the function where you think there's a bug is even called in the first place. See if the variables at a start of a method are what you think they are. Unlike breakpoints, it's okay for there to be many log lines in which nothing special happens. You can simply search through the log afterwards. Once you hit a log line that's different from what you're expecting, add more in the same area. Narrow it down farther and farther, until it's small enough to be able to log every line in the bugged area.



Assertions can be used to trap incorrect values as they occur, rather than once they have a visible effect to the end user. The quicker you catch an incorrect value, the closer you are to the line that produced it.



Refactor and unit test. If your program is too big, it might be worthwhile to test it one class or one function at a time. Give it inputs, and look at the outputs, and see which are not as you're expecting. Being able to narrow down a bug from an entire program to a single function can make a huge difference in debugging time.



In case of memory leaks or memory stomping, use appropriate tools which are able to analyze and detect these at runtime. Being able to detect where the actual corruption occurs is the first step. After this, you can use logs to work your way back to where incorrect values were introduced.



Remember that debugging is a process going backwards. You have the end result - a bug - and find the cause, which preceded it. It's about working your way backwards and, unfortunately, debuggers only step forwards. This is where good logging and postmortem analysis can give you much better results.






share|improve this answer

















  • 3




    This would be a good answer... of a different question. It is a bad answer for this question. Perhaps you should ask that question and post this as a reply to it.
    – Raedwald
    Apr 7 '15 at 6:53






  • 3




    The actual question is described as "assist new programmers who have a problem with a program", "it does not produce the output I expect" and "I have examined the stack trace, but I still do not know the cause of the problem". All of which are aided by this answer. Additionally, when explaining what a debugger does, it's equally important to explain what it doesn't do.
    – SlugFiller
    Apr 7 '15 at 14:51






  • 3




    Great answer. I always used the debugger as main tool to find bugs. But now i'm working in a project where a huge infrastructure component is using many threads and a lot of network code(client/server) and notice that the debugger is the last thing that helps me. You have mentioned a lot of things where you should really use a different tool instead of relying on your good old debugger (most important: logging).
    – Rango
    Nov 24 '17 at 10:02















up vote
21
down vote










up vote
21
down vote









I want to add that a debugger isn't always the perfect solution, and shouldn't always be the go-to solution to debugging. Here are a few cases where a debugger might not work for you:




  • The part of your program which fails is really large (poor modularization, perhaps?) and you're not exactly sure where to start stepping through the code. Stepping through all of it might be too time consuming.

  • Your program uses a lot of callbacks and other non-linear flow control methods, which makes the debugger confused when you step through it.

  • Your program is multi-threaded. Or even worse, your problem is caused by a race condition.

  • The code that has the bug in it runs many times before it bugs out. This can be particularly problematic in main loops, or worse yet, in physics engines, where the problem could be numerical. Even setting a breakpoint, in this case, would simply have you hitting it many times, with the bug not appearing.

  • Your program must run in real time. This is a big issue for programs that connect to the network. If you set up a breakpoint in your network code, the other end isn't going to wait for you to step through, it's simply going to time out. Programs which rely on the system clock, e.g. games with frame skip, aren't much better off either.

  • Your program performs some form of destructive actions, like writing to files or sending e-mails, and you'd like to limit the number of times you need to run through it.

  • You can tell that your bug is caused by incorrect values arriving at function X, but you don't know where these values come from. Having to run through the program, again and again, setting breakpoints farther and farther back, can be a huge hassle. Especially if function X is called from many places throughout the program.


In all of these cases, either having your program stop abruptly could cause the end results to differ, or stepping through manually in search of the one line where the bug is caused is too much of a hassle. This can equally happen whether your bug is incorrect behavior, or a crash. For instance, if memory corruption causes a crash, by the time the crash happens, it's too far from where the memory corruption first occurred, and no useful information is left.



So, what are the alternatives?



Simplest is simply logging and assertions. Add logs to your program at various points, and compare what you get with what you're expecting. For instance, see if the function where you think there's a bug is even called in the first place. See if the variables at a start of a method are what you think they are. Unlike breakpoints, it's okay for there to be many log lines in which nothing special happens. You can simply search through the log afterwards. Once you hit a log line that's different from what you're expecting, add more in the same area. Narrow it down farther and farther, until it's small enough to be able to log every line in the bugged area.



Assertions can be used to trap incorrect values as they occur, rather than once they have a visible effect to the end user. The quicker you catch an incorrect value, the closer you are to the line that produced it.



Refactor and unit test. If your program is too big, it might be worthwhile to test it one class or one function at a time. Give it inputs, and look at the outputs, and see which are not as you're expecting. Being able to narrow down a bug from an entire program to a single function can make a huge difference in debugging time.



In case of memory leaks or memory stomping, use appropriate tools which are able to analyze and detect these at runtime. Being able to detect where the actual corruption occurs is the first step. After this, you can use logs to work your way back to where incorrect values were introduced.



Remember that debugging is a process going backwards. You have the end result - a bug - and find the cause, which preceded it. It's about working your way backwards and, unfortunately, debuggers only step forwards. This is where good logging and postmortem analysis can give you much better results.






share|improve this answer












I want to add that a debugger isn't always the perfect solution, and shouldn't always be the go-to solution to debugging. Here are a few cases where a debugger might not work for you:




  • The part of your program which fails is really large (poor modularization, perhaps?) and you're not exactly sure where to start stepping through the code. Stepping through all of it might be too time consuming.

  • Your program uses a lot of callbacks and other non-linear flow control methods, which makes the debugger confused when you step through it.

  • Your program is multi-threaded. Or even worse, your problem is caused by a race condition.

  • The code that has the bug in it runs many times before it bugs out. This can be particularly problematic in main loops, or worse yet, in physics engines, where the problem could be numerical. Even setting a breakpoint, in this case, would simply have you hitting it many times, with the bug not appearing.

  • Your program must run in real time. This is a big issue for programs that connect to the network. If you set up a breakpoint in your network code, the other end isn't going to wait for you to step through, it's simply going to time out. Programs which rely on the system clock, e.g. games with frame skip, aren't much better off either.

  • Your program performs some form of destructive actions, like writing to files or sending e-mails, and you'd like to limit the number of times you need to run through it.

  • You can tell that your bug is caused by incorrect values arriving at function X, but you don't know where these values come from. Having to run through the program, again and again, setting breakpoints farther and farther back, can be a huge hassle. Especially if function X is called from many places throughout the program.


In all of these cases, either having your program stop abruptly could cause the end results to differ, or stepping through manually in search of the one line where the bug is caused is too much of a hassle. This can equally happen whether your bug is incorrect behavior, or a crash. For instance, if memory corruption causes a crash, by the time the crash happens, it's too far from where the memory corruption first occurred, and no useful information is left.



So, what are the alternatives?



Simplest is simply logging and assertions. Add logs to your program at various points, and compare what you get with what you're expecting. For instance, see if the function where you think there's a bug is even called in the first place. See if the variables at a start of a method are what you think they are. Unlike breakpoints, it's okay for there to be many log lines in which nothing special happens. You can simply search through the log afterwards. Once you hit a log line that's different from what you're expecting, add more in the same area. Narrow it down farther and farther, until it's small enough to be able to log every line in the bugged area.



Assertions can be used to trap incorrect values as they occur, rather than once they have a visible effect to the end user. The quicker you catch an incorrect value, the closer you are to the line that produced it.



Refactor and unit test. If your program is too big, it might be worthwhile to test it one class or one function at a time. Give it inputs, and look at the outputs, and see which are not as you're expecting. Being able to narrow down a bug from an entire program to a single function can make a huge difference in debugging time.



In case of memory leaks or memory stomping, use appropriate tools which are able to analyze and detect these at runtime. Being able to detect where the actual corruption occurs is the first step. After this, you can use logs to work your way back to where incorrect values were introduced.



Remember that debugging is a process going backwards. You have the end result - a bug - and find the cause, which preceded it. It's about working your way backwards and, unfortunately, debuggers only step forwards. This is where good logging and postmortem analysis can give you much better results.







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 5 '15 at 12:15









SlugFiller

77259




77259








  • 3




    This would be a good answer... of a different question. It is a bad answer for this question. Perhaps you should ask that question and post this as a reply to it.
    – Raedwald
    Apr 7 '15 at 6:53






  • 3




    The actual question is described as "assist new programmers who have a problem with a program", "it does not produce the output I expect" and "I have examined the stack trace, but I still do not know the cause of the problem". All of which are aided by this answer. Additionally, when explaining what a debugger does, it's equally important to explain what it doesn't do.
    – SlugFiller
    Apr 7 '15 at 14:51






  • 3




    Great answer. I always used the debugger as main tool to find bugs. But now i'm working in a project where a huge infrastructure component is using many threads and a lot of network code(client/server) and notice that the debugger is the last thing that helps me. You have mentioned a lot of things where you should really use a different tool instead of relying on your good old debugger (most important: logging).
    – Rango
    Nov 24 '17 at 10:02
















  • 3




    This would be a good answer... of a different question. It is a bad answer for this question. Perhaps you should ask that question and post this as a reply to it.
    – Raedwald
    Apr 7 '15 at 6:53






  • 3




    The actual question is described as "assist new programmers who have a problem with a program", "it does not produce the output I expect" and "I have examined the stack trace, but I still do not know the cause of the problem". All of which are aided by this answer. Additionally, when explaining what a debugger does, it's equally important to explain what it doesn't do.
    – SlugFiller
    Apr 7 '15 at 14:51






  • 3




    Great answer. I always used the debugger as main tool to find bugs. But now i'm working in a project where a huge infrastructure component is using many threads and a lot of network code(client/server) and notice that the debugger is the last thing that helps me. You have mentioned a lot of things where you should really use a different tool instead of relying on your good old debugger (most important: logging).
    – Rango
    Nov 24 '17 at 10:02










3




3




This would be a good answer... of a different question. It is a bad answer for this question. Perhaps you should ask that question and post this as a reply to it.
– Raedwald
Apr 7 '15 at 6:53




This would be a good answer... of a different question. It is a bad answer for this question. Perhaps you should ask that question and post this as a reply to it.
– Raedwald
Apr 7 '15 at 6:53




3




3




The actual question is described as "assist new programmers who have a problem with a program", "it does not produce the output I expect" and "I have examined the stack trace, but I still do not know the cause of the problem". All of which are aided by this answer. Additionally, when explaining what a debugger does, it's equally important to explain what it doesn't do.
– SlugFiller
Apr 7 '15 at 14:51




The actual question is described as "assist new programmers who have a problem with a program", "it does not produce the output I expect" and "I have examined the stack trace, but I still do not know the cause of the problem". All of which are aided by this answer. Additionally, when explaining what a debugger does, it's equally important to explain what it doesn't do.
– SlugFiller
Apr 7 '15 at 14:51




3




3




Great answer. I always used the debugger as main tool to find bugs. But now i'm working in a project where a huge infrastructure component is using many threads and a lot of network code(client/server) and notice that the debugger is the last thing that helps me. You have mentioned a lot of things where you should really use a different tool instead of relying on your good old debugger (most important: logging).
– Rango
Nov 24 '17 at 10:02






Great answer. I always used the debugger as main tool to find bugs. But now i'm working in a project where a huge infrastructure component is using many threads and a lot of network code(client/server) and notice that the debugger is the last thing that helps me. You have mentioned a lot of things where you should really use a different tool instead of relying on your good old debugger (most important: logging).
– Rango
Nov 24 '17 at 10:02




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25385173%2fwhat-is-a-debugger-and-how-can-it-help-me-diagnose-problems%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga