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