Microsoft MVP성태의 닷넷 이야기
디버깅 기술: 70. windbg + 닷넷 디버깅 (1) - 배열 인덱스 사용 패턴 [링크 복사], [링크+제목 복사]
조회: 14336
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

windbg + 닷넷 디버깅 (1) - 배열 인덱스 사용 패턴

모듈명 지정은 대체로 확장자를 빼도 잘 됩니다.

0:021> !lmi TestApp
Loaded Module Info: [TestApp] 
         Module: TestApp
   Base Address: 6ded0000
     Image Name: C:\Windows\assembly\GAC_MSIL\TestApp\5.0.5.0__cc862a9be44088eb\TestApp.dll
   Machine Type: 332 (I386)
     Time Stamp: 54ec24dc Tue Feb 24 16:14:36 2015
           Size: 102000
       CheckSum: fe709
Characteristics: 2102  
Debug Data Dirs: Type  Size     VA  Pointer
             CODEVIEW   11c, fc290,   fb290 RSDS - GUID: {D7414896-35BC-40B9-9897-C786B3457959}
               Age: 1, Pdb: d:\TestApp\obj\Release\TestApp.pdb
    Symbol Type: DEFERRED - No error - symbol load deferred
    Load Report: no symbols loaded

실제로 lm 명령을 내려도 모듈 명으로 확장자가 누락되어 있기도 한데요.

0:021> lm
start    end        module name
6bba0000 6bffa000   mscorlib   (pdb symbols)          d:\symbols\mscorlib.pdb\F9DA4689335E4020947985CC71F44A541\mscorlib.pdb
6ded0000 6dfd2000   TestApp   (no symbols)           
...
77020000 770e6000   ADVAPI32   (pdb symbols)          d:\symbols\advapi32.pdb\66A66DBC8B0E4D88B54A47510DD8039B2\advapi32.pdb
77280000 773a8000   ntdll      (pdb symbols)          d:\symbols\ntdll.pdb\78B918DA4A614679A2053B1FB82EEE652\ntdll.pdb

그런데 위의 목록에 보면 TestApp 모듈이 pdb 로딩이 안되어 있어서 다음과 같이 명시적인 reload를 모듈명만 전달해 실행했더니 로드를 하지 못합니다.

0:021> .reload TestApp

"TestApp" was not found in the image list.
Debugger will attempt to load "TestApp" at given base 00000000.

Please provide the full image name, including the extension (i.e. kernel32.dll)
for more reliable results.Base address and size overrides can be given as
.reload <image.ext>=<base>,<size>.
DBGENG:  TestApp - Partial symbol image load missing image info
DBGHELP: No header for TestApp.  Searching for dbg file
DBGHELP: .\TestApp.dbg - file not found
DBGHELP: TestApp missing debug info.  Searching for pdb anyway
DBGHELP: Can't use symbol server for TestApp.pdb - no header information available
DBGHELP: TestApp.pdb - file not found
DBGHELP: TestApp - no symbols loaded
Unable to add module at 00000000'

헷갈리게도 /f /v 옵션을 붙이면 'unmatched'라는 결과값을 보여줍니다.

0:021> .reload /f /v TestApp

"TestApp" was not found in the image list.
Debugger will attempt to load "TestApp" at given base 00000000.

Please provide the full image name, including the extension (i.e. kernel32.dll)
for more reliable results.Base address and size overrides can be given as
.reload <image.ext>=<base>,<size>.
DBGENG:  TestApp - Partial symbol image load missing image info
DBGHELP: No header for jennifer5.  Searching for dbg file
DBGHELP: d:\TestApp\bin\TestApp.dbg - file not found
DBGHELP: .\TestApp.dbg - file not found
DBGHELP: TestApp missing debug info.  Searching for pdb anyway
DBGHELP: Can't use symbol server for TestApp.pdb - no header information available
DBGHELP: TestApp_0 - private symbols & lines 
         d:\TestApp\bin\TestApp.pdb - unmatched
Unable to add module at 00000000'

물론, chkmatch.exe로 확인해 보면 둘다 일치하다고 나옵니다.

D:\TestApp\bin>ChkMatch.exe -c C:\Windows\assembly\GAC_MSIL\TestApp\5.0.5.0__cc862a9be44088eb\TestApp.dll  D:\TestApp\bin\TestApp.pdb
ChkMatch - version 1.0
Copyright (C) 2004 Oleg Starodumov
http://www.debuginfo.com/


Executable: C:\Windows\assembly\GAC_MSIL\TestApp\5.0.5.0__cc862a9be44088eb\TestApp.dll
Debug info file: TestApp.pdb

Executable:
TimeDateStamp: 54ec24dc
Debug info: 2 ( CodeView )
TimeStamp: 54ec24dc  Characteristics: 0  MajorVer: 0  MinorVer: 0
Size: 284  RVA: 000fc290  FileOffset: 000fb290
CodeView format: RSDS
Signature: {d7414896-35bc-40b9-9897-c786b3457959}  Age: 1
PdbFile: d:\TestApp\obj\Release\TestApp.pdb


Debug information file:
Format: PDB 7.00
Signature: {d7414896-35bc-40b9-9897-c786b3457959}  Age: 1

Result: Matched

.reload 명령으로 pdb를 로드할 때는 (에러메시지에서도 나오지만) "full image name", 즉 확장자를 포함한 이름을 줘야 합니다. 그럼, 이렇게 잘 로드가 됩니다. ^^

0:021> .reload /f /v TestApp.dll
AddImage: C:\Windows\assembly\GAC_MSIL\jennifer5\5.0.5.0__cc862a9be44088eb\TestApp.dll
 DllBase  = 6ded0000
 Size     = 00102000
 Checksum = 000fe709
 TimeDateStamp = 54ec24dc
SYMSRV:  d:\symbols\TestApp.pdb\D741489635BC40B99897C786B34579591\TestApp.pdb not found
SYMSRV:  http://msdl.microsoft.com/download/symbols/TestApp.pdb/D741489635BC40B99897C786B34579591/TestApp.pdb not found
DBGHELP: TestApp - private symbols & lines 
         d:\TestApp\bin\TestApp.pdb




어셈블리의 cdq 명령어는,

...[생략]...
04ce0844 99              cdq
04ce0845 f7f9            idiv    eax,ecx

대부분 idiv와 쌍을 이룬다고 보시면 됩니다. 나눗셈 명령어는 나누는 수(divisor)가 32비트인 경우 피젯수(dividend)는 64비트가 되는 식으로 2배의 피젯수가 사용되는데요. cdq는 eax 레지스터의 내용을 부호를 고려해 edx 레지스터로 확장해 주는 역할을 합니다. 즉, 32비트의 값을 64비트로 확장해 주는 것입니다. 이 때문에 자연스럽게 idiv 명령어가 요구하는 64비트 피젯수가 마련됩니다.

CDQ - Convert Double to Quad
; http://faydoc.tripod.com/cpu/cdq.htm

위의 매뉴얼에 보면, 코드가 99로 동일한 CWD 명령어도 나오는데요. 아마도 이것은 16비트 시절에 사용된 명령어가 아닌가 생각됩니다.

그렇다면 64비트에서는 128비트로 확장해 주는 명령어가 필요하다는 것을 유추할 수 있는데요. 찾아보니 cqo 명령어가 그것입니다.

x64 Instructions
; https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/x64-instructions

rax 레지스터의 내용을 부호 비트를 고려해 rdx:rax로 확장해 주는데 명령어 코드도 역시 99입니다.

참고로, 1바이트의 내용을 2바이트로 확장해 주는 명령어로 cbw가 있는데... 예외적으로 이것은 명령어 코드가 98입니다.




닷넷 프로그램에서 배열 인덱스를 사용하는 경우,

int [] array = new int [4];
int n = 3;

int result = array[n];

닷넷은 관리코드로써 n의 값이 array.Length를 벗어나지 않음을 체크하기 때문에 이런 경우 다음과 같이 JIT_RngChkFail 메서드 호출이 JIT 컴파일 이후에 나타나게 됩니다.

04ce084a 8b45d8          mov     eax,3     // 배열 인덱스로 지정된 값
04ce084d 8b55c0          mov     edx,4     // 실제로는 Array타입의 Length값을 구해서 들어감.
04ce0850 3b4204          cmp     eax, edx  // cmp 명령어는 sub처럼 동작하지만 결과값을 저장하지는 않고 FLAG 레지스터만 변경
                                           // 결국, eax의 값에서 edx값을 빼기
                                           // 만약 이 값이 음수가 되면 CF=1을 설정
                                           // 간단히 말해서,
                                           // 배열 인덱스 값(eax)이 edx보다 작으면 CF=1 설정
                                           // 배열 인덱스 값(eax)이 edx보다 같거나 크면 CF=0 설정
04ce0853 7205            jb      04ce085a  // jb 명령어는 CF=1인 경우, 지정된 주소를 EIP 레지스터로 로드
                                           // 따라서, 배열 인덱스 값이 Array.Length보다 작으면 점프하게 되고,
                                           //                                         같거나 크면 바로 다음 구문을 실행
04ce0855 e86a1cd863      call    mscorwks!JIT_RngChkFail (68a624c4) // 이것이 바로 System.IndexOutOfRangeException 예외를 발생.

04ce085a 8b44820c        mov     eax,dword ptr [edx+eax*4+0Ch]

배열 인덱스가 사용되면 거의 저 패턴으로 기계어 코드가 나타난다고 보면 됩니다.




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







[최초 등록일: ]
[최종 수정일: 7/17/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)
13601정성태4/19/2024214닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/2024284닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/2024333닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/2024355닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드1
13597정성태4/15/2024436닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/2024804닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
13595정성태4/13/2024920닷넷: 2237. C# - Audio 장치 열기 (Windows Multimedia, NAudio)파일 다운로드1
13594정성태4/12/20241021닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241051닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
13592정성태4/2/20241207C/C++: 165. CLion으로 만든 Rust Win32 DLL을 C#과 연동
13591정성태4/2/20241169닷넷: 2234. C# - WPF 응용 프로그램에 Blazor App 통합파일 다운로드1
13590정성태3/31/20241073Linux: 70. Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제
13589정성태3/29/20241143닷넷: 2233. C# - 프로세스 CPU 사용량을 나타내는 성능 카운터와 Win32 API파일 다운로드1
13588정성태3/28/20241197닷넷: 2232. C# - Unity + 닷넷 App(WinForms/WPF) 간의 Named Pipe 통신파일 다운로드1
13587정성태3/27/20241156오류 유형: 900. Windows Update 오류 - 8024402C, 80070643
13586정성태3/27/20241298Windows: 263. Windows - 복구 파티션(Recovery Partition) 용량을 늘리는 방법
13585정성태3/26/20241096Windows: 262. PerformanceCounter의 InstanceName에 pid를 추가한 "Process V2"
13584정성태3/26/20241049개발 환경 구성: 708. Unity3D - C# Windows Forms / WPF Application에 통합하는 방법파일 다운로드1
13583정성태3/25/20241158Windows: 261. CPU Utilization이 100% 넘는 경우를 성능 카운터로 확인하는 방법
13582정성태3/19/20241420Windows: 260. CPU 사용률을 나타내는 2가지 수치 - 사용량(Usage)과 활용률(Utilization)파일 다운로드1
13581정성태3/18/20241588개발 환경 구성: 707. 빌드한 Unity3D 프로그램을 C++ Windows Application에 통합하는 방법
13580정성태3/15/20241138닷넷: 2231. C# - ReceiveTimeout, SendTimeout이 적용되지 않는 Socket await 비동기 호출파일 다운로드1
13579정성태3/13/20241494오류 유형: 899. HTTP Error 500.32 - ANCM Failed to Load dll
13578정성태3/11/20241629닷넷: 2230. C# - 덮어쓰기 가능한 환형 큐 (Circular queue)파일 다운로드1
13577정성태3/9/20241876닷넷: 2229. C# - 닷넷을 위한 난독화 도구 소개 (예: ConfuserEx)
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...