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