什么是调试器,它如何帮助我诊断问题? [英] What is a debugger and how can it help me diagnose problems?

查看:110
本文介绍了什么是调试器,它如何帮助我诊断问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个通用的问题,可以帮助有程序问题的新程序员,但不知道如何使用调试器来诊断问题的原因。



此问题涉及两类更具体的问题:




  • 当我运行我的程序时,它不会产生输出我期望我给它的输入。

  • 当我运行我的程序,它崩溃并给我一个堆栈跟踪。我有检查堆栈跟踪,但我仍然不知道问题的原因,因为堆栈跟踪不能提供足够的信息。


解决方案

调试器是一个可以检查程序状态的程序。 用于这样做的技术手段对于了解如何使用的基本知识并不重要使用调试器。您可以使用调试器在程序到达代码中的特定位置时停止执行程序,然后检查程序中变量的值。您可以使用调试器来缓慢运行程序,一行代码(称为单步执行),同时检查其变量的值。



通过这样做,您可以发现变量是否具有错误的值,并且程序中的值在哪里更改为错误的值。



使用单个您还可以发现控制流程是否符合您的期望。例如,当你期望它应该是 if 分支执行时。



使用调试器的具体细节取决于调试器,在较小程度上,您使用的编程语言。




  • 您可以附加一个调试器到已经运行你的程序的进程。如果你的程序被卡住,你可能会这样做。


  • 在实践中,从一开始就在调试器的控制下运行程序通常更容易。 p>


  • 您可以通过指示执行应该停止的行的源代码文件和行号来指示程序应该停止执行或通过指示程序应该停止的方法/函数的名称(如果要在执行进入方法时停止)。调试器用于使程序停止的技术意味着称为断点,此过程称为设置断点


  • 大多数现代调试器是IDE的一部分,并为您提供方便的GUI来检查源代码和程序的变量,一个点击式界面,用于设置断点,运行程序并单步执行。


  • 使用调试器可以非常困难,除非您的程序可执行文件或字节码文件包括调试符号信息。您可能需要稍微编译(或重新编译)您的程序不同的,以确保信息的存在。



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.

解决方案

A debugger is a program that can examine the state of your program. 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.

这篇关于什么是调试器,它如何帮助我诊断问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆