* Copyright (c) 2017 Simorfo, Inc. All rights reserved.
* **********************************************************/
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Simorfo nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL VMWARE, INC. OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#ifndef ASM_CODE_ONLY
# include "tools.h"
# include <windows.h>
static int count = 0;
void
set_debug_register();
void
test_debug_register();
void
single_step_addr0(void);
void
single_step_addr1(void);
static LONG
our_top_handler(struct _EXCEPTION_POINTERS *pExceptionInfo)
{
if (pExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT) {
pExceptionInfo->ContextRecord->Dr0 = (unsigned int)single_step_addr0;
pExceptionInfo->ContextRecord->Dr1 = (unsigned int)single_step_addr1;
pExceptionInfo->ContextRecord->Dr6 = 0xfffe0ff0;
pExceptionInfo->ContextRecord->Dr7 = 0x00000505;
print("set debug register\n");
pExceptionInfo->ContextRecord->CXT_XIP++;
return EXCEPTION_CONTINUE_EXECUTION;
} else if (pExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) {
print("single step seen eax = %x\n", pExceptionInfo->ContextRecord->Eax);
if (pExceptionInfo->ExceptionRecord->ExceptionAddress == single_step_addr0 ||
pExceptionInfo->ExceptionRecord->ExceptionAddress == single_step_addr1) {
count++;
pExceptionInfo->ContextRecord->CXT_XIP++;
} else {
print("got address " PFX ", expected " PFX " or " PFX "\n",
pExceptionInfo->ExceptionRecord->ExceptionAddress, single_step_addr0,
single_step_addr1);
}
if (count == 2) {
pExceptionInfo->ContextRecord->Dr0 = 0;
pExceptionInfo->ContextRecord->Dr6 = 0;
pExceptionInfo->ContextRecord->Dr7 = 0;
}
return EXCEPTION_CONTINUE_EXECUTION;
}
return EXCEPTION_EXECUTE_HANDLER;
}
int
main(void)
{
INIT();
SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)our_top_handler);
print("start of test, count = %d\n", count);
set_debug_register();
test_debug_register();
print("end of test, count = %d\n", count);
return 0;
}
#else
# include "asm_defines.asm"
START_FILE
* Generates one int 3 interruption and returns.
*/
#define FUNCNAME set_debug_register
DECLARE_FUNC(FUNCNAME)
GLOBAL_LABEL(FUNCNAME:)
int 3
nop
nop
ret
END_FUNC(FUNCNAME)
* Some amount of dummy code where to put a breakpoint
*/
#define FUNCNAME test_debug_register
DECLARE_FUNC(FUNCNAME)
GLOBAL_LABEL(FUNCNAME:)
xor eax, eax
mov eax, 1
DECLARE_GLOBAL(single_step_addr0)
ADDRTAKEN_LABEL(single_step_addr0:)
inc eax
nop
inc eax
jmp eaxEquals2
ret
eaxEquals2:
DECLARE_GLOBAL(single_step_addr1)
ADDRTAKEN_LABEL(single_step_addr1:)
inc eax
nop
ret
END_FUNC(FUNCNAME)
END_FILE
#endif