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