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