debug-info fix: packet_context is optional
[babeltrace.git] / plugins / lttng-utils / copy.c
CommitLineData
4f45f9bb
JD
1/*
2 * copy.c
3 *
4 * Babeltrace Copy Trace Structure
5 *
6 * Copyright 2017 Julien Desfossez <jdesfossez@efficios.com>
7 *
8 * Author: Julien Desfossez <jdesfossez@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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29#include <inttypes.h>
30#include <babeltrace/ctf-ir/event.h>
31#include <babeltrace/ctf-ir/packet.h>
32#include <babeltrace/ctf-ir/event-class.h>
33#include <babeltrace/ctf-ir/stream.h>
34#include <babeltrace/ctf-ir/stream-class.h>
35#include <babeltrace/ctf-ir/clock-class.h>
36#include <babeltrace/ctf-ir/fields.h>
37#include <babeltrace/ctf-writer/stream-class.h>
38#include <babeltrace/ctf-writer/stream.h>
39
40#include <ctfcopytrace.h>
41#include "debug-info.h"
42
cb0a5cf8
JD
43static
44struct bt_ctf_stream *insert_new_stream(
45 struct debug_info_iterator *debug_it,
46 struct bt_ctf_stream *stream,
47 struct debug_info_trace *di_trace);
48
504db471
JD
49static
50void unref_stream(struct bt_ctf_stream *stream)
51{
52 bt_put(stream);
53}
54
55static
56void unref_packet(struct bt_ctf_packet *packet)
57{
58 bt_put(packet);
59}
60
61static
62void unref_stream_class(struct bt_ctf_stream_class *stream_class)
63{
64 bt_put(stream_class);
65}
66
67static
68void unref_debug_info(struct debug_info *debug_info)
69{
70 debug_info_destroy(debug_info);
71}
72
1c78e839
JD
73static
74void destroy_stream_state_key(gpointer key)
75{
76 g_free((enum fs_writer_stream_state *) key);
77}
78
4f45f9bb
JD
79static
80struct bt_ctf_field *get_payload_field(FILE *err,
81 struct bt_ctf_event *event, const char *field_name)
82{
83 struct bt_ctf_field *field = NULL, *sec = NULL;
84 struct bt_ctf_field_type *sec_type = NULL;
85
86 sec = bt_ctf_event_get_payload(event, NULL);
87 if (!sec) {
88 fprintf(err, "[error] %s in %s:%d\n", __func__,
89 __FILE__, __LINE__);
90 goto end;
91 }
92
93 sec_type = bt_ctf_field_get_type(sec);
94 if (!sec_type) {
95 fprintf(err, "[error] %s in %s:%d\n", __func__,
96 __FILE__, __LINE__);
97 goto end;
98 }
99
1487a16a 100 if (bt_ctf_field_type_get_type_id(sec_type) != BT_CTF_FIELD_TYPE_ID_STRUCT) {
4f45f9bb
JD
101 fprintf(err, "[error] %s in %s:%d\n", __func__,
102 __FILE__, __LINE__);
103 goto end;
104 }
105
106 field = bt_ctf_field_structure_get_field(sec, field_name);
107
108end:
109 bt_put(sec_type);
110 bt_put(sec);
111 return field;
112}
113
114static
115struct bt_ctf_field *get_stream_event_context_field(FILE *err,
116 struct bt_ctf_event *event, const char *field_name)
117{
118 struct bt_ctf_field *field = NULL, *sec = NULL;
119 struct bt_ctf_field_type *sec_type = NULL;
120
121 sec = bt_ctf_event_get_stream_event_context(event);
122 if (!sec) {
4f45f9bb
JD
123 goto end;
124 }
125
126 sec_type = bt_ctf_field_get_type(sec);
127 if (!sec_type) {
128 fprintf(err, "[error] %s in %s:%d\n", __func__,
129 __FILE__, __LINE__);
130 goto end;
131 }
132
1487a16a 133 if (bt_ctf_field_type_get_type_id(sec_type) != BT_CTF_FIELD_TYPE_ID_STRUCT) {
4f45f9bb
JD
134 fprintf(err, "[error] %s in %s:%d\n", __func__,
135 __FILE__, __LINE__);
136 goto end;
137 }
138
139 field = bt_ctf_field_structure_get_field(sec, field_name);
140
141end:
142 bt_put(sec_type);
143 bt_put(sec);
144 return field;
145}
146
147BT_HIDDEN
148int get_stream_event_context_unsigned_int_field_value(FILE *err,
149 struct bt_ctf_event *event, const char *field_name,
150 uint64_t *value)
151{
152 int ret;
153 struct bt_ctf_field *field = NULL;
154 struct bt_ctf_field_type *field_type = NULL;
155
156 field = get_stream_event_context_field(err, event, field_name);
157 if (!field) {
4f45f9bb
JD
158 goto error;
159 }
160
161 field_type = bt_ctf_field_get_type(field);
162 if (!field_type) {
163 fprintf(err, "[error] %s in %s:%d\n", __func__,
164 __FILE__, __LINE__);
165 goto error;
166 }
167
1487a16a 168 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
4f45f9bb
JD
169 fprintf(err, "[error] %s in %s:%d\n", __func__,
170 __FILE__, __LINE__);
171 goto error;
172 }
173
174 if (bt_ctf_field_type_integer_get_signed(field_type) != 0) {
175 fprintf(err, "[error] %s in %s:%d\n", __func__,
176 __FILE__, __LINE__);
177 goto error;
178 }
179
180 ret = bt_ctf_field_unsigned_integer_get_value(field, value);
181 goto end;
182
183error:
184 ret = -1;
185end:
186 bt_put(field_type);
187 bt_put(field);
188 return ret;
189}
190
191BT_HIDDEN
192int get_stream_event_context_int_field_value(FILE *err, struct bt_ctf_event *event,
193 const char *field_name, int64_t *value)
194{
195 struct bt_ctf_field *field = NULL;
196 struct bt_ctf_field_type *field_type = NULL;
197 int ret;
198
199 field = get_stream_event_context_field(err, event, field_name);
200 if (!field) {
201 goto error;
202 }
203
204 field_type = bt_ctf_field_get_type(field);
205 if (!field_type) {
206 fprintf(err, "[error] %s in %s:%d\n", __func__,
207 __FILE__, __LINE__);
208 goto error;
209 }
210
1487a16a 211 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
4f45f9bb
JD
212 fprintf(err, "[error] %s in %s:%d\n", __func__,
213 __FILE__, __LINE__);
214 goto error;
215 }
216
217 if (bt_ctf_field_type_integer_get_signed(field_type) != 1) {
218 fprintf(err, "[error] %s in %s:%d\n", __func__,
219 __FILE__, __LINE__);
220 goto error;
221 }
222
223 ret = bt_ctf_field_signed_integer_get_value(field, value);
224 goto end;
225
226error:
227 ret = -1;
228end:
229 bt_put(field_type);
230 bt_put(field);
231 return ret;
232}
233
234BT_HIDDEN
235int get_payload_unsigned_int_field_value(FILE *err,
236 struct bt_ctf_event *event, const char *field_name,
237 uint64_t *value)
238{
239 struct bt_ctf_field *field = NULL;
240 struct bt_ctf_field_type *field_type = NULL;
241 int ret;
242
243 field = get_payload_field(err, event, field_name);
244 if (!field) {
245 fprintf(err, "[error] %s in %s:%d\n", __func__,
246 __FILE__, __LINE__);
247 goto error;
248 }
249
250 field_type = bt_ctf_field_get_type(field);
251 if (!field_type) {
252 fprintf(err, "[error] %s in %s:%d\n", __func__,
253 __FILE__, __LINE__);
254 goto error;
255 }
256
1487a16a 257 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
4f45f9bb
JD
258 fprintf(err, "[error] %s in %s:%d\n", __func__,
259 __FILE__, __LINE__);
260 goto error;
261 }
262
263 if (bt_ctf_field_type_integer_get_signed(field_type) != 0) {
264 fprintf(err, "[error] %s in %s:%d\n", __func__,
265 __FILE__, __LINE__);
266 goto error;
267 }
268
269 ret = bt_ctf_field_unsigned_integer_get_value(field, value);
270 goto end;
271
272error:
273 ret = -1;
274end:
275 bt_put(field_type);
276 bt_put(field);
277 return ret;
278}
279
280BT_HIDDEN
281int get_payload_int_field_value(FILE *err, struct bt_ctf_event *event,
282 const char *field_name, int64_t *value)
283{
284 struct bt_ctf_field *field = NULL;
285 struct bt_ctf_field_type *field_type = NULL;
286 int ret;
287
288 field = get_payload_field(err, event, field_name);
289 if (!field) {
290 fprintf(err, "[error] %s in %s:%d\n", __func__,
291 __FILE__, __LINE__);
292 goto error;
293 }
294
295 field_type = bt_ctf_field_get_type(field);
296 if (!field_type) {
297 fprintf(err, "[error] %s in %s:%d\n", __func__,
298 __FILE__, __LINE__);
299 goto error;
300 }
301
1487a16a 302 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_INTEGER) {
4f45f9bb
JD
303 fprintf(err, "[error] %s in %s:%d\n", __func__,
304 __FILE__, __LINE__);
305 goto error;
306 }
307
308 if (bt_ctf_field_type_integer_get_signed(field_type) != 1) {
309 fprintf(err, "[error] %s in %s:%d\n", __func__,
310 __FILE__, __LINE__);
311 goto error;
312 }
313
314 ret = bt_ctf_field_signed_integer_get_value(field, value);
315 goto end;
316
317error:
318 ret = -1;
319end:
320 bt_put(field_type);
321 bt_put(field);
322 return ret;
323}
324
325BT_HIDDEN
326int get_payload_string_field_value(FILE *err,
327 struct bt_ctf_event *event, const char *field_name,
328 const char **value)
329{
330 struct bt_ctf_field *field = NULL;
331 struct bt_ctf_field_type *field_type = NULL;
332 int ret;
333
65db8b88
JD
334 /*
335 * The field might not exist, no error here.
336 */
4f45f9bb
JD
337 field = get_payload_field(err, event, field_name);
338 if (!field) {
4f45f9bb
JD
339 goto error;
340 }
341
342 field_type = bt_ctf_field_get_type(field);
343 if (!field_type) {
344 fprintf(err, "[error] %s in %s:%d\n", __func__,
345 __FILE__, __LINE__);
346 goto error;
347 }
348
1487a16a 349 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_STRING) {
4f45f9bb
JD
350 fprintf(err, "[error] %s in %s:%d\n", __func__,
351 __FILE__, __LINE__);
352 goto error;
353 }
354
355 *value = bt_ctf_field_string_get_value(field);
356 if (!*value) {
357 fprintf(err, "[error] %s in %s:%d\n", __func__,
358 __FILE__, __LINE__);
359 goto error;
360 }
361
362 ret = 0;
363 goto end;
364
365error:
366 ret = -1;
367end:
368 bt_put(field_type);
369 bt_put(field);
370 return ret;
371}
372
373BT_HIDDEN
374int get_payload_build_id_field_value(FILE *err,
375 struct bt_ctf_event *event, const char *field_name,
376 uint8_t **build_id, uint64_t *build_id_len)
377{
378 struct bt_ctf_field *field = NULL, *seq_len = NULL;
379 struct bt_ctf_field_type *field_type = NULL;
380 struct bt_ctf_field *seq_field = NULL;
381 uint64_t i;
382 int ret;
383
384 *build_id = NULL;
385
386 field = get_payload_field(err, event, field_name);
387 if (!field) {
388 fprintf(err, "[error] %s in %s:%d\n", __func__,
389 __FILE__, __LINE__);
390 goto error;
391 }
392
393 field_type = bt_ctf_field_get_type(field);
394 if (!field_type) {
395 fprintf(err, "[error] %s in %s:%d\n", __func__,
396 __FILE__, __LINE__);
397 goto error;
398 }
399
1487a16a 400 if (bt_ctf_field_type_get_type_id(field_type) != BT_CTF_FIELD_TYPE_ID_SEQUENCE) {
4f45f9bb
JD
401 fprintf(err, "[error] %s in %s:%d\n", __func__,
402 __FILE__, __LINE__);
403 goto error;
404 }
405 BT_PUT(field_type);
406
407 seq_len = bt_ctf_field_sequence_get_length(field);
408 if (!seq_len) {
409 fprintf(err, "[error] %s in %s:%d\n", __func__,
410 __FILE__, __LINE__);
411 goto error;
412 }
413
414 ret = bt_ctf_field_unsigned_integer_get_value(seq_len, build_id_len);
415 if (ret) {
416 fprintf(err, "[error] %s in %s:%d\n", __func__,
417 __FILE__, __LINE__);
418 goto error;
419 }
420 BT_PUT(seq_len);
421
422 *build_id = g_new0(uint8_t, *build_id_len);
423 if (!*build_id) {
424 fprintf(err, "[error] %s in %s:%d\n", __func__,
425 __FILE__, __LINE__);
426 goto error;
427 }
428
429 for (i = 0; i < *build_id_len; i++) {
430 uint64_t tmp;
431
432 seq_field = bt_ctf_field_sequence_get_field(field, i);
433 if (!seq_field) {
434 fprintf(err, "[error] %s in %s:%d\n", __func__,
435 __FILE__, __LINE__);
436 goto error;
437 }
438
439 ret = bt_ctf_field_unsigned_integer_get_value(seq_field, &tmp);
440 if (ret) {
441 fprintf(err, "[error] %s in %s:%d\n", __func__,
442 __FILE__, __LINE__);
443 goto error;
444 }
445 BT_PUT(seq_field);
446 (*build_id)[i] = (uint8_t) tmp;
447 }
448 ret = 0;
449 goto end;
450
451error:
452 g_free(*build_id);
453 ret = -1;
454end:
455 bt_put(field_type);
456 bt_put(field);
457 return ret;
458}
459
460static
461struct debug_info *lookup_trace_debug_info(struct debug_info_iterator *debug_it,
504db471
JD
462 struct bt_ctf_trace *writer_trace,
463 struct debug_info_trace *di_trace)
4f45f9bb
JD
464{
465 return (struct debug_info *) g_hash_table_lookup(
504db471 466 di_trace->trace_debug_map,
4f45f9bb
JD
467 (gpointer) writer_trace);
468}
469
470static
471struct debug_info *insert_new_debug_info(struct debug_info_iterator *debug_it,
504db471
JD
472 struct bt_ctf_trace *writer_trace,
473 struct debug_info_trace *di_trace)
4f45f9bb
JD
474{
475 struct debug_info *debug_info = NULL;
476 struct bt_value *field = NULL;
477 const char *str_value;
478 enum bt_value_status ret;
479
480 field = bt_ctf_trace_get_environment_field_value_by_name(writer_trace,
481 "domain");
482 /* No domain field, no debug info */
483 if (!field) {
484 goto end;
485 }
486 ret = bt_value_string_get(field, &str_value);
487 if (ret != BT_VALUE_STATUS_OK) {
488 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
489 __FILE__, __LINE__);
490 goto end;
491 }
492 /* Domain not ust, no debug info */
493 if (strcmp(str_value, "ust") != 0) {
494 goto end;
495 }
496 BT_PUT(field);
497
498 /* No tracer_name, no debug info */
499 field = bt_ctf_trace_get_environment_field_value_by_name(writer_trace,
500 "tracer_name");
501 /* No tracer_name, no debug info */
502 if (!field) {
503 goto end;
504 }
505 ret = bt_value_string_get(field, &str_value);
506 if (ret != BT_VALUE_STATUS_OK) {
507 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
508 __FILE__, __LINE__);
509 goto end;
510 }
511 /* Tracer_name not lttng-ust, no debug info */
512 if (strcmp(str_value, "lttng-ust") != 0) {
513 goto end;
514 }
515 BT_PUT(field);
516
9d325e17 517 debug_info = debug_info_create(debug_it->debug_info_component);
4f45f9bb
JD
518 if (!debug_info) {
519 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
520 __FILE__, __LINE__);
521 goto end;
522 }
523
504db471 524 g_hash_table_insert(di_trace->trace_debug_map, (gpointer) writer_trace,
4f45f9bb
JD
525 debug_info);
526
527end:
528 bt_put(field);
529 return debug_info;
530}
531
532static
533struct debug_info *get_trace_debug_info(struct debug_info_iterator *debug_it,
504db471
JD
534 struct bt_ctf_trace *writer_trace,
535 struct debug_info_trace *di_trace)
4f45f9bb
JD
536{
537 struct debug_info *debug_info;
538
504db471 539 debug_info = lookup_trace_debug_info(debug_it, writer_trace, di_trace);
4f45f9bb
JD
540 if (debug_info) {
541 goto end;
542 }
543
504db471 544 debug_info = insert_new_debug_info(debug_it, writer_trace, di_trace);
4f45f9bb
JD
545
546end:
547 return debug_info;
548}
549
550static
504db471 551struct debug_info_trace *lookup_trace(struct debug_info_iterator *debug_it,
4f45f9bb
JD
552 struct bt_ctf_trace *trace)
553{
504db471 554 return (struct debug_info_trace *) g_hash_table_lookup(
4f45f9bb
JD
555 debug_it->trace_map,
556 (gpointer) trace);
557}
558
1c78e839
JD
559static
560enum debug_info_stream_state *insert_new_stream_state(
561 struct debug_info_iterator *debug_it,
562 struct debug_info_trace *di_trace, struct bt_ctf_stream *stream)
563{
564 enum debug_info_stream_state *v = NULL;
565
566 v = g_new0(enum debug_info_stream_state, 1);
567 if (!v) {
568 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
569 __FILE__, __LINE__);
ec273a88 570 goto end;
1c78e839
JD
571 }
572 *v = DEBUG_INFO_UNKNOWN_STREAM;
573
574 g_hash_table_insert(di_trace->stream_states, stream, v);
575
ec273a88 576end:
1c78e839
JD
577 return v;
578}
579
580static
581void check_completed_trace(gpointer key, gpointer value, gpointer user_data)
582{
583 enum debug_info_stream_state *state = value;
584 int *trace_completed = user_data;
585
586 if (*state != DEBUG_INFO_COMPLETED_STREAM) {
587 *trace_completed = 0;
588 }
589}
590
591static
592gboolean empty_ht(gpointer key, gpointer value, gpointer user_data)
593{
594 return TRUE;
595}
596
597BT_HIDDEN
598void debug_info_close_trace(struct debug_info_iterator *debug_it,
599 struct debug_info_trace *di_trace)
600{
ec273a88 601 if (di_trace->static_listener_id >= 0) {
1c78e839
JD
602 bt_ctf_trace_remove_is_static_listener(di_trace->trace,
603 di_trace->static_listener_id);
604 }
605
606 /* Empty the stream class HT. */
607 g_hash_table_foreach_remove(di_trace->stream_class_map,
608 empty_ht, NULL);
609 g_hash_table_destroy(di_trace->stream_class_map);
610
611 /* Empty the stream HT. */
612 g_hash_table_foreach_remove(di_trace->stream_map,
613 empty_ht, NULL);
614 g_hash_table_destroy(di_trace->stream_map);
615
616 /* Empty the stream state HT. */
617 g_hash_table_foreach_remove(di_trace->stream_states,
618 empty_ht, NULL);
619 g_hash_table_destroy(di_trace->stream_states);
620
621 /* Empty the packet HT. */
622 g_hash_table_foreach_remove(di_trace->packet_map,
623 empty_ht, NULL);
624 g_hash_table_destroy(di_trace->packet_map);
625
626 /* Empty the trace_debug HT. */
627 g_hash_table_foreach_remove(di_trace->trace_debug_map,
628 empty_ht, NULL);
629 g_hash_table_destroy(di_trace->trace_debug_map);
630}
631
cb0a5cf8
JD
632static
633int sync_event_classes(struct debug_info_iterator *debug_it,
634 struct bt_ctf_stream *stream,
635 struct bt_ctf_stream *writer_stream)
636{
637 int int_ret;
638 struct bt_ctf_stream_class *stream_class = NULL,
639 *writer_stream_class = NULL;
640 enum bt_component_status ret;
641
642 stream_class = bt_ctf_stream_get_class(stream);
643 if (!stream_class) {
644 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
645 __FILE__, __LINE__);
646 goto error;
647 }
648
649 writer_stream_class = bt_ctf_stream_get_class(writer_stream);
650 if (!writer_stream_class) {
651 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
652 __FILE__, __LINE__);
653 goto error;
654 }
655
656 ret = ctf_copy_event_classes(debug_it->err, stream_class,
657 writer_stream_class);
658 if (ret != BT_COMPONENT_STATUS_OK) {
659 goto error;
660 }
661
662 int_ret = 0;
663 goto end;
664
665error:
666 int_ret = -1;
667end:
668 bt_put(stream_class);
669 bt_put(writer_stream_class);
670 return int_ret;
671}
672
1c78e839
JD
673static
674void trace_is_static_listener(struct bt_ctf_trace *trace, void *data)
675{
676 struct debug_info_trace *di_trace = data;
cb0a5cf8
JD
677 struct debug_info_iterator *debug_it = di_trace->debug_it;
678 int trace_completed = 1, ret, nr_stream, i;
679 struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
680 struct bt_ctf_trace *writer_trace = di_trace->writer_trace;
681
682 /*
683 * When the trace becomes static, make sure that we have all
684 * the event classes in our stream_class copies before setting it
685 * static as well.
686 */
687 nr_stream = bt_ctf_trace_get_stream_count(trace);
688 for (i = 0; i < nr_stream; i++) {
689 stream = bt_ctf_trace_get_stream_by_index(trace, i);
690 if (!stream) {
691 fprintf(debug_it->err,
692 "[error] %s in %s:%d\n", __func__,
693 __FILE__, __LINE__);
694 goto error;
695 }
696 writer_stream = bt_ctf_trace_get_stream_by_index(writer_trace, i);
697 if (!writer_stream) {
698 fprintf(debug_it->err,
699 "[error] %s in %s:%d\n", __func__,
700 __FILE__, __LINE__);
701 goto error;
702 }
703 ret = sync_event_classes(di_trace->debug_it, stream, writer_stream);
704 if (ret) {
705 fprintf(debug_it->err,
706 "[error] %s in %s:%d\n", __func__,
707 __FILE__, __LINE__);
708 goto error;
709 }
710 BT_PUT(stream);
711 BT_PUT(writer_stream);
712 }
1c78e839 713
cb0a5cf8 714 bt_ctf_trace_set_is_static(di_trace->writer_trace);
1c78e839
JD
715 di_trace->trace_static = 1;
716
717 g_hash_table_foreach(di_trace->stream_states,
718 check_completed_trace, &trace_completed);
719 if (trace_completed) {
720 debug_info_close_trace(di_trace->debug_it, di_trace);
721 g_hash_table_remove(di_trace->debug_it->trace_map,
722 di_trace->trace);
723 }
cb0a5cf8
JD
724
725error:
726 bt_put(writer_stream);
727 bt_put(stream);
1c78e839
JD
728}
729
4f45f9bb 730static
504db471
JD
731struct debug_info_trace *insert_new_trace(struct debug_info_iterator *debug_it,
732 struct bt_ctf_stream *stream) {
4f45f9bb 733 struct bt_ctf_trace *writer_trace = NULL;
504db471
JD
734 struct debug_info_trace *di_trace = NULL;
735 struct bt_ctf_trace *trace = NULL;
736 struct bt_ctf_stream_class *stream_class = NULL;
cb0a5cf8 737 struct bt_ctf_stream *writer_stream = NULL;
1c78e839 738 int ret, nr_stream, i;
4f45f9bb
JD
739
740 writer_trace = bt_ctf_trace_create();
741 if (!writer_trace) {
742 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
743 __FILE__, __LINE__);
744 goto error;
745 }
504db471
JD
746
747 stream_class = bt_ctf_stream_get_class(stream);
748 if (!stream_class) {
749 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
750 __FILE__, __LINE__);
751 goto error;
752 }
753
754 trace = bt_ctf_stream_class_get_trace(stream_class);
755 if (!trace) {
756 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
757 __FILE__, __LINE__);
758 goto error;
759 }
4f45f9bb
JD
760
761 ret = ctf_copy_trace(debug_it->err, trace, writer_trace);
762 if (ret != BT_COMPONENT_STATUS_OK) {
763 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
764 __FILE__, __LINE__);
765 goto error;
766 }
767
504db471
JD
768 di_trace = g_new0(struct debug_info_trace, 1);
769 if (!di_trace) {
770 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
771 __FILE__, __LINE__);
772 goto error;
773 }
774
775 di_trace->trace = trace;
776 di_trace->writer_trace = writer_trace;
777 di_trace->debug_info_component = debug_it->debug_info_component;
1c78e839 778 di_trace->debug_it = debug_it;
504db471
JD
779 di_trace->stream_map = g_hash_table_new_full(g_direct_hash,
780 g_direct_equal, NULL, (GDestroyNotify) unref_stream);
781 di_trace->stream_class_map = g_hash_table_new_full(g_direct_hash,
782 g_direct_equal, NULL, (GDestroyNotify) unref_stream_class);
783 di_trace->packet_map = g_hash_table_new_full(g_direct_hash,
784 g_direct_equal, NULL, (GDestroyNotify) unref_packet);
785 di_trace->trace_debug_map = g_hash_table_new_full(g_direct_hash,
786 g_direct_equal, NULL, (GDestroyNotify) unref_debug_info);
1c78e839
JD
787 di_trace->stream_states = g_hash_table_new_full(g_direct_hash,
788 g_direct_equal, NULL, destroy_stream_state_key);
cb0a5cf8 789 g_hash_table_insert(debug_it->trace_map, (gpointer) trace, di_trace);
1c78e839
JD
790
791 /* Set all the existing streams in the unknown state. */
792 nr_stream = bt_ctf_trace_get_stream_count(trace);
793 for (i = 0; i < nr_stream; i++) {
794 stream = bt_ctf_trace_get_stream_by_index(trace, i);
795 if (!stream) {
796 fprintf(debug_it->err,
797 "[error] %s in %s:%d\n", __func__,
798 __FILE__, __LINE__);
799 goto error;
800 }
801 insert_new_stream_state(debug_it, di_trace, stream);
cb0a5cf8
JD
802 writer_stream = insert_new_stream(debug_it, stream, di_trace);
803 if (!writer_stream) {
804 fprintf(debug_it->err,
805 "[error] %s in %s:%d\n", __func__,
806 __FILE__, __LINE__);
807 goto error;
808 }
809 bt_get(writer_stream);
810 ret = sync_event_classes(debug_it, stream, writer_stream);
811 if (ret) {
812 fprintf(debug_it->err,
813 "[error] %s in %s:%d\n", __func__,
814 __FILE__, __LINE__);
815 goto error;
816 }
817 BT_PUT(writer_stream);
1c78e839
JD
818 BT_PUT(stream);
819 }
820
821 /* Check if the trace is already static or register a listener. */
822 if (bt_ctf_trace_is_static(trace)) {
823 di_trace->trace_static = 1;
824 di_trace->static_listener_id = -1;
cb0a5cf8 825 bt_ctf_trace_set_is_static(writer_trace);
1c78e839
JD
826 } else {
827 ret = bt_ctf_trace_add_is_static_listener(trace,
828 trace_is_static_listener, di_trace);
829 if (ret < 0) {
830 fprintf(debug_it->err,
831 "[error] %s in %s:%d\n", __func__, __FILE__,
832 __LINE__);
833 goto error;
834 }
835 di_trace->static_listener_id = ret;
836 }
504db471 837
504db471 838
4f45f9bb
JD
839 goto end;
840
841error:
842 BT_PUT(writer_trace);
504db471
JD
843 g_free(di_trace);
844 di_trace = NULL;
4f45f9bb 845end:
cb0a5cf8
JD
846 bt_put(stream);
847 bt_put(writer_stream);
504db471
JD
848 bt_put(stream_class);
849 bt_put(trace);
850 return di_trace;
4f45f9bb
JD
851}
852
853static
854struct bt_ctf_packet *lookup_packet(struct debug_info_iterator *debug_it,
504db471
JD
855 struct bt_ctf_packet *packet,
856 struct debug_info_trace *di_trace)
4f45f9bb
JD
857{
858 return (struct bt_ctf_packet *) g_hash_table_lookup(
504db471 859 di_trace->packet_map,
4f45f9bb
JD
860 (gpointer) packet);
861}
862
863static
864struct bt_ctf_packet *insert_new_packet(struct debug_info_iterator *debug_it,
865 struct bt_ctf_packet *packet,
504db471
JD
866 struct bt_ctf_stream *writer_stream,
867 struct debug_info_trace *di_trace)
4f45f9bb
JD
868{
869 struct bt_ctf_packet *writer_packet;
870
871 writer_packet = bt_ctf_packet_create(writer_stream);
872 if (!writer_packet) {
873 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
874 __FILE__, __LINE__);
875 goto end;
876 }
504db471 877 g_hash_table_insert(di_trace->packet_map, (gpointer) packet, writer_packet);
4f45f9bb
JD
878
879end:
880 return writer_packet;
881}
882
883static
884int add_debug_info_fields(FILE *err,
885 struct bt_ctf_field_type *writer_event_context_type,
886 struct debug_info_component *component)
887{
888 struct bt_ctf_field_type *ip_field = NULL, *debug_field_type = NULL,
889 *bin_field_type = NULL, *func_field_type = NULL,
890 *src_field_type = NULL;
891 int ret = 0;
892
893 ip_field = bt_ctf_field_type_structure_get_field_type_by_name(
894 writer_event_context_type, "_ip");
895 /* No ip field, so no debug info. */
896 if (!ip_field) {
897 goto end;
898 }
899 BT_PUT(ip_field);
900
901 debug_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
902 writer_event_context_type,
903 component->arg_debug_info_field_name);
904 /* Already existing debug_info field, no need to add it. */
905 if (debug_field_type) {
906 goto end;
907 }
908
909 debug_field_type = bt_ctf_field_type_structure_create();
910 if (!debug_field_type) {
911 fprintf(err, "[error] %s in %s:%d\n", __func__,
912 __FILE__, __LINE__);
913 goto error;
914 }
915
916 bin_field_type = bt_ctf_field_type_string_create();
917 if (!bin_field_type) {
918 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
919 __LINE__);
920 goto error;
921 }
922
923 func_field_type = bt_ctf_field_type_string_create();
924 if (!func_field_type) {
925 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
926 __LINE__);
927 goto error;
928 }
929
930 src_field_type = bt_ctf_field_type_string_create();
931 if (!src_field_type) {
932 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
933 __LINE__);
934 goto error;
935 }
936
937 ret = bt_ctf_field_type_structure_add_field(debug_field_type,
938 bin_field_type, "bin");
939 if (ret) {
940 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
941 __LINE__);
942 goto error;
943 }
944
945 ret = bt_ctf_field_type_structure_add_field(debug_field_type,
946 func_field_type, "func");
947 if (ret) {
948 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
949 __LINE__);
950 goto error;
951 }
952
953 ret = bt_ctf_field_type_structure_add_field(debug_field_type,
954 src_field_type, "src");
955 if (ret) {
956 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
957 __LINE__);
958 goto error;
959 }
960
961 ret = bt_ctf_field_type_structure_add_field(writer_event_context_type,
962 debug_field_type, component->arg_debug_info_field_name);
963 if (ret) {
964 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
965 __LINE__);
966 goto error;
967 }
968
969 ret = 0;
970 goto end;
971
972error:
973 BT_PUT(debug_field_type);
974 ret = -1;
975end:
976 bt_put(src_field_type);
977 bt_put(func_field_type);
978 bt_put(bin_field_type);
979 bt_put(debug_field_type);
980 return ret;
981}
982
983static
984int create_debug_info_event_context_type(FILE *err,
985 struct bt_ctf_field_type *event_context_type,
986 struct bt_ctf_field_type *writer_event_context_type,
987 struct debug_info_component *component)
988{
989 int ret, nr_fields, i;
990
991 nr_fields = bt_ctf_field_type_structure_get_field_count(event_context_type);
992 for (i = 0; i < nr_fields; i++) {
993 struct bt_ctf_field_type *field_type = NULL;
994 const char *field_name;
995
996 if (bt_ctf_field_type_structure_get_field(event_context_type,
997 &field_name, &field_type, i) < 0) {
998 fprintf(err, "[error] %s in %s:%d\n", __func__,
999 __FILE__, __LINE__);
1000 goto error;
1001 }
1002
1003 ret = bt_ctf_field_type_structure_add_field(writer_event_context_type,
1004 field_type, field_name);
1005 BT_PUT(field_type);
1006 if (ret) {
1007 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1008 __LINE__);
1009 goto error;
1010 }
1011 }
1012
1013 ret = add_debug_info_fields(err, writer_event_context_type,
1014 component);
1015 goto end;
1016
1017error:
1018 ret = -1;
1019end:
1020 return ret;
1021}
1022
1023static
1024struct bt_ctf_stream_class *copy_stream_class_debug_info(FILE *err,
1025 struct bt_ctf_stream_class *stream_class,
1026 struct bt_ctf_trace *writer_trace,
1027 struct debug_info_component *component)
1028{
1029 struct bt_ctf_field_type *type = NULL;
1030 struct bt_ctf_stream_class *writer_stream_class = NULL;
1031 struct bt_ctf_field_type *writer_event_context_type = NULL;
1032 int ret_int;
1033 const char *name = bt_ctf_stream_class_get_name(stream_class);
1034
1035 if (strlen(name) == 0) {
1036 name = NULL;
1037 }
1038
2d64a1eb 1039 writer_stream_class = bt_ctf_stream_class_create_empty(name);
4f45f9bb
JD
1040 if (!writer_stream_class) {
1041 fprintf(err, "[error] %s in %s:%d\n",
1042 __func__, __FILE__, __LINE__);
1043 goto error;
1044 }
1045
1046 type = bt_ctf_stream_class_get_packet_context_type(stream_class);
2d64a1eb
JD
1047 if (type) {
1048 ret_int = bt_ctf_stream_class_set_packet_context_type(
1049 writer_stream_class, type);
1050 if (ret_int < 0) {
1051 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1052 __LINE__);
1053 goto error;
1054 }
1055 BT_PUT(type);
4f45f9bb 1056 }
4f45f9bb
JD
1057
1058 type = bt_ctf_stream_class_get_event_header_type(stream_class);
290b95cc
MD
1059 if (type) {
1060 ret_int = bt_ctf_stream_class_set_event_header_type(
1061 writer_stream_class, type);
1062 if (ret_int < 0) {
1063 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1064 __LINE__);
1065 goto error;
1066 }
1067 BT_PUT(type);
4f45f9bb 1068 }
4f45f9bb
JD
1069
1070 type = bt_ctf_stream_class_get_event_context_type(stream_class);
1071 if (type) {
1072 writer_event_context_type = bt_ctf_field_type_structure_create();
1073 if (!writer_event_context_type) {
1074 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1075 __LINE__);
1076 goto error;
1077 }
1078 ret_int = create_debug_info_event_context_type(err, type,
1079 writer_event_context_type, component);
1080 if (ret_int) {
1081 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1082 __LINE__);
1083 goto error;
1084 }
1085 BT_PUT(type);
1086
1087 ret_int = bt_ctf_stream_class_set_event_context_type(
1088 writer_stream_class, writer_event_context_type);
1089 if (ret_int < 0) {
1090 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1091 __LINE__);
1092 goto error;
1093 }
1094 BT_PUT(writer_event_context_type);
1095 }
1096
1097 goto end;
1098
1099error:
1100 BT_PUT(writer_stream_class);
1101end:
1102 bt_put(writer_event_context_type);
1103 bt_put(type);
1104 return writer_stream_class;
1105}
1106
1107/*
1108 * Add the original clock classes to the new trace, we do not need to copy
1109 * them, and if we did, we would have to manually inspect the stream class
1110 * to update the integers mapping to a clock.
1111 */
1112static
1113int add_clock_classes(FILE *err, struct bt_ctf_trace *writer_trace,
1114 struct bt_ctf_stream_class *writer_stream_class,
1115 struct bt_ctf_trace *trace)
1116{
1117 int ret, clock_class_count, i;
1118
1119 clock_class_count = bt_ctf_trace_get_clock_class_count(trace);
1120
1121 for (i = 0; i < clock_class_count; i++) {
1122 struct bt_ctf_clock_class *clock_class =
9ac68eb1 1123 bt_ctf_trace_get_clock_class_by_index(trace, i);
4f45f9bb
JD
1124
1125 if (!clock_class) {
1126 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1127 __LINE__);
1128 goto error;
1129 }
1130
1131 ret = bt_ctf_trace_add_clock_class(writer_trace, clock_class);
1132 BT_PUT(clock_class);
1133 if (ret != 0) {
1134 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1135 __LINE__);
1136 goto error;
1137 }
1138 }
1139
1140 ret = 0;
1141 goto end;
1142
1143error:
1144 ret = -1;
1145end:
1146 return ret;
1147
1148}
1149
1150static
1151struct bt_ctf_stream_class *insert_new_stream_class(
1152 struct debug_info_iterator *debug_it,
1153 struct bt_ctf_stream_class *stream_class)
1154{
1155 struct bt_ctf_stream_class *writer_stream_class = NULL;
1156 struct bt_ctf_trace *trace, *writer_trace = NULL;
504db471 1157 struct debug_info_trace *di_trace;
4f45f9bb
JD
1158 enum bt_component_status ret;
1159 int int_ret;
1160
1161 trace = bt_ctf_stream_class_get_trace(stream_class);
1162 if (!trace) {
1163 fprintf(debug_it->err,
1164 "[error] %s in %s:%d\n", __func__, __FILE__,
1165 __LINE__);
1166 goto error;
1167 }
1168
504db471
JD
1169 di_trace = lookup_trace(debug_it, trace);
1170 if (!di_trace) {
1171 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
1172 __FILE__, __LINE__);
1173 ret = BT_COMPONENT_STATUS_ERROR;
1174 goto error;
4f45f9bb 1175 }
504db471 1176 writer_trace = di_trace->writer_trace;
4f45f9bb
JD
1177 bt_get(writer_trace);
1178
1179 writer_stream_class = copy_stream_class_debug_info(debug_it->err, stream_class,
1180 writer_trace, debug_it->debug_info_component);
1181 if (!writer_stream_class) {
1182 fprintf(debug_it->err, "[error] Failed to copy stream class\n");
1183 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1184 __func__, __FILE__, __LINE__);
1185 goto error;
1186 }
1187
1188 int_ret = bt_ctf_trace_add_stream_class(writer_trace, writer_stream_class);
1189 if (int_ret) {
1190 fprintf(debug_it->err,
1191 "[error] %s in %s:%d\n", __func__, __FILE__,
1192 __LINE__);
1193 goto error;
1194 }
1195
1196 ret = add_clock_classes(debug_it->err, writer_trace,
1197 writer_stream_class, trace);
1198 if (ret != BT_COMPONENT_STATUS_OK) {
1199 fprintf(debug_it->err,
1200 "[error] %s in %s:%d\n", __func__, __FILE__,
1201 __LINE__);
1202 goto error;
1203 }
4f45f9bb 1204
504db471 1205 g_hash_table_insert(di_trace->stream_class_map,
4f45f9bb
JD
1206 (gpointer) stream_class, writer_stream_class);
1207
1208 goto end;
1209
1210error:
1211 BT_PUT(writer_stream_class);
1212end:
1213 bt_put(trace);
1214 bt_put(writer_trace);
1215 return writer_stream_class;
1216}
1217
1218static
1219struct bt_ctf_stream *insert_new_stream(
1220 struct debug_info_iterator *debug_it,
504db471
JD
1221 struct bt_ctf_stream *stream,
1222 struct debug_info_trace *di_trace)
4f45f9bb
JD
1223{
1224 struct bt_ctf_stream *writer_stream = NULL;
504db471 1225 struct bt_ctf_stream_class *stream_class = NULL;
4f45f9bb
JD
1226 struct bt_ctf_stream_class *writer_stream_class = NULL;
1227
504db471
JD
1228 stream_class = bt_ctf_stream_get_class(stream);
1229 if (!stream_class) {
1230 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1231 __func__, __FILE__, __LINE__);
1232 goto error;
1233 }
1234
4f45f9bb 1235 writer_stream_class = g_hash_table_lookup(
504db471 1236 di_trace->stream_class_map,
4f45f9bb
JD
1237 (gpointer) stream_class);
1238
1239 if (!writer_stream_class) {
1240 writer_stream_class = insert_new_stream_class(debug_it,
1241 stream_class);
1242 if (!writer_stream_class) {
1243 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1244 __func__, __FILE__, __LINE__);
1245 goto error;
1246 }
1247 }
1248 bt_get(writer_stream_class);
1249
1250 writer_stream = bt_ctf_stream_create(writer_stream_class,
1251 bt_ctf_stream_get_name(stream));
1252 if (!writer_stream) {
1253 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1254 __func__, __FILE__, __LINE__);
1255 goto error;
1256 }
1257
504db471 1258 g_hash_table_insert(di_trace->stream_map, (gpointer) stream,
4f45f9bb
JD
1259 writer_stream);
1260
1261 goto end;
1262
1263error:
1264 BT_PUT(writer_stream);
1265end:
504db471 1266 bt_put(stream_class);
4f45f9bb
JD
1267 bt_put(writer_stream_class);
1268 return writer_stream;
1269}
1270
1271static
1272struct bt_ctf_stream *lookup_stream(struct debug_info_iterator *debug_it,
504db471
JD
1273 struct bt_ctf_stream *stream,
1274 struct debug_info_trace *di_trace)
4f45f9bb
JD
1275{
1276 return (struct bt_ctf_stream *) g_hash_table_lookup(
504db471 1277 di_trace->stream_map, (gpointer) stream);
4f45f9bb
JD
1278}
1279
1280static
1281struct bt_ctf_event_class *get_event_class(struct debug_info_iterator *debug_it,
1282 struct bt_ctf_stream_class *writer_stream_class,
1283 struct bt_ctf_event_class *event_class)
1284{
a9f0d01b
PP
1285 return bt_ctf_stream_class_get_event_class_by_id(writer_stream_class,
1286 bt_ctf_event_class_get_id(event_class));
4f45f9bb
JD
1287}
1288
504db471
JD
1289static
1290struct debug_info_trace *lookup_di_trace_from_stream(
1291 struct debug_info_iterator *debug_it,
1292 struct bt_ctf_stream *stream)
1293{
1294 struct bt_ctf_stream_class *stream_class = NULL;
1295 struct bt_ctf_trace *trace = NULL;
1296 struct debug_info_trace *di_trace = NULL;
1297
1298 stream_class = bt_ctf_stream_get_class(stream);
1299 if (!stream_class) {
1300 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1301 __func__, __FILE__, __LINE__);
1302 goto end;
1303 }
1304
1305 trace = bt_ctf_stream_class_get_trace(stream_class);
1306 if (!trace) {
1307 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1308 __func__, __FILE__, __LINE__);
1309 goto end;
1310 }
1311
1312 di_trace = (struct debug_info_trace *) g_hash_table_lookup(
1313 debug_it->trace_map, (gpointer) trace);
1314
1315end:
1316 BT_PUT(stream_class);
1317 BT_PUT(trace);
1318 return di_trace;
1319}
1320
4f45f9bb
JD
1321static
1322struct bt_ctf_stream *get_writer_stream(
1323 struct debug_info_iterator *debug_it,
1324 struct bt_ctf_packet *packet, struct bt_ctf_stream *stream)
1325{
1326 struct bt_ctf_stream_class *stream_class = NULL;
1327 struct bt_ctf_stream *writer_stream = NULL;
504db471 1328 struct debug_info_trace *di_trace = NULL;
4f45f9bb
JD
1329
1330 stream_class = bt_ctf_stream_get_class(stream);
1331 if (!stream_class) {
1332 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1333 __func__, __FILE__, __LINE__);
1334 goto error;
1335 }
1336
504db471
JD
1337 di_trace = lookup_di_trace_from_stream(debug_it, stream);
1338 if (!di_trace) {
1339 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1340 __func__, __FILE__, __LINE__);
1341 goto error;
1342 }
1343
1344 writer_stream = lookup_stream(debug_it, stream, di_trace);
4f45f9bb 1345 if (!writer_stream) {
504db471
JD
1346 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1347 __func__, __FILE__, __LINE__);
1348 goto error;
4f45f9bb
JD
1349 }
1350 bt_get(writer_stream);
1351
1352 goto end;
1353
1354error:
1355 BT_PUT(writer_stream);
1356end:
1357 bt_put(stream_class);
1358 return writer_stream;
1359}
1360
1361BT_HIDDEN
1362struct bt_ctf_packet *debug_info_new_packet(
1363 struct debug_info_iterator *debug_it,
1364 struct bt_ctf_packet *packet)
1365{
1366 struct bt_ctf_stream *stream = NULL, *writer_stream = NULL;
1367 struct bt_ctf_field *writer_packet_context = NULL;
1368 struct bt_ctf_packet *writer_packet = NULL;
2d64a1eb 1369 struct bt_ctf_field *packet_context = NULL;
504db471 1370 struct debug_info_trace *di_trace;
4f45f9bb
JD
1371 int int_ret;
1372
1373 stream = bt_ctf_packet_get_stream(packet);
1374 if (!stream) {
1375 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1376 __func__, __FILE__, __LINE__);
1377 goto error;
1378 }
1379
1380 writer_stream = get_writer_stream(debug_it, packet, stream);
1381 if (!writer_stream) {
1382 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1383 __func__, __FILE__, __LINE__);
1384 goto error;
1385 }
1386
504db471
JD
1387 di_trace = lookup_di_trace_from_stream(debug_it, stream);
1388 if (!di_trace) {
1389 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
1390 __FILE__, __LINE__);
1391 goto error;
1392 }
1393
4f45f9bb
JD
1394 /*
1395 * If a packet was already opened, close it and remove it from
1396 * the HT.
1397 */
504db471 1398 writer_packet = lookup_packet(debug_it, packet, di_trace);
4f45f9bb 1399 if (writer_packet) {
504db471 1400 g_hash_table_remove(di_trace->packet_map, packet);
4f45f9bb
JD
1401 BT_PUT(writer_packet);
1402 }
1403
504db471
JD
1404 writer_packet = insert_new_packet(debug_it, packet, writer_stream,
1405 di_trace);
4f45f9bb
JD
1406 if (!writer_packet) {
1407 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1408 __func__, __FILE__, __LINE__);
1409 goto error;
1410 }
4f45f9bb 1411
2d64a1eb
JD
1412 packet_context = bt_ctf_packet_get_context(packet);
1413 if (packet_context) {
1414 writer_packet_context = ctf_copy_packet_context(debug_it->err,
1415 packet, writer_stream);
1416 if (!writer_packet_context) {
1417 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1418 __func__, __FILE__, __LINE__);
1419 goto error;
1420 }
1421 BT_PUT(packet_context);
4f45f9bb
JD
1422 }
1423
1424 int_ret = bt_ctf_packet_set_context(writer_packet, writer_packet_context);
1425 if (int_ret) {
1426 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1427 __func__, __FILE__, __LINE__);
1428 goto error;
1429 }
1c78e839
JD
1430
1431 bt_get(writer_packet);
4f45f9bb
JD
1432 goto end;
1433
1434error:
1435
1436end:
2d64a1eb 1437 bt_put(packet_context);
4f45f9bb
JD
1438 bt_put(writer_packet_context);
1439 bt_put(writer_stream);
1440 bt_put(stream);
1441 return writer_packet;
1442}
1443
1444BT_HIDDEN
1445struct bt_ctf_packet *debug_info_close_packet(
1446 struct debug_info_iterator *debug_it,
1447 struct bt_ctf_packet *packet)
1448{
1449 struct bt_ctf_packet *writer_packet = NULL;
504db471
JD
1450 struct bt_ctf_stream *stream = NULL;
1451 struct debug_info_trace *di_trace;
1452
1453 stream = bt_ctf_packet_get_stream(packet);
1454 if (!stream) {
1455 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
1456 __FILE__, __LINE__);
1457 goto end;
1458 }
1459
1460 di_trace = lookup_di_trace_from_stream(debug_it, stream);
1461 if (!di_trace) {
1462 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
1463 __FILE__, __LINE__);
1464 goto end;
1465 }
4f45f9bb 1466
504db471 1467 writer_packet = lookup_packet(debug_it, packet, di_trace);
4f45f9bb
JD
1468 if (!writer_packet) {
1469 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1470 __func__, __FILE__, __LINE__);
1471 goto end;
1472 }
1c78e839 1473 bt_get(writer_packet);
504db471 1474 g_hash_table_remove(di_trace->packet_map, packet);
4f45f9bb
JD
1475
1476end:
504db471 1477 bt_put(stream);
4f45f9bb
JD
1478 return writer_packet;
1479}
1480
504db471
JD
1481BT_HIDDEN
1482struct bt_ctf_stream *debug_info_stream_begin(
1483 struct debug_info_iterator *debug_it,
1484 struct bt_ctf_stream *stream)
1485{
cb0a5cf8 1486 struct bt_ctf_stream *writer_stream = NULL;
1c78e839 1487 enum debug_info_stream_state *state;
504db471
JD
1488 struct debug_info_trace *di_trace = NULL;
1489
1490 di_trace = lookup_di_trace_from_stream(debug_it, stream);
1491 if (!di_trace) {
1492 di_trace = insert_new_trace(debug_it, stream);
1493 if (!di_trace) {
1494 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1495 __func__, __FILE__, __LINE__);
1c78e839 1496 goto error;
504db471
JD
1497 }
1498 }
1499
1c78e839
JD
1500 /* Set the stream as active */
1501 state = g_hash_table_lookup(di_trace->stream_states, stream);
1502 if (!state) {
1503 if (di_trace->trace_static) {
1504 fprintf(debug_it->err, "[error] Adding a new stream "
1505 "on a static trace\n");
1506 goto error;
1507 }
1508 state = insert_new_stream_state(debug_it, di_trace,
1509 stream);
ec273a88
JD
1510 if (!state) {
1511 fprintf(debug_it->err, "[error] Adding a new stream "
1512 "on a static trace\n");
1513 goto error;
1514 }
1c78e839
JD
1515 }
1516 if (*state != DEBUG_INFO_UNKNOWN_STREAM) {
1517 fprintf(debug_it->err, "[error] Unexpected stream state %d\n",
1518 *state);
1519 goto error;
1520 }
1521 *state = DEBUG_INFO_ACTIVE_STREAM;
1522
cb0a5cf8
JD
1523 writer_stream = lookup_stream(debug_it, stream, di_trace);
1524 if (!writer_stream) {
1525 writer_stream = insert_new_stream(debug_it, stream, di_trace);
1526 }
1c78e839 1527 bt_get(writer_stream);
504db471 1528
1c78e839
JD
1529 goto end;
1530
1531error:
1532 BT_PUT(writer_stream);
504db471
JD
1533end:
1534 return writer_stream;
1535}
1536
4f45f9bb
JD
1537BT_HIDDEN
1538struct bt_ctf_stream *debug_info_stream_end(struct debug_info_iterator *debug_it,
1539 struct bt_ctf_stream *stream)
1540{
0bb3da02 1541 struct bt_ctf_stream *writer_stream = NULL;
504db471 1542 struct debug_info_trace *di_trace = NULL;
1c78e839 1543 enum debug_info_stream_state *state;
504db471
JD
1544
1545 di_trace = lookup_di_trace_from_stream(debug_it, stream);
1546 if (!di_trace) {
1547 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1548 __func__, __FILE__, __LINE__);
1c78e839 1549 goto error;
504db471 1550 }
4f45f9bb 1551
504db471 1552 writer_stream = lookup_stream(debug_it, stream, di_trace);
4f45f9bb
JD
1553 if (!writer_stream) {
1554 fprintf(debug_it->err, "[error] %s in %s:%d\n",
1555 __func__, __FILE__, __LINE__);
1c78e839 1556 goto error;
4f45f9bb 1557 }
1c78e839
JD
1558 /*
1559 * Take the ref on the stream and keep it until the notification
1560 * is created.
1561 */
34101229 1562 bt_get(writer_stream);
1c78e839
JD
1563
1564 state = g_hash_table_lookup(di_trace->stream_states, stream);
1565 if (*state != DEBUG_INFO_ACTIVE_STREAM) {
1566 fprintf(debug_it->err, "[error] Unexpected stream "
1567 "state %d\n", *state);
1568 goto error;
1569 }
1570 *state = DEBUG_INFO_COMPLETED_STREAM;
1571
504db471 1572 g_hash_table_remove(di_trace->stream_map, stream);
4f45f9bb 1573
1c78e839
JD
1574 if (di_trace->trace_static) {
1575 int trace_completed = 1;
1576
1577 g_hash_table_foreach(di_trace->stream_states,
1578 check_completed_trace, &trace_completed);
1579 if (trace_completed) {
1580 debug_info_close_trace(debug_it, di_trace);
1581 g_hash_table_remove(debug_it->trace_map,
1582 di_trace->trace);
1583 }
1584 }
1585
1586 goto end;
1587
1588error:
1589 BT_PUT(writer_stream);
1590
4f45f9bb
JD
1591end:
1592 return writer_stream;
1593}
1594
1595static
1596struct debug_info_source *lookup_debug_info(FILE *err,
1597 struct bt_ctf_event *event,
1598 struct debug_info *debug_info)
1599{
1600 int64_t vpid;
1601 uint64_t ip;
1602 struct debug_info_source *dbg_info_src = NULL;
1603 int ret;
1604
1605 ret = get_stream_event_context_int_field_value(err, event,
1606 "_vpid", &vpid);
1607 if (ret) {
1608 goto end;
1609 }
1610
1611 ret = get_stream_event_context_unsigned_int_field_value(err, event,
1612 "_ip", &ip);
1613 if (ret) {
1614 goto end;
1615 }
1616
1617 /* Get debug info for this context. */
1618 dbg_info_src = debug_info_query(debug_info, vpid, ip);
1619
1620end:
1621 return dbg_info_src;
1622}
1623
1624static
1625int set_debug_info_field(FILE *err, struct bt_ctf_field *debug_field,
1626 struct debug_info_source *dbg_info_src,
1627 struct debug_info_component *component)
1628{
2afcfbfb 1629 int i, nr_fields, ret = 0;
4f45f9bb
JD
1630 struct bt_ctf_field_type *debug_field_type = NULL;
1631 struct bt_ctf_field *field = NULL;
1632 struct bt_ctf_field_type *field_type = NULL;
1633
1634 debug_field_type = bt_ctf_field_get_type(debug_field);
1635 if (!debug_field_type) {
1636 fprintf(err, "[error] %s in %s:%d\n", __func__,
1637 __FILE__, __LINE__);
1638 goto error;
1639 }
1640
1641 nr_fields = bt_ctf_field_type_structure_get_field_count(debug_field_type);
1642 for (i = 0; i < nr_fields; i++) {
1643 const char *field_name;
1644
1645 if (bt_ctf_field_type_structure_get_field(debug_field_type,
1646 &field_name, &field_type, i) < 0) {
1647 fprintf(err, "[error] %s in %s:%d\n", __func__,
1648 __FILE__, __LINE__);
1649 goto error;
1650 }
1651 BT_PUT(field_type);
1652
1653 field = bt_ctf_field_structure_get_field_by_index(debug_field, i);
1654 if (!strcmp(field_name, "bin")) {
1655 if (dbg_info_src && dbg_info_src->bin_path) {
1656 GString *tmp = g_string_new(NULL);
1657
1658 if (component->arg_full_path) {
1659 g_string_printf(tmp, "%s%s",
1660 dbg_info_src->bin_path,
1661 dbg_info_src->bin_loc);
1662 } else {
1663 g_string_printf(tmp, "%s%s",
1664 dbg_info_src->short_bin_path,
1665 dbg_info_src->bin_loc);
1666 }
1667 ret = bt_ctf_field_string_set_value(field, tmp->str);
1668 g_string_free(tmp, true);
1669 } else {
1670 ret = bt_ctf_field_string_set_value(field, "");
1671 }
1672 } else if (!strcmp(field_name, "func")) {
1673 if (dbg_info_src && dbg_info_src->func) {
1674 ret = bt_ctf_field_string_set_value(field,
1675 dbg_info_src->func);
1676 } else {
1677 ret = bt_ctf_field_string_set_value(field, "");
1678 }
1679 } else if (!strcmp(field_name, "src")) {
1680 if (dbg_info_src && dbg_info_src->src_path) {
1681 GString *tmp = g_string_new(NULL);
1682
1683 if (component->arg_full_path) {
1684 g_string_printf(tmp, "%s:%" PRId64,
1685 dbg_info_src->src_path,
1686 dbg_info_src->line_no);
1687 } else {
1688 g_string_printf(tmp, "%s:%" PRId64,
1689 dbg_info_src->short_src_path,
1690 dbg_info_src->line_no);
1691 }
1692 ret = bt_ctf_field_string_set_value(field, tmp->str);
1693 g_string_free(tmp, true);
1694 } else {
1695 ret = bt_ctf_field_string_set_value(field, "");
1696 }
1697 }
1698 BT_PUT(field);
1699 if (ret) {
1700 fprintf(err, "[error] %s in %s:%d\n", __func__,
1701 __FILE__, __LINE__);
1702 goto error;
1703 }
1704 }
1705 ret = 0;
1706 goto end;
1707
1708error:
1709 ret = -1;
1710end:
1711 bt_put(field_type);
1712 bt_put(field);
1713 bt_put(debug_field_type);
1714 return ret;
1715}
1716
1717static
1718int copy_set_debug_info_stream_event_context(FILE *err,
1719 struct bt_ctf_field *event_context,
1720 struct bt_ctf_event *event,
1721 struct bt_ctf_event *writer_event,
1722 struct debug_info *debug_info,
1723 struct debug_info_component *component)
1724{
08b6e8e8
JD
1725 struct bt_ctf_field_type *writer_event_context_type = NULL,
1726 *event_context_type = NULL;
4f45f9bb
JD
1727 struct bt_ctf_field *writer_event_context = NULL;
1728 struct bt_ctf_field *field = NULL, *copy_field = NULL, *debug_field = NULL;
1729 struct bt_ctf_field_type *field_type = NULL;
1730 struct debug_info_source *dbg_info_src;
1731 int ret, nr_fields, i;
1732
1733 writer_event_context = bt_ctf_event_get_stream_event_context(writer_event);
1734 if (!writer_event_context) {
1735 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__, __LINE__);
1736 goto error;
1737 }
1738
1739 writer_event_context_type = bt_ctf_field_get_type(writer_event_context);
1740 if (!writer_event_context_type) {
1741 fprintf(err, "[error] %s in %s:%d\n", __func__,
1742 __FILE__, __LINE__);
1743 goto error;
1744 }
1745
08b6e8e8
JD
1746 event_context_type = bt_ctf_field_get_type(event_context);
1747 if (!event_context_type) {
1748 fprintf(err, "[error] %s in %s:%d\n", __func__,
1749 __FILE__, __LINE__);
1750 goto error;
1751 }
1752
4f45f9bb
JD
1753 /*
1754 * If it is not a structure, we did not modify it to add the debug info
1755 * fields, so just assign it as is.
1756 */
1487a16a 1757 if (bt_ctf_field_type_get_type_id(writer_event_context_type) != BT_CTF_FIELD_TYPE_ID_STRUCT) {
4f45f9bb
JD
1758 ret = bt_ctf_event_set_event_context(writer_event, event_context);
1759 goto end;
1760 }
1761
1762 dbg_info_src = lookup_debug_info(err, event, debug_info);
1763
1764 nr_fields = bt_ctf_field_type_structure_get_field_count(writer_event_context_type);
1765 for (i = 0; i < nr_fields; i++) {
1766 const char *field_name;
1767
1768 if (bt_ctf_field_type_structure_get_field(writer_event_context_type,
1769 &field_name, &field_type, i) < 0) {
1770 fprintf(err, "[error] %s in %s:%d\n", __func__,
1771 __FILE__, __LINE__);
1772 goto error;
1773 }
1774
08b6e8e8
JD
1775 /*
1776 * Prevent illegal access in the event_context.
1777 */
1778 if (i < bt_ctf_field_type_structure_get_field_count(event_context_type)) {
1779 field = bt_ctf_field_structure_get_field_by_index(event_context, i);
1780 }
4f45f9bb
JD
1781 /*
1782 * The debug_info field, only exists in the writer event or
1783 * if it was set by a earlier pass of the debug_info plugin.
4f45f9bb
JD
1784 */
1785 if (!strcmp(field_name, component->arg_debug_info_field_name) &&
1786 !field) {
1787 debug_field = bt_ctf_field_structure_get_field_by_index(
1788 writer_event_context, i);
1789 if (!debug_field) {
1790 fprintf(err, "[error] %s in %s:%d\n", __func__,
1791 __FILE__, __LINE__);
1792 goto error;
1793 }
1794 ret = set_debug_info_field(err, debug_field,
1795 dbg_info_src, component);
1796 if (ret) {
1797 fprintf(err, "[error] %s in %s:%d\n", __func__,
1798 __FILE__, __LINE__);
1799 goto error;
1800 }
1801 BT_PUT(debug_field);
1802 } else {
1803 copy_field = bt_ctf_field_copy(field);
1804 if (!copy_field) {
1805 fprintf(err, "[error] %s in %s:%d\n", __func__,
1806 __FILE__, __LINE__);
1807 goto error;
1808 }
1809
2225de6b
PP
1810 ret = bt_ctf_field_structure_set_field_by_name(
1811 writer_event_context,
4f45f9bb
JD
1812 field_name, copy_field);
1813 if (ret) {
1814 fprintf(err, "[error] %s in %s:%d\n", __func__,
1815 __FILE__, __LINE__);
1816 goto error;
1817 }
1818 BT_PUT(copy_field);
1819 }
1820 BT_PUT(field_type);
1821 BT_PUT(field);
1822 }
1823
1824 ret = 0;
1825 goto end;
1826
1827error:
1828 ret = -1;
1829end:
08b6e8e8 1830 bt_put(event_context_type);
4f45f9bb
JD
1831 bt_put(writer_event_context_type);
1832 bt_put(writer_event_context);
1833 bt_put(field);
1834 bt_put(copy_field);
1835 bt_put(debug_field);
1836 bt_put(field_type);
1837 return ret;
1838}
1839
1840static
1841struct bt_ctf_clock_class *stream_class_get_clock_class(FILE *err,
1842 struct bt_ctf_stream_class *stream_class)
1843{
1844 struct bt_ctf_trace *trace = NULL;
1845 struct bt_ctf_clock_class *clock_class = NULL;
1846
1847 trace = bt_ctf_stream_class_get_trace(stream_class);
1848 if (!trace) {
1849 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1850 __LINE__);
1851 goto end;
1852 }
1853
290b95cc
MD
1854 if (!bt_ctf_trace_get_clock_class_count(trace)) {
1855 /* No clock. */
1856 goto end;
1857 }
1858
4f45f9bb 1859 /* FIXME multi-clock? */
9ac68eb1 1860 clock_class = bt_ctf_trace_get_clock_class_by_index(trace, 0);
4f45f9bb
JD
1861
1862 bt_put(trace);
1863
1864end:
1865 return clock_class;
1866}
1867
1868static
1869struct bt_ctf_clock_class *event_get_clock_class(FILE *err, struct bt_ctf_event *event)
1870{
1871 struct bt_ctf_event_class *event_class = NULL;
1872 struct bt_ctf_stream_class *stream_class = NULL;
1873 struct bt_ctf_clock_class *clock_class = NULL;
1874
1875 event_class = bt_ctf_event_get_class(event);
1876 if (!event_class) {
1877 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1878 __LINE__);
1879 goto error;
1880 }
1881
1882 stream_class = bt_ctf_event_class_get_stream_class(event_class);
1883 if (!stream_class) {
1884 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1885 __LINE__);
1886 goto error;
1887 }
1888
1889 clock_class = stream_class_get_clock_class(err, stream_class);
1890 goto end;
1891
1892error:
1893 BT_PUT(clock_class);
1894end:
1895 bt_put(stream_class);
1896 bt_put(event_class);
1897 return clock_class;
1898}
1899
1900static
1901int set_event_clock_value(FILE *err, struct bt_ctf_event *event,
1902 struct bt_ctf_event *writer_event)
1903{
1904 struct bt_ctf_clock_class *clock_class = NULL;
1905 struct bt_ctf_clock_value *clock_value = NULL;
1081db08 1906 int ret = 0;
4f45f9bb
JD
1907
1908 clock_class = event_get_clock_class(err, event);
1909 if (!clock_class) {
290b95cc
MD
1910 /* No clock on input trace. */
1911 goto end;
4f45f9bb
JD
1912 }
1913
1914 clock_value = bt_ctf_event_get_clock_value(event, clock_class);
1915 if (!clock_value) {
1916 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1917 __LINE__);
1918 goto error;
1919 }
1920
1921 /*
1922 * We share the same clocks, so we can assign the clock value to the
1923 * writer event.
1924 */
1925 ret = bt_ctf_event_set_clock_value(writer_event, clock_value);
1926 if (ret) {
1927 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1928 __LINE__);
1929 goto error;
1930 }
1931
1932 ret = 0;
1933 goto end;
1934
1935error:
1936 ret = -1;
1937end:
1938 bt_put(clock_class);
1939 bt_put(clock_value);
1940 return ret;
1941}
1942
1943static
1944struct bt_ctf_event *debug_info_copy_event(FILE *err, struct bt_ctf_event *event,
1945 struct bt_ctf_event_class *writer_event_class,
1946 struct debug_info *debug_info,
1947 struct debug_info_component *component)
1948{
1949 struct bt_ctf_event *writer_event = NULL;
f6f999a3 1950 struct bt_ctf_field *field = NULL, *copy_field = NULL;
4f45f9bb
JD
1951 int ret;
1952
1953 writer_event = bt_ctf_event_create(writer_event_class);
1954 if (!writer_event) {
1955 fprintf(err, "[error] %s in %s:%d\n", __func__, __FILE__,
1956 __LINE__);
1957 goto error;
1958 }
1959
1960 ret = set_event_clock_value(err, event, writer_event);
1961 if (ret) {
1962 fprintf(err, "[error] %s in %s:%d\n", __func__,
1963 __FILE__, __LINE__);
1964 goto error;
1965 }
1966
60ef553b 1967 /* Optional field, so it can fail silently. */
4f45f9bb 1968 field = bt_ctf_event_get_header(event);
60ef553b
JD
1969 if (field) {
1970 ret = ctf_copy_event_header(err, event, writer_event_class,
1971 writer_event, field);
1972 if (ret) {
1973 fprintf(err, "[error] %s in %s:%d\n", __func__,
1974 __FILE__, __LINE__);
1975 goto error;
1976 }
1977 BT_PUT(field);
4f45f9bb 1978 }
4f45f9bb
JD
1979
1980 /* Optional field, so it can fail silently. */
1981 field = bt_ctf_event_get_stream_event_context(event);
1982 if (field) {
1983 ret = copy_set_debug_info_stream_event_context(err,
1984 field, event, writer_event, debug_info,
1985 component);
1986 if (ret < 0) {
1987 fprintf(err, "[error] %s in %s:%d\n", __func__,
1988 __FILE__, __LINE__);
1989 goto error;
1990 }
1991 BT_PUT(field);
1992 }
1993
1994 /* Optional field, so it can fail silently. */
1995 field = bt_ctf_event_get_event_context(event);
60ef553b
JD
1996 if (field) {
1997 copy_field = bt_ctf_field_copy(field);
1998 if (!copy_field) {
1999 fprintf(err, "[error] %s in %s:%d\n", __func__,
2000 __FILE__, __LINE__);
2001 goto error;
2002 }
4f45f9bb
JD
2003 ret = bt_ctf_event_set_event_context(writer_event, copy_field);
2004 if (ret < 0) {
2005 fprintf(err, "[error] %s in %s:%d\n", __func__,
2006 __FILE__, __LINE__);
2007 goto error;
2008 }
2009 BT_PUT(copy_field);
60ef553b 2010 BT_PUT(field);
4f45f9bb 2011 }
4f45f9bb 2012
9ac68eb1 2013 field = bt_ctf_event_get_event_payload(event);
4f45f9bb
JD
2014 if (!field) {
2015 fprintf(err, "[error] %s in %s:%d\n", __func__,
2016 __FILE__, __LINE__);
2017 goto error;
2018 }
2019 copy_field = bt_ctf_field_copy(field);
2020 if (copy_field) {
9ac68eb1 2021 ret = bt_ctf_event_set_event_payload(writer_event, copy_field);
4f45f9bb
JD
2022 if (ret < 0) {
2023 fprintf(err, "[error] %s in %s:%d\n", __func__,
2024 __FILE__, __LINE__);
2025 goto error;
2026 }
2027 BT_PUT(copy_field);
2028 }
2029 BT_PUT(field);
2030
2031 goto end;
2032
2033error:
2034 BT_PUT(writer_event);
2035end:
2036 bt_put(copy_field);
2037 bt_put(field);
2038 return writer_event;
2039}
2040
2041BT_HIDDEN
2042struct bt_ctf_event *debug_info_output_event(
2043 struct debug_info_iterator *debug_it,
2044 struct bt_ctf_event *event)
2045{
2046 struct bt_ctf_event_class *event_class = NULL, *writer_event_class = NULL;
2047 struct bt_ctf_stream_class *stream_class = NULL, *writer_stream_class = NULL;
2048 struct bt_ctf_event *writer_event = NULL;
f6f999a3 2049 struct bt_ctf_packet *packet = NULL, *writer_packet = NULL;
4f45f9bb 2050 struct bt_ctf_trace *writer_trace = NULL;
504db471
JD
2051 struct bt_ctf_stream *stream = NULL;
2052 struct debug_info_trace *di_trace;
4f45f9bb
JD
2053 struct debug_info *debug_info;
2054 const char *event_name;
2055 int int_ret;
2056
2057 event_class = bt_ctf_event_get_class(event);
2058 if (!event_class) {
2059 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2060 __FILE__, __LINE__);
2061 goto error;
2062 }
2063
2064 event_name = bt_ctf_event_class_get_name(event_class);
2065 if (!event_name) {
2066 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2067 __FILE__, __LINE__);
2068 goto error;
2069 }
2070
2071 stream_class = bt_ctf_event_class_get_stream_class(event_class);
2072 if (!stream_class) {
2073 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2074 __FILE__, __LINE__);
2075 goto error;
2076 }
504db471
JD
2077 stream = bt_ctf_event_get_stream(event);
2078 if (!stream) {
2079 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2080 __FILE__, __LINE__);
2081 goto error;
2082 }
2083 di_trace = lookup_di_trace_from_stream(debug_it, stream);
2084 if (!di_trace) {
2085 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2086 __FILE__, __LINE__);
2087 goto error;
2088 }
4f45f9bb
JD
2089
2090 writer_stream_class = g_hash_table_lookup(
504db471 2091 di_trace->stream_class_map,
4f45f9bb 2092 (gpointer) stream_class);
504db471 2093 if (!writer_stream_class) {
4f45f9bb
JD
2094 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2095 __FILE__, __LINE__);
2096 goto error;
2097 }
504db471 2098 bt_get(writer_stream_class);
4f45f9bb
JD
2099
2100 writer_event_class = get_event_class(debug_it,
2101 writer_stream_class, event_class);
2102 if (!writer_event_class) {
2103 writer_event_class = ctf_copy_event_class(debug_it->err,
2104 event_class);
2105 if (!writer_event_class) {
2106 fprintf(debug_it->err, "[error] %s in %s:%d\n",
2107 __func__, __FILE__, __LINE__);
2108 goto error;
2109 }
2110 int_ret = bt_ctf_stream_class_add_event_class(
2111 writer_stream_class, writer_event_class);
2112 if (int_ret) {
2113 fprintf(debug_it->err, "[error] %s in %s:%d\n",
2114 __func__, __FILE__, __LINE__);
2115 goto error;
2116 }
2117 }
2118
2119 writer_trace = bt_ctf_stream_class_get_trace(writer_stream_class);
2120 if (!writer_trace) {
2121 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2122 __FILE__, __LINE__);
2123 goto error;
2124 }
2125
504db471 2126 debug_info = get_trace_debug_info(debug_it, writer_trace, di_trace);
4f45f9bb
JD
2127 if (debug_info) {
2128 debug_info_handle_event(debug_it->err, event, debug_info);
2129 }
2130
2131 writer_event = debug_info_copy_event(debug_it->err, event,
2132 writer_event_class, debug_info,
2133 debug_it->debug_info_component);
2134 if (!writer_event) {
2135 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2136 __FILE__, __LINE__);
2137 fprintf(debug_it->err, "[error] Failed to copy event %s\n",
2138 bt_ctf_event_class_get_name(writer_event_class));
2139 goto error;
2140 }
2141
2142 packet = bt_ctf_event_get_packet(event);
2143 if (!packet) {
2144 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2145 __FILE__, __LINE__);
2146 goto error;
2147 }
2148
504db471 2149 writer_packet = lookup_packet(debug_it, packet, di_trace);
4f45f9bb
JD
2150 if (!writer_packet) {
2151 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2152 __FILE__, __LINE__);
2153 goto error;
2154 }
2155 bt_get(writer_packet);
2156
2157 int_ret = bt_ctf_event_set_packet(writer_event, writer_packet);
2158 if (int_ret < 0) {
2159 fprintf(debug_it->err, "[error] %s in %s:%d\n", __func__,
2160 __FILE__, __LINE__);
2161 fprintf(debug_it->err, "[error] Failed to append event %s\n",
2162 bt_ctf_event_class_get_name(writer_event_class));
2163 goto error;
2164 }
2165
2166 /* Keep the reference on the writer event */
2167 goto end;
2168
2169error:
2170 BT_PUT(writer_event);
2171
2172end:
504db471 2173 bt_put(stream);
4f45f9bb
JD
2174 bt_put(writer_trace);
2175 bt_put(writer_packet);
2176 bt_put(packet);
2177 bt_put(writer_event_class);
2178 bt_put(writer_stream_class);
2179 bt_put(stream_class);
2180 bt_put(event_class);
2181 return writer_event;
2182}
This page took 0.116044 seconds and 4 git commands to generate.