/*
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 "MemoryRegions.h"

#include <QtDebug>
#include <QTextStream>
#include <QFile>

//------------------------------------------------------------------------------
// Name: primaryCodeRegion()
// Desc: 
//------------------------------------------------------------------------------
EDB_EXPORT MemRegion edb::v1::primaryCodeRegion() {
	
	const QString process_executable = getProcessExecutable();
	
	memoryRegions().sync();
	
	const QVector<MemRegion> r = memoryRegions().regions();
	Q_FOREACH(const MemRegion &region, r) {
		if(region.executable() && region.name == process_executable) {		
			return region;
		}
	}
	return MemRegion();
}

//------------------------------------------------------------------------------
// Name: loadedLibraries()
// Desc: 
//------------------------------------------------------------------------------
QStringList edb::v1::loadedLibraries() {
	QStringList ret;
	
	memoryRegions().sync();

	const QVector<MemRegion> r = memoryRegions().regions();
	Q_FOREACH(const MemRegion &region, r) {
		// modules seem to have full paths
		if(region.name.startsWith("/")) {
			if(!ret.contains(region.name)) {
				ret << region.name;
			}
		}
	}
	
	return ret;
}

//------------------------------------------------------------------------------
// Name: getProcessExecutable()
// Desc: 
//------------------------------------------------------------------------------
QString edb::v1::getProcessExecutable() {
	QString ret;
	
	if(debuggerBase != 0) {
		// TODO: assert attached!
		const edb::pid_t pid = debuggerBase->pid();
		ret = edb::v1::symlinkTarget(QString("/proc/%1/exe").arg(pid));
	}
	
	return ret;
}

//------------------------------------------------------------------------------
// Name: getProcessCurrentWorkingDirectory()
// Desc: 
//------------------------------------------------------------------------------
QString edb::v1::getProcessCurrentWorkingDirectory() {
	QString ret;
	if(debuggerBase != 0) {
		// TODO: assert attached!
		const edb::pid_t pid = debuggerBase->pid();
		ret = edb::v1::symlinkTarget(QString("/proc/%1/cwd").arg(pid));
	}
	return ret;
}

//------------------------------------------------------------------------------
// Name: getProcessArguments()
// Desc: 
//------------------------------------------------------------------------------
QStringList edb::v1::getProcessArguments() {
	QStringList ret;
	if(debuggerBase != 0) {
	
		// TODO: assert attached!
		const edb::pid_t pid = debuggerBase->pid();
		const QString cmdlineFile(QString("/proc/%1/cmdline").arg(pid));
		QFile file(cmdlineFile);

		if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
			QTextStream in(&file);

			QString s;
			QChar ch;

			while(in.status() == QTextStream::Ok) {
				in >> ch;
				if(ch == '\0') {
					if(!s.isEmpty()) {
						ret << s;
					}
					s.clear();
				} else {
					s += ch;
				}
			}

			if(!s.isEmpty()) {
				ret << s;
			}
		}
	}
	return ret;
}
