From b7c2d05a1a83dd91052ca6df20c2f60c802e773e Mon Sep 17 00:00:00 2001 From: Pavel Pisa Date: Fri, 22 Feb 2019 22:05:19 +0100 Subject: Add support for goto to selected symbol address. Signed-off-by: Pavel Pisa --- qtmips_gui/MainWindow.ui | 8 ++++ qtmips_gui/gotosymboldialog.cpp | 32 +++++++++++++ qtmips_gui/gotosymboldialog.h | 30 ++++++++++++ qtmips_gui/gotosymboldialog.ui | 88 +++++++++++++++++++++++++++++++++++ qtmips_gui/mainwindow.cpp | 19 +++++++- qtmips_gui/mainwindow.h | 1 + qtmips_gui/memorydock.cpp | 2 + qtmips_gui/memorydock.h | 1 + qtmips_gui/qtmips_gui.pro | 9 ++-- qtmips_machine/programloader.cpp | 33 +++++++++++++ qtmips_machine/programloader.h | 2 + qtmips_machine/qtmips_machine.pro | 6 ++- qtmips_machine/qtmipsmachine.cpp | 13 +++++- qtmips_machine/qtmipsmachine.h | 5 +- qtmips_machine/symboltable.cpp | 98 +++++++++++++++++++++++++++++++++++++++ qtmips_machine/symboltable.h | 82 ++++++++++++++++++++++++++++++++ 16 files changed, 421 insertions(+), 8 deletions(-) create mode 100644 qtmips_gui/gotosymboldialog.cpp create mode 100644 qtmips_gui/gotosymboldialog.h create mode 100644 qtmips_gui/gotosymboldialog.ui create mode 100644 qtmips_machine/symboltable.cpp create mode 100644 qtmips_machine/symboltable.h diff --git a/qtmips_gui/MainWindow.ui b/qtmips_gui/MainWindow.ui index 27bc045..755f539 100644 --- a/qtmips_gui/MainWindow.ui +++ b/qtmips_gui/MainWindow.ui @@ -373,6 +373,14 @@ Terminal + + + Show Symbol + + + Show Symbol + + diff --git a/qtmips_gui/gotosymboldialog.cpp b/qtmips_gui/gotosymboldialog.cpp new file mode 100644 index 0000000..59927f2 --- /dev/null +++ b/qtmips_gui/gotosymboldialog.cpp @@ -0,0 +1,32 @@ +#include "gotosymboldialog.h" +#include "ui_gotosymboldialog.h" + +GoToSymbolDialog::GoToSymbolDialog(QWidget *parent, QStringList &symlist) : + QDialog(parent), + ui(new Ui::GoToSymbolDialog) +{ + ui->setupUi(this); + + connect(ui->pushShowProg, SIGNAL(clicked()), this, SLOT(show_prog())); + connect(ui->pushShowMem, SIGNAL(clicked()), this, SLOT(show_mem())); + connect(ui->pushClose, SIGNAL(clicked()), this, SLOT(close())); + + ui->listSymbols->addItems(symlist); +} + +GoToSymbolDialog::~GoToSymbolDialog() +{ + delete ui; +} + +void GoToSymbolDialog::show_prog() { + std::uint32_t address = 0; + emit obtain_value_for_name(address, ui->listSymbols->currentItem()->text()); + emit program_focus_addr(address); +} + +void GoToSymbolDialog::show_mem() { + std::uint32_t address = 0; + emit obtain_value_for_name(address, ui->listSymbols->currentItem()->text()); + emit memory_focus_addr(address); +} diff --git a/qtmips_gui/gotosymboldialog.h b/qtmips_gui/gotosymboldialog.h new file mode 100644 index 0000000..27e8df2 --- /dev/null +++ b/qtmips_gui/gotosymboldialog.h @@ -0,0 +1,30 @@ +#ifndef GOTOSYMBOLDIALOG_H +#define GOTOSYMBOLDIALOG_H + +#include +#include +#include + +namespace Ui { +class GoToSymbolDialog; +} + +class GoToSymbolDialog : public QDialog +{ + Q_OBJECT + +public: + explicit GoToSymbolDialog(QWidget *parent, QStringList &symlist); + ~GoToSymbolDialog(); +signals: + void program_focus_addr(std::uint32_t); + void memory_focus_addr(std::uint32_t); + bool obtain_value_for_name(std::uint32_t &value, QString name) const; +public slots: + void show_prog(); + void show_mem(); +private: + Ui::GoToSymbolDialog *ui; +}; + +#endif // GOTOSYMBOLDIALOG_H diff --git a/qtmips_gui/gotosymboldialog.ui b/qtmips_gui/gotosymboldialog.ui new file mode 100644 index 0000000..f5b24e7 --- /dev/null +++ b/qtmips_gui/gotosymboldialog.ui @@ -0,0 +1,88 @@ + + + GoToSymbolDialog + + + + 0 + 0 + 400 + 331 + + + + Go To Symbol DIalog + + + + + + QLayout::SetMaximumSize + + + + + + + + + + Show program + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Show memory + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Close + + + + + + + + + + + listSymbols + pushShowProg + pushShowMem + pushClose + + + + diff --git a/qtmips_gui/mainwindow.cpp b/qtmips_gui/mainwindow.cpp index bbb3a3b..253ab03 100644 --- a/qtmips_gui/mainwindow.cpp +++ b/qtmips_gui/mainwindow.cpp @@ -37,6 +37,7 @@ #include "aboutdialog.h" #include "ossyscall.h" #include "fontsize.h" +#include "gotosymboldialog.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { machine = nullptr; @@ -81,6 +82,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { connect(ui->actionExit, SIGNAL(triggered(bool)), this, SLOT(close())); connect(ui->actionNew, SIGNAL(triggered(bool)), this, SLOT(new_machine())); connect(ui->actionReload, SIGNAL(triggered(bool)), this, SLOT(machine_reload())); + connect(ui->actionShow_Symbol, SIGNAL(triggered(bool)), this, SLOT(show_symbol_dialog())); connect(ui->actionRegisters, SIGNAL(triggered(bool)), this, SLOT(show_registers())); connect(ui->actionProgram_memory, SIGNAL(triggered(bool)), this, SLOT(show_program())); connect(ui->actionMemory, SIGNAL(triggered(bool)), this, SLOT(show_memory())); @@ -129,7 +131,7 @@ void MainWindow::start() { void MainWindow::create_core(const machine::MachineConfig &config) { // Create machine - machine::QtMipsMachine *new_machine = new machine::QtMipsMachine(&config); + machine::QtMipsMachine *new_machine = new machine::QtMipsMachine(&config, true); // Remove old machine if (machine != nullptr) @@ -235,6 +237,21 @@ SHOW_HANDLER(peripherals) SHOW_HANDLER(terminal) #undef SHOW_HANDLER +void MainWindow::show_symbol_dialog(){ + if (machine == nullptr || machine->symbol_table() == nullptr) + return; + QStringList *symnames = machine->symbol_table()->names(); + GoToSymbolDialog *gotosyboldialog = new GoToSymbolDialog(this, *symnames); + connect(gotosyboldialog, SIGNAL(program_focus_addr(std::uint32_t)), + program, SIGNAL(focus_addr(std::uint32_t))); + connect(gotosyboldialog, SIGNAL(memory_focus_addr(std::uint32_t)), + memory, SIGNAL(focus_addr(std::uint32_t))); + connect(gotosyboldialog, SIGNAL(obtain_value_for_name(std::uint32_t&,QString)), + machine->symbol_table(), SLOT(name_to_value(std::uint32_t&,QString))); + gotosyboldialog->exec(); + delete symnames; +} + void MainWindow::about_qtmips() { AboutDialog *aboutdialog = new AboutDialog(this); diff --git a/qtmips_gui/mainwindow.h b/qtmips_gui/mainwindow.h index 61e6896..796dd33 100644 --- a/qtmips_gui/mainwindow.h +++ b/qtmips_gui/mainwindow.h @@ -74,6 +74,7 @@ public slots: void show_cache_program(); void show_peripherals(); void show_terminal(); + void show_symbol_dialog(); // Actions - help menu void about_qtmips(); void about_qt(); diff --git a/qtmips_gui/memorydock.cpp b/qtmips_gui/memorydock.cpp index 323f053..2e0f780 100644 --- a/qtmips_gui/memorydock.cpp +++ b/qtmips_gui/memorydock.cpp @@ -91,6 +91,8 @@ MemoryDock::MemoryDock(QWidget *parent, QSettings *settings) : Super(parent) { memory_content, SLOT(go_to_address(std::uint32_t))); connect(memory_content, SIGNAL(address_changed(std::uint32_t)), go_edit, SLOT(set_value(std::uint32_t))); + connect(this, SIGNAL(focus_addr(std::uint32_t)), + memory_content, SLOT(focus_address(std::uint32_t))); } void MemoryDock::setup(machine::QtMipsMachine *machine) { diff --git a/qtmips_gui/memorydock.h b/qtmips_gui/memorydock.h index 5dd366f..b7e1f85 100644 --- a/qtmips_gui/memorydock.h +++ b/qtmips_gui/memorydock.h @@ -53,6 +53,7 @@ public: signals: void machine_setup(machine::QtMipsMachine *machine); + void focus_addr(std::uint32_t); private: diff --git a/qtmips_gui/qtmips_gui.pro b/qtmips_gui/qtmips_gui.pro index 541798a..acdbd55 100644 --- a/qtmips_gui/qtmips_gui.pro +++ b/qtmips_gui/qtmips_gui.pro @@ -61,7 +61,8 @@ SOURCES += \ terminaldock.cpp \ peripheralsview.cpp \ coreview/multitext.cpp \ - fontsize.cpp + fontsize.cpp \ + gotosymboldialog.cpp HEADERS += \ mainwindow.h \ @@ -98,13 +99,15 @@ HEADERS += \ terminaldock.h \ peripheralsview.h \ coreview/multitext.h \ - fontsize.h + fontsize.h \ + gotosymboldialog.h FORMS += \ NewDialog.ui \ NewDialogCache.ui \ MainWindow.ui \ - peripheralsview.ui + peripheralsview.ui \ + gotosymboldialog.ui RESOURCES += \ icons.qrc diff --git a/qtmips_machine/programloader.cpp b/qtmips_machine/programloader.cpp index 750fd00..859bab4 100644 --- a/qtmips_machine/programloader.cpp +++ b/qtmips_machine/programloader.cpp @@ -130,3 +130,36 @@ std::uint32_t ProgramLoader::end() { std::uint32_t ProgramLoader::get_executable_entry() { return executable_entry; } + +SymbolTable *ProgramLoader::get_symbol_table() { + SymbolTable *p_st = new SymbolTable(); + Elf_Scn *scn = NULL; + GElf_Shdr shdr; + Elf_Data *data; + int count, ii; + + elf_version(EV_CURRENT); + + while (1) { + if ((scn = elf_nextscn(this->elf, scn)) == NULL) + return p_st; + gelf_getshdr(scn, &shdr); + if (shdr.sh_type == SHT_SYMTAB) { + /* found a symbol table, go print it. */ + break; + } + } + + data = elf_getdata(scn, NULL); + count = shdr.sh_size / shdr.sh_entsize; + + /* rettrieve the symbol names */ + for (ii = 0; ii < count; ++ii) { + GElf_Sym sym; + gelf_getsym(data, ii, &sym); + p_st->add_symbol(elf_strptr(elf, shdr.sh_link, sym.st_name), + sym.st_value, sym.st_size, sym.st_info, sym.st_other); + } + + return p_st; +} diff --git a/qtmips_machine/programloader.h b/qtmips_machine/programloader.h index 607bdb0..b275c2f 100644 --- a/qtmips_machine/programloader.h +++ b/qtmips_machine/programloader.h @@ -43,6 +43,7 @@ #include #include #include +#include "symboltable.h" namespace machine { @@ -55,6 +56,7 @@ public: void to_memory(Memory *mem); // Writes all loaded sections to memory std::uint32_t end(); // Return address after which there is no more code for sure std::uint32_t get_executable_entry(); + SymbolTable *get_symbol_table(); private: int fd; Elf *elf; diff --git a/qtmips_machine/qtmips_machine.pro b/qtmips_machine/qtmips_machine.pro index 961800c..9964357 100644 --- a/qtmips_machine/qtmips_machine.pro +++ b/qtmips_machine/qtmips_machine.pro @@ -27,7 +27,8 @@ SOURCES += \ physaddrspace.cpp \ peripheral.cpp \ serialport.cpp \ - peripspiled.cpp + peripspiled.cpp \ + symboltable.cpp HEADERS += \ qtmipsmachine.h \ @@ -45,4 +46,5 @@ HEADERS += \ physaddrspace.h \ peripheral.h \ serialport.h \ - peripspiled.h + peripspiled.h \ + symboltable.h diff --git a/qtmips_machine/qtmipsmachine.cpp b/qtmips_machine/qtmipsmachine.cpp index 37e5461..e1ecf93 100644 --- a/qtmips_machine/qtmipsmachine.cpp +++ b/qtmips_machine/qtmipsmachine.cpp @@ -39,13 +39,17 @@ using namespace machine; -QtMipsMachine::QtMipsMachine(const MachineConfig &cc) : QObject(), mcnf(&cc) { +QtMipsMachine::QtMipsMachine(const MachineConfig &cc, bool load_symtab) : + QObject(), mcnf(&cc) { MemoryAccess *cpu_mem; stat = ST_READY; + symtab = nullptr; ProgramLoader program(cc.elf()); mem_program_only = new Memory(); program.to_memory(mem_program_only); + if (load_symtab) + symtab = program.get_symbol_table(); program_end = program.end(); regs = new Registers(); if (program.get_executable_entry()) @@ -102,6 +106,9 @@ QtMipsMachine::~QtMipsMachine() { if (mem_program_only != nullptr) delete mem_program_only; mem_program_only = nullptr; + if (symtab != nullptr) + delete symtab; + symtab = nullptr; } const MachineConfig &QtMipsMachine::config() { @@ -145,6 +152,10 @@ PeripSpiLed *QtMipsMachine::peripheral_spi_led() { return perip_spi_led; } +const SymbolTable *QtMipsMachine::symbol_table() { + return symtab; +} + const Core *QtMipsMachine::core() { return cr; } diff --git a/qtmips_machine/qtmipsmachine.h b/qtmips_machine/qtmipsmachine.h index b49cb2f..1c80b9c 100644 --- a/qtmips_machine/qtmipsmachine.h +++ b/qtmips_machine/qtmipsmachine.h @@ -49,13 +49,14 @@ #include #include #include +#include namespace machine { class QtMipsMachine : public QObject { Q_OBJECT public: - QtMipsMachine(const MachineConfig &cc); + QtMipsMachine(const MachineConfig &cc, bool load_symtab = false); ~QtMipsMachine(); const MachineConfig &config(); @@ -69,6 +70,7 @@ public: Cache *cache_data_rw(); SerialPort *serial_port(); PeripSpiLed *peripheral_spi_led(); + const SymbolTable *symbol_table(); const Core *core(); const CoreSingle *core_singe(); const CorePipelined *core_pipelined(); @@ -122,6 +124,7 @@ private: QTimer *run_t; unsigned int time_chunk; + SymbolTable *symtab; std::uint32_t program_end; enum Status stat; void set_status(enum Status st); diff --git a/qtmips_machine/symboltable.cpp b/qtmips_machine/symboltable.cpp new file mode 100644 index 0000000..f22acaa --- /dev/null +++ b/qtmips_machine/symboltable.cpp @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#include "symboltable.h" + +using namespace machine; + +SymbolTableEntry::SymbolTableEntry(QString name, std::uint32_t value, + std::uint32_t size, unsigned char info, unsigned char other) { + this->name = name; + this->value = value; + this->size = size; + this->info = info; + this->other = other; +} + +SymbolTable::SymbolTable(QObject *parent) : QObject(parent), + map_value_to_symbol(), map_name_to_symbol() { +} + +SymbolTable::~SymbolTable() { + while (!map_value_to_symbol.isEmpty()) { + SymbolTableEntry *p_ste = map_value_to_symbol.first(); + p_ste = map_value_to_symbol.take(p_ste->value); + map_name_to_symbol.remove(p_ste->name); + delete p_ste; + } +} + +void SymbolTable::add_symbol(QString name, std::uint32_t value, std::uint32_t size, + unsigned char info, unsigned char other) { + SymbolTableEntry *p_ste = new SymbolTableEntry(name, value, size, info, other); + map_value_to_symbol.insert(value, p_ste); + map_name_to_symbol.insert(name, p_ste); +} + +bool SymbolTable::name_to_value(std::uint32_t &value, QString name) const { + SymbolTableEntry *p_ste = map_name_to_symbol.value(name); + if (p_ste == nullptr) { + value = 0; + return false; + } + value = p_ste->value; + return true; +} + +bool SymbolTable::value_to_name(QString &name, std::uint32_t value) const { + SymbolTableEntry *p_ste = map_value_to_symbol.value(value); + if (p_ste == nullptr) { + name = ""; + return false; + } + name = p_ste->name; + return true; +} + +QStringList *SymbolTable::names() const { + QStringList *l = new QStringList(); + + auto i = map_name_to_symbol.begin(); + while (i != map_name_to_symbol.end()) { + l->append(i.value()->name); + i++; + } + return l; +} diff --git a/qtmips_machine/symboltable.h b/qtmips_machine/symboltable.h new file mode 100644 index 0000000..174917c --- /dev/null +++ b/qtmips_machine/symboltable.h @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0+ +/******************************************************************************* + * QtMips - MIPS 32-bit Architecture Subset Simulator + * + * Implemented to support following courses: + * + * B35APO - Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b35apo + * + * B4M35PAP - Advanced Computer Architectures + * https://cw.fel.cvut.cz/wiki/courses/b4m35pap/start + * + * Copyright (c) 2017-2019 Karel Koci + * Copyright (c) 2019 Pavel Pisa + * + * Faculty of Electrical Engineering (http://www.fel.cvut.cz) + * Czech Technical University (http://www.cvut.cz/) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + * + ******************************************************************************/ + +#ifndef SYMBOLTABLE_H +#define SYMBOLTABLE_H + +#include +#include +#include +#include +#include + +namespace machine { + +class SymbolTableEntry { + friend class SymbolTable; + SymbolTableEntry(QString name, std::uint32_t value, std::uint32_t size, + unsigned char info = 0, unsigned char other = 0); +protected: + QString name; + std::uint32_t value; + std::uint32_t size; + unsigned char info; + unsigned char other; +}; + +class SymbolTable : public QObject +{ + Q_OBJECT +public: + SymbolTable(QObject *parent = 0); + ~SymbolTable(); + + void add_symbol(QString name, std::uint32_t value, std::uint32_t size, + unsigned char info = 0, unsigned char other = 0); + QStringList *names() const; +public slots: + bool name_to_value(std::uint32_t &value, QString name) const; + bool value_to_name(QString &name, std::uint32_t value) const; +signals: + + +private: + QMultiMap map_value_to_symbol; + QMap map_name_to_symbol; +}; + +} + +#endif // SYMBOLTABLE_H -- cgit v1.2.3