【问题标题】:Debugging .net application with windbg, cannot insert breakpoints用windbg调试.net应用程序,不能插入断点
【发布时间】:2017-09-17 09:27:49
【问题描述】:

我使用 windbg 来调试一个简单的 c# 应用程序,它只包含如下的空表单:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

我编译,运行这个应用程序,用 windbg 附加到它,然后在 windbg 中运行:

0:009> .cordll -ve -u -l
Automatically loaded SOS Extension
CLRDLL: Loaded DLL C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscordacwks.dll
CLR DLL status: Loaded DLL C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscordacwks.dll

然后我加载 SOS 扩展并验证它是否已加载:

0:009> .loadby sos mscorwks
0:009> .chain
Extension DLL search Path:
    C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\WINXP;C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\winext;C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\winext\arcade;C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\pri;C:\Program Files (x86)\Windows Kits\10\Debuggers\x64;C:\Users\username\AppData\Local\Dbg\EngineExtensions;C:\Program Files (x86)\Windows Kits\10\Debuggers\x64;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\PuTTY\;C:\Program Files (x86)\Bitvise SSH Client;C:\Program Files\nodejs\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files\Git\cmd;C:\Users\username\AppData\Local\Android\sdk\platform-tools;C:\Program Files (x86)\Nox\bin\;C:\Users\username\AppData\Local\Microsoft\WindowsApps;C:\Users\username\AppData\Roaming\npm;C:\Program Files (x86)\Nmap;C:\Program Files (x86)\mitmproxy\bin
Extension DLL chain:
    C:\Windows\Microsoft.NET\Framework64\v2.0.50727\sos: image 2.0.50727.8794, API 1.0.0, built Tue Jun 20 23:15:41 2017
        [path: C:\Windows\Microsoft.NET\Framework64\v2.0.50727\SOS.dll]
    C:\Windows\Microsoft.NET\Framework64\v2.0.50727\SOS.dll: image 2.0.50727.8794, API 1.0.0, built Tue Jun 20 23:15:41 2017
        [path: C:\Windows\Microsoft.NET\Framework64\v2.0.50727\SOS.dll]
    dbghelp: image 10.0.15063.468, API 10.0.6, built Thu Jan  1 03:00:00 1970
        [path: C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\dbghelp.dll]
    ext: image 10.0.15063.468, API 1.0.0, built Thu Jan  1 03:00:00 1970
        [path: C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\winext\ext.dll]
    exts: image 10.0.15063.468, API 1.0.0, built Thu Jan  1 03:00:00 1970
        [path: C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\WINXP\exts.dll]
    uext: image 10.0.15063.468, API 1.0.0, built Thu Jan  1 03:00:00 1970
        [path: C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\winext\uext.dll]
    ntsdexts: image 10.0.15063.468, API 1.0.0, built Thu Jan  1 03:00:00 1970
        [path: C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\WINXP\ntsdexts.dll]

然后我将调试符号路径设置为.pdb所在的文件夹和sources文件夹。

但是当我打开源文件并在源代码行设置断点,然后继续该过程时,windbg 中出现错误:

0:009> g
Unable to insert breakpoint 3 at 00000000`008f001a, Win32 error 0n998
    "Invalid access to memory."
bp3 at 00000000`008f001a failed
WaitForEvent failed

我预计它会中断,并显示.net 汇编程序指令,但它总是失败。那么如何使用 windbg 对 .net 应用程序进行断点呢?

【问题讨论】:

  • 很好的问题,但缺少最重要的部分:你是如何设置断点的?
  • @ThomasWeller 我打开了windbg 菜单File 选择了Open Source File 打开了Form1.cs,在源文件中我将光标放在InitializeComponent(); 行并按F9。线变成红色,所以断点似乎已经放置

标签: c# .net debugging windbg


【解决方案1】:

您不能在 .NET 中对源文件使用 F9 设置断点。相反,您需要 SOS !bpmdSOSEX !mbp 命令。

SOS 语法是

!BPMD <module name> <method name>
!BPMD -md <MethodDesc>

它不能使用行号。

SOSEX 语法是

!sosex.mbp <source file> <line number> [<column number>] [Options]

如果 PDB 可用,则可以使用行号。

!mbc 清除托管断点。 !mbd 禁用托管断点,!mbl 列出托管断点。

如果我没记错的话,有一个区别:!bpmd 仅在该方法已经过 JIT 编译时才有效,因此可以在可执行机器代码中设置断点。 !mbp 可以设置断点,尽管该方法不是 JIT 编译的。编译方法后,它会自行设置断点。

【讨论】:

  • !bpmd 还可以在尚未经过 JIT 编译的方法上设置未解析的断点。 !bpmd module MyClass.MyFunc!bpmd -md &lt;MethodDesc&gt;.
猜你喜欢
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-20
相关资源
最近更新 更多