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