| 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-internal.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 (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, |
| 39 | int whence), FILE *metadata_fp); |
| 40 | void ctf_text_close_trace(struct trace_descriptor *descriptor); |
| 41 | |
| 42 | static |
| 43 | rw_dispatch write_dispatch_table[] = { |
| 44 | [ CTF_TYPE_INTEGER ] = ctf_text_integer_write, |
| 45 | [ CTF_TYPE_FLOAT ] = ctf_text_float_write, |
| 46 | [ CTF_TYPE_ENUM ] = ctf_text_enum_write, |
| 47 | [ CTF_TYPE_STRING ] = ctf_text_string_write, |
| 48 | [ CTF_TYPE_STRUCT ] = ctf_text_struct_write, |
| 49 | [ CTF_TYPE_VARIANT ] = ctf_text_variant_write, |
| 50 | [ CTF_TYPE_ARRAY ] = ctf_text_array_write, |
| 51 | [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write, |
| 52 | }; |
| 53 | |
| 54 | static |
| 55 | struct format ctf_text_format = { |
| 56 | .open_trace = ctf_text_open_trace, |
| 57 | .close_trace = ctf_text_close_trace, |
| 58 | }; |
| 59 | |
| 60 | static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN, |
| 61 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END, |
| 62 | Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED, |
| 63 | Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE, |
| 64 | Q_STREAM_PACKET_CONTEXT_PACKET_SIZE; |
| 65 | |
| 66 | static |
| 67 | void __attribute__((constructor)) init_quarks(void) |
| 68 | { |
| 69 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin"); |
| 70 | Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end"); |
| 71 | Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded"); |
| 72 | Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size"); |
| 73 | Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size"); |
| 74 | } |
| 75 | |
| 76 | int print_field(struct definition *definition) |
| 77 | { |
| 78 | /* Print all fields in verbose mode */ |
| 79 | if (babeltrace_verbose) |
| 80 | return 1; |
| 81 | |
| 82 | /* Filter out part of the packet context */ |
| 83 | if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN) |
| 84 | return 0; |
| 85 | if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END) |
| 86 | return 0; |
| 87 | if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED) |
| 88 | return 0; |
| 89 | if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE) |
| 90 | return 0; |
| 91 | if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE) |
| 92 | return 0; |
| 93 | |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | static |
| 98 | int ctf_text_write_event(struct stream_pos *ppos, |
| 99 | struct ctf_stream *stream) |
| 100 | { |
| 101 | struct ctf_text_stream_pos *pos = |
| 102 | container_of(ppos, struct ctf_text_stream_pos, parent); |
| 103 | struct ctf_stream_class *stream_class = stream->stream_class; |
| 104 | int field_nr_saved; |
| 105 | struct ctf_event *event_class; |
| 106 | struct ctf_stream_event *event; |
| 107 | uint64_t id; |
| 108 | int ret; |
| 109 | |
| 110 | id = stream->event_id; |
| 111 | |
| 112 | if (id >= stream_class->events_by_id->len) { |
| 113 | fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id); |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | event = g_ptr_array_index(stream->events_by_id, id); |
| 117 | if (!event) { |
| 118 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | event_class = g_ptr_array_index(stream_class->events_by_id, id); |
| 122 | if (!event) { |
| 123 | fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | if (stream->has_timestamp) { |
| 128 | if (pos->print_names) |
| 129 | fprintf(pos->fp, "timestamp = "); |
| 130 | else |
| 131 | fprintf(pos->fp, "["); |
| 132 | fprintf(pos->fp, "%12" PRIu64, stream->timestamp); |
| 133 | if (!pos->print_names) |
| 134 | fprintf(pos->fp, "]"); |
| 135 | |
| 136 | if (pos->print_names) |
| 137 | fprintf(pos->fp, ", "); |
| 138 | else |
| 139 | fprintf(pos->fp, " "); |
| 140 | } |
| 141 | if (pos->print_names) |
| 142 | fprintf(pos->fp, "name = "); |
| 143 | fprintf(pos->fp, "%s", g_quark_to_string(event_class->name)); |
| 144 | if (pos->print_names) |
| 145 | pos->field_nr++; |
| 146 | else |
| 147 | fprintf(pos->fp, ":"); |
| 148 | |
| 149 | /* print cpuid field from packet context */ |
| 150 | if (stream->stream_packet_context) { |
| 151 | if (pos->field_nr++ != 0) |
| 152 | fprintf(pos->fp, ","); |
| 153 | if (pos->print_names) |
| 154 | fprintf(pos->fp, " stream.packet.context ="); |
| 155 | field_nr_saved = pos->field_nr; |
| 156 | pos->field_nr = 0; |
| 157 | ret = generic_rw(ppos, &stream->stream_packet_context->p); |
| 158 | if (ret) |
| 159 | goto error; |
| 160 | pos->field_nr = field_nr_saved; |
| 161 | } |
| 162 | |
| 163 | /* Only show the event header in verbose mode */ |
| 164 | if (babeltrace_verbose && stream->stream_event_header) { |
| 165 | if (pos->field_nr++ != 0) |
| 166 | fprintf(pos->fp, ","); |
| 167 | if (pos->print_names) |
| 168 | fprintf(pos->fp, " stream.event.header ="); |
| 169 | field_nr_saved = pos->field_nr; |
| 170 | pos->field_nr = 0; |
| 171 | ret = generic_rw(ppos, &stream->stream_event_header->p); |
| 172 | if (ret) |
| 173 | goto error; |
| 174 | pos->field_nr = field_nr_saved; |
| 175 | } |
| 176 | |
| 177 | /* print stream-declared event context */ |
| 178 | if (stream->stream_event_context) { |
| 179 | if (pos->field_nr++ != 0) |
| 180 | fprintf(pos->fp, ","); |
| 181 | if (pos->print_names) |
| 182 | fprintf(pos->fp, " stream.event.context ="); |
| 183 | field_nr_saved = pos->field_nr; |
| 184 | pos->field_nr = 0; |
| 185 | ret = generic_rw(ppos, &stream->stream_event_context->p); |
| 186 | if (ret) |
| 187 | goto error; |
| 188 | pos->field_nr = field_nr_saved; |
| 189 | } |
| 190 | |
| 191 | /* print event-declared event context */ |
| 192 | if (event->event_context) { |
| 193 | if (pos->field_nr++ != 0) |
| 194 | fprintf(pos->fp, ","); |
| 195 | if (pos->print_names) |
| 196 | fprintf(pos->fp, " event.context ="); |
| 197 | field_nr_saved = pos->field_nr; |
| 198 | pos->field_nr = 0; |
| 199 | ret = generic_rw(ppos, &event->event_context->p); |
| 200 | if (ret) |
| 201 | goto error; |
| 202 | pos->field_nr = field_nr_saved; |
| 203 | } |
| 204 | |
| 205 | /* Read and print event payload */ |
| 206 | if (event->event_fields) { |
| 207 | if (pos->field_nr++ != 0) |
| 208 | fprintf(pos->fp, ","); |
| 209 | if (pos->print_names) |
| 210 | fprintf(pos->fp, " event.fields ="); |
| 211 | field_nr_saved = pos->field_nr; |
| 212 | pos->field_nr = 0; |
| 213 | ret = generic_rw(ppos, &event->event_fields->p); |
| 214 | if (ret) |
| 215 | goto error; |
| 216 | pos->field_nr = field_nr_saved; |
| 217 | } |
| 218 | /* newline */ |
| 219 | fprintf(pos->fp, "\n"); |
| 220 | pos->field_nr = 0; |
| 221 | |
| 222 | return 0; |
| 223 | |
| 224 | error: |
| 225 | fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n"); |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | |
| 230 | struct trace_descriptor *ctf_text_open_trace(const char *path, int flags, |
| 231 | void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset, |
| 232 | int whence), FILE *metadata_fp) |
| 233 | { |
| 234 | struct ctf_text_stream_pos *pos; |
| 235 | FILE *fp; |
| 236 | |
| 237 | pos = g_new0(struct ctf_text_stream_pos, 1); |
| 238 | |
| 239 | switch (flags & O_ACCMODE) { |
| 240 | case O_RDWR: |
| 241 | if (!path) |
| 242 | fp = stdout; |
| 243 | else |
| 244 | fp = fopen(path, "w"); |
| 245 | if (!fp) |
| 246 | goto error; |
| 247 | pos->fp = fp; |
| 248 | pos->parent.rw_table = write_dispatch_table; |
| 249 | pos->parent.event_cb = ctf_text_write_event; |
| 250 | pos->print_names = opt_field_names; |
| 251 | break; |
| 252 | case O_RDONLY: |
| 253 | default: |
| 254 | fprintf(stdout, "[error] Incorrect open flags.\n"); |
| 255 | goto error; |
| 256 | } |
| 257 | |
| 258 | return &pos->trace_descriptor; |
| 259 | error: |
| 260 | g_free(pos); |
| 261 | return NULL; |
| 262 | } |
| 263 | |
| 264 | void ctf_text_close_trace(struct trace_descriptor *td) |
| 265 | { |
| 266 | struct ctf_text_stream_pos *pos = |
| 267 | container_of(td, struct ctf_text_stream_pos, trace_descriptor); |
| 268 | fclose(pos->fp); |
| 269 | g_free(pos); |
| 270 | } |
| 271 | |
| 272 | void __attribute__((constructor)) ctf_text_init(void) |
| 273 | { |
| 274 | int ret; |
| 275 | |
| 276 | ctf_text_format.name = g_quark_from_static_string("text"); |
| 277 | ret = bt_register_format(&ctf_text_format); |
| 278 | assert(!ret); |
| 279 | } |
| 280 | |
| 281 | /* TODO: finalize */ |