lib: make graph API const-correct
[babeltrace.git] / plugins / text / pretty / pretty.c
1 /*
2 * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 * Copyright 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <babeltrace/babeltrace.h>
27 #include <babeltrace/compiler-internal.h>
28 #include <babeltrace/common-internal.h>
29 #include <plugins-common.h>
30 #include <stdio.h>
31 #include <stdbool.h>
32 #include <glib.h>
33 #include <babeltrace/assert-internal.h>
34
35 #include "pretty.h"
36
37 GQuark stream_packet_context_quarks[STREAM_PACKET_CONTEXT_QUARKS_LEN];
38
39 static
40 const char *plugin_options[] = {
41 "color",
42 "path",
43 "no-delta",
44 "clock-cycles",
45 "clock-seconds",
46 "clock-date",
47 "clock-gmt",
48 "verbose",
49 "name-default", /* show/hide */
50 "name-payload",
51 "name-context",
52 "name-scope",
53 "name-header",
54 "field-default", /* show/hide */
55 "field-trace",
56 "field-trace:hostname",
57 "field-trace:domain",
58 "field-trace:procname",
59 "field-trace:vpid",
60 "field-loglevel",
61 "field-emf",
62 "field-callsite",
63 };
64
65 static
66 void destroy_pretty_data(struct pretty_component *pretty)
67 {
68 bt_object_put_ref(pretty->iterator);
69
70 if (pretty->string) {
71 (void) g_string_free(pretty->string, TRUE);
72 }
73
74 if (pretty->tmp_string) {
75 (void) g_string_free(pretty->tmp_string, TRUE);
76 }
77
78 if (pretty->out != stdout) {
79 int ret;
80
81 ret = fclose(pretty->out);
82 if (ret) {
83 perror("close output file");
84 }
85 }
86 g_free(pretty->options.output_path);
87 g_free(pretty);
88 }
89
90 static
91 struct pretty_component *create_pretty(void)
92 {
93 struct pretty_component *pretty;
94
95 pretty = g_new0(struct pretty_component, 1);
96 if (!pretty) {
97 goto end;
98 }
99 pretty->string = g_string_new("");
100 if (!pretty->string) {
101 goto error;
102 }
103 pretty->tmp_string = g_string_new("");
104 if (!pretty->tmp_string) {
105 goto error;
106 }
107 end:
108 return pretty;
109
110 error:
111 g_free(pretty);
112 return NULL;
113 }
114
115 BT_HIDDEN
116 void pretty_finalize(struct bt_self_component_sink *comp)
117 {
118 destroy_pretty_data(
119 bt_self_component_get_data(
120 bt_self_component_sink_as_self_component(comp)));
121 }
122
123 static
124 enum bt_self_component_status handle_notification(
125 struct pretty_component *pretty,
126 const struct bt_notification *notification)
127 {
128 enum bt_self_component_status ret = BT_SELF_COMPONENT_STATUS_OK;
129
130 BT_ASSERT(pretty);
131
132 switch (bt_notification_get_type(notification)) {
133 case BT_NOTIFICATION_TYPE_PACKET_BEGIN:
134 if (pretty_print_packet(pretty, notification)) {
135 ret = BT_SELF_COMPONENT_STATUS_ERROR;
136 }
137 break;
138 case BT_NOTIFICATION_TYPE_EVENT:
139 if (pretty_print_event(pretty, notification)) {
140 ret = BT_SELF_COMPONENT_STATUS_ERROR;
141 }
142 break;
143 case BT_NOTIFICATION_TYPE_INACTIVITY:
144 fprintf(stderr, "Inactivity notification\n");
145 break;
146 default:
147 break;
148 }
149
150 return ret;
151 }
152
153 BT_HIDDEN
154 enum bt_self_component_status pretty_port_connected(
155 struct bt_self_component_sink *comp,
156 struct bt_self_component_port_input *self_port,
157 const struct bt_port_output *other_port)
158 {
159 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
160 struct pretty_component *pretty;
161
162 pretty = bt_self_component_get_data(
163 bt_self_component_sink_as_self_component(comp));
164 BT_ASSERT(pretty);
165 BT_ASSERT(!pretty->iterator);
166 pretty->iterator = bt_self_component_port_input_notification_iterator_create(
167 self_port);
168 if (!pretty->iterator) {
169 status = BT_SELF_COMPONENT_STATUS_NOMEM;
170 }
171
172 return status;
173 }
174
175 BT_HIDDEN
176 enum bt_self_component_status pretty_consume(
177 struct bt_self_component_sink *comp)
178 {
179 enum bt_self_component_status ret;
180 bt_notification_array_const notifs;
181 struct bt_self_component_port_input_notification_iterator *it;
182 struct pretty_component *pretty = bt_self_component_get_data(
183 bt_self_component_sink_as_self_component(comp));
184 enum bt_notification_iterator_status it_ret;
185 uint64_t count = 0;
186 uint64_t i = 0;
187
188 it = pretty->iterator;
189 it_ret = bt_self_component_port_input_notification_iterator_next(it,
190 &notifs, &count);
191
192 switch (it_ret) {
193 case BT_NOTIFICATION_ITERATOR_STATUS_OK:
194 break;
195 case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM:
196 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
197 goto end;
198 case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN:
199 ret = BT_SELF_COMPONENT_STATUS_AGAIN;
200 goto end;
201 case BT_NOTIFICATION_ITERATOR_STATUS_END:
202 ret = BT_SELF_COMPONENT_STATUS_END;
203 BT_OBJECT_PUT_REF_AND_RESET(pretty->iterator);
204 goto end;
205 default:
206 ret = BT_SELF_COMPONENT_STATUS_ERROR;
207 goto end;
208 }
209
210 BT_ASSERT(it_ret == BT_NOTIFICATION_ITERATOR_STATUS_OK);
211
212 for (i = 0; i < count; i++) {
213 ret = handle_notification(pretty, notifs[i]);
214 if (ret) {
215 goto end;
216 }
217
218 bt_object_put_ref(notifs[i]);
219 }
220
221 end:
222 for (; i < count; i++) {
223 bt_object_put_ref(notifs[i]);
224 }
225
226 return ret;
227 }
228
229 static
230 int add_params_to_map(struct bt_value *plugin_opt_map)
231 {
232 int ret = 0;
233 unsigned int i;
234
235 for (i = 0; i < BT_ARRAY_SIZE(plugin_options); i++) {
236 const char *key = plugin_options[i];
237 enum bt_value_status status;
238
239 status = bt_value_map_insert_entry(plugin_opt_map, key,
240 bt_value_null);
241 switch (status) {
242 case BT_VALUE_STATUS_OK:
243 break;
244 default:
245 ret = -1;
246 goto end;
247 }
248 }
249 end:
250 return ret;
251 }
252
253 static
254 bt_bool check_param_exists(const char *key, const struct bt_value *object,
255 void *data)
256 {
257 struct pretty_component *pretty = data;
258
259 if (!bt_value_map_has_entry(pretty->plugin_opt_map,
260 key)) {
261 fprintf(pretty->err,
262 "[warning] Parameter \"%s\" unknown to \"text.pretty\" sink component\n", key);
263 }
264 return BT_TRUE;
265 }
266
267 static
268 void apply_one_string(const char *key, const struct bt_value *params, char **option)
269 {
270 const struct bt_value *value = NULL;
271 const char *str;
272
273 value = bt_value_map_borrow_entry_value_const(params, key);
274 if (!value) {
275 goto end;
276 }
277 if (bt_value_is_null(value)) {
278 goto end;
279 }
280 str = bt_value_string_get(value);
281 *option = g_strdup(str);
282
283 end:
284 return;
285 }
286
287 static
288 void apply_one_bool(const char *key, const struct bt_value *params, bool *option,
289 bool *found)
290 {
291 const struct bt_value *value = NULL;
292 bt_bool bool_val;
293
294 value = bt_value_map_borrow_entry_value_const(params, key);
295 if (!value) {
296 goto end;
297 }
298 bool_val = bt_value_bool_get(value);
299 *option = (bool) bool_val;
300 if (found) {
301 *found = true;
302 }
303
304 end:
305 return;
306 }
307
308 static
309 void warn_wrong_color_param(struct pretty_component *pretty)
310 {
311 fprintf(pretty->err,
312 "[warning] Accepted values for the \"color\" parameter are:\n \"always\", \"auto\", \"never\"\n");
313 }
314
315 static
316 int open_output_file(struct pretty_component *pretty)
317 {
318 int ret = 0;
319
320 if (!pretty->options.output_path) {
321 goto end;
322 }
323
324 pretty->out = fopen(pretty->options.output_path, "w");
325 if (!pretty->out) {
326 goto error;
327 }
328
329 goto end;
330
331 error:
332 ret = -1;
333
334 end:
335 return ret;
336 }
337
338 static
339 int apply_params(struct pretty_component *pretty, const struct bt_value *params)
340 {
341 int ret = 0;
342 enum bt_value_status status;
343 bool value, found;
344 char *str = NULL;
345
346 pretty->plugin_opt_map = bt_value_map_create();
347 if (!pretty->plugin_opt_map) {
348 ret = -1;
349 goto end;
350 }
351 ret = add_params_to_map(pretty->plugin_opt_map);
352 if (ret) {
353 goto end;
354 }
355 /* Report unknown parameters. */
356 status = bt_value_map_foreach_entry_const(params,
357 check_param_exists, pretty);
358 switch (status) {
359 case BT_VALUE_STATUS_OK:
360 break;
361 default:
362 ret = -1;
363 goto end;
364 }
365 /* Known parameters. */
366 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
367 if (bt_value_map_has_entry(params, "color")) {
368 const struct bt_value *color_value;
369 const char *color;
370
371 color_value = bt_value_map_borrow_entry_value_const(params,
372 "color");
373 if (!color_value) {
374 goto end;
375 }
376
377 color = bt_value_string_get(color_value);
378
379 if (strcmp(color, "never") == 0) {
380 pretty->options.color = PRETTY_COLOR_OPT_NEVER;
381 } else if (strcmp(color, "auto") == 0) {
382 pretty->options.color = PRETTY_COLOR_OPT_AUTO;
383 } else if (strcmp(color, "always") == 0) {
384 pretty->options.color = PRETTY_COLOR_OPT_ALWAYS;
385 } else {
386 warn_wrong_color_param(pretty);
387 }
388 }
389
390 apply_one_string("path", params, &pretty->options.output_path);
391 ret = open_output_file(pretty);
392 if (ret) {
393 goto end;
394 }
395
396 value = false; /* Default. */
397 apply_one_bool("no-delta", params, &value, NULL);
398 pretty->options.print_delta_field = !value; /* Reverse logic. */
399
400 value = false; /* Default. */
401 apply_one_bool("clock-cycles", params, &value, NULL);
402 pretty->options.print_timestamp_cycles = value;
403
404 value = false; /* Default. */
405 apply_one_bool("clock-seconds", params, &value, NULL);
406 pretty->options.clock_seconds = value;
407
408 value = false; /* Default. */
409 apply_one_bool("clock-date", params, &value, NULL);
410 pretty->options.clock_date = value;
411
412 value = false; /* Default. */
413 apply_one_bool("clock-gmt", params, &value, NULL);
414 pretty->options.clock_gmt = value;
415
416 value = false; /* Default. */
417 apply_one_bool("verbose", params, &value, NULL);
418 pretty->options.verbose = value;
419
420 /* Names. */
421 apply_one_string("name-default", params, &str);
422 if (!str) {
423 pretty->options.name_default = PRETTY_DEFAULT_UNSET;
424 } else if (!strcmp(str, "show")) {
425 pretty->options.name_default = PRETTY_DEFAULT_SHOW;
426 } else if (!strcmp(str, "hide")) {
427 pretty->options.name_default = PRETTY_DEFAULT_HIDE;
428 } else {
429 ret = -1;
430 goto end;
431 }
432 g_free(str);
433 str = NULL;
434
435 switch (pretty->options.name_default) {
436 case PRETTY_DEFAULT_UNSET:
437 pretty->options.print_payload_field_names = true;
438 pretty->options.print_context_field_names = true;
439 pretty->options.print_header_field_names = false;
440 pretty->options.print_scope_field_names = false;
441 break;
442 case PRETTY_DEFAULT_SHOW:
443 pretty->options.print_payload_field_names = true;
444 pretty->options.print_context_field_names = true;
445 pretty->options.print_header_field_names = true;
446 pretty->options.print_scope_field_names = true;
447 break;
448 case PRETTY_DEFAULT_HIDE:
449 pretty->options.print_payload_field_names = false;
450 pretty->options.print_context_field_names = false;
451 pretty->options.print_header_field_names = false;
452 pretty->options.print_scope_field_names = false;
453 break;
454 default:
455 ret = -1;
456 goto end;
457 }
458
459 value = false;
460 found = false;
461 apply_one_bool("name-payload", params, &value, &found);
462 if (found) {
463 pretty->options.print_payload_field_names = value;
464 }
465
466 value = false;
467 found = false;
468 apply_one_bool("name-context", params, &value, &found);
469 if (found) {
470 pretty->options.print_context_field_names = value;
471 }
472
473 value = false;
474 found = false;
475 apply_one_bool("name-header", params, &value, &found);
476 if (found) {
477 pretty->options.print_header_field_names = value;
478 }
479
480 value = false;
481 found = false;
482 apply_one_bool("name-scope", params, &value, &found);
483 if (found) {
484 pretty->options.print_scope_field_names = value;
485 }
486
487 /* Fields. */
488 apply_one_string("field-default", params, &str);
489 if (!str) {
490 pretty->options.field_default = PRETTY_DEFAULT_UNSET;
491 } else if (!strcmp(str, "show")) {
492 pretty->options.field_default = PRETTY_DEFAULT_SHOW;
493 } else if (!strcmp(str, "hide")) {
494 pretty->options.field_default = PRETTY_DEFAULT_HIDE;
495 } else {
496 ret = -1;
497 goto end;
498 }
499 g_free(str);
500 str = NULL;
501
502 switch (pretty->options.field_default) {
503 case PRETTY_DEFAULT_UNSET:
504 pretty->options.print_trace_field = false;
505 pretty->options.print_trace_hostname_field = true;
506 pretty->options.print_trace_domain_field = false;
507 pretty->options.print_trace_procname_field = true;
508 pretty->options.print_trace_vpid_field = true;
509 pretty->options.print_loglevel_field = false;
510 pretty->options.print_emf_field = false;
511 pretty->options.print_callsite_field = false;
512 break;
513 case PRETTY_DEFAULT_SHOW:
514 pretty->options.print_trace_field = true;
515 pretty->options.print_trace_hostname_field = true;
516 pretty->options.print_trace_domain_field = true;
517 pretty->options.print_trace_procname_field = true;
518 pretty->options.print_trace_vpid_field = true;
519 pretty->options.print_loglevel_field = true;
520 pretty->options.print_emf_field = true;
521 pretty->options.print_callsite_field = true;
522 break;
523 case PRETTY_DEFAULT_HIDE:
524 pretty->options.print_trace_field = false;
525 pretty->options.print_trace_hostname_field = false;
526 pretty->options.print_trace_domain_field = false;
527 pretty->options.print_trace_procname_field = false;
528 pretty->options.print_trace_vpid_field = false;
529 pretty->options.print_loglevel_field = false;
530 pretty->options.print_emf_field = false;
531 pretty->options.print_callsite_field = false;
532 break;
533 default:
534 ret = -1;
535 goto end;
536 }
537
538 value = false;
539 found = false;
540 apply_one_bool("field-trace", params, &value, &found);
541 if (found) {
542 pretty->options.print_trace_field = value;
543 }
544
545 value = false;
546 found = false;
547 apply_one_bool("field-trace:hostname", params, &value, &found);
548 if (found) {
549 pretty->options.print_trace_hostname_field = value;
550 }
551
552 value = false;
553 found = false;
554 apply_one_bool("field-trace:domain", params, &value, &found);
555 if (found) {
556 pretty->options.print_trace_domain_field = value;
557 }
558
559 value = false;
560 found = false;
561 apply_one_bool("field-trace:procname", params, &value, &found);
562 if (found) {
563 pretty->options.print_trace_procname_field = value;
564 }
565
566 value = false;
567 found = false;
568 apply_one_bool("field-trace:vpid", params, &value, &found);
569 if (found) {
570 pretty->options.print_trace_vpid_field = value;
571 }
572
573 value = false;
574 found = false;
575 apply_one_bool("field-loglevel", params, &value, &found);
576 if (found) {
577 pretty->options.print_loglevel_field = value;
578 }
579
580 value = false;
581 found = false;
582 apply_one_bool("field-emf", params, &value, &found);
583 if (found) {
584 pretty->options.print_emf_field = value;
585 }
586
587 value = false;
588 found = false;
589 apply_one_bool("field-callsite", params, &value, &found);
590 if (found) {
591 pretty->options.print_callsite_field = value;
592 }
593
594 end:
595 bt_object_put_ref(pretty->plugin_opt_map);
596 pretty->plugin_opt_map = NULL;
597 g_free(str);
598 return ret;
599 }
600
601 static
602 void set_use_colors(struct pretty_component *pretty)
603 {
604 switch (pretty->options.color) {
605 case PRETTY_COLOR_OPT_ALWAYS:
606 pretty->use_colors = true;
607 break;
608 case PRETTY_COLOR_OPT_AUTO:
609 pretty->use_colors = pretty->out == stdout &&
610 bt_common_colors_supported();
611 break;
612 case PRETTY_COLOR_OPT_NEVER:
613 pretty->use_colors = false;
614 break;
615 }
616 }
617
618 static
619 void init_stream_packet_context_quarks(void)
620 {
621 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
622 g_quark_from_string("timestamp_begin");
623 stream_packet_context_quarks[Q_TIMESTAMP_BEGIN] =
624 g_quark_from_string("timestamp_begin");
625 stream_packet_context_quarks[Q_TIMESTAMP_END] =
626 g_quark_from_string("timestamp_end");
627 stream_packet_context_quarks[Q_EVENTS_DISCARDED] =
628 g_quark_from_string("events_discarded");
629 stream_packet_context_quarks[Q_CONTENT_SIZE] =
630 g_quark_from_string("content_size");
631 stream_packet_context_quarks[Q_PACKET_SIZE] =
632 g_quark_from_string("packet_size");
633 stream_packet_context_quarks[Q_PACKET_SEQ_NUM] =
634 g_quark_from_string("packet_seq_num");
635 }
636
637 BT_HIDDEN
638 enum bt_self_component_status pretty_init(
639 struct bt_self_component_sink *comp,
640 const struct bt_value *params,
641 UNUSED_VAR void *init_method_data)
642 {
643 enum bt_self_component_status ret;
644 struct pretty_component *pretty = create_pretty();
645
646 if (!pretty) {
647 ret = BT_SELF_COMPONENT_STATUS_NOMEM;
648 goto end;
649 }
650
651 ret = bt_self_component_sink_add_input_port(comp, "in", NULL, NULL);
652 if (ret != BT_SELF_COMPONENT_STATUS_OK) {
653 goto end;
654 }
655
656 pretty->out = stdout;
657 pretty->err = stderr;
658
659 pretty->delta_cycles = -1ULL;
660 pretty->last_cycles_timestamp = -1ULL;
661
662 pretty->delta_real_timestamp = -1ULL;
663 pretty->last_real_timestamp = -1ULL;
664
665 if (apply_params(pretty, params)) {
666 ret = BT_SELF_COMPONENT_STATUS_ERROR;
667 goto error;
668 }
669
670 set_use_colors(pretty);
671 bt_self_component_set_data(
672 bt_self_component_sink_as_self_component(comp), pretty);
673 init_stream_packet_context_quarks();
674
675 end:
676 return ret;
677
678 error:
679 destroy_pretty_data(pretty);
680 return ret;
681 }
This page took 0.064056 seconds and 4 git commands to generate.