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