调试器如何工作? [英] How does a debugger work?

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

问题描述

我一直在想,调试器是如何工作的?可以附加到已经运行的可执行文件。我知道编译器将代码翻译成机器语言,但是调试器怎么知道它正在附加什么?

解决方案

调试器的工作原理将取决于您正在调试的内容以及操作系统的内容。对于Windows上的本地调试,您可以在MSDN上找到一些详细信息: Win32调试API



用户通过名称或进程ID告知调试器要附加哪个进程。如果它是一个名称,那么调试器将查找进程ID,并通过系统调用启动调试会话;在Windows下,这将是 DebugActiveProcess



一旦附加,调试器将会像任何UI一样进入一个事件循环,而不是来自窗口系统的事件,操作系统将根据进程中发生的情况生成事件被调试 - 例如发生异常。请参阅 WaitForDebugEvent



调试器能够读取和写入目标进程的虚拟内存,甚至通过操作系统提供的API调整其注册值。请参阅Windows的调试功能列表。 / p>

调试器能够使用符号文件中的信息从源代码中的地址转换为变量名称和位置。符号文件信息是一组单独的API,不是操作系统的核心部分。在Windows上,这是通过调试接口访问SDK



如果您正在调试托管环境(.NET,Java等),该过程通常看起来是类似的,但细节不同,因为虚拟机环境提供了调试API比底层操作系统。


I keep wondering how does a debugger work? Particulary the one that can be 'attached' to already running executable. I understand that compiler translates code to machine language, but then how does debugger 'know' what it is being attached to?

解决方案

The details of how a debugger works will depend on what you are debugging, and what the OS is. For native debugging on Windows you can find some details on MSDN: Win32 Debugging API.

The user tells the debugger which process to attach to, either by name or by process ID. If it is a name then the debugger will look up the process ID, and initiate the debug session via a system call; under Windows this would be DebugActiveProcess.

Once attached, the debugger will enter an event loop much like for any UI, but instead of events coming from the windowing system, the OS will generate events based on what happens in the process being debugged – for example an exception occurring. See WaitForDebugEvent.

The debugger is able to read and write the target process' virtual memory, and even adjust its register values through APIs provided by the OS. See the list of debugging functions for Windows.

The debugger is able to use information from symbol files to translate from addresses to variable names and locations in the source code. The symbol file information is a separate set of APIs and isn't a core part of the OS as such. On Windows this is through the Debug Interface Access SDK.

If you are debugging a managed environment (.NET, Java, etc.) the process will typically look similar, but the details are different, as the virtual machine environment provides the debug API rather than the underlying OS.

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

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