Microsoft MVP성태의 닷넷 이야기
Linux: 26. .NET Core 응용 프로그램을 위한 메모리 덤프 방법 [링크 복사], [링크+제목 복사]
조회: 13080
글쓴 사람
정성태 (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분
정성태

... 16  17  18  19  [20]  21  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13128정성태9/23/20228066C/C++: 158. Visual C++ - IDL 구문 중 "unsigned long"을 인식하지 못하는 #import파일 다운로드1
13127정성태9/22/20226496Windows: 210. WSL에 systemd 도입
13126정성태9/15/20227116.NET Framework: 2049. C# 11 - 정적 메서드에 대한 delegate 처리 시 cache 적용
13125정성태9/14/20227311.NET Framework: 2048. C# 11 - 구조체 필드의 자동 초기화(auto-default structs)
13124정성태9/13/20227057.NET Framework: 2047. Golang, Python, C#에서의 CRC32 사용
13123정성태9/8/20227495.NET Framework: 2046. C# 11 - 멤버(속성/필드)에 지정할 수 있는 required 예약어 추가
13122정성태8/26/20227489.NET Framework: 2045. C# 11 - 메서드 매개 변수에 대한 nameof 지원
13121정성태8/23/20225487C/C++: 157. Golang - 구조체의 slice 필드를 Reflection을 이용해 변경하는 방법
13120정성태8/19/20226987Windows: 209. Windows NT Service에서 UI를 다루는 방법 [3]
13119정성태8/18/20226548.NET Framework: 2044. .NET Core/5+ 프로젝트에서 참조 DLL이 보관된 공통 디렉터리를 지정하는 방법
13118정성태8/18/20225423.NET Framework: 2043. WPF Color의 기본 색 영역은 (sRGB가 아닌) scRGB [2]
13117정성태8/17/20227537.NET Framework: 2042. C# 11 - 파일 범위 내에서 유효한 타입 정의 (File-local types)파일 다운로드1
13116정성태8/4/20228005.NET Framework: 2041. C# - Socket.Close 시 Socket.Receive 메서드에서 예외가 발생하는 문제파일 다운로드1
13115정성태8/3/20228381.NET Framework: 2040. C# - ValueTask와 Task의 성능 비교 [1]파일 다운로드1
13114정성태8/2/20228515.NET Framework: 2039. C# - Task와 비교해 본 ValueTask 사용법파일 다운로드1
13113정성태7/31/20227740.NET Framework: 2038. C# 11 - Span 타입에 대한 패턴 매칭 (Pattern matching on ReadOnlySpan<char>)
13112정성태7/30/20228154.NET Framework: 2037. C# 11 - 목록 패턴(List patterns) [1]파일 다운로드1
13111정성태7/29/20227966.NET Framework: 2036. C# 11 - IntPtr/UIntPtr과 nint/nuint의 통합파일 다운로드1
13110정성태7/27/20228005.NET Framework: 2035. C# 11 - 새로운 연산자 ">>>" (Unsigned Right Shift)파일 다운로드1
13109정성태7/27/20229378VS.NET IDE: 177. 비주얼 스튜디오 2022를 이용한 (소스 코드가 없는) 닷넷 모듈 디버깅 - "외부 원본(External Sources)" [1]
13108정성태7/26/20227402Linux: 53. container에 실행 중인 Golang 프로세스를 디버깅하는 방법 [1]
13107정성태7/25/20226614Linux: 52. Debian/Ubuntu 계열의 docker container에서 자주 설치하게 되는 명령어
13106정성태7/24/20226277오류 유형: 819. 닷넷 6 프로젝트의 "Conditional compilation symbols" 기본값 오류
13105정성태7/23/20227560.NET Framework: 2034. .NET Core/5+ 환경에서 (프로젝트가 아닌) C# 코드 파일을 입력으로 컴파일하는 방법 - 두 번째 이야기 [1]
13104정성태7/23/202210673Linux: 51. WSL - init에서 systemd로 전환하는 방법
13103정성태7/22/20227224오류 유형: 818. WSL - systemd-genie와 관련한 2가지(systemd-remount-fs.service, multipathd.socket) 에러
... 16  17  18  19  [20]  21  22  23  24  25  26  27  28  29  30  ...