| 1 | // |
| 2 | // callstackbrowser.cpp - Call Stack |
| 3 | // |
| 4 | // by Jean-Paul Mari |
| 5 | // |
| 6 | // JPM = Jean-Paul Mari <djipi.mari@gmail.com> |
| 7 | // |
| 8 | // Who When (M/D/Y) What |
| 9 | // --- ------------ ----------------------------------------------------------- |
| 10 | // JPM 08/31/2018 Created this file |
| 11 | // JPM 09/12/2018 Added a status bar and better status report |
| 12 | // JPM 10/20/2018 Added the return address information in the call stack |
| 13 | // JPM 08/09/2019 Prevent crash in case of call stack is out of range |
| 14 | // JPM 03/16/2020 Modified the layout window and added source filename from the called source line |
| 15 | |
| 16 | // STILL TO DO: |
| 17 | // To set the information display at the right |
| 18 | // To use DWARF frame information? |
| 19 | // To check if call stack pointer is used (DWARF information?) |
| 20 | // |
| 21 | |
| 22 | #include "debugger/callstackbrowser.h" |
| 23 | #include "memory.h" |
| 24 | #include "debugger/DBGManager.h" |
| 25 | #include "m68000/m68kinterface.h" |
| 26 | #include "settings.h" |
| 27 | |
| 28 | |
| 29 | // |
| 30 | CallStackBrowserWindow::CallStackBrowserWindow(QWidget * parent/*= 0*/) : QWidget(parent, Qt::Dialog), |
| 31 | #ifdef CS_LAYOUTTEXTS |
| 32 | text(new QTextBrowser), |
| 33 | #else |
| 34 | TableView(new QTableView), |
| 35 | model(new QStandardItemModel), |
| 36 | #endif |
| 37 | statusbar(new QStatusBar), |
| 38 | layout(new QVBoxLayout) |
| 39 | { |
| 40 | setWindowTitle(tr("Call Stack")); |
| 41 | |
| 42 | // Set the font |
| 43 | QFont fixedFont("Lucida Console", 8, QFont::Normal); |
| 44 | fixedFont.setStyleHint(QFont::TypeWriter); |
| 45 | |
| 46 | #ifdef CS_LAYOUTTEXTS |
| 47 | // Set original layout |
| 48 | text->setFont(fixedFont); |
| 49 | layout->addWidget(text); |
| 50 | #else |
| 51 | // Set the new layout with proper identation and readibility |
| 52 | model->setColumnCount(5); |
| 53 | model->setHeaderData(0, Qt::Horizontal, QObject::tr("Function")); |
| 54 | model->setHeaderData(1, Qt::Horizontal, QObject::tr("#Line")); |
| 55 | model->setHeaderData(2, Qt::Horizontal, QObject::tr("Line")); |
| 56 | model->setHeaderData(3, Qt::Horizontal, QObject::tr("Return address")); |
| 57 | model->setHeaderData(4, Qt::Horizontal, QObject::tr("Filename")); |
| 58 | // Information table |
| 59 | TableView->setModel(model); |
| 60 | TableView->setEditTriggers(QAbstractItemView::NoEditTriggers); |
| 61 | TableView->setShowGrid(0); |
| 62 | TableView->setFont(fixedFont); |
| 63 | TableView->verticalHeader()->setDefaultSectionSize(TableView->verticalHeader()->minimumSectionSize()); |
| 64 | TableView->verticalHeader()->setDefaultAlignment(Qt::AlignRight); |
| 65 | layout->addWidget(TableView); |
| 66 | #endif |
| 67 | |
| 68 | // Status bar |
| 69 | layout->addWidget(statusbar); |
| 70 | setLayout(layout); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | // |
| 75 | CallStackBrowserWindow::~CallStackBrowserWindow(void) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | |
| 80 | // |
| 81 | void CallStackBrowserWindow::RefreshContents(void) |
| 82 | { |
| 83 | char msg[1024]; |
| 84 | size_t Error = CS_NOERROR; |
| 85 | DBGstatus FilenameStatus; |
| 86 | unsigned int a6, Sa6, ret; |
| 87 | char *Name; |
| 88 | size_t NumError = 0; |
| 89 | #ifdef CS_LAYOUTTEXTS |
| 90 | QString CallStack; |
| 91 | char string[1024]; |
| 92 | #else |
| 93 | int NbRaw = 0; |
| 94 | QString FunctionName; |
| 95 | #endif |
| 96 | |
| 97 | if (isVisible()) |
| 98 | { |
| 99 | #ifndef CS_LAYOUTTEXTS |
| 100 | model->setRowCount(0); |
| 101 | #endif |
| 102 | if ((a6 = m68k_get_reg(NULL, M68K_REG_A6)) && DBGManager_GetType()) |
| 103 | { |
| 104 | while ((Sa6 = a6) && !NumError) |
| 105 | { |
| 106 | if ((Sa6 >= (m68k_get_reg(NULL, M68K_REG_SP) - 4)) && (Sa6 < vjs.DRAM_size)) |
| 107 | { |
| 108 | a6 = GET32(jaguarMainRAM, Sa6); |
| 109 | ret = GET32(jaguarMainRAM, Sa6 + 4); |
| 110 | #ifdef CS_LAYOUTTEXTS |
| 111 | sprintf(string, "0x%06X | Ret: 0x%06X | From: %s - 0x%06X | Line: %s", Sa6, ret, (Name = DBGManager_GetFunctionName(ret)), (unsigned int)DBGManager_GetAdrFromSymbolName(Name), DBGManager_GetLineSrcFromAdr(ret, DBG_NO_TAG)); |
| 112 | CallStack += QString(string); |
| 113 | if (a6) |
| 114 | { |
| 115 | CallStack += QString("<br>"); |
| 116 | } |
| 117 | #else |
| 118 | // insert line |
| 119 | model->insertRow(NbRaw); |
| 120 | // display the function name |
| 121 | model->setItem(NbRaw, 0, new QStandardItem(QString("%1").arg((Name = DBGManager_GetFunctionName(ret)) ? Name : "(N/A)"))); |
| 122 | // display the line number |
| 123 | sprintf(msg, "%zi", DBGManager_GetNumLineFromAdr(ret, DBG_NO_TAG)); |
| 124 | model->setItem(NbRaw, 1, new QStandardItem(QString("%1").arg((msg[0] != '0') ? msg : "(N/A)"))); |
| 125 | // display the called line |
| 126 | FunctionName = QString(Name = DBGManager_GetLineSrcFromAdr(ret, DBG_NO_TAG)); |
| 127 | //FunctionName.replace(" ", " "); |
| 128 | FunctionName = FunctionName.trimmed(); |
| 129 | model->setItem(NbRaw, 2, new QStandardItem(QString("%1").arg(Name ? FunctionName : "(N/A)"))); |
| 130 | // display the return address |
| 131 | sprintf(msg, "0x%06X", ret); |
| 132 | model->setItem(NbRaw, 3, new QStandardItem(QString("%1").arg(msg))); |
| 133 | // display the source filename from called source line |
| 134 | model->setItem(NbRaw++, 4, new QStandardItem(QString("%1").arg(((Name = DBGManager_GetFullSourceFilenameFromAdr(ret, &FilenameStatus)) && !FilenameStatus) ? Name : "(N/A)"))); |
| 135 | #endif |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | NumError = 0x1; |
| 140 | } |
| 141 | } |
| 142 | #ifdef CS_LAYOUTTEXTS |
| 143 | text->clear(); |
| 144 | text->setText(CallStack); |
| 145 | #endif |
| 146 | switch (NumError) |
| 147 | { |
| 148 | case 0: |
| 149 | sprintf(msg, "Ready"); |
| 150 | Error = CS_NOERROR; |
| 151 | break; |
| 152 | |
| 153 | case 0x1: |
| 154 | sprintf(msg, "Call Stack out of range"); |
| 155 | Error = CS_ERROR; |
| 156 | break; |
| 157 | |
| 158 | default: |
| 159 | sprintf(msg, "Call Stack in limbo"); |
| 160 | Error = CS_WARNING; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | sprintf(msg, "Call Stack not available"); |
| 167 | Error = CS_NOCALLSTACK; |
| 168 | #ifdef CS_LAYOUTTEXTS |
| 169 | text->clear(); |
| 170 | #endif |
| 171 | } |
| 172 | |
| 173 | // Display status bar |
| 174 | if (Error) |
| 175 | { |
| 176 | if ((Error & CS_WARNING)) |
| 177 | { |
| 178 | statusbar->setStyleSheet("background-color: lightyellow; font: bold"); |
| 179 | } |
| 180 | else |
| 181 | { |
| 182 | statusbar->setStyleSheet("background-color: tomato; font: bold"); |
| 183 | } |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | statusbar->setStyleSheet("background-color: lightgreen; font: bold"); |
| 188 | } |
| 189 | statusbar->showMessage(QString(msg)); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | |
| 194 | // |
| 195 | void CallStackBrowserWindow::keyPressEvent(QKeyEvent * e) |
| 196 | { |
| 197 | if (e->key() == Qt::Key_Escape) |
| 198 | { |
| 199 | hide(); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | |