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