/*
Copyright (C) 2006 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.
*/

#ifndef DEBUGGERCORE_20060430_H_
#define DEBUGGERCORE_20060430_H_

#include "DebuggerCoreInterface.h"

class QProcess;

class DebuggerCore : public QObject, public DebuggerCoreInterface {
	Q_OBJECT
	Q_INTERFACES(DebuggerCoreInterface)
	Q_CLASSINFO("Author", "Evan Teran")
	Q_CLASSINFO("URL", "http://www.codef00.com")

	
public:
	DebuggerCore();
	virtual ~DebuggerCore();
	
public:
	virtual int executionBreakpointSize() const;
	virtual QByteArray executionBreakpointData() const;
	
public:
	virtual edb::address_t pageSize() const;
	
public:
	virtual bool writeBytes(edb::address_t address, const void *buf, std::size_t len);
	virtual bool readBytes(edb::address_t address, void *buf, std::size_t len);
	virtual bool readPages(edb::address_t address, void *buf, std::size_t count);
	
public:
	virtual bool open(const QString &path, const QString &cwd, const QStringList &args, const QString &tty);
	virtual bool open(const QString &path, const QString &cwd, const QStringList &args);
	virtual bool attach(edb::pid_t pid);
	virtual void detach();
	virtual void kill();
	virtual void pause();
	virtual void resume(int signum);
	virtual void step(int signum);
	virtual void getState(State &state);
	virtual void setState(const State &state);
	virtual void waitForDebugEvent(DebugEvent *event, bool &ok, int secs);
	
public:
	virtual edb::address_t getFaultAddress();
	
public:
	virtual void setBreakpointCondition(edb::address_t address, const QString &condition);
	virtual QString getBreakpointCondition(edb::address_t address);
	virtual void addBreakpoint(edb::address_t address, bool oneTime = false);
	virtual void removeBreakpoint(edb::address_t address);
	virtual edb::address_t disableBreakpoint(edb::address_t address);
	virtual edb::address_t enableBreakpoint(edb::address_t address);
	virtual void backupBreakpoints(BreakpointState &state);
	virtual Breakpoint *findBreakpoint(edb::address_t address);
	virtual void clearBreakpoints();

public:
	virtual edb::pid_t pid() const;
	
private:
	long readData(edb::address_t address, bool &ok);
	void writeData(edb::address_t address, long value, bool &ok);
	void writeByte(edb::address_t address, quint8 value, bool &ok);
	void writeByteBase(edb::address_t address, quint8 value, bool &ok);
	quint8 readByte(edb::address_t address, bool &ok);
	quint8 readByteBase(edb::address_t address, bool &ok);
	bool attached() const;
	void doExecProcess(const QString &path, const QString &cwd, const QStringList &args);
	
private:
	edb::pid_t			m_PID;
	BreakpointState		m_Breakpoints;
	static const char	m_BreakInstruction[];
};

#endif
