Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)

windbg - 힙에서 .NET 타입에 대한 배열을 찾는 방법

.NET 입장에서, 배열은 결국 System.Array를 상속받은 개체로 다뤄집니다. 지난번 이야기에서 "System.Char[]" 개체가 "dumpheap -stat" 명령으로 확인되었던 것을 볼 수 있었는데요.

StringBuilder에서의 OutOfMemoryException 오류 원인 분석
; https://www.sysnet.pe.kr/2/0/1171

Primitive 타입에 대한 배열은 그렇다 치고, 기타 타입들에 대한 배열은 "dumpheap -stat"에서 직접적으로 보이지 않습니다. 이에 대한 이해를 돕기 위해 예제를 다뤄볼까요?

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Program[] pgList = new Program[0];
            Thread.Sleep(-1);
        }
    }
}

위와 같은 코드로 작성된 프로그램을 windbg로 로드하고 Thread.Sleep까지 진행 시킨 후, Heap을 살펴보면 다음과 같은 결과를 얻을 수 있습니다.

0:004> .loadby sos clr

0:004>  !dumpheap -stat
total 0 objects
Statistics:
      MT    Count    TotalSize Class Name
72fd5f10        1           12 System.Security.HostSecurityManager
72fd5938        1           12 System.Collections.Generic.ObjectEqualityComparer`1[[System.Type, mscorlib]]
72fd4b44        1           12 System.Security.Permissions.ReflectionPermission
72fd4a60        1           12 System.Security.Permissions.FileDialogPermission
72fd4980        1           12 System.Security.PolicyManager
72fd4898        1           12 System.Security.SecurityDocument
72fd27fc        1           12 System.Collections.Generic.GenericEqualityComparer`1[[System.String, mscorlib]]
72fd5ec8        1           16 System.Security.Policy.Evidence+EvidenceLockHolder
72fd4bf4        1           16 System.Security.Permissions.UIPermission
72fd4640        1           16 System.Security.Util.Parser
... [생략] ...
72fcf92c      469        15652 System.String
72f86ba8       67        19148 System.Object[]
Total 784 objects

예상대로라면, Count == 1, TotalSize == 12 조건을 만족하는 목록 중에서 "ConsoleApplication1.Program[]"인 항목이 나와야 하는데 없는 것입니다. 왜 그럴까요?

아쉽게도 (네, 아쉽습니다.) Primitive 타입을 제외하고는 모든 타입에 대해서 "System.Object[]"로 통털어서 출력이 됩니다. 그래서, 다음과 같이 부가적인 검사를 해야 합니다.

0:004> !dumpheap -mt 72f86ba8       
 Address       MT     Size
027d18e4 72f86ba8       88     
027d5578 72f86ba8       56     
027d55d0 72f86ba8       20     
027d55e4 72f86ba8       32     
027d5604 72f86ba8       20     
... [생략] ...
037d1010 72f86ba8     4096     
037d2020 72f86ba8      528     
037d2240 72f86ba8     4096     
total 0 objects
Statistics:
      MT    Count    TotalSize Class Name
72f86ba8       67        19148 System.Object[]
Total 67 objects

그런데, 과연 67개의 인스턴스 중에서 어떤 것이 Program[]의 개체일까요? (크기를 정확하게 알고 있다면 대상이 좀 더 축소가 되겠지만) 현실적으로는 방법이 없습니다. 67개의 개체 모두 일일이 "!do" 명령어를 내려서 확인해야 합니다. 물론, 위의 경우에는 작은 프로그램이니 그나마 쉽게(?) 찾을 수 있겠지만 대형 프로그램의 경우에는 어림도 없는 작업입니다.

대신 그때그때 상황에 맞게 지름길을 선택하는 정도가 최선일 텐데요. 가령 예를 들어서, 위와 같은 예제 코드에서 pgList 인스턴스가 아직 스택에 존재할 거라는 정보가 있기 때문에 이를 찾아나서면 됩니다.

우선, "~*e!CLRStack" 명령어를 이용해서 pgList를 보유하고 있는 메서드를 어떤 스레드에서 실행하고 있는지 확인해야 하는데,

0:004> ~*e!CLRStack
OS Thread Id: 0x2490 (0)
Child SP IP       Call Site
003af0dc 7762fd81 [HelperMethodFrame: 003af0dc] System.Threading.Thread.SleepInternal(Int32)
003af128 72f03409 System.Threading.Thread.Sleep(Int32)
003af12c 004b00e9 ConsoleApplication1.Program.Main(System.String[]) [D:\...\Program.cs @ 17]
003af398 73b421bb [GCFrame: 003af398] 
OS Thread Id: 0x1dd8 (1)
Unable to walk the managed stack. The current thread is likely not a managed thread. You can run !threads to get a list of managed threads in the process
OS Thread Id: 0x2c2c (2)
Child SP IP       Call Site
00fdfdd8 7763014d [DebuggerU2MCatchHandlerFrame: 00fdfdd8] 
OS Thread Id: 0x312c (3)
Unable to walk the managed stack. The current thread is likely not a managed thread. You can run !threads to get a list of managed threads in the process
OS Thread Id: 0x2ee8 (4)
Unable to walk the managed stack. The current thread is likely not a managed thread. You can run !threads to get a list of managed threads in the process

총 5개의 스레드가 실행되는 중에 우리가 원하는 메서드(Main)를 실행 중인 것은 0번 인덱스의 0x2490 스레드임을 알 수 있습니다. 또한, 현재 windbg의 스레드 문맥은 "0:004>" 프롬프트에서 알려주듯이 4번 인덱스의 스레드이므로 이후의 명령어를 진행하기에 앞서 스레드 전환을 해줍니다.

0:004> ~0s
eax=00000000 ebx=00000001 ecx=00000000 edx=00000000 esi=003aeff8 edi=00000000
eip=7762fd81 esp=003aefb4 ebp=003af01c iopl=0         nv up ei pl zr na pe nc
cs=0023  ss=002b  ds=002b  es=002b  fs=0053  gs=002b             efl=00000246
ntdll!NtDelayExecution+0x15:
7762fd81 83c404          add     esp,4

위와 같이 "~[스레드 인덱스 번호]s" 명령어 이외에, windbg의 "View" / "Processes and Threads (Alt+9)" 메뉴를 이용하여 GUI 윈도우에서 마우스를 이용하여 전환하는 것도 가능합니다.

how_to_find_type_array_mt_1.png

그다음, 현재 선택된 스레드 문맥에 포함된 stack object들을 모두 나열시키면,

0:000> !dso
OS Thread Id: 0x2490 (0)
ESP/REG  Object   Name
003AF09C 027dbf80 System.Object[]    (System.String[])
003AF12C 027dbf90 System.Object[]    (ConsoleApplication1.Program[])
003AF130 027dbf90 System.Object[]    (ConsoleApplication1.Program[])
003AF134 027dbf90 System.Object[]    (ConsoleApplication1.Program[])
003AF140 027dbf80 System.Object[]    (System.String[])
003AF214 027dbf80 System.Object[]    (System.String[])
003AF3BC 027dbf80 System.Object[]    (System.String[])
003AF3F0 027dbf80 System.Object[]    (System.String[])

Name 필드에 친절하게도 어떤 타입의 배열인지 알려주기 때문에 쉽게 Object 주소를 알 수 있습니다. 이후의 Method Table이나 EEClass를 찾는 것은 목적에 따라 실행해 주시면 되겠고. ^^

0:000> !do 027dbf90 
Name:        System.Object[]
MethodTable: 72f86ba8
EEClass:     72d09688
Size:        20(0x14) bytes
Array:       Rank 1, Number of elements 1, Type CLASS
Element Type:ConsoleApplication1.Program
Fields:
None

0:000> !dumpmt 72f86ba8
EEClass:      72d09688
Module:       72cb1000
Name:         System.Object[]
mdToken:      02000000
File:         C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
BaseSize:        0x10
ComponentSize:   0x4
Slots in VTable: 28
Number of IFaces in IFaceMap: 6

0:000> !dumpclass 72d09688
Class Name:      System.Object[]
mdToken:         02000000
File:            C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
Parent Class:    72d08ad8
Module:          72cb1000
Method Table:    72f86ba8
Vtable Slots:    18
Total Method Slots:  1c
Class Attributes:    2101  
Transparency:        Transparent
NumInstanceFields:   0
NumStaticFields:     0




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/10/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13533정성태1/18/20242452닷넷: 2206. C# - TCP KeepAlive의 서버 측 구현파일 다운로드1
13532정성태1/17/20242346닷넷: 2205. C# - SuperSimpleTcp 사용 시 주의할 점파일 다운로드1
13531정성태1/16/20242236닷넷: 2204. C# - TCP KeepAlive에 새로 추가된 Retry 옵션파일 다운로드1
13530정성태1/15/20242192닷넷: 2203. C# - Python과의 AES 암호화 연동파일 다운로드1
13529정성태1/15/20242053닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
13528정성태1/14/20242180Linux: 68. busybox 컨테이너에서 실행 가능한 C++, Go 프로그램 빌드
13527정성태1/14/20242123오류 유형: 892. Visual Studio - Failed to launch debug adapter. Additional information may be available in the output window.
13526정성태1/14/20242205닷넷: 2201. C# - Facebook 연동 / 사용자 탈퇴 처리 방법
13525정성태1/13/20242158오류 유형: 891. Visual Studio - Web Application을 실행하지 못하는 IISExpress
13524정성태1/12/20242220오류 유형: 890. 한국투자증권 KIS Developers OpenAPI - GW라우팅 중 오류가 발생했습니다.
13523정성태1/12/20242028오류 유형: 889. Visual Studio - error : A project with that name is already opened in the solution.
13522정성태1/11/20242190닷넷: 2200. C# - HttpClient.PostAsJsonAsync 호출 시 "Transfer-Encoding: chunked" 대신 "Content-Length" 헤더 처리
13521정성태1/11/20242256닷넷: 2199. C# - 한국투자증권 KIS Developers OpenAPI의 WebSocket Ping, Pong 처리
13520정성태1/10/20242001오류 유형: 888. C# - Unable to resolve service for type 'Microsoft.Extensions.ObjectPool.ObjectPool`....'
13519정성태1/10/20242093닷넷: 2198. C# - Reflection을 이용한 ClientWebSocket의 Ping 호출파일 다운로드1
13518정성태1/9/20242353닷넷: 2197. C# - ClientWebSocket의 Ping, Pong 처리
13517정성태1/8/20242198스크립트: 63. Python - 공개 패키지를 이용한 위성 이미지 생성 (pystac_client, odc.stac)
13516정성태1/7/20242304닷넷: 2196. IIS - AppPool의 "Disable Overlapped Recycle" 옵션의 부작용
13515정성태1/6/20242582닷넷: 2195. async 메서드 내에서 C# 7의 discard 구문 활용 사례 [1]
13514정성태1/5/20242255개발 환경 구성: 702. IIS - AppPool의 "Disable Overlapped Recycle" 옵션
13513정성태1/5/20242176닷넷: 2194. C# - WebActivatorEx / System.Web의 PreApplicationStartMethod 특성
13512정성태1/4/20242143개발 환경 구성: 701. IIS - w3wp.exe 프로세스의 ASP.NET 런타임을 항상 Warmup 모드로 유지하는 preload Enabled 설정
13511정성태1/4/20242162닷넷: 2193. C# - ASP.NET Web Application + OpenAPI(Swashbuckle) 스펙 제공
13510정성태1/3/20242099닷넷: 2192. C# - 특정 실행 파일이 있는지 확인하는 방법 (Linux)
13509정성태1/3/20242122오류 유형: 887. .NET Core 2 이하의 프로젝트에서 System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp2.0.
13508정성태1/3/20242141오류 유형: 886. ORA-28000: the account is locked
1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...