update gitignore
[babeltrace.git] / converter / babeltrace-lib.c
CommitLineData
46322b33
MD
1/*
2 * babeltrace-lib.c
3 *
4 * Babeltrace Trace Converter Library
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19#include <stdlib.h>
20#include <errno.h>
21#include <stdio.h>
22#include <babeltrace/babeltrace.h>
23#include <babeltrace/format.h>
24#include <babeltrace/ctf/types.h>
25#include <babeltrace/ctf/metadata.h>
26#include <babeltrace/ctf-text/types.h>
27
28static
29int print_event(struct format *fmt_write, struct ctf_text_stream_pos *sout,
30 struct format *fmt_read, struct ctf_file_stream *sin)
31{
32 struct ctf_stream *stream_class = sin->stream;
33 struct ctf_event *event_class;
34 uint64_t id = 0;
35 int len_index;
36
37 /* Read and print event header */
38 if (stream_class->event_header) {
39 fmt_read->struct_read(&sin->pos.parent, stream_class->event_header);
40
41 /* lookup event id */
42 len_index = struct_declaration_lookup_field_index(td->packet_header->declaration,
43 g_quark_from_static_string("id"));
44 if (len_index >= 0) {
45 struct definition_integer *defint;
46 struct field *field;
47
48 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
49 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
50 defint = container_of(field->definition, struct definition_integer, p);
51 assert(defint->declaration->signedness == FALSE);
52 id = defint->value._unsigned; /* set id */
53 }
54
55 fmt_write->struct_write(&sout->parent, stream_class->event_header);
56 }
57
58 /* Read and print stream-declared event context */
59 if (stream_class->event_context) {
60 fmt_read->struct_read(&sin->pos.parent, stream_class->event_context);
61 fmt_write->struct_write(&sout->parent, stream_class->event_context);
62 }
63
64 if (id >= stream_class->events_by_id->len) {
65 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
66 return -EINVAL;
67 }
68 event_class = g_ptr_array_index(stream_class->events_by_id, id);
69 if (!event_class) {
70 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
71 return -EINVAL;
72 }
73
74 /* Read and print event-declared event context */
75 if (event_class->context) {
76 fmt_read->struct_read(&sin->pos.parent, event_class->context);
77 fmt_write->struct_write(&sout->parent, event_class->context);
78 }
79
80 /* Read and print event payload */
81 if (event_class->fields) {
82 fmt_read->struct_read(&sin->pos.parent, event_class->fields);
83 fmt_write->struct_write(&sout->parent, event_class->fields);
84 }
85
86 return 0;
87}
88
89static
90int print_stream(struct format *fmt_write, struct ctf_text_stream_pos *sout,
91 struct format *fmt_read, struct ctf_file_stream *sin)
92{
93 int ret;
94
95 /* For each event, print header, context, payload */
96 /* TODO: order events by timestamps across streams */
97 for (;;) {
98 ret = print_event(fmt_write, sout, fmt_read, sin);
99 if (ret == -EOF)
100 break;
101 else if (ret) {
102 fprintf(stdout, "[error] Printing event failed.\n");
103 goto error;
104 }
105 }
106
107 return 0;
108
109error:
110 return ret;
111}
112
113int print_trace(struct format *fmt_write,
114 struct trace_descriptor *td_write,
115 struct format *fmt_read,
116 struct trace_descriptor *td_read)
117{
118 struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent);
119 struct ctf_text_stream_pos *sout = container_of(td_write, struct ctf_text_stream_pos, parent);
120 int stream_id, filenr;
121 int ret;
122
123 /* For each stream (TODO: order events by timestamp) */
124 for (stream_id = 0; stream_id < tin->streams->len; stream_id++) {
125 struct ctf_stream *stream = g_ptr_array_index(tin->streams, stream_id);
126
127 if (!stream)
128 continue;
129 for (filenr = 0; filenr < stream->files->len; filenr++) {
130 struct ctf_file_stream *file_stream = g_ptr_array_index(stream->files, filenr);
131 ret = print_stream(fmt_write, sout, fmt_read, file_stream);
132 if (ret) {
133 fprintf(stdout, "[error] Printing stream %d failed.\n", stream_id);
134 goto error;
135 }
136 }
137 }
138
139 return 0;
140
141error:
142 return ret;
143}
This page took 0.02716 seconds and 4 git commands to generate.