X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=formats%2Fctf-text%2Fctf-text.c;h=2ec71c56765b872a30c2ab040f865356227d75a6;hp=8d84c37f932163462659d32633905f1e590b5d9d;hb=aa6bffaea450106c60fc1292152bd94a270fd243;hpb=b61922b5e7b4d7ec5ed960c3d842886b5a97f549 diff --git a/formats/ctf-text/ctf-text.c b/formats/ctf-text/ctf-text.c index 8d84c37f..2ec71c56 100644 --- a/formats/ctf-text/ctf-text.c +++ b/formats/ctf-text/ctf-text.c @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -34,11 +35,6 @@ struct trace_descriptor *ctf_text_open_trace(const char *path, int flags); void ctf_text_close_trace(struct trace_descriptor *descriptor); -static -rw_dispatch read_dispatch_table[] = { - /* All unimplemented */ -}; - static rw_dispatch write_dispatch_table[] = { [ CTF_TYPE_INTEGER ] = ctf_text_integer_write, @@ -57,6 +53,116 @@ struct format ctf_text_format = { .close_trace = ctf_text_close_trace, }; +static +int ctf_text_write_event(struct stream_pos *ppos, + struct ctf_stream_class *stream_class) +{ + struct ctf_text_stream_pos *pos = + container_of(ppos, struct ctf_text_stream_pos, parent); + struct ctf_event *event_class; + uint64_t id = 0; + int len_index; + int ret; + int field_nr = 0; + + /* print event header */ + if (stream_class->event_header) { + /* lookup event id */ + len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl, + g_quark_from_static_string("id")); + if (len_index >= 0) { + struct definition_integer *defint; + struct definition *field; + + field = struct_definition_get_field_from_index(stream_class->event_header, len_index); + assert(field->declaration->id == CTF_TYPE_INTEGER); + defint = container_of(field, struct definition_integer, p); + assert(defint->declaration->signedness == FALSE); + id = defint->value._unsigned; /* set id */ + } + } + + if (id >= stream_class->events_by_id->len) { + fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id); + return -EINVAL; + } + event_class = g_ptr_array_index(stream_class->events_by_id, id); + if (!event_class) { + fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); + return -EINVAL; + } + + if (field_nr++ != 0) + fprintf(pos->fp, ", "); + if (pos->print_names) + fprintf(pos->fp, "timestamp = "); + else + fprintf(pos->fp, "["); + fprintf(pos->fp, "%" PRIu64, (uint64_t) 0); /* TODO */ + if (!pos->print_names) + fprintf(pos->fp, "]"); + + if (field_nr++ != 0) + fprintf(pos->fp, ", "); + if (pos->print_names) + fprintf(pos->fp, "name = "); + fprintf(pos->fp, "%s: ", g_quark_to_string(event_class->name)); + + if (stream_class->event_header) { + if (field_nr++ != 0) + fprintf(pos->fp, ", "); + if (pos->print_names) + fprintf(pos->fp, "stream.event.header = "); + ret = generic_rw(ppos, &stream_class->event_header->p); + if (ret) + goto error; + } + + /* print stream-declared event context */ + if (stream_class->event_context) { + if (field_nr++ != 0) + fprintf(pos->fp, ", "); + if (pos->print_names) + fprintf(pos->fp, "stream.event.context = "); + fprintf(pos->fp, " "); + ret = generic_rw(ppos, &stream_class->event_context->p); + if (ret) + goto error; + } + + /* print event-declared event context */ + if (event_class->context) { + if (field_nr++ != 0) + fprintf(pos->fp, ", "); + if (pos->print_names) + fprintf(pos->fp, "event.context = "); + fprintf(pos->fp, " "); + ret = generic_rw(ppos, &event_class->context->p); + if (ret) + goto error; + } + + /* Read and print event payload */ + if (event_class->fields) { + if (field_nr++ != 0) + fprintf(pos->fp, ", "); + if (pos->print_names) + fprintf(pos->fp, "event.fields = "); + fprintf(pos->fp, " "); + ret = generic_rw(ppos, &event_class->fields->p); + if (ret) + goto error; + } + fprintf(pos->fp, "\n"); + + return 0; + +error: + fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n"); + return ret; +} + + struct trace_descriptor *ctf_text_open_trace(const char *path, int flags) { struct ctf_text_stream_pos *pos; @@ -65,14 +171,17 @@ struct trace_descriptor *ctf_text_open_trace(const char *path, int flags) pos = g_new0(struct ctf_text_stream_pos, 1); switch (flags & O_ACCMODE) { - case O_WRONLY: + case O_RDWR: if (!path) - path = "/dev/stdout"; - fp = fopen(path, "w"); + fp = stdout; + else + fp = fopen(path, "w"); if (!fp) goto error; pos->fp = fp; pos->parent.rw_table = write_dispatch_table; + pos->parent.event_cb = ctf_text_write_event; + pos->print_names = opt_field_names; break; case O_RDONLY: default: