Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 2개 있습니다.)
(시리즈 글이 4개 있습니다.)
디버깅 기술: 90. windbg의 lm 명령으로 보이지 않는 .NET 4.0 ClassLibrary를 명시적으로 로드하는 방법
; https://www.sysnet.pe.kr/2/0/11256

.NET Framework: 689. CLR 4.0 환경에서 DLL 모듈의 로드 주소(Base address) 알아내는 방법
; https://www.sysnet.pe.kr/2/0/11325

.NET Framework: 690. coreclr 소스코드로 알아보는 .NET 4.0의 모듈 로딩 함수
; https://www.sysnet.pe.kr/2/0/11326

디버깅 기술: 102. windbg - .NET 4.0 이상의 환경에서 DLL의 심벌 파일 로드 방법
; https://www.sysnet.pe.kr/2/0/11330




windbg의 lm 명령으로 보이지 않는 .NET 4.0 ClassLibrary를 명시적으로 로드하는 방법

재현을 간단하게 해볼 수 있습니다. 우선 다음과 같은 코드를 가진 라이브러리를 생성하고,

// ClassLibrary1.dll 프로젝트
using System;

namespace ClassLibrary1
{
    public class Class1
    {
        public void Do()
        {
            Console.WriteLine("TEST IS GOOD");
        }
    }
}

콘솔 EXE에서 이렇게 사용한 후,

// .NET 4.0 이상의 ConsoleApp1.exe 프로젝트
using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ClassLibrary1.Class1 cl = new ClassLibrary1.Class1();
            cl.Do();

            Console.WriteLine("Press...");
            Console.ReadLine();
        }
    }
}

실행해서 Full Dump 파일을 받습니다. 이를 windbg에서 열어 lm 명령으로 확인하면,

0:000> lm
start    end        module name
00a60000 00a68000   ConsoleApp1   (deferred)             
601d0000 6155d000   mscorlib_ni   (deferred)             
72290000 722bf000   rsaenh     (deferred)             
...[생략]...
77270000 77363000   ole32      (deferred)             
77370000 77430000   rpcrt4     (deferred)             
774c0000 7764e000   ntdll      (pdb symbols)          C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\sym\wntdll.pdb\3E6B95F10656482A735FBE90515F548E1\wntdll.pdb

Unloaded modules:
75530000 75536000   psapi.dll

EXE 파일이었던 ConsoleApp1은 목록에 있는 반면, ClassLibrary1.dll은 보이질 않습니다. 찾아 보니 다음의 Q&A에 답이 나옵니다.

WinDbg lm command does not show all loaded modules.
; https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/263a3d84-8256-4f03-a70e-47d482a24cfb/windbg-lm-command-does-not-show-all-loaded-modules?forum=windbg

Solving .net source code lines problem in windbg with SOSEX
; https://debugandconquer.blogspot.kr/2016/08/solving-net-source-code-lines-problem.html

위의 글에서는 Process Explorer를 이용해 ClassLibrary1.dll의 실제 로딩 주소를 파악하는 방법이 나오는데요. 대개의 경우 full dump 파일을 얻는 상황은 실제 프로세스가 살아있지 않은 다른 PC에서 사용되는 것이 보통이므로 다른 방법이 있어야 합니다.

이를 위해 sos의 도움이 필요한데요, 다음과 같이 원하는 DLL에 포함된 심벌을 조회해 보면,

0:000> .loadby sos clr

0:000> !name2ee *!ClassLibrary1.Class1
Module:      601d1000
Assembly:    mscorlib.dll
--------------------------------------
Module:      00fe3ffc
Assembly:    ConsoleApp1.exe
--------------------------------------
Module:      00fe4e04
Assembly:    ClassLibrary1.dll
Token:       02000002
MethodTable: 00fe51f0
EEClass:     00fe295c
Name:        ClassLibrary1.Class1

ClassLibrary1.dll의 Module 주소를 구할 수 있고 이를 다음과 같이 .reload 명령어의 이미지 주소로 사용할 수 있습니다.

// 형식: .reload /f image_name=image_base_address

0:000> .reload /f ClassLibrary1.dll=00fe4e04

이후 다시 lm 명령을 내리면 다음과 같이 해당 DLL이 목록에 나타납니다.

0:000> lm
start    end        module name
00a60000 00a68000   ConsoleApp1   (deferred)             
00fe4e04 00fe4e04   ClassLibrary1   (no symbols)           
601d0000 6155d000   mscorlib_ni   (deferred)             
72290000 722bf000   rsaenh     (deferred)             
722c0000 722d3000   cryptsp    (deferred)             
722e0000 72360000   clrjit     (deferred)             
...[생략]...
77030000 77268000   combase    (deferred)             
77270000 77363000   ole32      (deferred)             
77370000 77430000   rpcrt4     (deferred)             
774c0000 7764e000   ntdll      (pdb symbols)          C:\Program Files (x86)\Windows Kits\10\Debuggers\x86\sym\wntdll.pdb\3E6B95F10656482A735FBE90515F548E1\wntdll.pdb

Unloaded modules:
75530000 75536000   psapi.dll

그런데, 가만 보면 ConsoleApp1.exe의 Name2EE로 출력된 Module 주소가 00fe3ffc로 나오는 반면 lm의 ConsoleApp1.exe start 주소는 00a60000입니다. 이렇게 다른데도 정상적으로 이미지 로딩이 된 걸까요?

확인을 위해 각각의 모듈을 lm 출력 결과로 보이는 start 주소를 이용해 파일로 저장해 봤습니다.

0:000> !savemodule 00a60000 c:\temp\ConsoleApp1.exe
3 sections in file
section 0 - VA=2000, VASize=87c, FileAddr=200, FileSize=a00
section 1 - VA=4000, VASize=5bc, FileAddr=c00, FileSize=600
section 2 - VA=6000, VASize=c, FileAddr=1200, FileSize=200

0:000> !savemodule 00fe4e04 c:\temp\ClassLibrary1.dll
3 sections in file
section 0 - VA=2000, VASize=8dc, FileAddr=200, FileSize=a00
section 1 - VA=4000, VASize=398, FileAddr=c00, FileSize=400
section 2 - VA=6000, VASize=c, FileAddr=1000, FileSize=200

잘 저장이 되는군요. ^^ 게다가 .NET Reflector 등의 디컴파일러 도구를 이용해 ClassLibrary1.dll을 조사해 보면 정상적으로 코드가 나오는 것을 확인할 수 있습니다.




참고로, .NET 3.5 대상으로 이 글의 예제를 빌드하면 lm 명령어의 출력에 정상적으로 나옵니다.

또한, !Name2EE로 심벌을 조사하는데 이때 원하는 DLL에 구현된 클래스를 알지 못해도 상관없습니다. 그냥 다음과 같이 아무 이름이나 넣으면,

!name2ee *!NOT_CARE

모든 닷넷 어셈블리들의 목록이 출력되고 그중 원하는 DLL의 Module 주소만 이용하면 됩니다. 그러니까, 한 가지 팁일 수 있는데요. 해당 프로세스에 로드된 모듈 목록은 lm 명령어로 확인할 수 있지만, 그 프로세스에 로드된 닷넷 모듈을 확인하는 명시적인 명령어는 없습니다. 하지만, "!name2ee *!ANYTHING"과 같은 식으로 명령을 주면 아쉬운 대로 닷넷 모듈 목록을 확인할 수 있습니다.




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 11/3/2022]

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

비밀번호

댓글 작성자
 



2017-10-17 01시08분
windbg - .NET 4.0 이상의 환경에서 DLL의 심벌 파일 로드 방법
; http://www.sysnet.pe.kr/2/0/11330

windbg - .NET 4.0 이상의 환경에서 모든 DLL에 대한 심벌 파일을 로드하는 파이썬 스크립트
; http://www.sysnet.pe.kr/2/0/11331

windbg - 풀 덤프에 포함된 모든 닷넷 모듈을 파일로 저장하는 방법
; https://www.sysnet.pe.kr/2/0/11297
정성태

... 121  122  [123]  124  125  126  127  128  129  130  131  132  133  134  135  ...
NoWriterDateCnt.TitleFile(s)
10848정성태9/8/201527323VS.NET IDE: 103. Visual Studio의 Ctrl + F5 실행 동작파일 다운로드1
10847정성태9/8/201523443VS.NET IDE: 102. 목록(List) 타입의 값을 디버깅 중 Watch 창에서 확인하는 방법 [1]파일 다운로드1
10846정성태9/8/201520806오류 유형: 306. "query user" 명령어에 공백 문자가 포함된 계정을 전달하는 경우
10845정성태9/3/201521891오류 유형: 305. 윈도우 백업 시 오류 - 0x80780166
10844정성태9/2/201523099.NET Framework: 528. C# - 상호 참조하는 경우의 정적 생성자 동작 방식 [4]파일 다운로드1
10843정성태9/1/201523799VS.NET IDE: 101. Visual Studio 2015의 솔루션 탐색기가 클래스 뷰 정보로 인해 느려지는 현상
10842정성태9/1/201520863.NET Framework: 527. 닷넷 사용자 정의 예외 클래스의 최소 구현 코드 - 세 번째 이야기
10841정성태8/31/201530175개발 환경 구성: 276. Visual Studio 2013에서 C# 6과 닷넷 4.6 기능을 사용하려면?
10839정성태8/22/201528746Windows: 112. 윈도우 10에서 터치 키보드를 안 뜨게 할 수 있는 방법 [4]
10838정성태8/22/201539046오류 유형: 304. Windows 10에서 VPN 연결이 실패한다면? [3]
10837정성태8/21/201519331오류 유형: 303. Your computer is low on memory. Save your files and close these programs...
10836정성태8/21/201520249오류 유형: 302. 설치 파일 실행 시 "This app can't run on your PC" 오류가 뜬다면?
10835정성태8/21/201528785웹: 31. Microsoft Edge 브라우저를 명령행에서 띄우는 방법 [1]
10834정성태8/19/201520867.NET Framework: 526. 닷넷 - 값 형식을 new 없이 생성하면 0으로 초기화되지 않는다?
10833정성태8/18/201525470.NET Framework: 525. C# - 닷넷에서 프로세스가 열고 있는 파일 목록을 구하는 방법파일 다운로드1
10832정성태8/17/201529895디버깅 기술: 74. x64 콜 스택 인자 추적과 windbg의 Child-SP, RetAddr, Args to Child 값 확인 [8]파일 다운로드2
10831정성태8/13/201529893.NET Framework: 524. .NET 4.0과 .NET 4.5의 컴파일 결과 차이점 [1]파일 다운로드1
10830정성태8/12/201523885개발 환경 구성: 275. Web.config이 적용되지 않는 프로젝트에서 Razor 템플릿 파일의 C# 컴파일러 버전 제어 [1]
10829정성태8/10/201525969개발 환경 구성: 274. PowerShell/명령행에서 JDK/JRE를 무인(unattended)/자동 설치를 하는 방법 [3]
10828정성태8/10/201531564웹: 30. Edge 브라우저에서 "이 웹 사이트에는 Internet Explorer가 필요함" 단계를 없애는 방법 [1]
10827정성태7/8/201532964개발 환경 구성: 273. Visual Studio 2015에서 Github와 연동하는 방법 [3]
10826정성태7/8/201522575오류 유형: 301. The trust relationship between this workstation and the primary domain failed. - 두 번째 이야기
10825정성태7/8/201522142개발 환경 구성: 272. Visual Studio IDE 설치 없이 Visual Studio SDK 설치하는 방법
10824정성태7/7/201527321개발 환경 구성: 271. Team Foundation Server 2015 설치 방법 [1]
10823정성태7/7/201527896오류 유형: 300. SqlException (0x80131904): Unable to open the physical file
10822정성태7/7/201527246오류 유형: 299. The 'Visual C++ Project System Package' package did not load correctly.
... 121  122  [123]  124  125  126  127  128  129  130  131  132  133  134  135  ...