How to effectively generate dump in ten thousand dead methods of net program (Part 2)

Time:2021-7-3

1: Background

Last time we talked about how to get throughprocdumpGrabCPU explosionandMemory explosionIn two cases, this article will talk about how to grasp the programHang upandUnexpected exit

2: The program is dead

1. Definition

The program is deadIn short, the program does not respond. Since it does not respond, it may bedeadlock, possiblyThe load is too large and the thread pool is exhaustedWait a minute. There are all kinds of situations in the world.

Now that it’s for useprocdumpGo get it. I need to know what it’s good forHang onWhat is your definition?

-h	Write dump if process has a hung window (does not respond to window messages for at least 5 seconds).

From the above definition, it seems that people are judging whether the window responds within a specified timewindows messages To judge, I know what you are thinking. The response time of the web request you are looking for is too long. This scenario can’t be captured through – H. I feel that it is especially suitable for grabbing with GUI programs, such as: (WPF, WinForm).

2. Case demonstration

Now I’m going to create a simple onewinformIn the button event, the main thread sleep is deliberately allowed to cause the program to feign death. The reference code is as follows:

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

        private void button1_Click(object sender, EventArgs e)
        {
            Thread.Sleep(1000 * 10);

            MessageBox.Show("clicked me!");
        }
    }

Next, start the CMD window and enter:

C:\Windows\system32>procdump -ma -h -w WindowsFormsApp1.exe E:\net5\hungwindow.dmp

ProcDump v10.0 - Sysinternals process dump utility
Copyright (C) 2009-2020 Mark Russinovich and Andrew Richards
Sysinternals - www.sysinternals.com

Waiting for process named WindowsFormsApp1.exe...

After starting the program, click button to let WinForm feign death. You can see that procdump automatically outputs dump after 5 seconds.

C:\Windows\system32>procdump -ma -h -w WindowsFormsApp1.exe E:\net5\hungwindow.dmp


Press Ctrl-C to end monitoring without terminating the process.

[14:49:53] Hung Window:
[14:49:53] Dump 1 initiated: E:\net5\hungwindow.dmp
[14:49:53] Dump 1 writing: Estimated dump file size is 303 MB.
[14:49:53] Dump 1 complete: 303 MB written in 0.7 seconds
[14:49:54] Dump count reached.

Then use WinDbg to see what each thread is doing?

0:000> ~*e !clrstack
OS Thread Id: 0x6698 (0)
Child SP       IP Call Site
00cfeb60 7722327c [HelperMethodFrame: 00cfeb60] System.Threading.Thread.SleepInternal(Int32)
00cfebe4 5da9be7b System.Threading.Thread.Sleep(Int32)
00cfebec 02d1238d WindowsFormsApp1.Form1.button1_Click(System.Object, System.EventArgs) [E:\net5\ConsoleApp1\WindowsFormsApp1\Form1.cs @ 23]
00cfec04 5a3b95bb System.Windows.Forms.Control.OnClick(System.EventArgs)
00cfec18 5a3bbe57 System.Windows.Forms.Button.OnClick(System.EventArgs)
...

3: Unexpected exit

1. Concept

Unexpected exitI think a lot of friends have met, the original console program run well, received alarm messages in the middle of the night… And user feedback, your terminal is feasible, you click a few times to hang up…

Some friends may be thinking, SD, this problem is not simple, add an overall situationUnhandled exceptionWhy not??? I don’t know what to think.

Ha ha, I always thinkGlobal exception handlingIt’s still too young to be able to cure all kinds of diseases. I remember that the last company used Alibaba’s SDK, and the underlying layer was encapsulated in C + +. The program exited inexplicably, and there was no log for global exception handling. I think you know that the exception thrown by the unmanaged layer is the younger brother of the managed layer, which is so simple

2. Demonstration

I’m going to throw a simpleDivideByZeroExceptionTo make it easy to exit the program.

public class Program
    {
        public static void Main(string[] args)
        {
            var result = CalcDAL();

            Console.WriteLine($"result={result}");

            Console.ReadLine();
        }

        public static int CalcDAL()
        {
            try
            {
                var query = "0";
                Thread.Sleep(2000);  //do sth...

                return 0 / Convert.ToInt32(query);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
        }
    }

After the program runs, it is used on procdump-eCommand grab.

C:\Windows\system32>procdump -ma -e  -w ConsoleApp1.exe E:\net5\test.dmp

ProcDump v10.0 - Sysinternals process dump utility
Copyright (C) 2009-2020 Mark Russinovich and Andrew Richards
Sysinternals - www.sysinternals.com

Waiting for process named ConsoleApp1.exe...

Press Ctrl-C to end monitoring without terminating the process.

[15:29:56] Exception: 04242420
[15:29:58] Exception: C0000094.INT_DIVIDE_BY_ZERO
[15:29:58] Exception: C0000094.INT_DIVIDE_BY_ZERO
[15:29:58] Exception: C0000094.INT_DIVIDE_BY_ZERO
[15:29:58] Unhandled: C0000094.INT_DIVIDE_BY_ZERO
[15:29:58] Dump 1 initiated: E:\net5\test-2.dmp
[15:29:58] Dump 1 writing: Estimated dump file size is 50 MB.
[15:29:59] Dump 1 complete: 50 MB written in 0.2 seconds
[15:29:59] Dump count reached.

From the output, everything is OK.

3. Development

I don’t know if I have any friends. I still remember that vs has oneException Breakpoint Is that right? It means that when an exception is thrown, the program will automatically enter the debugging state at the breakpoint. This is a sharp tool to help find a bug, but there are still certain restrictions. After all, the program runs in production, and you can’t move vs to it, and you can’t do remote debugging. So how can you automatically generate a dump after a certain exception is thrown???

In front of the powerful procdump, these are the younger brothers, mainly through the following two ways for abnormal collision retrieval.

  • adoptException typeGrab

What is itException typeFor example, in this sectionDivideByZeroExceptionException, by setting the-e 1 -f DivideByZeroExceptionThat’s it.

holdCalcDAL()Method to ensure that the program does not exit abnormally.

public static int CalcDAL()
        {
            try
            {
                var query = "0";
                Thread.Sleep(2000);  //do sth...

                return 0 / Convert.ToInt32(query);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0;
            }
        }

Then useproddumpEnter the following command.

C:\Windows\system32>procdump -ma  -w -e 1 -f   "divide by zero"  -w ConsoleApp1.exe E:\net5\test.dmp

ProcDump v10.0 - Sysinternals process dump utility
Copyright (C) 2009-2020 Mark Russinovich and Andrew Richards
Sysinternals - www.sysinternals.com

Waiting for process named ConsoleApp1.exe...

Press Ctrl-C to end monitoring without terminating the process.

CoreCLR Version: v5.0.3

[15:44:15] Exception: E0434F4D.System.DivideByZeroException ("Attempted to divide by zero.")
[15:44:15] Dump 1 initiated: E:\net5\test-3.dmp
[15:44:16] Dump 1 writing: Estimated dump file size is 50 MB.
[15:44:16] Dump 1 complete: 50 MB written in 0.2 seconds
[15:44:16] Dump count reached.

See the picture aboveException: E0434F4D.System.DivideByZeroException ("Attempted to divide by zero.")Are you ready? Ha ha, it has been successfully captured. Isn’t it interesting.

  • adoptAbnormal informationGrab

useAbnormal informationI think it’s more flexible. For example, I’ll search:divide by zeroKeywords can be captured successfully.

C:\Windows\system32>procdump -ma  -w -e 1 -f   "divide by zero"  -w ConsoleApp1.exe E:\net5\test.dmp

[15:46:34] Exception: E0434F4D.System.DivideByZeroException ("Attempted to divide by zero.")
[15:46:34] Dump 1 initiated: E:\net5\test-4.dmp
[15:46:34] Dump 1 writing: Estimated dump file size is 49 MB.
[15:46:34] Dump 1 complete: 49 MB written in 0.2 seconds
[15:46:35] Dump count reached.

4: Summary

I think you’ve learned all the four ways of grasping. Everything is difficult at the beginning. With dump, the next step is to study it well!

More high quality dry goods: see my GitHub:dotnetfly

图片名称

Recommended Today

Java Engineer Interview Questions

The content covers: Java, mybatis, zookeeper, Dubbo, elasticsearch, memcached, redis, mysql, spring, spring boot, springcloud, rabbitmq, Kafka, Linux, etcMybatis interview questions1. What is mybatis?1. Mybatis is a semi ORM (object relational mapping) framework. It encapsulates JDBC internally. During development, you only need to pay attention to the SQL statement itself, and you don’t need to […]