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

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

问题描述

我一直想知道调试器是如何工作的?特别是可以附加"到已经运行的可执行文件的那个.我知道编译器将代码翻译成机器语言,但是调试器如何知道"它所附加的内容?

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?

推荐答案

调试器如何工作的细节将取决于您正在调试的内容以及操作系统是什么.对于 Windows 上的本机调试,您可以在 MSDN 上找到一些详细信息:Win32 Debugging API.

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.

用户通过名称或进程 ID 告诉调试器要附加到哪个进程.如果是名称,则调试器将查找进程 ID,并通过系统调用启动调试会话;在 Windows 下,这将是 DebugActiveProcess.

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.

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

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.

调试器能够读写目标进程的虚拟内存,甚至可以通过操作系统提供的 API 调整其寄存器值.请参阅 Windows 的调试功能列表.

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.

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

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.

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

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天全站免登陆