/*
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 DEBUGGEROPS_20090424_H_
#define DEBUGGEROPS_20090424_H_

#include "State.h"
#include "Debugger.h"
#include "Instruction.h"
#include "DebuggerCoreInterface.h"

template <class F1, class F2>
void stepOver(F1 runFunc, F2 stepFunc) {
	const edb::address_t ip = edb::v1::currentState().instructionPointer();

	quint8 buffer[edb::Instruction::MAX_SIZE];
	int sz = sizeof(buffer);

	if(edb::v1::getInstructionBytes(ip, buffer, sz)) {
		edb::Instruction insn(buffer, sz, 0, std::nothrow);
		if(insn.valid()) {
			if(edb::v1::archProcessor().canStepOver(insn)) {
				// add a temporary breakpoint at the instruction just 
				// after the call
				edb::v1::debuggerBase->addBreakpoint(ip + insn.size(), true);

				runFunc();
				return;
			}
		}
	}

	// if all else fails, it's a step into
	stepFunc();
}

#endif
