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