ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
main.cpp File Reference

ascii-query-server entry point More...

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Detailed Description

ascii-query-server entry point

This is the controller process for the query tool. It attaches to a target process via LLDB and serves HTTP requests for variable inspection.

Usage: ascii-query-server –attach <pid> –port 9999 ascii-query-server –attach-name ascii-chat –port 9999

See also
docs/tooling/QUERY_TOOL_PLAN.md

Definition in file main.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 393 of file main.cpp.

393 {
394 // Parse command line arguments
395 pid_t attach_pid = 0;
396 std::string attach_name;
397 bool wait_for = false;
398 uint16_t port = 9999;
399
400 for (int i = 1; i < argc; i++) {
401 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
402 printUsage(argv[0]);
403 return 0;
404 } else if (strcmp(argv[i], "--attach") == 0 && i + 1 < argc) {
405 // Parse PID using safe parsing utility
406 unsigned long pid_val = 0;
407 if (parse_ulong(argv[++i], &pid_val, 1, ULONG_MAX) != ASCIICHAT_OK) {
408 fprintf(stderr, "Error: Invalid PID value\n");
409 return 1;
410 }
411 attach_pid = static_cast<pid_t>(pid_val);
412 } else if (strcmp(argv[i], "--attach-name") == 0 && i + 1 < argc) {
413 attach_name = argv[++i];
414 } else if (strcmp(argv[i], "--wait") == 0) {
415 wait_for = true;
416 } else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc) {
417 // Parse port using safe parsing utility with proper range validation
418 if (parse_port(argv[++i], &port) != ASCIICHAT_OK) {
419 fprintf(stderr, "Error: Invalid port number (must be 1-65535)\n");
420 return 1;
421 }
422 } else {
423 fprintf(stderr, "Unknown option: %s\n", argv[i]);
424 printUsage(argv[0]);
425 return 1;
426 }
427 }
428
429 // Validate arguments
430 if (attach_pid == 0 && attach_name.empty()) {
431 fprintf(stderr, "Error: Must specify --attach <pid> or --attach-name <name>\n\n");
432 printUsage(argv[0]);
433 return 1;
434 }
435
436 // Setup signal handlers
437 signal(SIGINT, signalHandler);
438 signal(SIGTERM, signalHandler);
439#ifndef _WIN32
440 signal(SIGPIPE, SIG_IGN);
441#endif
442
443 // Initialize LLDB controller
445 g_controller = &controller;
446
447 if (!controller.initialize()) {
448 fprintf(stderr, "Error: Failed to initialize LLDB: %s\n", controller.lastError().c_str());
449 return 1;
450 }
451
452 // Attach to target
453 fprintf(stderr, "[ascii-query-server] Attaching to ");
454 if (attach_pid > 0) {
455 fprintf(stderr, "PID %llu...\n", static_cast<unsigned long long>(attach_pid));
456 if (!controller.attach(attach_pid)) {
457 fprintf(stderr, "Error: Failed to attach: %s\n", controller.lastError().c_str());
458 return 1;
459 }
460 } else {
461 fprintf(stderr, "process '%s'%s...\n", attach_name.c_str(), wait_for ? " (waiting)" : "");
462 if (!controller.attachByName(attach_name, wait_for)) {
463 fprintf(stderr, "Error: Failed to attach: %s\n", controller.lastError().c_str());
464 return 1;
465 }
466 }
467
468 fprintf(stderr, "[ascii-query-server] Attached to %s (PID %llu)\n", controller.targetName().c_str(),
469 static_cast<unsigned long long>(controller.targetPid()));
470
471 // Resume the target (it's stopped after attach)
472 if (controller.state() == ascii_query::ProcessState::Stopped) {
473 fprintf(stderr, "[ascii-query-server] Resuming target...\n");
474 controller.resume();
475 }
476
477 // Setup HTTP server
479 g_server = &server;
480
481 setupRoutes(server, controller);
482
483 // Start HTTP server
484 if (!server.start(port)) {
485 fprintf(stderr, "Error: Failed to start HTTP server: %s\n", server.lastError().c_str());
486 controller.detach();
487 return 1;
488 }
489
490 fprintf(stderr, "[ascii-query-server] HTTP server listening on http://localhost:%d\n", port);
491 fprintf(stderr, "[ascii-query-server] Press Ctrl+C to stop\n");
492
493 // Wait for shutdown
494 while (!g_shutdown_requested && controller.isAttached()) {
495#ifdef _WIN32
496 Sleep(100);
497#else
498 usleep(100000);
499#endif
500 ascii_query::ProcessState state = controller.state();
502 fprintf(stderr, "[ascii-query-server] Target %s, shutting down\n",
503 state == ascii_query::ProcessState::Exited ? "exited" : "crashed");
504 break;
505 }
506 }
507
508 // Cleanup
509 fprintf(stderr, "[ascii-query-server] Shutting down...\n");
510 server.stop();
511 controller.detach();
512 controller.shutdown();
513
514 fprintf(stderr, "[ascii-query-server] Done\n");
515 return 0;
516}
Simple single-threaded HTTP server.
bool start(uint16_t port)
Start the server.
const std::string & lastError() const
Get the last error message.
void stop()
Stop the server.
LLDB process controller.
bool attachByName(const std::string &process_name, bool wait_for=false)
Attach to a process by name.
pid_t targetPid() const
Get the PID of the attached process.
std::string targetName() const
Get the name of the attached process.
const std::string & lastError() const
Get the last error message.
bool initialize()
Initialize LLDB. Must be called before any other methods.
void shutdown()
Shutdown LLDB and release resources.
bool isAttached() const
Check if attached to a process.
bool resume()
Resume the target process.
void detach()
Detach from the current process.
bool attach(pid_t pid)
Attach to a process by PID.
ProcessState state() const
Get the current process state.
unsigned short uint16_t
Definition common.h:57
@ ASCIICHAT_OK
Definition error_codes.h:48
ProcessState
Process state enumeration.
@ Exited
Process has exited.
@ Crashed
Process crashed.
@ Stopped
Process is stopped (breakpoint, signal, etc.)
asciichat_error_t parse_ulong(const char *str, unsigned long *out_value, unsigned long min_value, unsigned long max_value)
Parse unsigned long integer with range validation.
Definition parsing.c:137
asciichat_error_t parse_port(const char *str, uint16_t *out_port)
Parse port number (1-65535) from string.
Definition parsing.c:221

References ASCIICHAT_OK, ascii_query::LLDBController::attach(), ascii_query::LLDBController::attachByName(), ascii_query::Crashed, ascii_query::LLDBController::detach(), ascii_query::Exited, ascii_query::LLDBController::initialize(), ascii_query::LLDBController::isAttached(), ascii_query::HttpServer::lastError(), ascii_query::LLDBController::lastError(), parse_port(), parse_ulong(), ascii_query::LLDBController::resume(), ascii_query::LLDBController::shutdown(), ascii_query::HttpServer::start(), ascii_query::LLDBController::state(), ascii_query::HttpServer::stop(), ascii_query::Stopped, ascii_query::LLDBController::targetName(), and ascii_query::LLDBController::targetPid().