/*
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 "Debugger.h"
#include "DebuggerCoreInterface.h"
#include <QTextStream>
#include <tlhelp32.h>

//------------------------------------------------------------------------------
// Name: 
// Desc: 
//------------------------------------------------------------------------------
MemRegion edb::v1::primaryCodeRegion() {
	return MemRegion();
}

//------------------------------------------------------------------------------
// Name: loadedLibraries()
// Desc: 
//------------------------------------------------------------------------------
QStringList edb::v1::loadedLibraries() {
	QStringList ret;
	edb::pid_t pid = edb::v1::debuggerBase->pid();
	HANDLE hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
	if(hModuleSnap != INVALID_HANDLE_VALUE) {
		MODULEENTRY32 me32;
		
		me32.dwSize = sizeof(me32);
		
		if(!Module32First(hModuleSnap, &me32)) {
			do {
				ret << QString::fromWCharArray(me32.szModule);
			} while(Module32Next(hModuleSnap, &me32));
		}
	}
	CloseHandle(hModuleSnap);
	return ret;
}

//------------------------------------------------------------------------------
// Name: getProcessExecutable()
// Desc: 
//------------------------------------------------------------------------------
QString edb::v1::getProcessExecutable() {
	QString ret;
	edb::pid_t pid = edb::v1::debuggerBase->pid();
	HANDLE ph = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
	if(ph != 0) {	
		WCHAR name[MAX_PATH];
		DWORD size = MAX_PATH;
		/*
		if(GetModuleFileNameEx(ph, NULL, name, PATH_MAX)) {
			ret = name;
		}
		*/
		
		if(QueryFullProcessImageName(ph, 0, name, &size)) {
			ret = QString::fromWCharArray(name);
		}
		CloseHandle(ph);
	}

	return ret;
}

//------------------------------------------------------------------------------
// Name: getProcessCurrentWorkingDirectory()
// Desc: 
//------------------------------------------------------------------------------
QString edb::v1::getProcessCurrentWorkingDirectory() {
	QString ret;
	return ret;
}

//------------------------------------------------------------------------------
// Name: getProcessArguments()
// Desc: 
//------------------------------------------------------------------------------
QStringList edb::v1::getProcessArguments() {
	QStringList ret;
	return ret;
}
