ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
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 408 of file main.cpp.

408 {
409 // Parse command line arguments
410 pid_t attach_pid = 0;
411 std::string attach_name;
412 bool wait_for = false;
413 bool attach_ascii_chat = false;
414 bool list_processes = false;
415 uint16_t port = 9999;
416
417 for (int i = 1; i < argc; i++) {
418 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
419 printUsage(argv[0]);
420 return 0;
421 } else if (strcmp(argv[i], "--attach") == 0 && i + 1 < argc) {
422 // Parse PID using safe parsing utility
423 unsigned long pid_val = 0;
424 if (parse_ulong(argv[++i], &pid_val, 1, ULONG_MAX) != ASCIICHAT_OK) {
425 fprintf(stderr, "Error: Invalid PID value\n");
426 return 1;
427 }
428 attach_pid = static_cast<pid_t>(pid_val);
429 } else if (strcmp(argv[i], "--attach-name") == 0 && i + 1 < argc) {
430 attach_name = argv[++i];
431 } else if (strcmp(argv[i], "--attach-ascii-chat") == 0) {
432 attach_ascii_chat = true;
433 } else if (strcmp(argv[i], "--list-processes") == 0 || strcmp(argv[i], "--list") == 0) {
434 list_processes = true;
435 } else if (strcmp(argv[i], "--wait") == 0) {
436 wait_for = true;
437 } else if (strcmp(argv[i], "--port") == 0 && i + 1 < argc) {
438 // Parse port using safe parsing utility with proper range validation
439 if (parse_port(argv[++i], &port) != ASCIICHAT_OK) {
440 fprintf(stderr, "Error: Invalid port number (must be 1-65535)\n");
441 return 1;
442 }
443 } else {
444 fprintf(stderr, "Unknown option: %s\n", argv[i]);
445 printUsage(argv[0]);
446 return 1;
447 }
448 }
449
450 if (list_processes) {
451 for (const auto &process : listAsciiChatProcesses()) {
452 fprintf(stdout, "%llu\t%s\n", static_cast<unsigned long long>(process.pid), process.name.c_str());
453 }
454 return 0;
455 }
456
457 if (attach_ascii_chat) {
458 const auto processes = listAsciiChatProcesses();
459 if (processes.empty()) {
460 fprintf(stderr, "Error: No running process with 'ascii-chat' in its name\n");
461 return 1;
462 }
463 attach_pid = processes.front().pid;
464 }
465
466 // Validate arguments
467 if (attach_pid == 0 && attach_name.empty()) {
468 fprintf(stderr, "Error: Must specify --attach <pid> or --attach-name <name>\n\n");
469 printUsage(argv[0]);
470 return 1;
471 }
472
473 // Setup signal handlers
474 signal(SIGINT, signalHandler);
475 signal(SIGTERM, signalHandler);
476#ifndef _WIN32
477 signal(SIGPIPE, SIG_IGN);
478#endif
479
480 // Initialize LLDB controller
482 g_controller = &controller;
483
484 if (!controller.initialize()) {
485 fprintf(stderr, "Error: Failed to initialize LLDB: %s\n", controller.lastError().c_str());
486 return 1;
487 }
488
489 // Attach to target
490 fprintf(stderr, "[ascii-query-server] Attaching to ");
491 if (attach_pid > 0) {
492 fprintf(stderr, "PID %llu...\n", static_cast<unsigned long long>(attach_pid));
493 if (!controller.attach(attach_pid)) {
494 fprintf(stderr, "Error: Failed to attach: %s\n", controller.lastError().c_str());
495 return 1;
496 }
497 } else {
498 fprintf(stderr, "process '%s'%s...\n", attach_name.c_str(), wait_for ? " (waiting)" : "");
499 if (!controller.attachByName(attach_name, wait_for)) {
500 fprintf(stderr, "Error: Failed to attach: %s\n", controller.lastError().c_str());
501 return 1;
502 }
503 }
504
505 fprintf(stderr, "[ascii-query-server] Attached to %s (PID %llu)\n", controller.targetName().c_str(), static_cast<unsigned long long>(controller.targetPid()));
506
507 // Resume the target (it's stopped after attach)
508 if (controller.state() == ascii_query::ProcessState::Stopped) {
509 fprintf(stderr, "[ascii-query-server] Resuming target...\n");
510 controller.resume();
511 }
512
513 // Setup HTTP server
515 g_server = &server;
516
517 setupRoutes(server, controller);
518
519 // Start HTTP server
520 if (!server.start(port)) {
521 fprintf(stderr, "Error: Failed to start HTTP server: %s\n", server.lastError().c_str());
522 controller.detach();
523 return 1;
524 }
525
526 fprintf(stderr, "[ascii-query-server] HTTP server listening on http://localhost:%d\n", port);
527 fprintf(stderr, "[ascii-query-server] Press Ctrl+C to stop\n");
528
529 // Wait for shutdown
530 while (!g_shutdown_requested && controller.isAttached()) {
531#ifdef _WIN32
532 Sleep(100);
533#else
534 usleep(100000);
535#endif
536 ascii_query::ProcessState state = controller.state();
538 fprintf(stderr, "[ascii-query-server] Target %s, shutting down\n", state == ascii_query::ProcessState::Exited ? "exited" : "crashed");
539 break;
540 }
541 }
542
543 // Cleanup
544 fprintf(stderr, "[ascii-query-server] Shutting down...\n");
545 server.stop();
546 controller.detach();
547 controller.shutdown();
548
549 fprintf(stderr, "[ascii-query-server] Done\n");
550 return 0;
551}
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
@ ASCIICHAT_OK
Definition error_codes.h:50
ProcessState
This is the handoff point for ThreadInfo. Callers fill its members before passing it on; consumers re...
@ 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.
asciichat_error_t parse_port(const char *str, uint16_t *out_port)
Parse port number (1-65535) from string.

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().