/*
Copyright (C) 2006 - 2009 Evan Teran
                   eteran@alum.rit.edu

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; either version 2 of the License, or
(at your option) any later version.

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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "X86Breakpoint.h"
#include "DebuggerCoreInterface.h"
#include "Debugger.h"

static const quint8 BreakpointInstruction[X86Breakpoint::size] = {0xcc};

//------------------------------------------------------------------------------
// Name: Breakpoint(edb::address_t address, bool onetime)
// Desc: constructor
//------------------------------------------------------------------------------
X86Breakpoint::X86Breakpoint(edb::address_t address, bool onetime) : m_Address(address), m_HitCount(0), m_Enabled(false), m_OneTime(onetime), m_Internal(false) {
	enable();
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
X86Breakpoint::~X86Breakpoint() {
	disable();
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
bool X86Breakpoint::enable() {
	if(!enabled()) {
		char prev[size];
		if(edb::v1::debuggerBase->readBytes(address(), prev, size)) {
			if(edb::v1::debuggerBase->writeBytes(address(), BreakpointInstruction, size)) {
				m_OriginalBytes = QByteArray(prev, size);
				m_Enabled = true;
				return true;
			}
		}
	}
	return false;
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
bool X86Breakpoint::disable() {
	if(enabled()) {
		if(edb::v1::debuggerBase->writeBytes(address(), m_OriginalBytes, size)) {
			m_Enabled = false;
			return true;
		}
	}
	return false;
}
