/*
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 "MemRegion.h"
#include "Types.h"
#include "Util.h"
#include "State.h"
#include "SmartPointer.h"
#include "DebuggerCoreInterface.h"
#include "DebugEventHandlerInterface.h"
#include "MemoryRegions.h"
#include "Debugger.h"

#include <QtGlobal>
#include <QMessageBox>
#include <QApplication>
#include <cstring>

#ifdef Q_OS_FREEBSD
#include <sys/syscall.h>
#endif

#include <sys/mman.h>

template <size_t N>
class BackupInfo : public DebugEventHandlerInterface {
public:
	BackupInfo(edb::address_t address, quint8 perms);
	
private:
	Q_DISABLE_COPY(BackupInfo)
	
public:
	quint8 perms() const	{ return m_Perms; }
	bool locked() const		{ return m_Lock != 0; }
	
public:
	bool backup();
	bool restore();
	
public:
	virtual void handleEvent(const DebugEvent &event);

private:
	int						m_Lock;
	edb::address_t			m_Address;
	quint8					m_Perms;
	State					m_State;
	quint8					m_Buffer[N];
	
public:
	DebugEventHandlerInterface	*eventhandler;
};

//------------------------------------------------------------------------------
// Name: BackupInfo(td::size_t size, edb::address_t address, quint8 perms)
// Desc: 
//------------------------------------------------------------------------------
template <size_t N>
BackupInfo<N>::BackupInfo(edb::address_t address, quint8 perms) :
		m_Lock(1), m_Address(address), m_Perms(perms), eventhandler(0) {
}

//------------------------------------------------------------------------------
// Name: backup()
// Desc: 
//------------------------------------------------------------------------------
template <size_t N>
bool BackupInfo<N>::backup() {
	edb::v1::debuggerBase->getState(m_State);
	return edb::v1::debuggerBase->readBytes(m_Address, m_Buffer, N);
}

//------------------------------------------------------------------------------
// Name: restore()
// Desc: 
//------------------------------------------------------------------------------
template <size_t N>
bool BackupInfo<N>::restore() {
	edb::v1::debuggerBase->setState(m_State);
	return edb::v1::debuggerBase->writeBytes(m_Address, m_Buffer, N);
}

//------------------------------------------------------------------------------
// Name: handleEvent(const DebugEvent &event)
// Desc: 
//------------------------------------------------------------------------------
template <size_t N>
void BackupInfo<N>::handleEvent(const DebugEvent &event) {
	Q_UNUSED(event);
	
	// TODO: check that the event was caused by a hlt in our shellcode	
	m_Lock = 0;
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
bool MemRegion::accessible() const {
	return readable() || writable() || executable();
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
bool MemRegion::readable() const {
	return (m_Permissions & PROT_READ) != 0;
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
bool MemRegion::writable() const {
	return (m_Permissions & PROT_WRITE) != 0;
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
bool MemRegion::executable() const {
	return (m_Permissions & PROT_EXEC) != 0;
}

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
int MemRegion::size() const {
	return end - start;
}

//------------------------------------------------------------------------------
// Name: setPermissions(bool read, bool write, bool execute)
// Desc: attempts to change the permissions of a given region by injecting
//       shellcode into the debugged process, and executing a mprotect syscall
//       we probably need to ensure that the debug event server thread is not
//       waiting for an event at this moment because we need to catch the 'hlt'
//       instruction we injected in order to restore the original code
//       we also probably need this code to work perfectly in order to implement
//       memory access breakpoints
//------------------------------------------------------------------------------
void MemRegion::setPermissions(bool read, bool write, bool execute, edb::address_t tempAddress) {
}

//------------------------------------------------------------------------------
// Name: ~MemRegion()
// Desc: 
//------------------------------------------------------------------------------
MemRegion::~MemRegion() {
}

//------------------------------------------------------------------------------
// Name: setPermissions(bool read, bool write, bool execute)
// Desc: wrapper around normal set permissions
//------------------------------------------------------------------------------
void MemRegion::setPermissions(bool read, bool write, bool execute) {

}
