1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
/*
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "base/debugging.h"
#if defined(_WIN32) && defined(_DEBUG)
#include <windows.h>
#include <stdexcept>
//--------------------------------------------------------------------------------------------------
DataBreakpoint::DataBreakpoint()
{
_register_index = -1;
}
//--------------------------------------------------------------------------------------------------
DataBreakpoint::~DataBreakpoint()
{
Clear();
}
//--------------------------------------------------------------------------------------------------
/**
* Used to set the given value into target.
*
* @param target The target value to modify.
* @param offset Bit offset in the target, counted from LSB.
* @param bits Number of bits to set with value.
* @value The new value to set.
*/
void DataBreakpoint::SetBits(REGISTER_TYPE& target, REGISTER_TYPE offset, REGISTER_TYPE bits, REGISTER_TYPE value)
{
REGISTER_TYPE mask = (1 << bits) - 1;
target = (target & ~(mask << offset)) | (value << offset);
}
//--------------------------------------------------------------------------------------------------
/**
* Sets a data watch for the given address.
*
* @address The location to watch.
* @size The size of the memory block address points to. Can be 1, 2 or 4 bytes.
* @when Determines when to trigger the break point (write or read/write).
*/
void DataBreakpoint::Set(void* address, int size, Condition when)
{
if (_register_index != -1)
throw std::runtime_error("Watch point already set. Use clear() before setting a new one.");
switch (size)
{
case 1:
size = 0;
break;
case 2:
size = 1;
break;
case 4:
size = 3;
break;
default:
throw std::runtime_error("Invalid data length for watch point specified.");
}
HANDLE currentThread = GetCurrentThread();
// Set up the thread context for this watch point.
CONTEXT context;
context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
// Read the register values
if (!GetThreadContext(currentThread, &context))
throw std::runtime_error("Failed getting the current thread context.");
// Find an available hardware register.
for (_register_index = 0; _register_index < 4; ++_register_index)
{
if ((context.Dr7 & (REGISTER_TYPE)(1 << (2 * _register_index))) == 0)
break;
}
if (_register_index >= 4)
throw std::runtime_error("Could not find a free hardware register.");
switch (_register_index)
{
case 0:
context.Dr0 = (REGISTER_TYPE)address;
break;
case 1:
context.Dr1 = (REGISTER_TYPE)address;
break;
case 2:
context.Dr2 = (REGISTER_TYPE)address;
break;
case 3:
context.Dr3 = (REGISTER_TYPE)address;
break;
}
SetBits(context.Dr7, 16 + (4 * _register_index), 2, when);
SetBits(context.Dr7, 18 + (4 * _register_index), 2, size);
SetBits(context.Dr7, 2 * _register_index, 1, 1);
if (!SetThreadContext(currentThread, &context))
throw std::runtime_error("Could not set modified thread context for the watch point.");
}
//--------------------------------------------------------------------------------------------------
/**
* Removes a currently set watch point if one is set.
*/
void DataBreakpoint::Clear()
{
if (_register_index != -1)
{
CONTEXT context;
HANDLE currentThread = GetCurrentThread();
context.ContextFlags = CONTEXT_DEBUG_REGISTERS;
if (!GetThreadContext(currentThread, &context))
throw std::runtime_error("Failed getting the current thread context.");
// Reset debug register settings for this watch point.
if (_register_index < 0 && _register_index >= 4)
throw std::runtime_error("Internal error: debug register index is wrong.");
SetBits(context.Dr7, 2 * _register_index, 1, 0);
if (!SetThreadContext(currentThread, &context))
throw std::runtime_error("Could not set modified thread context for the watch point.");
_register_index = -1;
}
}
//--------------------------------------------------------------------------------------------------
#endif // _DEBUG
|