生活随笔
收集整理的這篇文章主要介紹了
如何分析 StackOverflow 异常 ?
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一般來說,當你的方法遞歸調用次數太多大于線程棧的默認1M內存時將會拋出 StackOverflowException 異常。
舉個例子,假設你有下面這段代碼:
using?System;namespace?temp
{class?Program{static?void?Main(string[]?args){Main(args);?//?Oops,?this?recursion?won't?stop.}}
}
這個 Main 方法會持續的調用自己直到超出默認的棧空間,因為沒有棧空間了,所有 CLR 執行引擎 就會拋出 StackOverflowException 異常。
>?dotnet?run
Stack?overflow.
接下來我們看看如何在 .netcore 中生成這種 crash dump。
netcore 生成 dump
設置環境變量,配置crash時自動生成dump
>?export?DOTNET_DbgEnableMiniDump=1
>?dotnet?run
Stack?overflow.
Writing?minidump?with?heap?to?file?/tmp/coredump.6412
Written?58191872?bytes?(14207?pages)?to?core?file
安裝 ?dotnet-sos 工具
dotnet-sos?installc
通過 lldb 調試 dump
lldb?--core?/temp/coredump.6412
(lldb)?bt
...frame?#261930:?0x00007f59b40900ccframe?#261931:?0x00007f59b40900ccframe?#261932:?0x00007f59b40900ccframe?#261933:?0x00007f59b40900ccframe?#261934:?0x00007f59b40900ccframe?#261935:?0x00007f5a2d4a080f?libcoreclr.so`CallDescrWorkerInternal?at?unixasmmacrosamd64.inc:867frame?#261936:?0x00007f5a2d3cc4c3?libcoreclr.so`MethodDescCallSite::CallTargetWorker(unsigned?long?const*,?unsigned?long*,?int)?at?callhelpers.cpp:70frame?#261937:?0x00007f5a2d3cc468?libcoreclr.so`MethodDescCallSite::CallTargetWorker(this=<unavailable>,?pArguments=0x00007ffe8222e7b0,?pReturnValue=0x0000000000000000,?cbReturnValue=0)?at?callhelpers.cpp:604frame?#261938:?0x00007f5a2d4b6182?libcoreclr.so`RunMain(MethodDesc*,?short,?int*,?PtrArray**)?[inlined]?MethodDescCallSite::Call(this=<unavailable>,?pArguments=<unavailable>)?at?callhelpers.h:468
...
可以看到棧頂的 0x00007f59b40900cc 出現了多次重復,然后使用 SOS 的 ip2md 命令通過 address 找到對應的方法。
(lldb)?ip2md?0x00007f59b40900cc
MethodDesc:???00007f59b413ffa8
Method?Name:??????????temp.Program.Main(System.String[])
Class:????????????????00007f59b4181d40
MethodTable:??????????00007f59b4190020
mdToken:??????????????0000000006000001
Module:???????????????00007f59b413dbf8
IsJitted:?????????????yes
Current?CodeAddr:?????00007f59b40900a0
Version?History:ILCodeVersion:??????0000000000000000ReJIT?ID:???????????0IL?Addr:????????????0000000000000000CodeAddr:???????????00007f59b40900a0??(MinOptJitted)NativeCodeVersion:??0000000000000000
Source?file:??/temp/Program.cs?@?9
有了方法接下來可以到 /temp/Program.cs @ 9 行中找到問題代碼,如果還沒有水落石出的話,可以再輔助一些日志。
總結
以上是生活随笔為你收集整理的如何分析 StackOverflow 异常 ?的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。