/*
 * trace_parser.h - Parse QEMU simpletrace .bin files.
 *
 * Provides structured access to trace events, summary statistics, and
 * formatted output (text / JSON / CSV).
 */

#ifndef TRACE_PARSER_H
#define TRACE_PARSER_H

#include <stdint.h>
#include <stdio.h>
#include <stddef.h>

/* Maximum number of distinct event IDs we track (QEMU uses sequential IDs). */
#define TRACE_MAX_EVENT_ID 256

/* A single parsed trace event (node in a singly-linked list). */
typedef struct trace_event {
    uint64_t event_id;
    uint64_t timestamp_ns;
    uint64_t pid;
    uint32_t args_len;
    uint8_t *args;                 /* raw argument bytes (malloc'd, may be NULL) */
    char     name[64];             /* resolved name, or "event_<id>" fallback */
    struct trace_event *next;
} trace_event_t;

/* Aggregate statistics over all parsed events. */
typedef struct trace_summary {
    uint64_t total_events;
    uint64_t events_by_id[TRACE_MAX_EVENT_ID];   /* count per event_id */
    char     event_names[TRACE_MAX_EVENT_ID][64]; /* name mapping */
    uint64_t first_timestamp;
    uint64_t last_timestamp;
    uint64_t total_disk_reads;
    uint64_t total_disk_writes;
    uint64_t total_serial_writes;
    trace_event_t *events;         /* linked-list head (all parsed events) */
    size_t   event_count;          /* number of nodes in the list */
} trace_summary_t;

/*
 * Parse a QEMU simpletrace binary file.
 * Returns 0 on success (even if the file contained 0 events), -1 on error
 * (file not found, bad magic, read failure).
 * Caller must call trace_free_summary() to release memory.
 */
int  trace_parse_file(const char *path, trace_summary_t *summary);

/* Free all dynamically-allocated memory in *summary. */
void trace_free_summary(trace_summary_t *summary);

/* Output helpers — write formatted summaries to *out. */
void trace_print_text(const trace_summary_t *summary, FILE *out);
void trace_print_json(const trace_summary_t *summary, FILE *out);
void trace_print_csv(const trace_summary_t *summary, FILE *out);

#endif /* TRACE_PARSER_H */
