生活随笔
收集整理的這篇文章主要介紹了
如何分析 StackOverflow 异常 ?
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一般來(lái)說(shuō),當(dāng)你的方法遞歸調(diào)用次數(shù)太多大于線程棧的默認(rèn)1M內(nèi)存時(shí)將會(huì)拋出 StackOverflowException 異常。
舉個(gè)例子,假設(shè)你有下面這段代碼:
using?System;namespace?temp
{class?Program{static?void?Main(string[]?args){Main(args);?//?Oops,?this?recursion?won't?stop.}}
}
這個(gè) Main 方法會(huì)持續(xù)的調(diào)用自己直到超出默認(rèn)的棧空間,因?yàn)闆](méi)有??臻g了,所有 CLR 執(zhí)行引擎 就會(huì)拋出 StackOverflowException 異常。
>?dotnet?run
Stack?overflow.
接下來(lái)我們看看如何在 .netcore 中生成這種 crash dump。
netcore 生成 dump
設(shè)置環(huán)境變量,配置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 工具
dotnet-sos?installc
通過(guò) lldb 調(diào)試 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
...
可以看到棧頂?shù)?0x00007f59b40900cc 出現(xiàn)了多次重復(fù),然后使用 SOS 的 ip2md 命令通過(guò) address 找到對(duì)應(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
有了方法接下來(lái)可以到 /temp/Program.cs @ 9 行中找到問(wèn)題代碼,如果還沒(méi)有水落石出的話,可以再輔助一些日志。
總結(jié)
以上是生活随笔為你收集整理的如何分析 StackOverflow 异常 ?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。