debugger.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00034 #include "gpucalc/debugger/debugger.h"
00035 
00036 #include "gpucalc/task.h"
00037 #include "gpucalc/data.h"
00038 
00039 namespace gpucalc
00040 {
00041 
00042  template<> debugger::Debugger * gpucalc::Singleton<debugger::Debugger>::mSingleton = 0;
00043 
00044 
00045  debugger::Debugger * debugger::Debugger::getSingletonPtr()
00046  {
00047   return mSingleton;
00048  }
00049 
00050 
00051  debugger::Debugger & debugger::Debugger::getSingleton()
00052  {
00053   assert(mSingleton);
00054   return(* mSingleton);
00055  }
00056 
00057 namespace debugger
00058 {
00059   Debugger::Debugger(): Singleton<Debugger>(_DebuggerName), mCodeInstructorMap(), mActiveCodeInstructor(0),
00060    mIsDebug(false), mTask(0), mOriginalSource(), mExpression(), mLine(0)
00061   {
00062   }
00063 
00064 
00065   Debugger::~Debugger()
00066   {
00067   }
00068 
00069 
00070   void Debugger::addCodeInstructor(CodeInstructor * CodeInst)
00071   {
00072    addToContainer(CodeInst, CodeInst->getLanguageID(), mCodeInstructorMap);
00073   }
00074 
00075 
00076   void Debugger::setTask(Task * task)
00077   {
00078    mTask = task;
00079    Kernel * kernel = task->getActiveKernel();
00080    mOriginalSource = kernel->getShader()->getSource();
00081    {
00082     CodeInstructorMap::iterator i = mCodeInstructorMap.find(kernel->getShader()->getLanguageID());
00083     if (i != mCodeInstructorMap.end())
00084     {
00085      mActiveCodeInstructor = i->second;
00086     }
00087     else
00088     {
00089      Except<ERR_ITEM_NOT_FOUND>(this,
00090       "CodeInstructor with language id \"" + kernel->getShader()->getLanguageID() + "\" cannot be found. Debugging is not available.",
00091       "debugger::Debugger::setTask()", __FILE__, __LINE__);
00092     }
00093    }
00094    mActiveCodeInstructor->setOriginalSource(mOriginalSource);
00095    mIsDebug = true;
00096   }
00097 
00098 
00099   void Debugger::setBreakPoint(size_t line)
00100   {
00101    mLine = line;
00102   }
00103 
00104 
00105   void Debugger::print(const std::string & expression)
00106   {
00107    mExpression = expression;
00108    doPrintingData(process(), 0);
00109   }
00110 
00111 
00112   void Debugger::stepInto()
00113   {
00114    /*
00115    if (mActiveCodeInstructor->isExternalFunctionCalling(mLine))
00116    {
00117     mLine = mActiveCodeInstructor->getExternalFunctionBody(mActiveCodeInstructor->getExternalFunctionName(mLine));
00118    }
00119    */
00120    Warning<ERR_NOT_IMPLEMENTED>(this, "StepInto is not implemented yet.", "debugger::Debugger::stepInto()", __FILE__, __LINE__);
00121    setBreakPoint(mLine + 1);
00122   }
00123 
00124 
00125   void Debugger::stepOver()
00126   {
00132    /*
00133    if (mActiveCodeInstructor->isCycle(mLine))
00134    {
00135     mLine = mActiveCodeInstructor->getEndOfCycle(mLine);
00136    }
00137    */
00138    Warning<ERR_NOT_IMPLEMENTED>(this, "StepOver is not implemented yet.", "debugger::Debugger::stepOver()", __FILE__, __LINE__);
00139    setBreakPoint(mLine + 1);
00140   }
00141 
00142 
00143   const Data & Debugger::process()
00144   {
00145    if ((mLine != 0) && (mExpression.length() != 0))
00146    {
00147     std::string InstructedSource = mActiveCodeInstructor->instruct(mLine, mExpression);
00148     mTask->getActiveKernel()->getShader()->setSource(InstructedSource);
00149     mTask->compute();
00150     return  mTask->getResultData();
00151    }
00152    else
00153    {
00154     Except<ERR_INVALIDPARAMS>(this, "Line or Expression are incorrect", "debugger::Debugger::process()", __FILE__, __LINE__);
00155    }
00156   }
00157 
00158 
00159   void Debugger::stop()
00160   {
00161    mIsDebug = false;
00162    mTask = 0;
00163    mOriginalSource = std::string();
00164    mExpression = std::string();
00165    mLine = 0;
00166    mActiveCodeInstructor->setOriginalSource(mOriginalSource);
00167   }
00168 
00169 
00170   void Debugger::doPrintingData(const Data & data, size_t xcomp, size_t ycomp, size_t zcomp)
00171   {
00172    switch (data.getValueType())
00173    {
00174     case FloatType:
00175      elseOnePrintingData<float>(data, xcomp, ycomp, zcomp);
00176      break;
00177 
00178     case CharType:
00179      elseOnePrintingData<char>(data, xcomp, ycomp, zcomp);
00180      break;
00181 
00182     default:
00183      Except<ERR_NOT_IMPLEMENTED>(Object(_SingletonClassName, _DebuggerName), "Other value types is not implemented yet.",
00184       "debugger::Debugger::doPrintingData()", __FILE__, __LINE__);
00185      break;
00186    }
00187   }
00188   
00189 
00190   template<typename T> void Debugger::elseOnePrintingData(const Data & data, size_t xcomp, size_t ycomp, size_t zcomp)
00191   {
00192    size_t offset = data.getNumberOfComponents() * (data.getSizeX() * (zcomp * data.getSizeY() + ycomp) + xcomp);
00193    for (size_t i = 0; i < data.getNumberOfComponents(); ++i)
00194    {
00195     std::cout << static_cast<const T *>(data.pointer())[offset + i] << "\t";
00196    }
00197    std::cout << std::endl;
00198   }
00199  }
00200 }

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6