/*
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.
*/

#ifndef DEBUGGERCOREUNIX_20090529_H_
#define DEBUGGERCOREUNIX_20090529_H_

#include "DebuggerCoreBase.h"

#define SET_OK(ok, v)	do { (ok) = ((v) != -1) || (errno == 0); } while(0)
#define EDB_WORDSIZE	sizeof(long)

class DebuggerCoreUNIX : public DebuggerCoreBase {
public:
	DebuggerCoreUNIX();
	virtual ~DebuggerCoreUNIX() {}
	
protected:
	void writeByte(edb::address_t address, quint8 value, bool &ok);
	quint8 readByte(edb::address_t address, bool &ok);
	void writeByteBase(edb::address_t address, quint8 value, bool &ok);
	quint8 readByteBase(edb::address_t address, bool &ok);
	void executeProcess(const QString &path, const QString &cwd, const QStringList &args);

protected:
	static pid_t waitpid_timeout(pid_t pid, int *status, int options, int msecs, bool &timeout);

public:
	static ssize_t native_read(int fd, void *buf, size_t count);
	static ssize_t native_write(int fd, const void *buf, size_t count);
	static int native_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
	static pid_t native_waitpid(pid_t pid, int *status, int options);
	static int native_execvp(const char *file, char *const argv[]);
	
public:
	virtual bool readPages(edb::address_t address, void *buf, std::size_t count);
	virtual bool readBytes(edb::address_t address, void *buf, std::size_t len);
	virtual bool writeBytes(edb::address_t address, const void *buf, std::size_t len);
	
protected:
	virtual long readData(edb::address_t address, bool &ok) = 0;
	virtual bool writeData(edb::address_t address, long value) = 0;
};

#endif
