# Path: dbginterface.py
# def LocalKernelDebugger(quiet=True):
#     """| Check that all conditions to Local Kernel Debugging are met
#        | and return a LKD (subclass of :class:`LocalKernelDebuggerBase`
#     """
#     if not windows.utils.check_debug():
#         raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging on kernel not in DEBUG mode")
#     if not windows.utils.check_is_elevated():
#         raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging from non-Admin process")
#     windows.utils.enable_privilege(SE_DEBUG_NAME, True)
#     if windows.system.bitness == 64:
#         if windows.current_process.is_wow_64:
#             raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging from SysWow64 process (please launch from 64bits python)")
#         return LocalKernelDebugger64(quiet)
#     return LocalKernelDebugger32(quiet)


import ctypes
import sys
import os
import time
from windows.generated_def.winstructs import *
from dbginterface import LocalKernelDebugger
        elif pebs_record_v == 2:
            self.PEBSRecord = PEBSRecordV2
        else:
            raise ValueError("Don't know the format of PEBS Records of version {0}".format(pebs_record_v))

    def setup_perfevtsel0(self, enable, mask=0, eventselect=0xc0, user_mod=1, os_mod=1):
        """Setup the PERFEVTSE0 MSR to manage the IA32_PMC0 perf counter
        Default eventselect 0xc0 is 'Instruction retired'"""
        # Instruction retired
        MASK = mask << 8
        EVENTSELECT = eventselect << 0
        USER_MOD = user_mod << 16
        OS_MOD = os_mod << 17
        ENABLE = enable << 22
        value = MASK | EVENTSELECT | USER_MOD | OS_MOD | ENABLE
        self.kdbg.write_msr(PERFEVTSE0, value)

    def get_pebs_records_version(self):
        return (self.kdbg.read_msr(PERF_CAPABILITIES) >> 8) & 0xf

    def setup_DsManagementArea(self, proc_nb):
        """Setup the setup_DsManagementArea for the proc `proc_nb`
           proc_nb must be the number of the current processor
           
           The write to nt!VfBTSDataManagementArea is where ntokrnl store this information"""
        ds_addr, ds_content = self.get_DsManagementArea(proc_nb)
        if ds_addr:
            return
        ds_management_area_addr = self.kdbg.alloc_memory(0x1000)
        self.kdbg.write_virtual_memory(ds_management_area_addr, "\x00" * 0x1000)

# Path: dbginterface.py
# def LocalKernelDebugger(quiet=True):
#     """| Check that all conditions to Local Kernel Debugging are met
#        | and return a LKD (subclass of :class:`LocalKernelDebuggerBase`
#     """
#     if not windows.utils.check_debug():
#         raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging on kernel not in DEBUG mode")
#     if not windows.utils.check_is_elevated():
#         raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging from non-Admin process")
#     windows.utils.enable_privilege(SE_DEBUG_NAME, True)
#     if windows.system.bitness == 64:
#         if windows.current_process.is_wow_64:
#             raise LocalKernelDebuggerError("Cannot perform LocalKernelDebugging from SysWow64 process (please launch from 64bits python)")
#         return LocalKernelDebugger64(quiet)
#     return LocalKernelDebugger32(quiet)


# Path: example/PEBS_BTS_demo.py
import ctypes
import sys
import os
import time
from windows.generated_def.winstructs import *
from dbginterface import LocalKernelDebugger

        elif pebs_record_v == 2:
            self.PEBSRecord = PEBSRecordV2
        else:
            raise ValueError("Don't know the format of PEBS Records of version {0}".format(pebs_record_v))

    def setup_perfevtsel0(self, enable, mask=0, eventselect=0xc0, user_mod=1, os_mod=1):
        """Setup the PERFEVTSE0 MSR to manage the IA32_PMC0 perf counter
        Default eventselect 0xc0 is 'Instruction retired'"""
        # Instruction retired
        MASK = mask << 8
        EVENTSELECT = eventselect << 0
        USER_MOD = user_mod << 16
        OS_MOD = os_mod << 17
        ENABLE = enable << 22
        value = MASK | EVENTSELECT | USER_MOD | OS_MOD | ENABLE
        self.kdbg.write_msr(PERFEVTSE0, value)

    def get_pebs_records_version(self):
        return (self.kdbg.read_msr(PERF_CAPABILITIES) >> 8) & 0xf

    def setup_DsManagementArea(self, proc_nb):
        """Setup the setup_DsManagementArea for the proc `proc_nb`
           proc_nb must be the number of the current processor
           
           The write to nt!VfBTSDataManagementArea is where ntokrnl store this information"""
        ds_addr, ds_content = self.get_DsManagementArea(proc_nb)
        if ds_addr:
            return
        ds_management_area_addr = self.kdbg.alloc_memory(0x1000)
        self.kdbg.write_virtual_memory(ds_management_area_addr, "\x00" * 0x1000)

        VfBTSDataManagementArea = kdbg.get_symbol_offset("nt!VfBTSDataManagementArea")