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