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