Microsoft MVP성태의 닷넷 이야기
디버깅 기술: 70. windbg + 닷넷 디버깅 (1) - 배열 인덱스 사용 패턴 [링크 복사], [링크+제목 복사],
조회: 14434
글쓴 사람
정성태 (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)
13514정성태1/5/20242297개발 환경 구성: 702. IIS - AppPool의 "Disable Overlapped Recycle" 옵션
13513정성태1/5/20242222닷넷: 2194. C# - WebActivatorEx / System.Web의 PreApplicationStartMethod 특성
13512정성태1/4/20242173개발 환경 구성: 701. IIS - w3wp.exe 프로세스의 ASP.NET 런타임을 항상 Warmup 모드로 유지하는 preload Enabled 설정
13511정성태1/4/20242194닷넷: 2193. C# - ASP.NET Web Application + OpenAPI(Swashbuckle) 스펙 제공
13510정성태1/3/20242120닷넷: 2192. C# - 특정 실행 파일이 있는지 확인하는 방법 (Linux)
13509정성태1/3/20242168오류 유형: 887. .NET Core 2 이하의 프로젝트에서 System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp2.0.
13508정성태1/3/20242216오류 유형: 886. ORA-28000: the account is locked
13507정성태1/2/20242862닷넷: 2191. C# - IPGlobalProperties를 이용해 netstat처럼 사용 중인 Socket 목록 구하는 방법파일 다운로드1
13506정성태12/29/20232451닷넷: 2190. C# - 닷넷 코어/5+에서 달라지는 System.Text.Encoding 지원
13505정성태12/27/20232979닷넷: 2189. C# - WebSocket 클라이언트를 닷넷으로 구현하는 예제 (System.Net.WebSockets)파일 다운로드1
13504정성태12/27/20232568닷넷: 2188. C# - ASP.NET Core SignalR로 구현하는 채팅 서비스 예제파일 다운로드1
13503정성태12/27/20232434Linux: 67. WSL 환경 + mlocate(locate) 도구의 /mnt 디렉터리 검색 문제
13502정성태12/26/20232546닷넷: 2187. C# - 다른 프로세스의 환경변수 읽는 예제파일 다운로드1
13501정성태12/25/20232320개발 환경 구성: 700. WSL + uwsgi - IPv6로 바인딩하는 방법
13500정성태12/24/20232410디버깅 기술: 194. Windbg - x64 가상 주소를 물리 주소로 변환
13498정성태12/23/20233098닷넷: 2186. 한국투자증권 KIS Developers OpenAPI의 C# 래퍼 버전 - eFriendOpenAPI NuGet 패키지
13497정성태12/22/20232494오류 유형: 885. Visual Studiio - error : Could not connect to the remote system. Please verify your connection settings, and that your machine is on the network and reachable.
13496정성태12/21/20232478Linux: 66. 리눅스 - 실행 중인 프로세스 내부의 환경변수 설정을 구하는 방법 (gdb)
13495정성태12/20/20232412Linux: 65. clang++로 공유 라이브러리의 -static 옵션 빌드가 가능할까요?
13494정성태12/20/20232579Linux: 64. Linux 응용 프로그램의 (C++) so 의존성 줄이기(ReleaseMinDependency) - 두 번째 이야기
13493정성태12/19/20232701닷넷: 2185. C# - object를 QueryString으로 직렬화하는 방법
13492정성태12/19/20232375개발 환경 구성: 699. WSL에 nopCommerce 예제 구성
13491정성태12/19/20232267Linux: 63. 리눅스 - 다중 그룹 또는 사용자를 리소스에 권한 부여
13490정성태12/19/20232399개발 환경 구성: 698. Golang - GLIBC 의존을 없애는 정적 빌드 방법
13489정성태12/19/20232175개발 환경 구성: 697. GoLand에서 ldflags 지정 방법
13488정성태12/18/20232109오류 유형: 884. HTTP 500.0 - 명령행에서 실행한 ASP.NET Core 응용 프로그램을 실행하는 방법
1  2  3  4  [5]  6  7  8  9  10  11  12  13  14  15  ...