Windbg - 커널 모드 디버깅 상태에서 사용자 프로그램을 디버깅하는 방법
Hyper-V에 띄운 VM(이름: win10en)이 있다고 가정했을 때, 다음과 같이 COM 포트를 pipe로 연결하고,
[Debugger 호스트 측에서 실행]
// Generation 2 VM 유형인 경우, 설정 창이 없으므로 명령행에서 설정
PS C:\temp> Set-VMComPort -VMName win10en -Path \\.\pipe\com1 -Number 1
WinDbg를 실행해 커널 디버깅 모드로 연결 대기를 해둡니다.
[Debugger 호스트 측에서 실행]
c:\temp> "C:\Program Files (x86)\Windows Kits\10\Debuggers\x64\windbg" -Q -k "com:port=\\.\pipe\com1,baud=115200,pipe,reconnect"
그다음, VM 측에서는 호스트 측에서 실행해 둔 WinDbg에 연결하기 위한 설정을 합니다.
[Debuggee VM 측에서 실행]
c:\temp> bcdedit /debug on
// COM 1 포트로 연결한 경우
c:\temp> bcdedit /dbgsettings SERIAL DEBUGPORT:1 BAUDRATE:115200
// 이후, 재시작 (재부팅 시에 자동으로 호스트 측 Windbg에 연결됨)
c:\temp> shutdown -r -t 0
// 디버그 원격 연결 해제하려는 경우,
// bcdedit /dbgsettings LOCAL
저런 식으로 커널 디버깅을 하게 되었을 때 사용자의 응용 프로그램 문맥으로 들어가,
// ConsoleApplication1.exe 사용자 프로그램 문맥으로 변경
7: kd> !process 0 0 ConsoleApplication1.exe
PROCESS ffffde87aabe1080
SessionId: 1 Cid: 04bc Peb: 26efd4d000 ParentCid: 0b70
DirBase: 1ab333000 ObjectTable: ffff900e9e55bac0 HandleCount: 46.
Image: ConsoleApplication1.exe
4: kd> .process /i ffffde87aabe1080
You need to continue execution (press 'g' <enter>) for the context
to be switched. When the debugger breaks in again, you will be in
the new process context.
4: kd> g
Break instruction exception - code 80000003 (first chance)
nt!DbgBreakPointWithStatus:
fffff806`2a81f130 cc int 3
BP를 걸려고 해도 풀이가 안 된다는 메시지와 함께, 실제로 BP가 걸리지 않습니다.
4: kd> bp ConsoleApplication1!FuncInThread1
Bp expression 'ConsoleApplication1!FuncInThread1' could not be resolved, adding deferred bp
4: kd> g
// BP에 해당하는 코드가 실행돼도 멈추지 않음
이런 경우, 모듈 목록을 조사해 보면 사용자 측 모듈이 아닌, 커널 측 모듈이 나오는 것을 확인할 수 있습니다.
4: kd> lm o
start end module name
fffff806`83400000 fffff806`84447000 nt (pdb symbols) e:\symbols\ntkrnlmp.pdb\F2F2E6BC961003D3115AD38C4A9FB42E1\ntkrnlmp.pdb
fffff806`a9060000 fffff806`a907d000 WdNisDrv (deferred)
fffff806`a9510000 fffff806`a9537000 WinSetupMon (deferred)
fffff806`a9780000 fffff806`a978f000 wimmount (deferred)
fffff806`a98e0000 fffff806`a98f0000 terminpt (deferred)
따라서, 이게 가능하려면 응용 프로그램 측 모듈의 심벌 파일을 명시적으로 로드해야 합니다.
// .reload (Reload Module)
// https://learn.microsoft.com/en-us/windows-hardware/drivers/debuggercmds/-reload--reload-module-#parameters
4: kd> .reload /user
이후, 다시 BP를 걸어보면 이름 풀이가 되고 BP에서의 실행 역시 잘 멈추게 됩니다.
[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]