Microsoft MVP성태의 닷넷 이야기
디버깅 기술: 70. windbg + 닷넷 디버깅 (1) - 배열 인덱스 사용 패턴 [링크 복사], [링크+제목 복사],
조회: 14456
글쓴 사람
정성태 (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

비밀번호

댓글 작성자
 




... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...
NoWriterDateCnt.TitleFile(s)
11807정성태1/29/201912195Windows: 156. 가상 디스크의 용량을 복구 파티션으로 인해 늘리지 못하는 경우 [4]
11806정성태1/29/201911898디버깅 기술: 122. windbg - 덤프 파일로부터 PID와 환경 변수 등의 정보를 구하는 방법
11805정성태1/28/201913785.NET Framework: 806. C# - int []와 object []의 차이로 이해하는 제네릭의 필요성 [4]파일 다운로드1
11804정성태1/24/201911808Windows: 155. diskpart - remove letter 이후 재부팅 시 다시 드라이브 문자가 할당되는 경우
11803정성태1/10/201911316디버깅 기술: 121. windbg - 닷넷 Finalizer 스레드가 멈춰있는 현상
11802정성태1/7/201912684.NET Framework: 805. 두 개의 윈도우를 각각 실행하는 방법(Windows Forms, WPF)파일 다운로드1
11801정성태1/1/201913575개발 환경 구성: 427. Netsh의 네트워크 모니터링 기능 [3]
11800정성태12/28/201812912오류 유형: 509. WCF 호출 오류 메시지 - System.ServiceModel.CommunicationException: Internal Server Error
11799정성태12/19/201813748.NET Framework: 804. WPF(또는 WinForm)에서 UWP UI 구성 요소 사용하는 방법 [3]파일 다운로드1
11798정성태12/19/201812920개발 환경 구성: 426. vcpkg - "Building vcpkg.exe failed. Please ensure you have installed Visual Studio with the Desktop C++ workload and the Windows SDK for Desktop C++"
11797정성태12/19/201810322개발 환경 구성: 425. vcpkg - CMake Error: Problem with archive_write_header(): Can't create '' 빌드 오류
11796정성태12/19/20189975개발 환경 구성: 424. vcpkg - "File does not have expected hash" 오류를 무시하는 방법
11795정성태12/19/201812355Windows: 154. PowerShell - Zone 별로 DNS 레코드 유형 정보 조회 [1]
11794정성태12/16/20189810오류 유형: 508. Get-AzureWebsite : Request to a downlevel service failed.
11793정성태12/16/201811409개발 환경 구성: 423. NuGet 패키지 제작 - Native와 Managed DLL을 분리하는 방법 [1]
11792정성태12/11/201812195Graphics: 34. .NET으로 구현하는 OpenGL (11) - Per-Pixel Lighting파일 다운로드1
11791정성태12/11/201812167VS.NET IDE: 130. C/C++ 프로젝트의 시작 프로그램으로 .NET Core EXE를 지정하는 경우 닷넷 디버깅이 안 되는 문제 [1]
11790정성태12/11/201810546오류 유형: 507. Could not save daemon configuration to C:\ProgramData\Docker\config\daemon.json: Access to the path 'C:\ProgramData\Docker\config' is denied.
11789정성태12/10/201820375Windows: 153. C# - USB 장치의 연결 및 해제 알림을 위한 WM_DEVICECHANGE 메시지 처리 [2]파일 다운로드2
11788정성태12/4/201810356오류 유형: 506. SqlClient - Value was either too large or too small for an Int32.Couldn't store <2151292191> in ... Column
11787정성태11/29/201814295Graphics: 33. .NET으로 구현하는 OpenGL (9), (10) - OBJ File Format, Loading 3D Models파일 다운로드1
11786정성태11/29/201811060오류 유형: 505. OpenGL.NET 예제 실행 시 "Managed Debugging Assistant 'CallbackOnCollectedDelegate'" 예외 발생
11785정성태11/21/201813428디버깅 기술: 120. windbg 분석 사례 - ODP.NET 사용 시 Finalizer에서 System.AccessViolationException 예외 발생으로 인한 비정상 종료
11784정성태11/18/201813080Graphics: 32. .NET으로 구현하는 OpenGL (7), (8) - Matrices and Uniform Variables, Model, View & Projection Matrices파일 다운로드1
11783정성태11/18/201811203오류 유형: 504. 윈도우 환경에서 docker가 설치된 컴퓨터 간의 ping IP 주소 풀이 오류
11782정성태11/18/201810988Windows: 152. 윈도우 10에서 사라진 "Adapters and Bindings" 네트워크 우선순위 조정 기능 - 두 번째 이야기
... 61  62  63  64  65  66  67  68  69  70  71  72  [73]  74  75  ...