Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

(시리즈 글이 7개 있습니다.)
디버깅 기술: 3. 원격 컴퓨터 디버깅 - VPC 설정
; https://www.sysnet.pe.kr/2/0/265

디버깅 기술: 3.1. Managed 원격 디버깅과 WinDBG 원격 디버깅
; https://www.sysnet.pe.kr/2/0/314

디버깅 기술: 27. Windbg - Local Kernel Debug 모드
; https://www.sysnet.pe.kr/2/0/934

디버깅 기술: 29. Windbg - Hyper-V 윈도우 7 원격 디버깅 구성
; https://www.sysnet.pe.kr/2/0/938

Windows: 57. 새로 추가된 네트워크 커널 디버깅 및 PowerShell 3.0
; https://www.sysnet.pe.kr/2/0/1259

디버깅 기술: 62. windbg - 사용자 모드 원격 디버깅
; https://www.sysnet.pe.kr/2/0/1602

디버깅 기술: 192. Windbg - Hyper-V VM으로 이더넷 원격 디버깅 연결하는 방법
; https://www.sysnet.pe.kr/2/0/13343




Windbg - Hyper-V VM으로 이더넷 원격 디버깅 연결하는 방법

예전에는 COM 포트를 이용했었는데요,

Windbg - Hyper-V 윈도우 7 원격 디버깅 구성
; https://www.sysnet.pe.kr/2/0/938

이번에는 이더넷을 이용한 연결 방법을 시도해 봤습니다. 사실 이에 대해서는 아래의 공식 문서에서 잘 설명하고 있습니다.

BCDEdit /dbgsettings
; https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/bcdedit--dbgsettings

Setting Up Network Debugging of a Virtual Machine - KDNET
; https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/setting-up-network-debugging-of-a-virtual-machine-host

문서에 따라, Windbg가 설치돼 있는 호스트 측에서 다음의 경로에 있는 (같은 위치의 VerifiedNICList.xml을 사용하는) kdnet.exe로,

C:\Program Files (x86)\Windows Kits\10\Debuggers\x64

자신의 PC에 장착한 이더넷 카드가 네트워크 디버깅을 지원하는지 알아볼 수 있습니다.

C:\Program Files (x86)\Windows Kits\10\Debuggers\x64> kdnet

Network debugging is supported on the following NICs:
busparams=9.0.0, Realtek PCIe 2.5GbE Family Controller #2, Cable status unknown.

Network debugging is supported on the following USB controllers:
busparams=4.0.0, AMD USB 3.10 eXtensible Host Controller - 1.10 (Microsoft)
busparams=10.0.4, AMD USB 3.10 eXtensible Host Controller - 1.10 (Microsoft)
busparams=10.0.3, AMD USB 3.10 eXtensible Host Controller - 1.10 (Microsoft)

This Microsoft hypervisor supports using KDNET in guest VMs.

위의 결과에서는 9.0.0 버스에 연결된 Realtek 어댑터가 지원하고, 그 외에도 USB 컨트롤러도 네트워크 디버깅을 지원한다고 나옵니다.




따라서 호스트 측에서는 windbg를 실행해 두고 임의의 포트와 Key를 선정해 다음과 같이 대기해 둡니다.

hyperv_windbg_net_1.png

특이하게도 Key의 경우에는 '.'을 구분자로 (마치 IP처럼) 4개의 값을 줘야 한다고 합니다. 그래서 위의 경우 "test"라고 하지 않고 "t.e.s.t"라고 설정한 것입니다.

그다음, Debuggee 측에서는 호스트 측에서 대기하고 있는 설정대로 연결할 수 있도록 연결 정보를 전달해 kdnet.exe를 실행합니다. 이때 kdnet 실행을 위해 Windows SDK를 설치할 필요 없이 호스트 측에 있는 kdnet.exe와 VerifiedNICList.xml 파일을 복사해 실행하면 됩니다.

// 호스트 측의 IP가 192.168.100.50이라고 가정

c:\temp> kdnet 192.168.100.50 65100

Enabling network debugging on Network debugging is supported by this Microsoft Hypervisor Virtual Machine.

To debug this vm, run the following command on your debugger host machine.
windbg -k net:port=65100,key=xxxxxxx.xxxxxxx.xxxxxxx.xxxxxxx

Then restart this VM by running shutdown -r -t 0 from this command prompt.

이때 key 값이 무작위로 "xxxxxxx.xxxxxxx.xxxxxxx.xxxxxxx" 긴 문자열이 나오는데요, 이것을 다음의 명령어로 (호스트 측에 지정한 것과 동일하게) 새롭게 지정합니다.

c:\temp> bcdedit /set "{dbgsettings}" key t.e.s.t

잘 설정이 되었는지 마지막으로 확인해 주고,

c:\temp> bcdedit /dbgsettings
key                     t.e.s.t
debugtype               NET
hostip                  192.168.100.50
port                    65100
badmemoryaccess         Yes
dhcp                    Yes
The operation completed successfully.

재부팅합니다. 일단, 호스트 측에 실행해 둔 windbg는 다음의 출력과 함께 멈춰 있을 텐데요,

Microsoft (R) Windows Debugger Version 10.0.25324.1001 AMD64
Copyright (c) Microsoft Corporation. All rights reserved.

Using NET for debugging
Opened WinSock 2.0
Waiting to reconnect...

Debuggee 측이 재부팅하면 곧바로 192.168.100.50:65100으로 연결하게 되고 다음과 같은 호스트 측의 출력을 확인할 수 있습니다.

Connected to target 192.168.100.100 on port 65100 on local IP 192.168.100.50.
You can get the target MAC address by running .kdtargetmac command.
Connected to Windows 10 19041 x64 target at (Tue May  9 11:59:21.915 2023 (UTC + 9:00)), ptr64 TRUE
Kernel Debugger connection established.

...[생략]...

*******************************************************************************
*                                                                             *
*   You are seeing this message because you pressed either                    *
*       CTRL+C (if you run console kernel debugger) or,                       *
*       CTRL+BREAK (if you run GUI kernel debugger),                          *
*   on your debugger machine's keyboard.                                      *
*                                                                             *
*                   THIS IS NOT A BUG OR A SYSTEM CRASH                       *
*                                                                             *
* If you did not intend to break into the debugger, press the "g" key, then   *
* press the "Enter" key now.  This message might immediately reappear.  If it *
* does, press "g" and "Enter" again.                                          *
*                                                                             *
*******************************************************************************
nt!DbgBreakPointWithStatus:
fffff804`114051b0 cc              int     3

이때, Debuggee 측의 운영체제 자체가 멈춰버리기 때문에 호스트 측의 windbg 명령행에서 'g' 키를 입력해 실행을 계속하면 됩니다.

(참고로, 이때의 통신은 TCP가 아닌 UDP입니다.)




그런데, kdnet.exe를 Debuggee PC 측에서 실행했을 때 다음과 같은 오류가 나올 수 있습니다.

c:\temp> kdnet

Network debugging is supported by this Microsoft Hypervisor Virtual Machine

KDNET transport initialization failed during a previous boot.  Status = 0xC00000B5.
InitializeNetwork failed to get the MAC address of the host debugger. Check the dbgsettings hostip!

이런 오류가 Hyper-V VM 환경에서 발생한다면, VM 측에 할당한 네트워크 유형을 살펴야 합니다. 문서에도 나오지만, 이더넷을 이용한 디버깅은 이것을 지원하는 "물리 어댑터"의 기능을 이용하는 것이기 때문에 반드시 그 어댑터와 연결된 Virtual Switch를 VM에 할당해 줘야 합니다. 기본적으로 External 유형에만 물리 어댑터가 연결되므로 그 외의 "Internal", "Private" 유형을 VM에 할당한 경우에는 동작하지 않습니다.




경우에 따라, Internal과 External 2개의 어댑터를 연결한 VM에서도 kdnet.exe가 0xC00000B5 오류를 보고할 수 있습니다. 이럴 때 ipconfig을 실행해 보면,

c:\temp> ipconfig

Windows IP Configuration

Ethernet adapter Ethernet (Kernel Debugger):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::eab0:c8d9:e6d1:8bc0%4
   Autoconfiguration IPv4 Address. . : 169.254.169.55
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :

Ethernet adapter Ethernet 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::7fc9:901c:7365:5f2f%6
   IPv4 Address. . . . . . . . . . . : 192.168.100.100
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.100.1

"Kernel Debugger"가 "Internal" 네트워크로 설정한 어댑터로 연결되었을 것입니다. 이것을 물리 어댑터와 연결된 External 네트워크로 변경해야 하는데요, 위의 경우 "Ethernet adapter Ethernet 2"로 붙여야 합니다.

그런데, 딱히 이게 kdnet.exe 측에서는 설정하는 방법이 없습니다. 일단은, 저 규칙을 보면 우선순위가 높은 네트워크로 붙는 건데요, 따라서 다음의 글에 따라,

윈도우 - (네트워크 어댑터의 우선순위로 인한) 열거되는 IP 주소 순서를 조정하는 방법
; https://www.sysnet.pe.kr/2/0/12304

PowerShell을 이용해 InterfaceMetric을 확인하고,

PS C:\Windows\System32> Get-NetIPInterface | where {$_.ConnectionState -eq "Connected" -and $_.AddressFamily -eq "IPv4" } | select ifIndex,InterfaceAlias,InterfaceMetric,AddressFamily,ConnectionState | sort InterfaceMetric | FT

ifIndex InterfaceAlias              InterfaceMetric AddressFamily ConnectionState
------- --------------              --------------- ------------- ---------------
      6 Ethernet                                 15          IPv4       Connected
     11 Ethernet 2                               35          IPv4       Connected
      1 Loopback Pseudo-Interface 1              75          IPv4       Connected

위에서 6번 우선순위를 "Ethernet 2"보다 낮게(숫자는 크게) 설정하면 됩니다.

PS C:\Windows\System32> Set-NetIPInterface -InterfaceIndex 6 -InterfaceMetric 45

이후 ipconfig으로 우선순위가 바뀐 것을 확인하고,

c:\temp> ipconfig

Windows IP Configuration

Ethernet adapter Ethernet 2:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::5245:f089:a376:aca5%11
   IPv4 Address. . . . . . . . . . . : 192.168.100.100
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.100.1

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::7fc9:901c:7365:5f2f%6
   Autoconfiguration IPv4 Address. . : 169.254.169.55
   Subnet Mask . . . . . . . . . . . : 255.255.0.0
   Default Gateway . . . . . . . . . :

재부팅하면 정상적으로 동작하게 됩니다. 제 경우에 일단 "bcdedit /dbgsettings ..."로 해둔 설정을 삭제 및 재부팅 후 저 설정을 한 다음에 다시 "bcdedit /dbgsettings ..." 설정을 했는데요, 아마 저 상태에서 재부팅해도 잘되지 않을까... 싶습니다.




"bcdedit /dbgsettings ..."를 삭제하고 싶다면 /deletevalue 옵션을 4개의 값에 대해 일일이 지워야 합니다.

bcdedit /deletevalue {dbgsettings} key
bcdedit /deletevalue {dbgsettings} debugtype
bcdedit /deletevalue {dbgsettings} hostip 
bcdedit /deletevalue {dbgsettings} port

아니면, 그냥 LOCAL 설정으로 덮어쓰는 것도 좋겠고!

bcdedit /dbgsettings LOCAL

참고로, 본문에서는 "kdnet + bcdedit" 번에 걸쳐서 Debuggee 측의 설정을 했지만 그냥 다음과 같이 bcdedit 한 번으로 끝낼 수 있습니다.

bcdedit /dbgsettings NET HOSTIP:ip PORT:port [KEY:key]





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







[최초 등록일: ]
[최종 수정일: 5/9/2023]

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

비밀번호

댓글 작성자
 




... 31  32  33  34  [35]  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12773정성태8/11/20218319개발 환경 구성: 595. PyCharm - WSL과 연동해 Django App을 윈도우에서 리눅스 대상으로 개발
12772정성태8/11/20219750스크립트: 21. 파이썬 - 윈도우 환경에서 개발한 Django 앱을 WSL 환경의 uwsgi를 이용해 실행 [1]
12771정성태8/11/20218207Windows: 196. "Microsoft Windows Subsystem for Linux Background Host" / "Vmmem"을 종료하는 방법
12770정성태8/11/20219019.NET Framework: 1086. C# - Windows Forms 응용 프로그램의 자식 컨트롤 부하파일 다운로드1
12769정성태8/11/20216801오류 유형: 752. Python - ImportError: No module named pip._internal.cli.main 두 번째 이야기
12768정성태8/10/20217905.NET Framework: 1085. .NET 6에 포함된 신규 BCL API [1]파일 다운로드1
12767정성태8/10/20218952오류 유형: 752. Python - ImportError: No module named pip._internal.cli.main
12766정성태8/9/20217406Java: 32. closing inbound before receiving peer's close_notify
12765정성태8/9/20216754Java: 31. Cannot load JDBC driver class 'org.mysql.jdbc.Driver'
12764정성태8/9/202145229Java: 30. XML document from ServletContext resource [/WEB-INF/applicationContext.xml] is invalid
12763정성태8/9/20218262Java: 29. java.lang.NullPointerException - com.mysql.jdbc.ConnectionImpl.getServerCharset
12762정성태8/8/202111781Java: 28. IntelliJ - Unable to open debugger port 오류
12761정성태8/8/20218923Java: 27. IntelliJ - java: package javax.inject does not exist [2]
12760정성태8/8/20216264개발 환경 구성: 594. 전용 "Command Prompt for ..." 단축 아이콘 만들기
12759정성태8/8/20219531Java: 26. IntelliJ + Spring Framework + 새로운 Controller 추가 [2]파일 다운로드1
12758정성태8/7/20218853오류 유형: 751. Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
12757정성태8/7/20219555Java: 25. IntelliJ + Spring Framework 프로젝트 생성
12756정성태8/6/20218284.NET Framework: 1084. C# - .NET Core Web API 단위 테스트 방법 [1]파일 다운로드1
12755정성태8/5/20217501개발 환경 구성: 593. MSTest - 단위 테스트에 static/instance 유형의 private 멤버 접근 방법파일 다운로드1
12754정성태8/5/20218367오류 유형: 750. manage.py - Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
12753정성태8/5/20218615오류 유형: 749. PyCharm - Error: Django is not importable in this environment
12752정성태8/4/20216696개발 환경 구성: 592. JetBrains의 IDE(예를 들어, PyCharm)에서 Visual Studio 키보드 매핑 적용
12751정성태8/4/20219792개발 환경 구성: 591. Windows 10 WSL2 환경에서 docker-compose 빌드하는 방법
12750정성태8/3/20216553디버깅 기술: 181. windbg - 콜 스택의 "Call Site" 오프셋 값이 가리키는 위치
12749정성태8/2/20215951개발 환경 구성: 590. Visual Studio 2017부터 단위 테스트에 DataRow 특성 지원
12748정성태8/2/20216605개발 환경 구성: 589. Azure Active Directory - tenant의 관리자(admin) 계정 로그인 방법
... 31  32  33  34  [35]  36  37  38  39  40  41  42  43  44  45  ...