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