Dot-Net

WinDbg 和 SoS,如何列印/轉儲大字元串?

  • October 15, 2019

我正在使用帶有 SoS 擴展的 WinDbg 調試來自生產伺服器的掛起轉儲。

在其中一個堆棧中有一個字元串參數,我需要知道它的值。但是,它是一個相當大的字元串,當我使用DumpObj. 這是來自的輸出DumpObj

0:036> !do 00000001b30d8668
Name: System.String
MethodTable: 0000064278436728
EEClass: 000006427803e520
Size: 5125300(0x4e34b4) bytes
(C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll)
String: <String is invalid or too large to print>

Fields:
             MT    Field   Offset                 Type VT     Attr            Value Name
000006427843d998  4000096        8         System.Int32  1 instance          2562638 m_arrayLength
000006427843d998  4000097        c         System.Int32  1 instance          2562637 m_stringLength
0000064278438170  4000098       10          System.Char  1 instance               3c m_firstChar
0000064278436728  4000099       20        System.String  0   shared           static Empty
                                >> Domain:Value  0000000000163260:000000007fff0370 00000000001a6760:000000007fff0370 <<
0000064278438020  400009a       28        System.Char[]  0   shared           static WhitespaceChars
                                >> Domain:Value  0000000000163260:000000007fff0b60 00000000001a6760:000000007fff89f0 <<

如何獲取此字元串實例的值?最好轉儲到文件中。

在轉儲 2562638 個字元的文本之前,我會三思而後行,但如果你真的想要,文本儲存在字元串實例的欄位之後,所以你可以du <address+offset> <end address>轉儲字元串的實際文本。輸出將如下所示:

00000000`132ab050  "this is an extremely long string"
00000000`132ab090  " of text, so don't even bother t"
00000000`132ab0d0  "o try to dump it to the screen -"

通過將會話輸出記錄到文件中,您可以輕鬆擷取輸出並進行所需的任何後處理。

這是我編寫的將字元串轉儲到 windbg 中的文件的腳本。

$$ Dumps the managed strings to a file
$$ Platform x86
$$ Usage $$>a<"c:\temp\dumpstringtofolder.txt" 6544f9ac 5000 c:\temp\stringtest
$$ First argument is the string method table pointer
$$ Second argument is the Min size of the string that needs to be used filter
$$ the strings
$$ Third is the path of the file
.foreach ($string {!dumpheap -short -mt ${$arg1}  -min ${$arg2}})
{ 

 $$ MT        Field      Offset               Type  VT     Attr    Value Name
 $$ 65452978  40000ed        4         System.Int32  1 instance    71117 m_stringLength
 $$ 65451dc8  40000ee        8          System.Char  1 instance       3c m_firstChar
 $$ 6544f9ac  40000ef        8        System.String  0   shared   static Empty

 $$ start of string is stored in the 8th offset, which can be inferred from above
 $$ Size of the string which is stored in the 4th offset
 r@$t0=  poi(${$string}+4)*2
 .writemem ${$arg3}${$string}.txt ${$string}+8 ${$string}+8+@$t0
}

這就是它的使用方式$$>a<”c:\temp\dumpstringtofolder.txt” 6544f9ac 5000 c:\temp\stringtest

轉儲的內容將採用 Unicode 格式,並使用類似這樣的方式查看其內容Console.WriteLine(ASCIIEncoding.Unicode.GetString(File.ReadAllBytes(@"c:\temp\stringtest03575270.txt")));

高溫高壓

引用自:https://stackoverflow.com/questions/5349945