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