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