flt.lttng-utils.debug-info: tidy up component initialization
[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"
ca9f27f3
FD
46
47#define DEFAULT_DEBUG_INFO_FIELD_NAME "debug_info"
48#define LTTNG_UST_STATEDUMP_PREFIX "lttng_ust"
49#define VPID_FIELD_NAME "vpid"
50#define IP_FIELD_NAME "ip"
51#define BADDR_FIELD_NAME "baddr"
52#define CRC32_FIELD_NAME "crc"
53#define BUILD_ID_FIELD_NAME "build_id"
54#define FILENAME_FIELD_NAME "filename"
55#define IS_PIC_FIELD_NAME "is_pic"
56#define MEMSZ_FIELD_NAME "memsz"
57#define PATH_FIELD_NAME "path"
58
59struct debug_info_component {
3a3d15f3 60 bt_logging_level log_level;
91bc8451 61 bt_self_component *self_comp;
06d1cf5d
FD
62 gchar *arg_debug_dir;
63 gchar *arg_debug_info_field_name;
64 gchar *arg_target_prefix;
65 bt_bool arg_full_path;
ca9f27f3
FD
66};
67
68struct debug_info_msg_iter {
3a3d15f3 69 bt_logging_level log_level;
ca9f27f3
FD
70 struct debug_info_component *debug_info_component;
71 bt_self_message_iterator *input_iterator;
72 bt_self_component *self_comp;
73 bt_self_component_port_input_message_iterator *msg_iter;
74
75 struct trace_ir_maps *ir_maps;
76 /* in_trace -> debug_info_mapping. */
77 GHashTable *debug_info_map;
1e638f98
FD
78
79 struct bt_fd_cache fd_cache;
ca9f27f3
FD
80};
81
82struct debug_info_source {
83 /* Strings are owned by debug_info_source. */
06d1cf5d 84 gchar *func;
ca9f27f3
FD
85 /*
86 * Store the line number as a string so that the allocation and
87 * conversion to string is only done once.
88 */
06d1cf5d
FD
89 gchar *line_no;
90 gchar *src_path;
ca9f27f3 91 /* short_src_path points inside src_path, no need to free. */
06d1cf5d
FD
92 const gchar *short_src_path;
93 gchar *bin_path;
ca9f27f3 94 /* short_bin_path points inside bin_path, no need to free. */
06d1cf5d 95 const gchar *short_bin_path;
ca9f27f3
FD
96 /*
97 * Location within the binary. Either absolute (@0x1234) or
98 * relative (+0x4321).
99 */
06d1cf5d 100 gchar *bin_loc;
ca9f27f3 101};
4f45f9bb
JD
102
103struct proc_debug_info_sources {
104 /*
105 * Hash table: base address (pointer to uint64_t) to bin info; owned by
106 * proc_debug_info_sources.
107 */
108 GHashTable *baddr_to_bin_info;
109
110 /*
111 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
112 * owned by proc_debug_info_sources.
113 */
114 GHashTable *ip_to_debug_info_src;
115};
116
117struct debug_info {
3a3d15f3 118 bt_logging_level log_level;
91bc8451 119 bt_self_component *self_comp;
9d325e17 120 struct debug_info_component *comp;
ca9f27f3 121 const bt_trace *input_trace;
2054a0d1 122 bt_listener_id destruction_listener_id;
9d325e17 123
4f45f9bb
JD
124 /*
125 * Hash table of VPIDs (pointer to int64_t) to
f9597f41 126 * (struct proc_debug_info_sources*); owned by debug_info.
4f45f9bb
JD
127 */
128 GHashTable *vpid_to_proc_dbg_info_src;
129 GQuark q_statedump_bin_info;
130 GQuark q_statedump_debug_link;
131 GQuark q_statedump_build_id;
132 GQuark q_statedump_start;
133 GQuark q_dl_open;
134 GQuark q_lib_load;
135 GQuark q_lib_unload;
1e638f98 136 struct bt_fd_cache *fd_cache; /* Weak ref. Owned by the iterator. */
4f45f9bb
JD
137};
138
139static
140int debug_info_init(struct debug_info *info)
141{
142 info->q_statedump_bin_info = g_quark_from_string(
143 "lttng_ust_statedump:bin_info");
144 info->q_statedump_debug_link = g_quark_from_string(
ca9f27f3 145 "lttng_ust_statedump:debug_link");
4f45f9bb
JD
146 info->q_statedump_build_id = g_quark_from_string(
147 "lttng_ust_statedump:build_id");
148 info->q_statedump_start = g_quark_from_string(
149 "lttng_ust_statedump:start");
150 info->q_dl_open = g_quark_from_string("lttng_ust_dl:dlopen");
151 info->q_lib_load = g_quark_from_string("lttng_ust_lib:load");
152 info->q_lib_unload = g_quark_from_string("lttng_ust_lib:unload");
153
91bc8451 154 return bin_info_init(info->log_level, info->self_comp);
4f45f9bb
JD
155}
156
157static
158void debug_info_source_destroy(struct debug_info_source *debug_info_src)
159{
160 if (!debug_info_src) {
161 return;
162 }
163
06d1cf5d
FD
164 g_free(debug_info_src->func);
165 g_free(debug_info_src->line_no);
166 g_free(debug_info_src->src_path);
167 g_free(debug_info_src->bin_path);
168 g_free(debug_info_src->bin_loc);
4f45f9bb
JD
169 g_free(debug_info_src);
170}
171
172static
ca9f27f3 173struct debug_info_source *debug_info_source_create_from_bin(
91bc8451
PP
174 struct bin_info *bin, uint64_t ip,
175 bt_self_component *self_comp)
4f45f9bb
JD
176{
177 int ret;
178 struct debug_info_source *debug_info_src = NULL;
179 struct source_location *src_loc = NULL;
3a3d15f3 180 bt_logging_level log_level = bin->log_level;
4f45f9bb
JD
181
182 debug_info_src = g_new0(struct debug_info_source, 1);
183
184 if (!debug_info_src) {
185 goto end;
186 }
187
188 /* Lookup function name */
189 ret = bin_info_lookup_function_name(bin, ip, &debug_info_src->func);
190 if (ret) {
191 goto error;
192 }
193
194 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
195 if (!bin->is_elf_only || !debug_info_src->func) {
196 /* Lookup source location */
197 ret = bin_info_lookup_source_location(bin, ip, &src_loc);
ca9f27f3 198 if (ret) {
91bc8451 199 BT_COMP_LOGI("Failed to lookup source location: ret=%d", ret);
ca9f27f3 200 }
4f45f9bb
JD
201 }
202
203 if (src_loc) {
06d1cf5d
FD
204 debug_info_src->line_no =
205 g_strdup_printf("%"PRId64, src_loc->line_no);
206 if (!debug_info_src->line_no) {
91bc8451 207 BT_COMP_LOGE("Error occured when setting line_no field.");
ca9f27f3
FD
208 goto error;
209 }
4f45f9bb
JD
210
211 if (src_loc->filename) {
06d1cf5d 212 debug_info_src->src_path = g_strdup(src_loc->filename);
4f45f9bb
JD
213 if (!debug_info_src->src_path) {
214 goto error;
215 }
216
217 debug_info_src->short_src_path = get_filename_from_path(
bc463d34 218 debug_info_src->src_path);
4f45f9bb 219 }
4f45f9bb
JD
220 source_location_destroy(src_loc);
221 }
222
223 if (bin->elf_path) {
06d1cf5d 224 debug_info_src->bin_path = g_strdup(bin->elf_path);
4f45f9bb
JD
225 if (!debug_info_src->bin_path) {
226 goto error;
227 }
228
229 debug_info_src->short_bin_path = get_filename_from_path(
bc463d34 230 debug_info_src->bin_path);
4f45f9bb
JD
231
232 ret = bin_info_get_bin_loc(bin, ip, &(debug_info_src->bin_loc));
233 if (ret) {
234 goto error;
235 }
236 }
237
238end:
239 return debug_info_src;
240
241error:
242 debug_info_source_destroy(debug_info_src);
243 return NULL;
244}
245
246static
247void proc_debug_info_sources_destroy(
248 struct proc_debug_info_sources *proc_dbg_info_src)
249{
250 if (!proc_dbg_info_src) {
251 return;
252 }
253
254 if (proc_dbg_info_src->baddr_to_bin_info) {
255 g_hash_table_destroy(proc_dbg_info_src->baddr_to_bin_info);
256 }
257
258 if (proc_dbg_info_src->ip_to_debug_info_src) {
259 g_hash_table_destroy(proc_dbg_info_src->ip_to_debug_info_src);
260 }
261
262 g_free(proc_dbg_info_src);
263}
264
265static
266struct proc_debug_info_sources *proc_debug_info_sources_create(void)
267{
268 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
269
270 proc_dbg_info_src = g_new0(struct proc_debug_info_sources, 1);
271 if (!proc_dbg_info_src) {
272 goto end;
273 }
274
275 proc_dbg_info_src->baddr_to_bin_info = g_hash_table_new_full(
bc463d34
FD
276 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
277 (GDestroyNotify) bin_info_destroy);
4f45f9bb
JD
278 if (!proc_dbg_info_src->baddr_to_bin_info) {
279 goto error;
280 }
281
282 proc_dbg_info_src->ip_to_debug_info_src = g_hash_table_new_full(
bc463d34
FD
283 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
284 (GDestroyNotify) debug_info_source_destroy);
4f45f9bb
JD
285 if (!proc_dbg_info_src->ip_to_debug_info_src) {
286 goto error;
287 }
288
289end:
290 return proc_dbg_info_src;
291
292error:
293 proc_debug_info_sources_destroy(proc_dbg_info_src);
294 return NULL;
295}
296
297static
298struct proc_debug_info_sources *proc_debug_info_sources_ht_get_entry(
299 GHashTable *ht, int64_t vpid)
300{
301 gpointer key = g_new0(int64_t, 1);
302 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
303
304 if (!key) {
305 goto end;
306 }
307
308 *((int64_t *) key) = vpid;
309
310 /* Exists? Return it */
311 proc_dbg_info_src = g_hash_table_lookup(ht, key);
312 if (proc_dbg_info_src) {
313 goto end;
314 }
315
316 /* Otherwise, create and return it */
317 proc_dbg_info_src = proc_debug_info_sources_create();
318 if (!proc_dbg_info_src) {
319 goto end;
320 }
321
322 g_hash_table_insert(ht, key, proc_dbg_info_src);
323 /* Ownership passed to ht */
324 key = NULL;
325end:
326 g_free(key);
327 return proc_dbg_info_src;
328}
329
ca9f27f3
FD
330static inline
331const bt_field *event_borrow_payload_field(const bt_event *event,
332 const char *field_name)
333{
334 const bt_field *event_payload, *field;
335
336 event_payload = bt_event_borrow_payload_field_const(event);
337 BT_ASSERT(event_payload);
338
339 field = bt_field_structure_borrow_member_field_by_name_const(
bc463d34 340 event_payload, field_name);
ca9f27f3
FD
341 return field;
342}
343
344static inline
345const bt_field *event_borrow_common_context_field(const bt_event *event,
346 const char *field_name)
347{
348 const bt_field *event_common_ctx, *field = NULL;
349
350 event_common_ctx = bt_event_borrow_common_context_field_const(event);
351 if (!event_common_ctx) {
352 goto end;
353 }
354
355 field = bt_field_structure_borrow_member_field_by_name_const(
bc463d34 356 event_common_ctx, field_name);
ca9f27f3
FD
357
358end:
359 return field;
360}
361
362static inline
363void event_get_common_context_signed_integer_field_value(
364 const bt_event *event, const char *field_name, int64_t *value)
365{
9c08c816 366 *value = bt_field_integer_signed_get_value(
bc463d34 367 event_borrow_common_context_field(event, field_name));
ca9f27f3
FD
368}
369
370static inline
371int event_get_payload_build_id_length(const bt_event *event,
372 const char *field_name, uint64_t *build_id_len)
373{
374 const bt_field *build_id_field;
375 const bt_field_class *build_id_field_class;
376
377 build_id_field = event_borrow_payload_field(event, field_name);
378 build_id_field_class = bt_field_borrow_class_const(build_id_field);
379
380 BT_ASSERT(bt_field_class_get_type(build_id_field_class) ==
bc463d34 381 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
ca9f27f3 382 BT_ASSERT(bt_field_class_get_type(
bc463d34
FD
383 bt_field_class_array_borrow_element_field_class_const(
384 build_id_field_class)) ==
385 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
ca9f27f3
FD
386
387 *build_id_len = bt_field_array_get_length(build_id_field);
388
389 return 0;
390}
391
392static inline
393int event_get_payload_build_id_value(const bt_event *event,
394 const char *field_name, uint8_t *build_id)
395{
396 const bt_field *curr_field, *build_id_field;
397 const bt_field_class *build_id_field_class;
398 uint64_t i, build_id_len;
399 int ret;
400
401 ret = 0;
402
403 build_id_field = event_borrow_payload_field(event, field_name);
404 build_id_field_class = bt_field_borrow_class_const(build_id_field);
405
406 BT_ASSERT(bt_field_class_get_type(build_id_field_class) ==
bc463d34 407 BT_FIELD_CLASS_TYPE_DYNAMIC_ARRAY);
ca9f27f3 408 BT_ASSERT(bt_field_class_get_type(
bc463d34
FD
409 bt_field_class_array_borrow_element_field_class_const(
410 build_id_field_class)) ==
411 BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER);
ca9f27f3
FD
412
413 build_id_len = bt_field_array_get_length(build_id_field);
414
415 for (i = 0; i < build_id_len; i++) {
bc463d34
FD
416 curr_field = bt_field_array_borrow_element_field_by_index_const(
417 build_id_field, i);
ca9f27f3 418
9c08c816 419 build_id[i] = bt_field_integer_unsigned_get_value(curr_field);
ca9f27f3
FD
420 }
421
422 return ret;
423}
424
425static
426void event_get_payload_unsigned_integer_field_value(const bt_event *event,
427 const char *field_name, uint64_t *value)
428{
9c08c816 429 *value = bt_field_integer_unsigned_get_value(
bc463d34 430 event_borrow_payload_field(event, field_name));
ca9f27f3
FD
431}
432
433static
434void event_get_payload_string_field_value(const bt_event *event,
435 const char *field_name, const char **value)
436{
437 *value = bt_field_string_get_value(
bc463d34 438 event_borrow_payload_field(event, field_name));
ca9f27f3
FD
439}
440
441static inline
442bool event_has_payload_field(const bt_event *event,
443 const char *field_name)
444{
5084732e 445 return event_borrow_payload_field(event, field_name);
ca9f27f3
FD
446}
447
4f45f9bb
JD
448static
449struct debug_info_source *proc_debug_info_sources_get_entry(
3a3d15f3 450 struct debug_info *debug_info,
4f45f9bb
JD
451 struct proc_debug_info_sources *proc_dbg_info_src, uint64_t ip)
452{
453 struct debug_info_source *debug_info_src = NULL;
454 gpointer key = g_new0(uint64_t, 1);
455 GHashTableIter iter;
456 gpointer baddr, value;
457
458 if (!key) {
459 goto end;
460 }
461
462 *((uint64_t *) key) = ip;
463
464 /* Look in IP to debug infos hash table first. */
465 debug_info_src = g_hash_table_lookup(
bc463d34 466 proc_dbg_info_src->ip_to_debug_info_src, key);
4f45f9bb
JD
467 if (debug_info_src) {
468 goto end;
469 }
470
471 /* Check in all bin_infos. */
472 g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_bin_info);
473
474 while (g_hash_table_iter_next(&iter, &baddr, &value))
475 {
476 struct bin_info *bin = value;
477
478 if (!bin_info_has_address(value, ip)) {
479 continue;
480 }
481
482 /*
483 * Found; add it to cache.
484 *
485 * FIXME: this should be bounded in size (and implement
486 * a caching policy), and entries should be prunned when
487 * libraries are unmapped.
488 */
91bc8451
PP
489 debug_info_src = debug_info_source_create_from_bin(bin, ip,
490 debug_info->self_comp);
4f45f9bb
JD
491 if (debug_info_src) {
492 g_hash_table_insert(
bc463d34
FD
493 proc_dbg_info_src->ip_to_debug_info_src, key,
494 debug_info_src);
4f45f9bb
JD
495 /* Ownership passed to ht. */
496 key = NULL;
497 }
498 break;
499 }
500
501end:
502 free(key);
503 return debug_info_src;
504}
505
1e638f98 506static
4f45f9bb
JD
507struct debug_info_source *debug_info_query(struct debug_info *debug_info,
508 int64_t vpid, uint64_t ip)
509{
510 struct debug_info_source *dbg_info_src = NULL;
511 struct proc_debug_info_sources *proc_dbg_info_src;
512
513 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
bc463d34 514 debug_info->vpid_to_proc_dbg_info_src, vpid);
4f45f9bb
JD
515 if (!proc_dbg_info_src) {
516 goto end;
517 }
518
3a3d15f3
PP
519 dbg_info_src = proc_debug_info_sources_get_entry(debug_info,
520 proc_dbg_info_src, ip);
4f45f9bb
JD
521
522end:
523 return dbg_info_src;
524}
525
1e638f98 526static
ca9f27f3 527struct debug_info *debug_info_create(struct debug_info_component *comp,
1e638f98 528 const bt_trace *trace, struct bt_fd_cache *fdc)
4f45f9bb
JD
529{
530 int ret;
531 struct debug_info *debug_info;
532
1e638f98
FD
533 BT_ASSERT(comp);
534 BT_ASSERT(trace);
535 BT_ASSERT(fdc);
536
4f45f9bb
JD
537 debug_info = g_new0(struct debug_info, 1);
538 if (!debug_info) {
539 goto end;
540 }
541
3a3d15f3 542 debug_info->log_level = comp->log_level;
91bc8451 543 debug_info->self_comp = comp->self_comp;
4f45f9bb 544 debug_info->vpid_to_proc_dbg_info_src = g_hash_table_new_full(
bc463d34
FD
545 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
546 (GDestroyNotify) proc_debug_info_sources_destroy);
4f45f9bb
JD
547 if (!debug_info->vpid_to_proc_dbg_info_src) {
548 goto error;
549 }
550
9d325e17 551 debug_info->comp = comp;
4f45f9bb
JD
552 ret = debug_info_init(debug_info);
553 if (ret) {
554 goto error;
555 }
556
ca9f27f3 557 debug_info->input_trace = trace;
1e638f98 558 debug_info->fd_cache = fdc;
ca9f27f3 559
4f45f9bb
JD
560end:
561 return debug_info;
562error:
563 g_free(debug_info);
564 return NULL;
565}
566
1e638f98 567static
4f45f9bb
JD
568void debug_info_destroy(struct debug_info *debug_info)
569{
3a3d15f3 570 bt_logging_level log_level;
91bc8451 571 bt_self_component *self_comp;
d24d5663 572 bt_trace_remove_listener_status remove_listener_status;
4f45f9bb
JD
573 if (!debug_info) {
574 goto end;
575 }
576
3a3d15f3 577 log_level = debug_info->log_level;
91bc8451 578 self_comp = debug_info->self_comp;
3a3d15f3 579
4f45f9bb
JD
580 if (debug_info->vpid_to_proc_dbg_info_src) {
581 g_hash_table_destroy(debug_info->vpid_to_proc_dbg_info_src);
582 }
583
d24d5663 584 remove_listener_status = bt_trace_remove_destruction_listener(
bc463d34
FD
585 debug_info->input_trace,
586 debug_info->destruction_listener_id);
d24d5663 587 if (remove_listener_status != BT_TRACE_REMOVE_LISTENER_STATUS_OK) {
91bc8451 588 BT_COMP_LOGE("Trace destruction listener removal failed.");
b80991f6 589 bt_current_thread_clear_error();
3b40fbf9 590 }
ca9f27f3 591
4f45f9bb
JD
592 g_free(debug_info);
593end:
594 return;
595}
596
597static
ca9f27f3 598void handle_event_statedump_build_id(struct debug_info *debug_info,
b19ff26f 599 const bt_event *event)
4f45f9bb
JD
600{
601 struct proc_debug_info_sources *proc_dbg_info_src;
ca9f27f3
FD
602 uint64_t build_id_len, baddr;
603 uint8_t *build_id = NULL;
604 struct bin_info *bin;
4f45f9bb 605 int64_t vpid;
ca9f27f3 606 int ret = 0;
4f45f9bb 607
ca9f27f3 608 event_get_common_context_signed_integer_field_value(event,
bc463d34 609 VPID_FIELD_NAME, &vpid);
ca9f27f3 610 event_get_payload_unsigned_integer_field_value(event,
bc463d34 611 BADDR_FIELD_NAME, &baddr);
4f45f9bb
JD
612
613 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
bc463d34 614 debug_info->vpid_to_proc_dbg_info_src, vpid);
4f45f9bb
JD
615 if (!proc_dbg_info_src) {
616 goto end;
617 }
618
619 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
bc463d34 620 (gpointer) &baddr);
4f45f9bb
JD
621 if (!bin) {
622 /*
623 * The build_id event comes after the bin has been
624 * created. If it isn't found, just ignore this event.
625 */
626 goto end;
627 }
ca9f27f3 628 ret = event_get_payload_build_id_length(event, BUILD_ID_FIELD_NAME,
bc463d34 629 &build_id_len);
ca9f27f3
FD
630
631 build_id = g_new0(uint8_t, build_id_len);
632 if (!build_id) {
633 goto end;
634 }
4f45f9bb 635
ca9f27f3 636 ret = event_get_payload_build_id_value(event, BUILD_ID_FIELD_NAME,
bc463d34 637 build_id);
4f45f9bb 638 if (ret) {
4f45f9bb
JD
639 goto end;
640 }
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
FD
957
958 BT_ASSERT(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) {
91bc8451 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) {
91bc8451 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) {
91bc8451 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
FD
1000
1001 BT_ASSERT(bt_field_get_class_type(curr_field) ==
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) {
91bc8451 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
FD
1024
1025 BT_ASSERT(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) {
91bc8451 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) {
91bc8451 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) {
91bc8451 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) {
91bc8451 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
1076 BT_ASSERT(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
FD
1085
1086 BT_ASSERT(bt_field_get_class_type(bin_field) ==
bc463d34 1087 BT_FIELD_CLASS_TYPE_STRING);
ca9f27f3 1088 BT_ASSERT(bt_field_get_class_type(func_field) ==
bc463d34 1089 BT_FIELD_CLASS_TYPE_STRING);
ca9f27f3 1090 BT_ASSERT(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) {
91bc8451 1095 BT_COMP_LOGE("Cannot set \"bin\" bin_field 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) {
91bc8451 1102 BT_COMP_LOGE("Cannot set \"func\" func_field 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) {
91bc8451 1109 BT_COMP_LOGE("Cannot set \"src\" src_field 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
1121 BT_ASSERT(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
1126 BT_ASSERT(bt_field_class_structure_get_member_count(debug_info_fc) == 3);
1127
1128 dbg_info_src = debug_info_query(debug_info, vpid, ip);
1129
3a3d15f3
PP
1130 fill_debug_info_bin_field(dbg_info_src,
1131 debug_info->comp->arg_full_path,
1132 bt_field_structure_borrow_member_field_by_name(
1133 debug_info_field, "bin"),
91bc8451 1134 debug_info->log_level, debug_info->self_comp);
ca9f27f3 1135 fill_debug_info_func_field(dbg_info_src,
3a3d15f3
PP
1136 bt_field_structure_borrow_member_field_by_name(
1137 debug_info_field, "func"),
91bc8451 1138 debug_info->log_level, debug_info->self_comp);
3a3d15f3
PP
1139 fill_debug_info_src_field(dbg_info_src,
1140 debug_info->comp->arg_full_path,
1141 bt_field_structure_borrow_member_field_by_name(
1142 debug_info_field, "src"),
91bc8451 1143 debug_info->log_level, debug_info->self_comp);
ca9f27f3
FD
1144}
1145
1146static
1147void fill_debug_info_event_if_needed(struct debug_info_msg_iter *debug_it,
1148 const bt_event *in_event, bt_event *out_event)
1149{
1150 bt_field *out_common_ctx_field, *out_debug_info_field;
1151 const bt_field *vpid_field, *ip_field, *in_common_ctx_field;
1152 const bt_field_class *in_common_ctx_fc;
1153 struct debug_info *debug_info;
1154 uint64_t vpid;
1155 int64_t ip;
06d1cf5d 1156 gchar *debug_info_field_name =
ca9f27f3 1157 debug_it->debug_info_component->arg_debug_info_field_name;
3a3d15f3 1158 bt_logging_level log_level = debug_it->log_level;
91bc8451 1159 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1160
1161 in_common_ctx_field = bt_event_borrow_common_context_field_const(
bc463d34 1162 in_event);
ca9f27f3
FD
1163 if (!in_common_ctx_field) {
1164 /*
1165 * There is no event common context so no need to add debug
1166 * info field.
1167 */
1168 goto end;
1169 }
1170
1171 in_common_ctx_fc = bt_field_borrow_class_const(in_common_ctx_field);
1172 if (!is_event_common_ctx_dbg_info_compatible(in_common_ctx_fc,
bc463d34 1173 debug_it->ir_maps->debug_info_field_class_name)) {
ca9f27f3
FD
1174 /*
1175 * The input event common context does not have the necessary
1176 * fields to resolve debug information.
1177 */
1178 goto end;
1179 }
1180
1181 /* Borrow the debug-info field. */
1182 out_common_ctx_field = bt_event_borrow_common_context_field(out_event);
1183 if (!out_common_ctx_field) {
1184 goto end;
4f45f9bb
JD
1185 }
1186
ca9f27f3 1187 out_debug_info_field = bt_field_structure_borrow_member_field_by_name(
bc463d34 1188 out_common_ctx_field, debug_info_field_name);
ca9f27f3
FD
1189
1190 vpid_field = bt_field_structure_borrow_member_field_by_name_const(
bc463d34 1191 out_common_ctx_field, VPID_FIELD_NAME);
ca9f27f3 1192 ip_field = bt_field_structure_borrow_member_field_by_name_const(
bc463d34 1193 out_common_ctx_field, IP_FIELD_NAME);
ca9f27f3 1194
9c08c816
PP
1195 vpid = bt_field_integer_signed_get_value(vpid_field);
1196 ip = bt_field_integer_unsigned_get_value(ip_field);
ca9f27f3
FD
1197
1198 /*
1199 * Borrow the debug_info structure needed for the source
1200 * resolving.
1201 */
1202 debug_info = g_hash_table_lookup(debug_it->debug_info_map,
bc463d34
FD
1203 bt_stream_borrow_trace_const(
1204 bt_event_borrow_stream_const(in_event)));
ca9f27f3
FD
1205
1206 if (debug_info) {
1207 /*
1208 * Perform the debug-info resolving and set the event fields
1209 * accordingly.
1210 */
1211 fill_debug_info_field(debug_info, vpid, ip, out_debug_info_field);
1212 } else {
91bc8451 1213 BT_COMP_LOGD("No debug information for this trace. Setting debug "
ca9f27f3 1214 "info fields to empty strings.");
91bc8451
PP
1215 fill_debug_info_field_empty(out_debug_info_field,
1216 log_level, self_comp);
ca9f27f3 1217 }
4f45f9bb
JD
1218end:
1219 return;
1220}
ca9f27f3
FD
1221
1222static
1223void update_event_statedump_if_needed(struct debug_info_msg_iter *debug_it,
1224 const bt_event *in_event)
1225{
1226 const bt_field *event_common_ctx;
1227 const bt_field_class *event_common_ctx_fc;
1228 const bt_event_class *in_event_class = bt_event_borrow_class_const(in_event);
1229
1230 /*
1231 * If the event is an lttng_ust_statedump event AND has the right event
1232 * common context fields update the debug-info view for this process.
1233 */
1234 event_common_ctx = bt_event_borrow_common_context_field_const(in_event);
1235 if (!event_common_ctx) {
1236 goto end;
1237 }
1238
1239 event_common_ctx_fc = bt_field_borrow_class_const(event_common_ctx);
1240 if (is_event_common_ctx_dbg_info_compatible(event_common_ctx_fc,
bc463d34 1241 debug_it->ir_maps->debug_info_field_class_name)) {
ca9f27f3
FD
1242 /* Checkout if it might be a one of lttng ust statedump events. */
1243 const char *in_event_name = bt_event_class_get_name(in_event_class);
1244 if (strncmp(in_event_name, LTTNG_UST_STATEDUMP_PREFIX,
1245 strlen(LTTNG_UST_STATEDUMP_PREFIX)) == 0) {
1246 /* Handle statedump events. */
1247 handle_event_statedump(debug_it, in_event);
1248 }
1249 }
1250end:
1251 return;
1252}
1253
1254static
1255bt_message *handle_event_message(struct debug_info_msg_iter *debug_it,
1256 const bt_message *in_message)
1257{
1258 const bt_clock_snapshot *cs;
1259 const bt_clock_class *default_cc;
1260 const bt_packet *in_packet;
26fc5aed
PP
1261 const bt_stream *in_stream;
1262 const bt_stream *out_stream;
ca9f27f3 1263 bt_event_class *out_event_class;
26fc5aed 1264 bt_packet *out_packet = NULL;
ca9f27f3 1265 bt_event *out_event;
3a3d15f3 1266 bt_logging_level log_level = debug_it->log_level;
91bc8451 1267 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1268
1269 bt_message *out_message = NULL;
1270
1271 /* Borrow the input event and its event class. */
1272 const bt_event *in_event =
1273 bt_message_event_borrow_event_const(in_message);
1274 const bt_event_class *in_event_class =
1275 bt_event_borrow_class_const(in_event);
1276
1277 update_event_statedump_if_needed(debug_it, in_event);
1278
1279 out_event_class = trace_ir_mapping_borrow_mapped_event_class(
1280 debug_it->ir_maps, in_event_class);
1281 if (!out_event_class) {
1282 out_event_class = trace_ir_mapping_create_new_mapped_event_class(
bc463d34 1283 debug_it->ir_maps, in_event_class);
ca9f27f3
FD
1284 }
1285 BT_ASSERT(out_event_class);
1286
26fc5aed
PP
1287 /* Borrow the input stream. */
1288 in_stream = bt_event_borrow_stream_const(in_event);
1289 BT_ASSERT(in_stream);
1290 out_stream = trace_ir_mapping_borrow_mapped_stream(debug_it->ir_maps,
1291 in_stream);
1292 BT_ASSERT(in_stream);
1293
ca9f27f3
FD
1294 /* Borrow the input and output packets. */
1295 in_packet = bt_event_borrow_packet_const(in_event);
26fc5aed
PP
1296 if (in_packet) {
1297 out_packet = trace_ir_mapping_borrow_mapped_packet(
1298 debug_it->ir_maps, in_packet);
1299 }
ca9f27f3
FD
1300
1301 default_cc = bt_stream_class_borrow_default_clock_class_const(
bc463d34 1302 bt_event_class_borrow_stream_class_const(in_event_class));
ca9f27f3
FD
1303 if (default_cc) {
1304 /* Borrow event clock snapshot. */
0cbc2c33 1305 cs = bt_message_event_borrow_default_clock_snapshot_const(
bc463d34 1306 in_message);
ca9f27f3
FD
1307
1308 /* Create an output event message. */
26fc5aed
PP
1309 if (out_packet) {
1310 out_message =
1311 bt_message_event_create_with_packet_and_default_clock_snapshot(
ca9f27f3
FD
1312 debug_it->input_iterator,
1313 out_event_class, out_packet,
1314 bt_clock_snapshot_get_value(cs));
26fc5aed
PP
1315 } else {
1316 out_message =
1317 bt_message_event_create_with_default_clock_snapshot(
1318 debug_it->input_iterator,
1319 out_event_class, out_stream,
1320 bt_clock_snapshot_get_value(cs));
1321 }
ca9f27f3 1322 } else {
26fc5aed
PP
1323 if (out_packet) {
1324 out_message = bt_message_event_create_with_packet(
1325 debug_it->input_iterator, out_event_class,
1326 out_packet);
1327 } else {
1328 out_message = bt_message_event_create(
1329 debug_it->input_iterator, out_event_class,
1330 out_stream);
1331 }
ca9f27f3
FD
1332 }
1333
1334 if (!out_message) {
91bc8451 1335 BT_COMP_LOGE("Error creating output event message.");
ca9f27f3
FD
1336 goto error;
1337 }
1338
1339 out_event = bt_message_event_borrow_event(out_message);
1340
1341 /* Copy the original fields to the output event. */
91bc8451 1342 copy_event_content(in_event, out_event, log_level, self_comp);
ca9f27f3
FD
1343
1344 /*
1345 * Try to set the debug-info fields based on debug information that is
1346 * gathered so far.
1347 */
1348 fill_debug_info_event_if_needed(debug_it, in_event, out_event);
1349
1350error:
1351 return out_message;
1352}
1353
1354static
1355bt_message *handle_stream_begin_message(struct debug_info_msg_iter *debug_it,
1356 const bt_message *in_message)
1357{
1358 const bt_stream *in_stream;
1359 bt_message *out_message;
1360 bt_stream *out_stream;
3a3d15f3 1361 bt_logging_level log_level = debug_it->log_level;
91bc8451 1362 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1363
1364 in_stream = bt_message_stream_beginning_borrow_stream_const(in_message);
1365 BT_ASSERT(in_stream);
1366
1367 /* Create a duplicated output stream. */
1368 out_stream = trace_ir_mapping_create_new_mapped_stream(
bc463d34 1369 debug_it->ir_maps, in_stream);
ca9f27f3
FD
1370 if (!out_stream) {
1371 out_message = NULL;
1372 goto error;
1373 }
1374
1375 /* Create an output stream beginning message. */
1376 out_message = bt_message_stream_beginning_create(
bc463d34 1377 debug_it->input_iterator, out_stream);
ca9f27f3 1378 if (!out_message) {
91bc8451 1379 BT_COMP_LOGE("Error creating output stream beginning message: "
ca9f27f3
FD
1380 "out-s-addr=%p", out_stream);
1381 }
1382error:
1383 return out_message;
1384}
1385
1386static
1387bt_message *handle_stream_end_message(struct debug_info_msg_iter *debug_it,
1388 const bt_message *in_message)
1389{
1390 const bt_stream *in_stream;
1391 bt_message *out_message = NULL;
1392 bt_stream *out_stream;
3a3d15f3 1393 bt_logging_level log_level = debug_it->log_level;
91bc8451 1394 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1395
1396 in_stream = bt_message_stream_end_borrow_stream_const(in_message);
1397 BT_ASSERT(in_stream);
1398
1399 out_stream = trace_ir_mapping_borrow_mapped_stream(
bc463d34 1400 debug_it->ir_maps, in_stream);
ca9f27f3
FD
1401 BT_ASSERT(out_stream);
1402
1403 /* Create an output stream end message. */
1404 out_message = bt_message_stream_end_create(debug_it->input_iterator,
bc463d34 1405 out_stream);
ca9f27f3 1406 if (!out_message) {
91bc8451 1407 BT_COMP_LOGE("Error creating output stream end message: out-s-addr=%p",
ca9f27f3 1408 out_stream);
b80991f6 1409 goto end;
ca9f27f3
FD
1410 }
1411
1412 /* Remove stream from trace mapping hashtable. */
1413 trace_ir_mapping_remove_mapped_stream(debug_it->ir_maps, in_stream);
1414
b80991f6 1415end:
ca9f27f3
FD
1416 return out_message;
1417}
1418
1419static
1420bt_message *handle_packet_begin_message(struct debug_info_msg_iter *debug_it,
1421 const bt_message *in_message)
1422{
649934d2 1423 bool has_default_clock_snapshot;
ca9f27f3
FD
1424 const bt_clock_snapshot *cs;
1425 bt_message *out_message = NULL;
1426 bt_packet *out_packet;
3a3d15f3 1427 bt_logging_level log_level = debug_it->log_level;
91bc8451 1428 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1429
1430 const bt_packet *in_packet =
1431 bt_message_packet_beginning_borrow_packet_const(in_message);
1432 BT_ASSERT(in_packet);
1433
1434 /* This packet should not be already mapped. */
1435 BT_ASSERT(!trace_ir_mapping_borrow_mapped_packet(
1436 debug_it->ir_maps, in_packet));
1437
1438 out_packet = trace_ir_mapping_create_new_mapped_packet(debug_it->ir_maps,
bc463d34 1439 in_packet);
ca9f27f3
FD
1440
1441 BT_ASSERT(out_packet);
1442
649934d2 1443 has_default_clock_snapshot =
9b24b6aa 1444 bt_stream_class_packets_have_beginning_default_clock_snapshot(
ca9f27f3
FD
1445 bt_stream_borrow_class_const(
1446 bt_packet_borrow_stream_const(in_packet)));
649934d2 1447 if (has_default_clock_snapshot) {
ca9f27f3 1448 /* Borrow clock snapshot. */
0cbc2c33 1449 cs = bt_message_packet_beginning_borrow_default_clock_snapshot_const(
bc463d34 1450 in_message);
ca9f27f3
FD
1451
1452 /* Create an output packet beginning message. */
ca9f27f3 1453 out_message = bt_message_packet_beginning_create_with_default_clock_snapshot(
bc463d34
FD
1454 debug_it->input_iterator, out_packet,
1455 bt_clock_snapshot_get_value(cs));
ca9f27f3
FD
1456 } else {
1457 out_message = bt_message_packet_beginning_create(
bc463d34 1458 debug_it->input_iterator, out_packet);
ca9f27f3
FD
1459 }
1460 if (!out_message) {
91bc8451 1461 BT_COMP_LOGE("Error creating output packet beginning message: "
ca9f27f3
FD
1462 "out-p-addr=%p", out_packet);
1463 }
1464
1465 return out_message;
1466}
1467
1468static
1469bt_message *handle_packet_end_message(struct debug_info_msg_iter *debug_it,
1470 const bt_message *in_message)
1471{
649934d2 1472 bool has_default_clock_snapshot;
ca9f27f3
FD
1473 const bt_clock_snapshot *cs;
1474 const bt_packet *in_packet;
ca9f27f3
FD
1475 bt_message *out_message = NULL;
1476 bt_packet *out_packet;
3a3d15f3 1477 bt_logging_level log_level = debug_it->log_level;
91bc8451 1478 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1479
1480 in_packet = bt_message_packet_end_borrow_packet_const(in_message);
1481 BT_ASSERT(in_packet);
1482
1483 out_packet = trace_ir_mapping_borrow_mapped_packet(debug_it->ir_maps, in_packet);
1484 BT_ASSERT(out_packet);
1485
649934d2 1486 has_default_clock_snapshot =
9b24b6aa 1487 bt_stream_class_packets_have_end_default_clock_snapshot(
ca9f27f3
FD
1488 bt_stream_borrow_class_const(
1489 bt_packet_borrow_stream_const(in_packet)));
649934d2 1490 if (has_default_clock_snapshot) {
ca9f27f3 1491 /* Borrow clock snapshot. */
0cbc2c33 1492 cs = bt_message_packet_end_borrow_default_clock_snapshot_const(
bc463d34 1493 in_message);
ca9f27f3
FD
1494
1495 /* Create an outpute packet end message. */
ca9f27f3 1496 out_message = bt_message_packet_end_create_with_default_clock_snapshot(
bc463d34
FD
1497 debug_it->input_iterator, out_packet,
1498 bt_clock_snapshot_get_value(cs));
ca9f27f3
FD
1499 } else {
1500 out_message = bt_message_packet_end_create(
bc463d34 1501 debug_it->input_iterator, out_packet);
ca9f27f3
FD
1502 }
1503
1504 if (!out_message) {
91bc8451 1505 BT_COMP_LOGE("Error creating output packet end message: "
ca9f27f3 1506 "out-p-addr=%p", out_packet);
b80991f6 1507 goto end;
ca9f27f3
FD
1508 }
1509
1510 /* Remove packet from data mapping hashtable. */
1511 trace_ir_mapping_remove_mapped_packet(debug_it->ir_maps, in_packet);
1512
b80991f6 1513end:
ca9f27f3
FD
1514 return out_message;
1515}
1516
1517static
1518bt_message *handle_msg_iterator_inactivity(struct debug_info_msg_iter *debug_it,
1519 const bt_message *in_message)
1520{
1521 /*
1522 * This message type can be forwarded directly because it does
1523 * not refer to any objects in the trace class.
1524 */
1525 bt_message_get_ref(in_message);
1526 return (bt_message*) in_message;
1527}
1528
ca9f27f3
FD
1529static
1530bt_message *handle_discarded_events_message(struct debug_info_msg_iter *debug_it,
1531 const bt_message *in_message)
1532{
1533 const bt_clock_snapshot *begin_cs, *end_cs;
1534 const bt_stream *in_stream;
2e90378a 1535 bool has_default_clock_snapshots;
ca9f27f3 1536 uint64_t discarded_events, begin_cs_value, end_cs_value;
ca9f27f3
FD
1537 bt_property_availability prop_avail;
1538 bt_message *out_message = NULL;
1539 bt_stream *out_stream;
3a3d15f3 1540 bt_logging_level log_level = debug_it->log_level;
91bc8451 1541 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1542
1543 in_stream = bt_message_discarded_events_borrow_stream_const(
1544 in_message);
1545 BT_ASSERT(in_stream);
1546
bc463d34
FD
1547 out_stream = trace_ir_mapping_borrow_mapped_stream( debug_it->ir_maps,
1548 in_stream);
ca9f27f3
FD
1549 BT_ASSERT(out_stream);
1550
2e90378a
PP
1551 has_default_clock_snapshots =
1552 bt_stream_class_discarded_events_have_default_clock_snapshots(
ca9f27f3 1553 bt_stream_borrow_class_const(in_stream));
2e90378a 1554 if (has_default_clock_snapshots) {
bc463d34
FD
1555 begin_cs = bt_message_discarded_events_borrow_beginning_default_clock_snapshot_const(
1556 in_message);
1557 end_cs = bt_message_discarded_events_borrow_end_default_clock_snapshot_const(
1558 in_message);
ca9f27f3
FD
1559
1560 begin_cs_value = bt_clock_snapshot_get_value(begin_cs);
1561 end_cs_value = bt_clock_snapshot_get_value(end_cs);
1562
bc463d34
FD
1563 out_message = bt_message_discarded_events_create_with_default_clock_snapshots(
1564 debug_it->input_iterator, out_stream,
1565 begin_cs_value, end_cs_value);
ca9f27f3
FD
1566 } else {
1567 out_message = bt_message_discarded_events_create(
bc463d34 1568 debug_it->input_iterator, out_stream);
ca9f27f3
FD
1569 }
1570 if (!out_message) {
91bc8451 1571 BT_COMP_LOGE("Error creating output discarded events message: "
ca9f27f3
FD
1572 "out-s-addr=%p", out_stream);
1573 goto error;
1574 }
1575
1576 prop_avail = bt_message_discarded_events_get_count(in_message,
bc463d34 1577 &discarded_events);
ca9f27f3
FD
1578
1579 if (prop_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1580 bt_message_discarded_events_set_count(out_message,
bc463d34 1581 discarded_events);
ca9f27f3
FD
1582 }
1583
1584error:
1585 return out_message;
1586}
1587
1588static
1589bt_message *handle_discarded_packets_message(struct debug_info_msg_iter *debug_it,
1590 const bt_message *in_message)
1591{
1592 const bt_clock_snapshot *begin_cs, *end_cs;
2e90378a 1593 bool has_default_clock_snapshots;
ca9f27f3
FD
1594 const bt_stream *in_stream;
1595 uint64_t discarded_packets, begin_cs_value, end_cs_value;
ca9f27f3
FD
1596 bt_property_availability prop_avail;
1597 bt_message *out_message = NULL;
1598 bt_stream *out_stream;
3a3d15f3 1599 bt_logging_level log_level = debug_it->log_level;
91bc8451 1600 bt_self_component *self_comp = debug_it->self_comp;
ca9f27f3
FD
1601
1602 in_stream = bt_message_discarded_packets_borrow_stream_const(
bc463d34 1603 in_message);
ca9f27f3
FD
1604 BT_ASSERT(in_stream);
1605
bc463d34
FD
1606 out_stream = trace_ir_mapping_borrow_mapped_stream( debug_it->ir_maps,
1607 in_stream);
ca9f27f3
FD
1608 BT_ASSERT(out_stream);
1609
2e90378a
PP
1610 has_default_clock_snapshots =
1611 bt_stream_class_discarded_packets_have_default_clock_snapshots(
ca9f27f3 1612 bt_stream_borrow_class_const(in_stream));
2e90378a 1613 if (has_default_clock_snapshots) {
bc463d34
FD
1614 begin_cs = bt_message_discarded_packets_borrow_beginning_default_clock_snapshot_const(
1615 in_message);
ca9f27f3 1616
bc463d34
FD
1617 end_cs = bt_message_discarded_packets_borrow_end_default_clock_snapshot_const(
1618 in_message);
ca9f27f3
FD
1619
1620 begin_cs_value = bt_clock_snapshot_get_value(begin_cs);
1621 end_cs_value = bt_clock_snapshot_get_value(end_cs);
1622
1623 out_message = bt_message_discarded_packets_create_with_default_clock_snapshots(
bc463d34
FD
1624 debug_it->input_iterator, out_stream,
1625 begin_cs_value, end_cs_value);
ca9f27f3
FD
1626 } else {
1627 out_message = bt_message_discarded_packets_create(
bc463d34 1628 debug_it->input_iterator, out_stream);
ca9f27f3
FD
1629 }
1630 if (!out_message) {
91bc8451 1631 BT_COMP_LOGE("Error creating output discarded packet message: "
ca9f27f3
FD
1632 "out-s-addr=%p", out_stream);
1633 goto error;
1634 }
1635
1636 prop_avail = bt_message_discarded_packets_get_count(in_message,
bc463d34 1637 &discarded_packets);
ca9f27f3
FD
1638 if (prop_avail == BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE) {
1639 bt_message_discarded_packets_set_count(out_message,
bc463d34 1640 discarded_packets);
ca9f27f3
FD
1641 }
1642
1643error:
1644 return out_message;
1645}
1646
1647static
1648const bt_message *handle_message(struct debug_info_msg_iter *debug_it,
1649 const bt_message *in_message)
1650{
1651 bt_message *out_message = NULL;
1652
1653 switch (bt_message_get_type(in_message)) {
1654 case BT_MESSAGE_TYPE_EVENT:
bc463d34 1655 out_message = handle_event_message(debug_it, in_message);
ca9f27f3
FD
1656 break;
1657 case BT_MESSAGE_TYPE_PACKET_BEGINNING:
bc463d34 1658 out_message = handle_packet_begin_message(debug_it, in_message);
ca9f27f3
FD
1659 break;
1660 case BT_MESSAGE_TYPE_PACKET_END:
bc463d34 1661 out_message = handle_packet_end_message(debug_it, in_message);
ca9f27f3
FD
1662 break;
1663 case BT_MESSAGE_TYPE_STREAM_BEGINNING:
bc463d34 1664 out_message = handle_stream_begin_message(debug_it, in_message);
ca9f27f3
FD
1665 break;
1666 case BT_MESSAGE_TYPE_STREAM_END:
bc463d34 1667 out_message = handle_stream_end_message(debug_it, in_message);
ca9f27f3
FD
1668 break;
1669 case BT_MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY:
bc463d34 1670 out_message = handle_msg_iterator_inactivity(debug_it, in_message);
ca9f27f3 1671 break;
ca9f27f3 1672 case BT_MESSAGE_TYPE_DISCARDED_EVENTS:
bc463d34 1673 out_message = handle_discarded_events_message(debug_it, in_message);
ca9f27f3
FD
1674 break;
1675 case BT_MESSAGE_TYPE_DISCARDED_PACKETS:
bc463d34 1676 out_message = handle_discarded_packets_message(debug_it, in_message);
ca9f27f3
FD
1677 break;
1678 default:
1679 abort();
1680 break;
1681 }
1682
1683 return out_message;
1684}
1685
1686static
f4f11e84
SM
1687bt_component_class_initialize_method_status init_from_params(
1688 struct debug_info_component *debug_info_component,
ca9f27f3
FD
1689 const bt_value *params)
1690{
f4f11e84
SM
1691 const bt_value *value;
1692 bt_component_class_initialize_method_status status;
ca9f27f3
FD
1693
1694 BT_ASSERT(params);
1695
1696 value = bt_value_map_borrow_entry_value_const(params,
1697 "debug-info-field-name");
1698 if (value) {
ca9f27f3 1699 debug_info_component->arg_debug_info_field_name =
06d1cf5d 1700 g_strdup(bt_value_string_get(value));
ca9f27f3
FD
1701 } else {
1702 debug_info_component->arg_debug_info_field_name =
06d1cf5d 1703 g_strdup(DEFAULT_DEBUG_INFO_FIELD_NAME);
ca9f27f3
FD
1704 }
1705
1706 value = bt_value_map_borrow_entry_value_const(params, "debug-info-dir");
1707 if (value) {
06d1cf5d
FD
1708 debug_info_component->arg_debug_dir =
1709 g_strdup(bt_value_string_get(value));
1710 } else {
1711 debug_info_component->arg_debug_dir = NULL;
ca9f27f3
FD
1712 }
1713
06d1cf5d 1714
ca9f27f3
FD
1715 value = bt_value_map_borrow_entry_value_const(params, "target-prefix");
1716 if (value) {
1717 debug_info_component->arg_target_prefix =
06d1cf5d
FD
1718 g_strdup(bt_value_string_get(value));
1719 } else {
1720 debug_info_component->arg_target_prefix = NULL;
ca9f27f3
FD
1721 }
1722
1723 value = bt_value_map_borrow_entry_value_const(params, "full-path");
1724 if (value) {
1725 debug_info_component->arg_full_path = bt_value_bool_get(value);
06d1cf5d
FD
1726 } else {
1727 debug_info_component->arg_full_path = BT_FALSE;
ca9f27f3
FD
1728 }
1729
f4f11e84
SM
1730 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
1731
1732 return status;
ca9f27f3
FD
1733}
1734
1735BT_HIDDEN
21a9f056 1736bt_component_class_initialize_method_status debug_info_comp_init(
91bc8451 1737 bt_self_component_filter *self_comp_flt,
59225a3e 1738 bt_self_component_filter_configuration *config,
c88dd1cb 1739 const bt_value *params, __attribute__((unused)) void *init_method_data)
ca9f27f3 1740{
ca9f27f3 1741 struct debug_info_component *debug_info_comp;
21a9f056
FD
1742 bt_component_class_initialize_method_status status =
1743 BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
d24d5663 1744 bt_self_component_add_port_status add_port_status;
91bc8451
PP
1745 bt_self_component *self_comp =
1746 bt_self_component_filter_as_self_component(self_comp_flt);
3a3d15f3 1747 bt_logging_level log_level = bt_component_get_logging_level(
91bc8451 1748 bt_self_component_as_component(self_comp));
ca9f27f3 1749
91bc8451 1750 BT_COMP_LOGI("Initializing debug_info component: "
ca9f27f3
FD
1751 "comp-addr=%p, params-addr=%p", self_comp, params);
1752
1753 debug_info_comp = g_new0(struct debug_info_component, 1);
1754 if (!debug_info_comp) {
91bc8451 1755 BT_COMP_LOGE_STR("Failed to allocate one debug_info component.");
ca9f27f3
FD
1756 goto error;
1757 }
1758
3a3d15f3 1759 debug_info_comp->log_level = log_level;
91bc8451
PP
1760 debug_info_comp->self_comp = self_comp;
1761 bt_self_component_set_data(self_comp, debug_info_comp);
ca9f27f3 1762
d24d5663
PP
1763 add_port_status = bt_self_component_filter_add_input_port(
1764 self_comp_flt, "in", NULL, NULL);
f4f11e84
SM
1765 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
1766 status = (int) add_port_status;
ca9f27f3
FD
1767 goto error;
1768 }
1769
d24d5663 1770 add_port_status = bt_self_component_filter_add_output_port(
f4f11e84
SM
1771 self_comp_flt, "out", NULL, NULL);
1772 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
1773 status = (int) add_port_status;
ca9f27f3
FD
1774 goto error;
1775 }
1776
f4f11e84
SM
1777 status = init_from_params(debug_info_comp, params);
1778 if (status != BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
91bc8451 1779 BT_COMP_LOGE("Cannot configure debug_info component: "
ca9f27f3
FD
1780 "debug_info-comp-addr=%p, params-addr=%p",
1781 debug_info_comp, params);
1782 goto error;
1783 }
1784
1785 goto end;
1786
1787error:
1788 destroy_debug_info_comp(debug_info_comp);
91bc8451 1789 bt_self_component_set_data(self_comp, NULL);
ca9f27f3 1790
21a9f056
FD
1791 if (status == BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK) {
1792 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
ca9f27f3
FD
1793 }
1794end:
1795 return status;
1796}
1797
1798BT_HIDDEN
91bc8451 1799void debug_info_comp_finalize(bt_self_component_filter *self_comp_flt)
ca9f27f3
FD
1800{
1801 struct debug_info_component *debug_info =
1802 bt_self_component_get_data(
bc463d34
FD
1803 bt_self_component_filter_as_self_component(
1804 self_comp_flt));
3a3d15f3 1805 bt_logging_level log_level = debug_info->log_level;
91bc8451 1806 bt_self_component *self_comp = debug_info->self_comp;
3a3d15f3 1807
91bc8451 1808 BT_COMP_LOGI("Finalizing debug_info self_component: comp-addr=%p",
ca9f27f3
FD
1809 self_comp);
1810
1811 destroy_debug_info_comp(debug_info);
1812}
1813
1814BT_HIDDEN
d24d5663 1815bt_component_class_message_iterator_next_method_status debug_info_msg_iter_next(
ca9f27f3
FD
1816 bt_self_message_iterator *self_msg_iter,
1817 const bt_message_array_const msgs, uint64_t capacity,
1818 uint64_t *count)
1819{
1820 bt_self_component_port_input_message_iterator *upstream_iterator = NULL;
d24d5663 1821 bt_message_iterator_next_status upstream_iterator_ret_status;
ca9f27f3
FD
1822 struct debug_info_msg_iter *debug_info_msg_iter;
1823 struct debug_info_component *debug_info = NULL;
d24d5663 1824 bt_component_class_message_iterator_next_method_status status;
ca9f27f3
FD
1825 bt_self_component *self_comp = NULL;
1826 bt_message_array_const input_msgs;
1827 const bt_message *out_message;
1828 uint64_t curr_msg_idx, i;
1829
d24d5663 1830 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_OK;
ca9f27f3
FD
1831
1832 self_comp = bt_self_message_iterator_borrow_component(self_msg_iter);
1833 BT_ASSERT(self_comp);
1834
1835 debug_info = bt_self_component_get_data(self_comp);
1836 BT_ASSERT(debug_info);
1837
1838 debug_info_msg_iter = bt_self_message_iterator_get_data(self_msg_iter);
1839 BT_ASSERT(debug_info_msg_iter);
1840
1841 upstream_iterator = debug_info_msg_iter->msg_iter;
1842 BT_ASSERT(upstream_iterator);
1843
1844 upstream_iterator_ret_status =
1845 bt_self_component_port_input_message_iterator_next(
bc463d34 1846 upstream_iterator, &input_msgs, count);
d24d5663
PP
1847 if (upstream_iterator_ret_status !=
1848 BT_MESSAGE_ITERATOR_NEXT_STATUS_OK) {
ca9f27f3 1849 /*
d24d5663
PP
1850 * No messages were returned. Not necessarily an error.
1851 * Convert the upstream message iterator status to a
1852 * self status.
ca9f27f3 1853 */
d24d5663 1854 status = (int) upstream_iterator_ret_status;
ca9f27f3
FD
1855 goto end;
1856 }
1857
1858 /*
1859 * There should never be more received messages than the capacity we
1860 * provided.
1861 */
1862 BT_ASSERT(*count <= capacity);
1863
1864 for (curr_msg_idx = 0; curr_msg_idx < *count; curr_msg_idx++) {
1865 out_message = handle_message(debug_info_msg_iter,
d24d5663 1866 input_msgs[curr_msg_idx]);
ca9f27f3
FD
1867 if (!out_message) {
1868 goto handle_msg_error;
1869 }
1870
1871 msgs[curr_msg_idx] = out_message;
1872 /*
1873 * Drop our reference of the input message as we are done with
1874 * it and created a output copy.
1875 */
1876 bt_message_put_ref(input_msgs[curr_msg_idx]);
1877 }
1878
1879 goto end;
1880
1881handle_msg_error:
1882 /*
1883 * Drop references of all the output messages created before the
1884 * failure.
1885 */
1886 for (i = 0; i < curr_msg_idx; i++) {
1887 bt_message_put_ref(msgs[i]);
1888 }
1889
acc179dd
FD
1890 /*
1891 * Drop references of all the input messages not dropped before the
1892 * failure.
1893 */
1894 for (i = curr_msg_idx; i < *count; i++) {
1895 bt_message_put_ref(input_msgs[i]);
1896 }
1897
d24d5663
PP
1898 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_NEXT_METHOD_STATUS_MEMORY_ERROR;
1899
ca9f27f3
FD
1900end:
1901 return status;
1902}
1903
cbba2996
FD
1904static
1905void debug_info_msg_iter_destroy(struct debug_info_msg_iter *debug_info_msg_iter)
1906{
1907 if (!debug_info_msg_iter) {
1908 goto end;
1909 }
1910
1911 if (debug_info_msg_iter->msg_iter) {
1912 bt_self_component_port_input_message_iterator_put_ref(
bc463d34 1913 debug_info_msg_iter->msg_iter);
cbba2996
FD
1914 }
1915
1916 if (debug_info_msg_iter->ir_maps) {
1917 trace_ir_maps_destroy(debug_info_msg_iter->ir_maps);
1918 }
1919
1920 if (debug_info_msg_iter->debug_info_map) {
1921 g_hash_table_destroy(debug_info_msg_iter->debug_info_map);
1922 }
1923
1924 bt_fd_cache_fini(&debug_info_msg_iter->fd_cache);
1925 g_free(debug_info_msg_iter);
1926
1927end:
1928 return;
1929}
1930
ca9f27f3 1931BT_HIDDEN
21a9f056 1932bt_component_class_message_iterator_initialize_method_status debug_info_msg_iter_init(
ca9f27f3 1933 bt_self_message_iterator *self_msg_iter,
8d8b141d 1934 bt_self_message_iterator_configuration *config,
91bc8451 1935 bt_self_component_filter *self_comp_flt,
ca9f27f3
FD
1936 bt_self_component_port_output *self_port)
1937{
21a9f056 1938 bt_component_class_message_iterator_initialize_method_status status;
e803df70
SM
1939 bt_self_component_port_input_message_iterator_create_from_message_iterator_status
1940 msg_iter_status;
cbba2996
FD
1941 struct bt_self_component_port_input *input_port = NULL;
1942 bt_self_component_port_input_message_iterator *upstream_iterator = NULL;
1943 struct debug_info_msg_iter *debug_info_msg_iter = NULL;
06d1cf5d 1944 gchar *debug_info_field_name;
1e638f98 1945 int ret;
91bc8451
PP
1946 bt_self_component *self_comp =
1947 bt_self_component_filter_as_self_component(self_comp_flt);
3a3d15f3 1948 bt_logging_level log_level = bt_component_get_logging_level(
91bc8451 1949 bt_self_component_as_component(self_comp));
ca9f27f3
FD
1950
1951 /* Borrow the upstream input port. */
1952 input_port = bt_self_component_filter_borrow_input_port_by_name(
91bc8451 1953 self_comp_flt, "in");
ca9f27f3 1954 if (!input_port) {
21a9f056 1955 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_ERROR;
cbba2996
FD
1956 goto error;
1957 }
1958
1959 debug_info_msg_iter = g_new0(struct debug_info_msg_iter, 1);
1960 if (!debug_info_msg_iter) {
21a9f056 1961 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
cbba2996 1962 goto error;
ca9f27f3
FD
1963 }
1964
3a3d15f3 1965 debug_info_msg_iter->log_level = log_level;
91bc8451 1966 debug_info_msg_iter->self_comp = self_comp;
3a3d15f3 1967
ca9f27f3 1968 /* Create an iterator on the upstream component. */
e803df70
SM
1969 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_message_iterator(
1970 self_msg_iter, input_port, &upstream_iterator);
1971 if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_MESSAGE_ITERATOR_STATUS_OK) {
1972 status = (int) msg_iter_status;
cbba2996 1973 goto error;
ca9f27f3
FD
1974 }
1975
cbba2996
FD
1976 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
1977 debug_info_msg_iter->msg_iter, upstream_iterator);
ca9f27f3 1978
cbba2996 1979 /* Create hashtable that will contain debug info mapping. */
ca9f27f3 1980 debug_info_msg_iter->debug_info_map = g_hash_table_new_full(
cbba2996
FD
1981 g_direct_hash, g_direct_equal, (GDestroyNotify) NULL,
1982 (GDestroyNotify) debug_info_destroy);
ca9f27f3 1983 if (!debug_info_msg_iter->debug_info_map) {
21a9f056 1984 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
cbba2996 1985 goto error;
ca9f27f3
FD
1986 }
1987
91bc8451
PP
1988 debug_info_msg_iter->debug_info_component =
1989 bt_self_component_get_data(self_comp);
ca9f27f3 1990
06d1cf5d
FD
1991 debug_info_field_name =
1992 debug_info_msg_iter->debug_info_component->arg_debug_info_field_name;
1993
91bc8451 1994 debug_info_msg_iter->ir_maps = trace_ir_maps_create(self_comp,
3a3d15f3 1995 debug_info_field_name, log_level);
ca9f27f3 1996 if (!debug_info_msg_iter->ir_maps) {
21a9f056 1997 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
cbba2996 1998 goto error;
ca9f27f3
FD
1999 }
2000
3a3d15f3 2001 ret = bt_fd_cache_init(&debug_info_msg_iter->fd_cache, log_level);
1e638f98 2002 if (ret) {
21a9f056 2003 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
cbba2996 2004 goto error;
1e638f98
FD
2005 }
2006
c0e46a7c
SM
2007 bt_self_message_iterator_configuration_set_can_seek_forward(config,
2008 bt_self_component_port_input_message_iterator_can_seek_forward(
2009 debug_info_msg_iter->msg_iter));
2010
ca9f27f3 2011 bt_self_message_iterator_set_data(self_msg_iter, debug_info_msg_iter);
ca9f27f3
FD
2012 debug_info_msg_iter->input_iterator = self_msg_iter;
2013
21a9f056 2014 status = BT_COMPONENT_CLASS_MESSAGE_ITERATOR_INITIALIZE_METHOD_STATUS_OK;
cbba2996
FD
2015 goto end;
2016
2017error:
2018 debug_info_msg_iter_destroy(debug_info_msg_iter);
d24d5663 2019
ca9f27f3
FD
2020end:
2021 return status;
2022}
2023
2024BT_HIDDEN
f2fb1b32 2025bt_component_class_message_iterator_can_seek_beginning_method_status
bc463d34
FD
2026debug_info_msg_iter_can_seek_beginning(bt_self_message_iterator *self_msg_iter,
2027 bt_bool *can_seek)
ca9f27f3
FD
2028{
2029 struct debug_info_msg_iter *debug_info_msg_iter =
2030 bt_self_message_iterator_get_data(self_msg_iter);
2031 BT_ASSERT(debug_info_msg_iter);
2032
f2fb1b32
SM
2033 return (int) bt_self_component_port_input_message_iterator_can_seek_beginning(
2034 debug_info_msg_iter->msg_iter, can_seek);
ca9f27f3
FD
2035}
2036
2037BT_HIDDEN
bc463d34
FD
2038bt_component_class_message_iterator_seek_beginning_method_status
2039debug_info_msg_iter_seek_beginning(bt_self_message_iterator *self_msg_iter)
ca9f27f3
FD
2040{
2041 struct debug_info_msg_iter *debug_info_msg_iter =
2042 bt_self_message_iterator_get_data(self_msg_iter);
d24d5663
PP
2043 bt_component_class_message_iterator_seek_beginning_method_status status =
2044 BT_COMPONENT_CLASS_MESSAGE_ITERATOR_SEEK_BEGINNING_METHOD_STATUS_OK;
2045 bt_message_iterator_seek_beginning_status seek_beg_status;
ca9f27f3
FD
2046
2047 BT_ASSERT(debug_info_msg_iter);
2048
2049 /* Ask the upstream component to seek to the beginning. */
d24d5663 2050 seek_beg_status = bt_self_component_port_input_message_iterator_seek_beginning(
ca9f27f3 2051 debug_info_msg_iter->msg_iter);
d24d5663
PP
2052 if (seek_beg_status != BT_MESSAGE_ITERATOR_SEEK_BEGINNING_STATUS_OK) {
2053 status = (int) seek_beg_status;
ca9f27f3
FD
2054 goto end;
2055 }
2056
2057 /* Clear this iterator data. */
2058 trace_ir_maps_clear(debug_info_msg_iter->ir_maps);
2059 g_hash_table_remove_all(debug_info_msg_iter->debug_info_map);
d24d5663 2060
ca9f27f3 2061end:
d24d5663 2062 return status;
ca9f27f3
FD
2063}
2064
2065BT_HIDDEN
2066void debug_info_msg_iter_finalize(bt_self_message_iterator *it)
2067{
2068 struct debug_info_msg_iter *debug_info_msg_iter;
2069
2070 debug_info_msg_iter = bt_self_message_iterator_get_data(it);
2071 BT_ASSERT(debug_info_msg_iter);
2072
cbba2996 2073 debug_info_msg_iter_destroy(debug_info_msg_iter);
ca9f27f3 2074}
This page took 0.163626 seconds and 4 git commands to generate.