ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
Query

Files

file  query.h
 Runtime variable query tool API for debug builds.

Functions

int query_init (int preferred_port)
 Initialize the query tool by spawning the controller process.
void query_destroy (void)
 Shutdown the query tool and terminate the controller process.
bool query_is_active (void)
 Check if the query tool controller is currently active.
int query_get_port (void)
 Get the port number of the active query server.

Convenience Macros

These macros provide a convenient interface that compiles out completely in release builds. Use these instead of calling the functions directly.

#define QUERY_INIT(port)
 Initialize query tool (debug builds only).
#define QUERY_SHUTDOWN()
 Shutdown query tool (debug builds only).
#define QUERY_ACTIVE()
 Check if query tool is active (debug builds only).
#define QUERY_PORT()
 Get query server port (debug builds only).

Detailed Description

This header provides the public C API for the query tool, which enables runtime variable inspection via HTTP queries. The tool uses an external LLDB process to attach to the running application and read variable values.

Architecture

  • Target process (ascii-chat) runs normally with debug symbols
  • Controller process (ascii-query-server) attaches via LLDB
  • HTTP server in controller accepts curl requests
  • When target is stopped at breakpoint, controller is still running

Example Usage

// In your application startup (debug builds only)
int port = QUERY_INIT(9999);
if (port > 0) {
printf("Query server at http://localhost:%d\n", port);
}
// ... application runs ...
// Query variables via curl:
// curl 'localhost:9999/query?file=src/server.c&line=100&name=client_count'
// curl 'localhost:9999/query?file=src/server.c&line=100&name=client.socket.fd&break'
// curl -X POST 'localhost:9999/continue'
// On shutdown
#define QUERY_INIT(port)
Initialize query tool (debug builds only).
Definition query.h:123
#define QUERY_SHUTDOWN()
Shutdown query tool (debug builds only).
Definition query.h:128

Note: All functions and macros compile out completely in release builds (when NDEBUG is defined). The query tool has zero runtime overhead in production builds.

See also
docs/tooling/query.md for full documentation
docs/tooling/QUERY_TOOL_PLAN.md for implementation details

Macro Definition Documentation

◆ QUERY_ACTIVE

#define QUERY_ACTIVE ( )

#include <include/ascii-chat/tooling/query/query.h>

Value:
bool query_is_active(void)
Check if the query tool controller is currently active.
Definition query.c:369

Check if query tool is active (debug builds only).

Returns
true if active, false otherwise (always false in release builds)

Definition at line 134 of file query.h.

◆ QUERY_INIT

#define QUERY_INIT ( port)

#include <include/ascii-chat/tooling/query/query.h>

Value:
int query_init(int preferred_port)
Initialize the query tool by spawning the controller process.
Definition query.c:217

Initialize query tool (debug builds only).

Parameters
portPreferred HTTP server port
Returns
Port number on success, -1 on failure (or in release builds)

Definition at line 123 of file query.h.

◆ QUERY_PORT

#define QUERY_PORT ( )

#include <include/ascii-chat/tooling/query/query.h>

Value:
int query_get_port(void)
Get the port number of the active query server.
Definition query.c:401

Get query server port (debug builds only).

Returns
Port number if active, -1 otherwise (always -1 in release builds)

Definition at line 140 of file query.h.

◆ QUERY_SHUTDOWN

#define QUERY_SHUTDOWN ( )

#include <include/ascii-chat/tooling/query/query.h>

Value:
void query_destroy(void)
Shutdown the query tool and terminate the controller process.
Definition query.c:312

Shutdown query tool (debug builds only).

Definition at line 128 of file query.h.

Function Documentation

◆ query_destroy()

void query_destroy ( void )

#include <include/ascii-chat/tooling/query/query.h>

Shutdown the query tool and terminate the controller process.

This function cleanly terminates the ascii-query-server process. The target process continues running normally after shutdown.

Note
Safe to call even if query_init() was not called or failed
Only available in debug builds (NDEBUG not defined)

Definition at line 312 of file query.c.

312 {
313 if (!g_query_active && g_controller_pid <= 0) {
314 return;
315 }
316
317 fprintf(stderr, "[query] Shutting down query server...\n");
318
319#ifdef _WIN32
320 if (g_controller_handle != NULL) {
321 // Send termination signal
322 TerminateProcess(g_controller_handle, 0);
323
324 // Wait for process to exit (with timeout)
325 WaitForSingleObject(g_controller_handle, 3000);
326
327 CloseHandle(g_controller_handle);
328 g_controller_handle = NULL;
329 g_controller_pid = 0;
330 }
331
332 // Winsock cleanup handled by platform_cleanup()
333#else
334 if (g_controller_pid > 0) {
335 // Send SIGTERM for graceful shutdown
336 kill(g_controller_pid, SIGTERM);
337
338 // Wait for process to exit (with timeout)
339 int status;
340 int wait_count = 0;
341 while (wait_count < 30) { // 3 second timeout
342 pid_t result = waitpid(g_controller_pid, &status, WNOHANG);
343 if (result == g_controller_pid) {
344 break;
345 }
346 if (result < 0) {
347 break;
348 }
349 usleep(100 * US_PER_MS_INT); // 100ms
350 wait_count++;
351 }
352
353 // If still running, force kill
354 if (wait_count >= 30) {
355 kill(g_controller_pid, SIGKILL);
356 waitpid(g_controller_pid, &status, 0);
357 }
358
359 g_controller_pid = -1;
360 }
361#endif
362
363 g_query_active = false;
364 g_query_port = -1;
365
366 fprintf(stderr, "[query] Query server stopped\n");
367}
#define US_PER_MS_INT
Definition time.h:176

References US_PER_MS_INT.

Referenced by query_init().

◆ query_get_port()

int query_get_port ( void )

#include <include/ascii-chat/tooling/query/query.h>

Get the port number of the active query server.

Returns
The port number if active, -1 if not active
Note
Only available in debug builds (NDEBUG not defined)

Definition at line 401 of file query.c.

401 {
402 return g_query_port;
403}

◆ query_init()

int query_init ( int preferred_port)

#include <include/ascii-chat/tooling/query/query.h>

Initialize the query tool by spawning the controller process.

This function spawns the ascii-query-server process, which attaches to the current process via LLDB and starts an HTTP server on the specified port.

The controller process is completely separate from the target process:

  • Target can be stopped at breakpoints while controller serves HTTP
  • Controller sends LLDB commands to read variables and control execution
  • No instrumentation or code modification in the target is required
Parameters
preferred_portThe port number for the HTTP server (e.g., 9999)
Returns
The actual port number on success, -1 on failure
Note
Only available in debug builds (NDEBUG not defined)
The controller may take a moment to attach; this function waits for the HTTP server to become ready before returning

Platform notes:

  • macOS: May require code signing with get-task-allow entitlement
  • Linux: May require ptrace permissions (check /proc/sys/kernel/yama/ptrace_scope)
  • Windows: Uses CreateProcess instead of fork/exec

Definition at line 217 of file query.c.

217 {
218 // Already initialized?
219 if (g_query_active) {
220 return g_query_port;
221 }
222
223 // Find the query server executable
224 char server_path[PLATFORM_MAX_PATH_LENGTH];
225 if (!find_query_server_path(server_path, sizeof(server_path))) {
226 fprintf(stderr, "[query] Could not find ascii-query-server executable\n");
227 fprintf(stderr, "[query] Set ASCIICHAT_QUERY_SERVER environment variable or ensure "
228 "it's in .deps-cache/query-tool/\n");
229 return -1;
230 }
231
232#ifdef _WIN32
233 // Windows implementation using CreateProcess
234 // Winsock is already initialized by platform_init()
235
237 safe_snprintf(cmdline, sizeof(cmdline), "\"%s\" --attach %lu --port %d", server_path, (unsigned long)GetCurrentProcessId(), preferred_port);
238
239 STARTUPINFOA si;
240 PROCESS_INFORMATION pi;
241 memset(&si, 0, sizeof(si));
242 si.cb = sizeof(si);
243 memset(&pi, 0, sizeof(pi));
244
245 // Create the controller process
246 if (!CreateProcessA(NULL, // Use command line
247 cmdline, // Command line
248 NULL, // Process security attributes
249 NULL, // Thread security attributes
250 FALSE, // Don't inherit handles
251 CREATE_NEW_CONSOLE, // Creation flags
252 NULL, // Use parent's environment
253 NULL, // Use parent's directory
254 &si, &pi)) {
255 fprintf(stderr, "[query] Failed to start query server: error %lu\n", GetLastError());
256 return -1;
257 }
258
259 g_controller_handle = pi.hProcess;
260 g_controller_pid = pi.dwProcessId;
261 CloseHandle(pi.hThread); // Don't need the thread handle
262
263 fprintf(stderr, "[query] Started query server (PID %lu) on port %d\n", (unsigned long)g_controller_pid, preferred_port);
264
265#else
266 // Unix implementation using fork/exec
267 pid_t self_pid = getpid();
268
269 char port_str[TOOLING_QUERY_PORT_BUFFER_SIZE];
270 char pid_str[TOOLING_QUERY_PID_BUFFER_SIZE];
271 safe_snprintf(port_str, sizeof(port_str), "%d", preferred_port);
272 safe_snprintf(pid_str, sizeof(pid_str), "%d", self_pid);
273
274 pid_t child = fork();
275 if (child < 0) {
276 fprintf(stderr, "[query] fork() failed: %s\n", strerror(errno));
277 return -1;
278 }
279
280 if (child == 0) {
281 // Child process: exec the controller
282 // Redirect stdout/stderr to /dev/null or a log file to avoid clutter
283 // (The controller has its own logging)
284
285 execl(server_path, "ascii-query-server", "--attach", pid_str, "--port", port_str, (char *)NULL);
286
287 // If exec fails
288 fprintf(stderr, "[query] exec(%s) failed: %s\n", server_path, strerror(errno));
289 exit(1);
290 }
291
292 // Parent process
293 g_controller_pid = child;
294 fprintf(stderr, "[query] Started query server (PID %d) on port %d\n", child, preferred_port);
295#endif
296
297 // Wait for the HTTP server to become ready
298 fprintf(stderr, "[query] Waiting for HTTP server to be ready...\n");
299 if (!wait_for_http_ready(preferred_port, HEALTH_CHECK_TIMEOUT_NS)) {
300 fprintf(stderr, "[query] Timeout waiting for query server to start\n");
302 return -1;
303 }
304
305 g_query_active = true;
306 g_query_port = preferred_port;
307
308 fprintf(stderr, "[query] Query server ready at http://localhost:%d\n", preferred_port);
309 return preferred_port;
310}
#define TOOLING_QUERY_PID_BUFFER_SIZE
Definition query.c:16
#define HEALTH_CHECK_TIMEOUT_NS
Definition query.c:45
#define TOOLING_QUERY_PORT_BUFFER_SIZE
Definition query.c:15
#define TOOLING_QUERY_CMDLINE_BUFFER_SIZE
Definition query.c:14

References HEALTH_CHECK_TIMEOUT_NS, query_destroy(), TOOLING_QUERY_CMDLINE_BUFFER_SIZE, TOOLING_QUERY_PID_BUFFER_SIZE, and TOOLING_QUERY_PORT_BUFFER_SIZE.

◆ query_is_active()

bool query_is_active ( void )

#include <include/ascii-chat/tooling/query/query.h>

Check if the query tool controller is currently active.

Returns
true if the controller process is running and responsive
false if the controller is not running or not responding
Note
Only available in debug builds (NDEBUG not defined)

Definition at line 369 of file query.c.

369 {
370 if (!g_query_active) {
371 return false;
372 }
373
374// Verify the controller is still running
375#ifdef _WIN32
376 if (g_controller_handle != NULL) {
377 DWORD exit_code;
378 if (GetExitCodeProcess(g_controller_handle, &exit_code)) {
379 if (exit_code != STILL_ACTIVE) {
380 g_query_active = false;
381 g_controller_handle = NULL;
382 g_controller_pid = 0;
383 return false;
384 }
385 }
386 }
387#else
388 if (g_controller_pid > 0) {
389 // Check if process is still alive
390 if (kill(g_controller_pid, 0) != 0) {
391 g_query_active = false;
392 g_controller_pid = -1;
393 return false;
394 }
395 }
396#endif
397
398 return g_query_active;
399}