Fix debug-info: handle event layouts of lttng-ust traces prior to 2.9
[babeltrace.git] / plugins / lttng-utils / debug-info.c
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>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include <assert.h>
29 #include <glib.h>
30 #include "debug-info.h"
31 #include "bin-info.h"
32 #include "utils.h"
33 #include "copy.h"
34
35 struct proc_debug_info_sources {
36 /*
37 * Hash table: base address (pointer to uint64_t) to bin info; owned by
38 * proc_debug_info_sources.
39 */
40 GHashTable *baddr_to_bin_info;
41
42 /*
43 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
44 * owned by proc_debug_info_sources.
45 */
46 GHashTable *ip_to_debug_info_src;
47 };
48
49 struct debug_info {
50 struct debug_info_component *comp;
51
52 /*
53 * Hash table of VPIDs (pointer to int64_t) to
54 * (struct ctf_proc_debug_infos*); owned by debug_info.
55 */
56 GHashTable *vpid_to_proc_dbg_info_src;
57 GQuark q_statedump_bin_info;
58 GQuark q_statedump_debug_link;
59 GQuark q_statedump_build_id;
60 GQuark q_statedump_start;
61 GQuark q_dl_open;
62 GQuark q_lib_load;
63 GQuark q_lib_unload;
64 };
65
66 static
67 int debug_info_init(struct debug_info *info)
68 {
69 info->q_statedump_bin_info = g_quark_from_string(
70 "lttng_ust_statedump:bin_info");
71 info->q_statedump_debug_link = g_quark_from_string(
72 "lttng_ust_statedump:debug_link)");
73 info->q_statedump_build_id = g_quark_from_string(
74 "lttng_ust_statedump:build_id");
75 info->q_statedump_start = g_quark_from_string(
76 "lttng_ust_statedump:start");
77 info->q_dl_open = g_quark_from_string("lttng_ust_dl:dlopen");
78 info->q_lib_load = g_quark_from_string("lttng_ust_lib:load");
79 info->q_lib_unload = g_quark_from_string("lttng_ust_lib:unload");
80
81 return bin_info_init();
82 }
83
84 static
85 void debug_info_source_destroy(struct debug_info_source *debug_info_src)
86 {
87 if (!debug_info_src) {
88 return;
89 }
90
91 free(debug_info_src->func);
92 free(debug_info_src->src_path);
93 free(debug_info_src->bin_path);
94 free(debug_info_src->bin_loc);
95 g_free(debug_info_src);
96 }
97
98 static
99 struct debug_info_source *debug_info_source_create_from_bin(struct bin_info *bin,
100 uint64_t ip)
101 {
102 int ret;
103 struct debug_info_source *debug_info_src = NULL;
104 struct source_location *src_loc = NULL;
105
106 debug_info_src = g_new0(struct debug_info_source, 1);
107
108 if (!debug_info_src) {
109 goto end;
110 }
111
112 /* Lookup function name */
113 ret = bin_info_lookup_function_name(bin, ip, &debug_info_src->func);
114 if (ret) {
115 goto error;
116 }
117
118 /* Can't retrieve src_loc from ELF, or could not find binary, skip. */
119 if (!bin->is_elf_only || !debug_info_src->func) {
120 /* Lookup source location */
121 ret = bin_info_lookup_source_location(bin, ip, &src_loc);
122 printf_verbose("Failed to lookup source location (err: %i)\n", ret);
123 }
124
125 if (src_loc) {
126 debug_info_src->line_no = src_loc->line_no;
127
128 if (src_loc->filename) {
129 debug_info_src->src_path = strdup(src_loc->filename);
130 if (!debug_info_src->src_path) {
131 goto error;
132 }
133
134 debug_info_src->short_src_path = get_filename_from_path(
135 debug_info_src->src_path);
136 }
137
138 source_location_destroy(src_loc);
139 }
140
141 if (bin->elf_path) {
142 debug_info_src->bin_path = strdup(bin->elf_path);
143 if (!debug_info_src->bin_path) {
144 goto error;
145 }
146
147 debug_info_src->short_bin_path = get_filename_from_path(
148 debug_info_src->bin_path);
149
150 ret = bin_info_get_bin_loc(bin, ip, &(debug_info_src->bin_loc));
151 if (ret) {
152 goto error;
153 }
154 }
155
156 end:
157 return debug_info_src;
158
159 error:
160 debug_info_source_destroy(debug_info_src);
161 return NULL;
162 }
163
164 static
165 void proc_debug_info_sources_destroy(
166 struct proc_debug_info_sources *proc_dbg_info_src)
167 {
168 if (!proc_dbg_info_src) {
169 return;
170 }
171
172 if (proc_dbg_info_src->baddr_to_bin_info) {
173 g_hash_table_destroy(proc_dbg_info_src->baddr_to_bin_info);
174 }
175
176 if (proc_dbg_info_src->ip_to_debug_info_src) {
177 g_hash_table_destroy(proc_dbg_info_src->ip_to_debug_info_src);
178 }
179
180 g_free(proc_dbg_info_src);
181 }
182
183 static
184 struct proc_debug_info_sources *proc_debug_info_sources_create(void)
185 {
186 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
187
188 proc_dbg_info_src = g_new0(struct proc_debug_info_sources, 1);
189 if (!proc_dbg_info_src) {
190 goto end;
191 }
192
193 proc_dbg_info_src->baddr_to_bin_info = g_hash_table_new_full(
194 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
195 (GDestroyNotify) bin_info_destroy);
196 if (!proc_dbg_info_src->baddr_to_bin_info) {
197 goto error;
198 }
199
200 proc_dbg_info_src->ip_to_debug_info_src = g_hash_table_new_full(
201 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
202 (GDestroyNotify) debug_info_source_destroy);
203 if (!proc_dbg_info_src->ip_to_debug_info_src) {
204 goto error;
205 }
206
207 end:
208 return proc_dbg_info_src;
209
210 error:
211 proc_debug_info_sources_destroy(proc_dbg_info_src);
212 return NULL;
213 }
214
215 static
216 struct proc_debug_info_sources *proc_debug_info_sources_ht_get_entry(
217 GHashTable *ht, int64_t vpid)
218 {
219 gpointer key = g_new0(int64_t, 1);
220 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
221
222 if (!key) {
223 goto end;
224 }
225
226 *((int64_t *) key) = vpid;
227
228 /* Exists? Return it */
229 proc_dbg_info_src = g_hash_table_lookup(ht, key);
230 if (proc_dbg_info_src) {
231 goto end;
232 }
233
234 /* Otherwise, create and return it */
235 proc_dbg_info_src = proc_debug_info_sources_create();
236 if (!proc_dbg_info_src) {
237 goto end;
238 }
239
240 g_hash_table_insert(ht, key, proc_dbg_info_src);
241 /* Ownership passed to ht */
242 key = NULL;
243 end:
244 g_free(key);
245 return proc_dbg_info_src;
246 }
247
248 static
249 struct debug_info_source *proc_debug_info_sources_get_entry(
250 struct proc_debug_info_sources *proc_dbg_info_src, uint64_t ip)
251 {
252 struct debug_info_source *debug_info_src = NULL;
253 gpointer key = g_new0(uint64_t, 1);
254 GHashTableIter iter;
255 gpointer baddr, value;
256
257 if (!key) {
258 goto end;
259 }
260
261 *((uint64_t *) key) = ip;
262
263 /* Look in IP to debug infos hash table first. */
264 debug_info_src = g_hash_table_lookup(
265 proc_dbg_info_src->ip_to_debug_info_src,
266 key);
267 if (debug_info_src) {
268 goto end;
269 }
270
271 /* Check in all bin_infos. */
272 g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_bin_info);
273
274 while (g_hash_table_iter_next(&iter, &baddr, &value))
275 {
276 struct bin_info *bin = value;
277
278 if (!bin_info_has_address(value, ip)) {
279 continue;
280 }
281
282 /*
283 * Found; add it to cache.
284 *
285 * FIXME: this should be bounded in size (and implement
286 * a caching policy), and entries should be prunned when
287 * libraries are unmapped.
288 */
289 debug_info_src = debug_info_source_create_from_bin(bin, ip);
290 if (debug_info_src) {
291 g_hash_table_insert(
292 proc_dbg_info_src->ip_to_debug_info_src,
293 key, debug_info_src);
294 /* Ownership passed to ht. */
295 key = NULL;
296 }
297 break;
298 }
299
300 end:
301 free(key);
302 return debug_info_src;
303 }
304
305 BT_HIDDEN
306 struct debug_info_source *debug_info_query(struct debug_info *debug_info,
307 int64_t vpid, uint64_t ip)
308 {
309 struct debug_info_source *dbg_info_src = NULL;
310 struct proc_debug_info_sources *proc_dbg_info_src;
311
312 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
313 debug_info->vpid_to_proc_dbg_info_src, vpid);
314 if (!proc_dbg_info_src) {
315 goto end;
316 }
317
318 dbg_info_src = proc_debug_info_sources_get_entry(proc_dbg_info_src, ip);
319
320 end:
321 return dbg_info_src;
322 }
323
324 BT_HIDDEN
325 struct debug_info *debug_info_create(struct debug_info_component *comp)
326 {
327 int ret;
328 struct debug_info *debug_info;
329
330 debug_info = g_new0(struct debug_info, 1);
331 if (!debug_info) {
332 goto end;
333 }
334
335 debug_info->vpid_to_proc_dbg_info_src = g_hash_table_new_full(
336 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
337 (GDestroyNotify) proc_debug_info_sources_destroy);
338 if (!debug_info->vpid_to_proc_dbg_info_src) {
339 goto error;
340 }
341
342 debug_info->comp = comp;
343 ret = debug_info_init(debug_info);
344 if (ret) {
345 goto error;
346 }
347
348 end:
349 return debug_info;
350 error:
351 g_free(debug_info);
352 return NULL;
353 }
354
355 BT_HIDDEN
356 void debug_info_destroy(struct debug_info *debug_info)
357 {
358 if (!debug_info) {
359 goto end;
360 }
361
362 if (debug_info->vpid_to_proc_dbg_info_src) {
363 g_hash_table_destroy(debug_info->vpid_to_proc_dbg_info_src);
364 }
365
366 g_free(debug_info);
367 end:
368 return;
369 }
370
371 static
372 void handle_statedump_build_id_event(FILE *err, struct debug_info *debug_info,
373 struct bt_ctf_event *event)
374 {
375 struct proc_debug_info_sources *proc_dbg_info_src;
376 struct bin_info *bin = NULL;
377 int ret;
378 int64_t vpid;
379 uint64_t baddr;
380
381 ret = get_stream_event_context_int_field_value(err,
382 event, "_vpid", &vpid);
383 if (ret) {
384 goto end;
385 }
386
387 ret = get_payload_unsigned_int_field_value(err,
388 event, "_baddr", &baddr);
389 if (ret) {
390 fprintf(err, "[error] %s in %s:%d\n", __func__,
391 __FILE__, __LINE__);
392 goto end;
393 }
394
395 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
396 debug_info->vpid_to_proc_dbg_info_src, vpid);
397 if (!proc_dbg_info_src) {
398 goto end;
399 }
400
401 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
402 (gpointer) &baddr);
403 if (!bin) {
404 /*
405 * The build_id event comes after the bin has been
406 * created. If it isn't found, just ignore this event.
407 */
408 goto end;
409 }
410
411 ret = get_payload_build_id_field_value(err, event, "_build_id",
412 &bin->build_id, &bin->build_id_len);
413 if (ret) {
414 fprintf(err, "[error] %s in %s:%d\n", __func__,
415 __FILE__, __LINE__);
416 goto end;
417 }
418
419 /*
420 * Reset the is_elf_only flag in case it had been set
421 * previously, because we might find separate debug info using
422 * the new build id information.
423 */
424 bin->is_elf_only = false;
425
426 // TODO
427 // bin_info_set_build_id(bin, build_id, build_id_len);
428
429 end:
430 return;
431 }
432
433 static
434 void handle_statedump_debug_link_event(FILE *err, struct debug_info *debug_info,
435 struct bt_ctf_event *event)
436 {
437 struct proc_debug_info_sources *proc_dbg_info_src;
438 struct bin_info *bin = NULL;
439 int64_t vpid;
440 uint64_t baddr;
441 const char *filename = NULL;
442 uint32_t crc32;
443 uint64_t tmp;
444 int ret;
445
446 ret = get_stream_event_context_int_field_value(err, event,
447 "_vpid", &vpid);
448 if (ret) {
449 goto end;
450 }
451
452 ret = get_payload_unsigned_int_field_value(err,
453 event, "_baddr", &baddr);
454 if (ret) {
455 fprintf(err, "[error] %s in %s:%d\n", __func__,
456 __FILE__, __LINE__);
457 ret = -1;
458 goto end;
459 }
460
461 ret = get_payload_unsigned_int_field_value(err, event, "_crc32", &tmp);
462 if (ret) {
463 fprintf(err, "[error] %s in %s:%d\n", __func__,
464 __FILE__, __LINE__);
465 ret = -1;
466 goto end;
467 }
468 crc32 = (uint32_t) tmp;
469
470 ret = get_payload_string_field_value(err,
471 event, "_filename", &filename);
472 if (ret) {
473 fprintf(err, "[error] %s in %s:%d\n", __func__,
474 __FILE__, __LINE__);
475 ret = -1;
476 goto end;
477 }
478
479 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
480 debug_info->vpid_to_proc_dbg_info_src, vpid);
481 if (!proc_dbg_info_src) {
482 goto end;
483 }
484
485 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
486 (gpointer) &baddr);
487 if (!bin) {
488 /*
489 * The debug_link event comes after the bin has been
490 * created. If it isn't found, just ignore this event.
491 */
492 goto end;
493 }
494
495 bin_info_set_debug_link(bin, filename, crc32);
496
497 end:
498 return;
499 }
500
501 static
502 void handle_bin_info_event(FILE *err, struct debug_info *debug_info,
503 struct bt_ctf_event *event, bool has_pic_field)
504 {
505 struct proc_debug_info_sources *proc_dbg_info_src;
506 struct bin_info *bin;
507 uint64_t baddr, memsz;
508 int64_t vpid;
509 const char *path;
510 gpointer key = NULL;
511 bool is_pic;
512 int ret;
513
514 ret = get_payload_unsigned_int_field_value(err,
515 event, "_baddr", &baddr);
516 if (ret) {
517 fprintf(err, "[error] %s in %s:%d\n", __func__,
518 __FILE__, __LINE__);
519 goto end;
520 }
521
522 ret = get_payload_unsigned_int_field_value(err,
523 event, "_memsz", &memsz);
524 if (ret) {
525 fprintf(err, "[error] %s in %s:%d\n", __func__,
526 __FILE__, __LINE__);
527 goto end;
528 }
529
530 /*
531 * This field is not produced by the dlopen event emitted before
532 * lttng-ust 2.9.
533 */
534 ret = get_payload_string_field_value(err,
535 event, "_path", &path);
536 if (ret || !path) {
537 goto end;
538 }
539
540 if (has_pic_field) {
541 uint64_t tmp;
542
543 ret = get_payload_unsigned_int_field_value(err,
544 event, "_is_pic", &tmp);
545 if (ret) {
546 fprintf(err, "[error] %s in %s:%d\n", __func__,
547 __FILE__, __LINE__);
548 ret = -1;
549 goto end;
550 }
551 is_pic = (tmp == 1);
552 } else {
553 /*
554 * dlopen has no is_pic field, because the shared
555 * object is always PIC.
556 */
557 is_pic = true;
558 }
559
560 ret = get_stream_event_context_int_field_value(err, event, "_vpid",
561 &vpid);
562 if (ret) {
563 goto end;
564 }
565
566 if (memsz == 0) {
567 /* Ignore VDSO. */
568 goto end;
569 }
570
571 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
572 debug_info->vpid_to_proc_dbg_info_src, vpid);
573 if (!proc_dbg_info_src) {
574 goto end;
575 }
576
577 key = g_new0(uint64_t, 1);
578 if (!key) {
579 goto end;
580 }
581
582 *((uint64_t *) key) = baddr;
583
584 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
585 key);
586 if (bin) {
587 goto end;
588 }
589
590 bin = bin_info_create(path, baddr, memsz, is_pic,
591 debug_info->comp->arg_debug_dir,
592 debug_info->comp->arg_target_prefix);
593 if (!bin) {
594 goto end;
595 }
596
597 g_hash_table_insert(proc_dbg_info_src->baddr_to_bin_info,
598 key, bin);
599 /* Ownership passed to ht. */
600 key = NULL;
601
602 end:
603 g_free(key);
604 return;
605 }
606
607 static inline
608 void handle_statedump_bin_info_event(FILE *err, struct debug_info *debug_info,
609 struct bt_ctf_event *event)
610 {
611 handle_bin_info_event(err, debug_info, event, true);
612 }
613
614 static inline
615 void handle_lib_load_event(FILE *err, struct debug_info *debug_info,
616 struct bt_ctf_event *event)
617 {
618 handle_bin_info_event(err, debug_info, event, false);
619 }
620
621 static inline
622 void handle_lib_unload_event(FILE *err, struct debug_info *debug_info,
623 struct bt_ctf_event *event)
624 {
625 struct proc_debug_info_sources *proc_dbg_info_src;
626 uint64_t baddr;
627 int64_t vpid;
628 gpointer key_ptr = NULL;
629 int ret;
630
631 ret = get_payload_unsigned_int_field_value(err,
632 event, "_baddr", &baddr);
633 if (ret) {
634 fprintf(err, "[error] %s in %s:%d\n", __func__,
635 __FILE__, __LINE__);
636 ret = -1;
637 goto end;
638 }
639
640 ret = get_stream_event_context_int_field_value(err, event, "_vpid",
641 &vpid);
642 if (ret) {
643 goto end;
644 }
645
646 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
647 debug_info->vpid_to_proc_dbg_info_src, vpid);
648 if (!proc_dbg_info_src) {
649 goto end;
650 }
651
652 key_ptr = (gpointer) &baddr;
653 (void) g_hash_table_remove(proc_dbg_info_src->baddr_to_bin_info,
654 key_ptr);
655 end:
656 return;
657 }
658
659 static
660 void handle_statedump_start(FILE *err, struct debug_info *debug_info,
661 struct bt_ctf_event *event)
662 {
663 struct proc_debug_info_sources *proc_dbg_info_src;
664 int64_t vpid;
665 int ret;
666
667 ret = get_stream_event_context_int_field_value(err, event,
668 "_vpid", &vpid);
669 if (ret) {
670 goto end;
671 }
672
673 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
674 debug_info->vpid_to_proc_dbg_info_src, vpid);
675 if (!proc_dbg_info_src) {
676 goto end;
677 }
678
679 g_hash_table_remove_all(proc_dbg_info_src->baddr_to_bin_info);
680 g_hash_table_remove_all(proc_dbg_info_src->ip_to_debug_info_src);
681
682 end:
683 return;
684 }
685
686 BT_HIDDEN
687 void debug_info_handle_event(FILE *err, struct bt_ctf_event *event,
688 struct debug_info *debug_info)
689 {
690 struct bt_ctf_event_class *event_class;
691 const char *event_name;
692 GQuark q_event_name;
693
694 if (!debug_info || !event) {
695 goto end;
696 }
697 event_class = bt_ctf_event_get_class(event);
698 if (!event_class) {
699 goto end;
700 }
701 event_name = bt_ctf_event_class_get_name(event_class);
702 if (!event_name) {
703 goto end_put_class;
704 }
705 q_event_name = g_quark_try_string(event_name);
706
707 if (q_event_name == debug_info->q_statedump_bin_info) {
708 /* State dump */
709 handle_statedump_bin_info_event(err, debug_info, event);
710 } else if (q_event_name == debug_info->q_dl_open ||
711 q_event_name == debug_info->q_lib_load) {
712 /*
713 * dl_open and lib_load events are both checked for since
714 * only dl_open was produced as of lttng-ust 2.8.
715 *
716 * lib_load, which is produced from lttng-ust 2.9+, is a lot
717 * more reliable since it will be emitted when other functions
718 * of the dlopen family are called (e.g. dlmopen) and when
719 * library are transitively loaded.
720 */
721 handle_lib_load_event(err, debug_info, event);
722 } else if (q_event_name == debug_info->q_statedump_start) {
723 /* Start state dump */
724 handle_statedump_start(err, debug_info, event);
725 } else if (q_event_name == debug_info->q_statedump_debug_link) {
726 /* Debug link info */
727 handle_statedump_debug_link_event(err, debug_info, event);
728 } else if (q_event_name == debug_info->q_statedump_build_id) {
729 /* Build ID info */
730 handle_statedump_build_id_event(err, debug_info, event);
731 } else if (q_event_name == debug_info-> q_lib_unload) {
732 handle_lib_unload_event(err, debug_info, event);
733 }
734
735 end_put_class:
736 bt_put(event_class);
737 end:
738 return;
739 }
This page took 0.044654 seconds and 4 git commands to generate.