RedPanda-CPP/RedPandaIDE/debugger.cpp

2542 lines
71 KiB
C++
Raw Normal View History

2021-12-26 23:18:28 +08:00
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
2021-07-03 21:57:50 +08:00
#include "debugger.h"
2021-07-17 19:32:23 +08:00
#include "utils.h"
#include "mainwindow.h"
2021-07-24 11:18:25 +08:00
#include "editor.h"
2021-07-25 00:26:13 +08:00
#include "settings.h"
2021-08-01 23:24:37 +08:00
#include "widgets/cpudialog.h"
#include "systemconsts.h"
2021-07-25 00:26:13 +08:00
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
2021-08-01 10:00:27 +08:00
#include <QPlainTextEdit>
2021-08-01 01:06:43 +08:00
#include <QDebug>
#include <QDir>
2021-11-23 21:08:33 +08:00
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
2021-11-25 09:05:45 +08:00
#include "widgets/signalmessagedialog.h"
2021-07-03 21:57:50 +08:00
Debugger::Debugger(QObject *parent) : QObject(parent)
{
2021-07-24 11:18:25 +08:00
mBreakpointModel=new BreakpointModel(this);
mBacktraceModel=new BacktraceModel(this);
2021-08-01 01:06:43 +08:00
mWatchModel = new WatchModel(this);
2021-08-01 23:24:37 +08:00
mRegisterModel = new RegisterModel(this);
mMemoryModel = new MemoryModel(8,this);
connect(mMemoryModel,&MemoryModel::setMemoryData,
this, &Debugger::setMemoryData);
connect(mWatchModel, &WatchModel::setWatchVarValue,
this, &Debugger::setWatchVarValue);
2021-07-27 00:14:24 +08:00
mExecuting = false;
mReader = nullptr;
mTarget = nullptr;
2021-07-27 00:14:24 +08:00
mCommandChanged = false;
mLeftPageIndexBackup = -1;
2021-11-25 20:26:43 +08:00
connect(mWatchModel, &WatchModel::fetchChildren,
this, &Debugger::fetchVarChildren);
2021-07-24 11:18:25 +08:00
}
bool Debugger::start(const QString& inferior)
2021-07-25 00:26:13 +08:00
{
Settings::PCompilerSet compilerSet = pSettings->compilerSets().defaultSet();
if (!compilerSet) {
QMessageBox::critical(pMainWindow,
tr("No compiler set"),
tr("No compiler set is configured.")+tr("Can't start debugging."));
2021-08-20 12:43:01 +08:00
return false;
2021-07-25 00:26:13 +08:00
}
2021-07-26 22:29:47 +08:00
mExecuting = true;
2021-07-25 00:26:13 +08:00
QString debuggerPath = compilerSet->debugger();
//QFile debuggerProgram(debuggerPath);
if (!isTextAllAscii(debuggerPath)) {
mExecuting = false;
QMessageBox::critical(pMainWindow,
tr("Debugger path error"),
tr("Debugger's path \"%1\" contains non-ascii characters.")
.arg(debuggerPath)
+ "<br />"
+ tr("This prevents it from executing."));
return false;
}
if (!fileExists(debuggerPath)) {
2021-08-20 12:43:01 +08:00
mExecuting = false;
2021-07-25 00:26:13 +08:00
QMessageBox::critical(pMainWindow,
tr("Debugger not exists"),
tr("Can''t find debugger in : \"%1\"").arg(debuggerPath));
2021-08-20 12:43:01 +08:00
return false;
2021-07-25 00:26:13 +08:00
}
if (pSettings->debugger().useGDBServer()) {
if (!isTextAllAscii(compilerSet->debugServer())) {
mExecuting = false;
QMessageBox::critical(pMainWindow,
tr("GDB Server path error"),
tr("GDB Server's path \"%1\" contains non-ascii characters.")
.arg(compilerSet->debugServer())
+ "<br />"
+ tr("This prevents it from executing."));
return false;
}
if (!fileExists(compilerSet->debugServer())) {
mExecuting = false;
QMessageBox::critical(pMainWindow,
tr("GDB Server not exists"),
tr("Can''t find gdb server in : \"%1\"").arg(compilerSet->debugServer()));
return false;
}
}
mMemoryModel->reset();
2021-11-25 20:26:43 +08:00
mWatchModel->resetAllVarInfos();
if (pSettings->debugger().useGDBServer()) {
mTarget = new DebugTarget(inferior,compilerSet->debugServer(),pSettings->debugger().GDBServerPort());
if (pSettings->executor().redirectInput())
mTarget->setInputFile(pSettings->executor().inputFilename());
connect(mTarget, &QThread::finished,[this](){
if (mExecuting) {
stop();
}
mTarget->deleteLater();
mTarget = nullptr;
});
mTarget->start();
mTarget->waitStart();
}
2021-07-26 00:22:08 +08:00
mReader = new DebugReader(this);
2021-07-25 00:26:13 +08:00
mReader->setDebuggerPath(debuggerPath);
connect(mReader, &QThread::finished,this,&Debugger::cleanUpReader);
connect(mReader, &QThread::finished,mMemoryModel,&MemoryModel::reset);
2021-07-26 11:47:54 +08:00
connect(mReader, &DebugReader::parseFinished,this,&Debugger::syncFinishedParsing,Qt::BlockingQueuedConnection);
2021-08-01 12:02:28 +08:00
connect(mReader, &DebugReader::changeDebugConsoleLastLine,this,&Debugger::onChangeDebugConsoleLastline);
connect(mReader, &DebugReader::cmdStarted,pMainWindow, &MainWindow::disableDebugActions);
connect(mReader, &DebugReader::cmdFinished,pMainWindow, &MainWindow::enableDebugActions);
2021-11-24 21:22:01 +08:00
connect(mReader, &DebugReader::inferiorStopped, pMainWindow, &MainWindow::enableDebugActions);
2021-11-23 21:08:33 +08:00
2021-11-21 08:38:03 +08:00
connect(mReader, &DebugReader::breakpointInfoGetted, mBreakpointModel,
&BreakpointModel::updateBreakpointNumber);
2021-11-24 10:07:35 +08:00
connect(mReader, &DebugReader::localsUpdated, pMainWindow,
&MainWindow::onLocalsReady);
2021-11-25 07:42:56 +08:00
connect(mReader, &DebugReader::memoryUpdated,this,
&Debugger::updateMemory);
connect(mReader, &DebugReader::evalUpdated,this,
&Debugger::updateEval);
2021-11-24 23:32:34 +08:00
connect(mReader, &DebugReader::disassemblyUpdate,this,
&Debugger::updateDisassembly);
2021-11-25 09:05:45 +08:00
connect(mReader, &DebugReader::registerNamesUpdated, this,
&Debugger::updateRegisterNames);
connect(mReader, &DebugReader::registerValuesUpdated, this,
&Debugger::updateRegisterValues);
2021-11-25 20:26:43 +08:00
connect(mReader, &DebugReader::varCreated,mWatchModel,
&WatchModel::updateVarInfo);
connect(mReader, &DebugReader::prepareVarChildren,mWatchModel,
&WatchModel::prepareVarChildren);
connect(mReader, &DebugReader::addVarChild,mWatchModel,
&WatchModel::addVarChild);
connect(mReader, &DebugReader::varValueUpdated,mWatchModel,
&WatchModel::updateVarValue);
2021-11-24 21:22:01 +08:00
connect(mReader, &DebugReader::inferiorContinued,pMainWindow,
&MainWindow::removeActiveBreakpoints);
2021-11-24 17:53:25 +08:00
connect(mReader, &DebugReader::inferiorStopped,pMainWindow,
&MainWindow::setActiveBreakpoint);
2021-11-25 20:26:43 +08:00
connect(mReader, &DebugReader::inferiorStopped,this,
&Debugger::refreshAll);
2021-11-24 21:22:01 +08:00
mReader->registerInferiorStoppedCommand("-stack-list-frames","");
2021-07-25 00:26:13 +08:00
mReader->start();
2021-11-10 17:05:37 +08:00
mReader->waitStart();
2021-07-25 00:26:13 +08:00
2021-07-26 00:22:08 +08:00
pMainWindow->updateAppTitle();
2021-07-25 00:26:13 +08:00
2021-07-25 13:03:46 +08:00
//Application.HintHidePause := 5000;
2021-08-20 12:43:01 +08:00
return true;
2021-07-25 13:03:46 +08:00
}
2021-07-31 14:04:43 +08:00
void Debugger::stop() {
if (mExecuting) {
if (mTarget) {
mTarget->stopDebug();
mTarget = nullptr;
}
2021-07-31 14:04:43 +08:00
mReader->stopDebug();
}
}
void Debugger::cleanUpReader()
2021-07-25 13:03:46 +08:00
{
if (mExecuting) {
mExecuting = false;
2021-07-26 00:22:08 +08:00
//stop debugger
mReader->deleteLater();
mReader=nullptr;
2021-07-31 14:04:43 +08:00
2021-08-01 23:24:37 +08:00
if (pMainWindow->cpuDialog()!=nullptr) {
pMainWindow->cpuDialog()->close();
}
2021-07-25 13:03:46 +08:00
// Free resources
2021-07-26 00:22:08 +08:00
pMainWindow->removeActiveBreakpoints();
2021-07-25 13:03:46 +08:00
2021-08-01 10:00:27 +08:00
pMainWindow->txtLocals()->clear();
2021-07-26 00:22:08 +08:00
pMainWindow->updateAppTitle();
2021-07-25 13:03:46 +08:00
2021-08-01 23:24:37 +08:00
pMainWindow->updateDebugEval("");
2021-07-26 11:47:54 +08:00
mBacktraceModel->clear();
2021-07-25 13:03:46 +08:00
2021-11-25 20:26:43 +08:00
mWatchModel->clearAllVarInfos();
mBreakpointModel->invalidateAllBreakpointNumbers();
2021-08-01 12:02:28 +08:00
pMainWindow->updateEditorActions();
2021-07-25 13:03:46 +08:00
}
2021-07-25 00:26:13 +08:00
}
2021-11-25 09:05:45 +08:00
void Debugger::updateRegisterNames(const QStringList &registerNames)
{
mRegisterModel->updateNames(registerNames);
}
void Debugger::updateRegisterValues(const QHash<int, QString> &values)
{
mRegisterModel->updateValues(values);
}
void Debugger::refreshAll()
{
refreshWatchVars();
sendCommand("-stack-list-variables", "--all-values");
if (memoryModel()->startAddress()>0)
sendCommand("-data-read-memory",QString("%1 x 1 8 8 ").arg(memoryModel()->startAddress()));
}
2021-08-01 23:24:37 +08:00
RegisterModel *Debugger::registerModel() const
{
return mRegisterModel;
}
2021-08-01 01:06:43 +08:00
WatchModel *Debugger::watchModel() const
{
return mWatchModel;
}
2021-11-24 17:53:25 +08:00
void Debugger::sendCommand(const QString &command, const QString &params, DebugCommandSource source)
2021-07-24 11:18:25 +08:00
{
if (mExecuting && mReader) {
2021-11-24 17:53:25 +08:00
mReader->postCommand(command,params,source);
2021-07-24 11:18:25 +08:00
}
}
2021-08-29 22:08:43 +08:00
bool Debugger::commandRunning()
{
if (mExecuting && mReader) {
return mReader->commandRunning();
}
return false;
}
2021-11-24 21:22:01 +08:00
bool Debugger::inferiorRunning()
{
if (mExecuting && mReader) {
return mReader->inferiorRunning();
}
return false;
}
void Debugger::interrupt()
{
sendCommand("-exec-interrupt", "");
}
2021-07-24 11:18:25 +08:00
void Debugger::addBreakpoint(int line, const Editor* editor)
{
addBreakpoint(line,editor->filename());
}
2021-07-03 21:57:50 +08:00
2021-07-24 11:18:25 +08:00
void Debugger::addBreakpoint(int line, const QString &filename)
{
PBreakpoint bp=std::make_shared<Breakpoint>();
2021-11-20 07:53:39 +08:00
bp->number = -1;
2021-07-24 11:18:25 +08:00
bp->line = line;
bp->filename = filename;
bp->condition = "";
bp->enabled = true;
2022-01-02 11:43:23 +08:00
bp->breakpointType = BreakpointType::Breakpoint;
2021-07-24 11:18:25 +08:00
mBreakpointModel->addBreakpoint(bp);
2021-07-25 13:03:46 +08:00
if (mExecuting) {
sendBreakpointCommand(bp);
}
2021-07-24 11:18:25 +08:00
}
void Debugger::deleteBreakpoints(const QString &filename)
{
for (int i=mBreakpointModel->breakpoints().size()-1;i>=0;i--) {
PBreakpoint bp = mBreakpointModel->breakpoints()[i];
if (bp->filename == filename) {
mBreakpointModel->removeBreakpoint(i);
}
}
}
void Debugger::deleteBreakpoints(const Editor *editor)
{
deleteBreakpoints(editor->filename());
}
void Debugger::deleteBreakpoints()
{
for (int i=mBreakpointModel->breakpoints().size()-1;i>=0;i--) {
removeBreakpoint(i);
}
}
2021-07-24 11:18:25 +08:00
void Debugger::removeBreakpoint(int line, const Editor *editor)
{
removeBreakpoint(line,editor->filename());
}
void Debugger::removeBreakpoint(int line, const QString &filename)
{
for (int i=mBreakpointModel->breakpoints().size()-1;i>=0;i--) {
PBreakpoint bp = mBreakpointModel->breakpoints()[i];
if (bp->filename == filename && bp->line == line) {
removeBreakpoint(i);
}
}
}
void Debugger::removeBreakpoint(int index)
{
sendClearBreakpointCommand(index);
mBreakpointModel->removeBreakpoint(index);
}
PBreakpoint Debugger::breakpointAt(int line, const QString& filename, int &index)
{
const QList<PBreakpoint>& breakpoints=mBreakpointModel->breakpoints();
for (index=0;index<breakpoints.count();index++){
PBreakpoint breakpoint = breakpoints[index];
if (breakpoint->line == line
&& breakpoint->filename == filename)
return breakpoint;
}
index=-1;
return PBreakpoint();
}
PBreakpoint Debugger::breakpointAt(int line, const Editor *editor, int &index)
{
return breakpointAt(line,editor->filename(),index);
}
2021-07-24 11:18:25 +08:00
void Debugger::setBreakPointCondition(int index, const QString &condition)
{
PBreakpoint breakpoint=mBreakpointModel->setBreakPointCondition(index,condition);
if (condition.isEmpty()) {
2021-11-10 12:29:02 +08:00
sendCommand("-break-condition",
QString("%1").arg(breakpoint->number));
2021-07-24 11:18:25 +08:00
} else {
2021-11-10 12:29:02 +08:00
sendCommand("-break-condition",
QString("%1 %2").arg(breakpoint->number).arg(condition));
2021-07-24 11:18:25 +08:00
}
2021-07-03 21:57:50 +08:00
}
2021-07-25 13:03:46 +08:00
void Debugger::sendAllBreakpointsToDebugger()
{
for (PBreakpoint breakpoint:mBreakpointModel->breakpoints()) {
sendBreakpointCommand(breakpoint);
}
}
2021-11-25 20:26:43 +08:00
void Debugger::addWatchVar(const QString &expression)
2021-07-26 18:22:09 +08:00
{
2021-07-31 20:19:45 +08:00
// Don't allow duplicates...
2021-11-25 20:26:43 +08:00
PWatchVar oldVar = mWatchModel->findWatchVar(expression);
2021-07-31 20:19:45 +08:00
if (oldVar)
return;
2021-07-26 18:22:09 +08:00
2021-07-31 20:19:45 +08:00
PWatchVar var = std::make_shared<WatchVar>();
var->parent= nullptr;
2021-11-25 20:26:43 +08:00
var->expression = expression;
2021-09-19 09:45:03 +08:00
var->value = tr("Execute to evaluate");
2021-11-25 11:24:38 +08:00
var->numChild = 0;
var->hasMore = false;
2021-11-25 20:26:43 +08:00
var->parent = nullptr;
2021-07-26 18:22:09 +08:00
2021-07-31 20:19:45 +08:00
mWatchModel->addWatchVar(var);
sendWatchCommand(var);
2021-07-26 18:22:09 +08:00
}
2021-11-25 20:26:43 +08:00
void Debugger::modifyWatchVarExpression(const QString &oldExpr, const QString &newExpr)
2021-07-26 18:22:09 +08:00
{
2021-07-31 20:19:45 +08:00
// check if name already exists;
2021-11-25 20:26:43 +08:00
PWatchVar var = mWatchModel->findWatchVar(newExpr);
2021-07-31 20:19:45 +08:00
if (var)
return;
2021-11-25 20:26:43 +08:00
var = mWatchModel->findWatchVar(oldExpr);
2021-07-31 20:19:45 +08:00
if (var) {
2021-11-25 20:26:43 +08:00
if (mExecuting && !var->expression.isEmpty())
2021-08-01 01:06:43 +08:00
sendRemoveWatchCommand(var);
2021-11-25 20:26:43 +08:00
var->expression = newExpr;
var->type.clear();
var->value.clear();
var->hasMore = false;
var->numChild=0;
var->name.clear();
var->children.clear();
2021-07-31 20:19:45 +08:00
if (mExecuting) {
sendWatchCommand(var);
}
}
2021-07-26 18:22:09 +08:00
}
void Debugger::refreshWatchVars()
{
2021-11-25 20:26:43 +08:00
if (mExecuting) {
sendAllWatchVarsToDebugger();
sendCommand("-var-update"," --all-values *");
}
}
void Debugger::fetchVarChildren(const QString &varName)
{
if (mExecuting) {
sendCommand("-var-list-children",varName);
2021-07-31 20:19:45 +08:00
}
2021-07-26 18:22:09 +08:00
}
MemoryModel *Debugger::memoryModel() const
{
return mMemoryModel;
}
void Debugger::removeWatchVars(bool deleteparent)
2021-07-26 18:22:09 +08:00
{
2021-07-31 20:19:45 +08:00
if (deleteparent) {
mWatchModel->clear();
} else {
for(const PWatchVar& var:mWatchModel->watchVars()) {
2021-07-31 20:19:45 +08:00
sendRemoveWatchCommand(var);
}
2021-11-25 20:26:43 +08:00
mWatchModel->clearAllVarInfos();
2021-07-31 20:19:45 +08:00
}
2021-07-26 18:22:09 +08:00
}
void Debugger::removeWatchVar(const QModelIndex &index)
{
2021-11-25 20:26:43 +08:00
PWatchVar var = mWatchModel->findWatchVar(index);
if (!var)
return;
sendRemoveWatchCommand(var);
mWatchModel->removeWatchVar(index);
}
2021-11-25 20:26:43 +08:00
void Debugger::sendAllWatchVarsToDebugger()
2021-07-26 18:22:09 +08:00
{
2021-07-31 20:19:45 +08:00
for (PWatchVar var:mWatchModel->watchVars()) {
2021-11-25 20:26:43 +08:00
if (var->name.isEmpty())
sendWatchCommand(var);
}
2021-08-01 01:06:43 +08:00
}
2021-11-25 20:26:43 +08:00
PWatchVar Debugger::findWatchVar(const QString &expression)
2021-08-01 01:06:43 +08:00
{
2021-11-25 20:26:43 +08:00
return mWatchModel->findWatchVar(expression);
2021-08-01 01:06:43 +08:00
}
2021-09-19 02:00:25 +08:00
//void Debugger::notifyWatchVarUpdated(PWatchVar var)
//{
// mWatchModel->notifyUpdated(var);
//}
2021-07-25 00:26:13 +08:00
BacktraceModel* Debugger::backtraceModel()
2021-07-24 08:12:51 +08:00
{
return mBacktraceModel;
}
2021-07-25 00:26:13 +08:00
BreakpointModel *Debugger::breakpointModel()
2021-07-24 11:18:25 +08:00
{
return mBreakpointModel;
}
2021-07-31 20:19:45 +08:00
void Debugger::sendWatchCommand(PWatchVar var)
2021-07-24 11:18:25 +08:00
{
2021-11-25 20:26:43 +08:00
sendCommand("-var-create", var->expression);
2021-07-31 20:19:45 +08:00
}
void Debugger::sendRemoveWatchCommand(PWatchVar var)
{
2021-11-25 11:24:38 +08:00
sendCommand("-var-delete",QString("%1").arg(var->name));
2021-07-25 13:03:46 +08:00
}
void Debugger::sendBreakpointCommand(PBreakpoint breakpoint)
{
if (breakpoint && mExecuting) {
// break "filename":linenum
QString condition;
if (!breakpoint->condition.isEmpty()) {
2021-11-10 12:29:02 +08:00
condition = " -c " + breakpoint->condition;
2021-07-25 13:03:46 +08:00
}
QString filename = breakpoint->filename;
filename.replace('\\','/');
2021-11-10 12:29:02 +08:00
sendCommand("-break-insert",
QString("%1 --source \"%2\" --line %3")
.arg(condition,filename)
.arg(breakpoint->line));
2021-07-24 11:18:25 +08:00
}
}
void Debugger::sendClearBreakpointCommand(int index)
2021-07-25 13:03:46 +08:00
{
2021-07-26 11:47:54 +08:00
sendClearBreakpointCommand(mBreakpointModel->breakpoints()[index]);
2021-07-25 13:03:46 +08:00
}
void Debugger::sendClearBreakpointCommand(PBreakpoint breakpoint)
2021-07-24 11:18:25 +08:00
{
// Debugger already running? Remove it from GDB
2021-11-20 07:53:39 +08:00
if (breakpoint && breakpoint->number>=0 && mExecuting) {
2021-07-24 11:18:25 +08:00
//clear "filename":linenum
QString filename = breakpoint->filename;
filename.replace('\\','/');
2021-11-20 07:53:39 +08:00
sendCommand("-break-delete",
QString("%1").arg(breakpoint->number));
2021-07-24 11:18:25 +08:00
}
}
2021-07-26 11:47:54 +08:00
void Debugger::syncFinishedParsing()
{
bool spawnedcpuform = false;
// GDB determined that the source code is more recent than the executable. Ask the user if he wants to rebuild.
2021-11-24 10:07:35 +08:00
if (mReader->receivedSFWarning()) {
2021-07-26 11:47:54 +08:00
if (QMessageBox::question(pMainWindow,
tr("Compile"),
tr("Source file is more recent than executable.")+"<BR /><BR />" + tr("Recompile?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes
) == QMessageBox::Yes) {
stop();
pMainWindow->compile();
return;
}
}
// show command output
if (pSettings->debugger().enableDebugConsole() ) {
if (pSettings->debugger().showDetailLog()) {
2021-11-24 17:53:25 +08:00
for (const QString& line:mReader->fullOutput()) {
pMainWindow->addDebugOutput(line);
2021-07-26 11:47:54 +08:00
}
2021-11-24 17:53:25 +08:00
} else {
if (mReader->currentCmd() && mReader->currentCmd()->command == "disas") {
} else {
for (const QString& line:mReader->consoleOutput()) {
pMainWindow->addDebugOutput(line);
}
if (
(mReader->currentCmd()
&& mReader->currentCmd()->source== DebugCommandSource::Console)
|| !mReader->consoleOutput().isEmpty() ) {
pMainWindow->addDebugOutput("(gdb)");
}
2021-07-26 11:47:54 +08:00
}
}
}
2021-11-24 21:22:01 +08:00
// The program to debug has stopped. Stop the debugger
if (mReader->processExited()) {
stop();
return;
}
if (mReader->signalReceived()
&& mReader->signalName()!="SIGINT"
&& mReader->signalName()!="SIGTRAP") {
2021-11-25 09:05:45 +08:00
SignalMessageDialog dialog(pMainWindow);
dialog.setOpenCPUInfo(pSettings->debugger().openCPUInfoWhenSignaled());
2021-11-25 09:05:45 +08:00
dialog.setMessage(
tr("Signal \"%1\" Received: ").arg(mReader->signalName())
+ "<br />"
+ mReader->signalMeaning());
int result = dialog.exec();
if (result == QDialog::Accepted && dialog.openCPUInfo()) {
pMainWindow->showCPUInfoDialog();
}
2021-07-26 11:47:54 +08:00
}
// CPU form updates itself when spawned, don't update twice!
2021-11-23 21:08:33 +08:00
if ((mReader->updateCPUInfo() && !spawnedcpuform) && (pMainWindow->cpuDialog()!=nullptr)) {
2021-08-01 23:24:37 +08:00
pMainWindow->cpuDialog()->updateInfo();
2021-07-26 11:47:54 +08:00
}
}
void Debugger::setMemoryData(qulonglong address, unsigned char data)
{
sendCommand("-data-write-memory-bytes", QString("%1 \"%2\"").arg(address).arg(data,2,16,QChar('0')));
refreshAll();
}
void Debugger::setWatchVarValue(const QString &name, const QString &value)
{
sendCommand("-var-assign",QString("%1 %2").arg(name,value));
refreshAll();
}
2021-11-25 07:42:56 +08:00
void Debugger::updateMemory(const QStringList &value)
{
mMemoryModel->updateMemory(value);
2021-11-25 07:42:56 +08:00
emit memoryExamineReady(value);
}
void Debugger::updateEval(const QString &value)
{
emit evalValueReady(value);
}
void Debugger::updateDisassembly(const QString& file, const QString& func, const QStringList &value)
2021-11-24 23:32:34 +08:00
{
if (pMainWindow->cpuDialog()) {
2021-11-25 07:42:56 +08:00
pMainWindow->cpuDialog()->setDisassembly(file,func,value);
2021-11-24 23:32:34 +08:00
}
}
2021-11-24 10:07:35 +08:00
void Debugger::onChangeDebugConsoleLastline(const QString& text)
2021-07-30 23:28:58 +08:00
{
//pMainWindow->changeDebugOutputLastline(text);
pMainWindow->addDebugOutput(text);
}
2021-07-26 18:22:09 +08:00
int Debugger::leftPageIndexBackup() const
{
return mLeftPageIndexBackup;
}
void Debugger::setLeftPageIndexBackup(int leftPageIndexBackup)
{
mLeftPageIndexBackup = leftPageIndexBackup;
}
2021-07-26 11:47:54 +08:00
bool Debugger::executing() const
{
return mExecuting;
}
2021-07-27 00:14:24 +08:00
DebugReader::DebugReader(Debugger* debugger, QObject *parent) : QThread(parent),
2022-01-04 16:50:54 +08:00
mCmdQueueMutex(QMutex::Recursive),
2021-07-27 00:14:24 +08:00
mStartSemaphore(0)
2021-07-03 21:57:50 +08:00
{
2021-07-25 00:26:13 +08:00
mDebugger = debugger;
2021-12-25 20:02:53 +08:00
mProcess = std::make_shared<QProcess>();
2021-07-31 20:19:45 +08:00
mCmdRunning = false;
mAsyncUpdated = false;
2021-07-03 21:57:50 +08:00
}
2021-11-24 17:53:25 +08:00
void DebugReader::postCommand(const QString &Command, const QString &Params,
DebugCommandSource Source)
2021-07-19 23:02:32 +08:00
{
QMutexLocker locker(&mCmdQueueMutex);
PDebugCommand pCmd = std::make_shared<DebugCommand>();
pCmd->command = Command;
pCmd->params = Params;
pCmd->source = Source;
mCmdQueue.enqueue(pCmd);
2021-07-30 23:28:58 +08:00
// if (!mCmdRunning)
2021-11-24 17:53:25 +08:00
// runNextCmd();
2021-07-19 23:02:32 +08:00
}
2021-11-24 17:53:25 +08:00
void DebugReader::registerInferiorStoppedCommand(const QString &Command, const QString &Params)
2021-07-03 21:57:50 +08:00
{
2021-07-19 23:02:32 +08:00
QMutexLocker locker(&mCmdQueueMutex);
2021-11-24 17:53:25 +08:00
PDebugCommand pCmd = std::make_shared<DebugCommand>();
pCmd->command = Command;
pCmd->params = Params;
pCmd->source = DebugCommandSource::Other;
mInferiorStoppedHookCommands.append(pCmd);
2021-07-18 00:18:07 +08:00
}
2021-11-24 17:53:25 +08:00
void DebugReader::clearCmdQueue()
2021-07-18 00:18:07 +08:00
{
2021-11-24 17:53:25 +08:00
QMutexLocker locker(&mCmdQueueMutex);
mCmdQueue.clear();
2021-07-18 00:18:07 +08:00
}
2021-11-12 10:51:00 +08:00
void DebugReader::processConsoleOutput(const QByteArray& line)
2021-07-18 00:18:07 +08:00
{
2021-11-10 17:05:37 +08:00
if (line.length()>3 && line.startsWith("~\"") && line.endsWith("\"")) {
2021-11-24 23:32:34 +08:00
QByteArray s=line.mid(2,line.length()-3);
QByteArray stringValue;
const char *p=s.data();
while (*p!=0) {
if (*p=='\\' && *(p+1)!=0) {
p++;
switch (*p) {
case '\'':
stringValue+=0x27;
p++;
break;
case '"':
stringValue+=0x22;
p++;
break;
case '?':
stringValue+=0x3f;
p++;
break;
case '\\':
stringValue+=0x5c;
p++;
break;
case 'a':
stringValue+=0x07;
p++;
break;
case 'b':
stringValue+=0x08;
p++;
break;
case 'f':
stringValue+=0x0c;
p++;
break;
case 'n':
stringValue+=0x0a;
p++;
break;
case 'r':
stringValue+=0x0d;
p++;
break;
case 't':
stringValue+=0x09;
p++;
break;
case 'v':
stringValue+=0x0b;
p++;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
{
int i=0;
for (i=0;i<3;i++) {
if (*(p+i)<'0' || *(p+i)>'7')
break;
}
QByteArray numStr(p,i);
bool ok;
unsigned char ch = numStr.toInt(&ok,8);
stringValue+=ch;
p+=i;
break;
}
}
} else {
stringValue+=*p;
p++;
}
}
mConsoleOutput.append(QString::fromLocal8Bit(stringValue));
2021-11-10 17:05:37 +08:00
}
}
2021-11-12 10:51:00 +08:00
void DebugReader::processResult(const QByteArray &result)
2021-11-10 17:05:37 +08:00
{
2021-11-20 07:53:39 +08:00
GDBMIResultParser parser;
GDBMIResultType resultType;
GDBMIResultParser::ParseObject multiValues;
if (!mCurrentCmd)
return;
bool parseOk = parser.parse(result, mCurrentCmd->command, resultType,multiValues);
2021-11-20 07:53:39 +08:00
if (!parseOk)
return;
switch(resultType) {
2021-11-23 21:08:33 +08:00
case GDBMIResultType::BreakpointTable:
case GDBMIResultType::Frame:
case GDBMIResultType::Locals:
break;
2021-11-20 07:53:39 +08:00
case GDBMIResultType::Breakpoint:
handleBreakpoint(multiValues["bkpt"].object());
2021-11-20 07:53:39 +08:00
return;
2021-11-23 21:08:33 +08:00
case GDBMIResultType::FrameStack:
handleStack(multiValues["stack"].array());
2021-11-23 21:08:33 +08:00
return;
case GDBMIResultType::LocalVariables:
handleLocalVariables(multiValues["variables"].array());
2021-11-23 21:08:33 +08:00
return;
case GDBMIResultType::Evaluation:
handleEvaluation(multiValues["value"].value());
return;
case GDBMIResultType::Memory:
handleMemory(multiValues["memory"].array());
2021-11-23 21:08:33 +08:00
return;
2021-11-25 09:05:45 +08:00
case GDBMIResultType::RegisterNames:
handleRegisterNames(multiValues["register-names"].array());
return;
case GDBMIResultType::RegisterValues:
handleRegisterValue(multiValues["register-values"].array());
return;
2021-11-25 20:26:43 +08:00
case GDBMIResultType::CreateVar:
handleCreateVar(multiValues);
return;
case GDBMIResultType::ListVarChildren:
handleListVarChildren(multiValues);
return;
case GDBMIResultType::UpdateVarValue:
handleUpdateVarValue(multiValues["changelist"].array());
return;
2021-11-20 07:53:39 +08:00
}
2021-11-10 17:05:37 +08:00
}
2021-11-21 10:36:50 +08:00
void DebugReader::processExecAsyncRecord(const QByteArray &line)
2021-11-10 17:05:37 +08:00
{
2021-11-21 10:36:50 +08:00
QByteArray result;
GDBMIResultParser::ParseObject multiValues;
GDBMIResultParser parser;
if (!parser.parseAsyncResult(line,result,multiValues))
return;
if (result == "running") {
2021-11-24 10:07:35 +08:00
mInferiorRunning = true;
2021-11-24 17:53:25 +08:00
mCurrentAddress=0;
mCurrentFile.clear();
mCurrentLine=-1;
2021-11-25 07:42:56 +08:00
mCurrentFunc.clear();
2021-11-24 10:07:35 +08:00
emit inferiorContinued();
2021-11-10 17:05:37 +08:00
return;
}
2021-11-24 17:53:25 +08:00
if (result == "stopped") {
2021-11-24 10:07:35 +08:00
mInferiorRunning = false;
2021-11-21 10:36:50 +08:00
QByteArray reason = multiValues["reason"].value();
if (reason == "exited") {
//inferior exited, gdb should terminate too
mProcessExited = true;
2021-11-10 17:05:37 +08:00
return;
2021-11-21 10:36:50 +08:00
}
if (reason == "exited-normally") {
2021-11-10 17:05:37 +08:00
//inferior exited, gdb should terminate too
mProcessExited = true;
return;
}
2021-11-24 10:07:35 +08:00
if (reason == "exited-signalled") {
//inferior exited, gdb should terminate too
mProcessExited = true;
mSignalReceived = true;
return;
}
2021-11-23 21:08:33 +08:00
mUpdateCPUInfo = true;
2021-11-24 17:53:25 +08:00
GDBMIResultParser::ParseValue frame(multiValues["frame"]);
if (frame.isValid()) {
GDBMIResultParser::ParseObject frameObj = frame.object();
mCurrentAddress = frameObj["addr"].hexValue();
mCurrentLine = frameObj["line"].intValue();
mCurrentFile = frameObj["fullname"].pathValue();
2021-11-25 07:42:56 +08:00
mCurrentFunc = frameObj["func"].value();
2021-11-24 17:53:25 +08:00
}
2021-11-21 10:36:50 +08:00
if (reason == "signal-received") {
2021-11-23 21:08:33 +08:00
mSignalReceived = true;
2021-11-25 09:05:45 +08:00
mSignalName = multiValues["signal-name"].value();
mSignalMeaning = multiValues["signal-meaning"].value();
2021-11-10 17:05:37 +08:00
}
2021-11-24 17:53:25 +08:00
runInferiorStoppedHook();
if (mCurrentCmd && mCurrentCmd->source == DebugCommandSource::Console)
emit inferiorStopped(mCurrentFile, mCurrentLine,false);
else
emit inferiorStopped(mCurrentFile, mCurrentLine,true);
2021-11-10 17:05:37 +08:00
}
}
2021-11-12 10:51:00 +08:00
void DebugReader::processError(const QByteArray &errorLine)
2021-11-10 17:05:37 +08:00
{
2021-11-12 10:51:00 +08:00
mConsoleOutput.append(QString::fromLocal8Bit(errorLine));
2021-07-18 00:18:07 +08:00
}
2021-11-12 10:51:00 +08:00
void DebugReader::processResultRecord(const QByteArray &line)
2021-11-10 12:57:18 +08:00
{
if (line.startsWith("^exit")) {
2021-11-10 17:05:37 +08:00
mProcessExited = true;
return;
}
if (line.startsWith("^error")) {
processError(line);
return;
}
if (line.startsWith("^done")
|| line.startsWith("^running")) {
int pos = line.indexOf(',');
if (pos>=0) {
2021-11-12 10:51:00 +08:00
QByteArray result = line.mid(pos+1);
2021-11-10 17:05:37 +08:00
processResult(result);
2021-11-24 23:32:34 +08:00
} else if (mCurrentCmd && !(mCurrentCmd->command.startsWith('-'))) {
if (mCurrentCmd->command == "disas") {
2021-11-25 07:42:56 +08:00
QStringList disOutput = mConsoleOutput;
if (disOutput.length()>=3) {
2021-11-24 23:32:34 +08:00
disOutput.pop_back();
disOutput.pop_front();
2021-11-25 07:42:56 +08:00
disOutput.pop_front();
2021-11-24 23:32:34 +08:00
}
2021-11-25 07:42:56 +08:00
emit disassemblyUpdate(mCurrentFile,mCurrentFunc, disOutput);
2021-11-24 23:32:34 +08:00
}
2021-11-10 17:05:37 +08:00
}
return ;
}
if (line.startsWith("^connected")) {
//TODO: connected to remote target
2021-11-10 12:57:18 +08:00
return;
}
}
2021-11-12 10:51:00 +08:00
void DebugReader::processDebugOutput(const QByteArray& debugOutput)
2021-07-18 00:18:07 +08:00
{
// Only update once per update at most
2021-07-18 08:52:53 +08:00
//WatchView.Items.BeginUpdate;
emit parseStarted();
2021-11-10 17:05:37 +08:00
mConsoleOutput.clear();
2021-11-24 17:53:25 +08:00
mFullOutput.clear();
2021-11-10 17:05:37 +08:00
2021-11-23 21:08:33 +08:00
mSignalReceived = false;
mUpdateCPUInfo = false;
2021-11-24 10:07:35 +08:00
mReceivedSFWarning = false;
2021-11-24 23:32:34 +08:00
QList<QByteArray> lines = splitByteArrayToLines(debugOutput);
for (int i=0;i<lines.count();i++) {
QByteArray line = lines[i];
if (pSettings->debugger().showDetailLog())
mFullOutput.append(line);
2021-11-24 23:32:34 +08:00
line = removeToken(line);
if (line.isEmpty()) {
continue;
}
switch (line[0]) {
case '~': // console stream output
processConsoleOutput(line);
break;
case '@': // target stream output
case '&': // log stream output
break;
case '^': // result record
processResultRecord(line);
break;
case '*': // exec async output
processExecAsyncRecord(line);
break;
case '+': // status async output
case '=': // notify async output
break;
}
}
emit parseFinished();
mConsoleOutput.clear();
mFullOutput.clear();
2021-07-18 08:52:53 +08:00
}
2021-11-24 17:53:25 +08:00
void DebugReader::runInferiorStoppedHook()
{
foreach (const PDebugCommand& cmd, mInferiorStoppedHookCommands) {
mCmdQueue.push_front(cmd);
}
}
2021-07-19 23:02:32 +08:00
void DebugReader::runNextCmd()
{
QMutexLocker locker(&mCmdQueueMutex);
if (mCurrentCmd) {
DebugCommandSource commandSource = mCurrentCmd->source;
mCurrentCmd=nullptr;
if (commandSource!=DebugCommandSource::HeartBeat)
emit cmdFinished();
2021-07-19 23:02:32 +08:00
}
if (mCmdQueue.isEmpty()) {
if (pSettings->debugger().useGDBServer() && mInferiorRunning && !mAsyncUpdated) {
mAsyncUpdated = true;
QTimer::singleShot(50,this,&DebugReader::asyncUpdate);
}
2021-11-24 17:53:25 +08:00
return;
}
2021-07-19 23:02:32 +08:00
PDebugCommand pCmd = mCmdQueue.dequeue();
mCmdRunning = true;
mCurrentCmd = pCmd;
if (pCmd->source!=DebugCommandSource::HeartBeat)
emit cmdStarted();
2021-07-19 23:02:32 +08:00
QByteArray s;
2021-11-25 20:26:43 +08:00
QByteArray params;
s=pCmd->command.toLocal8Bit();
2021-07-19 23:02:32 +08:00
if (!pCmd->params.isEmpty()) {
2021-11-25 20:26:43 +08:00
params = pCmd->params.toLocal8Bit();
}
if (pCmd->command == "-var-create") {
//hack for variable creation,to easy remember var expression
params = " - @ "+params;
} else if (pCmd->command == "-var-list-children") {
//hack for list variable children,to easy remember var expression
params = " --all-values \"" + params+'\"';
2021-07-19 23:02:32 +08:00
}
2021-11-25 20:26:43 +08:00
s+=" "+params;
2021-07-19 23:02:32 +08:00
s+= "\n";
2021-07-27 00:14:24 +08:00
if (mProcess->write(s)<0) {
2021-07-19 23:02:32 +08:00
emit writeToDebugFailed();
}
// if devDebugger.ShowCommandLog or pCmd^.ShowInConsole then begin
if (pSettings->debugger().enableDebugConsole() ) {
2021-07-19 23:02:32 +08:00
//update debug console
if (pSettings->debugger().showDetailLog()
&& pCmd->source != DebugCommandSource::Console) {
2021-11-25 20:26:43 +08:00
emit changeDebugConsoleLastLine(pCmd->command + ' ' + params);
2021-07-19 23:02:32 +08:00
}
}
}
2021-09-19 09:45:03 +08:00
QStringList DebugReader::tokenize(const QString &s)
{
QStringList result;
int tStart,tEnd;
int i=0;
while (i<s.length()) {
QChar ch = s[i];
if (ch == ' ' || ch == '\t'
|| ch == '\r'
|| ch == '\n') {
// if (!current.isEmpty()) {
// result.append(current);
// current = "";
// }
i++;
continue;
} else if (ch == '\'') {
tStart = i;
i++; //skip \'
while (i<s.length()) {
if (s[i]=='\'') {
i++;
break;
} else if (s[i] == '\\') {
i+=2;
continue;
2021-09-19 09:45:03 +08:00
}
i++;
}
tEnd = std::min(i,s.length());
result.append(s.mid(tStart,tEnd-tStart));
} else if (ch == '\"') {
2021-09-19 09:45:03 +08:00
tStart = i;
i++; //skip \'
while (i<s.length()) {
if (s[i]=='\"') {
i++;
break;
} else if (s[i] == '\\') {
i+=2;
continue;
}
i++;
}
tEnd = std::min(i,s.length());
result.append(s.mid(tStart,tEnd-tStart));
} else if (ch == '<') {
tStart = i;
i++;
while (i<s.length()) {
if (s[i]=='>') {
i++;
2021-09-19 09:45:03 +08:00
break;
}
i++;
}
tEnd = std::min(i,s.length());
result.append(s.mid(tStart,tEnd-tStart));
} else if (ch == '(') {
tStart = i;
i++;
while (i<s.length()) {
if (s[i]==')') {
i++;
break;
}
i++;
}
tEnd = std::min(i,s.length());
result.append(s.mid(tStart,tEnd-tStart));
} else if (ch == '_' ||
ch == '.' ||
ch == '+' ||
ch == '-' ||
ch.isLetterOrNumber() ) {
2021-09-19 09:45:03 +08:00
tStart = i;
while (i<s.length()) {
ch = s[i];
if (!(ch == '_' ||
ch == '.' ||
ch == '+' ||
ch == '-' ||
ch.isLetterOrNumber() ))
2021-09-19 09:45:03 +08:00
break;
i++;
}
tEnd = std::min(i,s.length());
result.append(s.mid(tStart,tEnd-tStart));
} else {
result.append(s[i]);
i++;
}
}
return result;
}
2021-11-24 17:53:25 +08:00
bool DebugReader::outputTerminated(const QByteArray &text)
{
QStringList lines = textToLines(QString::fromUtf8(text));
foreach (const QString& line,lines) {
if (line.trimmed() == "(gdb)")
return true;
}
return false;
}
2021-11-21 08:38:03 +08:00
void DebugReader::handleBreakpoint(const GDBMIResultParser::ParseObject& breakpoint)
2021-11-12 10:51:00 +08:00
{
2021-11-21 08:38:03 +08:00
// gdb use system encoding for file path
QString filename = breakpoint["fullname"].pathValue();
2021-11-21 08:38:03 +08:00
int line = breakpoint["line"].intValue();
int number = breakpoint["number"].intValue();
emit breakpointInfoGetted(filename, line , number);
2021-11-12 10:51:00 +08:00
}
2021-11-23 21:08:33 +08:00
void DebugReader::handleStack(const QList<GDBMIResultParser::ParseValue> & stack)
{
mDebugger->backtraceModel()->clear();
foreach (const GDBMIResultParser::ParseValue& frameValue, stack) {
GDBMIResultParser::ParseObject frameObject = frameValue.object();
PTrace trace = std::make_shared<Trace>();
trace->funcname = frameObject["func"].value();
trace->filename = frameObject["fullname"].pathValue();
2021-11-24 21:22:01 +08:00
trace->line = frameObject["line"].intValue();
2021-11-23 21:08:33 +08:00
trace->level = frameObject["level"].intValue(0);
trace->address = frameObject["addr"].value();
mDebugger->backtraceModel()->addTrace(trace);
}
}
void DebugReader::handleLocalVariables(const QList<GDBMIResultParser::ParseValue> &variables)
{
2021-11-24 10:07:35 +08:00
QStringList locals;
2021-11-23 21:08:33 +08:00
foreach (const GDBMIResultParser::ParseValue& varValue, variables) {
GDBMIResultParser::ParseObject varObject = varValue.object();
2022-01-04 16:50:54 +08:00
locals.append(
QString("%1 = %2")
.arg(
QString(varObject["name"].value()),
QString(varObject["value"].value())
));
2021-11-23 21:08:33 +08:00
}
2021-11-24 10:07:35 +08:00
emit localsUpdated(locals);
2021-11-23 21:08:33 +08:00
}
void DebugReader::handleEvaluation(const QString &value)
{
2021-11-24 10:07:35 +08:00
emit evalUpdated(value);
2021-11-23 21:08:33 +08:00
}
void DebugReader::handleMemory(const QList<GDBMIResultParser::ParseValue> &rows)
{
2021-11-24 10:07:35 +08:00
QStringList memory;
2021-11-23 21:08:33 +08:00
foreach (const GDBMIResultParser::ParseValue& row, rows) {
GDBMIResultParser::ParseObject rowObject = row.object();
QList<GDBMIResultParser::ParseValue> data = rowObject["data"].array();
QStringList values;
foreach (const GDBMIResultParser::ParseValue& val, data) {
values.append(val.value());
}
2021-11-24 10:07:35 +08:00
memory.append(QString("%1 %2")
2021-11-23 21:08:33 +08:00
.arg(rowObject["addr"].value(),values.join(" ")));
}
2021-11-24 10:07:35 +08:00
emit memoryUpdated(memory);
2021-11-23 21:08:33 +08:00
}
2021-11-25 09:05:45 +08:00
void DebugReader::handleRegisterNames(const QList<GDBMIResultParser::ParseValue> &names)
{
QStringList nameList;
foreach (const GDBMIResultParser::ParseValue& nameValue, names) {
nameList.append(nameValue.value());
}
emit registerNamesUpdated(nameList);
}
void DebugReader::handleRegisterValue(const QList<GDBMIResultParser::ParseValue> &values)
{
QHash<int,QString> result;
foreach (const GDBMIResultParser::ParseValue& val, values) {
GDBMIResultParser::ParseObject obj = val.object();
int number = obj["number"].intValue();
QString value = obj["value"].value();
bool ok;
long long intVal;
intVal = value.toLongLong(&ok,10);
if (ok) {
value = QString("0x%1").arg(intVal,0,16);
}
result.insert(number,value);
}
emit registerValuesUpdated(result);
}
2021-11-25 20:26:43 +08:00
void DebugReader::handleCreateVar(const GDBMIResultParser::ParseObject &multiVars)
{
if (!mCurrentCmd)
return;
QString expression = mCurrentCmd->params;
QString name = multiVars["name"].value();
int numChild = multiVars["numchild"].intValue(0);
QString value = multiVars["value"].value();
QString type = multiVars["type"].value();
bool hasMore = multiVars["has_more"].value() != "0";
emit varCreated(expression,name,numChild,value,type,hasMore);
}
void DebugReader::handleListVarChildren(const GDBMIResultParser::ParseObject &multiVars)
{
if (!mCurrentCmd)
return;
QString parentName = mCurrentCmd->params;
int parentNumChild = multiVars["numchild"].intValue(0);
QList<GDBMIResultParser::ParseValue> children = multiVars["children"].array();
bool hasMore = multiVars["has_more"].value()!="0";
emit prepareVarChildren(parentName,parentNumChild,hasMore);
foreach(const GDBMIResultParser::ParseValue& child, children) {
GDBMIResultParser::ParseObject childObj = child.object();
QString name = childObj["name"].value();
QString exp = childObj["exp"].value();
int numChild = childObj["numchild"].intValue(0);
QString value = childObj["value"].value();
QString type = childObj["type"].value();
bool hasMore = childObj["has_more"].value() != "0";
emit addVarChild(parentName,
name,
exp,
numChild,
value,
type,
hasMore);
}
}
void DebugReader::handleUpdateVarValue(const QList<GDBMIResultParser::ParseValue> &changes)
{
foreach (const GDBMIResultParser::ParseValue& value, changes) {
GDBMIResultParser::ParseObject obj = value.object();
QString name = obj["name"].value();
QString val = obj["value"].value();
QString inScope = obj["in_scope"].value();
bool typeChanged = (obj["type_changed"].value()=="true");
QString newType = obj["new_type"].value();
int newNumChildren = obj["new_num_children"].intValue(-1);
bool hasMore = (obj["has_more"].value() == "1");
emit varValueUpdated(name,val,inScope,typeChanged,newType,newNumChildren,
hasMore);
}
}
2021-11-12 10:51:00 +08:00
QByteArray DebugReader::removeToken(const QByteArray &line)
2021-11-10 17:05:37 +08:00
{
int p=0;
while (p<line.length()) {
2021-11-10 22:00:01 +08:00
QChar ch=line[p];
if (ch<'0' || ch>'9') {
break;
}
p++;
2021-11-10 17:05:37 +08:00
}
2021-11-10 22:00:01 +08:00
if (p<line.length())
return line.mid(p);
return line;
2021-11-10 17:05:37 +08:00
}
void DebugReader::asyncUpdate()
{
QMutexLocker locker(&mCmdQueueMutex);
if (mCmdQueue.isEmpty())
postCommand("-var-update"," --all-values *",DebugCommandSource::HeartBeat);
mAsyncUpdated = false;
}
2021-11-25 09:05:45 +08:00
const QString &DebugReader::signalMeaning() const
{
return mSignalMeaning;
}
const QString &DebugReader::signalName() const
{
return mSignalName;
}
2021-11-24 21:22:01 +08:00
bool DebugReader::inferiorRunning() const
{
return mInferiorRunning;
}
2021-11-24 17:53:25 +08:00
const QStringList &DebugReader::fullOutput() const
2021-11-23 21:08:33 +08:00
{
2021-11-24 17:53:25 +08:00
return mFullOutput;
2021-11-23 21:08:33 +08:00
}
2021-11-24 17:53:25 +08:00
bool DebugReader::receivedSFWarning() const
2021-11-23 21:08:33 +08:00
{
2021-11-24 17:53:25 +08:00
return mReceivedSFWarning;
2021-11-23 21:08:33 +08:00
}
bool DebugReader::updateCPUInfo() const
{
return mUpdateCPUInfo;
}
const PDebugCommand &DebugReader::currentCmd() const
{
return mCurrentCmd;
}
const QStringList &DebugReader::consoleOutput() const
{
return mConsoleOutput;
}
bool DebugReader::signalReceived() const
{
return mSignalReceived;
}
2021-11-10 17:05:37 +08:00
bool DebugReader::processExited() const
{
return mProcessExited;
}
2021-07-25 00:26:13 +08:00
QString DebugReader::debuggerPath() const
{
return mDebuggerPath;
}
void DebugReader::setDebuggerPath(const QString &debuggerPath)
{
mDebuggerPath = debuggerPath;
}
void DebugReader::stopDebug()
{
mStop = true;
}
2021-08-29 22:08:43 +08:00
bool DebugReader::commandRunning()
{
return !mCmdQueue.isEmpty();
}
2021-11-24 17:53:25 +08:00
void DebugReader::waitStart()
2021-11-10 17:05:37 +08:00
{
mStartSemaphore.acquire(1);
}
2021-07-19 23:02:32 +08:00
void DebugReader::run()
{
mStop = false;
2021-11-24 10:07:35 +08:00
mInferiorRunning = false;
2021-11-10 17:05:37 +08:00
mProcessExited = false;
2021-12-25 20:02:53 +08:00
mErrorOccured = false;
2021-07-25 00:26:13 +08:00
QString cmd = mDebuggerPath;
// QString arguments = "--annotate=2";
2021-11-10 12:29:02 +08:00
QString arguments = "--interpret=mi --silent";
2021-07-25 00:26:13 +08:00
QString workingDir = QFileInfo(mDebuggerPath).path();
mProcess = std::make_shared<QProcess>();
2021-12-25 20:02:53 +08:00
auto action = finally([&]{
mProcess.reset();
});
2021-07-27 00:14:24 +08:00
mProcess->setProgram(cmd);
2022-01-04 16:50:54 +08:00
mProcess->setArguments(splitProcessCommand(arguments));
mProcess->setProcessChannelMode(QProcess::MergedChannels);
QString cmdDir = extractFileDir(cmd);
if (!cmdDir.isEmpty()) {
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString path = env.value("PATH");
cmdDir.replace("/",QDir::separator());
if (path.isEmpty()) {
path = cmdDir;
} else {
path = cmdDir + PATH_SEPARATOR + path;
}
env.insert("PATH",path);
mProcess->setProcessEnvironment(env);
}
2021-07-27 00:14:24 +08:00
mProcess->setWorkingDirectory(workingDir);
2021-07-19 23:02:32 +08:00
connect(mProcess.get(), &QProcess::errorOccurred,
2021-07-19 23:02:32 +08:00
[&](){
2021-12-25 20:02:53 +08:00
mErrorOccured= true;
2021-07-19 23:02:32 +08:00
});
QByteArray buffer;
QByteArray readed;
2021-07-27 00:14:24 +08:00
mProcess->start();
mProcess->waitForStarted(5000);
mStartSemaphore.release(1);
2021-07-19 23:02:32 +08:00
while (true) {
2021-08-01 01:06:43 +08:00
mProcess->waitForFinished(1);
2021-07-27 00:14:24 +08:00
if (mProcess->state()!=QProcess::Running) {
2021-07-19 23:02:32 +08:00
break;
}
if (mStop) {
2021-08-01 23:24:37 +08:00
mProcess->closeReadChannel(QProcess::StandardOutput);
mProcess->closeReadChannel(QProcess::StandardError);
mProcess->closeWriteChannel();
2021-07-27 00:14:24 +08:00
mProcess->terminate();
2021-08-01 23:24:37 +08:00
mProcess->kill();
2021-07-31 14:04:43 +08:00
break;
2021-07-19 23:02:32 +08:00
}
2021-12-25 20:02:53 +08:00
if (mErrorOccured)
2021-07-19 23:02:32 +08:00
break;
2021-07-30 23:28:58 +08:00
readed = mProcess->readAll();
buffer += readed;
2021-11-10 12:29:02 +08:00
2021-11-24 21:22:01 +08:00
if (readed.endsWith("\n")&& outputTerminated(buffer)) {
2021-11-12 10:51:00 +08:00
processDebugOutput(buffer);
2021-07-30 23:28:58 +08:00
buffer.clear();
2021-07-19 23:02:32 +08:00
mCmdRunning = false;
runNextCmd();
2021-07-30 23:28:58 +08:00
} else if (!mCmdRunning && readed.isEmpty()){
runNextCmd();
2021-08-01 01:06:43 +08:00
} else if (readed.isEmpty()){
msleep(1);
2021-07-19 23:02:32 +08:00
}
}
2021-12-25 20:02:53 +08:00
if (mErrorOccured) {
2021-07-27 00:14:24 +08:00
emit processError(mProcess->error());
2021-07-19 23:02:32 +08:00
}
}
2021-07-24 11:18:25 +08:00
BreakpointModel::BreakpointModel(QObject *parent):QAbstractTableModel(parent)
{
}
2021-07-17 19:32:23 +08:00
int BreakpointModel::rowCount(const QModelIndex &) const
{
return mList.size();
}
int BreakpointModel::columnCount(const QModelIndex &) const
{
return 3;
}
QVariant BreakpointModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row()<0 || index.row() >= static_cast<int>(mList.size()))
return QVariant();
PBreakpoint breakpoint = mList[index.row()];
if (!breakpoint)
return QVariant();
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case 0: {
2021-09-10 12:37:02 +08:00
return extractFileName(breakpoint->filename);
}
case 1:
if (breakpoint->line>0)
return breakpoint->line;
else
return "";
case 2:
return breakpoint->condition;
default:
return QVariant();
}
case Qt::ToolTipRole:
2021-07-17 19:32:23 +08:00
switch (index.column()) {
case 0:
return breakpoint->filename;
case 1:
if (breakpoint->line>0)
return breakpoint->line;
else
return "";
case 2:
return breakpoint->condition;
default:
return QVariant();
}
default:
return QVariant();
}
}
QVariant BreakpointModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case 0:
return tr("Filename");
2021-07-17 19:32:23 +08:00
case 1:
return tr("Line");
case 2:
return tr("Condition");
}
}
return QVariant();
}
void BreakpointModel::addBreakpoint(PBreakpoint p)
{
beginInsertRows(QModelIndex(),mList.size(),mList.size());
mList.push_back(p);
endInsertRows();
}
void BreakpointModel::clear()
{
beginResetModel();
2021-07-17 19:32:23 +08:00
mList.clear();
endResetModel();
2021-07-17 19:32:23 +08:00
}
void BreakpointModel::removeBreakpoint(int row)
{
beginRemoveRows(QModelIndex(),row,row);
mList.removeAt(row);
endRemoveRows();
}
2021-11-21 08:38:03 +08:00
void BreakpointModel::invalidateAllBreakpointNumbers()
{
foreach (PBreakpoint bp,mList) {
bp->number = -1;
}
//emit dateChanged(createIndex(0,0),)
}
2021-07-24 11:18:25 +08:00
PBreakpoint BreakpointModel::setBreakPointCondition(int index, const QString &condition)
{
PBreakpoint breakpoint = mList[index];
breakpoint->condition = condition;
2021-07-31 20:19:45 +08:00
emit dataChanged(createIndex(index,0),createIndex(index,2));
2021-07-24 11:18:25 +08:00
return breakpoint;
}
const QList<PBreakpoint> &BreakpointModel::breakpoints() const
{
return mList;
}
PBreakpoint BreakpointModel::breakpoint(int index) const
{
if (index<0 && index>=mList.count())
return PBreakpoint();
return mList[index];
}
void BreakpointModel::save(const QString &filename)
{
QFile file(filename);
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QJsonArray array;
foreach (const PBreakpoint& breakpoint, mList) {
QJsonObject obj;
obj["filename"]=breakpoint->filename;
obj["line"]=breakpoint->line;
obj["condition"]=breakpoint->condition;
obj["enabled"]=breakpoint->enabled;
2022-01-02 11:43:23 +08:00
obj["breakpoint_type"] = static_cast<int>(breakpoint->breakpointType);
array.append(obj);
}
QJsonDocument doc;
doc.setArray(array);
if (file.write(doc.toJson())<0) {
throw FileError(tr("Save file '%1' failed.")
.arg(filename));
}
} else {
throw FileError(tr("Can't open file '%1' for write.")
.arg(filename));
}
}
void BreakpointModel::load(const QString &filename)
{
clear();
QFile file(filename);
if (!file.exists())
return;
if (file.open(QFile::ReadOnly)) {
QByteArray content = file.readAll();
QJsonParseError error;
QJsonDocument doc(QJsonDocument::fromJson(content,&error));
if (error.error != QJsonParseError::NoError) {
throw FileError(tr("Error in json file '%1':%2 : %3")
.arg(filename)
.arg(error.offset)
.arg(error.errorString()));
}
QJsonArray array = doc.array();
for (int i=0;i<array.count();i++) {
QJsonValue value = array[i];
QJsonObject obj=value.toObject();
PBreakpoint breakpoint = std::make_shared<Breakpoint>();
breakpoint->filename = QFileInfo(obj["filename"].toString()).absoluteFilePath();
breakpoint->line = obj["line"].toInt();
breakpoint->condition = obj["condition"].toString();
breakpoint->enabled = obj["enabled"].toBool();
2022-01-02 11:43:23 +08:00
breakpoint->breakpointType = static_cast<BreakpointType>(obj["breakpoint_type"].toInt());
addBreakpoint(breakpoint);
}
} else {
throw FileError(tr("Can't open file '%1' for read.")
.arg(filename));
}
}
2021-11-24 10:07:35 +08:00
void BreakpointModel::updateBreakpointNumber(const QString& filename, int line, int number)
2021-11-21 08:38:03 +08:00
{
foreach (PBreakpoint bp, mList) {
2021-11-21 10:36:50 +08:00
if (bp->filename == filename && bp->line == line) {
2021-11-21 08:38:03 +08:00
bp->number = number;
2021-11-23 21:08:33 +08:00
return;
2021-11-21 08:38:03 +08:00
}
}
}
2021-11-24 10:07:35 +08:00
void BreakpointModel::onFileDeleteLines(const QString& filename, int startLine, int count)
{
for (int i = mList.count()-1;i>=0;i--){
PBreakpoint breakpoint = mList[i];
if (breakpoint->filename == filename
&& breakpoint->line>=startLine) {
if (breakpoint->line >= startLine+count) {
breakpoint->line -= count;
2021-10-21 17:31:25 +08:00
emit dataChanged(createIndex(i,0),createIndex(i,2));
} else {
2021-10-21 17:31:25 +08:00
removeBreakpoint(i);
}
}
}
}
2021-11-24 10:07:35 +08:00
void BreakpointModel::onFileInsertLines(const QString& filename, int startLine, int count)
{
for (int i = mList.count()-1;i>=0;i--){
PBreakpoint breakpoint = mList[i];
if (breakpoint->filename == filename
&& breakpoint->line>=startLine) {
breakpoint->line+=count;
2021-10-21 17:31:25 +08:00
emit dataChanged(createIndex(i,0),createIndex(i,2));
}
}
}
2021-07-24 11:18:25 +08:00
BacktraceModel::BacktraceModel(QObject *parent):QAbstractTableModel(parent)
{
}
2021-07-17 19:32:23 +08:00
int BacktraceModel::rowCount(const QModelIndex &) const
{
return mList.size();
}
int BacktraceModel::columnCount(const QModelIndex &) const
{
return 3;
}
QVariant BacktraceModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row()<0 || index.row() >= static_cast<int>(mList.size()))
return QVariant();
PTrace trace = mList[index.row()];
if (!trace)
return QVariant();
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case 0:
return trace->funcname;
case 1:
return trace->filename;
case 2:
if (trace->line>0)
return trace->line;
else
return "";
default:
return QVariant();
}
default:
return QVariant();
}
}
QVariant BacktraceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case 0:
return tr("Function");
case 1:
return tr("Filename");
case 2:
return tr("Line");
}
}
return QVariant();
}
void BacktraceModel::addTrace(PTrace p)
{
beginInsertRows(QModelIndex(),mList.size(),mList.size());
mList.push_back(p);
endInsertRows();
}
void BacktraceModel::clear()
{
beginResetModel();
2021-07-17 19:32:23 +08:00
mList.clear();
endResetModel();
2021-07-17 19:32:23 +08:00
}
void BacktraceModel::removeTrace(int row)
{
beginRemoveRows(QModelIndex(),row,row);
mList.removeAt(row);
endRemoveRows();
}
2021-07-24 11:18:25 +08:00
const QList<PTrace> &BacktraceModel::backtraces() const
{
return mList;
}
2021-07-31 14:04:43 +08:00
PTrace BacktraceModel::backtrace(int index) const
{
if (index>=0 && index < mList.count()){
return mList[index];
}
return PTrace();
}
2021-07-31 20:19:45 +08:00
WatchModel::WatchModel(QObject *parent):QAbstractItemModel(parent)
{
mUpdateCount = 0;
2021-07-31 20:19:45 +08:00
}
QVariant WatchModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) {
return QVariant();
}
WatchVar* item = static_cast<WatchVar*>(index.internalPointer());
switch (role) {
case Qt::DisplayRole:
2021-09-19 09:45:03 +08:00
switch(index.column()) {
case 0:
2021-11-25 11:24:38 +08:00
return item->expression;
2021-09-19 09:45:03 +08:00
case 1:
2021-11-25 11:24:38 +08:00
return item->type;
case 2:
2021-09-19 09:45:03 +08:00
return item->value;
}
2021-07-31 20:19:45 +08:00
}
return QVariant();
}
2021-07-31 14:04:43 +08:00
QModelIndex WatchModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row,column,parent))
return QModelIndex();
WatchVar* parentItem;
PWatchVar pChild;
if (!parent.isValid()) {
parentItem = nullptr;
pChild = mWatchVars[row];
} else {
parentItem = static_cast<WatchVar*>(parent.internalPointer());
pChild = parentItem->children[row];
}
if (pChild) {
2021-07-31 14:04:43 +08:00
return createIndex(row,column,pChild.get());
}
2021-07-31 14:04:43 +08:00
return QModelIndex();
}
static int getWatchIndex(WatchVar* var, const QList<PWatchVar> list) {
for (int i=0;i<list.size();i++) {
PWatchVar v = list[i];
if (v.get() == var) {
return i;
}
}
2021-08-27 23:51:42 +08:00
return -1;
2021-07-31 14:04:43 +08:00
}
QModelIndex WatchModel::parent(const QModelIndex &index) const
{
if (!index.isValid()) {
return QModelIndex();
}
WatchVar* childItem = static_cast<WatchVar*>(index.internalPointer());
2021-08-01 01:06:43 +08:00
WatchVar* parentItem = childItem->parent;
2021-07-31 14:04:43 +08:00
//parent is root
if (parentItem == nullptr) {
return QModelIndex();
}
int row;
WatchVar* grandItem = parentItem->parent;
if (grandItem == nullptr) {
row = getWatchIndex(parentItem,mWatchVars);
} else {
row = getWatchIndex(parentItem,grandItem->children);
}
return createIndex(row,0,parentItem);
}
int WatchModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return mWatchVars.count();
} else {
WatchVar* parentItem = static_cast<WatchVar*>(parent.internalPointer());
return parentItem->children.count();
}
}
2021-07-31 20:19:45 +08:00
int WatchModel::columnCount(const QModelIndex&) const
2021-07-31 14:04:43 +08:00
{
2021-11-25 11:24:38 +08:00
return 3;
2021-07-31 14:04:43 +08:00
}
void WatchModel::addWatchVar(PWatchVar watchVar)
{
for (PWatchVar var:mWatchVars) {
2021-11-25 11:24:38 +08:00
if (watchVar->expression == var->expression) {
2021-07-31 14:04:43 +08:00
return;
}
}
2021-11-25 20:26:43 +08:00
beginInsertRows(QModelIndex(),mWatchVars.count(),mWatchVars.count());
2021-07-31 14:04:43 +08:00
mWatchVars.append(watchVar);
2021-11-25 20:26:43 +08:00
endInsertRows();
2021-07-31 14:04:43 +08:00
}
2021-11-25 11:24:38 +08:00
void WatchModel::removeWatchVar(const QString &express)
2021-07-31 14:04:43 +08:00
{
for (int i=mWatchVars.size()-1;i>=0;i--) {
2021-07-31 20:19:45 +08:00
PWatchVar var = mWatchVars[i];
2021-11-25 11:24:38 +08:00
if (express == var->expression) {
this->beginResetModel();
//this->beginRemoveRows(QModelIndex(),i,i);
2021-11-25 20:26:43 +08:00
if (mVarIndex.contains(var->name))
mVarIndex.remove(var->name);
mWatchVars.removeAt(i);
//this->endRemoveRows();
this->endResetModel();
2021-07-31 14:04:43 +08:00
}
}
}
void WatchModel::removeWatchVar(const QModelIndex &index)
{
int r=index.row();
this->beginRemoveRows(QModelIndex(),r,r);
2021-11-25 20:26:43 +08:00
PWatchVar var = mWatchVars[r];
if (mVarIndex.contains(var->name))
mVarIndex.remove(var->name);
mWatchVars.removeAt(r);
this->endRemoveRows();
}
2021-07-31 14:04:43 +08:00
void WatchModel::clear()
{
2021-07-31 20:19:45 +08:00
this->beginResetModel();
2021-07-31 14:04:43 +08:00
mWatchVars.clear();
2021-07-31 20:19:45 +08:00
this->endResetModel();
2021-07-31 14:04:43 +08:00
}
2021-07-31 20:19:45 +08:00
const QList<PWatchVar> &WatchModel::watchVars()
2021-07-31 14:04:43 +08:00
{
return mWatchVars;
}
2021-11-25 20:26:43 +08:00
PWatchVar WatchModel::findWatchVar(const QModelIndex &index)
{
if (!index.isValid())
return PWatchVar();
int r=index.row();
return mWatchVars[r];
}
PWatchVar WatchModel::findWatchVar(const QString &expr)
2021-07-31 14:04:43 +08:00
{
for (PWatchVar var:mWatchVars) {
2021-11-25 20:26:43 +08:00
if (expr == var->expression) {
2021-07-31 14:04:43 +08:00
return var;
}
}
2021-07-31 20:19:45 +08:00
return PWatchVar();
2021-07-31 14:04:43 +08:00
}
2021-11-25 20:26:43 +08:00
void WatchModel::resetAllVarInfos()
2021-07-31 14:04:43 +08:00
{
2021-11-25 20:26:43 +08:00
beginResetModel();
foreach (PWatchVar var, mWatchVars) {
var->name.clear();
var->value = tr("Not Valid");
var->numChild = 0;
var->hasMore = false;
var->type.clear();
var->children.clear();
}
mVarIndex.clear();
endResetModel();
}
void WatchModel::updateVarInfo(const QString &expression, const QString &name, int numChild, const QString &value, const QString &type, bool hasMore)
{
PWatchVar var = findWatchVar(expression);
if (!var)
return;
var->name = name;
var->value = value;
var->numChild = numChild;
var->hasMore = hasMore;
var->type = type;
mVarIndex.insert(name,var);
QModelIndex idx = index(var);
if (!idx.isValid())
return;
emit dataChanged(idx,createIndex(idx.row(),2,var.get()));
}
void WatchModel::prepareVarChildren(const QString &parentName, int numChild, bool hasMore)
{
PWatchVar var = mVarIndex.value(parentName,PWatchVar());
if (var) {
var->numChild = numChild;
var->hasMore = hasMore;
if (var->children.count()>0) {
beginRemoveRows(index(var),0,var->children.count()-1);
var->children.clear();
endRemoveRows();
}
2021-07-31 14:04:43 +08:00
}
2021-11-25 20:26:43 +08:00
}
void WatchModel::addVarChild(const QString &parentName, const QString &name,
const QString &exp, int numChild, const QString &value,
const QString &type, bool hasMore)
{
PWatchVar var = mVarIndex.value(parentName,PWatchVar());
if (!var)
return;
beginInsertRows(index(var),var->children.count(),var->children.count());
PWatchVar child = std::make_shared<WatchVar>();
child->name = name;
child->expression = exp;
child->numChild = numChild;
child->value = value;
child->type = type;
child->hasMore = hasMore;
child->parent = var.get();
var->children.append(child);
endInsertRows();
mVarIndex.insert(name,child);
}
void WatchModel::updateVarValue(const QString &name, const QString &val, const QString &inScope, bool typeChanged, const QString &newType, int newNumChildren, bool hasMore)
{
PWatchVar var = mVarIndex.value(name,PWatchVar());
if (!var)
return;
if (inScope == "true") {
var->value = val;
} else{
var->value = tr("Not Valid");
}
if (typeChanged) {
var->type = newType;
}
QModelIndex idx = index(var);
bool oldHasMore = var->hasMore;
var->hasMore = hasMore;
2021-11-25 20:26:43 +08:00
if (newNumChildren>=0
&& var->numChild!=newNumChildren) {
var->numChild = newNumChildren;
fetchMore(idx);
} else if (!oldHasMore && hasMore) {
fetchMore(idx);
2021-11-25 20:26:43 +08:00
}
emit dataChanged(idx,createIndex(idx.row(),2,var.get()));
}
void WatchModel::clearAllVarInfos()
{
beginResetModel();
foreach (PWatchVar var, mWatchVars) {
var->name.clear();
var->value = tr("Execute to evaluate");
var->numChild = 0;
var->hasMore = false;
var->type.clear();
var->children.clear();
}
mVarIndex.clear();
endResetModel();
2021-07-31 20:19:45 +08:00
}
void WatchModel::beginUpdate()
{
if (mUpdateCount == 0) {
beginResetModel();
}
mUpdateCount++;
}
void WatchModel::endUpdate()
{
mUpdateCount--;
if (mUpdateCount == 0) {
endResetModel();
}
}
2021-07-31 20:19:45 +08:00
void WatchModel::notifyUpdated(PWatchVar var)
{
if (!var)
return;
int row;
if (var->parent==nullptr) {
row = mWatchVars.indexOf(var);
} else {
row = var->parent->children.indexOf(var);
}
if (row<0)
return;
2021-09-17 08:01:02 +08:00
//qDebug()<<"dataChanged"<<row<<":"<<var->text;
2021-07-31 20:19:45 +08:00
emit dataChanged(createIndex(row,0,var.get()),createIndex(row,0,var.get()));
2021-07-31 14:04:43 +08:00
}
2021-08-01 23:24:37 +08:00
void WatchModel::save(const QString &filename)
{
QFile file(filename);
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QJsonArray array;
foreach (const PWatchVar& watchVar, mWatchVars) {
QJsonObject obj;
2021-11-25 11:24:38 +08:00
obj["expression"]=watchVar->expression;
array.append(obj);
}
QJsonDocument doc;
doc.setArray(array);
if (file.write(doc.toJson())<0) {
throw FileError(tr("Save file '%1' failed.")
.arg(filename));
}
} else {
throw FileError(tr("Can't open file '%1' for write.")
.arg(filename));
}
}
void WatchModel::load(const QString &filename)
{
clear();
QFile file(filename);
if (!file.exists())
return;
if (file.open(QFile::ReadOnly)) {
QByteArray content = file.readAll();
QJsonParseError error;
QJsonDocument doc(QJsonDocument::fromJson(content,&error));
if (error.error != QJsonParseError::NoError) {
throw FileError(tr("Error in json file '%1':%2 : %3")
.arg(filename)
.arg(error.offset)
.arg(error.errorString()));
}
QJsonArray array = doc.array();
for (int i=0;i<array.count();i++) {
QJsonValue value = array[i];
QJsonObject obj=value.toObject();
PWatchVar var = std::make_shared<WatchVar>();
var->parent= nullptr;
2021-11-25 11:24:38 +08:00
var->expression = obj["expression"].toString();
var->value = tr("Execute to evaluate");
2021-11-25 11:24:38 +08:00
var->numChild = 0;
var->hasMore=false;
2021-11-25 20:26:43 +08:00
var->parent = nullptr;
addWatchVar(var);
}
} else {
throw FileError(tr("Can't open file '%1' for read.")
.arg(filename));
}
}
2021-11-25 20:26:43 +08:00
QModelIndex WatchModel::index(PWatchVar var) const
{
if (!var)
return QModelIndex();
return index(var.get());
}
QModelIndex WatchModel::index(WatchVar* pVar) const {
if (pVar==nullptr)
return QModelIndex();
if (pVar->parent) {
int row=-1;
for (int i=0;i<pVar->parent->children.count();i++) {
if (pVar->parent->children[i].get() == pVar) {
row = i;
break;
}
}
if (row<0)
return QModelIndex();
return createIndex(row,0,pVar);
} else {
int row=-1;
for (int i=0;i<mWatchVars.count();i++) {
if (mWatchVars[i].get() == pVar) {
row = i;
break;
}
}
if (row<0)
return QModelIndex();
return createIndex(row,0,pVar);
}
}
bool WatchModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid()) {
return false;
}
if (index.column()==2 && role == Qt::EditRole) {
WatchVar* item = static_cast<WatchVar*>(index.internalPointer());
emit setWatchVarValue(item->name,value.toString());
}
return false;
}
Qt::ItemFlags WatchModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (!index.isValid()) {
return Qt::ItemIsEnabled;
}
if (index.column() == 2) {
WatchVar* item = static_cast<WatchVar*>(index.internalPointer());
if (item->numChild==0 && !item->type.isEmpty())
flags |= Qt::ItemIsEditable;
}
return flags;
}
QVariant WatchModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case 0:
return tr("Expression");
case 1:
2021-11-25 11:24:38 +08:00
return tr("Type");
case 2:
return tr("Value");
}
}
return QVariant();
}
2021-11-25 11:24:38 +08:00
void WatchModel::fetchMore(const QModelIndex &parent)
{
2021-11-25 20:26:43 +08:00
if (!parent.isValid()) {
return;
}
WatchVar* item = static_cast<WatchVar*>(parent.internalPointer());
item->hasMore = false;
item->numChild = item->children.count();
emit fetchChildren(item->name);
2021-11-25 11:24:38 +08:00
}
bool WatchModel::canFetchMore(const QModelIndex &parent) const
{
if (!parent.isValid()) {
return false;
}
WatchVar* item = static_cast<WatchVar*>(parent.internalPointer());
2021-11-25 20:26:43 +08:00
return item->numChild>item->children.count() || item->hasMore;
2021-11-25 11:24:38 +08:00
}
bool WatchModel::hasChildren(const QModelIndex &parent) const
{
if (!parent.isValid()) {
2021-11-25 20:26:43 +08:00
return true;
2021-11-25 11:24:38 +08:00
}
WatchVar* item = static_cast<WatchVar*>(parent.internalPointer());
return item->numChild>0;
}
2021-08-01 23:24:37 +08:00
RegisterModel::RegisterModel(QObject *parent):QAbstractTableModel(parent)
{
}
2021-10-20 18:05:43 +08:00
int RegisterModel::rowCount(const QModelIndex &) const
2021-08-01 23:24:37 +08:00
{
2021-11-25 09:05:45 +08:00
return mRegisterNames.count();
2021-08-01 23:24:37 +08:00
}
2021-10-20 18:05:43 +08:00
int RegisterModel::columnCount(const QModelIndex &) const
2021-08-01 23:24:37 +08:00
{
2021-11-25 09:05:45 +08:00
return 2;
2021-08-01 23:24:37 +08:00
}
QVariant RegisterModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
2021-11-25 09:05:45 +08:00
if (index.row()<0 || index.row() >= static_cast<int>(mRegisterNames.size()))
2021-08-01 23:24:37 +08:00
return QVariant();
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
case 0:
2021-11-25 09:05:45 +08:00
return mRegisterNames[index.row()];
2021-08-01 23:24:37 +08:00
case 1:
2021-11-25 09:05:45 +08:00
return mRegisterValues.value(index.row(),"");
2021-08-01 23:24:37 +08:00
default:
return QVariant();
}
default:
return QVariant();
}
}
QVariant RegisterModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case 0:
return tr("Register");
case 1:
2021-11-25 09:05:45 +08:00
return tr("Value");
2021-08-01 23:24:37 +08:00
}
}
return QVariant();
}
2021-11-25 09:05:45 +08:00
void RegisterModel::updateNames(const QStringList &regNames)
2021-08-01 23:24:37 +08:00
{
beginResetModel();
2021-11-25 09:05:45 +08:00
mRegisterNames = regNames;
2021-08-01 23:24:37 +08:00
endResetModel();
}
2021-11-25 09:05:45 +08:00
void RegisterModel::updateValues(const QHash<int, QString> registerValues)
{
mRegisterValues= registerValues;
emit dataChanged(createIndex(0,1),
createIndex(mRegisterNames.count()-1,1));
}
2021-08-01 23:24:37 +08:00
void RegisterModel::clear()
{
beginResetModel();
2021-11-25 09:05:45 +08:00
mRegisterNames.clear();
mRegisterValues.clear();
2021-08-01 23:24:37 +08:00
endResetModel();
}
DebugTarget::DebugTarget(
const QString &inferior,
const QString &GDBServer,
int port,
QObject *parent):
QThread(parent),
mInferior(inferior),
mGDBServer(GDBServer),
mPort(port),
mStop(false),
2021-12-25 20:02:53 +08:00
mStartSemaphore(0),
mErrorOccured(false)
{
2021-12-25 20:02:53 +08:00
mProcess = nullptr;
}
void DebugTarget::setInputFile(const QString &inputFile)
{
mInputFile = inputFile;
}
void DebugTarget::stopDebug()
{
mStop = true;
}
void DebugTarget::waitStart()
{
mStartSemaphore.acquire(1);
}
void DebugTarget::run()
{
mStop = false;
2021-12-25 20:02:53 +08:00
mErrorOccured = false;
//find first available port
QString cmd;
QString arguments;
#ifdef Q_OS_WIN
cmd= mGDBServer;
arguments = QString(" localhost:%1 \"%2\"").arg(mPort).arg(mInferior);
#else
cmd= pSettings->environment().terminalPath();
arguments = QString(" -e \"%1\" localhost:%2 \"%3\"").arg(mGDBServer).arg(mPort).arg(mInferior);
#endif
QString workingDir = QFileInfo(mInferior).path();
mProcess = std::make_shared<QProcess>();
2021-12-25 20:02:53 +08:00
auto action = finally([&]{
mProcess.reset();
});
mProcess->setProgram(cmd);
2022-01-04 16:50:54 +08:00
mProcess->setArguments(splitProcessCommand(arguments));
mProcess->setProcessChannelMode(QProcess::MergedChannels);
QString cmdDir = extractFileDir(cmd);
if (!cmdDir.isEmpty()) {
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
QString path = env.value("PATH");
cmdDir.replace("/",QDir::separator());
if (path.isEmpty()) {
path = cmdDir;
} else {
path = cmdDir + PATH_SEPARATOR + path;
}
env.insert("PATH",path);
mProcess->setProcessEnvironment(env);
}
mProcess->setWorkingDirectory(workingDir);
#ifdef Q_OS_WIN
mProcess->setCreateProcessArgumentsModifier([this](QProcess::CreateProcessArguments * args){
if (programHasConsole(mInferior)) {
args->flags |= CREATE_NEW_CONSOLE;
args->flags &= ~CREATE_NO_WINDOW;
}
if (mInputFile.isEmpty()) {
args->startupInfo -> dwFlags &= ~STARTF_USESTDHANDLES;
2022-01-12 19:25:37 +08:00
} else {
args->startupInfo->hStdOutput = NULL;
args->startupInfo->hStdError = NULL;
}
2022-01-12 19:25:37 +08:00
});
#endif
connect(mProcess.get(), &QProcess::errorOccurred,
[&](){
2021-12-25 20:02:53 +08:00
mErrorOccured= true;
});
mProcess->start();
mProcess->waitForStarted(5000);
mStartSemaphore.release(1);
if (mProcess->state()==QProcess::Running && !mInputFile.isEmpty()) {
mProcess->write(readFileToByteArray(mInputFile));
mProcess->closeWriteChannel();
}
while (true) {
mProcess->waitForFinished(1);
if (mProcess->state()!=QProcess::Running) {
break;
}
if (mStop) {
mProcess->closeReadChannel(QProcess::StandardOutput);
mProcess->closeReadChannel(QProcess::StandardError);
mProcess->closeWriteChannel();
mProcess->terminate();
mProcess->kill();
break;
}
2021-12-25 20:02:53 +08:00
if (mErrorOccured)
break;
msleep(1);
}
2021-12-25 20:02:53 +08:00
if (mErrorOccured) {
emit processError(mProcess->error());
}
}
MemoryModel::MemoryModel(int dataPerLine, QObject *parent):
QAbstractTableModel(parent),
mDataPerLine(dataPerLine),
mStartAddress(0)
{
}
void MemoryModel::updateMemory(const QStringList &value)
{
QRegExp delimiter("(\\s+)");
QList<PMemoryLine> newModel;
for (int i=0;i<value.length();i++) {
QString line = value[i].trimmed();
QStringList dataLst = line.split(delimiter,QString::SkipEmptyParts);
PMemoryLine memoryLine = std::make_shared<MemoryLine>();
memoryLine->startAddress = -1;
if (dataLst.length()>0) {
memoryLine->startAddress = stringToHex(dataLst[0],0);
for (int j=1;j<dataLst.length();j++) {
qulonglong data = stringToHex(dataLst[j],-1);
if (data>=0)
memoryLine->datas.append((unsigned char)data);
}
}
newModel.append(memoryLine);
}
if (newModel.count()>0 && newModel.count()== mLines.count() &&
newModel[0]->startAddress == mLines[0]->startAddress) {
for (int i=0;i<newModel.count();i++) {
PMemoryLine newLine = newModel[i];
PMemoryLine oldLine = mLines[i];
for (int j=0;j<newLine->datas.count();j++) {
if (j>=oldLine->datas.count())
break;
if (newLine->datas[j]!=oldLine->datas[j])
newLine->changedDatas.insert(j);
}
}
mLines = newModel;
emit dataChanged(createIndex(0,0),
createIndex(mLines.count()-1,mDataPerLine-1));
} else {
beginResetModel();
mLines = newModel;
endResetModel();
}
if (mLines.count()>0) {
mStartAddress = mLines[0]->startAddress;
} else {
mStartAddress = 0;
}
}
int MemoryModel::rowCount(const QModelIndex &parent) const
{
return mLines.count();
}
int MemoryModel::columnCount(const QModelIndex &parent) const
{
return mDataPerLine;
}
QVariant MemoryModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row()<0 || index.row()>=mLines.count())
return QVariant();
PMemoryLine line = mLines[index.row()];
int col = index.column();
if (col<0 || col>=line->datas.count())
return QVariant();
if (role == Qt::DisplayRole)
return QString("%1").arg(line->datas[col],2,16,QChar('0'));
return QVariant();
}
QVariant MemoryModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Vertical && role == Qt::DisplayRole) {
if (section<0 || section>=mLines.count())
return QVariant();
PMemoryLine line = mLines[section];
return QString("0x%1").arg(line->startAddress,0,16,QChar('0'));
}
return QVariant();
}
bool MemoryModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (!index.isValid())
return false;
if (index.row()<0 || index.row()>=mLines.count())
return false;
PMemoryLine line = mLines[index.row()];
int col = index.column();
if (col<0 || col>=line->datas.count())
return false;
if (role == Qt::EditRole && mStartAddress>0) {
bool ok;
unsigned char val = ("0x"+value.toString()).toUInt(&ok,16);
if (!ok || val>255)
return false;
emit setMemoryData(mStartAddress+mDataPerLine*index.row()+col,val);
return true;
}
return false;
}
Qt::ItemFlags MemoryModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (mStartAddress!=0)
flags |= Qt::ItemIsEditable;
return flags;
}
qulonglong MemoryModel::startAddress() const
{
return mStartAddress;
}
void MemoryModel::reset()
{
mStartAddress=0;
mLines.clear();
}