Files
r2xsql/examples/list_funcs.cpp
Elias Bachaalany 288bcaab43 r2xsql 0.0.1 - SQL interface for radare2
r2xsql exposes a radare2 analysis session as live SQL virtual tables: every
query re-issues the JSON form of the relevant r2 command (aflj, izj, axtj, ...)
against the current core, so results are never a stale dump. Reads are SELECTs;
annotation is ordinary SQL writes -- rename functions, set prototypes, comments,
flags, locals, types, bookmarks, patches -- and work saves back through
radare2's native project format.

Two interchangeable flavors over one SQL surface:
  - r2xsql       portable; drives radare2 over r2pipe, needs only the radare2
                 binary on PATH at runtime. Always built.
  - r2xsql-full  in-process (libr) build; also ships core_r2xsql, an in-r2
                 plugin that adds the `sql` command inside radare2. ABI-locked
                 to the radare2 it was built against.

~48 tables and 13 views span functions, blocks, instructions, xrefs, strings,
imports/exports, sections/segments, flags, comments, types, classes, relocs,
registers, syscalls, calling conventions, ROP gadgets, ESIL, and decompilation.
An always-on HTTP REST endpoint and an MCP server expose the same surface to
tooling and agents.

Build with CMake (both flavors, MCP, tests), or -- for the plugin alone -- a
radare2-native Makefile / meson build (plugin paths from `r2 -H`, radare2 via
pkg-config r_core). Agent skills for driving r2xsql live in
radareorg/radare2-skills.

Developed by Elias Bachaalany, in collaboration with the radare2 project (radareorg).
2026-08-12 13:28:52 -07:00

79 lines
2.1 KiB
C++

// SPDX-License-Identifier: LicenseRef-Human-Origin-Source-1.0
// Copyright (c) 2024-2026 Elias Bachaalany
//
// This file is licensed under the Human-Origin Source License v1.0.
// See LICENSE.
//
// list_funcs.cpp — minimal libr2xsql consumer.
//
// Opens a binary (libr backend if available, else r2pipe), lists the
// first N functions via SQL. Demonstrates the API a third-party tool
// would use to embed r2xsql.
#include <r2xsql/r2xsql.hpp>
#include <cstdio>
#include <cstdlib>
#include <memory>
#include <string>
static void usage(const char* argv0) {
std::fprintf(stderr,
"usage: %s <binary-or-project> [limit]\n"
" Lists the first <limit> functions (default 20).\n",
argv0);
}
int main(int argc, char** argv) {
if (argc < 2) {
usage(argv[0]);
return 2;
}
const std::string path = argv[1];
const int limit = (argc >= 3) ? std::atoi(argv[2]) : 20;
std::unique_ptr<r2xsql::Backend> backend;
// Prefer libr; fall back to r2pipe if libr isn't linked or fails.
r2xsql::LibrOpenOptions libr_opts;
libr_opts.path = path;
backend = r2xsql::open_libr(libr_opts);
if (!backend) {
std::fprintf(stderr, "libr backend unavailable, falling back to r2pipe...\n");
r2xsql::R2PipeOpenOptions pipe_opts;
pipe_opts.path = path;
backend = r2xsql::open_r2pipe(pipe_opts);
}
if (!backend) {
std::fprintf(stderr, "error: could not open '%s' with any backend\n",
path.c_str());
return 1;
}
r2xsql::Session sess(std::move(backend));
const std::string sql =
"SELECT addr, name, size FROM funcs ORDER BY size DESC LIMIT " +
std::to_string(limit);
auto result = sess.query(sql);
if (!result.ok()) {
std::fprintf(stderr, "error: query failed\n");
return 1;
}
std::printf("%-18s %-40s %s\n", "addr", "name", "size");
std::printf("%-18s %-40s %s\n", "------------------",
"----------------------------------------", "----");
for (auto& row : result.rows) {
std::printf("%-18s %-40s %s\n",
row[0].c_str(), row[1].c_str(), row[2].c_str());
}
std::printf("\n%zu rows\n", result.rows.size());
return 0;
}