生活随笔
收集整理的這篇文章主要介紹了
如何排查 StackOverflow 异常
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
StackOverflowException 從名字上就能看出來它表示當(dāng)前執(zhí)行棧溢出,通常都是由于無限的方法遞歸調(diào)用導(dǎo)致的。
舉個(gè)例子,假如你有下面的一段代碼:
using?System;namespace?temp
{class?Program{static?void?Main(string[]?args){Main(args);?//?Oops,?this?recursion?won't?stop.}}
}
上面的 Main 方法會(huì)不斷的調(diào)用自己直到?jīng)]有足夠的棧空間,一旦到了這個(gè)臨界值,執(zhí)行引擎將會(huì)拋出 StackOverflowException。
>?dotnet?run
Stack?overflow.
Example
運(yùn)行程序前先配置變量 export DOTNET_DbgEnableMiniDump=1,這樣就可以在crash的時(shí)候自動(dòng)生成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 安裝 sos 擴(kuò)展
dotnet-sos?install
使用 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 重復(fù)了很多次,然后使用 !ip2md 查看該地址對應(yīng)的方法描述符。
(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 和 temp.Program.Main(System.String[]) 兩個(gè)信息相信就可以找出程序中的問題,當(dāng)然,如果你還不是特別明白的話,可以在質(zhì)疑處多加點(diǎn)日志排查。
總結(jié)
以上是生活随笔為你收集整理的如何排查 StackOverflow 异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。