Diagnosticsservice FFI and CPP bindings

This commit is contained in:
Jackson Coxson
2025-08-16 12:21:47 -06:00
parent 5cbdb2505a
commit 2b75fe1c05
7 changed files with 539 additions and 1 deletions

View File

@@ -0,0 +1,128 @@
// Jackson Coxson
#include <cstdint>
#include <fstream>
#include <iostream>
#include <optional>
#include <string>
#include <idevice++/bindings.hpp>
#include <idevice++/core_device_proxy.hpp>
#include <idevice++/diagnosticsservice.hpp>
#include <idevice++/ffi.hpp>
#include <idevice++/provider.hpp>
#include <idevice++/rsd.hpp>
#include <idevice++/usbmuxd.hpp>
using namespace IdeviceFFI;
static void fail(const char* msg, const FfiError& e) {
std::cerr << msg;
if (e)
std::cerr << ": " << e.message;
std::cerr << "\n";
std::exit(1);
}
int main() {
idevice_init_logger(Debug, Disabled, NULL);
FfiError err;
// 1) usbmuxd, pick first device
auto mux = UsbmuxdConnection::default_new(/*tag*/ 0, err);
if (!mux)
fail("failed to connect to usbmuxd", err);
auto devices = mux->get_devices(err);
if (!devices)
fail("failed to list devices", err);
if (devices->empty()) {
std::cerr << "no devices connected\n";
return 1;
}
auto& dev = (*devices)[0];
auto udid = dev.get_udid();
auto mux_id = dev.get_id();
if (!udid || !mux_id) {
std::cerr << "device missing udid or mux id\n";
return 1;
}
// 2) Provider via default usbmuxd addr
auto addr = UsbmuxdAddr::default_new();
const uint32_t tag = 0;
const std::string label = "diagnosticsservice-jkcoxson";
auto provider = Provider::usbmuxd_new(std::move(addr), tag, *udid, *mux_id, label, err);
if (!provider)
fail("failed to create provider", err);
// 3) CoreDeviceProxy
auto cdp = CoreDeviceProxy::connect(*provider, err);
if (!cdp)
fail("failed CoreDeviceProxy connect", err);
auto rsd_port = cdp->get_server_rsd_port(err);
if (!rsd_port)
fail("failed to get RSD port", err);
// 4) Software tunnel → connect to RSD
auto adapter = std::move(*cdp).create_tcp_adapter(err);
if (!adapter)
fail("failed to create software tunnel adapter", err);
auto stream = adapter->connect(*rsd_port, err);
if (!stream)
fail("failed to connect RSD stream", err);
// 5) RSD handshake
auto rsd = RsdHandshake::from_socket(std::move(*stream), err);
if (!rsd)
fail("failed RSD handshake", err);
// 6) Diagnostics Service over RSD
auto diag = DiagnosticsService::connect_rsd(*adapter, *rsd, err);
if (!diag)
fail("failed to connect DiagnosticsService", err);
std::cout << "Getting sysdiagnose, this takes a while! iOS is slow...\n";
auto cap = diag->capture_sysdiagnose(/*dry_run=*/false, err);
if (!cap)
fail("capture_sysdiagnose failed", err);
std::cout << "Got sysdiagnose! Saving to file: " << cap->preferred_filename << "\n";
// 7) Stream to file with progress
std::ofstream out(cap->preferred_filename, std::ios::binary);
if (!out) {
std::cerr << "failed to open output file\n";
return 1;
}
std::size_t written = 0;
const std::size_t total = cap->expected_length;
for (;;) {
auto chunk = cap->stream.next_chunk(err);
if (!chunk) {
if (err)
fail("stream error", err); // err set only on real error
break; // nullptr means end-of-stream
}
if (!chunk->empty()) {
out.write(reinterpret_cast<const char*>(chunk->data()),
static_cast<std::streamsize>(chunk->size()));
if (!out) {
std::cerr << "write failed\n";
return 1;
}
written += chunk->size();
}
std::cout << "wrote " << written << "/" << total << " bytes\r" << std::flush;
}
out.flush();
std::cout << "\nDone! Saved to " << cap->preferred_filename << "\n";
return 0;
}

View File

@@ -0,0 +1,110 @@
// Jackson Coxson
#pragma once
#include <cstddef>
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
#include <idevice++/bindings.hpp>
#include <idevice++/core_device_proxy.hpp>
#include <idevice++/ffi.hpp>
#include <idevice++/rsd.hpp>
namespace IdeviceFFI {
class SysdiagnoseStream {
public:
SysdiagnoseStream() = default;
SysdiagnoseStream(const SysdiagnoseStream&) = delete;
SysdiagnoseStream& operator=(const SysdiagnoseStream&) = delete;
SysdiagnoseStream(SysdiagnoseStream&& other) noexcept : h_(other.h_) { other.h_ = nullptr; }
SysdiagnoseStream& operator=(SysdiagnoseStream&& other) noexcept {
if (this != &other) {
reset();
h_ = other.h_;
other.h_ = nullptr;
}
return *this;
}
~SysdiagnoseStream() { reset(); }
// Pull next chunk. Returns nullopt on end-of-stream. On error, returns nullopt and sets `err`.
std::optional<std::vector<uint8_t>> next_chunk(FfiError& err);
SysdiagnoseStreamHandle* raw() const { return h_; }
private:
friend class DiagnosticsService;
explicit SysdiagnoseStream(::SysdiagnoseStreamHandle* h) : h_(h) {}
void reset() {
if (h_) {
::sysdiagnose_stream_free(h_);
h_ = nullptr;
}
}
::SysdiagnoseStreamHandle* h_ = nullptr;
};
// The result of starting a sysdiagnose capture.
struct SysdiagnoseCapture {
std::string preferred_filename;
std::size_t expected_length = 0;
SysdiagnoseStream stream;
};
// RAII for Diagnostics service client
class DiagnosticsService {
public:
DiagnosticsService() = default;
DiagnosticsService(const DiagnosticsService&) = delete;
DiagnosticsService& operator=(const DiagnosticsService&) = delete;
DiagnosticsService(DiagnosticsService&& other) noexcept : h_(other.h_) { other.h_ = nullptr; }
DiagnosticsService& operator=(DiagnosticsService&& other) noexcept {
if (this != &other) {
reset();
h_ = other.h_;
other.h_ = nullptr;
}
return *this;
}
~DiagnosticsService() { reset(); }
// Connect via RSD (borrows adapter & handshake; does not consume them)
static std::optional<DiagnosticsService>
connect_rsd(Adapter& adapter, RsdHandshake& rsd, FfiError& err);
// Create from a ReadWrite stream (consumes it)
static std::optional<DiagnosticsService> from_stream_ptr(::ReadWriteOpaque* consumed,
FfiError& err);
static std::optional<DiagnosticsService> from_stream(ReadWrite&& rw, FfiError& err) {
return from_stream_ptr(rw.release(), err);
}
// Start sysdiagnose capture; on success returns filename, length and a byte stream
std::optional<SysdiagnoseCapture> capture_sysdiagnose(bool dry_run, FfiError& err);
::DiagnosticsServiceHandle* raw() const { return h_; }
private:
explicit DiagnosticsService(::DiagnosticsServiceHandle* h) : h_(h) {}
void reset() {
if (h_) {
::diagnostics_service_free(h_);
h_ = nullptr;
}
}
::DiagnosticsServiceHandle* h_ = nullptr;
};
} // namespace IdeviceFFI

View File

@@ -0,0 +1,88 @@
// Jackson Coxson
#include <idevice++/diagnosticsservice.hpp>
namespace IdeviceFFI {
// Local helper: take ownership of a C string and convert to std::string
static std::optional<std::string> take_cstring(char* p) {
if (!p)
return std::nullopt;
std::string s(p);
::idevice_string_free(p);
return s;
}
// -------- SysdiagnoseStream --------
std::optional<std::vector<uint8_t>> SysdiagnoseStream::next_chunk(FfiError& err) {
if (!h_)
return std::nullopt;
uint8_t* data = nullptr;
std::size_t len = 0;
if (IdeviceFfiError* e = ::sysdiagnose_stream_next(h_, &data, &len)) {
err = FfiError(e);
return std::nullopt;
}
if (!data || len == 0) {
// End of stream
return std::nullopt;
}
// Copy into a C++ buffer
std::vector<uint8_t> out(len);
std::memcpy(out.data(), data, len);
idevice_data_free(data, len);
return out;
}
// -------- DiagnosticsService --------
std::optional<DiagnosticsService>
DiagnosticsService::connect_rsd(Adapter& adapter, RsdHandshake& rsd, FfiError& err) {
::DiagnosticsServiceHandle* out = nullptr;
if (IdeviceFfiError* e = ::diagnostics_service_connect_rsd(adapter.raw(), rsd.raw(), &out)) {
err = FfiError(e);
return std::nullopt;
}
return DiagnosticsService(out);
}
std::optional<DiagnosticsService> DiagnosticsService::from_stream_ptr(::ReadWriteOpaque* consumed,
FfiError& err) {
::DiagnosticsServiceHandle* out = nullptr;
if (IdeviceFfiError* e = ::diagnostics_service_new(consumed, &out)) {
err = FfiError(e);
return std::nullopt;
}
return DiagnosticsService(out);
}
std::optional<SysdiagnoseCapture> DiagnosticsService::capture_sysdiagnose(bool dry_run,
FfiError& err) {
if (!h_)
return std::nullopt;
char* filename_c = nullptr;
std::size_t expected_len = 0;
::SysdiagnoseStreamHandle* stream_h = nullptr;
if (IdeviceFfiError* e = ::diagnostics_service_capture_sysdiagnose(
h_, dry_run ? true : false, &filename_c, &expected_len, &stream_h)) {
err = FfiError(e);
return std::nullopt;
}
auto fname = take_cstring(filename_c).value_or(std::string{});
SysdiagnoseStream stream(stream_h);
SysdiagnoseCapture cap{/*preferred_filename*/ std::move(fname),
/*expected_length*/ expected_len,
/*stream*/ std::move(stream)};
return cap;
}
} // namespace IdeviceFFI