2019-09-06 05:40:20 +00:00
|
|
|
#ifndef DECOMPILERWIDGET_H
|
|
|
|
#define DECOMPILERWIDGET_H
|
2017-12-06 23:19:14 +00:00
|
|
|
|
2019-12-19 17:58:30 +00:00
|
|
|
#include <QTextEdit>
|
2017-12-06 23:19:14 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2019-02-22 16:50:45 +00:00
|
|
|
#include "core/Cutter.h"
|
2019-03-27 08:24:54 +00:00
|
|
|
#include "MemoryDockWidget.h"
|
2019-08-27 15:27:39 +00:00
|
|
|
#include "Decompiler.h"
|
2017-12-06 23:19:14 +00:00
|
|
|
|
2018-03-21 20:32:32 +00:00
|
|
|
namespace Ui {
|
2019-09-06 05:40:20 +00:00
|
|
|
class DecompilerWidget;
|
2017-12-21 14:23:44 +00:00
|
|
|
}
|
2017-12-15 10:52:47 +00:00
|
|
|
|
|
|
|
class QTextEdit;
|
2019-07-11 13:21:54 +00:00
|
|
|
class QSyntaxHighlighter;
|
2019-07-12 08:57:07 +00:00
|
|
|
class QTextCursor;
|
2020-06-22 20:25:30 +00:00
|
|
|
class DecompilerContextMenu;
|
2019-07-12 08:57:07 +00:00
|
|
|
struct DecompiledCodeTextLine;
|
2017-12-06 23:19:14 +00:00
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
class DecompilerWidget : public MemoryDockWidget
|
2017-12-06 23:19:14 +00:00
|
|
|
{
|
2018-03-21 20:32:32 +00:00
|
|
|
Q_OBJECT
|
2019-08-07 11:38:22 +00:00
|
|
|
protected:
|
2020-06-22 20:25:30 +00:00
|
|
|
DecompilerContextMenu *mCtxMenu;
|
2017-12-06 23:19:14 +00:00
|
|
|
|
|
|
|
public:
|
2020-05-22 11:49:34 +00:00
|
|
|
explicit DecompilerWidget(MainWindow *main);
|
2019-09-06 05:40:20 +00:00
|
|
|
~DecompilerWidget();
|
2020-08-30 11:26:56 +00:00
|
|
|
static QString getWidgetType();
|
2019-08-07 11:38:22 +00:00
|
|
|
public slots:
|
2020-08-15 08:01:27 +00:00
|
|
|
void showDecompilerContextMenu(const QPoint &pt);
|
2017-12-06 23:19:14 +00:00
|
|
|
|
2019-12-19 17:58:30 +00:00
|
|
|
void highlightPC();
|
2017-12-06 23:19:14 +00:00
|
|
|
private slots:
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Copy to clipboard what's needed depending on the state of text widget.
|
|
|
|
*
|
|
|
|
* @note If something is selected in the text widget, copy selection.
|
|
|
|
* If something is highlighted, copy highlighted word.
|
|
|
|
* Otherwise, copy the line under cursor.
|
|
|
|
*/
|
2020-07-30 08:41:23 +00:00
|
|
|
void copy();
|
2019-10-12 05:50:10 +00:00
|
|
|
void fontsUpdatedSlot();
|
2017-12-06 23:19:14 +00:00
|
|
|
void colorsUpdatedSlot();
|
2019-09-06 05:40:20 +00:00
|
|
|
void refreshDecompiler();
|
2019-07-16 18:33:05 +00:00
|
|
|
void decompilerSelected();
|
2019-07-12 08:57:07 +00:00
|
|
|
void cursorPositionChanged();
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief When the synced seek is changed, this refreshes the decompiler widget if needed.
|
2020-08-29 05:15:47 +00:00
|
|
|
*
|
2020-08-15 08:01:27 +00:00
|
|
|
* Decompiler widget is not refreshed in the following two cases
|
|
|
|
* - Seek changed to an offset contained in the decompiled function.
|
|
|
|
* - Auto-refresh is disabled.
|
|
|
|
*/
|
2019-07-12 08:57:07 +00:00
|
|
|
void seekChanged();
|
2020-06-08 21:29:26 +00:00
|
|
|
void decompilationFinished(RAnnotatedCode *code);
|
2017-12-15 10:52:47 +00:00
|
|
|
|
|
|
|
private:
|
2019-09-06 05:40:20 +00:00
|
|
|
std::unique_ptr<Ui::DecompilerWidget> ui;
|
2017-12-21 14:23:44 +00:00
|
|
|
|
2019-09-01 09:06:54 +00:00
|
|
|
RefreshDeferrer *refreshDeferrer;
|
|
|
|
|
2020-08-31 06:00:55 +00:00
|
|
|
bool usingAnnotationBasedHighlighting = false;
|
|
|
|
std::unique_ptr<QSyntaxHighlighter> syntaxHighlighter;
|
2019-08-28 17:01:12 +00:00
|
|
|
bool decompilerSelectionEnabled;
|
2019-09-01 09:06:54 +00:00
|
|
|
|
|
|
|
/**
|
2020-08-29 05:15:47 +00:00
|
|
|
* True if the selected decompiler is currently running a decompilation for this widget. Once the decompilation
|
|
|
|
* is over, this should be set to false.
|
2019-09-01 09:06:54 +00:00
|
|
|
*/
|
2020-08-29 05:15:47 +00:00
|
|
|
bool decompilerBusy;
|
2017-12-21 14:23:44 +00:00
|
|
|
|
2020-08-29 05:15:47 +00:00
|
|
|
bool seekFromCursor;
|
2020-08-02 09:51:56 +00:00
|
|
|
int scrollerHorizontal;
|
|
|
|
int scrollerVertical;
|
|
|
|
RVA previousFunctionAddr;
|
2019-09-01 09:06:54 +00:00
|
|
|
RVA decompiledFunctionAddr;
|
2020-06-29 19:08:02 +00:00
|
|
|
std::unique_ptr<RAnnotatedCode, void (*)(RAnnotatedCode *)> code;
|
2019-07-12 08:57:07 +00:00
|
|
|
|
2020-08-29 05:15:47 +00:00
|
|
|
/**
|
|
|
|
* Specifies the lowest offset of instructions among all the instructions in the decompiled function.
|
|
|
|
*/
|
|
|
|
RVA lowestOffsetInCode;
|
|
|
|
/**
|
|
|
|
* Specifies the highest offset of instructions among all the instructions in the decompiled function.
|
|
|
|
*/
|
|
|
|
RVA highestOffsetInCode;
|
2019-09-01 09:06:54 +00:00
|
|
|
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
2020-08-29 05:15:47 +00:00
|
|
|
* @brief Gets the current decompiler selected by the user.
|
2020-08-15 08:01:27 +00:00
|
|
|
*
|
2020-08-29 05:15:47 +00:00
|
|
|
* @return A pointer to the currently selected decompiler
|
2020-08-15 08:01:27 +00:00
|
|
|
*/
|
2020-08-29 05:15:47 +00:00
|
|
|
Decompiler *getCurrentDecompiler();
|
|
|
|
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
2020-08-29 05:15:47 +00:00
|
|
|
* @brief Calls the function doRefresh() if the address specified is a part of the decompiled function.
|
|
|
|
*
|
|
|
|
* @param addr Address at which a change occurred.
|
2020-08-15 08:01:27 +00:00
|
|
|
*/
|
2020-08-29 05:15:47 +00:00
|
|
|
void refreshIfChanged(RVA addr);
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Refreshes the decompiler.
|
2020-08-29 05:15:47 +00:00
|
|
|
*
|
2020-08-15 08:01:27 +00:00
|
|
|
* - This does the following if the specified offset is valid
|
|
|
|
* - Decompile function that contains the specified offset.
|
|
|
|
* - Clears all selections stored for highlighting purposes.
|
|
|
|
* - Reset previousFunctionAddr with the current function's address
|
|
|
|
* and decompiledFunctionAddr with the address of the function that
|
|
|
|
* was decompiled.
|
|
|
|
* - If the offset is invalid, error message is shown in the text widget.
|
|
|
|
*
|
|
|
|
* @param addr Specified offset/offset in sync.
|
|
|
|
*/
|
2020-08-29 05:15:47 +00:00
|
|
|
void doRefresh();
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Update fonts
|
|
|
|
*/
|
2017-12-15 10:52:47 +00:00
|
|
|
void setupFonts();
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Update highlights in the text widget.
|
2020-08-29 05:15:47 +00:00
|
|
|
*
|
2020-08-15 08:01:27 +00:00
|
|
|
* These include respective highlights for:
|
|
|
|
* - Line under cursor
|
|
|
|
* - Word under cursor
|
|
|
|
* - Program Counter(PC) while debugging
|
|
|
|
*/
|
2019-07-12 08:57:07 +00:00
|
|
|
void updateSelection();
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Connect/Disconnect SIGNAL-SLOT connection that deals with changes in cursor position.
|
|
|
|
*
|
2020-08-29 05:15:47 +00:00
|
|
|
* If the argument is true, then connect the SIGNAL-SLOT connection
|
2020-08-15 08:01:27 +00:00
|
|
|
* that changes the view as cursor position gets changed in the text widget.
|
2020-08-29 05:15:47 +00:00
|
|
|
* Otherwise, disconnect the corresponding signal with slot.
|
2020-08-15 08:01:27 +00:00
|
|
|
*
|
2020-08-29 05:15:47 +00:00
|
|
|
* @param connectPositionChange
|
2020-08-15 08:01:27 +00:00
|
|
|
*/
|
2020-08-29 05:15:47 +00:00
|
|
|
void connectCursorPositionChanged(bool connectPositionChange);
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Find the current global offset in sync and update cursor
|
|
|
|
* to the position specified by this offset (found using positionForOffset() )
|
|
|
|
*/
|
2019-07-12 08:57:07 +00:00
|
|
|
void updateCursorPosition();
|
|
|
|
|
2019-06-18 13:02:41 +00:00
|
|
|
QString getWindowTitle() const override;
|
2020-08-15 08:01:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Event filter that intercept the following events:
|
|
|
|
* 1. Double click
|
|
|
|
* 2. Right click
|
|
|
|
*
|
|
|
|
* @param obj
|
|
|
|
* @param event
|
|
|
|
* @return
|
|
|
|
*/
|
2019-12-14 12:57:36 +00:00
|
|
|
bool eventFilter(QObject *obj, QEvent *event) override;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief a wrapper around CutterSeekable::seekToReference to seek to an object which is
|
|
|
|
* referenced from the address under cursor
|
|
|
|
*/
|
|
|
|
void seekToReference();
|
2019-12-19 17:58:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Retrieve the Cursor for a location as close as possible to the given address
|
|
|
|
* @param addr - an address in the decompiled function
|
|
|
|
* @return a Cursor object for the given address
|
|
|
|
*/
|
|
|
|
QTextCursor getCursorForAddress(RVA addr);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Append a highlighted line to the TextEdit
|
|
|
|
* @param extraSelection - an ExtraSelection object colored with the appropriate color
|
|
|
|
* @return True on success, otherwise False
|
|
|
|
*/
|
|
|
|
bool colorLine(QTextEdit::ExtraSelection extraSelection);
|
|
|
|
|
|
|
|
/**
|
2020-08-29 05:15:47 +00:00
|
|
|
* @brief This function is responsible for highlighting all the breakpoints in the decompiler view.
|
2019-12-19 17:58:30 +00:00
|
|
|
* It will also run when a breakpoint is added, removed or modified.
|
|
|
|
*/
|
|
|
|
void highlightBreakpoints();
|
2020-06-29 19:08:02 +00:00
|
|
|
/**
|
|
|
|
* @brief Finds the earliest offset and breakpoints within the specified range [startPos, endPos]
|
2020-08-15 08:01:27 +00:00
|
|
|
* in the specified RAnnotatedCode.
|
2020-06-29 19:08:02 +00:00
|
|
|
*
|
|
|
|
* This function is supposed to be used for finding the earliest offset and breakpoints within the specified range
|
|
|
|
* [startPos, endPos]. This will set the value of the variables 'RVA firstOffsetInLine' and 'QVector<RVA> availableBreakpoints' in
|
2020-08-15 08:01:27 +00:00
|
|
|
* the context menu.
|
2020-06-29 19:08:02 +00:00
|
|
|
*
|
|
|
|
* @param codeDecompiled - A reference to the RAnnotatedCode for the function that is decompiled.
|
|
|
|
* @param startPos - Position of the start of the range(inclusive).
|
|
|
|
* @param endPos - Position of the end of the range(inclusive).
|
|
|
|
*/
|
|
|
|
void gatherBreakpointInfo(RAnnotatedCode &codeDecompiled, size_t startPos, size_t endPos);
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Finds the offset that's closest to the specified position in the decompiled code.
|
|
|
|
*
|
|
|
|
* @note If no annotations that covers the specified position is found, the first offset in the line
|
|
|
|
* containing specified position will be returned
|
|
|
|
*
|
|
|
|
* @param pos - Position of the decompiled code.
|
|
|
|
* @return Offset for the specified position/first offset in line.
|
|
|
|
*/
|
|
|
|
ut64 offsetForPosition(size_t pos);
|
|
|
|
/**
|
|
|
|
* @brief Find the start position of the annotation with the offset that's closest to
|
|
|
|
* the specified offset
|
|
|
|
*
|
|
|
|
* @param offset
|
|
|
|
* @return Position found or SIZE_MAX
|
|
|
|
*/
|
|
|
|
size_t positionForOffset(ut64 offset);
|
|
|
|
/**
|
|
|
|
* @brief Updates the view when breakpoints are changed
|
|
|
|
*/
|
2020-08-29 05:15:47 +00:00
|
|
|
void updateBreakpoints(RVA addr);
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Set information about the breakpoints on the line in the context menu
|
|
|
|
*/
|
2020-06-29 19:08:02 +00:00
|
|
|
void setInfoForBreakpoints();
|
2020-08-15 08:01:27 +00:00
|
|
|
/**
|
|
|
|
* @brief Find the context-related annotation covering the specified position.
|
|
|
|
* If found, set the variable annotationHere in the decompiler context menu.
|
|
|
|
*
|
|
|
|
* @param pos Position of cursor in the decompiled code.
|
|
|
|
*/
|
2020-07-16 08:56:38 +00:00
|
|
|
void setAnnotationsAtCursor(size_t pos);
|
2020-08-29 05:15:47 +00:00
|
|
|
/**
|
|
|
|
* @brief Checks if the specified address is a part of the decompiled function.
|
|
|
|
*
|
|
|
|
* @param addr An offset in the binary.
|
|
|
|
* @return True if the specified is a part of the decompiled function, False otherwise.
|
|
|
|
*/
|
|
|
|
bool addressInRange(RVA addr);
|
2020-08-31 06:00:55 +00:00
|
|
|
|
|
|
|
void setCode(RAnnotatedCode *code);
|
|
|
|
|
|
|
|
void setHighlighter(bool annotationBasedHighlighter);
|
2017-12-06 23:19:14 +00:00
|
|
|
};
|
|
|
|
|
2019-09-06 05:40:20 +00:00
|
|
|
#endif // DECOMPILERWIDGET_H
|