cutter/src/Cutter.h

565 lines
14 KiB
C
Raw Normal View History

#ifndef CUTTER_H
2017-09-25 12:55:41 +00:00
#define CUTTER_H
#include "r_core.h"
2017-10-03 12:42:31 +00:00
// Workaround for compile errors on Windows
2017-03-30 03:07:34 +00:00
#ifdef _WIN32
#undef min
2017-10-03 12:42:31 +00:00
#undef max
2017-03-30 03:07:34 +00:00
#endif //_WIN32
#include <QMap>
#include <QDebug>
#include <QObject>
#include <QStringList>
#include <QMessageBox>
#include <QJsonDocument>
#define HAVE_LATEST_LIBR2 false
2017-09-25 17:51:49 +00:00
#define CutterRListForeach(list, it, type, x) \
if (list) for (it = list->head; it && ((x=(type*)it->data)); it = it->n)
#define __alert(x) QMessageBox::question (this, "Alert", QString(x), QMessageBox::Ok)
#define __question(x) (QMessageBox::Yes==QMessageBox::question (this, "Alert", QString(x), QMessageBox::Yes| QMessageBox::No))
2017-09-25 12:55:41 +00:00
#define APPNAME "Cutter"
2018-04-23 17:47:32 +00:00
#define CUTTER_VERSION "1.4"
2017-09-25 12:55:41 +00:00
#define Core() (CutterCore::getInstance())
/*!
* \brief Type to be used for all kinds of addresses/offsets in r2 address space.
*/
2017-10-03 18:38:34 +00:00
typedef ut64 RVA;
/*!
* \brief Maximum value of RVA. Do NOT use this for specifying invalid values, use RVA_INVALID instead.
*/
#define RVA_MAX UT64_MAX
/*!
* \brief Value for specifying an invalid RVA.
*/
#define RVA_INVALID RVA_MAX
2017-10-03 18:38:34 +00:00
2018-05-26 16:21:23 +00:00
class AsyncTaskManager;
class CutterCore;
#include "plugins/CutterPlugin.h"
2018-05-26 16:21:23 +00:00
2017-04-09 18:42:45 +00:00
class RCoreLocked
{
2017-04-09 19:55:06 +00:00
RCore *core;
2017-04-09 18:42:45 +00:00
public:
2017-04-09 19:55:06 +00:00
explicit RCoreLocked(RCore *core);
RCoreLocked(const RCoreLocked &) = delete;
RCoreLocked &operator=(const RCoreLocked &) = delete;
RCoreLocked(RCoreLocked &&);
~RCoreLocked();
2017-04-09 19:55:06 +00:00
operator RCore *() const;
RCore *operator->() const;
2017-10-03 18:38:34 +00:00
RVA seek(RVA offset);
RVA getSeek();
};
inline QString RAddressString(RVA addr)
{
return QString::asprintf("%#010llx", addr);
}
inline QString RSizeString(RVA size)
{
return QString::asprintf("%lld", size);
}
inline QString RHexString(RVA size)
{
return QString::asprintf("%#llx", size);
}
2018-03-21 20:32:32 +00:00
struct FunctionDescription {
RVA offset;
RVA size;
RVA nargs;
RVA nbbs;
RVA nlocals;
RVA cc;
QString calltype;
QString name;
2018-03-21 20:32:32 +00:00
bool contains(RVA addr) const
{
return addr >= offset && addr < offset + size;
}
};
2018-03-21 20:32:32 +00:00
struct ImportDescription {
RVA plt;
int ordinal;
QString bind;
QString type;
QString name;
};
2018-03-21 20:32:32 +00:00
struct ExportDescription {
RVA vaddr;
RVA paddr;
RVA size;
QString type;
QString name;
QString flag_name;
};
2018-05-21 17:34:41 +00:00
struct HeaderDescription
{
RVA vaddr;
RVA paddr;
QString value;
QString name;
};
2018-05-24 15:37:37 +00:00
struct ZignatureDescription
{
QString name;
QString bytes;
RVA cc;
RVA nbbs;
RVA edges;
RVA ebbs;
RVA offset;
QStringList refs;
};
2018-03-21 20:32:32 +00:00
struct TypeDescription {
2018-03-06 17:21:48 +00:00
QString type;
int size;
QString format;
};
2018-03-21 20:32:32 +00:00
struct SearchDescription {
RVA offset;
int size;
QString code;
QString data;
};
2018-03-21 20:32:32 +00:00
struct SymbolDescription {
RVA vaddr;
QString bind;
QString type;
QString name;
};
2018-03-21 20:32:32 +00:00
struct CommentDescription {
RVA offset;
QString name;
};
2018-03-21 20:32:32 +00:00
struct RelocDescription {
RVA vaddr;
RVA paddr;
QString type;
QString name;
};
2018-03-21 20:32:32 +00:00
struct StringDescription {
RVA vaddr;
QString string;
2018-02-10 17:16:16 +00:00
QString type;
2018-02-09 14:22:45 +00:00
ut32 length;
ut32 size;
};
2018-03-21 20:32:32 +00:00
struct FlagspaceDescription {
QString name;
};
2018-03-21 20:32:32 +00:00
struct FlagDescription {
RVA offset;
RVA size;
QString name;
};
2018-03-21 20:32:32 +00:00
struct SectionDescription {
RVA vaddr;
RVA paddr;
RVA size;
RVA vsize;
QString name;
QString flags;
2018-05-05 10:11:44 +00:00
QString entropy;
};
2018-03-21 20:32:32 +00:00
struct EntrypointDescription {
RVA vaddr;
RVA paddr;
RVA baddr;
RVA laddr;
RVA haddr;
QString type;
};
2018-03-21 20:32:32 +00:00
struct XrefDescription {
2017-06-07 10:56:55 +00:00
RVA from;
2017-11-27 16:05:10 +00:00
QString from_str;
2017-06-07 10:56:55 +00:00
RVA to;
2017-11-27 16:05:10 +00:00
QString to_str;
2017-06-07 10:56:55 +00:00
QString type;
};
2018-03-21 20:32:32 +00:00
struct RBinPluginDescription {
QString name;
QString description;
QString license;
QString type;
};
2018-03-21 20:32:32 +00:00
struct RIOPluginDescription {
QString name;
QString description;
QString license;
QString permissions;
};
2018-03-21 20:32:32 +00:00
struct RCorePluginDescription {
QString name;
QString description;
};
2018-03-21 20:32:32 +00:00
struct RAsmPluginDescription {
QString name;
QString architecture;
QString author;
QString version;
QString cpus;
QString description;
QString license;
};
2018-03-21 20:32:32 +00:00
struct DisassemblyLine {
RVA offset;
QString text;
};
2018-03-21 20:32:32 +00:00
struct ClassMethodDescription {
2017-12-23 16:42:42 +00:00
QString name;
RVA addr;
};
2018-03-21 20:32:32 +00:00
struct ClassFieldDescription {
2017-12-23 16:42:42 +00:00
QString name;
RVA addr;
};
2018-03-21 20:32:32 +00:00
struct ClassDescription {
2017-12-23 16:42:42 +00:00
QString name;
RVA addr;
ut64 index;
QList<ClassMethodDescription> methods;
QList<ClassFieldDescription> fields;
};
2018-03-21 20:32:32 +00:00
struct ResourcesDescription {
2018-02-09 13:19:36 +00:00
int name;
2018-02-04 14:32:18 +00:00
RVA vaddr;
ut64 index;
QString type;
ut64 size;
QString lang;
};
2018-03-21 20:32:32 +00:00
struct VTableDescription {
2018-02-26 22:26:18 +00:00
RVA addr;
QList<ClassMethodDescription> methods;
};
Q_DECLARE_METATYPE(FunctionDescription)
Q_DECLARE_METATYPE(ImportDescription)
Q_DECLARE_METATYPE(ExportDescription)
Q_DECLARE_METATYPE(SymbolDescription)
Q_DECLARE_METATYPE(CommentDescription)
Q_DECLARE_METATYPE(RelocDescription)
Q_DECLARE_METATYPE(StringDescription)
Q_DECLARE_METATYPE(FlagspaceDescription)
Q_DECLARE_METATYPE(FlagDescription)
Q_DECLARE_METATYPE(XrefDescription)
Q_DECLARE_METATYPE(EntrypointDescription)
Q_DECLARE_METATYPE(RBinPluginDescription)
Q_DECLARE_METATYPE(RIOPluginDescription)
Q_DECLARE_METATYPE(RCorePluginDescription)
Q_DECLARE_METATYPE(RAsmPluginDescription)
2017-12-23 16:42:42 +00:00
Q_DECLARE_METATYPE(ClassMethodDescription)
Q_DECLARE_METATYPE(ClassFieldDescription)
Q_DECLARE_METATYPE(ClassDescription)
Q_DECLARE_METATYPE(const ClassDescription *)
Q_DECLARE_METATYPE(const ClassMethodDescription *)
Q_DECLARE_METATYPE(const ClassFieldDescription *)
2018-02-09 13:19:36 +00:00
Q_DECLARE_METATYPE(ResourcesDescription)
2018-02-26 22:26:18 +00:00
Q_DECLARE_METATYPE(VTableDescription)
2018-03-06 17:21:48 +00:00
Q_DECLARE_METATYPE(TypeDescription)
2018-05-21 17:34:41 +00:00
Q_DECLARE_METATYPE(HeaderDescription)
2018-05-24 15:37:37 +00:00
Q_DECLARE_METATYPE(ZignatureDescription)
Q_DECLARE_METATYPE(SearchDescription)
Q_DECLARE_METATYPE(SectionDescription)
2017-09-25 12:55:41 +00:00
class CutterCore: public QObject
{
Q_OBJECT
2017-10-09 18:08:35 +00:00
friend class ccClass;
public:
explicit CutterCore(QObject *parent = nullptr);
2017-09-25 12:55:41 +00:00
~CutterCore();
static CutterCore *getInstance();
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);
QString cmd(const QString &str);
QString cmdRaw(const QString &str);
QJsonDocument cmdj(const QString &str);
2018-03-21 20:32:32 +00:00
QStringList cmdList(const QString &str)
{
auto l = cmd(str).split("\n");
l.removeAll("");
return l;
}
2018-05-29 16:19:59 +00:00
QString cmdTask(const QString &str);
QJsonDocument cmdjTask(const QString &str);
2018-06-20 09:24:28 +00:00
QString getVersionInformation();
2018-05-29 16:19:59 +00:00
QJsonDocument parseJson(const char *res, const QString &cmd = QString());
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);
QString cmdFunctionAt(QString addr);
QString cmdFunctionAt(RVA addr);
QString createFunctionAt(RVA addr, QString name);
void markString(RVA addr);
/* 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);
void triggerFlagsChanged();
2018-06-20 09:24:28 +00:00
/* Edition functions */
void editInstruction(RVA addr, const QString &inst);
void nopInstruction(RVA addr);
void jmpReverse(RVA addr);
void editBytes(RVA addr, const QString &inst);
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);
2018-06-20 09:24:28 +00:00
/* File related methods */
2018-05-05 13:20:14 +00:00
bool loadFile(QString path, ut64 baddr = 0LL, ut64 mapaddr = 0LL, int perms = R_IO_READ,
int va = 0, bool loadbin = false, const QString &forceBinPlugin = nullptr);
bool tryFile(QString path, bool rw);
2018-06-20 09:24:28 +00:00
void openFile(QString path, RVA mapaddr);
void loadScript(const QString &scriptname);
QJsonArray getOpenedFiles();
/* Analysis functions */
void analyze(int level, QList<QString> advanced);
2017-10-16 19:00:47 +00:00
2018-06-20 09:24:28 +00:00
/* Seek functions */
void seek(QString thing);
void seek(ut64 offset);
2017-10-16 19:00:47 +00:00
void seekPrev();
void seekNext();
RVA getOffset();
RVA prevOpAddr(RVA startAddr, int count);
RVA nextOpAddr(RVA startAddr, int count);
2018-06-20 09:24:28 +00:00
/* Disassembly/Graph/Hexdump/Pseudocode view priority */
enum class MemoryWidgetType { Disassembly, Graph, Hexdump, Pseudocode };
2018-03-21 20:32:32 +00:00
MemoryWidgetType getMemoryWidgetPriority() const
{
return memoryWidgetPriority;
}
void setMemoryWidgetPriority(MemoryWidgetType type)
{
memoryWidgetPriority = type;
}
void triggerRaisePrioritizedMemoryWidget()
{
emit raisePrioritizedMemoryWidget(memoryWidgetPriority);
}
2017-10-22 10:21:44 +00:00
2018-06-20 09:24:28 +00:00
/* Math functions */
ut64 math(const QString &expr);
2017-04-09 19:55:06 +00:00
QString itoa(ut64 num, int rdx = 16);
2018-06-20 09:24:28 +00:00
/* Config functions */
void setConfig(const QString &k, const QString &v);
void setConfig(const QString &k, int v);
void setConfig(const QString &k, bool v);
void setConfig(const QString &k, const char *v) { setConfig(k, QString(v)); }
void setConfig(const QString &k, const QVariant &v);
2017-09-02 08:17:48 +00:00
int getConfigi(const QString &k);
bool getConfigb(const QString &k);
2017-09-02 08:17:48 +00:00
QString getConfig(const QString &k);
2018-06-20 09:24:28 +00:00
QList<QString> getColorThemes();
2018-06-20 09:24:28 +00:00
/* Assembly related methods */
QString assemble(const QString &code);
QString disassemble(const QString &hex);
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);
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);
void setBBSize(int size);
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);
int get_size();
ulong get_baddr();
QList<QList<QString>> get_exec_sections();
QString getOffsetInfo(QString addr);
QJsonDocument getRegistersInfo();
QJsonDocument getRegisterValues();
QString getRegisterName(QString registerRole);
void setRegister(QString regName, QString regValue);
QJsonDocument getStack(int size = 0x40);
QJsonDocument getBacktrace();
2017-11-28 11:56:38 +00:00
RVA getOffsetJump(RVA addr);
QString getDecompiledCode(RVA addr);
QString getDecompiledCode(QString addr);
QJsonDocument getFileInfo();
QJsonDocument getSignatureInfo();
QJsonDocument getFileVersionInfo();
QStringList getStats();
QString getSimpleGraph(QString function);
void getOpcodes();
QList<QString> opcodes;
QList<QString> regs;
void setSettings();
void loadPDB(const QString &file);
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 = nullptr);
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 = NULL);
QList<SectionDescription> getAllSections();
QList<EntrypointDescription> getAllEntrypoint();
QList<ClassDescription> getAllClassesFromBin();
QList<ClassDescription> getAllClassesFromFlags();
2018-02-04 14:32:18 +00:00
QList<ResourcesDescription> getAllResources();
2018-02-26 22:26:18 +00:00
QList<VTableDescription> getAllVTables();
2018-03-06 17:21:48 +00:00
QList<TypeDescription> getAllTypes();
QList<SearchDescription> getAllSearch(QString search_for, QString space);
2018-03-21 20:32:32 +00:00
QList<XrefDescription> getXRefs(RVA addr, bool to, bool whole_function,
const QString &filterType = QString::null);
2017-06-07 10:56:55 +00:00
2018-05-29 16:19:59 +00:00
QList<StringDescription> parseStringsJson(const QJsonDocument &doc);
2018-06-25 19:28:34 +00:00
QList<FunctionDescription> parseFunctionsJson(const QJsonDocument &doc);
2018-05-29 16:19:59 +00:00
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();
2018-05-29 16:19:59 +00:00
RCoreTask *startTask(const QString &cmd);
void joinTask(RCoreTask *task);
void deleteTask(RCoreTask *task);
void setCutterPlugins(QList<CutterPlugin*> plugins);
QList<CutterPlugin*> getCutterPlugins();
RCoreLocked core() const;
signals:
void refreshAll();
void functionRenamed(const QString &prev_name, const QString &new_name);
void varsChanged();
void functionsChanged();
void flagsChanged();
void commentsChanged();
void instructionChanged(RVA offset);
void notesChanged(const QString &notes);
void projectSaved(const QString &name);
/*!
* emitted when config regarding disassembly display changes
*/
void asmOptionsChanged();
/*!
* emitted when config regarding graph display changes
*/
void graphOptionsChanged();
2017-10-09 18:08:35 +00:00
/*!
* \brief seekChanged is emitted each time radare2 seek value is modified
* \param offset
*/
void seekChanged(RVA offset);
void raisePrioritizedMemoryWidget(CutterCore::MemoryWidgetType type);
public slots:
private:
MemoryWidgetType memoryWidgetPriority;
QString notes;
RCore *core_;
2018-05-26 16:21:23 +00:00
AsyncTaskManager *asyncTaskManager;
QList<CutterPlugin*> plugins;
};
2017-10-09 18:08:35 +00:00
class ccClass : public CutterCore
{
};
2017-09-25 12:55:41 +00:00
#endif // CUTTER_H