| 1 | /* |
| 2 | * BabelTrace - Common Trace Format (CTF) |
| 3 | * |
| 4 | * CTF Text Format registration. |
| 5 | * |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | * of this software and associated documentation files (the "Software"), to deal |
| 12 | * in the Software without restriction, including without limitation the rights |
| 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | * copies of the Software, and to permit persons to whom the Software is |
| 15 | * furnished to do so, subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be included in |
| 18 | * all copies or substantial portions of the Software. |
| 19 | */ |
| 20 | |
| 21 | #include <babeltrace/format.h> |
| 22 | #include <babeltrace/ctf-text/types.h> |
| 23 | #include <babeltrace/ctf/metadata.h> |
| 24 | #include <babeltrace/babeltrace.h> |
| 25 | #include <inttypes.h> |
| 26 | #include <uuid/uuid.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <errno.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <dirent.h> |
| 33 | #include <glib.h> |
| 34 | #include <unistd.h> |
| 35 | #include <stdlib.h> |
| 36 | |
| 37 | struct trace_descriptor *ctf_text_open_trace(const char *path, int flags); |
| 38 | void ctf_text_close_trace(struct trace_descriptor *descriptor); |
| 39 | |
| 40 | static |
| 41 | rw_dispatch write_dispatch_table[] = { |
| 42 | [ CTF_TYPE_INTEGER ] = ctf_text_integer_write, |
| 43 | [ CTF_TYPE_FLOAT ] = ctf_text_float_write, |
| 44 | [ CTF_TYPE_ENUM ] = ctf_text_enum_write, |
| 45 | [ CTF_TYPE_STRING ] = ctf_text_string_write, |
| 46 | [ CTF_TYPE_STRUCT ] = ctf_text_struct_write, |
| 47 | [ CTF_TYPE_VARIANT ] = ctf_text_variant_write, |
| 48 | [ CTF_TYPE_ARRAY ] = ctf_text_array_write, |
| 49 | [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write, |
| 50 | }; |
| 51 | |
| 52 | static |
| 53 | struct format ctf_text_format = { |
| 54 | .open_trace = ctf_text_open_trace, |
| 55 | .close_trace = ctf_text_close_trace, |
| 56 | }; |
| 57 | |
| 58 | static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN, |
| 59 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END, |
| 60 | Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED, |
| 61 | Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE, |
| 62 | Q_STREAM_PACKET_CONTEXT_PACKET_SIZE; |
| 63 | |
| 64 | static |
| 65 | void __attribute__((constructor)) init_quarks(void) |
| 66 | { |
| 67 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin"); |
| 68 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end"); |
| 69 | Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded"); |
| 70 | Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size"); |
| 71 | Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size"); |
| 72 | } |
| 73 | |
| 74 | int print_field(struct definition *definition) |
| 75 | { |
| 76 | /* Print all fields in verbose mode */ |
| 77 | if (babeltrace_verbose) |
| 78 | return 1; |
| 79 | |
| 80 | /* Filter out part of the packet context */ |
| 81 | if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN) |
| 82 | return 0; |
| 83 | if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END) |
| 84 | return 0; |
| 85 | if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED) |
| 86 | return 0; |
| 87 | if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE) |
| 88 | return 0; |
| 89 | if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE) |
| 90 | return 0; |
| 91 | |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | static |
| 96 | int ctf_text_write_event(struct stream_pos *ppos, |
| 97 | struct ctf_stream *stream) |
| 98 | { |
| 99 | struct ctf_text_stream_pos *pos = |
| 100 | container_of(ppos, struct ctf_text_stream_pos, parent); |
| 101 | struct ctf_stream_class *stream_class = stream->stream_class; |
| 102 | int field_nr_saved; |
| 103 | struct ctf_event *event_class; |
| 104 | struct ctf_stream_event *event; |
| 105 | uint64_t id = 0; |
| 106 | int ret; |
| 107 | |
| 108 | /* print event header */ |
| 109 | if (stream->stream_event_header) { |
| 110 | struct definition_integer *integer_definition; |
| 111 | struct definition *variant; |
| 112 | |
| 113 | /* lookup event id */ |
| 114 | integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE); |
| 115 | if (integer_definition) { |
| 116 | id = integer_definition->value._unsigned; |
| 117 | } else { |
| 118 | struct definition_enum *enum_definition; |
| 119 | |
| 120 | enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE); |
| 121 | if (enum_definition) { |
| 122 | id = enum_definition->integer->value._unsigned; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | variant = lookup_variant(&stream->stream_event_header->p, "v"); |
| 127 | if (variant) { |
| 128 | integer_definition = lookup_integer(variant, "id", FALSE); |
| 129 | if (integer_definition) { |
| 130 | id = integer_definition->value._unsigned; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (id >= stream_class->events_by_id->len) { |
| 136 | fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id); |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | event = g_ptr_array_index(stream->events_by_id, id); |
| 140 | if (!event) { |
| 141 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); |
| 142 | return -EINVAL; |
| 143 | } |
| 144 | event_class = g_ptr_array_index(stream_class->events_by_id, id); |
| 145 | if (!event) { |
| 146 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | if (stream->has_timestamp) { |
| 151 | if (pos->print_names) |
| 152 | fprintf(pos->fp, "timestamp = "); |
| 153 | else |
| 154 | fprintf(pos->fp, "["); |
| 155 | fprintf(pos->fp, "%12" PRIu64, stream->timestamp); |
| 156 | if (!pos->print_names) |
| 157 | fprintf(pos->fp, "]"); |
| 158 | |
| 159 | if (pos->print_names) |
| 160 | fprintf(pos->fp, ", "); |
| 161 | else |
| 162 | fprintf(pos->fp, " "); |
| 163 | } |
| 164 | if (pos->print_names) |
| 165 | fprintf(pos->fp, "name = "); |
| 166 | fprintf(pos->fp, "%s", g_quark_to_string(event_class->name)); |
| 167 | if (pos->print_names) |
| 168 | pos->field_nr++; |
| 169 | else |
| 170 | fprintf(pos->fp, ":"); |
| 171 | |
| 172 | /* print cpuid field from packet context */ |
| 173 | if (stream->stream_packet_context) { |
| 174 | if (pos->field_nr++ != 0) |
| 175 | fprintf(pos->fp, ","); |
| 176 | if (pos->print_names) |
| 177 | fprintf(pos->fp, " stream.packet.context ="); |
| 178 | field_nr_saved = pos->field_nr; |
| 179 | pos->field_nr = 0; |
| 180 | ret = generic_rw(ppos, &stream->stream_packet_context->p); |
| 181 | if (ret) |
| 182 | goto error; |
| 183 | pos->field_nr = field_nr_saved; |
| 184 | } |
| 185 | |
| 186 | /* Only show the event header in verbose mode */ |
| 187 | if (babeltrace_verbose && stream->stream_event_header) { |
| 188 | if (pos->field_nr++ != 0) |
| 189 | fprintf(pos->fp, ","); |
| 190 | if (pos->print_names) |
| 191 | fprintf(pos->fp, " stream.event.header ="); |
| 192 | field_nr_saved = pos->field_nr; |
| 193 | pos->field_nr = 0; |
| 194 | ret = generic_rw(ppos, &stream->stream_event_header->p); |
| 195 | if (ret) |
| 196 | goto error; |
| 197 | pos->field_nr = field_nr_saved; |
| 198 | } |
| 199 | |
| 200 | /* print stream-declared event context */ |
| 201 | if (stream->stream_event_context) { |
| 202 | if (pos->field_nr++ != 0) |
| 203 | fprintf(pos->fp, ","); |
| 204 | if (pos->print_names) |
| 205 | fprintf(pos->fp, " stream.event.context ="); |
| 206 | field_nr_saved = pos->field_nr; |
| 207 | pos->field_nr = 0; |
| 208 | ret = generic_rw(ppos, &stream->stream_event_context->p); |
| 209 | if (ret) |
| 210 | goto error; |
| 211 | pos->field_nr = field_nr_saved; |
| 212 | } |
| 213 | |
| 214 | /* print event-declared event context */ |
| 215 | if (event->event_context) { |
| 216 | if (pos->field_nr++ != 0) |
| 217 | fprintf(pos->fp, ","); |
| 218 | if (pos->print_names) |
| 219 | fprintf(pos->fp, " event.context ="); |
| 220 | field_nr_saved = pos->field_nr; |
| 221 | pos->field_nr = 0; |
| 222 | ret = generic_rw(ppos, &event->event_context->p); |
| 223 | if (ret) |
| 224 | goto error; |
| 225 | pos->field_nr = field_nr_saved; |
| 226 | } |
| 227 | |
| 228 | /* Read and print event payload */ |
| 229 | if (event->event_fields) { |
| 230 | if (pos->field_nr++ != 0) |
| 231 | fprintf(pos->fp, ","); |
| 232 | if (pos->print_names) |
| 233 | fprintf(pos->fp, " event.fields ="); |
| 234 | field_nr_saved = pos->field_nr; |
| 235 | pos->field_nr = 0; |
| 236 | ret = generic_rw(ppos, &event->event_fields->p); |
| 237 | if (ret) |
| 238 | goto error; |
| 239 | pos->field_nr = field_nr_saved; |
| 240 | } |
| 241 | /* newline */ |
| 242 | fprintf(pos->fp, "\n"); |
| 243 | pos->field_nr = 0; |
| 244 | |
| 245 | return 0; |
| 246 | |
| 247 | error: |
| 248 | fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n"); |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | struct trace_descriptor *ctf_text_open_trace(const char *path, int flags) |
| 254 | { |
| 255 | struct ctf_text_stream_pos *pos; |
| 256 | FILE *fp; |
| 257 | |
| 258 | pos = g_new0(struct ctf_text_stream_pos, 1); |
| 259 | |
| 260 | switch (flags & O_ACCMODE) { |
| 261 | case O_RDWR: |
| 262 | if (!path) |
| 263 | fp = stdout; |
| 264 | else |
| 265 | fp = fopen(path, "w"); |
| 266 | if (!fp) |
| 267 | goto error; |
| 268 | pos->fp = fp; |
| 269 | pos->parent.rw_table = write_dispatch_table; |
| 270 | pos->parent.event_cb = ctf_text_write_event; |
| 271 | pos->print_names = opt_field_names; |
| 272 | break; |
| 273 | case O_RDONLY: |
| 274 | default: |
| 275 | fprintf(stdout, "[error] Incorrect open flags.\n"); |
| 276 | goto error; |
| 277 | } |
| 278 | |
| 279 | return &pos->trace_descriptor; |
| 280 | error: |
| 281 | g_free(pos); |
| 282 | return NULL; |
| 283 | } |
| 284 | |
| 285 | void ctf_text_close_trace(struct trace_descriptor *td) |
| 286 | { |
| 287 | struct ctf_text_stream_pos *pos = |
| 288 | container_of(td, struct ctf_text_stream_pos, trace_descriptor); |
| 289 | fclose(pos->fp); |
| 290 | g_free(pos); |
| 291 | } |
| 292 | |
| 293 | void __attribute__((constructor)) ctf_text_init(void) |
| 294 | { |
| 295 | int ret; |
| 296 | |
| 297 | ctf_text_format.name = g_quark_from_static_string("text"); |
| 298 | ret = bt_register_format(&ctf_text_format); |
| 299 | assert(!ret); |
| 300 | } |
| 301 | |
| 302 | /* TODO: finalize */ |