2013-01-13 23:29:42 +00:00
|
|
|
#include "ctrldisasmview.h"
|
2013-02-10 15:36:06 +00:00
|
|
|
#include <QPainter>
|
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QAction>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QInputDialog>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
2013-11-16 15:49:08 +00:00
|
|
|
#include "math.h"
|
|
|
|
|
2013-02-10 15:36:06 +00:00
|
|
|
#include "debugger_disasm.h"
|
|
|
|
#include "Core/Debugger/SymbolMap.h"
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
u32 halfAndHalf(u32 a, u32 b)
|
|
|
|
{
|
|
|
|
return ((a>>1)&0x7f7f7f7f) + ((b>>1)&0x7f7f7f7f);
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 23:29:42 +00:00
|
|
|
|
|
|
|
CtrlDisAsmView::CtrlDisAsmView(QWidget *parent) :
|
2013-02-10 15:36:06 +00:00
|
|
|
QWidget(parent)
|
|
|
|
{
|
|
|
|
curAddress=0;
|
|
|
|
rowHeight=14;
|
|
|
|
align=2;
|
|
|
|
selecting=false;
|
|
|
|
showHex=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::keyPressEvent(QKeyEvent *e)
|
2013-01-13 23:29:42 +00:00
|
|
|
{
|
2013-02-10 15:36:06 +00:00
|
|
|
int page=(rect().bottom()/rowHeight)/2-1;
|
|
|
|
|
2013-02-20 14:52:12 +00:00
|
|
|
switch (e->key())
|
2013-02-10 15:36:06 +00:00
|
|
|
{
|
2013-02-20 14:52:12 +00:00
|
|
|
case Qt::Key_Down: curAddress += align; break;
|
|
|
|
case Qt::Key_Up: curAddress -= align; break;
|
|
|
|
case Qt::Key_PageDown: curAddress += page*align; break;
|
|
|
|
case Qt::Key_PageUp: curAddress -= page*align; break;
|
|
|
|
default: QWidget::keyPressEvent(e); break;
|
2013-02-10 15:36:06 +00:00
|
|
|
}
|
2013-02-20 14:52:12 +00:00
|
|
|
|
2013-02-10 15:36:06 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::mousePressEvent(QMouseEvent *e)
|
|
|
|
{
|
|
|
|
int x = e->pos().x();
|
|
|
|
int y = e->pos().y();
|
|
|
|
if (x>16)
|
|
|
|
{
|
|
|
|
oldSelection=selection;
|
|
|
|
selection=yToAddress(y);
|
|
|
|
bool oldselecting=selecting;
|
|
|
|
selecting=true;
|
|
|
|
if (!oldselecting || (selection!=oldSelection))
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
debugger->toggleBreakpoint(yToAddress(y));
|
|
|
|
parentWindow->Update();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::wheelEvent(QWheelEvent* e)
|
|
|
|
{
|
|
|
|
int numDegrees = e->delta() / 8;
|
|
|
|
int numSteps = numDegrees / 15;
|
2013-02-20 14:52:12 +00:00
|
|
|
if (e->orientation() == Qt::Vertical)
|
|
|
|
{
|
2013-02-10 15:36:06 +00:00
|
|
|
curAddress -= numSteps*align;
|
|
|
|
update();
|
2013-02-20 14:52:12 +00:00
|
|
|
}
|
2013-01-13 23:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::redraw()
|
|
|
|
{
|
2013-02-10 15:36:06 +00:00
|
|
|
update();
|
2013-01-13 23:29:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::contextMenu(const QPoint &pos)
|
|
|
|
{
|
2013-02-10 15:36:06 +00:00
|
|
|
QMenu menu(this);
|
|
|
|
|
|
|
|
QAction *copyAddress = new QAction(tr("Copy &address"), this);
|
|
|
|
connect(copyAddress, SIGNAL(triggered()), this, SLOT(CopyAddress()));
|
|
|
|
menu.addAction(copyAddress);
|
|
|
|
|
|
|
|
QAction *copyInstrHex = new QAction(tr("Copy instruction (&hex)"), this);
|
|
|
|
connect(copyInstrHex, SIGNAL(triggered()), this, SLOT(CopyInstrHex()));
|
|
|
|
menu.addAction(copyInstrHex);
|
|
|
|
|
|
|
|
QAction *copyInstrDisAsm = new QAction(tr("Copy instruction (&disasm)"), this);
|
|
|
|
connect(copyInstrDisAsm, SIGNAL(triggered()), this, SLOT(CopyInstrDisAsm()));
|
|
|
|
menu.addAction(copyInstrDisAsm);
|
|
|
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
|
|
|
QAction *runToHere = new QAction(tr("&Run to here"), this);
|
|
|
|
connect(runToHere, SIGNAL(triggered()), this, SLOT(RunToHere()));
|
|
|
|
menu.addAction(runToHere);
|
|
|
|
|
|
|
|
QAction *setNextStatement = new QAction(tr("&Set Next Statement"), this);
|
|
|
|
connect(setNextStatement, SIGNAL(triggered()), this, SLOT(SetNextStatement()));
|
|
|
|
menu.addAction(setNextStatement);
|
|
|
|
|
|
|
|
QAction *toggleBreakpoint = new QAction(tr("&Toggle breakpoint"), this);
|
|
|
|
connect(toggleBreakpoint, SIGNAL(triggered()), this, SLOT(ToggleBreakpoint()));
|
|
|
|
menu.addAction(toggleBreakpoint);
|
|
|
|
|
|
|
|
QAction *followBranch = new QAction(tr("&Follow branch"), this);
|
|
|
|
connect(followBranch, SIGNAL(triggered()), this, SLOT(FollowBranch()));
|
|
|
|
menu.addAction(followBranch);
|
|
|
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
|
|
|
//QAction *showDynarecResults = new QAction(tr("&Show Dynarec Results"), this);
|
|
|
|
//connect(showDynarecResults, SIGNAL(triggered()), this, SLOT(ShowDynarecResults()));
|
|
|
|
//menu.addAction(showDynarecResults);
|
|
|
|
|
|
|
|
QAction *goToMemoryView = new QAction(tr("Go to in &Memory View"), this);
|
|
|
|
connect(goToMemoryView, SIGNAL(triggered()), this, SLOT(GoToMemoryView()));
|
|
|
|
menu.addAction(goToMemoryView);
|
|
|
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
|
|
|
//QAction *killFunction = new QAction(tr("&Kill function"), this);
|
|
|
|
//connect(killFunction, SIGNAL(triggered()), this, SLOT(KillFunction()));
|
|
|
|
//menu.addAction(killFunction);
|
|
|
|
|
|
|
|
QAction *renameFunction = new QAction(tr("&Rename function..."), this);
|
|
|
|
connect(renameFunction, SIGNAL(triggered()), this, SLOT(RenameFunction()));
|
|
|
|
menu.addAction(renameFunction);
|
|
|
|
|
|
|
|
|
|
|
|
menu.exec( mapToGlobal(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::GoToMemoryView()
|
|
|
|
{
|
|
|
|
parentWindow->ShowMemory(selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::CopyAddress()
|
|
|
|
{
|
2013-02-17 00:06:06 +00:00
|
|
|
QApplication::clipboard()->setText(QString("%1").arg(selection,8,16,QChar('0')));
|
2013-02-10 15:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::CopyInstrDisAsm()
|
|
|
|
{
|
|
|
|
QApplication::clipboard()->setText(debugger->disasm(selection,align));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::CopyInstrHex()
|
|
|
|
{
|
2013-02-17 00:06:06 +00:00
|
|
|
QApplication::clipboard()->setText(QString("%1").arg(debugger->readMemory(selection),8,16,QChar('0')));
|
2013-02-10 15:36:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::SetNextStatement()
|
|
|
|
{
|
|
|
|
debugger->setPC(selection);
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::ToggleBreakpoint()
|
|
|
|
{
|
|
|
|
debugger->toggleBreakpoint(selection);
|
|
|
|
parentWindow->Update();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::FollowBranch()
|
|
|
|
{
|
2013-11-19 11:02:43 +00:00
|
|
|
const char *temp = debugger->disasm(selection,align);;
|
2013-02-10 15:36:06 +00:00
|
|
|
const char *mojs=strstr(temp,"->$");
|
|
|
|
if (mojs)
|
|
|
|
{
|
|
|
|
u32 dest;
|
|
|
|
sscanf(mojs+3,"%08x",&dest);
|
|
|
|
if (dest)
|
|
|
|
{
|
|
|
|
marker = selection;
|
|
|
|
gotoAddr(dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::RunToHere()
|
|
|
|
{
|
|
|
|
debugger->setBreakpoint(selection);
|
|
|
|
debugger->runToBreakpoint();
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::RenameFunction()
|
|
|
|
{
|
2013-11-27 14:06:41 +00:00
|
|
|
u32 funcBegin = symbolMap.GetFunctionStart(curAddress);
|
2014-01-28 15:42:33 +00:00
|
|
|
if (funcBegin != (u32)-1)
|
2013-11-27 14:06:41 +00:00
|
|
|
{
|
2014-01-28 15:42:33 +00:00
|
|
|
QString name = QString::fromStdString(symbolMap.GetLabelString(funcBegin));
|
2013-11-27 14:06:41 +00:00
|
|
|
bool ok;
|
|
|
|
QString newname = QInputDialog::getText(this, tr("New function name"),
|
|
|
|
tr("New function name:"), QLineEdit::Normal,
|
|
|
|
name, &ok);
|
|
|
|
if (ok && !newname.isEmpty())
|
|
|
|
{
|
|
|
|
symbolMap.SetLabelName(newname.toStdString().c_str(),funcBegin);
|
|
|
|
redraw();
|
|
|
|
parentWindow->NotifyMapLoaded();
|
|
|
|
}
|
2013-02-10 15:36:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-20 14:52:12 +00:00
|
|
|
QMessageBox::information(this,tr("Warning"),tr("No symbol selected"),QMessageBox::Ok);
|
2013-02-10 15:36:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CtrlDisAsmView::paintEvent(QPaintEvent *)
|
|
|
|
{
|
|
|
|
QPainter painter(this);
|
|
|
|
painter.setBrush(Qt::white);
|
|
|
|
painter.setPen(Qt::white);
|
|
|
|
painter.drawRect(rect());
|
|
|
|
|
|
|
|
struct branch
|
|
|
|
{
|
|
|
|
int src,dst,srcAddr;
|
|
|
|
bool conditional;
|
|
|
|
};
|
|
|
|
branch branches[256];
|
|
|
|
int numBranches=0;
|
|
|
|
|
|
|
|
int width = rect().width();
|
2013-11-24 07:53:09 +00:00
|
|
|
int numRows=(rect().height()/rowHeight);
|
2013-02-10 15:36:06 +00:00
|
|
|
|
2013-02-20 14:52:12 +00:00
|
|
|
QColor bgColor(0xFFFFFFFF);
|
|
|
|
QPen nullPen(bgColor);
|
|
|
|
QPen currentPen(QColor(0,0,0));
|
|
|
|
QPen selPen(QColor(0xFF808080));
|
|
|
|
QPen condPen(QColor(0xFFFF3020));
|
2013-02-10 15:36:06 +00:00
|
|
|
|
|
|
|
QBrush lbr;
|
|
|
|
lbr.setColor(bgColor);
|
2013-02-20 14:52:12 +00:00
|
|
|
QBrush currentBrush(QColor(0xFFFFEfE8));
|
|
|
|
QBrush pcBrush(QColor(0xFF70FF70));
|
2013-02-10 15:36:06 +00:00
|
|
|
|
2013-02-20 14:52:12 +00:00
|
|
|
QFont normalFont("Arial", 10);
|
|
|
|
QFont boldFont("Arial", 10);
|
|
|
|
QFont alignedFont("Monospace", 10);
|
2013-11-24 07:53:09 +00:00
|
|
|
alignedFont.setStyleHint(QFont::Monospace);
|
2013-02-10 15:36:06 +00:00
|
|
|
boldFont.setBold(true);
|
|
|
|
painter.setFont(normalFont);
|
|
|
|
|
|
|
|
|
2013-11-24 05:49:32 +00:00
|
|
|
QImage breakPoint(":/resources/breakpoint.ico");
|
2013-02-10 15:36:06 +00:00
|
|
|
int i;
|
|
|
|
curAddress&=~(align-1);
|
|
|
|
|
|
|
|
align=(debugger->getInstructionSize(0));
|
2013-11-24 07:53:09 +00:00
|
|
|
for (i=0; i<=numRows; i++)
|
2013-02-10 15:36:06 +00:00
|
|
|
{
|
2013-11-24 07:53:09 +00:00
|
|
|
unsigned int address=curAddress + (i-(numRows/2))*align;
|
2013-02-10 15:36:06 +00:00
|
|
|
|
2013-11-24 07:53:09 +00:00
|
|
|
int rowY1 = rect().top() + rowHeight*i;
|
|
|
|
int rowY2 = rect().top() + rowHeight*i + rowHeight - 1;
|
2013-02-10 15:36:06 +00:00
|
|
|
|
2013-02-24 15:34:32 +00:00
|
|
|
lbr.setColor((unsigned int)marker == address ? QColor(0xFFFFEEE0) : QColor(debugger->getColor(address)));
|
2013-02-10 15:36:06 +00:00
|
|
|
QColor bg = lbr.color();
|
2013-11-24 07:53:09 +00:00
|
|
|
painter.setBrush(currentBrush);
|
2013-02-10 15:36:06 +00:00
|
|
|
painter.setPen(nullPen);
|
|
|
|
painter.drawRect(0,rowY1,16-1,rowY2-rowY1);
|
|
|
|
|
2013-02-24 15:34:32 +00:00
|
|
|
if (selecting && address == (unsigned int)selection)
|
2013-02-10 15:36:06 +00:00
|
|
|
painter.setPen(selPen);
|
|
|
|
else
|
2013-11-24 07:53:09 +00:00
|
|
|
{
|
|
|
|
if(i==numRows/2)
|
|
|
|
painter.setPen(currentPen);
|
|
|
|
else
|
|
|
|
painter.setPen(bg);
|
|
|
|
}
|
|
|
|
painter.setBrush(QBrush(bg));
|
2013-02-10 15:36:06 +00:00
|
|
|
|
|
|
|
if (address == debugger->getPC())
|
|
|
|
{
|
|
|
|
painter.setBrush(pcBrush);
|
|
|
|
}
|
|
|
|
|
|
|
|
painter.drawRect(16,rowY1,width-16-1,rowY2-rowY1);
|
|
|
|
painter.setBrush(currentBrush);
|
2013-02-20 14:52:12 +00:00
|
|
|
QPen textPen(QColor(halfAndHalf(bg.rgba(),0)));
|
2013-02-10 15:36:06 +00:00
|
|
|
painter.setPen(textPen);
|
|
|
|
painter.setFont(alignedFont);
|
2013-02-17 00:06:06 +00:00
|
|
|
painter.drawText(17,rowY1-3+rowHeight,QString("%1").arg(address,8,16,QChar('0')));
|
2013-02-10 15:36:06 +00:00
|
|
|
painter.setFont(normalFont);
|
|
|
|
textPen.setColor(QColor(0xFF000000));
|
|
|
|
painter.setPen(textPen);
|
|
|
|
if (debugger->isAlive())
|
|
|
|
{
|
|
|
|
const char *dizz = debugger->disasm(address, align);
|
|
|
|
char dis[512];
|
|
|
|
strcpy(dis, dizz);
|
|
|
|
char *dis2 = strchr(dis,'\t');
|
|
|
|
char desc[256]="";
|
|
|
|
if (dis2)
|
|
|
|
{
|
|
|
|
*dis2=0;
|
|
|
|
dis2++;
|
|
|
|
const char *mojs=strstr(dis2,"->$");
|
|
|
|
if (mojs)
|
|
|
|
{
|
|
|
|
for (int i=0; i<8; i++)
|
|
|
|
{
|
|
|
|
bool found=false;
|
|
|
|
for (int j=0; j<22; j++)
|
|
|
|
{
|
|
|
|
if (mojs[i+3]=="0123456789ABCDEFabcdef"[j])
|
|
|
|
found=true;
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
mojs=0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mojs)
|
|
|
|
{
|
|
|
|
int offs;
|
|
|
|
sscanf(mojs+3,"%08x",&offs);
|
|
|
|
branches[numBranches].src=rowY1 + rowHeight/2;
|
|
|
|
branches[numBranches].srcAddr=address/align;
|
|
|
|
branches[numBranches].dst=(int)(rowY1+((s64)offs-(s64)address)*rowHeight/align + rowHeight/2);
|
|
|
|
branches[numBranches].conditional = (dis[1]!=0); //unconditional 'b' branch
|
|
|
|
numBranches++;
|
2014-01-28 15:42:33 +00:00
|
|
|
const char *t = debugger->getDescription(offs).c_str();
|
2013-02-10 15:36:06 +00:00
|
|
|
if (memcmp(t,"z_",2)==0)
|
|
|
|
t+=2;
|
|
|
|
if (memcmp(t,"zz_",3)==0)
|
|
|
|
t+=3;
|
|
|
|
sprintf(desc,"-->%s", t);
|
|
|
|
textPen.setColor(QColor(0xFF600060));
|
|
|
|
painter.setPen(textPen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textPen.setColor(QColor(0xFF000000));
|
|
|
|
painter.setPen(textPen);
|
|
|
|
}
|
|
|
|
painter.drawText(149,rowY1-3+rowHeight,QString(dis2));
|
|
|
|
}
|
|
|
|
textPen.setColor(QColor(0xFF007000));
|
|
|
|
painter.setPen(textPen);
|
|
|
|
painter.setFont(boldFont);
|
|
|
|
painter.drawText(84,rowY1-3+rowHeight,QString(dis));
|
|
|
|
painter.setFont(normalFont);
|
|
|
|
if (desc[0]==0)
|
|
|
|
{
|
2014-01-28 15:42:33 +00:00
|
|
|
const char *t = debugger->getDescription(address).c_str();
|
2013-02-10 15:36:06 +00:00
|
|
|
if (memcmp(t,"z_",2)==0)
|
|
|
|
t+=2;
|
|
|
|
if (memcmp(t,"zz_",3)==0)
|
|
|
|
t+=3;
|
|
|
|
strcpy(desc,t);
|
|
|
|
}
|
|
|
|
if (memcmp(desc,"-->",3) == 0)
|
|
|
|
{
|
|
|
|
textPen.setColor(QColor(0xFF0000FF));
|
|
|
|
painter.setPen(textPen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
textPen.setColor(halfAndHalf(halfAndHalf(bg.rgba(),0),bg.rgba()));
|
|
|
|
painter.setPen(textPen);
|
|
|
|
}
|
|
|
|
if (strlen(desc))
|
|
|
|
painter.drawText(std::max(280,width/3+190),rowY1-3+rowHeight,QString(desc));
|
|
|
|
if (debugger->isBreakpoint(address))
|
|
|
|
{
|
|
|
|
painter.drawImage(2,rowY1+2,breakPoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i=0; i<numBranches; i++)
|
|
|
|
{
|
|
|
|
painter.setPen(branches[i].conditional ? condPen : currentPen);
|
|
|
|
int x=280+(branches[i].srcAddr%9)*8;
|
2013-02-20 14:52:12 +00:00
|
|
|
QPoint curPos(x-2,branches[i].src);
|
2013-02-10 15:36:06 +00:00
|
|
|
|
|
|
|
if (branches[i].dst<rect().bottom()+200 && branches[i].dst>-200)
|
|
|
|
{
|
|
|
|
painter.drawLine(curPos, QPoint(x+2,branches[i].src));
|
|
|
|
curPos = QPoint(x+2,branches[i].src);
|
|
|
|
painter.drawLine(curPos, QPoint(x+2,branches[i].dst));
|
|
|
|
curPos = QPoint(x+2,branches[i].dst);
|
|
|
|
painter.drawLine(curPos, QPoint(x-4,branches[i].dst));
|
|
|
|
|
|
|
|
curPos = QPoint(x,branches[i].dst-4);
|
|
|
|
painter.drawLine(curPos, QPoint(x-4,branches[i].dst));
|
|
|
|
curPos = QPoint(x-4,branches[i].dst);
|
|
|
|
painter.drawLine(curPos, QPoint(x+1,branches[i].dst+5));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
painter.drawLine(curPos, QPoint(x+4,branches[i].src));
|
|
|
|
}
|
|
|
|
}
|
2013-01-13 23:29:42 +00:00
|
|
|
}
|
|
|
|
|
2013-02-10 15:36:06 +00:00
|
|
|
int CtrlDisAsmView::yToAddress(int y)
|
2013-01-13 23:29:42 +00:00
|
|
|
{
|
2013-11-24 07:53:09 +00:00
|
|
|
//int ydiff=y - rect().bottom()/2;//-rowHeight/2;
|
|
|
|
int ydiff=(int)(floor((float)y / (float)rowHeight));
|
|
|
|
ydiff -= (rect().height()/rowHeight)/2;
|
|
|
|
return curAddress + ydiff *align;
|
2013-01-13 23:29:42 +00:00
|
|
|
}
|