使用windbg调试.net应用程序,无法插入断点

时间:2017-09-17 09:27:50

标签: c# .net debugging windbg

我使用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所在的文件夹和来源文件夹。

但是当我打开源文件并在源代码行上设置断点,然后继续该过程时我在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应用程序呢?

1 个答案:

答案 0 :(得分:5)

您无法在源文件中使用 F9 在.NET中设置断点。相反,您需要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编译的。一旦编译了方法,它就会自动设置断点。