cutter/src/core/Cutter.h

619 lines
22 KiB
C
Raw Normal View History

#ifndef CUTTER_H
2017-09-25 12:55:41 +00:00
#define CUTTER_H
#include "core/CutterCommon.h"
#include "core/CutterDescriptions.h"
#include "common/BasicInstructionHighlighter.h"
2017-03-30 03:07:34 +00:00
#include <QMap>
#include <QDebug>
#include <QObject>
#include <QStringList>
#include <QMessageBox>
#include <QJsonDocument>
#include <QErrorMessage>
2019-08-30 15:31:30 +00:00
#include <QMutex>
2018-05-26 16:21:23 +00:00
class AsyncTaskManager;
class BasicInstructionHighlighter;
class CutterCore;
class Decompiler;
class R2Task;
class R2TaskDialog;
#include "plugins/CutterPlugin.h"
2019-02-19 18:56:59 +00:00
#include "common/BasicBlockHighlighter.h"
#include "common/R2Task.h"
#include "dialogs/R2TaskDialog.h"
2018-05-26 16:21:23 +00:00
#define Core() (CutterCore::instance())
2019-08-30 15:31:30 +00:00
class RCoreLocked;
2017-09-25 12:55:41 +00:00
class CutterCore: public QObject
{
Q_OBJECT
2019-08-30 15:31:30 +00:00
friend class RCoreLocked;
friend class R2Task;
public:
explicit CutterCore(QObject *parent = nullptr);
2017-09-25 12:55:41 +00:00
~CutterCore();
static CutterCore *instance();
2019-02-20 17:52:11 +00:00
void initialize();
2019-07-11 10:32:56 +00:00
void loadCutterRC();
2019-02-20 17:52:11 +00:00
2018-05-26 18:09:20 +00:00
AsyncTaskManager *getAsyncTaskManager() { return asyncTaskManager; }
RVA getOffset() const { return core_->offset; }
2018-06-20 09:24:28 +00:00
/* Core functions (commands) */
2017-10-09 18:08:35 +00:00
static QString sanitizeStringForCommand(QString s);
/**
* @brief send a command to radare2
* @param str the command you want to execute
* @return command output
* @note if you want to seek to an address, you should use CutterCore::seek.
*/
QString cmd(const char *str);
QString cmd(const QString &str) { return cmd(str.toUtf8().constData()); }
/**
* @brief send a command to radare2 asynchronously
* @param str the command you want to execute
* @param task a shared pointer that will be returned with the R2 command task
* @note connect to the &R2Task::finished signal to add your own logic once
* the command is finished. Use task->getResult()/getResultJson() for the
* return value.
2019-12-11 11:26:54 +00:00
* Once you have setup connections you can start the task with task->startTask()
* If you want to seek to an address, you should use CutterCore::seek.
*/
2019-12-11 11:26:54 +00:00
bool asyncCmd(const char *str, QSharedPointer<R2Task> &task);
bool asyncCmd(const QString &str, QSharedPointer<R2Task> &task) { return asyncCmd(str.toUtf8().constData(), task); }
QString cmdRaw(const QString &str);
QJsonDocument cmdj(const char *str);
QJsonDocument cmdj(const QString &str) { return cmdj(str.toUtf8().constData()); }
2019-03-23 10:54:34 +00:00
QStringList cmdList(const char *str) { return cmd(str).split(QLatin1Char('\n'), QString::SkipEmptyParts); }
QStringList cmdList(const QString &str) { return cmdList(str.toUtf8().constData()); }
2018-05-29 16:19:59 +00:00
QString cmdTask(const QString &str);
QJsonDocument cmdjTask(const QString &str);
/**
* @brief send a command to radare2 and check for ESIL errors
* @param command the command you want to execute
* @note If you want to seek to an address, you should use CutterCore::seek.
*/
void cmdEsil(const char *command);
void cmdEsil(const QString &command) { cmdEsil(command.toUtf8().constData()); }
/**
* @brief send a command to radare2 and check for ESIL errors
* @param command the command you want to execute
* @param task a shared pointer that will be returned with the R2 command task
* @note connect to the &R2Task::finished signal to add your own logic once
* the command is finished. Use task->getResult()/getResultJson() for the
* return value.
2019-12-11 11:26:54 +00:00
* Once you have setup connections you can start the task with task->startTask()
* If you want to seek to an address, you should use CutterCore::seek.
*/
2019-12-11 11:26:54 +00:00
bool asyncCmdEsil(const char *command, QSharedPointer<R2Task> &task);
bool asyncCmdEsil(const QString &command, QSharedPointer<R2Task> &task) { return asyncCmdEsil(command.toUtf8().constData(), task); }
2018-06-20 09:24:28 +00:00
QString getVersionInformation();
QJsonDocument parseJson(const char *res, const char *cmd = nullptr);
QJsonDocument parseJson(const char *res, const QString &cmd = QString())
{
return parseJson(res, cmd.isNull() ? nullptr : cmd.toLocal8Bit().constData());
}
2018-05-29 16:19:59 +00:00
QStringList autocomplete(const QString &cmd, RLinePromptType promptType, size_t limit = 4096);
2018-06-20 09:24:28 +00:00
/* Functions methods */
void renameFunction(const QString &oldName, const QString &newName);
void delFunction(RVA addr);
2017-11-27 08:22:52 +00:00
void renameFlag(QString old_name, QString new_name);
2018-06-20 09:24:28 +00:00
RAnalFunction *functionAt(ut64 addr);
RVA getFunctionStart(RVA addr);
RVA getFunctionEnd(RVA addr);
RVA getLastFunctionInstruction(RVA addr);
2018-06-20 09:24:28 +00:00
QString cmdFunctionAt(QString addr);
QString cmdFunctionAt(RVA addr);
QString createFunctionAt(RVA addr);
2018-06-20 09:24:28 +00:00
QString createFunctionAt(RVA addr, QString name);
QStringList getDisassemblyPreview(RVA address, int num_of_lines);
2018-06-20 09:24:28 +00:00
/* Flags */
void delFlag(RVA addr);
2018-03-11 16:40:52 +00:00
void delFlag(const QString &name);
2018-06-20 09:24:28 +00:00
void addFlag(RVA offset, QString name, RVA size);
/**
* @brief Get nearest flag at or before offset.
* @param offset search position
* @param flagOffsetOut adress of returned flag
* @return flag name
*/
QString nearestFlag(RVA offset, RVA *flagOffsetOut);
2018-06-20 09:24:28 +00:00
void triggerFlagsChanged();
2018-06-20 09:24:28 +00:00
/* Edition functions */
QString getInstructionBytes(RVA addr);
QString getInstructionOpcode(RVA addr);
void editInstruction(RVA addr, const QString &inst);
void nopInstruction(RVA addr);
void jmpReverse(RVA addr);
void editBytes(RVA addr, const QString &inst);
void editBytesEndian(RVA addr, const QString &bytes);
2018-08-04 18:05:56 +00:00
/* Code/Data */
void setToCode(RVA addr);
enum class StringTypeFormats { None, ASCII_LATIN1, UTF8 };
/**
* @brief Adds string at address
* That function calls the 'Cs' command
* \param addr The address of the array where the string will be applied
* \param size The size of string
* \param type The type of string
*/
void setAsString(RVA addr, int size = 0, StringTypeFormats type = StringTypeFormats::None);
/**
* @brief Removes string at address
* That function calls the 'Cs-' command
* \param addr The address of the array where the string will be applied
*/
void removeString(RVA addr);
/**
* @brief Gets string at address
* That function calls the 'ps' command
* \param addr The address of the first byte of the array
* @return string at requested address
*/
QString getString(RVA addr);
2018-08-04 18:05:56 +00:00
void setToData(RVA addr, int size, int repeat = 1);
int sizeofDataMeta(RVA addr);
2018-06-20 09:24:28 +00:00
/* Comments */
void setComment(RVA addr, const QString &cmt);
void delComment(RVA addr);
void setImmediateBase(const QString &r2BaseName, RVA offset = RVA_INVALID);
2018-02-12 09:48:06 +00:00
void setCurrentBits(int bits, RVA offset = RVA_INVALID);
2019-03-06 20:30:39 +00:00
/**
* @brief Changes immediate displacement to structure offset
* This function makes use of the "aht" command of r2 to apply structure
* offset to the immediate displacement used in the given instruction
* \param structureOffset The name of struct which will be applied
* \param offset The address of the instruction where the struct will be applied
*/
void applyStructureOffset(const QString &structureOffset, RVA offset = RVA_INVALID);
/* Classes */
QList<QString> getAllAnalClasses(bool sorted);
QList<AnalMethodDescription> getAnalClassMethods(const QString &cls);
QList<AnalBaseClassDescription> getAnalClassBaseClasses(const QString &cls);
QList<AnalVTableDescription> getAnalClassVTables(const QString &cls);
void createNewClass(const QString &cls);
void renameClass(const QString &oldName, const QString &newName);
void deleteClass(const QString &cls);
bool getAnalMethod(const QString &cls, const QString &meth, AnalMethodDescription *desc);
void renameAnalMethod(const QString &className, const QString &oldMethodName, const QString &newMethodName);
void setAnalMethod(const QString &cls, const AnalMethodDescription &meth);
2018-06-20 09:24:28 +00:00
/* File related methods */
2018-09-22 16:00:21 +00:00
bool loadFile(QString path, ut64 baddr = 0LL, ut64 mapaddr = 0LL, int perms = R_PERM_R,
int va = 0, bool loadbin = false, const QString &forceBinPlugin = QString());
bool tryFile(QString path, bool rw);
bool openFile(QString path, RVA mapaddr);
2018-06-20 09:24:28 +00:00
void loadScript(const QString &scriptname);
QJsonArray getOpenedFiles();
/* Seek functions */
void seek(QString thing);
void seek(ut64 offset);
2017-10-16 19:00:47 +00:00
void seekPrev();
void seekNext();
2018-09-06 17:32:12 +00:00
void updateSeek();
/**
* @brief Raise a memory widget showing current offset, prefer last active
* memory widget.
*/
void showMemoryWidget();
/**
* @brief Seek to \p offset and raise a memory widget showing it.
* @param offset
*/
void seekAndShow(ut64 offset);
/**
* @brief \see CutterCore::show(ut64)
* @param thing - addressable expression
*/
void seekAndShow(QString thing);
2017-10-16 19:00:47 +00:00
RVA getOffset();
RVA prevOpAddr(RVA startAddr, int count);
RVA nextOpAddr(RVA startAddr, int count);
2018-06-20 09:24:28 +00:00
/* Math functions */
ut64 math(const QString &expr);
ut64 num(const QString &expr);
QString itoa(ut64 num, int rdx = 16);
2018-06-20 09:24:28 +00:00
/* Config functions */
void setConfig(const char *k, const QString &v);
void setConfig(const QString &k, const QString &v) { setConfig(k.toUtf8().constData(), v); }
void setConfig(const char *k, int v);
void setConfig(const QString &k, int v) { setConfig(k.toUtf8().constData(), v); }
void setConfig(const char *k, bool v);
void setConfig(const QString &k, bool v) { setConfig(k.toUtf8().constData(), v); }
void setConfig(const char *k, const QVariant &v);
void setConfig(const QString &k, const QVariant &v) { setConfig(k.toUtf8().constData(), v); }
int getConfigi(const char *k);
int getConfigi(const QString &k) { return getConfigi(k.toUtf8().constData()); }
ut64 getConfigut64(const char *k);
ut64 getConfigut64(const QString &k) { return getConfigut64(k.toUtf8().constData()); }
bool getConfigb(const char *k);
bool getConfigb(const QString &k) { return getConfigb(k.toUtf8().constData()); }
QString getConfig(const char *k);
QString getConfig(const QString &k) { return getConfig(k.toUtf8().constData()); }
2018-06-20 09:24:28 +00:00
QList<QString> getColorThemes();
/* Assembly\Hexdump related methods */
QByteArray assemble(const QString &code);
QString disassemble(const QByteArray &data);
2017-06-07 15:48:36 +00:00
QString disassembleSingleInstruction(RVA addr);
2018-06-20 09:24:28 +00:00
QList<DisassemblyLine> disassembleLines(RVA offset, int lines);
static QByteArray hexStringToBytes(const QString &hex);
static QString bytesToHexString(const QByteArray &bytes);
enum class HexdumpFormats { Normal, Half, Word, Quad, Signed, Octal };
QString hexdump(RVA offset, int size, HexdumpFormats format);
QString getHexdumpPreview(RVA offset, int size);
2018-05-05 13:20:14 +00:00
void setCPU(QString arch, QString cpu, int bits);
2018-01-20 10:35:31 +00:00
void setEndianness(bool big);
2018-03-21 20:32:32 +00:00
2017-10-09 18:08:35 +00:00
/* SDB */
QList<QString> sdbList(QString path);
QList<QString> sdbListKeys(QString path);
QString sdbGet(QString path, QString key);
bool sdbSet(QString path, QString key, QString val);
2018-06-12 08:43:14 +00:00
2018-10-17 06:36:41 +00:00
/* Debug */
QJsonDocument getRegistersInfo();
QJsonDocument getRegisterValues();
QString getRegisterName(QString registerRole);
RVA getProgramCounterValue();
void setRegister(QString regName, QString regValue);
void setCurrentDebugThread(int tid);
/**
* @brief Attach to a given pid from a debug session
*/
void setCurrentDebugProcess(int pid);
2018-07-23 23:13:23 +00:00
QJsonDocument getStack(int size = 0x100);
/**
* @brief Get a list of a given process's threads
* @param pid The pid of the process, -1 for the currently debugged process
* @return JSON object result of dptj
*/
QJsonDocument getProcessThreads(int pid);
/**
* @brief Get a list of a given process's child processes
* @param pid The pid of the process, -1 for the currently debugged process
* @return JSON object result of dptj
*/
QJsonDocument getChildProcesses(int pid);
QJsonDocument getBacktrace();
2018-06-12 08:43:14 +00:00
void startDebug();
void startEmulation();
/**
* @brief attach to a remote debugger
* @param uri remote debugger uri
* @note attachedRemote(bool) signals the result
*/
void attachRemote(const QString &uri);
void attachDebug(int pid);
void stopDebug();
void suspendDebug();
void syncAndSeekProgramCounter();
2018-06-12 08:43:14 +00:00
void continueDebug();
void continueUntilCall();
void continueUntilSyscall();
2018-06-12 08:43:14 +00:00
void continueUntilDebug(QString offset);
void stepDebug();
void stepOverDebug();
void stepOutDebug();
void addBreakpoint(QString addr);
void toggleBreakpoint(RVA addr);
void toggleBreakpoint(QString addr);
void delBreakpoint(RVA addr);
void delAllBreakpoints();
void enableBreakpoint(RVA addr);
void disableBreakpoint(RVA addr);
/**
* @brief Enable or disable breakpoint tracing.
* @param index - breakpoint index to modify
* @param enabled - true if tracing should be enabled
*/
void setBreakpointTrace(int index, bool enabled);
bool isBreakpoint(const QList<RVA> &breakpoints, RVA addr);
QList<RVA> getBreakpointsAddresses();
/**
* @brief Get all breakpoinst that are belong to a functions at this address
*/
QList<RVA> getBreakpointsInFunction(RVA funcAddr);
QString getActiveDebugPlugin();
QStringList getDebugPlugins();
void setDebugPlugin(QString plugin);
bool isDebugTaskInProgress();
/**
* @brief Check if we can use output/input redirection with the currently debugged process
*/
bool isRedirectableDebugee();
bool currentlyDebugging = false;
bool currentlyEmulating = false;
int currentlyAttachedToPID = -1;
QString currentlyOpenFile;
2018-06-12 08:43:14 +00:00
/* Decompilers */
QList<Decompiler *> getDecompilers();
Decompiler *getDecompilerById(const QString &id);
/**
* Register a new decompiler
*
* The decompiler must have a unique id, otherwise this method will fail.
* The decompiler's parent will be set to this CutterCore instance, so it will automatically be freed later.
*
* @return whether the decompiler was registered successfully
*/
bool registerDecompiler(Decompiler *decompiler);
2018-09-08 07:12:08 +00:00
2017-11-28 11:56:38 +00:00
RVA getOffsetJump(RVA addr);
QJsonDocument getFileInfo();
QJsonDocument getSignatureInfo();
QJsonDocument getFileVersionInfo();
QStringList getStats();
void setGraphEmpty(bool empty);
bool isGraphEmpty();
void getOpcodes();
QList<QString> opcodes;
QList<QString> regs;
void setSettings();
void loadPDB(const QString &file);
2019-05-16 16:03:48 +00:00
QByteArray ioRead(RVA addr, int len);
QList<RVA> getSeekHistory();
2018-06-20 09:24:28 +00:00
/* Plugins */
QStringList getAsmPluginNames();
QStringList getAnalPluginNames();
2018-06-20 09:24:28 +00:00
/* Projects */
QStringList getProjectNames();
void openProject(const QString &name);
void saveProject(const QString &name);
void deleteProject(const QString &name);
static bool isProjectNameValid(const QString &name);
2018-06-20 09:24:28 +00:00
/* Widgets */
QList<RBinPluginDescription> getRBinPluginDescriptions(const QString &type = QString());
QList<RIOPluginDescription> getRIOPluginDescriptions();
QList<RCorePluginDescription> getRCorePluginDescriptions();
QList<RAsmPluginDescription> getRAsmPluginDescriptions();
QList<FunctionDescription> getAllFunctions();
QList<ImportDescription> getAllImports();
QList<ExportDescription> getAllExports();
QList<SymbolDescription> getAllSymbols();
2018-05-21 17:34:41 +00:00
QList<HeaderDescription> getAllHeaders();
2018-05-24 15:37:37 +00:00
QList<ZignatureDescription> getAllZignatures();
QList<CommentDescription> getAllComments(const QString &filterType);
QList<RelocDescription> getAllRelocs();
QList<StringDescription> getAllStrings();
QList<FlagspaceDescription> getAllFlagspaces();
QList<FlagDescription> getAllFlags(QString flagspace = QString());
QList<SectionDescription> getAllSections();
QList<SegmentDescription> getAllSegments();
QList<EntrypointDescription> getAllEntrypoint();
QList<BinClassDescription> getAllClassesFromBin();
QList<BinClassDescription> getAllClassesFromFlags();
2018-02-04 14:32:18 +00:00
QList<ResourcesDescription> getAllResources();
2018-02-26 22:26:18 +00:00
QList<VTableDescription> getAllVTables();
2019-03-06 20:30:39 +00:00
/**
* @return all loaded types
*/
2018-03-06 17:21:48 +00:00
QList<TypeDescription> getAllTypes();
2019-03-06 20:30:39 +00:00
/**
* @return all loaded primitive types
*/
QList<TypeDescription> getAllPrimitiveTypes();
2019-03-06 20:30:39 +00:00
/**
* @return all loaded unions
*/
QList<TypeDescription> getAllUnions();
2019-03-06 20:30:39 +00:00
/**
* @return all loaded structs
*/
QList<TypeDescription> getAllStructs();
2019-03-06 20:30:39 +00:00
/**
* @return all loaded enums
*/
QList<TypeDescription> getAllEnums();
2019-03-06 20:30:39 +00:00
/**
* @return all loaded typedefs
*/
QList<TypeDescription> getAllTypedefs();
/**
* @brief Fetching the C representation of a given Type
* @param name - the name or the type of the given Type / Struct
* @param category - the category of the given Type (Struct, Union, Enum, ...)
* @return The type decleration as C output
*/
QString getTypeAsC(QString name, QString category);
2019-03-06 20:30:39 +00:00
/**
* @brief Adds new types
* It first uses the r_parse_c_string() function from radare2 API to parse the
* supplied C file (in the form of a string). If there were errors, they are displayed.
* If there were no errors, it uses sdb_query_lines() function from radare2 API
* to save the parsed types returned by r_parse_c_string()
* \param str Contains the definition of the data types
* \return returns an empty QString if there was no error, else returns the error
*/
QString addTypes(const char *str);
QString addTypes(const QString &str) { return addTypes(str.toUtf8().constData()); }
2019-03-06 20:30:39 +00:00
/**
* @brief Checks if the given address is mapped to a region
* @param addr The address to be checked
* @return true if addr is mapped, false otherwise
*/
bool isAddressMapped(RVA addr);
2018-06-12 08:43:14 +00:00
QList<MemoryMapDescription> getMemoryMap();
QList<SearchDescription> getAllSearch(QString search_for, QString space);
2018-06-29 10:34:01 +00:00
BlockStatistics getBlockStatistics(unsigned int blocksCount);
QList<BreakpointDescription> getBreakpoints();
QList<ProcessDescription> getAllProcesses();
2018-07-19 14:35:46 +00:00
QList<RegisterRefDescription> getRegisterRefs();
QJsonObject getRegisterJson();
QList<VariableDescription> getVariables(RVA at);
2018-03-21 20:32:32 +00:00
QList<XrefDescription> getXRefs(RVA addr, bool to, bool whole_function,
2019-07-09 18:55:07 +00:00
const QString &filterType = QString());
2017-06-07 10:56:55 +00:00
2018-05-29 16:19:59 +00:00
QList<StringDescription> parseStringsJson(const QJsonDocument &doc);
void handleREvent(int type, void *data);
2018-06-20 09:24:28 +00:00
/* Signals related */
void triggerVarsChanged();
void triggerFunctionRenamed(const QString &prevName, const QString &newName);
void triggerRefreshAll();
void triggerAsmOptionsChanged();
void triggerGraphOptionsChanged();
void message(const QString &msg, bool debug = false);
QStringList getSectionList();
2019-08-30 15:31:30 +00:00
RCoreLocked core();
2019-02-15 12:33:23 +00:00
static QString ansiEscapeToHtml(const QString &text);
2019-02-19 18:56:59 +00:00
BasicBlockHighlighter *getBBHighlighter();
BasicInstructionHighlighter *getBIHighlighter();
2019-02-15 12:33:23 +00:00
signals:
void refreshAll();
void functionRenamed(const QString &prev_name, const QString &new_name);
void varsChanged();
void functionsChanged();
void flagsChanged();
void commentsChanged();
2018-06-12 08:43:14 +00:00
void registersChanged();
void instructionChanged(RVA offset);
void breakpointsChanged();
void refreshCodeViews();
void stackChanged();
/**
* @brief update all the widgets that are affected by rebasing in debug mode
*/
void codeRebased();
void switchedThread();
void switchedProcess();
void classNew(const QString &cls);
void classDeleted(const QString &cls);
void classRenamed(const QString &oldName, const QString &newName);
void classAttrsChanged(const QString &cls);
/**
* @brief end of current debug event received
*/
void debugProcessFinished(int pid);
void attachedRemote(bool successfully);
void projectSaved(bool successfully, const QString &name);
/**
* emitted when debugTask started or finished running
*/
void debugTaskStateChanged();
2019-03-06 20:30:39 +00:00
/**
* emitted when config regarding disassembly display changes
*/
void asmOptionsChanged();
2019-03-06 20:30:39 +00:00
/**
* emitted when config regarding graph display changes
*/
void graphOptionsChanged();
2019-03-06 20:30:39 +00:00
/**
* @brief seekChanged is emitted each time radare2 seek value is modified
* @param offset
2017-10-09 18:08:35 +00:00
*/
void seekChanged(RVA offset);
void toggleDebugView();
void newMessage(const QString &msg);
void newDebugMessage(const QString &msg);
void showMemoryWidgetRequested();
private:
QString notes;
2019-08-30 15:31:30 +00:00
/**
* Internal reference to the RCore.
* NEVER use this directly! Always use the CORE_LOCK(); macro and access it like core->...
*/
2019-02-20 17:52:11 +00:00
RCore *core_ = nullptr;
2019-08-30 15:31:30 +00:00
QMutex coreMutex;
int coreLockDepth = 0;
void *coreBed = nullptr;
2018-05-26 16:21:23 +00:00
AsyncTaskManager *asyncTaskManager;
RVA offsetPriorDebugging = RVA_INVALID;
QErrorMessage msgBox;
QList<Decompiler *> decompilers;
bool emptyGraph = false;
2019-02-19 18:56:59 +00:00
BasicBlockHighlighter *bbHighlighter;
BasicInstructionHighlighter biHighlighter;
QSharedPointer<R2Task> debugTask;
R2TaskDialog *debugTaskDialog;
};
2019-08-30 15:31:30 +00:00
class RCoreLocked
{
CutterCore * const core;
public:
explicit RCoreLocked(CutterCore *core);
RCoreLocked(const RCoreLocked &) = delete;
RCoreLocked &operator=(const RCoreLocked &) = delete;
RCoreLocked(RCoreLocked &&);
~RCoreLocked();
operator RCore *() const;
RCore *operator->() const;
};
2017-09-25 12:55:41 +00:00
#endif // CUTTER_H