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