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