lib: strictly type function return status enumerations
[babeltrace.git] / src / plugins / lttng-utils / debug-info / debug-info.c
CommitLineData
4f45f9bb
JD
1/*
2 * Babeltrace - Debug Information State Tracker
3 *
4 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
5 * Copyright (c) 2015 Philippe Proulx <pproulx@efficios.com>
6 * Copyright (c) 2015 Antoine Busque <abusque@efficios.com>
7 * Copyright (c) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
ca9f27f3 8 * Copyright (c) 2019 Francis Deslauriers <francis.deslauriers@efficios.com>
4f45f9bb
JD
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
91bc8451 29#define BT_COMP_LOG_SELF_COMP self_comp
3a3d15f3 30#define BT_LOG_OUTPUT_LEVEL log_level
350ad6c1 31#define BT_LOG_TAG "PLUGIN/FLT.LTTNG-UTILS.DEBUG-INFO"
91bc8451 32#include "plugins/comp-logging.h"
b4565e8b 33
4f45f9bb 34#include <glib.h>
ca9f27f3 35
578e048b
MJ
36#include "common/assert.h"
37#include "common/common.h"
38#include "fd-cache/fd-cache.h"
ca9f27f3 39
4f45f9bb 40#include "bin-info.h"
ca9f27f3
FD
41#include "debug-info.h"
42#include "trace-ir-data-copy.h"
43#include "trace-ir-mapping.h"
44#include "trace-ir-metadata-copy.h"
4f45f9bb 45#include "utils.h"
ca9f27f3
FD
46
47#define DEFAULT_DEBUG_INFO_FIELD_NAME "debug_info"
48#define LTTNG_UST_STATEDUMP_PREFIX "lttng_ust"
49#define VPID_FIELD_NAME "vpid"
50#define IP_FIELD_NAME "ip"
51#define BADDR_FIELD_NAME "baddr"
52#define CRC32_FIELD_NAME "crc"
53#define BUILD_ID_FIELD_NAME "build_id"
54#define FILENAME_FIELD_NAME "filename"
55#define IS_PIC_FIELD_NAME "is_pic"
56#define MEMSZ_FIELD_NAME "memsz"
57#define PATH_FIELD_NAME "path"
58
59struct debug_info_component {
3a3d15f3 60 bt_logging_level log_level;
91bc8451 61 bt_self_component *self_comp;
06d1cf5d
FD
62 gchar *arg_debug_dir;
63 gchar *arg_debug_info_field_name;
64 gchar *arg_target_prefix;
65 bt_bool arg_full_path;
ca9f27f3
FD
66};
67
68struct debug_info_msg_iter {
3a3d15f3 69 bt_logging_level log_level;
ca9f27f3
FD
70 struct debug_info_component *debug_info_component;
71 bt_self_message_iterator *input_iterator;
72 bt_self_component *self_comp;
73 bt_self_component_port_input_message_iterator *msg_iter;
74
75 struct trace_ir_maps *ir_maps;
76 /* in_trace -> debug_info_mapping. */
77 GHashTable *debug_info_map;
1e638f98
FD
78
79 struct bt_fd_cache fd_cache;
ca9f27f3
FD
80};
81
82struct debug_info_source {
83 /* Strings are owned by debug_info_source. */
06d1cf5d 84 gchar *func;
ca9f27f3
FD
85 /*
86 * Store the line number as a string so that the allocation and
87 * conversion to string is only done once.
88 */
06d1cf5d
FD
89 gchar *line_no;
90 gchar *src_path;
ca9f27f3 91 /* short_src_path points inside src_path, no need to free. */
06d1cf5d
FD
92 const gchar *short_src_path;
93 gchar *bin_path;
ca9f27f3 94 /* short_bin_path points inside bin_path, no need to free. */
06d1cf5d 95 const gchar *short_bin_path;
ca9f27f3
FD
96 /*
97 * Location within the binary. Either absolute (@0x1234) or
98 * relative (+0x4321).
99 */
06d1cf5d 100 gchar *bin_loc;
ca9f27f3 101};
4f45f9bb
JD
102
103struct proc_debug_info_sources {
104 /*
105 * Hash table: base address (pointer to uint64_t) to bin info; owned by
106 * proc_debug_info_sources.
107 */
108 GHashTable *baddr_to_bin_info;
109
110 /*
111 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
112 * owned by proc_debug_info_sources.
113 */
114 GHashTable *ip_to_debug_info_src;
115};
116
117struct debug_info {
3a3d15f3 118 bt_logging_level log_level;
91bc8451 119 bt_self_component *self_comp;
9d325e17 120 struct debug_info_component *comp;
ca9f27f3
FD
121 const bt_trace *input_trace;
122 uint64_t destruction_listener_id;
9d325e17 123
4f45f9bb
JD
124 /*
125 * Hash table of VPIDs (pointer to int64_t) to
f9597f41 126 * (struct proc_debug_info_sources*); owned by debug_info.
4f45f9bb
JD
127 */
128 GHashTable *vpid_to_proc_dbg_info_src;
129 GQuark q_statedump_bin_info;
130 GQuark q_statedump_debug_link;
131 GQuark q_statedump_build_id;
132 GQuark q_statedump_start;
133 GQuark q_dl_open;
134 GQuark q_lib_load;
135 GQuark q_lib_unload;
1e638f98 136 struct bt_fd_cache *fd_cache; /* Weak ref. Owned by the iterator. */
4f45f9bb
JD
137};
138
139static
140int debug_info_init(struct debug_info *info)
141{
142 info->q_statedump_bin_info = g_quark_from_string(
143 "lttng_ust_statedump:bin_info");
144 info->q_statedump_debug_link = g_quark_from_string(
ca9f27f3 145 "lttng_ust_statedump:debug_link");
4f45f9bb
JD
146 info->q_statedump_build_id = g_quark_from_string(
147 "lttng_ust_statedump:build_id");
148 info->q_statedump_start = g_quark_from_string(
149 "lttng_ust_statedump:start");
150 info->q_dl_open = g_quark_from_string("lttng_ust_dl:dlopen");
151 info->q_lib_load = g_quark_from_string("lttng_ust_lib:load");
152 info->q_lib_unload = g_quark_from_string("lttng_ust_lib:unload");
153
91bc8451 154 return bin_info_init(info->log_level, info->self_comp);
4f45f9bb
JD
155}
156
157static
158void debug_info_source_destroy(struct debug_info_source *debug_info_src)
159{
160 if (!debug_info_src) {
161 return;
162 }
163
06d1cf5d
FD
164 g_free(debug_info_src->func);
165 g_free(debug_info_src->line_no);
166 g_free(debug_info_src->src_path);
167 g_free(debug_info_src->bin_path);
168 g_free(debug_info_src->bin_loc);
4f45f9bb
JD
169 g_free(debug_info_src);
170}
171
172static
ca9f27f3 173struct debug_info_source *debug_info_source_create_from_bin(
91bc8451
PP
174 struct bin_info *bin, uint64_t ip,
175 bt_self_component *self_comp)
4f45f9bb
JD
176{
177 int ret;
178 struct debug_info_source *debug_info_src = NULL;
179 struct source_location *src_loc = NULL;
3a3d15f3 180 bt_logging_level log_level = bin->log_level;
4f45f9bb
JD
181
182 debug_info_src = g_new0(struct debug_info_source, 1);
183
184 if (!debug_info_src) {
185 goto end;
186 }
187
188 /* Lookup function name */
189 ret = bin_info_lookup_function_name(bin, ip, &debug_info_src->func);
190 if (ret) {
191 goto error;
192 }
193
194 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
195 if (!bin->is_elf_only || !debug_info_src->func) {
196 /* Lookup source location */
197 ret = bin_info_lookup_source_location(bin, ip, &src_loc);
ca9f27f3 198 if (ret) {
91bc8451 199 BT_COMP_LOGI("Failed to lookup source location: ret=%d", ret);
ca9f27f3 200 }
4f45f9bb
JD
201 }
202
203 if (src_loc) {
06d1cf5d
FD
204 debug_info_src->line_no =
205 g_strdup_printf("%"PRId64, src_loc->line_no);
206 if (!debug_info_src->line_no) {
91bc8451 207 BT_COMP_LOGE("Error occured when setting line_no field.");
ca9f27f3
FD
208 goto error;
209 }
4f45f9bb
JD
210
211 if (src_loc->filename) {
06d1cf5d 212 debug_info_src->src_path = g_strdup(src_loc->filename);
4f45f9bb
JD
213 if (!debug_info_src->src_path) {
214 goto error;
215 }
216
217 debug_info_src->short_src_path = get_filename_from_path(
218 debug_info_src->src_path);
219 }
4f45f9bb
JD
220 source_location_destroy(src_loc);
221 }
222
223 if (bin->elf_path) {
06d1cf5d 224 debug_info_src->bin_path = g_strdup(bin->elf_path);
4f45f9bb
JD
225 if (!debug_info_src->bin_path) {
226 goto error;
227 }
228
229 debug_info_src->short_bin_path = get_filename_from_path(
230 debug_info_src->bin_path);
231
232 ret = bin_info_get_bin_loc(bin, ip, &(debug_info_src->bin_loc));
233 if (ret) {
234 goto error;
235 }
236 }
237
238end:
239 return debug_info_src;
240
241error:
242 debug_info_source_destroy(debug_info_src);
243 return NULL;
244}
245
246static
247void proc_debug_info_sources_destroy(
248 struct proc_debug_info_sources *proc_dbg_info_src)
249{
250 if (!proc_dbg_info_src) {
251 return;
252 }
253
254 if (proc_dbg_info_src->baddr_to_bin_info) {
255 g_hash_table_destroy(proc_dbg_info_src->baddr_to_bin_info);
256 }
257
258 if (proc_dbg_info_src->ip_to_debug_info_src) {
259 g_hash_table_destroy(proc_dbg_info_src->ip_to_debug_info_src);
260 }
261
262 g_free(proc_dbg_info_src);
263}
264
265static
266struct proc_debug_info_sources *proc_debug_info_sources_create(void)
267{
268 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
269
270 proc_dbg_info_src = g_new0(struct proc_debug_info_sources, 1);
271 if (!proc_dbg_info_src) {
272 goto end;
273 }
274
275 proc_dbg_info_src->baddr_to_bin_info = g_hash_table_new_full(
276 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
277 (GDestroyNotify) bin_info_destroy);
278 if (!proc_dbg_info_src->baddr_to_bin_info) {
279 goto error;
280 }
281
282 proc_dbg_info_src->ip_to_debug_info_src = g_hash_table_new_full(
283 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
284 (GDestroyNotify) debug_info_source_destroy);
285 if (!proc_dbg_info_src->ip_to_debug_info_src) {
286 goto error;
287 }
288
289end:
290 return proc_dbg_info_src;
291
292error:
293 proc_debug_info_sources_destroy(proc_dbg_info_src);
294 return NULL;
295}
296
297static
298struct proc_debug_info_sources *proc_debug_info_sources_ht_get_entry(
299 GHashTable *ht, int64_t vpid)
300{
301 gpointer key = g_new0(int64_t, 1);
302 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
303
304 if (!key) {
305 goto end;
306 }
307
308 *((int64_t *) key) = vpid;
309
310 /* Exists? Return it */
311 proc_dbg_info_src = g_hash_table_lookup(ht, key);
312 if (proc_dbg_info_src) {
313 goto end;
314 }
315
316 /* Otherwise, create and return it */
317 proc_dbg_info_src = proc_debug_info_sources_create();
318 if (!proc_dbg_info_src) {
319 goto end;
320 }
321
322 g_hash_table_insert(ht, key, proc_dbg_info_src);
323 /* Ownership passed to ht */
324 key = NULL;
325end:
326 g_free(key);
327 return proc_dbg_info_src;
328}
329
ca9f27f3
FD
330static inline
331const bt_field *event_borrow_payload_field(const bt_event *event,
332 const char *field_name)
333{
334 const bt_field *event_payload, *field;
335
336 event_payload = bt_event_borrow_payload_field_const(event);
337 BT_ASSERT(event_payload);
338
339 field = bt_field_structure_borrow_member_field_by_name_const(
340 event_payload, field_name);
341 return field;
342}
343
344static inline
345const bt_field *event_borrow_common_context_field(const bt_event *event,
346 const char *field_name)
347{
348 const bt_field *event_common_ctx, *field = NULL;
349
350 event_common_ctx = bt_event_borrow_common_context_field_const(event);
351 if (!event_common_ctx) {
352 goto end;
353 }
354
355 field = bt_field_structure_borrow_member_field_by_name_const(
356 event_common_ctx, field_name);
357
358end:
359 return field;
360}
361
362static inline
363void event_get_common_context_signed_integer_field_value(
364 const bt_event *event, const char *field_name, int64_t *value)
365{
366 *value = bt_field_signed_integer_get_value(
367 event_borrow_common_context_field(event, field_name));
368}
369
370static inline
371int event_get_payload_build_id_length(const bt_event *event,
372 const char *field_name, uint64_t *build_id_len)
373{
374 const bt_field *build_id_field;
375 const bt_field_class *build_id_field_class;
376
377 build_id_field = event_borrow_payload_field(event, field_name);
378 build_id_field_class = bt_field_borrow_class_const(build_id_field);
379
380 BT_ASSERT(bt_field_class_get_type(build_id_field_class) ==
381 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
382 BT_ASSERT(bt_field_class_get_type(
383 bt_field_class_array_borrow_element_field_class_const(
384 build_id_field_class)) ==
385 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
386
387 *build_id_len = bt_field_array_get_length(build_id_field);
388
389 return 0;
390}
391
392static inline
393int event_get_payload_build_id_value(const bt_event *event,
394 const char *field_name, uint8_t *build_id)
395{
396 const bt_field *curr_field, *build_id_field;
397 const bt_field_class *build_id_field_class;
398 uint64_t i, build_id_len;
399 int ret;
400
401 ret = 0;
402
403 build_id_field = event_borrow_payload_field(event, field_name);
404 build_id_field_class = bt_field_borrow_class_const(build_id_field);
405
406 BT_ASSERT(bt_field_class_get_type(build_id_field_class) ==
407 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
408 BT_ASSERT(bt_field_class_get_type(
409 bt_field_class_array_borrow_element_field_class_const(
410 build_id_field_class)) ==
411 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
412
413 build_id_len = bt_field_array_get_length(build_id_field);
414
415 for (i = 0; i < build_id_len; i++) {
416 curr_field =
417 bt_field_array_borrow_element_field_by_index_const(
418 build_id_field, i);
419
420 build_id[i] = bt_field_unsigned_integer_get_value(curr_field);
421 }
422
423 return ret;
424}
425
426static
427void event_get_payload_unsigned_integer_field_value(const bt_event *event,
428 const char *field_name, uint64_t *value)
429{
430 *value = bt_field_unsigned_integer_get_value(
431 event_borrow_payload_field(event, field_name));
432}
433
434static
435void event_get_payload_string_field_value(const bt_event *event,
436 const char *field_name, const char **value)
437{
438 *value = bt_field_string_get_value(
439 event_borrow_payload_field(event, field_name));
440}
441
442static inline
443bool event_has_payload_field(const bt_event *event,
444 const char *field_name)
445{
446 return event_borrow_payload_field(event, field_name) != NULL;
447}
448
4f45f9bb
JD
449static
450struct debug_info_source *proc_debug_info_sources_get_entry(
3a3d15f3 451 struct debug_info *debug_info,
4f45f9bb
JD
452 struct proc_debug_info_sources *proc_dbg_info_src, uint64_t ip)
453{
454 struct debug_info_source *debug_info_src = NULL;
455 gpointer key = g_new0(uint64_t, 1);
456 GHashTableIter iter;
457 gpointer baddr, value;
458
459 if (!key) {
460 goto end;
461 }
462
463 *((uint64_t *) key) = ip;
464
465 /* Look in IP to debug infos hash table first. */
466 debug_info_src = g_hash_table_lookup(
467 proc_dbg_info_src->ip_to_debug_info_src,
468 key);
469 if (debug_info_src) {
470 goto end;
471 }
472
473 /* Check in all bin_infos. */
474 g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_bin_info);
475
476 while (g_hash_table_iter_next(&iter, &baddr, &value))
477 {
478 struct bin_info *bin = value;
479
480 if (!bin_info_has_address(value, ip)) {
481 continue;
482 }
483
484 /*
485 * Found; add it to cache.
486 *
487 * FIXME: this should be bounded in size (and implement
488 * a caching policy), and entries should be prunned when
489 * libraries are unmapped.
490 */
91bc8451
PP
491 debug_info_src = debug_info_source_create_from_bin(bin, ip,
492 debug_info->self_comp);
4f45f9bb
JD
493 if (debug_info_src) {
494 g_hash_table_insert(
495 proc_dbg_info_src->ip_to_debug_info_src,
496 key, debug_info_src);
497 /* Ownership passed to ht. */
498 key = NULL;
499 }
500 break;
501 }
502
503end:
504 free(key);
505 return debug_info_src;
506}
507
1e638f98 508static
4f45f9bb
JD
509struct debug_info_source *debug_info_query(struct debug_info *debug_info,
510 int64_t vpid, uint64_t ip)
511{
512 struct debug_info_source *dbg_info_src = NULL;
513 struct proc_debug_info_sources *proc_dbg_info_src;
514
515 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
516 debug_info->vpid_to_proc_dbg_info_src, vpid);
517 if (!proc_dbg_info_src) {
518 goto end;
519 }
520
3a3d15f3
PP
521 dbg_info_src = proc_debug_info_sources_get_entry(debug_info,
522 proc_dbg_info_src, ip);
4f45f9bb
JD
523
524end:
525 return dbg_info_src;
526}
527
1e638f98 528static
ca9f27f3 529struct debug_info *debug_info_create(struct debug_info_component *comp,
1e638f98 530 const bt_trace *trace, struct bt_fd_cache *fdc)
4f45f9bb
JD
531{
532 int ret;
533 struct debug_info *debug_info;
534
1e638f98
FD
535 BT_ASSERT(comp);
536 BT_ASSERT(trace);
537 BT_ASSERT(fdc);
538
4f45f9bb
JD
539 debug_info = g_new0(struct debug_info, 1);
540 if (!debug_info) {
541 goto end;
542 }
543
3a3d15f3 544 debug_info->log_level = comp->log_level;
91bc8451 545 debug_info->self_comp = comp->self_comp;
4f45f9bb
JD
546 debug_info->vpid_to_proc_dbg_info_src = g_hash_table_new_full(
547 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
548 (GDestroyNotify) proc_debug_info_sources_destroy);
549 if (!debug_info->vpid_to_proc_dbg_info_src) {
550 goto error;
551 }
552
9d325e17 553 debug_info->comp = comp;
4f45f9bb
JD
554 ret = debug_info_init(debug_info);
555 if (ret) {
556 goto error;
557 }
558
ca9f27f3 559 debug_info->input_trace = trace;
1e638f98 560 debug_info->fd_cache = fdc;
ca9f27f3 561
4f45f9bb
JD
562end:
563 return debug_info;
564error:
565 g_free(debug_info);
566 return NULL;
567}
568
1e638f98 569static
4f45f9bb
JD
570void debug_info_destroy(struct debug_info *debug_info)
571{
3a3d15f3 572 bt_logging_level log_level;
91bc8451 573 bt_self_component *self_comp;
d24d5663 574 bt_trace_remove_listener_status remove_listener_status;
4f45f9bb
JD
575 if (!debug_info) {
576 goto end;
577 }
578
3a3d15f3 579 log_level = debug_info->log_level;
91bc8451 580 self_comp = debug_info->self_comp;
3a3d15f3 581
4f45f9bb
JD
582 if (debug_info->vpid_to_proc_dbg_info_src) {
583 g_hash_table_destroy(debug_info->vpid_to_proc_dbg_info_src);
584 }
585
d24d5663
PP
586 remove_listener_status = bt_trace_remove_destruction_listener(
587 debug_info->input_trace,
ca9f27f3 588 debug_info->destruction_listener_id);
d24d5663 589 if (remove_listener_status != BT_TRACE_REMOVE_LISTENER_STATUS_OK) {
91bc8451 590 BT_COMP_LOGE("Trace destruction listener removal failed.");
3b40fbf9 591 }
ca9f27f3 592
4f45f9bb
JD
593 g_free(debug_info);
594end:
595 return;
596}
597
598static
ca9f27f3 599void handle_event_statedump_build_id(struct debug_info *debug_info,
b19ff26f 600 const bt_event *event)
4f45f9bb
JD
601{
602 struct proc_debug_info_sources *proc_dbg_info_src;
ca9f27f3
FD
603 uint64_t build_id_len, baddr;
604 uint8_t *build_id = NULL;
605 struct bin_info *bin;
4f45f9bb 606 int64_t vpid;
ca9f27f3 607 int ret = 0;
4f45f9bb 608
ca9f27f3
FD
609 event_get_common_context_signed_integer_field_value(event,
610 VPID_FIELD_NAME, &vpid);
611 event_get_payload_unsigned_integer_field_value(event,
612 BADDR_FIELD_NAME, &baddr);
4f45f9bb
JD
613
614 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
615 debug_info->vpid_to_proc_dbg_info_src, vpid);
616 if (!proc_dbg_info_src) {
617 goto end;
618 }
619
620 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
621 (gpointer) &baddr);
622 if (!bin) {
623 /*
624 * The build_id event comes after the bin has been
625 * created. If it isn't found, just ignore this event.
626 */
627 goto end;
628 }
ca9f27f3
FD
629 ret = event_get_payload_build_id_length(event, BUILD_ID_FIELD_NAME,
630 &build_id_len);
631
632 build_id = g_new0(uint8_t, build_id_len);
633 if (!build_id) {
634 goto end;
635 }
4f45f9bb 636
ca9f27f3
FD
637 ret = event_get_payload_build_id_value(event, BUILD_ID_FIELD_NAME,
638 build_id);
4f45f9bb 639 if (ret) {
4f45f9bb
JD
640 goto end;
641 }
ca9f27f3
FD
642
643 ret = bin_info_set_build_id(bin, build_id, build_id_len);
644 if (ret) {
645 goto end;
445e79d6 646 }
4f45f9bb
JD
647
648 /*
649 * Reset the is_elf_only flag in case it had been set
650 * previously, because we might find separate debug info using
651 * the new build id information.
652 */
653 bin->is_elf_only = false;
654
4f45f9bb 655end:
ca9f27f3 656 g_free(build_id);
4f45f9bb
JD
657 return;
658}
659
660static
ca9f27f3 661void handle_event_statedump_debug_link(struct debug_info *debug_info,
b19ff26f 662 const bt_event *event)
4f45f9bb
JD
663{
664 struct proc_debug_info_sources *proc_dbg_info_src;
665 struct bin_info *bin = NULL;
666 int64_t vpid;
667 uint64_t baddr;
668 const char *filename = NULL;
669 uint32_t crc32;
ca9f27f3 670 uint64_t crc_field_value;
4f45f9bb 671
ca9f27f3 672 event_get_common_context_signed_integer_field_value(event,
c4838cef 673 VPID_FIELD_NAME, &vpid);
4f45f9bb 674
ca9f27f3
FD
675 event_get_payload_unsigned_integer_field_value(event,
676 BADDR_FIELD_NAME, &baddr);
4f45f9bb 677
ca9f27f3
FD
678 event_get_payload_unsigned_integer_field_value(event,
679 CRC32_FIELD_NAME, &crc_field_value);
4f45f9bb 680
ca9f27f3
FD
681 crc32 = (uint32_t) crc_field_value;
682
683 event_get_payload_string_field_value(event,
684 FILENAME_FIELD_NAME, &filename);
4f45f9bb
JD
685
686 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
687 debug_info->vpid_to_proc_dbg_info_src, vpid);
688 if (!proc_dbg_info_src) {
689 goto end;
690 }
691
692 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
693 (gpointer) &baddr);
694 if (!bin) {
695 /*
696 * The debug_link event comes after the bin has been
697 * created. If it isn't found, just ignore this event.
698 */
699 goto end;
700 }
701
702 bin_info_set_debug_link(bin, filename, crc32);
703
704end:
705 return;
706}
707
708static
ca9f27f3 709void handle_bin_info_event(struct debug_info *debug_info,
b19ff26f 710 const bt_event *event, bool has_pic_field)
4f45f9bb
JD
711{
712 struct proc_debug_info_sources *proc_dbg_info_src;
713 struct bin_info *bin;
714 uint64_t baddr, memsz;
715 int64_t vpid;
716 const char *path;
717 gpointer key = NULL;
718 bool is_pic;
4f45f9bb 719
ca9f27f3
FD
720 event_get_payload_unsigned_integer_field_value(event,
721 MEMSZ_FIELD_NAME, &memsz);
722 if (memsz == 0) {
723 /* Ignore VDSO. */
4f45f9bb
JD
724 goto end;
725 }
726
ca9f27f3
FD
727 event_get_payload_unsigned_integer_field_value(event,
728 BADDR_FIELD_NAME, &baddr);
4f45f9bb 729
65db8b88
JD
730 /*
731 * This field is not produced by the dlopen event emitted before
732 * lttng-ust 2.9.
733 */
ca9f27f3 734 if (!event_has_payload_field(event, PATH_FIELD_NAME)) {
4f45f9bb
JD
735 goto end;
736 }
ca9f27f3 737 event_get_payload_string_field_value(event, PATH_FIELD_NAME, &path);
4f45f9bb
JD
738
739 if (has_pic_field) {
ca9f27f3 740 uint64_t is_pic_field_value;
4f45f9bb 741
ca9f27f3 742 event_get_payload_unsigned_integer_field_value(event,
1e638f98 743 IS_PIC_FIELD_NAME, &is_pic_field_value);
ca9f27f3 744 is_pic = is_pic_field_value == 1;
4f45f9bb
JD
745 } else {
746 /*
747 * dlopen has no is_pic field, because the shared
748 * object is always PIC.
749 */
750 is_pic = true;
751 }
752
ca9f27f3 753 event_get_common_context_signed_integer_field_value(event,
c4838cef 754 VPID_FIELD_NAME, &vpid);
4f45f9bb
JD
755
756 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
1e638f98 757 debug_info->vpid_to_proc_dbg_info_src, vpid);
4f45f9bb
JD
758 if (!proc_dbg_info_src) {
759 goto end;
760 }
761
762 key = g_new0(uint64_t, 1);
763 if (!key) {
764 goto end;
765 }
766
767 *((uint64_t *) key) = baddr;
768
1e638f98 769 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info, key);
4f45f9bb
JD
770 if (bin) {
771 goto end;
772 }
773
1e638f98 774 bin = bin_info_create(debug_info->fd_cache, path, baddr, memsz, is_pic,
9d325e17 775 debug_info->comp->arg_debug_dir,
3a3d15f3 776 debug_info->comp->arg_target_prefix,
91bc8451 777 debug_info->log_level, debug_info->self_comp);
4f45f9bb
JD
778 if (!bin) {
779 goto end;
780 }
781
1e638f98 782 g_hash_table_insert(proc_dbg_info_src->baddr_to_bin_info, key, bin);
4f45f9bb
JD
783 /* Ownership passed to ht. */
784 key = NULL;
785
786end:
787 g_free(key);
788 return;
789}
790
791static inline
ca9f27f3 792void handle_event_statedump_bin_info(struct debug_info *debug_info,
b19ff26f 793 const bt_event *event)
4f45f9bb 794{
ca9f27f3 795 handle_bin_info_event(debug_info, event, true);
4f45f9bb
JD
796}
797
798static inline
ca9f27f3 799void handle_event_lib_load(struct debug_info *debug_info,
b19ff26f 800 const bt_event *event)
4f45f9bb 801{
ca9f27f3 802 handle_bin_info_event(debug_info, event, false);
4f45f9bb
JD
803}
804
ca9f27f3
FD
805static
806void handle_event_lib_unload(struct debug_info *debug_info,
b19ff26f 807 const bt_event *event)
4f45f9bb 808{
ca9f27f3 809 gboolean ret;
4f45f9bb
JD
810 struct proc_debug_info_sources *proc_dbg_info_src;
811 uint64_t baddr;
812 int64_t vpid;
4f45f9bb 813
ca9f27f3
FD
814 event_get_payload_unsigned_integer_field_value(event, BADDR_FIELD_NAME,
815 &baddr);
4f45f9bb 816
ca9f27f3
FD
817 event_get_common_context_signed_integer_field_value(event,
818 VPID_FIELD_NAME, &vpid);
4f45f9bb
JD
819
820 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
821 debug_info->vpid_to_proc_dbg_info_src, vpid);
822 if (!proc_dbg_info_src) {
ca9f27f3
FD
823 /*
824 * It's an unload event for a library for which no load event
825 * was previously received.
826 */
4f45f9bb
JD
827 goto end;
828 }
829
ca9f27f3
FD
830 ret = g_hash_table_remove(proc_dbg_info_src->baddr_to_bin_info,
831 (gpointer) &baddr);
832 BT_ASSERT(ret);
4f45f9bb
JD
833end:
834 return;
835}
836
837static
ca9f27f3 838void handle_event_statedump_start(struct debug_info *debug_info,
b19ff26f 839 const bt_event *event)
4f45f9bb
JD
840{
841 struct proc_debug_info_sources *proc_dbg_info_src;
842 int64_t vpid;
4f45f9bb 843
ca9f27f3
FD
844 event_get_common_context_signed_integer_field_value(
845 event, VPID_FIELD_NAME, &vpid);
4f45f9bb
JD
846
847 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
848 debug_info->vpid_to_proc_dbg_info_src, vpid);
849 if (!proc_dbg_info_src) {
850 goto end;
851 }
852
853 g_hash_table_remove_all(proc_dbg_info_src->baddr_to_bin_info);
854 g_hash_table_remove_all(proc_dbg_info_src->ip_to_debug_info_src);
855
856end:
857 return;
858}
859
ca9f27f3
FD
860void trace_debug_info_remove_func(const bt_trace *in_trace, void *data)
861{
862 struct debug_info_msg_iter *debug_it = data;
863 if (debug_it->debug_info_map) {
864 gboolean ret;
865 ret = g_hash_table_remove(debug_it->debug_info_map,
866 (gpointer) in_trace);
867 BT_ASSERT(ret);
868 }
869}
870
871static
872void handle_event_statedump(struct debug_info_msg_iter *debug_it,
873 const bt_event *event)
4f45f9bb 874{
b19ff26f 875 const bt_event_class *event_class;
4f45f9bb
JD
876 const char *event_name;
877 GQuark q_event_name;
ca9f27f3
FD
878 const bt_trace *trace;
879 struct debug_info *debug_info;
880
881 BT_ASSERT(debug_it);
882 BT_ASSERT(event);
883
884 event_class = bt_event_borrow_class_const(event);
4f45f9bb 885
50842bdc 886 event_name = bt_event_class_get_name(event_class);
ca9f27f3
FD
887
888 trace = bt_stream_borrow_trace_const(
889 bt_event_borrow_stream_const(event));
890
891 debug_info = g_hash_table_lookup(debug_it->debug_info_map, trace);
892 if (!debug_info) {
893 debug_info = debug_info_create(debug_it->debug_info_component,
1e638f98 894 trace, &debug_it->fd_cache);
ca9f27f3
FD
895 g_hash_table_insert(debug_it->debug_info_map, (gpointer) trace,
896 debug_info);
897 bt_trace_add_destruction_listener(trace,
898 trace_debug_info_remove_func, debug_it,
899 &debug_info->destruction_listener_id);
4f45f9bb 900 }
ca9f27f3 901
4f45f9bb
JD
902 q_event_name = g_quark_try_string(event_name);
903
904 if (q_event_name == debug_info->q_statedump_bin_info) {
905 /* State dump */
ca9f27f3 906 handle_event_statedump_bin_info(debug_info, event);
4f45f9bb
JD
907 } else if (q_event_name == debug_info->q_dl_open ||
908 q_event_name == debug_info->q_lib_load) {
909 /*
910 * dl_open and lib_load events are both checked for since
911 * only dl_open was produced as of lttng-ust 2.8.
912 *
913 * lib_load, which is produced from lttng-ust 2.9+, is a lot
914 * more reliable since it will be emitted when other functions
915 * of the dlopen family are called (e.g. dlmopen) and when
916 * library are transitively loaded.
917 */
ca9f27f3 918 handle_event_lib_load(debug_info, event);
4f45f9bb
JD
919 } else if (q_event_name == debug_info->q_statedump_start) {
920 /* Start state dump */
ca9f27f3 921 handle_event_statedump_start(debug_info, event);
4f45f9bb
JD
922 } else if (q_event_name == debug_info->q_statedump_debug_link) {
923 /* Debug link info */
ca9f27f3 924 handle_event_statedump_debug_link(debug_info, event);
4f45f9bb
JD
925 } else if (q_event_name == debug_info->q_statedump_build_id) {
926 /* Build ID info */
ca9f27f3 927 handle_event_statedump_build_id(debug_info, event);
4f45f9bb 928 } else if (q_event_name == debug_info-> q_lib_unload) {
ca9f27f3
FD
929 handle_event_lib_unload(debug_info, event);
930 }
931
932 return;
933}
934
935static
936void destroy_debug_info_comp(struct debug_info_component *debug_info)
937{
06d1cf5d
FD
938 if (!debug_info) {
939 return;
940 }
941
942 g_free(debug_info->arg_debug_dir);
943 g_free(debug_info->arg_debug_info_field_name);
944 g_free(debug_info->arg_target_prefix);
ca9f27f3
FD
945 g_free(debug_info);
946}
947
948static
949void fill_debug_info_bin_field(struct debug_info_source *dbg_info_src,
3a3d15f3 950 bool full_path, bt_field *curr_field,
91bc8451 951 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3 952{
d24d5663
PP
953 bt_field_string_set_value_status set_status;
954 bt_field_string_append_status append_status;
ca9f27f3
FD
955
956 BT_ASSERT(bt_field_get_class_type(curr_field) ==
957 BT_FIELD_CLASS_TYPE_STRING);
958
959 if (dbg_info_src) {
960 if (full_path) {
d24d5663
PP
961 set_status = bt_field_string_set_value(curr_field,
962 dbg_info_src->bin_path);
ca9f27f3 963 } else {
d24d5663
PP
964 set_status = bt_field_string_set_value(curr_field,
965 dbg_info_src->short_bin_path);
ca9f27f3 966 }
d24d5663 967 if (set_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 968 BT_COMP_LOGE("Cannot set path component of \"bin\" "
ca9f27f3
FD
969 "curr_field field's value: str-fc-addr=%p",
970 curr_field);
971 }
972
d24d5663
PP
973 append_status = bt_field_string_append(curr_field,
974 dbg_info_src->bin_loc);
975 if (append_status != BT_FIELD_STRING_APPEND_STATUS_OK) {
91bc8451 976 BT_COMP_LOGE("Cannot set bin location component of \"bin\" "
ca9f27f3
FD
977 "curr_field field's value: str-fc-addr=%p",
978 curr_field);
979 }
980 } else {
d24d5663
PP
981 set_status = bt_field_string_set_value(curr_field, "");
982 if (set_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 983 BT_COMP_LOGE("Cannot set \"bin\" curr_field field's value: "
ca9f27f3
FD
984 "str-fc-addr=%p", curr_field);
985 }
986 }
987}
988
989static
990void fill_debug_info_func_field(struct debug_info_source *dbg_info_src,
91bc8451
PP
991 bt_field *curr_field, bt_logging_level log_level,
992 bt_self_component *self_comp)
ca9f27f3 993{
d24d5663 994 bt_field_string_set_value_status status;
ca9f27f3
FD
995
996 BT_ASSERT(bt_field_get_class_type(curr_field) ==
997 BT_FIELD_CLASS_TYPE_STRING);
998 if (dbg_info_src && dbg_info_src->func) {
999 status = bt_field_string_set_value(curr_field,
d24d5663 1000 dbg_info_src->func);
ca9f27f3
FD
1001 } else {
1002 status = bt_field_string_set_value(curr_field, "");
1003 }
d24d5663 1004 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 1005 BT_COMP_LOGE("Cannot set \"func\" curr_field field's value: "
ca9f27f3
FD
1006 "str-fc-addr=%p", curr_field);
1007 }
1008}
1009
1010static
1011void fill_debug_info_src_field(struct debug_info_source *dbg_info_src,
3a3d15f3 1012 bool full_path, bt_field *curr_field,
91bc8451
PP
1013 bt_logging_level log_level,
1014 bt_self_component *self_comp)
ca9f27f3 1015{
d24d5663
PP
1016 bt_field_string_set_value_status set_status;
1017 bt_field_string_append_status append_status;
ca9f27f3
FD
1018
1019 BT_ASSERT(bt_field_get_class_type(curr_field) ==
1020 BT_FIELD_CLASS_TYPE_STRING);
1021
1022 if (dbg_info_src && dbg_info_src->src_path) {
1023 if (full_path) {
d24d5663 1024 set_status = bt_field_string_set_value(curr_field,
ca9f27f3
FD
1025 dbg_info_src->src_path);
1026 } else {
d24d5663 1027 set_status = bt_field_string_set_value(curr_field,
ca9f27f3
FD
1028 dbg_info_src->short_src_path);
1029 }
d24d5663 1030 if (set_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 1031 BT_COMP_LOGE("Cannot set path component of \"src\" "
ca9f27f3
FD
1032 "curr_field field's value: str-fc-addr=%p",
1033 curr_field);
1034 }
1035
d24d5663
PP
1036 append_status = bt_field_string_append(curr_field, ":");
1037 if (append_status != BT_FIELD_STRING_APPEND_STATUS_OK) {
91bc8451 1038 BT_COMP_LOGE("Cannot set colon component of \"src\" "
ca9f27f3
FD
1039 "curr_field field's value: str-fc-addr=%p",
1040 curr_field);
1041 }
1042
d24d5663
PP
1043 append_status = bt_field_string_append(curr_field,
1044 dbg_info_src->line_no);
1045 if (append_status != BT_FIELD_STRING_APPEND_STATUS_OK) {
91bc8451 1046 BT_COMP_LOGE("Cannot set line number component of \"src\" "
ca9f27f3
FD
1047 "curr_field field's value: str-fc-addr=%p",
1048 curr_field);
1049 }
1050 } else {
d24d5663
PP
1051 set_status = bt_field_string_set_value(curr_field, "");
1052 if (set_status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 1053 BT_COMP_LOGE("Cannot set \"src\" curr_field field's value: "
ca9f27f3
FD
1054 "str-fc-addr=%p", curr_field);
1055 }
1056 }
1057}
1058
3a3d15f3
PP
1059static
1060void fill_debug_info_field_empty(bt_field *debug_info_field,
91bc8451 1061 bt_logging_level log_level, bt_self_component *self_comp)
ca9f27f3 1062{
d24d5663 1063 bt_field_string_set_value_status status;
ca9f27f3
FD
1064 bt_field *bin_field, *func_field, *src_field;
1065
1066 BT_ASSERT(bt_field_get_class_type(debug_info_field) ==
1067 BT_FIELD_CLASS_TYPE_STRUCTURE);
1068
1069 bin_field = bt_field_structure_borrow_member_field_by_name(
1070 debug_info_field, "bin");
1071 func_field = bt_field_structure_borrow_member_field_by_name(
1072 debug_info_field, "func");
1073 src_field = bt_field_structure_borrow_member_field_by_name(
1074 debug_info_field, "src");
1075
1076 BT_ASSERT(bt_field_get_class_type(bin_field) ==
1077 BT_FIELD_CLASS_TYPE_STRING);
1078 BT_ASSERT(bt_field_get_class_type(func_field) ==
1079 BT_FIELD_CLASS_TYPE_STRING);
1080 BT_ASSERT(bt_field_get_class_type(src_field) ==
1081 BT_FIELD_CLASS_TYPE_STRING);
1082
1083 status = bt_field_string_set_value(bin_field, "");
d24d5663 1084 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 1085 BT_COMP_LOGE("Cannot set \"bin\" bin_field field's value: "
ca9f27f3
FD
1086 "str-fc-addr=%p", bin_field);
1087 }
1088
1089 status = bt_field_string_set_value(func_field, "");
d24d5663 1090 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 1091 BT_COMP_LOGE("Cannot set \"func\" func_field field's value: "
ca9f27f3
FD
1092 "str-fc-addr=%p", func_field);
1093 }
1094
1095 status = bt_field_string_set_value(src_field, "");
d24d5663 1096 if (status != BT_FIELD_STRING_SET_VALUE_STATUS_OK) {
91bc8451 1097 BT_COMP_LOGE("Cannot set \"src\" src_field field's value: "
ca9f27f3
FD
1098 "str-fc-addr=%p", src_field);
1099 }
1100}
1101static
1102void fill_debug_info_field(struct debug_info *debug_info, int64_t vpid,
1103 uint64_t ip, bt_field *debug_info_field)
1104{
1105 struct debug_info_source *dbg_info_src;
1106 const bt_field_class *debug_info_fc;
1107
1108 BT_ASSERT(bt_field_get_class_type(debug_info_field) ==
1109 BT_FIELD_CLASS_TYPE_STRUCTURE);
1110
1111 debug_info_fc = bt_field_borrow_class_const(debug_info_field);
1112
1113 BT_ASSERT(bt_field_class_structure_get_member_count(debug_info_fc) == 3);
1114
1115 dbg_info_src = debug_info_query(debug_info, vpid, ip);
1116
3a3d15f3
PP
1117 fill_debug_info_bin_field(dbg_info_src,
1118 debug_info->comp->arg_full_path,
1119 bt_field_structure_borrow_member_field_by_name(
1120 debug_info_field, "bin"),
91bc8451 1121 debug_info->log_level, debug_info->self_comp);
ca9f27f3 1122 fill_debug_info_func_field(dbg_info_src,
3a3d15f3
PP
1123 bt_field_structure_borrow_member_field_by_name(
1124 debug_info_field, "func"),
91bc8451 1125 debug_info->log_level, debug_info->self_comp);
3a3d15f3
PP
1126 fill_debug_info_src_field(dbg_info_src,
1127 debug_info->comp->arg_full_path,
1128 bt_field_structure_borrow_member_field_by_name(
1129 debug_info_field, "src"),
91bc8451 1130 debug_info->log_level, debug_info->self_comp);
ca9f27f3
FD
1131}
1132
1133static
1134void fill_debug_info_event_if_needed(struct debug_info_msg_iter *debug_it,
1135 const bt_event *in_event, bt_event *out_event)
1136{
1137 bt_field *out_common_ctx_field, *out_debug_info_field;
1138 const bt_field *vpid_field, *ip_field, *in_common_ctx_field;
1139 const bt_field_class *in_common_ctx_fc;
1140 struct debug_info *debug_info;
1141 uint64_t vpid;
1142 int64_t ip;
06d1cf5d 1143 gchar *debug_info_field_name =
ca9f27f3 1144 debug_it->debug_info_component->arg_debug_info_field_name;
3a3d15f3 1145 bt_logging_level log_level = debug_it->log_level;
91bc8451 1146 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1147
1148 in_common_ctx_field = bt_event_borrow_common_context_field_const(
1149 in_event);
1150 if (!in_common_ctx_field) {
1151 /*
1152 * There is no event common context so no need to add debug
1153 * info field.
1154 */
1155 goto end;
1156 }
1157
1158 in_common_ctx_fc = bt_field_borrow_class_const(in_common_ctx_field);
1159 if (!is_event_common_ctx_dbg_info_compatible(in_common_ctx_fc,
1160 debug_it->ir_maps->debug_info_field_class_name)) {
1161 /*
1162 * The input event common context does not have the necessary
1163 * fields to resolve debug information.
1164 */
1165 goto end;
1166 }
1167
1168 /* Borrow the debug-info field. */
1169 out_common_ctx_field = bt_event_borrow_common_context_field(out_event);
1170 if (!out_common_ctx_field) {
1171 goto end;
4f45f9bb
JD
1172 }
1173
ca9f27f3
FD
1174 out_debug_info_field = bt_field_structure_borrow_member_field_by_name(
1175 out_common_ctx_field, debug_info_field_name);
1176
1177 vpid_field = bt_field_structure_borrow_member_field_by_name_const(
1178 out_common_ctx_field, VPID_FIELD_NAME);
1179 ip_field = bt_field_structure_borrow_member_field_by_name_const(
1180 out_common_ctx_field, IP_FIELD_NAME);
1181
1182 vpid = bt_field_signed_integer_get_value(vpid_field);
1183 ip = bt_field_unsigned_integer_get_value(ip_field);
1184
1185 /*
1186 * Borrow the debug_info structure needed for the source
1187 * resolving.
1188 */
1189 debug_info = g_hash_table_lookup(debug_it->debug_info_map,
1190 bt_stream_borrow_trace_const(
1191 bt_event_borrow_stream_const(in_event)));
1192
1193 if (debug_info) {
1194 /*
1195 * Perform the debug-info resolving and set the event fields
1196 * accordingly.
1197 */
1198 fill_debug_info_field(debug_info, vpid, ip, out_debug_info_field);
1199 } else {
91bc8451 1200 BT_COMP_LOGD("No debug information for this trace. Setting debug "
ca9f27f3 1201 "info fields to empty strings.");
91bc8451
PP
1202 fill_debug_info_field_empty(out_debug_info_field,
1203 log_level, self_comp);
ca9f27f3 1204 }
4f45f9bb
JD
1205end:
1206 return;
1207}
ca9f27f3
FD
1208
1209static
1210void update_event_statedump_if_needed(struct debug_info_msg_iter *debug_it,
1211 const bt_event *in_event)
1212{
1213 const bt_field *event_common_ctx;
1214 const bt_field_class *event_common_ctx_fc;
1215 const bt_event_class *in_event_class = bt_event_borrow_class_const(in_event);
1216
1217 /*
1218 * If the event is an lttng_ust_statedump event AND has the right event
1219 * common context fields update the debug-info view for this process.
1220 */
1221 event_common_ctx = bt_event_borrow_common_context_field_const(in_event);
1222 if (!event_common_ctx) {
1223 goto end;
1224 }
1225
1226 event_common_ctx_fc = bt_field_borrow_class_const(event_common_ctx);
1227 if (is_event_common_ctx_dbg_info_compatible(event_common_ctx_fc,
1228 debug_it->ir_maps->debug_info_field_class_name)) {
1229 /* Checkout if it might be a one of lttng ust statedump events. */
1230 const char *in_event_name = bt_event_class_get_name(in_event_class);
1231 if (strncmp(in_event_name, LTTNG_UST_STATEDUMP_PREFIX,
1232 strlen(LTTNG_UST_STATEDUMP_PREFIX)) == 0) {
1233 /* Handle statedump events. */
1234 handle_event_statedump(debug_it, in_event);
1235 }
1236 }
1237end:
1238 return;
1239}
1240
1241static
1242bt_message *handle_event_message(struct debug_info_msg_iter *debug_it,
1243 const bt_message *in_message)
1244{
1245 const bt_clock_snapshot *cs;
1246 const bt_clock_class *default_cc;
1247 const bt_packet *in_packet;
ca9f27f3
FD
1248 bt_event_class *out_event_class;
1249 bt_packet *out_packet;
1250 bt_event *out_event;
3a3d15f3 1251 bt_logging_level log_level = debug_it->log_level;
91bc8451 1252 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1253
1254 bt_message *out_message = NULL;
1255
1256 /* Borrow the input event and its event class. */
1257 const bt_event *in_event =
1258 bt_message_event_borrow_event_const(in_message);
1259 const bt_event_class *in_event_class =
1260 bt_event_borrow_class_const(in_event);
1261
1262 update_event_statedump_if_needed(debug_it, in_event);
1263
1264 out_event_class = trace_ir_mapping_borrow_mapped_event_class(
1265 debug_it->ir_maps, in_event_class);
1266 if (!out_event_class) {
1267 out_event_class = trace_ir_mapping_create_new_mapped_event_class(
1268 debug_it->ir_maps, in_event_class);
1269 }
1270 BT_ASSERT(out_event_class);
1271
1272 /* Borrow the input and output packets. */
1273 in_packet = bt_event_borrow_packet_const(in_event);
1274 out_packet = trace_ir_mapping_borrow_mapped_packet(debug_it->ir_maps,
1275 in_packet);
1276
1277 default_cc = bt_stream_class_borrow_default_clock_class_const(
1278 bt_event_class_borrow_stream_class_const(in_event_class));
1279 if (default_cc) {
1280 /* Borrow event clock snapshot. */
0cbc2c33
PP
1281 cs = bt_message_event_borrow_default_clock_snapshot_const(
1282 in_message);
ca9f27f3
FD
1283
1284 /* Create an output event message. */
ca9f27f3
FD
1285 out_message = bt_message_event_create_with_default_clock_snapshot(
1286 debug_it->input_iterator,
1287 out_event_class, out_packet,
1288 bt_clock_snapshot_get_value(cs));
1289 } else {
1290 out_message = bt_message_event_create(debug_it->input_iterator,
1291 out_event_class, out_packet);
1292 }
1293
1294 if (!out_message) {
91bc8451 1295 BT_COMP_LOGE("Error creating output event message.");
ca9f27f3
FD
1296 goto error;
1297 }
1298
1299 out_event = bt_message_event_borrow_event(out_message);
1300
1301 /* Copy the original fields to the output event. */
91bc8451 1302 copy_event_content(in_event, out_event, log_level, self_comp);
ca9f27f3
FD
1303
1304 /*
1305 * Try to set the debug-info fields based on debug information that is
1306 * gathered so far.
1307 */
1308 fill_debug_info_event_if_needed(debug_it, in_event, out_event);
1309
1310error:
1311 return out_message;
1312}
1313
1314static
1315bt_message *handle_stream_begin_message(struct debug_info_msg_iter *debug_it,
1316 const bt_message *in_message)
1317{
1318 const bt_stream *in_stream;
1319 bt_message *out_message;
1320 bt_stream *out_stream;
3a3d15f3 1321 bt_logging_level log_level = debug_it->log_level;
91bc8451 1322 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1323
1324 in_stream = bt_message_stream_beginning_borrow_stream_const(in_message);
1325 BT_ASSERT(in_stream);
1326
1327 /* Create a duplicated output stream. */
1328 out_stream = trace_ir_mapping_create_new_mapped_stream(
1329 debug_it->ir_maps, in_stream);
1330 if (!out_stream) {
1331 out_message = NULL;
1332 goto error;
1333 }
1334
1335 /* Create an output stream beginning message. */
1336 out_message = bt_message_stream_beginning_create(
1337 debug_it->input_iterator, out_stream);
1338 if (!out_message) {
91bc8451 1339 BT_COMP_LOGE("Error creating output stream beginning message: "
ca9f27f3
FD
1340 "out-s-addr=%p", out_stream);
1341 }
1342error:
1343 return out_message;
1344}
1345
1346static
1347bt_message *handle_stream_end_message(struct debug_info_msg_iter *debug_it,
1348 const bt_message *in_message)
1349{
1350 const bt_stream *in_stream;
1351 bt_message *out_message = NULL;
1352 bt_stream *out_stream;
3a3d15f3 1353 bt_logging_level log_level = debug_it->log_level;
91bc8451 1354 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1355
1356 in_stream = bt_message_stream_end_borrow_stream_const(in_message);
1357 BT_ASSERT(in_stream);
1358
1359 out_stream = trace_ir_mapping_borrow_mapped_stream(
1360 debug_it->ir_maps, in_stream);
1361 BT_ASSERT(out_stream);
1362
1363 /* Create an output stream end message. */
1364 out_message = bt_message_stream_end_create(debug_it->input_iterator,
1365 out_stream);
1366 if (!out_message) {
91bc8451 1367 BT_COMP_LOGE("Error creating output stream end message: out-s-addr=%p",
ca9f27f3
FD
1368 out_stream);
1369 }
1370
1371 /* Remove stream from trace mapping hashtable. */
1372 trace_ir_mapping_remove_mapped_stream(debug_it->ir_maps, in_stream);
1373
1374 return out_message;
1375}
1376
1377static
1378bt_message *handle_packet_begin_message(struct debug_info_msg_iter *debug_it,
1379 const bt_message *in_message)
1380{
649934d2 1381 bool has_default_clock_snapshot;
ca9f27f3
FD
1382 const bt_clock_snapshot *cs;
1383 bt_message *out_message = NULL;
1384 bt_packet *out_packet;
3a3d15f3 1385 bt_logging_level log_level = debug_it->log_level;
91bc8451 1386 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1387
1388 const bt_packet *in_packet =
1389 bt_message_packet_beginning_borrow_packet_const(in_message);
1390 BT_ASSERT(in_packet);
1391
1392 /* This packet should not be already mapped. */
1393 BT_ASSERT(!trace_ir_mapping_borrow_mapped_packet(
1394 debug_it->ir_maps, in_packet));
1395
1396 out_packet = trace_ir_mapping_create_new_mapped_packet(debug_it->ir_maps,
1397 in_packet);
1398
1399 BT_ASSERT(out_packet);
1400
649934d2 1401 has_default_clock_snapshot =
9b24b6aa 1402 bt_stream_class_packets_have_beginning_default_clock_snapshot(
ca9f27f3
FD
1403 bt_stream_borrow_class_const(
1404 bt_packet_borrow_stream_const(in_packet)));
649934d2 1405 if (has_default_clock_snapshot) {
ca9f27f3 1406 /* Borrow clock snapshot. */
0cbc2c33
PP
1407 cs = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
1408 in_message);
ca9f27f3
FD
1409
1410 /* Create an output packet beginning message. */
ca9f27f3
FD
1411 out_message = bt_message_packet_beginning_create_with_default_clock_snapshot(
1412 debug_it->input_iterator, out_packet,
1413 bt_clock_snapshot_get_value(cs));
1414 } else {
1415 out_message = bt_message_packet_beginning_create(
1416 debug_it->input_iterator, out_packet);
1417 }
1418 if (!out_message) {
91bc8451 1419 BT_COMP_LOGE("Error creating output packet beginning message: "
ca9f27f3
FD
1420 "out-p-addr=%p", out_packet);
1421 }
1422
1423 return out_message;
1424}
1425
1426static
1427bt_message *handle_packet_end_message(struct debug_info_msg_iter *debug_it,
1428 const bt_message *in_message)
1429{
649934d2 1430 bool has_default_clock_snapshot;
ca9f27f3
FD
1431 const bt_clock_snapshot *cs;
1432 const bt_packet *in_packet;
ca9f27f3
FD
1433 bt_message *out_message = NULL;
1434 bt_packet *out_packet;
3a3d15f3 1435 bt_logging_level log_level = debug_it->log_level;
91bc8451 1436 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1437
1438 in_packet = bt_message_packet_end_borrow_packet_const(in_message);
1439 BT_ASSERT(in_packet);
1440
1441 out_packet = trace_ir_mapping_borrow_mapped_packet(debug_it->ir_maps, in_packet);
1442 BT_ASSERT(out_packet);
1443
649934d2 1444 has_default_clock_snapshot =
9b24b6aa 1445 bt_stream_class_packets_have_end_default_clock_snapshot(
ca9f27f3
FD
1446 bt_stream_borrow_class_const(
1447 bt_packet_borrow_stream_const(in_packet)));
649934d2 1448 if (has_default_clock_snapshot) {
ca9f27f3 1449 /* Borrow clock snapshot. */
0cbc2c33
PP
1450 cs = bt_message_packet_end_borrow_default_clock_snapshot_const(
1451 in_message);
ca9f27f3
FD
1452
1453 /* Create an outpute packet end message. */
ca9f27f3
FD
1454 out_message = bt_message_packet_end_create_with_default_clock_snapshot(
1455 debug_it->input_iterator, out_packet,
1456 bt_clock_snapshot_get_value(cs));
1457 } else {
1458 out_message = bt_message_packet_end_create(
1459 debug_it->input_iterator, out_packet);
1460 }
1461
1462 if (!out_message) {
91bc8451 1463 BT_COMP_LOGE("Error creating output packet end message: "
ca9f27f3
FD
1464 "out-p-addr=%p", out_packet);
1465 }
1466
1467 /* Remove packet from data mapping hashtable. */
1468 trace_ir_mapping_remove_mapped_packet(debug_it->ir_maps, in_packet);
1469
1470 return out_message;
1471}
1472
1473static
1474bt_message *handle_msg_iterator_inactivity(struct debug_info_msg_iter *debug_it,
1475 const bt_message *in_message)
1476{
1477 /*
1478 * This message type can be forwarded directly because it does
1479 * not refer to any objects in the trace class.
1480 */
1481 bt_message_get_ref(in_message);
1482 return (bt_message*) in_message;
1483}
1484
1485static
1486bt_message *handle_stream_act_begin_message(struct debug_info_msg_iter *debug_it,
1487 const bt_message *in_message)
1488{
1489 const bt_clock_snapshot *cs;
1490 const bt_clock_class *default_cc;
1491 bt_message *out_message = NULL;
1492 bt_stream *out_stream;
1493 uint64_t cs_value;
1494 bt_message_stream_activity_clock_snapshot_state cs_state;
3a3d15f3 1495 bt_logging_level log_level = debug_it->log_level;
91bc8451 1496 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1497
1498 const bt_stream *in_stream =
1499 bt_message_stream_activity_beginning_borrow_stream_const(
1500 in_message);
1501 BT_ASSERT(in_stream);
1502
1503 out_stream = trace_ir_mapping_borrow_mapped_stream(debug_it->ir_maps,
1504 in_stream);
1505 BT_ASSERT(out_stream);
1506
1507 out_message = bt_message_stream_activity_beginning_create(
1508 debug_it->input_iterator, out_stream);
1509 if (!out_message) {
91bc8451 1510 BT_COMP_LOGE("Error creating output stream activity beginning "
ca9f27f3
FD
1511 "message: out-s-addr=%p", out_stream);
1512 goto error;
1513 }
1514
1515 default_cc = bt_stream_class_borrow_default_clock_class_const(
1516 bt_stream_borrow_class_const(in_stream));
1517 if (default_cc) {
1518 /* Borrow clock snapshot. */
1519 cs_state =
1520 bt_message_stream_activity_beginning_borrow_default_clock_snapshot_const(
1521 in_message, &cs);
1522
1523 if (cs_state == BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN) {
1524 cs_value = bt_clock_snapshot_get_value(cs);
1525 bt_message_stream_activity_beginning_set_default_clock_snapshot(
1526 out_message, cs_value);
1527 } else {
1528 bt_message_stream_activity_beginning_set_default_clock_snapshot_state(
1529 out_message, cs_state);
1530 }
1531 }
1532
1533error:
1534 return out_message;
1535}
1536
1537static
1538bt_message *handle_stream_act_end_message(struct debug_info_msg_iter *debug_it,
1539 const bt_message *in_message)
1540{
1541 const bt_clock_snapshot *cs;
1542 const bt_clock_class *default_cc;
1543 const bt_stream *in_stream;
1544 bt_message *out_message;
1545 bt_stream *out_stream;
1546 uint64_t cs_value;
1547 bt_message_stream_activity_clock_snapshot_state cs_state;
3a3d15f3 1548 bt_logging_level log_level = debug_it->log_level;
91bc8451 1549 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1550
1551 in_stream = bt_message_stream_activity_end_borrow_stream_const(
1552 in_message);
1553 BT_ASSERT(in_stream);
1554
cbba2996
FD
1555 out_stream = trace_ir_mapping_borrow_mapped_stream(debug_it->ir_maps,
1556 in_stream);
ca9f27f3
FD
1557 BT_ASSERT(out_stream);
1558
1559 out_message = bt_message_stream_activity_end_create(
1560 debug_it->input_iterator, out_stream);
1561 if (!out_message) {
91bc8451 1562 BT_COMP_LOGE("Error creating output stream activity end message: "
ca9f27f3
FD
1563 "out-s-addr=%p", out_stream);
1564 goto error;
1565 }
1566
1567 default_cc = bt_stream_class_borrow_default_clock_class_const(
1568 bt_stream_borrow_class_const(in_stream));
1569
1570 if (default_cc) {
1571 cs_state =
1572 bt_message_stream_activity_end_borrow_default_clock_snapshot_const(
1573 in_message, &cs);
1574
1575 if (cs_state == BT_MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN ) {
1576 cs_value = bt_clock_snapshot_get_value(cs);
1577 bt_message_stream_activity_end_set_default_clock_snapshot(
1578 out_message, cs_value);
1579 } else {
1580 bt_message_stream_activity_end_set_default_clock_snapshot_state(
1581 out_message, cs_state);
1582 }
1583 }
1584
1585error:
1586 return out_message;
1587}
1588
1589static
1590bt_message *handle_discarded_events_message(struct debug_info_msg_iter *debug_it,
1591 const bt_message *in_message)
1592{
1593 const bt_clock_snapshot *begin_cs, *end_cs;
1594 const bt_stream *in_stream;
2e90378a 1595 bool has_default_clock_snapshots;
ca9f27f3 1596 uint64_t discarded_events, begin_cs_value, end_cs_value;
ca9f27f3
FD
1597 bt_property_availability prop_avail;
1598 bt_message *out_message = NULL;
1599 bt_stream *out_stream;
3a3d15f3 1600 bt_logging_level log_level = debug_it->log_level;
91bc8451 1601 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1602
1603 in_stream = bt_message_discarded_events_borrow_stream_const(
1604 in_message);
1605 BT_ASSERT(in_stream);
1606
1607 out_stream = trace_ir_mapping_borrow_mapped_stream(
1608 debug_it->ir_maps, in_stream);
1609 BT_ASSERT(out_stream);
1610
2e90378a
PP
1611 has_default_clock_snapshots =
1612 bt_stream_class_discarded_events_have_default_clock_snapshots(
ca9f27f3 1613 bt_stream_borrow_class_const(in_stream));
2e90378a 1614 if (has_default_clock_snapshots) {
0cbc2c33 1615 begin_cs =
9b24b6aa 1616 bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
0cbc2c33
PP
1617 in_message);
1618 end_cs =
9b24b6aa 1619 bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
0cbc2c33 1620 in_message);
ca9f27f3
FD
1621
1622 begin_cs_value = bt_clock_snapshot_get_value(begin_cs);
1623 end_cs_value = bt_clock_snapshot_get_value(end_cs);
1624
1625 out_message =
1626 bt_message_discarded_events_create_with_default_clock_snapshots(
1627 debug_it->input_iterator, out_stream,
1628 begin_cs_value, end_cs_value);
1629 } else {
1630 out_message = bt_message_discarded_events_create(
1631 debug_it->input_iterator, out_stream);
1632 }
1633 if (!out_message) {
91bc8451 1634 BT_COMP_LOGE("Error creating output discarded events message: "
ca9f27f3
FD
1635 "out-s-addr=%p", out_stream);
1636 goto error;
1637 }
1638
1639 prop_avail = bt_message_discarded_events_get_count(in_message,
1640 &discarded_events);
1641
1642 if (prop_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1643 bt_message_discarded_events_set_count(out_message,
1644 discarded_events);
1645 }
1646
1647error:
1648 return out_message;
1649}
1650
1651static
1652bt_message *handle_discarded_packets_message(struct debug_info_msg_iter *debug_it,
1653 const bt_message *in_message)
1654{
1655 const bt_clock_snapshot *begin_cs, *end_cs;
2e90378a 1656 bool has_default_clock_snapshots;
ca9f27f3
FD
1657 const bt_stream *in_stream;
1658 uint64_t discarded_packets, begin_cs_value, end_cs_value;
ca9f27f3
FD
1659 bt_property_availability prop_avail;
1660 bt_message *out_message = NULL;
1661 bt_stream *out_stream;
3a3d15f3 1662 bt_logging_level log_level = debug_it->log_level;
91bc8451 1663 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1664
1665 in_stream = bt_message_discarded_packets_borrow_stream_const(
1666 in_message);
1667 BT_ASSERT(in_stream);
1668
1669 out_stream = trace_ir_mapping_borrow_mapped_stream(
1670 debug_it->ir_maps, in_stream);
1671 BT_ASSERT(out_stream);
1672
2e90378a
PP
1673 has_default_clock_snapshots =
1674 bt_stream_class_discarded_packets_have_default_clock_snapshots(
ca9f27f3 1675 bt_stream_borrow_class_const(in_stream));
2e90378a 1676 if (has_default_clock_snapshots) {
0cbc2c33 1677 begin_cs =
9b24b6aa 1678 bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
0cbc2c33 1679 in_message);
ca9f27f3 1680
0cbc2c33 1681 end_cs =
9b24b6aa 1682 bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
0cbc2c33 1683 in_message);
ca9f27f3
FD
1684
1685 begin_cs_value = bt_clock_snapshot_get_value(begin_cs);
1686 end_cs_value = bt_clock_snapshot_get_value(end_cs);
1687
1688 out_message = bt_message_discarded_packets_create_with_default_clock_snapshots(
1689 debug_it->input_iterator, out_stream,
1690 begin_cs_value, end_cs_value);
1691 } else {
1692 out_message = bt_message_discarded_packets_create(
1693 debug_it->input_iterator, out_stream);
1694 }
1695 if (!out_message) {
91bc8451 1696 BT_COMP_LOGE("Error creating output discarded packet message: "
ca9f27f3
FD
1697 "out-s-addr=%p", out_stream);
1698 goto error;
1699 }
1700
1701 prop_avail = bt_message_discarded_packets_get_count(in_message,
1702 &discarded_packets);
1703 if (prop_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1704 bt_message_discarded_packets_set_count(out_message,
1705 discarded_packets);
1706 }
1707
1708error:
1709 return out_message;
1710}
1711
1712static
1713const bt_message *handle_message(struct debug_info_msg_iter *debug_it,
1714 const bt_message *in_message)
1715{
1716 bt_message *out_message = NULL;
1717
1718 switch (bt_message_get_type(in_message)) {
1719 case BT_MESSAGE_TYPE_EVENT:
1720 out_message = handle_event_message(debug_it,
1721 in_message);
1722 break;
1723 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
1724 out_message = handle_packet_begin_message(debug_it,
1725 in_message);
1726 break;
1727 case BT_MESSAGE_TYPE_PACKET_END:
1728 out_message = handle_packet_end_message(debug_it,
1729 in_message);
1730 break;
1731 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
1732 out_message = handle_stream_begin_message(debug_it,
1733 in_message);
1734 break;
1735 case BT_MESSAGE_TYPE_STREAM_END:
1736 out_message = handle_stream_end_message(debug_it,
1737 in_message);
1738 break;
1739 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
1740 out_message = handle_msg_iterator_inactivity(debug_it,
1741 in_message);
1742 break;
1743 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING:
1744 out_message = handle_stream_act_begin_message(debug_it,
1745 in_message);
1746 break;
1747 case BT_MESSAGE_TYPE_STREAM_ACTIVITY_END:
1748 out_message = handle_stream_act_end_message(debug_it,
1749 in_message);
1750 break;
1751 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
1752 out_message = handle_discarded_events_message(debug_it,
1753 in_message);
1754 break;
1755 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
1756 out_message = handle_discarded_packets_message(debug_it,
1757 in_message);
1758 break;
1759 default:
1760 abort();
1761 break;
1762 }
1763
1764 return out_message;
1765}
1766
1767static
1768int init_from_params(struct debug_info_component *debug_info_component,
1769 const bt_value *params)
1770{
1771 const bt_value *value = NULL;
1772 int ret = 0;
1773
1774 BT_ASSERT(params);
1775
1776 value = bt_value_map_borrow_entry_value_const(params,
1777 "debug-info-field-name");
1778 if (value) {
ca9f27f3 1779 debug_info_component->arg_debug_info_field_name =
06d1cf5d 1780 g_strdup(bt_value_string_get(value));
ca9f27f3
FD
1781 } else {
1782 debug_info_component->arg_debug_info_field_name =
06d1cf5d 1783 g_strdup(DEFAULT_DEBUG_INFO_FIELD_NAME);
ca9f27f3
FD
1784 }
1785
1786 value = bt_value_map_borrow_entry_value_const(params, "debug-info-dir");
1787 if (value) {
06d1cf5d
FD
1788 debug_info_component->arg_debug_dir =
1789 g_strdup(bt_value_string_get(value));
1790 } else {
1791 debug_info_component->arg_debug_dir = NULL;
ca9f27f3
FD
1792 }
1793
06d1cf5d 1794
ca9f27f3
FD
1795 value = bt_value_map_borrow_entry_value_const(params, "target-prefix");
1796 if (value) {
1797 debug_info_component->arg_target_prefix =
06d1cf5d
FD
1798 g_strdup(bt_value_string_get(value));
1799 } else {
1800 debug_info_component->arg_target_prefix = NULL;
ca9f27f3
FD
1801 }
1802
1803 value = bt_value_map_borrow_entry_value_const(params, "full-path");
1804 if (value) {
1805 debug_info_component->arg_full_path = bt_value_bool_get(value);
06d1cf5d
FD
1806 } else {
1807 debug_info_component->arg_full_path = BT_FALSE;
ca9f27f3
FD
1808 }
1809
ca9f27f3
FD
1810 return ret;
1811}
1812
1813BT_HIDDEN
d24d5663 1814bt_component_class_init_method_status debug_info_comp_init(
91bc8451 1815 bt_self_component_filter *self_comp_flt,
c88dd1cb 1816 const bt_value *params, __attribute__((unused)) void *init_method_data)
ca9f27f3
FD
1817{
1818 int ret;
1819 struct debug_info_component *debug_info_comp;
d24d5663
PP
1820 bt_component_class_init_method_status status =
1821 BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK;
1822 bt_self_component_add_port_status add_port_status;
91bc8451
PP
1823 bt_self_component *self_comp =
1824 bt_self_component_filter_as_self_component(self_comp_flt);
3a3d15f3 1825 bt_logging_level log_level = bt_component_get_logging_level(
91bc8451 1826 bt_self_component_as_component(self_comp));
ca9f27f3 1827
91bc8451
PP
1828
1829 BT_COMP_LOGI("Initializing debug_info component: "
ca9f27f3
FD
1830 "comp-addr=%p, params-addr=%p", self_comp, params);
1831
1832 debug_info_comp = g_new0(struct debug_info_component, 1);
1833 if (!debug_info_comp) {
91bc8451 1834 BT_COMP_LOGE_STR("Failed to allocate one debug_info component.");
ca9f27f3
FD
1835 goto error;
1836 }
1837
3a3d15f3 1838 debug_info_comp->log_level = log_level;
91bc8451
PP
1839 debug_info_comp->self_comp = self_comp;
1840 bt_self_component_set_data(self_comp, debug_info_comp);
ca9f27f3 1841
d24d5663
PP
1842 add_port_status = bt_self_component_filter_add_input_port(
1843 self_comp_flt, "in", NULL, NULL);
1844 switch (add_port_status) {
1845 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
1846 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
1847 goto error;
1848 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
1849 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
ca9f27f3 1850 goto error;
d24d5663
PP
1851 default:
1852 break;
ca9f27f3
FD
1853 }
1854
d24d5663
PP
1855 add_port_status = bt_self_component_filter_add_output_port(
1856 self_comp_flt, "out", NULL, NULL);
1857 switch (add_port_status) {
1858 case BT_SELF_COMPONENT_ADD_PORT_STATUS_ERROR:
1859 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
1860 goto error;
1861 case BT_SELF_COMPONENT_ADD_PORT_STATUS_MEMORY_ERROR:
1862 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_MEMORY_ERROR;
ca9f27f3 1863 goto error;
d24d5663
PP
1864 default:
1865 break;
ca9f27f3
FD
1866 }
1867
1868 ret = init_from_params(debug_info_comp, params);
1869 if (ret) {
91bc8451 1870 BT_COMP_LOGE("Cannot configure debug_info component: "
ca9f27f3
FD
1871 "debug_info-comp-addr=%p, params-addr=%p",
1872 debug_info_comp, params);
1873 goto error;
1874 }
1875
1876 goto end;
1877
1878error:
1879 destroy_debug_info_comp(debug_info_comp);
91bc8451 1880 bt_self_component_set_data(self_comp, NULL);
ca9f27f3 1881
d24d5663
PP
1882 if (status == BT_COMPONENT_CLASS_INIT_METHOD_STATUS_OK) {
1883 status = BT_COMPONENT_CLASS_INIT_METHOD_STATUS_ERROR;
ca9f27f3
FD
1884 }
1885end:
1886 return status;
1887}
1888
1889BT_HIDDEN
91bc8451 1890void debug_info_comp_finalize(bt_self_component_filter *self_comp_flt)
ca9f27f3
FD
1891{
1892 struct debug_info_component *debug_info =
1893 bt_self_component_get_data(
1894 bt_self_component_filter_as_self_component(
91bc8451 1895 self_comp_flt));
3a3d15f3 1896 bt_logging_level log_level = debug_info->log_level;
91bc8451 1897 bt_self_component *self_comp = debug_info->self_comp;
3a3d15f3 1898
91bc8451 1899 BT_COMP_LOGI("Finalizing debug_info self_component: comp-addr=%p",
ca9f27f3
FD
1900 self_comp);
1901
1902 destroy_debug_info_comp(debug_info);
1903}
1904
1905BT_HIDDEN
d24d5663 1906bt_component_class_message_iterator_next_method_status debug_info_msg_iter_next(
ca9f27f3
FD
1907 bt_self_message_iterator *self_msg_iter,
1908 const bt_message_array_const msgs, uint64_t capacity,
1909 uint64_t *count)
1910{
1911 bt_self_component_port_input_message_iterator *upstream_iterator = NULL;
d24d5663 1912 bt_message_iterator_next_status upstream_iterator_ret_status;
ca9f27f3
FD
1913 struct debug_info_msg_iter *debug_info_msg_iter;
1914 struct debug_info_component *debug_info = NULL;
d24d5663 1915 bt_component_class_message_iterator_next_method_status status;
ca9f27f3
FD
1916 bt_self_component *self_comp = NULL;
1917 bt_message_array_const input_msgs;
1918 const bt_message *out_message;
1919 uint64_t curr_msg_idx, i;
1920
d24d5663 1921 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
ca9f27f3
FD
1922
1923 self_comp = bt_self_message_iterator_borrow_component(self_msg_iter);
1924 BT_ASSERT(self_comp);
1925
1926 debug_info = bt_self_component_get_data(self_comp);
1927 BT_ASSERT(debug_info);
1928
1929 debug_info_msg_iter = bt_self_message_iterator_get_data(self_msg_iter);
1930 BT_ASSERT(debug_info_msg_iter);
1931
1932 upstream_iterator = debug_info_msg_iter->msg_iter;
1933 BT_ASSERT(upstream_iterator);
1934
1935 upstream_iterator_ret_status =
1936 bt_self_component_port_input_message_iterator_next(
1937 upstream_iterator, &input_msgs, count);
d24d5663
PP
1938 if (upstream_iterator_ret_status !=
1939 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
ca9f27f3 1940 /*
d24d5663
PP
1941 * No messages were returned. Not necessarily an error.
1942 * Convert the upstream message iterator status to a
1943 * self status.
ca9f27f3 1944 */
d24d5663 1945 status = (int) upstream_iterator_ret_status;
ca9f27f3
FD
1946 goto end;
1947 }
1948
1949 /*
1950 * There should never be more received messages than the capacity we
1951 * provided.
1952 */
1953 BT_ASSERT(*count <= capacity);
1954
1955 for (curr_msg_idx = 0; curr_msg_idx < *count; curr_msg_idx++) {
1956 out_message = handle_message(debug_info_msg_iter,
d24d5663 1957 input_msgs[curr_msg_idx]);
ca9f27f3
FD
1958 if (!out_message) {
1959 goto handle_msg_error;
1960 }
1961
1962 msgs[curr_msg_idx] = out_message;
1963 /*
1964 * Drop our reference of the input message as we are done with
1965 * it and created a output copy.
1966 */
1967 bt_message_put_ref(input_msgs[curr_msg_idx]);
1968 }
1969
1970 goto end;
1971
1972handle_msg_error:
1973 /*
1974 * Drop references of all the output messages created before the
1975 * failure.
1976 */
1977 for (i = 0; i < curr_msg_idx; i++) {
1978 bt_message_put_ref(msgs[i]);
1979 }
1980
d24d5663
PP
1981 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR;
1982
ca9f27f3
FD
1983end:
1984 return status;
1985}
1986
cbba2996
FD
1987static
1988void debug_info_msg_iter_destroy(struct debug_info_msg_iter *debug_info_msg_iter)
1989{
1990 if (!debug_info_msg_iter) {
1991 goto end;
1992 }
1993
1994 if (debug_info_msg_iter->msg_iter) {
1995 bt_self_component_port_input_message_iterator_put_ref(
1996 debug_info_msg_iter->msg_iter);
1997 }
1998
1999 if (debug_info_msg_iter->ir_maps) {
2000 trace_ir_maps_destroy(debug_info_msg_iter->ir_maps);
2001 }
2002
2003 if (debug_info_msg_iter->debug_info_map) {
2004 g_hash_table_destroy(debug_info_msg_iter->debug_info_map);
2005 }
2006
2007 bt_fd_cache_fini(&debug_info_msg_iter->fd_cache);
2008 g_free(debug_info_msg_iter);
2009
2010end:
2011 return;
2012}
2013
ca9f27f3 2014BT_HIDDEN
d24d5663 2015bt_component_class_message_iterator_init_method_status debug_info_msg_iter_init(
ca9f27f3 2016 bt_self_message_iterator *self_msg_iter,
91bc8451 2017 bt_self_component_filter *self_comp_flt,
ca9f27f3
FD
2018 bt_self_component_port_output *self_port)
2019{
d24d5663
PP
2020 bt_component_class_message_iterator_init_method_status status =
2021 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_OK;
cbba2996
FD
2022 struct bt_self_component_port_input *input_port = NULL;
2023 bt_self_component_port_input_message_iterator *upstream_iterator = NULL;
2024 struct debug_info_msg_iter *debug_info_msg_iter = NULL;
06d1cf5d 2025 gchar *debug_info_field_name;
1e638f98 2026 int ret;
91bc8451
PP
2027 bt_self_component *self_comp =
2028 bt_self_component_filter_as_self_component(self_comp_flt);
3a3d15f3 2029 bt_logging_level log_level = bt_component_get_logging_level(
91bc8451 2030 bt_self_component_as_component(self_comp));
ca9f27f3
FD
2031
2032 /* Borrow the upstream input port. */
2033 input_port = bt_self_component_filter_borrow_input_port_by_name(
91bc8451 2034 self_comp_flt, "in");
ca9f27f3 2035 if (!input_port) {
d24d5663 2036 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_ERROR;
cbba2996
FD
2037 goto error;
2038 }
2039
2040 debug_info_msg_iter = g_new0(struct debug_info_msg_iter, 1);
2041 if (!debug_info_msg_iter) {
d24d5663 2042 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
cbba2996 2043 goto error;
ca9f27f3
FD
2044 }
2045
3a3d15f3 2046 debug_info_msg_iter->log_level = log_level;
91bc8451 2047 debug_info_msg_iter->self_comp = self_comp;
3a3d15f3 2048
ca9f27f3
FD
2049 /* Create an iterator on the upstream component. */
2050 upstream_iterator = bt_self_component_port_input_message_iterator_create(
2051 input_port);
2052 if (!upstream_iterator) {
d24d5663 2053 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
cbba2996 2054 goto error;
ca9f27f3
FD
2055 }
2056
cbba2996
FD
2057 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
2058 debug_info_msg_iter->msg_iter, upstream_iterator);
ca9f27f3 2059
cbba2996 2060 /* Create hashtable that will contain debug info mapping. */
ca9f27f3 2061 debug_info_msg_iter->debug_info_map = g_hash_table_new_full(
cbba2996
FD
2062 g_direct_hash, g_direct_equal, (GDestroyNotify) NULL,
2063 (GDestroyNotify) debug_info_destroy);
ca9f27f3 2064 if (!debug_info_msg_iter->debug_info_map) {
d24d5663 2065 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
cbba2996 2066 goto error;
ca9f27f3
FD
2067 }
2068
91bc8451
PP
2069 debug_info_msg_iter->debug_info_component =
2070 bt_self_component_get_data(self_comp);
ca9f27f3 2071
06d1cf5d
FD
2072 debug_info_field_name =
2073 debug_info_msg_iter->debug_info_component->arg_debug_info_field_name;
2074
91bc8451 2075 debug_info_msg_iter->ir_maps = trace_ir_maps_create(self_comp,
3a3d15f3 2076 debug_info_field_name, log_level);
ca9f27f3 2077 if (!debug_info_msg_iter->ir_maps) {
d24d5663 2078 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
cbba2996 2079 goto error;
ca9f27f3
FD
2080 }
2081
3a3d15f3 2082 ret = bt_fd_cache_init(&debug_info_msg_iter->fd_cache, log_level);
1e638f98 2083 if (ret) {
d24d5663 2084 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INIT_METHOD_STATUS_MEMORY_ERROR;
cbba2996 2085 goto error;
1e638f98
FD
2086 }
2087
ca9f27f3 2088 bt_self_message_iterator_set_data(self_msg_iter, debug_info_msg_iter);
ca9f27f3
FD
2089 debug_info_msg_iter->input_iterator = self_msg_iter;
2090
cbba2996
FD
2091 goto end;
2092
2093error:
2094 debug_info_msg_iter_destroy(debug_info_msg_iter);
d24d5663 2095
ca9f27f3
FD
2096end:
2097 return status;
2098}
2099
2100BT_HIDDEN
2101bt_bool debug_info_msg_iter_can_seek_beginning(
2102 bt_self_message_iterator *self_msg_iter)
2103{
2104 struct debug_info_msg_iter *debug_info_msg_iter =
2105 bt_self_message_iterator_get_data(self_msg_iter);
2106 BT_ASSERT(debug_info_msg_iter);
2107
2108 return bt_self_component_port_input_message_iterator_can_seek_beginning(
2109 debug_info_msg_iter->msg_iter);
2110}
2111
2112BT_HIDDEN
d24d5663 2113bt_component_class_message_iterator_seek_beginning_method_status debug_info_msg_iter_seek_beginning(
ca9f27f3
FD
2114 bt_self_message_iterator *self_msg_iter)
2115{
2116 struct debug_info_msg_iter *debug_info_msg_iter =
2117 bt_self_message_iterator_get_data(self_msg_iter);
d24d5663
PP
2118 bt_component_class_message_iterator_seek_beginning_method_status status =
2119 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
2120 bt_message_iterator_seek_beginning_status seek_beg_status;
ca9f27f3
FD
2121
2122 BT_ASSERT(debug_info_msg_iter);
2123
2124 /* Ask the upstream component to seek to the beginning. */
d24d5663 2125 seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning(
ca9f27f3 2126 debug_info_msg_iter->msg_iter);
d24d5663
PP
2127 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
2128 status = (int) seek_beg_status;
ca9f27f3
FD
2129 goto end;
2130 }
2131
2132 /* Clear this iterator data. */
2133 trace_ir_maps_clear(debug_info_msg_iter->ir_maps);
2134 g_hash_table_remove_all(debug_info_msg_iter->debug_info_map);
d24d5663 2135
ca9f27f3 2136end:
d24d5663 2137 return status;
ca9f27f3
FD
2138}
2139
2140BT_HIDDEN
2141void debug_info_msg_iter_finalize(bt_self_message_iterator *it)
2142{
2143 struct debug_info_msg_iter *debug_info_msg_iter;
2144
2145 debug_info_msg_iter = bt_self_message_iterator_get_data(it);
2146 BT_ASSERT(debug_info_msg_iter);
2147
cbba2996 2148 debug_info_msg_iter_destroy(debug_info_msg_iter);
ca9f27f3 2149}
This page took 0.140934 seconds and 4 git commands to generate.