Microsoft MVP성태의 닷넷 이야기
Linux: 26. .NET Core 응용 프로그램을 위한 메모리 덤프 방법 [링크 복사], [링크+제목 복사],
조회: 22835
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)
(시리즈 글이 6개 있습니다.)
Linux: 26. .NET Core 응용 프로그램을 위한 메모리 덤프 방법
; https://www.sysnet.pe.kr/2/0/12078

Linux: 27. linux - lldb를 이용한 .NET Core 응용 프로그램의 메모리 덤프 분석 방법
; https://www.sysnet.pe.kr/2/0/12083

.NET Framework: 2053. 리눅스 환경의 .NET Core 3/5+ 메모리 덤프를 분석하는 방법 - 두 번째 이야기
; https://www.sysnet.pe.kr/2/0/13135

.NET Framework: 2054. .NET Core/5+ SDK 설치 없이 dotnet-dump 사용하는 방법
; https://www.sysnet.pe.kr/2/0/13136

.NET Framework: 2055. 리눅스 환경의 .NET Core 3/5+ 메모리 덤프로부터 닷넷 모듈을 추출하는 방법
; https://www.sysnet.pe.kr/2/0/13137

.NET Framework: 2057. 리눅스 환경의 .NET Core 3/5+ 메모리 덤프로부터 모든 닷넷 모듈을 추출하는 방법
; https://www.sysnet.pe.kr/2/0/13139




linux - .NET Core 응용 프로그램을 위한 메모리 덤프 방법

리눅스 용 ProcDump가 나온 것은 아실 테고, ^^

microsoft/ProcDump-for-Linux
; https://github.com/Microsoft/ProcDump-for-Linux

Minimum OS:
    Red Hat Enterprise Linux / CentOS 7
    Fedora 26
    Ubuntu 14.04 LTS
gdb >= 7.6.1
zlib (build-time only)


설치 방법은 아래의 링크에서 자세하게 설명하지만,

Ubuntu 18.04 Package Manager - Install .NET Core
; https://learn.microsoft.com/en-us/dotnet/core/install/linux-package-manager-ubuntu-1804

(Ubuntu 18.04의 경우) 어차피 Microsoft Key와 Feed 등록은,

$ wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
$ sudo dpkg -i packages-microsoft-prod.deb

.NET Core SDK를 이미 설치한 경우 함께 등록했을 것이므로 그냥 곧바로 procdump를 설치해도 됩니다.

[설치] 
$ sudo apt-get install procdump

[제거]
$ sudo apt-get purge procdump

아쉬운 점이 있다면, procdump의 제어 옵션이 많지 않아서 (대부분의 경우) 그냥 process id 정도만 지정해 실행 중인 응용 프로그램을 덤프하는 용도로 사용할 수 있다는 점입니다.

$ sudo procdump -p 18362

그 외에 특정 예외가 발생했을 때라든지, 비정상 종료할 때와 같은 옵션이 없으므로 현실적으로 봤을 때 아직 사용처가 그다지 많지는 않습니다.




procdump 말고, .NET 런타임이 제공하는 createdump도 있습니다. 사실 현재는 procdump가 아직 크게 장점이 없으므로 그냥 기본 제공하는 createdump를 쓰는 것이 더 나을 수 있습니다.

$ readlink $(which dotnet) -f
/usr/share/dotnet/dotnet

$ $(find /usr/share/dotnet -name createdump)
createdump [options] pid
-f, --name - dump path and file name. The pid can be placed in the name with %d. The default is '/tmp/coredump.%d'
-n, --normal - create minidump.
-h, --withheap - create minidump with heap (default).
-t, --triage - create triage minidump.
-u, --full - create full core dump.
-d, --diag - enable diagnostic messages.

또는 (SCD 방식으로) publish된 .NET Core 응용 프로그램의 경우 배포된 바이너리에도 createdump를 포함하고 있으므로 그것을 사용해도 됩니다. (기본적으로 실행 권한이 없어 사용하기 전 chmod로 권한 변경을 해야 합니다.)

$ chmod u+x /home/tusr/corecorelin/createdump
$ /home/tusr/corecorelin/createdump

닷넷 응용 프로그램을 분석하려면 (Windows에서도 그랬듯이) full memory dump를 받아야 하므로 createdump에 "-u" 옵션을 주는 것을 잊지 말아야 합니다.

$ sudo $(find /usr/share/dotnet -name createdump) -u 55130




어차피 createdump나 procdump는 리눅스의 gcore 방식을 사용한다고 합니다. 그래서 다음의 글에도 나오지만, gcore로 뜬 덤프로도 분석이 가능합니다.

Analyzing .NET Core memory on Linux with LLDB
; https://codeblog.dotsandbrackets.com/net-core-memory-linux/

(따지고 보면 한 가지이지만) 이 3가지 방식의 문제는, 덤프 파일의 크기입니다. Full 메모리 덤프이기 때문에 해당 프로세스와 연관된 모든 메모리를 덤프하는데 pmap이 알려주는 메모리 크기라고 보시면 됩니다.

$ ps -Af | grep dotnet
root       9873      1  1 13:42 ?        00:00:56 /usr/share/dotnet/dotnet /home/tusr/corecorelin/CoreCoreLin.dll
root      17227 102608  0 15:01 pts/2    00:00:00 grep --color=auto dotnet

$ sudo pmap 9873 | grep total
...[생략]...
 total         22161860K

CoreCoreLin.dll은 비주얼 스튜디오가 생성해 주는 ASP.NET Core 기본 예제를 빌드한 것인데 (비록 압축을 하면 70MB 정도로 줄어들기는 하지만) 거의 22GB에 달하는 덤프 파일이 생성됩니다. (Centos 7에서 테스트하면 7GB 정도의 덤프가 생성됩니다.) ^^; 이 때문에 일부 리눅스 설정에서는,

#ulimit -a

ulimit의 "core file size" 제약이 있는 경우 덤프를 뜨기 전 "unlimited"로 풀어줄 필요가 있습니다.

#ulimit -c unlimited

참고로 procdump의 경우에는 향후 크기가 줄어들 여지가 있는데요,

How to reduce the dump file size ?
; https://github.com/Microsoft/ProcDump-for-Linux/issues/33

현재 새롭게 만들고 있는 core dumper가 구현된다면 그때에나 procdump가 createdump에 비해 쓸만할 수 있을 것입니다.




마지막으로 .NET Core 3 이상의 환경이라면 dotnet-dump 도구를 활용하는 방법도 있습니다.

덤프 수집 및 분석 유틸리티(dotnet-dump)
; https://learn.microsoft.com/ko-kr/dotnet/core/diagnostics/dotnet-dump

설치는 별도로 해야 하지만,

# dotnet tool install -g dotnet-dump

# /home/$USER/.dotnet/tools/dotnet-dump --help
Usage:
  dotnet-dump [options] [command]

Options:
  --version    Display version information

Commands:
  collect                Capture dumps from a process
  analyze     Starts an interactive shell with debugging commands to explore a dump
  ps                     Display a list of dotnet processes to create dump from

편리하게도 "ps" 옵션을 주면 실행 중인 프로세스 중에 ".NET Core 3.0" 이상의 런타임을 사용하는 목록만 나열하기 때문에,

$ sudo dotnet-dump ps
     25604 dotnet     /usr/share/dotnet/dotnet

그에 대해 "collect" 명령어로 덤프를 뜨면 됩니다.

$ sudo dotnet-dump collect --process-id 25604

// 만약 .NET Core 3.0 미만의 런타임을 사용하는 프로세스를 지정하면 오류 발생
$ sudo dotnet-dump collect -p 6154
Writing minidump with heap to /home/tusr/temp/core_20191213_101617
Process 6154 not running compatible .NET Core runtime

그런데, 웬일인지 제가 테스트했을 때는,

$ sudo dotnet-dump collect -p 25604
Writing minidump with heap to /home/tusr/temp/core_20191213_094748
Core dump generation FAILED 0x80004005

저렇게 0x80004005 오류가 발생하는 데, Ubuntu 18.04와 Centos 7 모두에서 덤프가 안 됩니다. 실행이 안 되므로 더 이상 실습을 할 수가 없군요. ^^; 일단 문서 상으로 보면,

The dotnet-dump CLI global tool is way to collect and analyze Windows and Linux dumps all without any native debugger involved like lldb on Linux. This is important on platforms like Alpine Linux or Linux ARM32/ARM64 where a fully working lldb isn't available. The dotnet-dump tool will allow you to run SOS commands to analyze crashes and the GC, but it isn't a native debugger so things like displaying the native stack frames isn't supported.

Currently not supported on macOS.


용량이 꽤나 줄어드는 것은 맞겠지만 그에 따라 "native stack frames" 등의 정보가 지원되지 않는다는 걸로 봐서는 웬만한 환경에서는 그냥 createdump로 뜨는 것이 더 나을 수 있습니다. 디버깅 해보신 분들은 알겠지만, 때로는 GC Heap 이외의 정보도 필요하기 때문에 기왕 덤프 뜨는 거 모든 정보를 갖는 파일 하나가... 제 생각에는 더 좋겠다는 생각입니다.




정리해 보면, 현재 시점 기준으로는 createdump가 가장 나은 듯합니다. 다른 덤프 방법들은 상황에 따라 잘 활용하시면 될 것 같고. ^^




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/20/2023]

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

비밀번호

댓글 작성자
 



2020-02-13 10시12분
정성태
2021-01-14 01시50분
정성태
2021-05-24 02시51분
정성태

... 91  92  93  94  95  96  97  98  99  [100]  101  102  103  104  105  ...
NoWriterDateCnt.TitleFile(s)
11463정성태3/15/201820220.NET Framework: 733. 스레드 간의 read/write 시에도 lock이 필요 없는 경우파일 다운로드1
11462정성태3/14/201823790개발 환경 구성: 354. HTTPS 호출에 대한 TLS 설정 확인하는 방법 [1]
11461정성태3/13/201826141오류 유형: 454. 윈도우 업데이트 설치 오류 - 0x800705b4 [1]
11460정성태3/13/201818254디버깅 기술: 112. windbg - 닷넷 메모리 덤프에서 전역 객체의 내용을 조사하는 방법
11459정성태3/13/201819467오류 유형: 453. Debug Diagnostic Tool에서 mscordacwks.dll을 찾지 못하는 문제
11458정성태2/21/201820423오류 유형: 452. This share requires the obsolete SMB1 protocol, which is unsafe and could expose your system to attack. [1]
11457정성태2/17/201824568.NET Framework: 732. C# - Task.ContinueWith 설명 [1]파일 다운로드1
11456정성태2/17/201830624.NET Framework: 731. C# - await을 Task 타입이 아닌 사용자 정의 타입에 적용하는 방법 [7]파일 다운로드1
11455정성태2/17/201819852오류 유형: 451. ASP.NET Core - An error occurred during the compilation of a resource required to process this request.
11454정성태2/12/201828830기타: 71. 만료된 Office 제품 키를 변경하는 방법
11453정성태1/31/201820593오류 유형: 450. Azure Cloud Services(classic) 배포 시 "Certificate with thumbprint ... doesn't exist." 오류 발생
11452정성태1/31/201826022기타: 70. 재현 가능한 최소한의 예제 프로젝트란? [3]파일 다운로드1
11451정성태1/24/201819879디버깅 기술: 111. windbg - x86 메모리 덤프 분석 시 닷넷 메서드의 호출 인자 값 확인
11450정성태1/24/201836098Windows: 146. PowerShell로 원격 프로세스(EXE, BAT) 실행하는 방법 [1]
11449정성태1/23/201822771오류 유형: 449. 단위 테스트 - Could not load file or assembly 'Microsoft.VisualStudio.QualityTools.VideoRecorderEngine' or one of its dependencies. [1]
11448정성태1/20/201820844오류 유형: 448. Fakes를 포함한 단위 테스트 프로젝트를 빌드 시 CS0619 관련 오류 발생
11447정성태1/20/201822136.NET Framework: 730. dotnet user-secrets 명령어 [2]파일 다운로드1
11446정성태1/20/201822477.NET Framework: 729. windbg로 살펴보는 GC heap의 Segment 구조 [2]파일 다운로드1
11445정성태1/20/201820733.NET Framework: 728. windbg - 눈으로 확인하는 Workstation GC / Server GC
11444정성태1/19/201820297VS.NET IDE: 125. Visual Studio에서 Selenium WebDriver를 이용한 웹 브라우저 단위 테스트 구성파일 다운로드1
11443정성태1/18/201821735VC++: 124. libuv 모듈 살펴 보기
11442정성태1/18/201818774개발 환경 구성: 353. ASP.NET Core 프로젝트의 "Enable unmanaged code debugging" 옵션 켜는 방법
11441정성태1/18/201817243오류 유형: 447. ASP.NET Core 배포 오류 - Ensure that restore has run and that you have included '...' in the TargetFrameworks for your project.
11440정성태1/17/201820526.NET Framework: 727. ASP.NET의 HttpContext.Current 구현에 대응하는 ASP.NET Core의 IHttpContextAccessor/HttpContextAccessor 사용법파일 다운로드1
11439정성태1/17/201826230기타: 69. C# - CPU 100% 부하 주는 프로그램파일 다운로드1
11438정성태1/17/201820128오류 유형: 446. Error CS0234 The type or namespace name 'ITuple' does not exist in the namespace
... 91  92  93  94  95  96  97  98  99  [100]  101  102  103  104  105  ...