cutter/src/core/Cutter.cpp

2675 lines
70 KiB
C++
Raw Normal View History

#include <QJsonArray>
#include <QJsonObject>
#include <QRegularExpression>
#include <QDir>
#include <QCoreApplication>
2018-10-17 07:55:53 +00:00
#include "common/TempConfig.h"
#include "common/Configuration.h"
#include "common/AsyncTask.h"
#include "common/R2Task.h"
#include "common/Json.h"
#include "core/Cutter.h"
2019-03-24 09:39:54 +00:00
#include "r_asm.h"
2017-09-25 12:55:41 +00:00
#include "sdb.h"
2019-02-20 17:52:11 +00:00
Q_GLOBAL_STATIC(CutterCore, uniqueInstance)
#define R_JSON_KEY(name) static const QString name = QStringLiteral(#name)
namespace RJsonKey {
R_JSON_KEY(addr);
R_JSON_KEY(addr_end);
R_JSON_KEY(baddr);
R_JSON_KEY(bind);
R_JSON_KEY(blocks);
R_JSON_KEY(blocksize);
R_JSON_KEY(bytes);
R_JSON_KEY(calltype);
R_JSON_KEY(cc);
R_JSON_KEY(classname);
R_JSON_KEY(code);
R_JSON_KEY(comment);
R_JSON_KEY(comments);
R_JSON_KEY(cost);
R_JSON_KEY(data);
R_JSON_KEY(description);
R_JSON_KEY(ebbs);
R_JSON_KEY(edges);
R_JSON_KEY(enabled);
R_JSON_KEY(entropy);
R_JSON_KEY(fcn_addr);
R_JSON_KEY(fcn_name);
R_JSON_KEY(fields);
R_JSON_KEY(file);
R_JSON_KEY(flags);
R_JSON_KEY(flagname);
R_JSON_KEY(format);
R_JSON_KEY(from);
R_JSON_KEY(functions);
R_JSON_KEY(graph);
R_JSON_KEY(haddr);
R_JSON_KEY(hw);
R_JSON_KEY(in_functions);
R_JSON_KEY(index);
R_JSON_KEY(jump);
R_JSON_KEY(laddr);
R_JSON_KEY(lang);
R_JSON_KEY(len);
R_JSON_KEY(length);
R_JSON_KEY(license);
R_JSON_KEY(methods);
R_JSON_KEY(name);
R_JSON_KEY(nargs);
R_JSON_KEY(nbbs);
R_JSON_KEY(nlocals);
R_JSON_KEY(offset);
R_JSON_KEY(opcode);
R_JSON_KEY(opcodes);
R_JSON_KEY(ordinal);
R_JSON_KEY(outdegree);
R_JSON_KEY(paddr);
R_JSON_KEY(path);
R_JSON_KEY(perm);
R_JSON_KEY(pid);
R_JSON_KEY(plt);
R_JSON_KEY(prot);
R_JSON_KEY(ref);
R_JSON_KEY(refs);
R_JSON_KEY(reg);
R_JSON_KEY(rwx);
R_JSON_KEY(section);
R_JSON_KEY(sections);
R_JSON_KEY(size);
R_JSON_KEY(stackframe);
R_JSON_KEY(status);
R_JSON_KEY(string);
R_JSON_KEY(strings);
R_JSON_KEY(symbols);
R_JSON_KEY(text);
R_JSON_KEY(to);
R_JSON_KEY(trace);
R_JSON_KEY(type);
R_JSON_KEY(uid);
R_JSON_KEY(vaddr);
R_JSON_KEY(value);
R_JSON_KEY(vsize);
}
#undef R_JSON_KEY
RCoreLocked::RCoreLocked(RCore *core)
: core(core)
{
r_th_lock_enter(core->lock);
}
2017-04-09 19:55:06 +00:00
RCoreLocked::RCoreLocked(RCoreLocked &&o)
2017-04-09 18:42:45 +00:00
: core(o.core)
{
2017-04-09 18:42:45 +00:00
o.core = nullptr;
}
RCoreLocked::~RCoreLocked()
{
r_th_lock_leave(core->lock);
}
2017-04-09 19:55:06 +00:00
RCoreLocked::operator RCore *() const
{
return core;
}
2017-04-09 19:55:06 +00:00
RCore *RCoreLocked::operator->() const
{
return core;
}
2017-09-25 12:55:41 +00:00
RCoreLocked CutterCore::core() const
{
return RCoreLocked(this->core_);
}
#define CORE_LOCK() RCoreLocked core_lock__(this->core_)
static void cutterREventCallback(REvent *, int type, void *user, void *data)
{
auto core = reinterpret_cast<CutterCore *>(user);
core->handleREvent(type, data);
}
2017-09-25 12:55:41 +00:00
CutterCore::CutterCore(QObject *parent) :
QObject(parent)
2019-02-20 17:52:11 +00:00
{
}
CutterCore *CutterCore::instance()
{
return uniqueInstance;
}
void CutterCore::initialize()
{
2017-04-09 19:55:06 +00:00
r_cons_new(); // initialize console
core_ = r_core_new();
r_event_hook(core_->anal->ev, R_EVENT_ALL, cutterREventCallback, this);
#if defined(APPIMAGE) || defined(MACOS_R2_BUNDLED)
auto prefix = QDir(QCoreApplication::applicationDirPath());
2018-09-05 05:46:40 +00:00
#ifdef APPIMAGE
2018-09-30 20:00:53 +00:00
// Executable is in appdir/bin
prefix.cdUp();
qInfo() << "Setting r2 prefix =" << prefix.absolutePath() << " for AppImage.";
2018-09-05 05:46:40 +00:00
#else // MACOS_R2_BUNDLED
2018-09-30 20:00:53 +00:00
// Executable is in Contents/MacOS, prefix is Contents/Resources/r2
prefix.cdUp();
prefix.cd("Resources");
prefix.cd("r2");
qInfo() << "Setting r2 prefix =" << prefix.absolutePath() << " for macOS Application Bundle.";
2018-09-05 05:46:40 +00:00
#endif
setConfig("dir.prefix", prefix.absolutePath());
#endif
2018-05-26 16:21:23 +00:00
2018-07-14 19:04:13 +00:00
r_core_loadlibs(this->core_, R_CORE_LOADLIBS_ALL, NULL);
// IMPLICIT r_bin_iobind (core_->bin, core_->io);
// Otherwise r2 may ask the user for input and Cutter would freeze
setConfig("scr.interactive", false);
2019-02-19 18:56:59 +00:00
// Initialize graph node highlighter
bbHighlighter = new BasicBlockHighlighter();
// Initialize Async tasks manager
2018-05-26 16:21:23 +00:00
asyncTaskManager = new AsyncTaskManager(this);
}
2017-09-25 12:55:41 +00:00
QList<QString> CutterCore::sdbList(QString path)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
QList<QString> list = QList<QString>();
2017-04-09 19:55:06 +00:00
Sdb *root = sdb_ns_path(core_->sdb, path.toUtf8().constData(), 0);
2018-03-21 20:32:32 +00:00
if (root) {
void *vsi;
ls_iter_t *iter;
2018-03-21 20:32:32 +00:00
ls_foreach(root->ns, iter, vsi) {
2017-04-09 19:55:06 +00:00
SdbNs *nsi = (SdbNs *)vsi;
list << nsi->name;
}
}
return list;
}
2017-09-25 12:55:41 +00:00
QList<QString> CutterCore::sdbListKeys(QString path)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
QList<QString> list = QList<QString>();
2017-04-09 19:55:06 +00:00
Sdb *root = sdb_ns_path(core_->sdb, path.toUtf8().constData(), 0);
2018-03-21 20:32:32 +00:00
if (root) {
void *vsi;
ls_iter_t *iter;
SdbList *l = sdb_foreach_list(root, false);
2018-03-21 20:32:32 +00:00
ls_foreach(l, iter, vsi) {
2017-04-09 19:55:06 +00:00
SdbKv *nsi = (SdbKv *)vsi;
list << reinterpret_cast<char *>(nsi->base.key);
}
}
return list;
}
2017-09-25 12:55:41 +00:00
QString CutterCore::sdbGet(QString path, QString key)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
2017-04-09 19:55:06 +00:00
Sdb *db = sdb_ns_path(core_->sdb, path.toUtf8().constData(), 0);
2018-03-21 20:32:32 +00:00
if (db) {
const char *val = sdb_const_get(db, key.toUtf8().constData(), 0);
if (val && *val)
return val;
}
2019-03-23 10:54:34 +00:00
return QString();
}
2017-09-25 12:55:41 +00:00
bool CutterCore::sdbSet(QString path, QString key, QString val)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
2017-04-09 19:55:06 +00:00
Sdb *db = sdb_ns_path(core_->sdb, path.toUtf8().constData(), 1);
if (!db) return false;
2017-04-09 19:55:06 +00:00
return sdb_set(db, key.toUtf8().constData(), val.toUtf8().constData(), 0);
}
2017-09-25 12:55:41 +00:00
CutterCore::~CutterCore()
2017-04-09 19:55:06 +00:00
{
2019-02-19 18:56:59 +00:00
delete bbHighlighter;
r_core_free(this->core_);
r_cons_free();
}
2017-09-25 12:55:41 +00:00
QString CutterCore::sanitizeStringForCommand(QString s)
{
static const QRegExp regexp(";|@");
return s.replace(regexp, QStringLiteral("_"));
}
2018-01-27 13:11:30 +00:00
/**
* @brief CutterCore::cmd send a command to radare2
* @param str the command you want to execute
* Note that if you want to seek to an address, you should use CutterCore::seek
* @return command output
*/
QString CutterCore::cmd(const char *str)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
2017-10-09 18:08:35 +00:00
RVA offset = core_->offset;
2018-05-29 16:19:59 +00:00
r_core_task_sync_begin(core_);
char *res = r_core_cmd_str(this->core_, str);
2018-05-29 16:19:59 +00:00
r_core_task_sync_end(core_);
QString o = QString(res ? res : "");
2017-03-30 03:07:34 +00:00
r_mem_free(res);
2018-03-21 20:32:32 +00:00
if (offset != core_->offset) {
updateSeek();
2017-10-09 18:08:35 +00:00
}
return o;
}
QString CutterCore::cmdRaw(const QString &str)
{
QString cmdStr = str;
cmdStr.replace('\"', QStringLiteral("\\\""));
return cmd(cmdStr.prepend('\"').append('\"'));
}
QJsonDocument CutterCore::cmdj(const char *str)
{
CORE_LOCK();
2018-05-29 16:19:59 +00:00
r_core_task_sync_begin(core_);
char *res = r_core_cmd_str(this->core_, str);
2018-05-29 16:19:59 +00:00
r_core_task_sync_end(core_);
QJsonDocument doc = parseJson(res, str);
r_mem_free(res);
return doc;
}
2018-05-29 16:19:59 +00:00
QString CutterCore::cmdTask(const QString &str)
{
2018-07-22 13:04:13 +00:00
R2Task task(str);
task.startTask();
task.joinTask();
return task.getResult();
2018-05-29 16:19:59 +00:00
}
QJsonDocument CutterCore::cmdjTask(const QString &str)
{
2018-07-22 13:04:13 +00:00
R2Task task(str);
task.startTask();
task.joinTask();
return parseJson(task.getResultRaw(), str);
2018-05-29 16:19:59 +00:00
}
QJsonDocument CutterCore::parseJson(const char *res, const char *cmd)
2018-05-29 16:19:59 +00:00
{
QByteArray json(res);
if (json.isEmpty()) {
return QJsonDocument();
}
QJsonParseError jsonError;
QJsonDocument doc = QJsonDocument::fromJson(json, &jsonError);
2018-03-21 20:32:32 +00:00
if (jsonError.error != QJsonParseError::NoError) {
if (cmd) {
eprintf("Failed to parse JSON for command \"%s\": %s\n", cmd,
2018-05-29 16:19:59 +00:00
jsonError.errorString().toLocal8Bit().constData());
} else {
eprintf("Failed to parse JSON: %s\n", jsonError.errorString().toLocal8Bit().constData());
}
eprintf("%s\n", json.constData());
}
return doc;
}
2018-06-20 09:24:28 +00:00
/**
* @brief CutterCore::loadFile
* Load initial file. TODO Maybe use the "o" commands?
* @param path File path
* @param baddr Base (RBin) address
* @param mapaddr Map address
* @param perms
* @param va
* @param loadbin Load RBin information
* @param forceBinPlugin
* @return
*/
2018-05-05 13:20:14 +00:00
bool CutterCore::loadFile(QString path, ut64 baddr, ut64 mapaddr, int perms, int va,
bool loadbin, const QString &forceBinPlugin)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
RCoreFile *f;
2018-05-05 13:20:14 +00:00
r_config_set_i(core_->config, "io.va", va);
2018-02-27 10:20:48 +00:00
f = r_core_file_open(core_, path.toUtf8().constData(), perms, mapaddr);
2018-03-21 20:32:32 +00:00
if (!f) {
2017-04-09 19:55:06 +00:00
eprintf("r_core_file_open failed\n");
return false;
}
2018-03-21 20:32:32 +00:00
if (!forceBinPlugin.isNull()) {
r_bin_force_plugin(r_core_get_bin(core_), forceBinPlugin.toUtf8().constData());
}
2018-05-05 13:20:14 +00:00
if (loadbin && va) {
if (!r_core_bin_load(core_, path.toUtf8().constData(), baddr)) {
eprintf("CANNOT GET RBIN INFO\n");
}
#if HAVE_MULTIPLE_RBIN_FILES_INSIDE_SELECT_WHICH_ONE
2018-03-21 20:32:32 +00:00
if (!r_core_file_open(core, path.toUtf8(), R_IO_READ | (rw ? R_IO_WRITE : 0, mapaddr))) {
2017-04-09 19:55:06 +00:00
eprintf("Cannot open file\n");
2018-03-21 20:32:32 +00:00
} else {
// load RBin information
// XXX only for sub-bins
2018-05-05 13:20:14 +00:00
r_core_bin_load(core, path.toUtf8(), baddr);
2017-04-09 19:55:06 +00:00
r_bin_select_idx(core_->bin, NULL, idx);
}
#endif
2018-03-21 20:32:32 +00:00
} else {
// Not loading RBin info coz va = false
}
auto iod = core_->io ? core_->io->desc : NULL;
auto debug = core_->file && iod && (core_->file->fd == iod->fd) && iod->plugin && \
2018-03-21 20:32:32 +00:00
iod->plugin->isdbg;
if (!debug && r_flag_get (core_->flags, "entry0")) {
2018-03-21 20:32:32 +00:00
r_core_cmd0 (core_, "s entry0");
}
2018-09-22 16:00:21 +00:00
if (perms & R_PERM_W) {
2018-03-21 20:32:32 +00:00
r_core_cmd0 (core_, "omfg+w");
}
ut64 hashLimit = getConfigut64("cfg.hashlimit");
2019-03-24 09:39:54 +00:00
r_bin_file_hash(core_->bin, hashLimit, path.toUtf8().constData(), NULL);
2017-04-09 19:55:06 +00:00
fflush(stdout);
return true;
}
2018-06-20 09:24:28 +00:00
bool CutterCore::tryFile(QString path, bool rw)
{
CORE_LOCK();
RCoreFile *cf;
2018-09-22 16:00:21 +00:00
int flags = R_PERM_R;
if (rw) flags = R_PERM_RW;
2018-06-20 09:24:28 +00:00
cf = r_core_file_open(this->core_, path.toUtf8().constData(), flags, 0LL);
if (!cf) {
return false;
}
r_core_file_close (this->core_, cf);
return true;
}
2019-03-06 20:30:39 +00:00
/**
* @brief Opens a file using r2 API
* @param path Path to file
* @param mapaddr Map Address
* @return bool
*/
bool CutterCore::openFile(QString path, RVA mapaddr)
2018-06-20 09:24:28 +00:00
{
CORE_LOCK();
RVA addr = mapaddr != RVA_INVALID ? mapaddr : 0;
ut64 baddr = Core()->getFileInfo().object()["bin"].toObject()["baddr"].toVariant().toULongLong();
if (r_core_file_open(core_, path.toUtf8().constData(), R_PERM_RX, addr)) {
r_core_bin_load(core_, path.toUtf8().constData(), baddr);
2018-06-20 09:24:28 +00:00
} else {
return false;
2018-06-20 09:24:28 +00:00
}
return true;
2018-06-20 09:24:28 +00:00
}
void CutterCore::renameFunction(const QString &oldName, const QString &newName)
2017-04-09 19:55:06 +00:00
{
cmdRaw("afn " + newName + " " + oldName);
emit functionRenamed(oldName, newName);
2017-11-27 08:22:52 +00:00
}
void CutterCore::delFunction(RVA addr)
{
cmd("af- " + RAddressString(addr));
emit functionsChanged();
}
2017-11-27 08:22:52 +00:00
void CutterCore::renameFlag(QString old_name, QString new_name)
{
cmdRaw("fr " + old_name + " " + new_name);
emit flagsChanged();
}
void CutterCore::delFlag(RVA addr)
{
cmd("f-@" + RAddressString(addr));
emit flagsChanged();
}
2018-03-11 16:40:52 +00:00
void CutterCore::delFlag(const QString &name)
{
cmdRaw("f-" + name);
emit flagsChanged();
}
QString CutterCore::getInstructionBytes(RVA addr)
{
return cmdj("aoj @ " + RAddressString(addr)).array().first().toObject()[RJsonKey::bytes].toString();
}
QString CutterCore::getInstructionOpcode(RVA addr)
{
return cmdj("aoj @ " + RAddressString(addr)).array().first().toObject()[RJsonKey::opcode].toString();
}
void CutterCore::editInstruction(RVA addr, const QString &inst)
{
2018-02-27 10:20:48 +00:00
cmd("wa " + inst + " @ " + RAddressString(addr));
emit instructionChanged(addr);
}
void CutterCore::nopInstruction(RVA addr)
{
cmd("wao nop @ " + RAddressString(addr));
emit instructionChanged(addr);
}
void CutterCore::jmpReverse(RVA addr)
{
cmd("wao recj @ " + RAddressString(addr));
emit instructionChanged(addr);
}
void CutterCore::editBytes(RVA addr, const QString &bytes)
{
2018-02-27 10:20:48 +00:00
cmd("wx " + bytes + " @ " + RAddressString(addr));
emit instructionChanged(addr);
}
void CutterCore::editBytesEndian(RVA addr, const QString &bytes)
{
cmd("wv " + bytes + " @ " + RAddressString(addr));
emit stackChanged();
}
2018-08-04 18:05:56 +00:00
void CutterCore::setToCode(RVA addr)
{
cmd("Cd- @ " + RAddressString(addr));
emit instructionChanged(addr);
}
2018-11-16 21:27:07 +00:00
void CutterCore::setAsString(RVA addr)
{
cmd("Cs @ " + RAddressString(addr));
emit instructionChanged(addr);
}
2018-08-04 18:05:56 +00:00
void CutterCore::setToData(RVA addr, int size, int repeat)
{
if (size <= 0 || repeat <= 0) {
return;
}
cmd("Cd- @ " + RAddressString(addr));
cmd(QString::asprintf("Cd %d %d @ %lld", size, repeat, addr));
emit instructionChanged(addr);
}
int CutterCore::sizeofDataMeta(RVA addr)
{
bool ok;
int size = cmd("Cd. @ " + RAddressString(addr)).toInt(&ok);
return (ok ? size : 0);
}
void CutterCore::setComment(RVA addr, const QString &cmt)
{
2017-11-08 09:18:07 +00:00
cmd("CCu base64:" + cmt.toLocal8Bit().toBase64() + " @ " + QString::number(addr));
emit commentsChanged();
}
void CutterCore::delComment(RVA addr)
2017-04-09 19:55:06 +00:00
{
cmd("CC- @ " + QString::number(addr));
emit commentsChanged();
}
void CutterCore::setImmediateBase(const QString &r2BaseName, RVA offset)
{
2018-03-21 20:32:32 +00:00
if (offset == RVA_INVALID) {
offset = getOffset();
}
this->cmd("ahi " + r2BaseName + " @ " + QString::number(offset));
emit instructionChanged(offset);
}
2018-02-12 09:48:06 +00:00
void CutterCore::setCurrentBits(int bits, RVA offset)
{
2018-03-21 20:32:32 +00:00
if (offset == RVA_INVALID) {
2018-02-12 09:48:06 +00:00
offset = getOffset();
}
this->cmd("ahb " + QString::number(bits) + " @ " + QString::number(offset));
emit instructionChanged(offset);
}
void CutterCore::applyStructureOffset(const QString &structureOffset, RVA offset)
{
if (offset == RVA_INVALID) {
offset = getOffset();
}
this->cmdRaw("ta " + structureOffset + " @ " + QString::number(offset));
emit instructionChanged(offset);
}
2018-01-27 13:11:30 +00:00
void CutterCore::seek(ut64 offset)
2017-04-09 19:55:06 +00:00
{
2017-10-16 19:00:47 +00:00
// Slower than using the API, but the API is not complete
// which means we either have to duplicate code from radare2
// here, or refactor radare2 API.
CORE_LOCK();
2018-03-21 20:32:32 +00:00
if (offset == RVA_INVALID) {
2018-01-27 13:11:30 +00:00
return;
}
cmd(QString("s %1").arg(offset));
// cmd already does emit seekChanged(core_->offset);
triggerRaisePrioritizedMemoryWidget();
}
void CutterCore::seek(QString thing)
2017-04-09 19:55:06 +00:00
{
cmdRaw(QString("s %1").arg(thing));
triggerRaisePrioritizedMemoryWidget();
}
2017-10-16 19:00:47 +00:00
void CutterCore::seekPrev()
{
2017-10-16 19:00:47 +00:00
cmd("s-");
triggerRaisePrioritizedMemoryWidget();
}
2017-10-16 19:00:47 +00:00
void CutterCore::seekNext()
{
cmd("s+");
triggerRaisePrioritizedMemoryWidget();
2017-10-16 19:00:47 +00:00
}
2018-09-06 17:32:12 +00:00
void CutterCore::updateSeek()
{
emit seekChanged(core_->offset);
}
RVA CutterCore::prevOpAddr(RVA startAddr, int count)
{
CORE_LOCK();
bool ok;
2018-03-21 20:32:32 +00:00
RVA offset = cmd("/O " + QString::number(count) + " @ " + QString::number(startAddr)).toULongLong(
&ok, 16);
return ok ? offset : startAddr - count;
}
RVA CutterCore::nextOpAddr(RVA startAddr, int count)
{
CORE_LOCK();
2018-03-21 20:32:32 +00:00
QJsonArray array = Core()->cmdj("pdj " + QString::number(count + 1) + "@" + QString::number(
startAddr)).array();
if (array.isEmpty()) {
return startAddr + 1;
}
QJsonValue instValue = array.last();
2018-03-21 20:32:32 +00:00
if (!instValue.isObject()) {
return startAddr + 1;
}
bool ok;
RVA offset = instValue.toObject()[RJsonKey::offset].toVariant().toULongLong(&ok);
2018-03-21 20:32:32 +00:00
if (!ok) {
return startAddr + 1;
}
return offset;
}
2017-10-16 19:00:47 +00:00
RVA CutterCore::getOffset()
{
return core_->offset;
}
2017-09-25 12:55:41 +00:00
ut64 CutterCore::math(const QString &expr)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
2017-04-09 19:55:06 +00:00
return r_num_math(this->core_ ? this->core_->num : NULL, expr.toUtf8().constData());
}
ut64 CutterCore::num(const QString &expr)
{
CORE_LOCK();
return r_num_get(this->core_ ? this->core_->num : NULL, expr.toUtf8().constData());
}
QString CutterCore::itoa(ut64 num, int rdx)
{
return QString::number(num, rdx);
}
void CutterCore::setConfig(const char *k, const QString &v)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
r_config_set(core_->config, k, v.toUtf8().constData());
}
void CutterCore::setConfig(const char *k, int v)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
r_config_set_i(core_->config, k, static_cast<ut64>(v));
}
void CutterCore::setConfig(const char *k, bool v)
{
CORE_LOCK();
r_config_set_i(core_->config, k, v ? 1 : 0);
}
int CutterCore::getConfigi(const char *k)
2017-08-31 17:43:46 +00:00
{
CORE_LOCK();
return static_cast<int>(r_config_get_i(core_->config, k));
}
ut64 CutterCore::getConfigut64(const char *k)
{
CORE_LOCK();
return r_config_get_i(core_->config, k);
}
bool CutterCore::getConfigb(const char *k)
{
CORE_LOCK();
return r_config_get_i(core_->config, k) != 0;
}
void CutterCore::triggerRefreshAll()
{
emit refreshAll();
}
void CutterCore::triggerAsmOptionsChanged()
{
emit asmOptionsChanged();
}
void CutterCore::triggerGraphOptionsChanged()
{
emit graphOptionsChanged();
}
void CutterCore::message(const QString &msg, bool debug)
{
if (msg.isEmpty())
return;
if (debug) {
qDebug() << msg;
emit newDebugMessage(msg);
return;
}
emit newMessage(msg);
}
QString CutterCore::getConfig(const char *k)
2017-09-02 08:17:48 +00:00
{
CORE_LOCK();
return QString(r_config_get(core_->config, k));
2017-09-02 08:17:48 +00:00
}
void CutterCore::setConfig(const char *k, const QVariant &v)
{
switch (v.type()) {
case QVariant::Type::Bool:
setConfig(k, v.toBool());
break;
case QVariant::Type::Int:
setConfig(k, v.toInt());
break;
default:
setConfig(k, v.toString());
break;
}
}
2018-05-05 13:20:14 +00:00
void CutterCore::setCPU(QString arch, QString cpu, int bits)
2017-04-09 19:55:06 +00:00
{
if (arch != nullptr) {
setConfig("asm.arch", arch);
}
if (cpu != nullptr) {
setConfig("asm.cpu", cpu);
}
setConfig("asm.bits", bits);
}
2018-01-20 10:35:31 +00:00
void CutterCore::setEndianness(bool big)
{
setConfig("cfg.bigendian", big);
}
QByteArray CutterCore::assemble(const QString &code)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
2017-04-09 19:55:06 +00:00
RAsmCode *ac = r_asm_massemble(core_->assembler, code.toUtf8().constData());
QByteArray res;
if (ac && ac->bytes) {
res = QByteArray(reinterpret_cast<const char *>(ac->bytes), ac->len);
}
2017-04-09 19:55:06 +00:00
r_asm_code_free(ac);
return res;
}
QString CutterCore::disassemble(const QByteArray &data)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
RAsmCode *ac = r_asm_mdisassemble(core_->assembler, reinterpret_cast<const ut8 *>(data.constData()), data.length());
QString code;
if (ac && ac->assembly) {
code = QString::fromUtf8(ac->assembly);
}
2017-04-09 19:55:06 +00:00
r_asm_code_free(ac);
return code;
}
2017-09-25 12:55:41 +00:00
QString CutterCore::disassembleSingleInstruction(RVA addr)
2017-06-07 15:48:36 +00:00
{
return cmd("pi 1@" + QString::number(addr)).simplified();
}
2017-09-25 12:55:41 +00:00
RAnalFunction *CutterCore::functionAt(ut64 addr)
2017-04-09 19:55:06 +00:00
{
CORE_LOCK();
//return r_anal_fcn_find (core_->anal, addr, addr);
2017-04-09 19:55:06 +00:00
return r_anal_get_fcn_in(core_->anal, addr, 0);
}
2017-09-25 12:55:41 +00:00
QString CutterCore::cmdFunctionAt(QString addr)
2017-04-09 19:55:06 +00:00
{
QString ret;
//afi~name:1[1] @ 0x08048e44
//ret = cmd("afi~name[1] @ " + addr);
ret = cmd(QString("fd @ ") + addr + "~[0]");
return ret.trimmed();
}
2017-09-25 12:55:41 +00:00
QString CutterCore::cmdFunctionAt(RVA addr)
{
return cmdFunctionAt(QString::number(addr));
}
void CutterCore::cmdEsil(const char *command)
{
QString res = cmd(command);
if (res.contains(QStringLiteral("[ESIL] Stopped execution in an invalid instruction"))) {
msgBox.showMessage("Stopped when attempted to run an invalid instruction. You can disable this in Preferences");
}
}
QString CutterCore::createFunctionAt(RVA addr, QString name)
{
static const QRegExp regExp("[^a-zA-Z0-9_]");
name.remove(regExp);
QString command = "af " + name + " " + RAddressString(addr);
QString ret = cmd(command);
emit functionsChanged();
return ret;
}
QJsonDocument CutterCore::getRegistersInfo()
{
return cmdj("aeafj");
}
2017-11-28 11:56:38 +00:00
RVA CutterCore::getOffsetJump(RVA addr)
2017-04-09 19:55:06 +00:00
{
2017-11-28 11:56:38 +00:00
bool ok;
2018-03-21 20:32:32 +00:00
RVA value = cmdj("aoj @" + QString::number(
addr)).array().first().toObject().value(RJsonKey::jump).toVariant().toULongLong(&ok);
2017-11-28 11:56:38 +00:00
2018-03-21 20:32:32 +00:00
if (!ok) {
2017-11-28 11:56:38 +00:00
return RVA_INVALID;
}
return value;
}
2018-09-08 07:12:08 +00:00
QString CutterCore::getDecompiledCodePDC(RVA addr)
{
return cmd("pdc @ " + QString::number(addr));
}
2018-09-08 07:12:08 +00:00
bool CutterCore::getR2DecAvailable()
2017-04-09 19:55:06 +00:00
{
2019-03-23 10:54:34 +00:00
return cmdList("e cmd.pdc=?").contains(QStringLiteral("r2dec"));
}
2018-09-08 07:12:08 +00:00
QString CutterCore::getDecompiledCodeR2Dec(RVA addr)
{
return cmd("pdd @ " + QString::number(addr));
}
QJsonDocument CutterCore::getFileInfo()
2017-04-09 19:55:06 +00:00
{
return cmdj("ij");
}
QJsonDocument CutterCore::getFileVersionInfo()
{
return cmdj("iVj");
}
QJsonDocument CutterCore::getSignatureInfo()
{
return cmdj("iCj");
}
QJsonDocument CutterCore::getStack(int size)
{
return cmdj("pxrj " + QString::number(size) + " @ r:SP");
}
QJsonDocument CutterCore::getRegisterValues()
{
return cmdj("drj");
}
2018-07-19 14:35:46 +00:00
QList<RegisterRefDescription> CutterCore::getRegisterRefs()
{
QList<RegisterRefDescription> ret;
QJsonArray registerRefArray = cmdj("drrj").array();
for (const QJsonValue &value : registerRefArray) {
2018-07-19 14:35:46 +00:00
QJsonObject regRefObject = value.toObject();
RegisterRefDescription regRef;
regRef.reg = regRefObject[RJsonKey::reg].toString();
regRef.value = regRefObject[RJsonKey::value].toString();
regRef.ref = regRefObject[RJsonKey::ref].toString();
2018-07-19 14:35:46 +00:00
ret << regRef;
}
return ret;
}
QList<VariableDescription> CutterCore::getVariables(RVA at)
{
QList<VariableDescription> ret;
QJsonObject varsObject = cmdj(QString("afvj @ %1").arg(at)).object();
auto addVars = [&](VariableDescription::RefType refType, const QJsonArray &array) {
for (const QJsonValue &varValue : array) {
QJsonObject varObject = varValue.toObject();
VariableDescription desc;
desc.refType = refType;
desc.name = varObject["name"].toString();
desc.type = varObject["type"].toString();
ret << desc;
}
};
addVars(VariableDescription::RefType::SP, varsObject["sp"].toArray());
addVars(VariableDescription::RefType::BP, varsObject["bp"].toArray());
addVars(VariableDescription::RefType::Reg, varsObject["reg"].toArray());
return ret;
}
QJsonObject CutterCore::getRegisterJson()
{
QJsonArray registerRefArray = cmdj("drrj").array();
QJsonObject registerJson;
for (const QJsonValue &value : registerRefArray) {
QJsonObject regRefObject = value.toObject();
QJsonObject registers;
registers.insert(RJsonKey::value, regRefObject[RJsonKey::value]);
registers.insert(RJsonKey::ref, regRefObject[RJsonKey::ref]);
registerJson.insert(regRefObject[RJsonKey::reg].toString(), registers);
}
return registerJson;
}
QString CutterCore::getRegisterName(QString registerRole)
{
return cmd("drn " + registerRole).trimmed();
}
RVA CutterCore::getProgramCounterValue()
{
bool ok;
if (currentlyDebugging) {
RVA addr = cmd("dr?`drn PC`").toULongLong(&ok, 16);
if (ok) {
return addr;
}
}
return RVA_INVALID;
}
void CutterCore::setRegister(QString regName, QString regValue)
{
cmd("dr " + regName + "=" + regValue);
2018-06-12 08:43:14 +00:00
emit registersChanged();
emit refreshCodeViews();
2018-06-12 08:43:14 +00:00
}
void CutterCore::startDebug()
{
2018-06-15 17:33:17 +00:00
if (!currentlyDebugging) {
offsetPriorDebugging = getOffset();
}
2018-08-06 14:48:28 +00:00
cmd("ood");
2018-06-12 08:43:14 +00:00
emit registersChanged();
2018-06-15 17:33:17 +00:00
if (!currentlyDebugging) {
setConfig("asm.flags", false);
currentlyDebugging = true;
2018-06-15 17:33:17 +00:00
emit changeDebugView();
emit flagsChanged();
emit refreshCodeViews();
}
emit stackChanged();
}
void CutterCore::startEmulation()
{
if (!currentlyDebugging) {
offsetPriorDebugging = getOffset();
}
// clear registers, init esil state, stack, progcounter at current seek
cmd("ar0; aei; aeim; aeip");
emit registersChanged();
if (!currentlyDebugging || !currentlyEmulating) {
// prevent register flags from appearing during debug/emul
2018-10-03 11:30:12 +00:00
setConfig("asm.flags", false);
// allows to view self-modifying code changes or other binary changes
setConfig("io.cache", true);
currentlyDebugging = true;
currentlyEmulating = true;
emit changeDebugView();
emit flagsChanged();
}
emit stackChanged();
emit refreshCodeViews();
}
void CutterCore::attachDebug(int pid)
{
if (!currentlyDebugging) {
offsetPriorDebugging = getOffset();
}
// attach to process with dbg plugin
cmd("o-*; e cfg.debug = true; o+ dbg://" + QString::number(pid));
QString programCounterValue = cmd("dr?`drn PC`").trimmed();
seek(programCounterValue);
emit registersChanged();
if (!currentlyDebugging || !currentlyEmulating) {
// prevent register flags from appearing during debug/emul
setConfig("asm.flags", false);
currentlyDebugging = true;
currentlyOpenFile = getConfig("file.path");
currentlyAttachedToPID = pid;
emit flagsChanged();
emit changeDebugView();
2018-06-15 17:33:17 +00:00
}
}
void CutterCore::stopDebug()
{
if (currentlyDebugging) {
if (currentlyEmulating) {
cmd("aeim-; aei-; wcr; .ar-");
currentlyEmulating = false;
} else if (currentlyAttachedToPID != -1) {
cmd(QString("dp- %1; o %2; .ar-").arg(QString::number(currentlyAttachedToPID), currentlyOpenFile));
currentlyAttachedToPID = -1;
} else {
cmd("dk 9; oo; .ar-");
}
seek(offsetPriorDebugging);
setConfig("asm.flags", true);
setConfig("io.cache", false);
currentlyDebugging = false;
emit flagsChanged();
emit changeDefinedView();
}
2018-06-12 08:43:14 +00:00
}
void CutterCore::continueDebug()
{
if (currentlyDebugging) {
2018-08-06 11:48:26 +00:00
if (currentlyEmulating) {
cmdEsil("aec");
} else {
cmd("dc");
}
emit registersChanged();
emit refreshCodeViews();
}
2018-06-12 08:43:14 +00:00
}
void CutterCore::continueUntilDebug(QString offset)
{
if (currentlyDebugging) {
2018-08-06 12:19:52 +00:00
if (currentlyEmulating) {
cmdEsil("aecu " + offset);
2018-08-06 12:19:52 +00:00
} else {
cmd("dcu " + offset);
}
emit registersChanged();
emit refreshCodeViews();
}
2018-06-12 08:43:14 +00:00
}
void CutterCore::continueUntilCall()
{
if (currentlyDebugging) {
if (currentlyEmulating) {
cmdEsil("aecc");
} else {
cmd("dcc");
}
QString programCounterValue = cmd("dr?`drn PC`").trimmed();
seek(programCounterValue);
emit registersChanged();
}
}
void CutterCore::continueUntilSyscall()
{
if (currentlyDebugging) {
if (currentlyEmulating) {
cmdEsil("aecs");
} else {
cmd("dcs");
}
QString programCounterValue = cmd("dr?`drn PC`").trimmed();
seek(programCounterValue);
emit registersChanged();
}
}
2018-06-12 08:43:14 +00:00
void CutterCore::stepDebug()
{
if (currentlyDebugging) {
cmdEsil("ds");
QString programCounterValue = cmd("dr?`drn PC`").trimmed();
seek(programCounterValue);
emit registersChanged();
}
2018-06-12 08:43:14 +00:00
}
void CutterCore::stepOverDebug()
{
if (currentlyDebugging) {
cmdEsil("dso");
QString programCounterValue = cmd("dr?`drn PC`").trimmed();
seek(programCounterValue);
emit registersChanged();
}
2018-06-12 08:43:14 +00:00
}
void CutterCore::stepOutDebug()
{
if (currentlyDebugging) {
cmd("dsf");
QString programCounterValue = cmd("dr?`drn PC`").trimmed();
seek(programCounterValue);
emit registersChanged();
}
}
QStringList CutterCore::getDebugPlugins()
{
QStringList plugins;
QJsonArray pluginArray = cmdj("dLj").array();
for (const QJsonValue &value : pluginArray) {
QJsonObject pluginObject = value.toObject();
QString plugin = pluginObject[RJsonKey::name].toString();
plugins << plugin;
}
return plugins;
}
QString CutterCore::getActiveDebugPlugin()
{
return getConfig("dbg.backend");
}
void CutterCore::setDebugPlugin(QString plugin)
{
setConfig("dbg.backend", plugin);
}
void CutterCore::toggleBreakpoint(RVA addr)
2018-06-12 08:43:14 +00:00
{
cmd("dbs " + RAddressString(addr));
2018-06-12 08:43:14 +00:00
emit instructionChanged(addr);
emit breakpointsChanged();
}
void CutterCore::toggleBreakpoint(QString addr)
{
cmd("dbs " + addr);
emit instructionChanged(addr.toULongLong());
emit breakpointsChanged();
}
void CutterCore::delBreakpoint(RVA addr)
{
cmd("db- " + RAddressString(addr));
emit instructionChanged(addr);
emit breakpointsChanged();
}
void CutterCore::delAllBreakpoints()
{
cmd("db-*");
emit refreshCodeViews();
}
void CutterCore::enableBreakpoint(RVA addr)
{
cmd("dbe " + RAddressString(addr));
emit instructionChanged(addr);
emit breakpointsChanged();
}
void CutterCore::disableBreakpoint(RVA addr)
{
cmd("dbd " + RAddressString(addr));
emit instructionChanged(addr);
emit breakpointsChanged();
}
QList<BreakpointDescription> CutterCore::getBreakpoints()
{
QList<BreakpointDescription> ret;
QJsonArray breakpointArray = cmdj("dbj").array();
for (const QJsonValue &value : breakpointArray) {
QJsonObject bpObject = value.toObject();
BreakpointDescription bp;
bp.addr = bpObject[RJsonKey::addr].toVariant().toULongLong();
bp.size = bpObject[RJsonKey::size].toInt();
bp.permission = bpObject[RJsonKey::prot].toString();
bp.hw = bpObject[RJsonKey::hw].toBool();
bp.trace = bpObject[RJsonKey::trace].toBool();
bp.enabled = bpObject[RJsonKey::enabled].toBool();
ret << bp;
}
return ret;
}
QList<RVA> CutterCore::getBreakpointsAddresses()
{
QList<RVA> bpAddresses;
for (const BreakpointDescription &bp : getBreakpoints()) {
bpAddresses << bp.addr;
}
return bpAddresses;
}
bool CutterCore::isBreakpoint(const QList<RVA> &breakpoints, RVA addr)
{
return breakpoints.contains(addr);
}
QJsonDocument CutterCore::getBacktrace()
{
return cmdj("dbtj");
}
QList<ProcessDescription> CutterCore::getAllProcesses()
{
QList<ProcessDescription> ret;
QJsonArray processArray = cmdj("dplj").array();
for (const QJsonValue &value : processArray) {
QJsonObject procObject = value.toObject();
ProcessDescription proc;
proc.pid = procObject[RJsonKey::pid].toInt();
proc.uid = procObject[RJsonKey::uid].toInt();
proc.status = procObject[RJsonKey::status].toString();
proc.path = procObject[RJsonKey::path].toString();
ret << proc;
}
return ret;
}
2018-06-12 08:43:14 +00:00
QList<MemoryMapDescription> CutterCore::getMemoryMap()
{
QList<MemoryMapDescription> ret;
QJsonArray memoryMapArray = cmdj("dmj").array();
for (const QJsonValue &value : memoryMapArray) {
2018-06-12 08:43:14 +00:00
QJsonObject memMapObject = value.toObject();
MemoryMapDescription memMap;
memMap.name = memMapObject[RJsonKey::name].toString();
memMap.fileName = memMapObject[RJsonKey::file].toString();
memMap.addrStart = memMapObject[RJsonKey::addr].toVariant().toULongLong();
memMap.addrEnd = memMapObject[RJsonKey::addr_end].toVariant().toULongLong();
memMap.type = memMapObject[RJsonKey::type].toString();
memMap.permission = memMapObject[RJsonKey::perm].toString();
2018-06-12 08:43:14 +00:00
ret << memMap;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QStringList CutterCore::getStats()
2017-04-09 19:55:06 +00:00
{
QStringList stats;
cmd("fs functions");
stats << cmd("f~?").trimmed();
QString imps = cmd("ii~?").trimmed();
stats << imps;
cmd("fs symbols");
stats << cmd("f~?").trimmed();
cmd("fs strings");
stats << cmd("f~?").trimmed();
cmd("fs relocs");
stats << cmd("f~?").trimmed();
cmd("fs sections");
stats << cmd("f~?").trimmed();
cmd("fs *");
stats << cmd("f~?").trimmed();
return stats;
}
void CutterCore::setGraphEmpty(bool empty)
{
emptyGraph = empty;
}
bool CutterCore::isGraphEmpty()
{
return emptyGraph;
}
2017-09-25 12:55:41 +00:00
void CutterCore::getOpcodes()
2017-04-09 19:55:06 +00:00
{
2019-03-23 10:54:34 +00:00
this->opcodes = cmdList("?O");
this->regs = cmdList("drp~[1]");
}
2017-09-25 12:55:41 +00:00
void CutterCore::setSettings()
2017-04-09 19:55:06 +00:00
{
setConfig("scr.interactive", false);
setConfig("hex.pairs", false);
setConfig("asm.xrefs", false);
2018-05-24 06:21:12 +00:00
setConfig("asm.tabs.once", true);
setConfig("asm.flags.middle", 2);
2017-11-28 13:24:35 +00:00
setConfig("anal.hasnext", false);
setConfig("asm.lines.call", false);
2017-08-31 17:43:46 +00:00
setConfig("cfg.fortunes.tts", false);
2017-10-22 10:21:44 +00:00
// Colors
setConfig("scr.color", COLOR_MODE_DISABLED);
2018-03-08 13:02:34 +00:00
// Don't show hits
setConfig("search.flags", false);
}
2017-09-25 12:55:41 +00:00
QList<RVA> CutterCore::getSeekHistory()
{
CORE_LOCK();
QList<RVA> ret;
QJsonArray jsonArray = cmdj("sj").array();
for (const QJsonValue &value : jsonArray)
ret << value.toVariant().toULongLong();
return ret;
}
2017-09-25 12:55:41 +00:00
QStringList CutterCore::getAsmPluginNames()
{
CORE_LOCK();
RListIter *it;
QStringList ret;
RAsmPlugin *ap;
2018-03-21 20:32:32 +00:00
CutterRListForeach(core_->assembler->plugins, it, RAsmPlugin, ap) {
ret << ap->name;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QStringList CutterCore::getAnalPluginNames()
{
CORE_LOCK();
RListIter *it;
QStringList ret;
RAnalPlugin *ap;
2018-03-21 20:32:32 +00:00
CutterRListForeach(core_->anal->plugins, it, RAnalPlugin, ap) {
ret << ap->name;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QStringList CutterCore::getProjectNames()
{
CORE_LOCK();
QStringList ret;
2018-04-11 09:44:01 +00:00
QJsonArray jsonArray = cmdj("Pj").array();
for (const QJsonValue &value : jsonArray)
ret.append(value.toString());
return ret;
}
QList<RBinPluginDescription> CutterCore::getRBinPluginDescriptions(const QString &type)
{
QList<RBinPluginDescription> ret;
QJsonObject jsonRoot = cmdj("iLj").object();
2018-03-21 20:32:32 +00:00
for (const QString &key : jsonRoot.keys()) {
if (!type.isNull() && key != type)
continue;
QJsonArray pluginArray = jsonRoot[key].toArray();
for (const QJsonValue &pluginValue : pluginArray) {
QJsonObject pluginObject = pluginValue.toObject();
RBinPluginDescription desc;
desc.name = pluginObject[RJsonKey::name].toString();
desc.description = pluginObject[RJsonKey::description].toString();
desc.license = pluginObject[RJsonKey::license].toString();
desc.type = key;
ret.append(desc);
}
}
return ret;
}
QList<RIOPluginDescription> CutterCore::getRIOPluginDescriptions()
{
QList<RIOPluginDescription> ret;
QJsonArray plugins = cmdj("oLj").object()["IO_Plugins"].toArray();
for (const QJsonValue &pluginValue : plugins) {
QJsonObject pluginObject = pluginValue.toObject();
RIOPluginDescription plugin;
plugin.name = pluginObject["Name"].toString();
plugin.description = pluginObject["Description"].toString();
plugin.license = pluginObject["License"].toString();
plugin.permissions = pluginObject["Permissions"].toString();
ret << plugin;
}
return ret;
}
QList<RCorePluginDescription> CutterCore::getRCorePluginDescriptions()
{
QList<RCorePluginDescription> ret;
QJsonArray plugins = cmdj("Lsj").array();
for (const QJsonValue &pluginValue : plugins) {
QJsonObject pluginObject = pluginValue.toObject();
RCorePluginDescription plugin;
plugin.name = pluginObject["Name"].toString();
plugin.description = pluginObject["Description"].toString();
ret << plugin;
}
return ret;
}
QList<RAsmPluginDescription> CutterCore::getRAsmPluginDescriptions()
{
CORE_LOCK();
RListIter *it;
QList<RAsmPluginDescription> ret;
RAsmPlugin *ap;
2018-03-21 20:32:32 +00:00
CutterRListForeach(core_->assembler->plugins, it, RAsmPlugin, ap) {
RAsmPluginDescription plugin;
plugin.name = ap->name;
plugin.architecture = ap->arch;
plugin.author = ap->author;
plugin.version = ap->version;
plugin.cpus = ap->cpus;
plugin.description = ap->desc;
plugin.license = ap->license;
ret << plugin;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<FunctionDescription> CutterCore::getAllFunctions()
{
2018-06-25 19:28:34 +00:00
return parseFunctionsJson(cmdjTask("aflj"));
}
2017-09-25 12:55:41 +00:00
QList<ImportDescription> CutterCore::getAllImports()
{
CORE_LOCK();
QList<ImportDescription> ret;
QJsonArray importsArray = cmdj("iij").array();
for (const QJsonValue &value : importsArray) {
QJsonObject importObject = value.toObject();
ImportDescription import;
import.plt = importObject[RJsonKey::plt].toVariant().toULongLong();
import.ordinal = importObject[RJsonKey::ordinal].toInt();
import.bind = importObject[RJsonKey::bind].toString();
import.type = importObject[RJsonKey::type].toString();
import.name = importObject[RJsonKey::name].toString();
ret << import;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<ExportDescription> CutterCore::getAllExports()
{
CORE_LOCK();
QList<ExportDescription> ret;
QJsonArray exportsArray = cmdj("iEj").array();
for (const QJsonValue &value : exportsArray) {
QJsonObject exportObject = value.toObject();
ExportDescription exp;
exp.vaddr = exportObject[RJsonKey::vaddr].toVariant().toULongLong();
exp.paddr = exportObject[RJsonKey::paddr].toVariant().toULongLong();
exp.size = exportObject[RJsonKey::size].toVariant().toULongLong();
exp.type = exportObject[RJsonKey::type].toString();
exp.name = exportObject[RJsonKey::name].toString();
exp.flag_name = exportObject[RJsonKey::flagname].toString();
ret << exp;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<SymbolDescription> CutterCore::getAllSymbols()
{
CORE_LOCK();
RListIter *it;
QList<SymbolDescription> ret;
RBinSymbol *bs;
2018-03-21 20:32:32 +00:00
if (core_ && core_->bin && core_->bin->cur && core_->bin->cur->o) {
CutterRListForeach(core_->bin->cur->o->symbols, it, RBinSymbol, bs) {
QString type = QString(bs->bind) + " " + QString(bs->type);
SymbolDescription symbol;
symbol.vaddr = bs->vaddr;
symbol.name = QString(bs->name);
symbol.bind = QString(bs->bind);
symbol.type = QString(bs->type);
ret << symbol;
}
/* list entrypoints as symbols too */
int n = 0;
RBinAddr *entry;
2018-03-21 20:32:32 +00:00
CutterRListForeach(core_->bin->cur->o->entries, it, RBinAddr, entry) {
SymbolDescription symbol;
symbol.vaddr = entry->vaddr;
symbol.name = QString("entry") + QString::number(n++);
2019-03-23 10:54:34 +00:00
symbol.bind.clear();
symbol.type = "entry";
ret << symbol;
}
}
return ret;
}
2018-05-21 17:34:41 +00:00
QList<HeaderDescription> CutterCore::getAllHeaders()
{
CORE_LOCK();
QList<HeaderDescription> ret;
QJsonArray headersArray = cmdj("ihj").array();
for (const QJsonValue &value : headersArray) {
2018-05-21 17:34:41 +00:00
QJsonObject headerObject = value.toObject();
HeaderDescription header;
header.vaddr = headerObject[RJsonKey::vaddr].toVariant().toULongLong();
header.paddr = headerObject[RJsonKey::paddr].toVariant().toULongLong();
header.value = headerObject[RJsonKey::comment].toString();
header.name = headerObject[RJsonKey::name].toString();
2018-05-21 17:34:41 +00:00
ret << header;
}
return ret;
}
2018-05-24 15:37:37 +00:00
QList<ZignatureDescription> CutterCore::getAllZignatures()
{
CORE_LOCK();
QList<ZignatureDescription> ret;
QJsonArray zignaturesArray = cmdj("zj").array();
for (const QJsonValue &value : zignaturesArray) {
2018-05-24 15:37:37 +00:00
QJsonObject zignatureObject = value.toObject();
ZignatureDescription zignature;
zignature.name = zignatureObject[RJsonKey::name].toString();
zignature.bytes = zignatureObject[RJsonKey::bytes].toString();
zignature.offset = zignatureObject[RJsonKey::offset].toVariant().toULongLong();
for (const QJsonValue &ref : zignatureObject[RJsonKey::refs].toArray()) {
2018-05-24 15:37:37 +00:00
zignature.refs << ref.toString();
}
QJsonObject graphObject = zignatureObject[RJsonKey::graph].toObject();
zignature.cc = graphObject[RJsonKey::cc].toVariant().toULongLong();
zignature.nbbs = graphObject[RJsonKey::nbbs].toVariant().toULongLong();
zignature.edges = graphObject[RJsonKey::edges].toVariant().toULongLong();
zignature.ebbs = graphObject[RJsonKey::ebbs].toVariant().toULongLong();
2018-05-24 15:37:37 +00:00
ret << zignature;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<CommentDescription> CutterCore::getAllComments(const QString &filterType)
{
CORE_LOCK();
QList<CommentDescription> ret;
QJsonArray commentsArray = cmdj("CCj").array();
for (const QJsonValue &value : commentsArray) {
QJsonObject commentObject = value.toObject();
QString type = commentObject[RJsonKey::type].toString();
if (type != filterType)
continue;
CommentDescription comment;
comment.offset = commentObject[RJsonKey::offset].toVariant().toULongLong();
comment.name = commentObject[RJsonKey::name].toString();
ret << comment;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<RelocDescription> CutterCore::getAllRelocs()
{
CORE_LOCK();
QList<RelocDescription> ret;
2018-03-21 20:32:32 +00:00
if (core_ && core_->bin && core_->bin->cur && core_->bin->cur->o) {
auto relocs = core_->bin->cur->o->relocs;
RBIter iter;
RBinReloc *br;
r_rbtree_foreach (relocs, iter, br, RBinReloc, vrb) {
RelocDescription reloc;
reloc.vaddr = br->vaddr;
reloc.paddr = br->paddr;
reloc.type = (br->additive ? "ADD_" : "SET_") + QString::number(br->type);
if (br->import)
reloc.name = br->import->name;
else
reloc.name = QString("reloc_%1").arg(QString::number(br->vaddr, 16));
ret << reloc;
}
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<StringDescription> CutterCore::getAllStrings()
{
2018-05-29 16:19:59 +00:00
return parseStringsJson(cmdjTask("izzj"));
}
QList<StringDescription> CutterCore::parseStringsJson(const QJsonDocument &doc)
{
QList<StringDescription> ret;
2018-05-28 15:19:58 +00:00
2018-08-24 16:06:07 +00:00
QJsonArray stringsArray = doc.array();
for (const QJsonValue &value : stringsArray) {
2018-02-10 17:50:00 +00:00
QJsonObject stringObject = value.toObject();
StringDescription string;
string.string = QString(QByteArray::fromBase64(stringObject[RJsonKey::string].toVariant().toByteArray()));
string.vaddr = stringObject[RJsonKey::vaddr].toVariant().toULongLong();
string.type = stringObject[RJsonKey::type].toString();
string.size = stringObject[RJsonKey::size].toVariant().toUInt();
string.length = stringObject[RJsonKey::length].toVariant().toUInt();
string.section = stringObject[RJsonKey::section].toString();
2018-02-10 17:50:00 +00:00
ret << string;
}
return ret;
}
2018-06-25 19:28:34 +00:00
QList<FunctionDescription> CutterCore::parseFunctionsJson(const QJsonDocument &doc)
{
QList<FunctionDescription> ret;
QJsonArray jsonArray = doc.array();
for (const QJsonValue &value : jsonArray) {
2018-06-25 19:28:34 +00:00
QJsonObject jsonObject = value.toObject();
FunctionDescription function;
function.offset = jsonObject[RJsonKey::offset].toVariant().toULongLong();
function.size = jsonObject[RJsonKey::size].toVariant().toULongLong();
function.nargs = jsonObject[RJsonKey::nargs].toVariant().toULongLong();
function.nbbs = jsonObject[RJsonKey::nbbs].toVariant().toULongLong();
function.nlocals = jsonObject[RJsonKey::nlocals].toVariant().toULongLong();
function.cc = jsonObject[RJsonKey::cc].toVariant().toULongLong();
function.calltype = jsonObject[RJsonKey::calltype].toString();
function.name = jsonObject[RJsonKey::name].toString();
function.edges = jsonObject[RJsonKey::edges].toVariant().toULongLong();
function.cost = jsonObject[RJsonKey::cost].toVariant().toULongLong();
function.calls = jsonObject[RJsonKey::outdegree].toVariant().toULongLong();
function.stackframe = jsonObject[RJsonKey::stackframe].toVariant().toULongLong();
2018-06-25 19:28:34 +00:00
ret << function;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<FlagspaceDescription> CutterCore::getAllFlagspaces()
{
CORE_LOCK();
QList<FlagspaceDescription> ret;
QJsonArray flagspacesArray = cmdj("fsj").array();
for (const QJsonValue &value : flagspacesArray) {
QJsonObject flagspaceObject = value.toObject();
FlagspaceDescription flagspace;
flagspace.name = flagspaceObject[RJsonKey::name].toString();
ret << flagspace;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<FlagDescription> CutterCore::getAllFlags(QString flagspace)
{
CORE_LOCK();
QList<FlagDescription> ret;
if (!flagspace.isEmpty())
cmd("fs " + flagspace);
else
cmd("fs *");
QJsonArray flagsArray = cmdj("fj").array();
for (const QJsonValue &value : flagsArray) {
QJsonObject flagObject = value.toObject();
FlagDescription flag;
flag.offset = flagObject[RJsonKey::offset].toVariant().toULongLong();
flag.size = flagObject[RJsonKey::size].toVariant().toULongLong();
flag.name = flagObject[RJsonKey::name].toString();
ret << flag;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<SectionDescription> CutterCore::getAllSections()
{
CORE_LOCK();
QList<SectionDescription> ret;
2018-05-05 10:11:44 +00:00
QJsonDocument sectionsDoc = cmdj("iSj entropy");
QJsonObject sectionsObj = sectionsDoc.object();
QJsonArray sectionsArray = sectionsObj[RJsonKey::sections].toArray();
2018-05-05 10:11:44 +00:00
for (const QJsonValue &value : sectionsArray) {
QJsonObject sectionObject = value.toObject();
QString name = sectionObject[RJsonKey::name].toString();
if (name.isEmpty())
continue;
SectionDescription section;
section.name = name;
section.vaddr = sectionObject[RJsonKey::vaddr].toVariant().toULongLong();
section.vsize = sectionObject[RJsonKey::vsize].toVariant().toULongLong();
section.paddr = sectionObject[RJsonKey::paddr].toVariant().toULongLong();
section.size = sectionObject[RJsonKey::size].toVariant().toULongLong();
section.flags = sectionObject[RJsonKey::flags].toString();
section.entropy = sectionObject[RJsonKey::entropy].toString();
ret << section;
}
return ret;
}
2017-06-07 10:56:55 +00:00
QStringList CutterCore::getSectionList()
{
CORE_LOCK();
QStringList ret;
QJsonArray sectionsArray = cmdj("iSj").array();
for (const QJsonValue &value : sectionsArray) {
ret << value.toObject()[RJsonKey::name].toString();
}
return ret;
}
QList<SegmentDescription> CutterCore::getAllSegments()
{
CORE_LOCK();
QList<SegmentDescription> ret;
QJsonArray segments = cmdj("iSSj").array();
for (const QJsonValue &value : segments) {
QJsonObject segmentObject = value.toObject();
QString name = segmentObject[RJsonKey::name].toString();
if (name.isEmpty())
continue;
SegmentDescription segment;
segment.name = name;
segment.vaddr = segmentObject[RJsonKey::vaddr].toVariant().toULongLong();
segment.paddr = segmentObject[RJsonKey::paddr].toVariant().toULongLong();
segment.size = segmentObject[RJsonKey::size].toVariant().toULongLong();
segment.vsize = segmentObject[RJsonKey::vsize].toVariant().toULongLong();
segment.perm = segmentObject[RJsonKey::perm].toString();
ret << segment;
}
return ret;
}
2017-09-25 12:55:41 +00:00
QList<EntrypointDescription> CutterCore::getAllEntrypoint()
{
CORE_LOCK();
QList<EntrypointDescription> ret;
QJsonArray entrypointsArray = cmdj("iej").array();
for (const QJsonValue &value : entrypointsArray) {
QJsonObject entrypointObject = value.toObject();
EntrypointDescription entrypoint;
entrypoint.vaddr = entrypointObject[RJsonKey::vaddr].toVariant().toULongLong();
entrypoint.paddr = entrypointObject[RJsonKey::paddr].toVariant().toULongLong();
entrypoint.baddr = entrypointObject[RJsonKey::baddr].toVariant().toULongLong();
entrypoint.laddr = entrypointObject[RJsonKey::laddr].toVariant().toULongLong();
entrypoint.haddr = entrypointObject[RJsonKey::haddr].toVariant().toULongLong();
entrypoint.type = entrypointObject[RJsonKey::type].toString();
ret << entrypoint;
}
return ret;
}
QList<BinClassDescription> CutterCore::getAllClassesFromBin()
2017-12-23 16:42:42 +00:00
{
CORE_LOCK();
QList<BinClassDescription> ret;
2017-12-23 16:42:42 +00:00
QJsonArray classesArray = cmdj("icj").array();
for (const QJsonValue &value : classesArray) {
2017-12-23 16:42:42 +00:00
QJsonObject classObject = value.toObject();
BinClassDescription cls;
cls.name = classObject[RJsonKey::classname].toString();
cls.addr = classObject[RJsonKey::addr].toVariant().toULongLong();
cls.index = classObject[RJsonKey::index].toVariant().toULongLong();
2017-12-23 16:42:42 +00:00
for (const QJsonValue &value2 : classObject[RJsonKey::methods].toArray()) {
2017-12-23 16:42:42 +00:00
QJsonObject methObject = value2.toObject();
2019-01-31 16:45:58 +00:00
BinClassMethodDescription meth;
meth.name = methObject[RJsonKey::name].toString();
meth.addr = methObject[RJsonKey::addr].toVariant().toULongLong();
2017-12-23 16:42:42 +00:00
cls.methods << meth;
}
for (const QJsonValue &value2 : classObject[RJsonKey::fields].toArray()) {
2017-12-23 16:42:42 +00:00
QJsonObject fieldObject = value2.toObject();
2019-01-31 16:45:58 +00:00
BinClassFieldDescription field;
field.name = fieldObject[RJsonKey::name].toString();
field.addr = fieldObject[RJsonKey::addr].toVariant().toULongLong();
2017-12-23 16:42:42 +00:00
cls.fields << field;
}
ret << cls;
}
return ret;
}
QList<BinClassDescription> CutterCore::getAllClassesFromFlags()
{
static const QRegularExpression classFlagRegExp("^class\\.(.*)$");
static const QRegularExpression methodFlagRegExp("^method\\.([^\\.]*)\\.(.*)$");
CORE_LOCK();
QList<BinClassDescription> ret;
QMap<QString, BinClassDescription *> classesCache;
QJsonArray flagsArray = cmdj("fj@F:classes").array();
for (const QJsonValue &value : flagsArray) {
QJsonObject flagObject = value.toObject();
QString flagName = flagObject[RJsonKey::name].toString();
QRegularExpressionMatch match = classFlagRegExp.match(flagName);
2018-03-21 20:32:32 +00:00
if (match.hasMatch()) {
QString className = match.captured(1);
BinClassDescription *desc = nullptr;
auto it = classesCache.find(className);
2018-03-21 20:32:32 +00:00
if (it == classesCache.end()) {
BinClassDescription cls = {};
ret << cls;
desc = &ret.last();
classesCache[className] = desc;
2018-03-21 20:32:32 +00:00
} else {
desc = it.value();
}
desc->name = match.captured(1);
desc->addr = flagObject[RJsonKey::offset].toVariant().toULongLong();
desc->index = RVA_INVALID;
continue;
}
match = methodFlagRegExp.match(flagName);
2018-03-21 20:32:32 +00:00
if (match.hasMatch()) {
QString className = match.captured(1);
BinClassDescription *classDesc = nullptr;
auto it = classesCache.find(className);
2018-03-21 20:32:32 +00:00
if (it == classesCache.end()) {
// add a new stub class, will be replaced if class flag comes after it
BinClassDescription cls;
cls.name = tr("Unknown (%1)").arg(className);
cls.addr = RVA_INVALID;
cls.index = 0;
ret << cls;
classDesc = &ret.last();
classesCache[className] = classDesc;
2018-03-21 20:32:32 +00:00
} else {
classDesc = it.value();
}
2019-01-31 16:45:58 +00:00
BinClassMethodDescription meth;
meth.name = match.captured(2);
meth.addr = flagObject[RJsonKey::offset].toVariant().toULongLong();
classDesc->methods << meth;
continue;
}
}
return ret;
}
QList<QString> CutterCore::getAllAnalClasses(bool sorted)
{
QList<QString> ret;
SdbList *l = r_anal_class_get_all(core_->anal, sorted);
if (!l) {
return ret;
}
2019-01-31 16:45:58 +00:00
ret.reserve(static_cast<int>(l->length));
SdbListIter *it;
void *entry;
ls_foreach(l, it, entry) {
auto kv = reinterpret_cast<SdbKv *>(entry);
ret.append(QString::fromUtf8(reinterpret_cast<const char *>(kv->base.key)));
}
ls_free(l);
return ret;
}
2019-01-31 16:45:58 +00:00
QList<AnalMethodDescription> CutterCore::getAnalClassMethods(const QString &cls)
{
QList<AnalMethodDescription> ret;
RVector *meths = r_anal_class_method_get_all(core_->anal, cls.toUtf8().constData());
if (!meths) {
return ret;
}
ret.reserve(static_cast<int>(meths->len));
RAnalMethod *meth;
CutterRVectorForeach(meths, meth, RAnalMethod) {
AnalMethodDescription desc;
desc.name = QString::fromUtf8(meth->name);
desc.addr = meth->addr;
desc.vtableOffset = meth->vtable_offset;
2019-02-01 14:51:29 +00:00
ret.append(desc);
2019-01-31 16:45:58 +00:00
}
r_vector_free(meths);
return ret;
}
QList<AnalBaseClassDescription> CutterCore::getAnalClassBaseClasses(const QString &cls)
{
QList<AnalBaseClassDescription> ret;
RVector *bases = r_anal_class_base_get_all(core_->anal, cls.toUtf8().constData());
if (!bases) {
return ret;
}
ret.reserve(static_cast<int>(bases->len));
RAnalBaseClass *base;
CutterRVectorForeach(bases, base, RAnalBaseClass) {
AnalBaseClassDescription desc;
desc.id = QString::fromUtf8(base->id);
desc.offset = base->offset;
desc.className = QString::fromUtf8(base->class_name);
2019-02-01 14:51:29 +00:00
ret.append(desc);
2019-01-31 16:45:58 +00:00
}
r_vector_free(bases);
return ret;
}
QList<AnalVTableDescription> CutterCore::getAnalClassVTables(const QString &cls)
{
QList<AnalVTableDescription> ret;
2019-02-01 16:00:09 +00:00
RVector *vtables = r_anal_class_vtable_get_all(core_->anal, cls.toUtf8().constData());
2019-01-31 16:45:58 +00:00
if (!vtables) {
return ret;
}
ret.reserve(static_cast<int>(vtables->len));
RAnalVTable *vtable;
CutterRVectorForeach(vtables, vtable, RAnalVTable) {
AnalVTableDescription desc;
desc.id = QString::fromUtf8(vtable->id);
desc.offset = vtable->offset;
desc.addr = vtable->addr;
2019-02-01 14:51:29 +00:00
ret.append(desc);
2019-01-31 16:45:58 +00:00
}
r_vector_free(vtables);
return ret;
}
void CutterCore::createNewClass(const QString &cls)
{
r_anal_class_create(core_->anal, cls.toUtf8().constData());
}
void CutterCore::renameClass(const QString &oldName, const QString &newName)
{
r_anal_class_rename(core_->anal, oldName.toUtf8().constData(), newName.toUtf8().constData());
}
void CutterCore::deleteClass(const QString &cls)
{
r_anal_class_delete(core_->anal, cls.toUtf8().constData());
}
bool CutterCore::getAnalMethod(const QString &cls, const QString &meth, AnalMethodDescription *desc)
{
RAnalMethod analMeth;
if (r_anal_class_method_get(core_->anal, cls.toUtf8().constData(), meth.toUtf8().constData(), &analMeth) != R_ANAL_CLASS_ERR_SUCCESS) {
return false;
}
desc->name = QString::fromUtf8(analMeth.name);
desc->addr = analMeth.addr;
desc->vtableOffset = analMeth.vtable_offset;
r_anal_class_method_fini(&analMeth);
return true;
}
void CutterCore::setAnalMethod(const QString &className, const AnalMethodDescription &meth)
{
RAnalMethod analMeth;
analMeth.name = strdup (meth.name.toUtf8().constData());
analMeth.addr = meth.addr;
analMeth.vtable_offset = meth.vtableOffset;
r_anal_class_method_set(core_->anal, className.toUtf8().constData(), &analMeth);
r_anal_class_method_fini(&analMeth);
}
void CutterCore::renameAnalMethod(const QString &className, const QString &oldMethodName, const QString &newMethodName)
{
r_anal_class_method_rename(core_->anal, className.toUtf8().constData(), oldMethodName.toUtf8().constData(), newMethodName.toUtf8().constData());
}
2018-02-04 14:32:18 +00:00
QList<ResourcesDescription> CutterCore::getAllResources()
{
CORE_LOCK();
QList<ResourcesDescription> ret;
QJsonArray resourcesArray = cmdj("iRj").array();
for (const QJsonValue &value : resourcesArray) {
2018-02-04 14:32:18 +00:00
QJsonObject resourceObject = value.toObject();
ResourcesDescription res;
res.name = resourceObject[RJsonKey::name].toInt();
res.vaddr = resourceObject[RJsonKey::vaddr].toVariant().toULongLong();
res.index = resourceObject[RJsonKey::index].toVariant().toULongLong();
res.type = resourceObject[RJsonKey::type].toString();
res.size = resourceObject[RJsonKey::size].toVariant().toULongLong();
res.lang = resourceObject[RJsonKey::lang].toString();
2018-02-04 14:32:18 +00:00
ret << res;
}
return ret;
}
2018-02-26 22:26:18 +00:00
QList<VTableDescription> CutterCore::getAllVTables()
{
CORE_LOCK();
QList<VTableDescription> ret;
QJsonArray vTablesArray = cmdj("avj").array();
for (const QJsonValue &vTableValue : vTablesArray) {
2018-02-26 22:26:18 +00:00
QJsonObject vTableObject = vTableValue.toObject();
VTableDescription res;
res.addr = vTableObject[RJsonKey::offset].toVariant().toULongLong();
QJsonArray methodArray = vTableObject[RJsonKey::methods].toArray();
2018-02-26 22:26:18 +00:00
for (const QJsonValue &methodValue : methodArray) {
2018-02-26 22:26:18 +00:00
QJsonObject methodObject = methodValue.toObject();
2019-01-31 16:45:58 +00:00
BinClassMethodDescription method;
method.addr = methodObject[RJsonKey::offset].toVariant().toULongLong();
method.name = methodObject[RJsonKey::name].toString();
2018-02-26 22:26:18 +00:00
res.methods << method;
}
ret << res;
}
return ret;
}
2018-03-06 17:21:48 +00:00
QList<TypeDescription> CutterCore::getAllTypes()
{
QList<TypeDescription> ret;
ret.append(getAllPrimitiveTypes());
ret.append(getAllUnions());
ret.append(getAllStructs());
ret.append(getAllEnums());
ret.append(getAllTypedefs());
return ret;
}
QList<TypeDescription> CutterCore::getAllPrimitiveTypes()
2018-03-06 17:21:48 +00:00
{
CORE_LOCK();
QList<TypeDescription> ret;
QJsonArray typesArray = cmdj("tj").array();
for (const QJsonValue &value : typesArray) {
2018-03-06 17:21:48 +00:00
QJsonObject typeObject = value.toObject();
TypeDescription exp;
exp.type = typeObject[RJsonKey::type].toString();
exp.size = typeObject[RJsonKey::size].toVariant().toULongLong();
exp.format = typeObject[RJsonKey::format].toString();
exp.category = tr("Primitive");
ret << exp;
}
return ret;
}
2018-03-06 17:21:48 +00:00
QList<TypeDescription> CutterCore::getAllUnions()
{
CORE_LOCK();
QList<TypeDescription> ret;
QJsonArray typesArray = cmdj("tuj").array();
for (const QJsonValue value: typesArray) {
QJsonObject typeObject = value.toObject();
TypeDescription exp;
exp.type = typeObject[RJsonKey::type].toString();
exp.size = typeObject[RJsonKey::size].toVariant().toULongLong();
exp.category = "Union";
ret << exp;
}
return ret;
}
QList<TypeDescription> CutterCore::getAllStructs()
{
CORE_LOCK();
QList<TypeDescription> ret;
QJsonArray typesArray = cmdj("tsj").array();
for (const QJsonValue value: typesArray) {
QJsonObject typeObject = value.toObject();
TypeDescription exp;
exp.type = typeObject[RJsonKey::type].toString();
exp.size = typeObject[RJsonKey::size].toVariant().toULongLong();
exp.category = "Struct";
ret << exp;
}
return ret;
}
QList<TypeDescription> CutterCore::getAllEnums()
{
CORE_LOCK();
QList<TypeDescription> ret;
QJsonObject typesObject = cmdj("tej").object();
for (QString key: typesObject.keys()) {
TypeDescription exp;
exp.type = key;
exp.size = 0;
exp.category = "Enum";
ret << exp;
}
return ret;
}
QList<TypeDescription> CutterCore::getAllTypedefs()
{
CORE_LOCK();
QList<TypeDescription> ret;
QJsonObject typesObject = cmdj("ttj").object();
for (QString key: typesObject.keys()) {
TypeDescription exp;
exp.type = key;
exp.size = 0;
exp.category = "Typedef";
2018-03-06 17:21:48 +00:00
ret << exp;
}
return ret;
}
QString CutterCore::addTypes(const char *str)
{
char *error_msg = nullptr;
char *parsed = r_parse_c_string(core_->anal, str, &error_msg);
if (!parsed) {
QString ret;
if (error_msg) {
ret = error_msg;
r_mem_free(error_msg);
}
return ret;
}
2019-03-31 08:50:53 +00:00
r_anal_save_parsed_type(core_->anal, parsed);
r_mem_free(parsed);
if (error_msg) {
r_mem_free(error_msg);
}
return QString();
}
bool CutterCore::isAddressMapped(RVA addr)
{
// If value returned by "om. @ addr" is empty means that address is not mapped
return !Core()->cmd(QString("om. @ %1").arg(addr)).isEmpty();
}
QList<SearchDescription> CutterCore::getAllSearch(QString search_for, QString space)
{
CORE_LOCK();
QList<SearchDescription> ret;
QJsonArray searchArray = cmdj(space + QString(" ") + search_for).array();
2018-03-21 20:32:32 +00:00
if (space == "/Rj") {
for (const QJsonValue &value : searchArray) {
2018-03-16 09:07:41 +00:00
QJsonObject searchObject = value.toObject();
2018-03-16 09:07:41 +00:00
SearchDescription exp;
2019-03-23 10:54:34 +00:00
exp.code.clear();
for (const QJsonValue &value2 : searchObject[RJsonKey::opcodes].toArray()) {
2018-03-16 09:07:41 +00:00
QJsonObject gadget = value2.toObject();
exp.code += gadget[RJsonKey::opcode].toString() + "; ";
2018-03-16 09:07:41 +00:00
}
2018-03-21 20:32:32 +00:00
exp.offset =
searchObject[RJsonKey::opcodes].toArray().first().toObject()[RJsonKey::offset].toVariant().toULongLong();
exp.size = searchObject[RJsonKey::size].toVariant().toULongLong();
2018-03-16 09:07:41 +00:00
ret << exp;
}
2018-03-21 20:32:32 +00:00
} else {
for (const QJsonValue &value : searchArray) {
2018-03-16 09:07:41 +00:00
QJsonObject searchObject = value.toObject();
2018-03-16 09:07:41 +00:00
SearchDescription exp;
exp.offset = searchObject[RJsonKey::offset].toVariant().toULongLong();
exp.size = searchObject[RJsonKey::len].toVariant().toULongLong();
exp.code = searchObject[RJsonKey::code].toString();
exp.data = searchObject[RJsonKey::data].toString();
2018-03-16 09:07:41 +00:00
ret << exp;
}
}
return ret;
}
2018-06-29 10:34:01 +00:00
BlockStatistics CutterCore::getBlockStatistics(unsigned int blocksCount)
{
2018-07-06 16:00:26 +00:00
if (blocksCount == 0) {
BlockStatistics ret;
ret.from = ret.to = ret.blocksize = 0;
return ret;
}
2018-06-29 12:35:02 +00:00
QJsonObject statsObj = cmdj("p-j " + QString::number(blocksCount)).object();
2018-06-29 10:34:01 +00:00
BlockStatistics ret;
ret.from = statsObj[RJsonKey::from].toVariant().toULongLong();
ret.to = statsObj[RJsonKey::to].toVariant().toULongLong();
ret.blocksize = statsObj[RJsonKey::blocksize].toVariant().toULongLong();
2018-06-29 10:34:01 +00:00
QJsonArray blocksArray = statsObj[RJsonKey::blocks].toArray();
2018-07-01 12:29:01 +00:00
2018-06-29 12:35:02 +00:00
for (const QJsonValue &value : blocksArray) {
2018-06-29 10:34:01 +00:00
QJsonObject blockObj = value.toObject();
2018-06-29 10:34:01 +00:00
BlockDescription block;
block.addr = blockObj[RJsonKey::offset].toVariant().toULongLong();
block.size = blockObj[RJsonKey::size].toVariant().toULongLong();
block.flags = blockObj[RJsonKey::flags].toInt(0);
block.functions = blockObj[RJsonKey::functions].toInt(0);
block.inFunctions = blockObj[RJsonKey::in_functions].toInt(0);
block.comments = blockObj[RJsonKey::comments].toInt(0);
block.symbols = blockObj[RJsonKey::symbols].toInt(0);
block.strings = blockObj[RJsonKey::strings].toInt(0);
2018-06-29 10:34:01 +00:00
block.rwx = 0;
QString rwxStr = blockObj[RJsonKey::rwx].toString();
2018-06-29 10:34:01 +00:00
if (rwxStr.length() == 3) {
if (rwxStr[0] == 'r') {
block.rwx |= (1 << 0);
}
if (rwxStr[1] == 'w') {
block.rwx |= (1 << 1);
}
if (rwxStr[2] == 'x') {
block.rwx |= (1 << 2);
}
}
ret.blocks << block;
}
2018-06-29 12:35:02 +00:00
2018-06-29 10:34:01 +00:00
return ret;
}
2018-03-21 20:32:32 +00:00
QList<XrefDescription> CutterCore::getXRefs(RVA addr, bool to, bool whole_function,
const QString &filterType)
2017-06-07 10:56:55 +00:00
{
QList<XrefDescription> ret = QList<XrefDescription>();
2017-06-07 10:56:55 +00:00
QJsonArray xrefsArray;
2018-11-05 21:51:27 +00:00
if (to) {
2017-06-07 10:56:55 +00:00
xrefsArray = cmdj("axtj@" + QString::number(addr)).array();
2018-11-05 21:51:27 +00:00
} else {
2017-06-07 10:56:55 +00:00
xrefsArray = cmdj("axfj@" + QString::number(addr)).array();
2018-11-05 21:51:27 +00:00
}
2017-06-07 10:56:55 +00:00
for (const QJsonValue &value : xrefsArray) {
2017-06-07 10:56:55 +00:00
QJsonObject xrefObject = value.toObject();
XrefDescription xref;
xref.type = xrefObject[RJsonKey::type].toString();
2017-06-07 10:56:55 +00:00
if (!filterType.isNull() && filterType != xref.type)
continue;
xref.from = xrefObject[RJsonKey::from].toVariant().toULongLong();
2018-09-11 09:30:28 +00:00
if (!to) {
xref.from_str = RAddressString(xref.from);
} else {
QString fcn = xrefObject[RJsonKey::fcn_name].toString();
2018-09-11 09:30:28 +00:00
if (!fcn.isEmpty()) {
RVA fcnAddr = xrefObject[RJsonKey::fcn_addr].toVariant().toULongLong();
2018-09-11 09:30:28 +00:00
xref.from_str = fcn + " + 0x" + QString::number(xref.from - fcnAddr, 16);
} else {
xref.from_str = RAddressString(xref.from);
}
}
2017-06-07 10:56:55 +00:00
2018-11-05 21:51:27 +00:00
if (!whole_function && !to && xref.from != addr) {
continue;
2018-11-05 21:51:27 +00:00
}
if (to && !xrefObject.contains(RJsonKey::to)) {
2017-06-07 10:56:55 +00:00
xref.to = addr;
2018-11-05 21:51:27 +00:00
} else {
xref.to = xrefObject[RJsonKey::to].toVariant().toULongLong();
2018-11-05 21:51:27 +00:00
}
2018-09-11 09:30:28 +00:00
xref.to_str = Core()->cmd("fd " + QString::number(xref.to)).trimmed();
2017-06-07 10:56:55 +00:00
ret << xref;
}
return ret;
}
2017-09-25 12:55:41 +00:00
void CutterCore::addFlag(RVA offset, QString name, RVA size)
{
name = sanitizeStringForCommand(name);
cmd(QString("f %1 %2 @ %3").arg(name).arg(size).arg(offset));
emit flagsChanged();
}
void CutterCore::handleREvent(int type, void *data)
{
switch (type) {
case R_EVENT_CLASS_NEW: {
auto ev = reinterpret_cast<REventClass *>(data);
emit classNew(QString::fromUtf8(ev->name));
break;
}
case R_EVENT_CLASS_DEL: {
auto ev = reinterpret_cast<REventClass *>(data);
emit classDeleted(QString::fromUtf8(ev->name));
break;
}
case R_EVENT_CLASS_RENAME: {
auto ev = reinterpret_cast<REventClassRename *>(data);
emit classRenamed(QString::fromUtf8(ev->name_old), QString::fromUtf8(ev->name_new));
break;
}
case R_EVENT_CLASS_ATTR_SET: {
auto ev = reinterpret_cast<REventClassAttrSet *>(data);
emit classAttrsChanged(QString::fromUtf8(ev->attr.class_name));
break;
}
case R_EVENT_CLASS_ATTR_DEL: {
auto ev = reinterpret_cast<REventClassAttr *>(data);
emit classAttrsChanged(QString::fromUtf8(ev->class_name));
break;
}
case R_EVENT_CLASS_ATTR_RENAME: {
auto ev = reinterpret_cast<REventClassAttrRename *>(data);
emit classAttrsChanged(QString::fromUtf8(ev->attr.class_name));
break;
}
default:
break;
}
}
void CutterCore::triggerFlagsChanged()
{
emit flagsChanged();
}
void CutterCore::triggerVarsChanged()
{
emit varsChanged();
}
void CutterCore::triggerFunctionRenamed(const QString &prevName, const QString &newName)
{
emit functionRenamed(prevName, newName);
}
void CutterCore::loadPDB(const QString &file)
{
cmd("idp " + sanitizeStringForCommand(file));
}
void CutterCore::openProject(const QString &name)
{
cmd("Po " + name);
QString notes = QString::fromUtf8(QByteArray::fromBase64(cmd("Pnj").toUtf8()));
}
void CutterCore::saveProject(const QString &name)
{
const QString &rv = cmd("Ps " + name.trimmed()).trimmed();
const bool ok = rv == name.trimmed();
cmd(QString("Pnj ") + notes.toUtf8().toBase64());
emit projectSaved(ok, name);
}
void CutterCore::deleteProject(const QString &name)
{
cmd("Pd " + name);
}
bool CutterCore::isProjectNameValid(const QString &name)
{
// see is_valid_project_name() in libr/core/project.c
static const QRegExp regexp(R"(^[a-zA-Z0-9\\\._:-]{1,}$)");
return regexp.exactMatch(name) && !name.endsWith(".zip") ;
}
QList<DisassemblyLine> CutterCore::disassembleLines(RVA offset, int lines)
{
2018-03-21 20:32:32 +00:00
QJsonArray array = cmdj(QString("pdJ ") + QString::number(lines) + QString(" @ ") + QString::number(
offset)).array();
QList<DisassemblyLine> r;
2019-02-15 12:33:23 +00:00
for (const QJsonValueRef &value : array) {
QJsonObject object = value.toObject();
DisassemblyLine line;
line.offset = object[RJsonKey::offset].toVariant().toULongLong();
2019-02-15 12:33:23 +00:00
line.text = ansiEscapeToHtml(object[RJsonKey::text].toString());
r << line;
}
return r;
}
2017-12-14 12:45:03 +00:00
QByteArray CutterCore::hexStringToBytes(const QString &hex)
{
QByteArray hexChars = hex.toUtf8();
QByteArray bytes;
bytes.reserve(hexChars.length() / 2);
int size = r_hex_str2bin(hexChars.constData(), reinterpret_cast<ut8 *>(bytes.data()));
bytes.resize(size);
return bytes;
}
QString CutterCore::bytesToHexString(const QByteArray &bytes)
{
QByteArray hex;
hex.resize(bytes.length() * 2);
r_hex_bin2str(reinterpret_cast<const ut8 *>(bytes.constData()), bytes.size(), hex.data());
return QString::fromUtf8(hex);
}
2017-12-14 12:45:03 +00:00
void CutterCore::loadScript(const QString &scriptname)
{
r_core_task_sync_begin(core_);
2018-07-24 17:50:55 +00:00
r_core_cmd_file(core_, scriptname.toUtf8().constData());
r_core_task_sync_end(core_);
triggerRefreshAll();
2017-12-14 12:45:03 +00:00
}
2018-07-24 17:50:55 +00:00
2017-12-15 16:09:04 +00:00
QString CutterCore::getVersionInformation()
{
int i;
QString ret;
struct vcs_t {
const char *name;
const char *(*callback)();
} vcs[] = {
{ "r_anal", &r_anal_version },
{ "r_lib", &r_lib_version },
{ "r_egg", &r_egg_version },
{ "r_asm", &r_asm_version },
{ "r_bin", &r_bin_version },
{ "r_cons", &r_cons_version },
{ "r_flag", &r_flag_version },
{ "r_core", &r_core_version },
{ "r_crypto", &r_crypto_version },
{ "r_bp", &r_bp_version },
{ "r_debug", &r_debug_version },
{ "r_hash", &r_hash_version },
{ "r_fs", &r_fs_version },
{ "r_io", &r_io_version },
2019-01-14 20:28:03 +00:00
#if !USE_LIB_MAGIC
2017-12-15 16:09:04 +00:00
{ "r_magic", &r_magic_version },
2019-01-14 20:28:03 +00:00
#endif
2017-12-15 16:09:04 +00:00
{ "r_parse", &r_parse_version },
{ "r_reg", &r_reg_version },
{ "r_sign", &r_sign_version },
{ "r_search", &r_search_version },
{ "r_syscall", &r_syscall_version },
{ "r_util", &r_util_version },
/* ... */
2018-03-21 20:32:32 +00:00
{NULL, NULL}
2017-12-15 16:09:04 +00:00
};
ret.append(QString("%1 r2\n").arg(R2_GITTAP));
for (i = 0; vcs[i].name; i++) {
2018-03-21 20:32:32 +00:00
struct vcs_t *v = &vcs[i];
const char *name = v->callback ();
ret.append(QString("%1 %2\n").arg(name, v->name));
2017-12-15 16:09:04 +00:00
}
return ret;
}
2018-06-25 19:28:34 +00:00
QJsonArray CutterCore::getOpenedFiles()
{
QJsonDocument files = cmdj("oj");
return files.array();
}
2018-05-29 16:19:59 +00:00
QList<QString> CutterCore::getColorThemes()
{
QList<QString> r;
QJsonDocument themes = cmdj("ecoj");
for (const QJsonValue &s : themes.array()) {
r << s.toString();
2018-11-05 21:51:27 +00:00
}
return r;
}
2018-05-29 16:19:59 +00:00
2019-02-15 12:33:23 +00:00
QString CutterCore::ansiEscapeToHtml(const QString &text)
{
int len;
char *html = r_cons_html_filter(text.toUtf8().constData(), &len);
if (!html) {
return QString();
}
QString r = QString::fromUtf8(html, len);
r_free(html);
2019-02-15 12:33:23 +00:00
return r;
}
2019-02-19 18:56:59 +00:00
BasicBlockHighlighter* CutterCore::getBBHighlighter()
{
return bbHighlighter;
}