Microsoft MVP성태의 닷넷 이야기
디버깅 기술: 70. windbg + 닷넷 디버깅 (1) - 배열 인덱스 사용 패턴 [링크 복사], [링크+제목 복사],
조회: 14437
글쓴 사람
정성태 (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)
13310정성태4/6/20234155C/C++: 162. Visual Studio - /NODEFAULTLIB 옵션 설정 후 수동으로 추가해야 할 library
13309정성태4/5/20234303.NET Framework: 2107. .NET 6+ FileStream의 구조 변화
13308정성태4/4/20234202스크립트: 47. 파이썬의 time.time() 실숫값을 GoLang / C#에서 사용하는 방법
13307정성태4/4/20233954.NET Framework: 2106. C# - .NET Core/5+ 환경의 Windows Forms 응용 프로그램에서 HINSTANCE 구하는 방법
13306정성태4/3/20233730Windows: 243. Win32 - 윈도우(cbWndExtra) 및 윈도우 클래스(cbClsExtra) 저장소 사용 방법
13305정성태4/1/20234125Windows: 242. Win32 - 시간 만료를 갖는 MessageBox 대화창 구현 (쉬운 버전)파일 다운로드1
13304정성태3/31/20234439VS.NET IDE: 181. Visual Studio - C/C++ 프로젝트에 application manifest 적용하는 방법
13303정성태3/30/20233815Windows: 241. 환경 변수 %PATH%에 DLL을 찾는 규칙
13302정성태3/30/20234442Windows: 240. RDP 환경에서 바뀌는 %TEMP% 디렉터리 경로
13301정성태3/29/20234570Windows: 239. C/C++ - Windows 10 Version 1607부터 지원하는 /DEPENDENTLOADFLAG 옵션파일 다운로드1
13300정성태3/28/20234209Windows: 238. Win32 - Modal UI 창에 올바른 Owner(HWND)를 설정해야 하는 이유
13299정성태3/27/20233966Windows: 237. Win32 - 모든 메시지 루프를 탈출하는 WM_QUIT 메시지
13298정성태3/27/20233947Windows: 236. Win32 - MessageBeep 소리가 안 들린다면?
13297정성태3/26/20234609Windows: 235. Win32 - Code Modal과 UI Modal
13296정성태3/25/20233930Windows: 234. IsDialogMessage와 협업하는 WM_GETDLGCODE Win32 메시지 [1]파일 다운로드1
13295정성태3/24/20234204Windows: 233. Win32 - modeless 대화창을 modal처럼 동작하게 만드는 방법파일 다운로드1
13294정성태3/22/20234375.NET Framework: 2105. LargeAddressAware 옵션이 적용된 닷넷 32비트 프로세스의 가용 메모리 - 두 번째
13293정성태3/22/20234441오류 유형: 853. dumpbin - warning LNK4048: Invalid format file; ignored
13292정성태3/21/20234553Windows: 232. C/C++ - 일반 창에도 사용 가능한 IsDialogMessage파일 다운로드1
13291정성태3/20/20234947.NET Framework: 2104. C# Windows Forms - WndProc 재정의와 IMessageFilter 사용 시의 차이점
13290정성태3/19/20234426.NET Framework: 2103. C# - 윈도우에서 기본 제공하는 FindText 대화창 사용법파일 다운로드1
13289정성태3/18/20233634Windows: 231. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 자식 윈도우를 생성하는 방법파일 다운로드1
13288정성태3/17/20233745Windows: 230. Win32 - 대화창의 DLU 단위를 pixel로 변경하는 방법파일 다운로드1
13287정성태3/16/20233923Windows: 229. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 윈도우를 직접 띄우는 방법파일 다운로드1
13286정성태3/15/20234365Windows: 228. Win32 - 리소스에 포함된 대화창 Template의 2진 코드 해석 방법
13285정성태3/14/20233936Windows: 227. Win32 C/C++ - Dialog Procedure를 재정의하는 방법파일 다운로드1
1  2  3  4  5  6  7  8  9  10  11  12  [13]  14  15  ...