cutter/src/Cutter.h

499 lines
12 KiB
C
Raw Normal View History

#ifndef CUTTER_H
2017-09-25 12:55:41 +00:00
#define CUTTER_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
#include <r_addr_interval_msvc.h>
#endif //_WIN32
2017-03-30 03:07:34 +00:00
#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-03-09 15:28:04 +00:00
#define CUTTER_VERSION "1.3"
2017-09-25 12:55:41 +00:00
#define Core() (CutterCore::getInstance())
2017-10-03 18:38:34 +00:00
typedef ut64 RVA;
#define RVA_INVALID UT64_MAX
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);
}
struct FunctionDescription
{
RVA offset;
RVA size;
QString name;
bool contains(RVA addr) const { return addr >= offset && addr < offset + size; }
};
struct ImportDescription
{
RVA plt;
int ordinal;
QString bind;
QString type;
QString name;
};
struct ExportDescription
{
RVA vaddr;
RVA paddr;
RVA size;
QString type;
QString name;
QString flag_name;
};
2018-03-06 17:21:48 +00:00
struct TypeDescription
{
QString type;
int size;
QString format;
};
struct SearchDescription
{
RVA offset;
int size;
QString code;
QString data;
};
struct SymbolDescription
{
RVA vaddr;
QString bind;
QString type;
QString name;
};
struct CommentDescription
{
RVA offset;
QString name;
};
struct RelocDescription
{
RVA vaddr;
RVA paddr;
QString type;
QString name;
};
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;
};
struct FlagspaceDescription
{
QString name;
};
struct FlagDescription
{
RVA offset;
RVA size;
QString name;
};
struct SectionDescription
{
RVA vaddr;
RVA paddr;
RVA size;
RVA vsize;
QString name;
QString flags;
};
struct EntrypointDescription
{
RVA vaddr;
RVA paddr;
RVA baddr;
RVA laddr;
RVA haddr;
QString type;
};
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;
};
struct RBinPluginDescription
{
QString name;
QString description;
QString license;
QString type;
};
struct RIOPluginDescription
{
QString name;
QString description;
QString license;
QString permissions;
};
struct RCorePluginDescription
{
QString name;
QString description;
};
struct RAsmPluginDescription
{
QString name;
QString architecture;
QString author;
QString version;
QString cpus;
QString description;
QString license;
};
struct DisassemblyLine
{
RVA offset;
QString text;
};
2017-12-23 16:42:42 +00:00
struct ClassMethodDescription
{
QString name;
RVA addr;
};
struct ClassFieldDescription
{
QString name;
RVA addr;
};
struct ClassDescription
{
QString name;
RVA addr;
ut64 index;
QList<ClassMethodDescription> methods;
QList<ClassFieldDescription> fields;
};
2018-02-04 14:32:18 +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-02-26 22:26:18 +00:00
struct VTableDescription
{
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)
Q_DECLARE_METATYPE(SearchDescription)
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:
2017-09-25 12:55:41 +00:00
explicit CutterCore(QObject *parent = 0);
~CutterCore();
static CutterCore *getInstance();
2017-10-09 18:08:35 +00:00
/* Getters */
2017-09-25 12:55:41 +00:00
RVA getOffset() const { return core_->offset; }
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);
QStringList cmdList(const QString &str) { auto l = cmd(str).split("\n"); l.removeAll(""); return l; }
QList<DisassemblyLine> disassembleLines(RVA offset, int lines);
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);
void delFlag(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 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-02-27 10:20:48 +00:00
bool loadFile(QString path, uint64_t loadaddr = 0LL, uint64_t mapaddr = 0LL, int perms = R_IO_READ, int va = 0, int idx = 0, bool loadbin = false, const QString &forceBinPlugin = nullptr);
bool tryFile(QString path, bool rw);
void analyze(int level, QList<QString> advanced);
2017-10-16 19:00:47 +00:00
// Seek functions
void seek(QString addr);
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);
// Disassembly/Graph/Hexdump/Pseudocode view priority
enum class MemoryWidgetType { Disassembly, Graph, Hexdump, Pseudocode };
MemoryWidgetType getMemoryWidgetPriority() const { return memoryWidgetPriority; }
void setMemoryWidgetPriority(MemoryWidgetType type) { memoryWidgetPriority = type; }
void triggerRaisePrioritizedMemoryWidget() { emit raisePrioritizedMemoryWidget(memoryWidgetPriority); }
2017-10-22 10:21:44 +00:00
ut64 math(const QString &expr);
2017-04-09 19:55:06 +00:00
QString itoa(ut64 num, int rdx = 16);
2017-10-09 18:08:35 +00:00
/* Config related */
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)); }
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);
QString assemble(const QString &code);
QString disassemble(const QString &hex);
2017-06-07 15:48:36 +00:00
QString disassembleSingleInstruction(RVA addr);
void setDefaultCPU();
2017-04-13 14:24:23 +00:00
void setCPU(QString arch, QString cpu, int bits, bool temporary = false);
2018-01-20 10:35:31 +00:00
void setEndianness(bool big);
void setBBSize(int size);
2017-04-09 19:55:06 +00:00
RAnalFunction *functionAt(ut64 addr);
QString cmdFunctionAt(QString addr);
QString cmdFunctionAt(RVA addr);
2017-10-09 18:08:35 +00:00
QString createFunctionAt(RVA addr, QString name);
void markString(RVA addr);
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);
2017-11-28 11:56:38 +00:00
RVA getOffsetJump(RVA addr);
QString getDecompiledCode(RVA addr);
QString getDecompiledCode(QString addr);
QJsonDocument getFileInfo();
QStringList getStats();
QString getSimpleGraph(QString function);
void getOpcodes();
QList<QString> opcodes;
QList<QString> regs;
void setSettings();
void loadPDB(const QString &file);
QList<RVA> getSeekHistory();
QStringList getAsmPluginNames();
QStringList getAnalPluginNames();
QStringList getProjectNames();
void openProject(const QString &name);
void saveProject(const QString &name);
static bool isProjectNameValid(const QString &name);
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();
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);
QList<XrefDescription> getXRefs(RVA addr, bool to, bool whole_function, const QString &filterType = QString::null);
2017-06-07 10:56:55 +00:00
void addFlag(RVA offset, QString name, RVA size);
void triggerFlagsChanged();
void triggerVarsChanged();
void triggerFunctionRenamed(const QString &prevName, const QString &newName);
void triggerRefreshAll();
void triggerAsmOptionsChanged();
void triggerGraphOptionsChanged();
void resetDefaultAsmOptions();
void saveDefaultAsmOptions();
2017-12-14 12:45:03 +00:00
void loadScript(const QString &scriptname);
2017-12-15 16:09:04 +00:00
QString getVersionInformation();
QJsonArray getOpenedFiles();
2017-12-14 12:45:03 +00:00
QList<QString> getColorThemes();
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:
QString default_arch;
QString default_cpu;
int default_bits;
MemoryWidgetType memoryWidgetPriority;
QString notes;
RCore *core_;
};
2017-10-09 18:08:35 +00:00
class ccClass : public CutterCore
{
};
2017-09-25 12:55:41 +00:00
#endif // CUTTER_H