Microsoft MVP성태의 닷넷 이야기
디버깅 기술: 46. Windbg 확장 DLL 만들기 (2) - Debugger Extension API 사용 [링크 복사], [링크+제목 복사],
조회: 20190
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 3개 있습니다.)
디버깅 기술: 45. Windbg 확장 DLL 만들기 (1) - 스레드를 강제 종료시키는 명령어
; https://www.sysnet.pe.kr/2/0/1198

디버깅 기술: 46. Windbg 확장 DLL 만들기 (2) - Debugger Extension API 사용
; https://www.sysnet.pe.kr/2/0/1200

디버깅 기술: 160. Windbg 확장 DLL 만들기 (3) - C#으로 만드는 방법
; https://www.sysnet.pe.kr/2/0/12119




Windbg 확장 DLL 만들기 (2) - Debugger Extension API 사용

지난번에 만들어진 Windbg 확장 DLL에서,

Windbg 확장 DLL 만들기 (1) - 스레드를 강제 종료시키는 명령어
; https://www.sysnet.pe.kr/2/0/1198

아쉬움이 있다면, 스레드 ID 인자를 10 진수로 "kt" 명령어에 전달해야 한다는 점입니다. 왜냐하면 atoi 함수를 사용했기 때문인데요, 물론 이 부분을 16진수 문자열을 받는 사용자 정의 함수로 고쳐도 상관은 없습니다. 이 글의 주제는 atoi 함수 개선은 아니기 때문에 그 부분은 넘어가고!

Windbg는 자체적으로 다양한 기능을 갖는 Helper 함수를 2가지 방법으로 제공해 줍니다. 하나는 예전 방식으로 wdbgexts.h 헤더 파일에 선언된 WINDBG_EXTENSION_APIS 구조체를 이용하는 것인데 현재 'deprecated' 된 상태입니다. 권장되는 다른 방식이 바로 dbgeng.h에 선언된 DbgEng API를 사용하는 것으로 이번 글에서는 후자의 방법만을 이야기할 것입니다.

지난번 소스 코드를 잠시 살펴보면, 모든 Windbg 확장 명령어는 다음과 같이 PDEBUG_CLIENT (IDebugClient *) 값을 인자로 받는 것을 볼 수 있습니다.

extern "C" HRESULT CALLBACK kt(PDEBUG_CLIENT pDebugClient, PCSTR args)
{
    ULONG64 threadId;
    HANDLE result;
    DWORD errcode =0;

    threadId = atoi(args);

    ... [생략] ...
}

extern "C" HRESULT CALLBACK help(PDEBUG_CLIENT pDebugClient, PCSTR args)
{
    dprintf("usage kt ThreadId Will kill The Thread");
    return S_OK;
}


IDebugClient 인터페이스 자체로도 많은 기능들을 가지고 있지만, 해당 인터페이스로부터 구할 수 있는 IDebugControl(IDebugControl2, IDebugControl3) 인터페이스가 예전의 WINDBG_EXTENSION_APIS 구조체에서 가지고 있던 것과 유사한 기능들을 (확장)제공하고 있습니다.

IDebugControl *pControl;
if (pDebugClient->QueryInterface(__uuidof(IDebugControl), (PVOID *)&pControl) != S_OK)
{
    return S_OK;
}

/*
참고로, QueryInterface(IID_IDebugControl, ...)로 명령을 내리면 다음과 같은 링크 에러가 발생합니다.

error LNK2001: unresolved external symbol _IID_IDebugControl
*/

IDebugControl 인터페이스에도 많은 기능들이 담겨 있지만, 모두 살펴 볼 여력은 없고 여기서 필요한 것만 사용해 볼 텐데요. 우선, (본문에서는 16진수 값으로 받아들일) 문자열 인자를 파싱해 주는 메서드를 이용해서 atoi 함수를 개선할 수 있습니다.

extern "C" HRESULT CALLBACK kt(PDEBUG_CLIENT pDebugClient, PCSTR args)
{
    ULONG64 threadId;
    HANDLE result;
    DWORD errcode = 0;

    IDebugControl *pDebugControl = ... [생략] ...;

    // threadId = atoi(args);

    DEBUG_VALUE debugValue;
    ULONG remainderIndex;
    HRESULT hr = pDebugControl->Evaluate(args, DEBUG_VALUE_INT64, &debugValue, &remainderIndex);
    if (hr == S_OK)
    {
        threadId = debugValue.I64;

        ... [생략] ...
    }
}

그다음으로 개선할 점이 있다면, DbgView로 텍스트를 확인해야만 했던 OutputDebugString의 사용입니다. 기왕이면, windbg 화면에 출력되는 것이 더욱 편리할 수 있기 때문에 dprintf 함수를 다음과 같이 개선해 줄 수 있습니다.

#define BUFFER_LENGTH 4096

void dprintf(IDebugControl *pDebugControl, wchar_t *fmt, ...)
 {
     wchar_t wchBuf[BUFFER_LENGTH];

     va_list ap;
     va_start(ap, fmt);

     int writtenLength = vswprintf(wchBuf, fmt, ap);
     va_end(ap);

     int chSize = BUFFER_LENGTH * 2;
     char *pOutputBuffer = new char[chSize];
     wcstombs(pOutputBuffer, wchBuf, writtenLength + 2);

     pDebugControl->Output(DEBUG_OUTPUT_NORMAL, pOutputBuffer);

     delete [] pOutputBuffer;
}

아쉽게도 IDebugControl::Output 메서드가 wchar_t 텍스트를 출력해주지 않고 char 형으로 인자를 받고 있어서 부득이 변환을 했습니다. (물론, 전체 프로젝트의 소스 코드를 char 형으로 변환하는 것도 가능하겠지만... 제가 병(?)적으로 wchar_t 형을 좋아하기 때문에. ^^)

아래는 지난번 예제를 최종적으로 변환한 소스 코드입니다.

#include "stdafx.h"
#include <cstdio>

extern "C" HRESULT CALLBACK DebugExtensionInitialize(PULONG Version, PULONG Flags)
{
    *Version = DEBUG_EXTENSION_VERSION(EXT_MAJOR_VER, EXT_MINOR_VER);
    *Flags = 0;  // Reserved for future use.
    return S_OK;
}

extern "C" void CALLBACK DebugExtensionNotify(ULONG Notify, ULONG64 Argument)
{
    UNREFERENCED_PARAMETER(Argument);
    switch (Notify)
    {
        case DEBUG_NOTIFY_SESSION_ACTIVE:
            break;
        case DEBUG_NOTIFY_SESSION_INACTIVE:
            break;
        case DEBUG_NOTIFY_SESSION_ACCESSIBLE:
            break;
        case DEBUG_NOTIFY_SESSION_INACCESSIBLE:
            break;
    }
    return;
}

extern "C" void CALLBACK DebugExtensionUninitialize(void)
{
    return;
}

#define BUFFER_LENGTH 4096

void dprintf(IDebugControl *pDebugControl, wchar_t *fmt, ...)
 {
     wchar_t wchBuf[BUFFER_LENGTH];

     va_list ap;
     va_start(ap, fmt);

     int writtenLength = vswprintf(wchBuf, fmt, ap);
     va_end(ap);

     int chSize = BUFFER_LENGTH * 2;
     char *pOutputBuffer = new char[chSize];
     wcstombs(pOutputBuffer, wchBuf, writtenLength + 2);

     pDebugControl->Output(DEBUG_OUTPUT_NORMAL, pOutputBuffer);

     delete [] pOutputBuffer;
}
 
// #include <C:\Program Files (x86)\Debugging Tools for Windows (x86)\sdk\inc\wdbgexts.h>

extern "C" HRESULT CALLBACK kt(PDEBUG_CLIENT pDebugClient, PCSTR args)
{
    ULONG64 threadId;
    HANDLE result;
    DWORD errcode = 0;

    IDebugControl *pDebugControl = NULL;
    if (pDebugClient->QueryInterface(__uuidof(IDebugControl), (PVOID *)&pDebugControl) != S_OK)
    {
        return S_OK;
    }

    do
    {
        // threadId = atoi(args);

        DEBUG_VALUE debugValue;
        ULONG remainderIndex;
        HRESULT hr = pDebugControl->Evaluate(args, DEBUG_VALUE_INT64, &debugValue, &remainderIndex);
        if (hr != S_OK)
        {
            break;
        }

        threadId = debugValue.I64;

        dprintf(pDebugControl, L"the thread to be killed has an ID of %x\n", threadId);
        result = OpenThread(THREAD_ALL_ACCESS, FALSE, (DWORD)threadId);
        if (result == 0)
        {
            errcode = GetLastError();
            dprintf(pDebugControl, L"gle = %x\n",errcode);
        }
        else
        {
            dprintf(pDebugControl, L"HANDLE to thread is %x\n",result);
            if(TerminateThread(result,DBG_TERMINATE_THREAD) == 0)
            {
                dprintf(pDebugControl, L"Terminate Thread Failed");
            }
            else
            {
                dprintf(pDebugControl, L"issue a g then ~* and you will see a thread has been killed\n"
                        L"if it was main thread the process would have gone\n"
                        L"pl read msdn for all the DANGEROUS FUNCTION caveats against using TerminateThread Function\n"
                        L"also read the need to define minimum platform _winnt_winxp???\n"
                        L"also iirc using win32apis in debugger extensions is not recommended you need to use idebug::whatever::went::somewhere::interfaces\n"
                        L"have fun terminating the process thread by thread\n");
            }
        }
    } while (false);

    if (pDebugControl != NULL)
    {
        pDebugControl->Release();
        pDebugControl = NULL;
    }

    return S_OK;
}

extern "C" HRESULT CALLBACK help(PDEBUG_CLIENT pDebugClient, PCSTR args)
{
    ULONG64 threadId;
    HANDLE result;
    DWORD errcode = 0;

    IDebugControl *pDebugControl = NULL;
    if (pDebugClient->QueryInterface(__uuidof(IDebugControl), (PVOID *)&pDebugControl) != S_OK)
    {
        return S_OK;
    }

    do
    {
        dprintf(pDebugControl, L"usage kt ThreadId Will kill The Thread");
    } while (false);

    if (pDebugControl != NULL)
    {
        pDebugControl->Release();
        pDebugControl = NULL;
    }

    return S_OK;
}

개선된 ThreadKill 확장 DLL로 다시 한번 테스트 해볼까요? ^^

0:004> .load D:\...[생략]...\Debug\ThreadKill.dll

0:004> ~*
   0  Id: 2728.b6c Suspend: 1 Teb: 7efdd000 Unfrozen
      Start: *** WARNING: Unable to verify checksum for D:\...[생략]...\Debug\ConsoleApplication1.exe
ConsoleApplication1!COM+_Entry_Point <PERF> (ConsoleApplication1+0x27be) (000927be) 
      Priority: 0  Priority class: 32  Affinity: ff
   1  Id: 2728.24fc Suspend: 1 Teb: 7efda000 Unfrozen
      Start: *** ERROR: Symbol file could not be found.  Defaulted to export symbols for C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll - 
clr!CreateApplicationContext+0xcab9 (7261b30c) 
      Priority: 0  Priority class: 32  Affinity: ff
   2  Id: 2728.2bc0 Suspend: 1 Teb: 7efd7000 Unfrozen
      Start: clr!GetMetaDataInternalInterfaceFromPublic+0x1e505 (726fc018) 
      Priority: 2  Priority class: 32  Affinity: ff
   3  Id: 2728.1dfc Suspend: 1 Teb: 7efaf000 Unfrozen
      Start: ntdll!RtlLoadString+0x430 (77a541f3) 
      Priority: 0  Priority class: 32  Affinity: ff
.  4  Id: 2728.17dc Suspend: 1 Teb: 7efac000 Unfrozen
      Start: ntdll!DbgUiRemoteBreakin (77aaf7ea) 
      Priority: 0  Priority class: 32  Affinity: ff

0:004> !kt 17dc
the thread to be killed has an ID of 17dc
HANDLE to thread is 258
issue a g then ~* and you will see a thread has been killed
if it was main thread the process would have gone
pl read msdn for all the DANGEROUS FUNCTION caveats against using TerminateThread Function
also read the need to define minimum platform _winnt_winxp???
also iirc using win32apis in debugger extensions is not recommended you need to use idebug::whatever::went::somewhere::interfaces
have fun terminating the process thread by thread

우와~~~ 완벽하군요. ^^

(첨부된 파일은 위의 코드를 포함한 예제 프로젝트와 DLL 결과물입니다.)

마지막으로, 확장 DLL에 대해 각각 x86/x64용으로 빌드해주는 스크립트를 만드는 것을 잊지 말아야겠죠. ^^

msbuild ".\ThreadKill.vcxproj" /p:Platform=Win32;Configuration=Release /p:TargetName=ThreadKill
msbuild ".\ThreadKill.vcxproj" /p:Platform=x64;Configuration=Release /p:TargetName=ThreadKill64




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







[최초 등록일: ]
[최종 수정일: 7/10/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)
13257정성태2/13/20234374.NET Framework: 2094. C# - Job에 Process 포함하는 방법 [1]파일 다운로드1
13256정성태2/10/20235218개발 환경 구성: 665. WSL 2의 네트워크 통신 방법 - 두 번째 이야기
13255정성태2/10/20234551오류 유형: 845. gihub - windows2022 이미지에서 .NET Framework 4.5.2 미만의 프로젝트에 대한 빌드 오류
13254정성태2/10/20234444Windows: 223. (WMI 쿼리를 위한) PowerShell 문자열 escape 처리
13253정성태2/9/20235219Windows: 222. C# - 다른 윈도우 프로그램이 실행되었음을 인식하는 방법파일 다운로드1
13252정성태2/9/20234044오류 유형: 844. ssh로 명령어 수행 시 멈춤 현상
13251정성태2/8/20234480스크립트: 44. 파이썬의 3가지 스레드 ID
13250정성태2/8/20236308오류 유형: 843. System.InvalidOperationException - Unable to configure HTTPS endpoint
13249정성태2/7/20235155오류 유형: 842. 리눅스 - You must wait longer to change your password
13248정성태2/7/20234185오류 유형: 841. 리눅스 - [사용자 계정] is not in the sudoers file. This incident will be reported.
13247정성태2/7/20235083VS.NET IDE: 180. Visual Studio - 닷넷 소스 코드 디버깅 중 "Decompile source code"가 동작하는 않는 문제
13246정성태2/6/20234223개발 환경 구성: 664. Hyper-V에 설치한 리눅스 VM의 VHD 크기 늘리는 방법 - 두 번째 이야기
13245정성태2/6/20234792.NET Framework: 2093. C# - PEM 파일을 이용한 RSA 개인키/공개키 설정 방법파일 다운로드1
13244정성태2/5/20234155VS.NET IDE: 179. Visual Studio - External Tools에 Shell 내장 명령어 등록
13243정성태2/5/20235013디버깅 기술: 190. windbg - Win32 API 호출 시점에 BP 거는 방법 [1]
13242정성태2/4/20234445디버깅 기술: 189. ASP.NET Web Application (.NET Framework) 프로젝트의 숨겨진 예외 - System.UnauthorizedAccessException
13241정성태2/3/20233930디버깅 기술: 188. ASP.NET Web Application (.NET Framework) 프로젝트의 숨겨진 예외 - System.IO.FileNotFoundException
13240정성태2/1/20234083디버깅 기술: 187. ASP.NET Web Application (.NET Framework) 프로젝트의 숨겨진 예외 - System.Web.HttpException
13239정성태2/1/20233748디버깅 기술: 186. C# - CacheDependency의 숨겨진 예외 - System.Web.HttpException
13238정성태1/31/20235836.NET Framework: 2092. IIS 웹 사이트를 TLS 1.2 또는 TLS 1.3 프로토콜로만 운영하는 방법
13237정성태1/30/20235502.NET Framework: 2091. C# - 웹 사이트가 어떤 버전의 TLS/SSL을 지원하는지 확인하는 방법
13236정성태1/29/20235109개발 환경 구성: 663. openssl을 이용해 인트라넷 IIS 사이트의 SSL 인증서 생성
13235정성태1/29/20234663개발 환경 구성: 662. openssl - 윈도우 환경의 명령행에서 SAN 적용하는 방법
13234정성태1/28/20235744개발 환경 구성: 661. dnSpy를 이용해 소스 코드가 없는 .NET 어셈블리의 코드를 변경하는 방법 [1]
13233정성태1/28/20237095오류 유형: 840. C# - WebClient로 https 호출 시 "The request was aborted: Could not create SSL/TLS secure channel" 예외 발생
13232정성태1/27/20234860스크립트: 43. uwsgi의 --processes와 --threads 옵션
1  2  3  4  5  6  7  8  9  10  11  12  13  14  [15]  ...