Skip to content

Plugin C99 ABI

github-actions[bot] edited this page Dec 6, 2026 · 2 revisions

GridLock C99 Plugin ABI v2.0 Reference

Every GridLock native plugin targets the pure-C99 ABI defined in include/gridlock/plugin_api.h (GRIDLOCK_PLUGIN_API_VERSION = 0x00020000u, entry symbol gridlock_plugin_init). No Qt6 headers or C++ standard library symbols are required.


1. Root Vtable (gridlock_host_api_t) & Version/Size Handshake

#define GRIDLOCK_PLUGIN_ABI_MAJOR 2u
#define GRIDLOCK_PLUGIN_ABI_MINOR 0u
#define GRIDLOCK_PLUGIN_API_VERSION \
  GRIDLOCK_MAKE_API_VERSION(GRIDLOCK_PLUGIN_ABI_MAJOR, GRIDLOCK_PLUGIN_ABI_MINOR)
#define GRIDLOCK_PLUGIN_INIT_SYMBOL "gridlock_plugin_init"

typedef struct gridlock_host_api_s {
  uint32_t api_version;
  uint32_t struct_size;
  void* host_ctx;
  const gridlock_core_api_t* core;
  const gridlock_buf_api_t* buf;
  const gridlock_keymap_api_t* keymap;
  const gridlock_autocmd_api_t* autocmd;
  const gridlock_dbg_api_t* dbg;
  const gridlock_ui_api_t* ui;
  const gridlock_marks_regs_api_t* marks_regs;
  const gridlock_diag_api_t* diag;
  void* reserved[4];
} gridlock_host_api_t;

Mandatory Entry-Point & Struct Size Handshake

Your shared library exports gridlock_plugin_init using GRIDLOCK_PLUGIN_EXPORT and GRIDLOCK_CALL:

GRIDLOCK_PLUGIN_EXPORT GridLockStatus GRIDLOCK_CALL gridlock_plugin_init(
    const gridlock_host_api_t* host,
    gridlock_plugin_descriptor_t* out_desc) {
  /* 1. Validate root host pointer, major/minor API version, and struct size */
  if (!host || !out_desc) return GRIDLOCK_ERR_INVALID_ARG;
  if (GRIDLOCK_API_VERSION_MAJOR(host->api_version) != GRIDLOCK_PLUGIN_ABI_MAJOR ||
      host->struct_size < (uint32_t)sizeof(gridlock_host_api_t)) {
    return GRIDLOCK_ERR_ABI_MISMATCH;
  }
  /* 2. Validate any required sub-table before dereferencing its function pointers */
  if (!host->core || host->core->struct_size < (uint32_t)sizeof(gridlock_core_api_t)) {
    return GRIDLOCK_ERR_ABI_MISMATCH;
  }
  /* 3. Stamp out_desc header (caller-zeroed by host; verify if non-zero) */
  if (out_desc->struct_size != 0u &&
      out_desc->struct_size < (uint32_t)sizeof(gridlock_plugin_descriptor_t)) {
    return GRIDLOCK_ERR_ABI_MISMATCH;
  }
  out_desc->api_version = GRIDLOCK_PLUGIN_API_VERSION;
  out_desc->struct_size = (uint32_t)sizeof(*out_desc);
  out_desc->reserved_flags = 0u;
  /* ... populate metadata strings (must remain valid until shutdown returns) ... */
  return GRIDLOCK_OK;
}

Every descriptor passed into a sub-table (gridlock_command_desc_t, gridlock_keymap_desc_t, gridlock_autocmd_desc_t, gridlock_panel_desc_t, gridlock_draw_cmd_t, gridlock_type_visualizer_desc_t, gridlock_rank_badge_desc_t, etc.) begins with uint32_t struct_size; - always zero-initialize the struct (= {0}) and set .struct_size = (uint32_t)sizeof(desc) so minor v2.x additions remain forward- and backward-compatible.


2. Sub-Table Function Reference

All calls pass host->host_ctx as the first argument and return GridLockStatus (GRIDLOCK_OK = 0, GRIDLOCK_ERR_INVALID_ARG = -1, GRIDLOCK_ERR_ABI_MISMATCH = -2, GRIDLOCK_ERR_BUFFER_TOO_SMALL = -3, GRIDLOCK_ERR_NOT_STOPPED = -4, GRIDLOCK_ERR_INVALID_RANK = -5, GRIDLOCK_ERR_EVAL_FAILED = -6, GRIDLOCK_ERR_MEMORY_READ_FAILED = -7, GRIDLOCK_ERR_UNSUPPORTED = -8, GRIDLOCK_ERR_TIMEOUT = -9, GRIDLOCK_ERR_CANCELLED = -10, GRIDLOCK_ERR_NOT_FOUND = -11).

host->core (include/gridlock/plugin_core_api.h)

  • alloc_bytes(host_ctx, num_bytes) / free_bytes(host_ctx, ptr) - Tracked host heap allocation; gridlock --verify-plugin fails if outstandingAllocBytes() != 0 after shutdown().
  • log_message(host_ctx, level, message_utf8) - ANSI/OSC-sanitized workstation log (GRIDLOCK_LOG_TRACE..GRIDLOCK_LOG_ERROR).
  • get_config_dir(host_ctx, out_utf8, cap, out_len) / get_data_dir(...) / get_workspace_root(...) - Two-pass UTF-8 path queries (--config-dir aware).
  • is_workspace_trusted(host_ctx, out_trusted) - Queries SHA-256 workspace trust state.
  • defer_fn(host_ctx, delay_ms, fn, user_data) - Schedules a one-shot callback on the Qt GUI thread; cancelled automatically on plugin unload.

host->buf (include/gridlock/plugin_buf_api.h)

  • open_virtual(host_ctx, uri_utf8, title_utf8, flags, out_buf_id) / close_buffer(host_ctx, buf_id) / find_buffer(...) / get_active_buffer(...)
  • get_line_count(host_ctx, buf_id, out_count) / get_lines(...) / set_lines(...) (backed directly by BufferEngine PieceTable)
  • get_cursor(...) / set_cursor(...) / get_selection(...) / set_selection(...)
  • create_namespace(host_ctx, name_utf8, out_ns_id) / set_extmark(...) / del_extmark(...) / clear_namespace(...) / set_extmark_virt_lines(host_ctx, buf_id, ns_id, extmark_id, lines, line_count) / get_extmark_virt_lines(...) (backed directly by AnchorStore, using pure-C99 gridlock_virt_chunk_t and gridlock_virt_line_t for multi-line virtual code blocks rendered below an anchor line without mutating PieceTable)

host->keymap (include/gridlock/plugin_keymap_api.h)

  • register_command(host_ctx, desc) / unregister_command(host_ctx, command_id_utf8) / invoke_command(host_ctx, command_id_utf8, args_utf8)
  • set_keymap(host_ctx, desc) / del_keymap(host_ctx, mode, buffer_id, lhs_utf8) / set_leader_key(host_ctx, leader_utf8)
  • get_editor_mode(host_ctx, out_mode) / set_editor_mode(host_ctx, mode) (GRIDLOCK_KEYMAP_MODE_GLOBAL=0, NORMAL=1, INSERT=2, VISUAL=3, DEBUG=4).
  • get_vim_mode(host_ctx, out_enabled) / set_vim_mode(host_ctx, enabled) - Queries or updates editor.vim_mode (gl.editor.vim_mode) on the GUI thread.

host->autocmd (include/gridlock/plugin_autocmd_api.h)

  • subscribe(host_ctx, desc, out_handle) / unsubscribe(host_ctx, handle) / clear_group(host_ctx, group_utf8)
  • Events (gridlock_autocmd_event_t): GRIDLOCK_AUTOCMD_BUF_READ_POST, BUF_WRITE_PRE (vetoable), BUF_WRITE_POST, CURSOR_MOVED, MODE_CHANGED, BUILD_FINISHED, THEME_CHANGED, DEBUG_LAUNCH_PRE (vetoable), DEBUG_SESSION_START, RANK_STOPPED, BREAKPOINT_HIT, FINALIZE_DETECTED.

host->dbg (include/gridlock/plugin_dbg_api.h)

  • active_rank(host_ctx, out_rank) / rank_count(host_ctx, out_ranks) / rank_state(host_ctx, rank, out_utf8, cap, out_len)
  • send_mi_async(host_ctx, rank, mi_cmd_utf8, reply_cb, user_data) - Tokenized async GDB/MI command; delivers token-stripped ^done,... / ^error,... payload on the GUI thread.
  • register_type_visualizer(host_ctx, desc) / unregister_type_visualizer(host_ctx, id_utf8)
  • set_rank_badge(host_ctx, desc) / clear_rank_badge(host_ctx, rank)
  • eval_expr(...), read_memory(...), write_memory(...), set_breakpoint(...), remove_breakpoint(...), query_type_info(...), resolve_contiguous_range(...), register_variable_action(...), query_mpi_collectives(...), query_mpi_queues(...).

host->ui (include/gridlock/plugin_ui_api.h)

  • create_panel(host_ctx, desc, out_panel_id) / close_panel(host_ctx, panel_id) / update_panel_rows(...)
    • Kinds: GRIDLOCK_PANEL_TABLE, TREE, TEXT, CUSTOM_BUFFER, CANVAS.
    • Dock areas: GRIDLOCK_PANEL_DOCK_BOTTOM, RIGHT, POPUP (non-modal Qt::Tool floating dialog with draggable header & close button).
  • open_popup(host_ctx, desc, out_popup_id) / close_popup(host_ctx, popup_id)
  • set_status_segment(host_ctx, desc) / remove_status_segment(host_ctx, segment_id_utf8)
  • prompt_input(...) / show_toast(...) / navigate_to_location(...) / get_theme_info(...) / emit_structured_event(...)
  • Retained-mode 2D Canvas: canvas_submit_commands(...) (CLEAR, LINE, POLYLINE, RECT, CIRCLE, TEXT), canvas_blit_scalar_grid(...) (VIRIDIS, PLASMA, COOLWARM, GRAYSCALE), canvas_blit_rgba(...), canvas_set_click_handler(...), canvas_set_key_handler(...).

host->marks_regs & host->diag (include/gridlock/plugin_editor_extras_api.h)

  • set_mark, get_mark, del_mark, get_register, set_register
  • set_diagnostics, clear_diagnostics

3. Thread-Safety, UI-Thread Dispatch & Symbol Hiding (plugin.map)

Thread-Safety & Dispatch Guarantees

Category Calling Thread Rules Callback Delivery Thread
host->core (alloc_bytes, free_bytes, log_message) Thread-safe from any thread (std::atomic / queued signal). defer_fn always fires on the Qt GUI thread.
host->ui, host->buf, host->keymap, host->autocmd, host->marks_regs, host->diag Safe from the GUI thread or worker threads (host marshals UI mutations via QMetaObject::invokeMethod). All command, keymap, autocmd, prompt, canvas click, and canvas key callbacks fire on the Qt GUI thread.
host->dbg (send_mi_async, eval_expr, read_memory, write_memory) Safe from GUI or worker threads; never call blocking eval_expr / read_memory from inside shutdown(). send_mi_async replies marshal onto the Qt GUI thread; cancelled safely if the plugin unloads before GDB replies.
Shutdown Lockout Once shutdown begins, PluginBindingContext::shuttingDown is set (memory_order_release). Late UI/registration/debugger calls return GRIDLOCK_ERR_UNSUPPORTED or GRIDLOCK_ERR_NOT_STOPPED. Release all alloc_bytes pointers before shutdown returns.

Symbol-Hiding Linker Version Script (plugin.map)

PluginManager loads shared libraries with QLibrary::ExportExternalSymbolsHint (RTLD_GLOBAL on Linux) so Tier-2 hosts such as libgridlock_lua.so can load C-backed LuaRocks modules. To avoid symbol collisions and prevent glibc STB_GNU_UNIQUE symbols from pinning your .so across live hot-reloads, compile with -fvisibility=hidden and pass a linker version script (-Wl,--version-script=plugin.map):

GRIDLOCK_ABI_2_0 {
  global:
    gridlock_plugin_init;
    gridlock_plugin_shutdown;
  local:
    *;
};

Tip

Verify your compiled binary's symbol hygiene and leak-free lifecycle at any time with: QT_QPA_PLATFORM=offscreen gridlock --verify-plugin ./libhello_c99.so


4. Complete Buildable C99 Plugin Example

#include <gridlock/plugin_api.h>
#include <stdio.h>

static void GRIDLOCK_CALL on_shutdown(void* ctx, const gridlock_host_api_t* host) {
  (void)ctx;
  if (host && host->core && host->core->log_message) {
    host->core->log_message(host->host_ctx, GRIDLOCK_LOG_INFO, "hello_c99: shutdown");
  }
}

static GridLockStatus GRIDLOCK_CALL on_hello_cmd(void* ctx, const char* cmd_id, const char* args) {
  const gridlock_host_api_t* host = (const gridlock_host_api_t*)ctx;
  (void)cmd_id; (void)args;
  if (host && host->ui && host->ui->show_toast) {
    host->ui->show_toast(host->host_ctx, GRIDLOCK_DIALOG_INFO, "Hello C99", "ABI v2.0 active!", 2500);
  }
  return GRIDLOCK_OK;
}

GRIDLOCK_PLUGIN_EXPORT GridLockStatus GRIDLOCK_CALL gridlock_plugin_init(
    const gridlock_host_api_t* host,
    gridlock_plugin_descriptor_t* out_desc) {
  if (!host || !out_desc ||
      GRIDLOCK_API_VERSION_MAJOR(host->api_version) != GRIDLOCK_PLUGIN_ABI_MAJOR ||
      host->struct_size < (uint32_t)sizeof(gridlock_host_api_t)) {
    return GRIDLOCK_ERR_ABI_MISMATCH;
  }
  out_desc->api_version = GRIDLOCK_PLUGIN_API_VERSION;
  out_desc->struct_size = (uint32_t)sizeof(*out_desc);
  out_desc->reserved_flags = 0u;
  out_desc->plugin_id_utf8 = "org.example.hello_c99";
  out_desc->plugin_name_utf8 = "Hello C99";
  out_desc->plugin_version_utf8 = "1.0.0";
  out_desc->description_utf8 = "Minimal C99 ABI v2.0 plugin";
  out_desc->plugin_ctx = (void*)host;
  out_desc->shutdown = &on_shutdown;

  if (host->keymap && host->keymap->register_command) {
    gridlock_command_desc_t cmd = {0};
    cmd.struct_size = (uint32_t)sizeof(cmd);
    cmd.command_id_utf8 = "example.hello";
    cmd.title_utf8 = "Hello C99 Toast";
    cmd.callback = &on_hello_cmd;
    cmd.plugin_ctx = (void*)host;
    host->keymap->register_command(host->host_ctx, &cmd);
  }
  return GRIDLOCK_OK;
}

Compile with plugin.map and verify headlessly:

gcc -std=c99 -shared -fPIC -O2 -fvisibility=hidden \
    -Wl,--version-script=plugin.map -Iinclude hello_c99.c -o libhello_c99.so
QT_QPA_PLATFORM=offscreen ./builddir/gridlock --verify-plugin ./libhello_c99.so

Clone this wiki locally