调试立即崩溃的 .NET EXE 文件

发布于 2024-08-22 14:24:16 字数 129 浏览 3 评论 0原文

我正在使用一个托管 EXE 文件,该文件在运行时立即崩溃。通常我希望有一个对话框,允许选择启动调试器,但在这种情况下没有这样的运气。另外,程序崩溃得太快,我无法在 Visual Studio 中使用附加到进程。

解决办法是什么?

I'm working with a managed EXE file that crashes immediately when run. Usually I'd expect a dialog which allows an option to launch the debugger, but no such luck in this case. Also, the program crashes too quickly for me to use attach to process in Visual Studio.

What is the solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

[旋木] 2024-08-29 14:24:17

一个常见的原因是,如果在调用 Application.Run... 时创建的任何对象的构造函数中抛出异常,如下所示:

Application.Run(new MyForm());

如果 MyForm 构造函数抛出异常,它通常会崩溃。

另请考虑使用程序集绑定日志查看器来确定如果您缺少所需的程序集,或者存在版本控制问题。

One common reason for this is if an exception is thrown in the constructor of whatever object is creating when calling Application.Run... as in:

Application.Run(new MyForm());

If the MyForm constructor throws an exception, it will usually just crash.

Also consider using Assembly Binding Log Viewer to determine if you are missing a required assembly, or have a versioning issue.

陌生 2024-08-29 14:24:17

在调试器中启动它(F5Ctrl + Shift + B)。您应该能够设置任意断点(如在主函数中)并单步执行。

Start it within the debugger (F5 or Ctrl + Shift + B). You should be able to set an arbitrary breakpoint (like in the main function) and single step your way through.

酸甜透明夹心 2024-08-29 14:24:16

如果您安装了 WinDbg,请使用菜单文件 → 打开可执行文件直接在调试器下打开应用程序并立即自动中断。

然后您可以使用Debug(即Go)下的命令正常执行并调试它。同时加载 SOS 扩展。不如 Visual Studio 好,但如果您只有 EXE(希望还有 PDB,尽管这是可选的)而没有源代码,则很有用。

示例:这是我的源代码,我们假设它不可用:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        int x = 10 - 10;
        int i = 2000/x;

        Application.Run(new Form1());
    }

它会立即崩溃,您没有机会及时附加调试器。这是点击“运行”后的 WinDbg 输出:

Removed dead ImageShack link - Freehand Circles by me

加载 SOS.dll 后,您可以使用 !DumpStack 查看抛出异常的位置:

Removed死 ImageShack 链接 - 没有手绘圆圈,抱歉!

请注意,JIT 或编译器优化可能会导致方法被内联,这可能会使 StackTrace 不 100% 可靠,但为了快速概览它是有效的。

WinDbg 有点神秘,但是一旦您完成了一些基础知识,它就很棒,并且至少有助于找到问题的根源。

If you have WinDbg installed, use menu FileOpen Executable to open the application directly under the debugger and automatically break immediately.

You can then use the commands under Debug (i.e., Go) to execute it normally and debug it. Also load the SOS Extensions. Not as nice as Visual Studio, but useful if you only have the EXE (and hopefully the PDB, although that's optional) and no source.

Example: This is my source code, which we assume is unavailable:

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        int x = 10 - 10;
        int i = 2000/x;

        Application.Run(new Form1());
    }

This crashes immediately, with no chance for you to attach a debugger in time. This is the WinDbg output after hitting "Run":

Removed dead ImageShack link - Freehand circles by me

After loading SOS.dll, you can use !DumpStack to see where the Exception was thrown:

Removed dead ImageShack link - No Freehand Circles, sorry!

Note that JIT or compiler optimizations may cause methods to be inlined which may make the StackTrace not 100% reliable, but for a quick overview it works.

WinDbg is a bit arcane, but once you got some basics done it's awesome and at least helps finding the root of the issue.

娇妻 2024-08-29 14:24:16

立即崩溃?

我使用 ildasm 只是为了确保我有一个有效的(看起来)托管可执行文件。

另外,我也曾因将 .exe 放在网络驱动器上而没有正确设置权限而被咬过几次。

Immediate crash?

I'd use ildasm just to make sure I've got a valid (looking) managed executable.

Also, I've been bitten a few times by having the .exe on a networked drive, and not having the permissions set correctly.

淡水深流 2024-08-29 14:24:16

上面没有提到的另一个选项是在程序的最开始插入一条

Debugger.Break();

语句 - 可能包含在 #ifdef DEBUG 中,以便在构建发布时更容易跳过。我曾使用此技术来调试早期崩溃的 Windows 服务。

One additional option not mentioned above is to insert a

Debugger.Break();

statement at the very beginning of the program - perhaps wrapped in a #ifdef DEBUG to make it easier to skip over when its time to build for release. I've used this technique to debug Windows services that were crashing early on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文