fs-sink: fix handling static notifications
[babeltrace.git] / plugins / lttng-utils / debug-info.c
CommitLineData
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>
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
35struct 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
49struct debug_info {
9d325e17
PP
50 struct debug_info_component *comp;
51
4f45f9bb
JD
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
66static
67int 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
84static
85void 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
98static
99struct 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
156end:
157 return debug_info_src;
158
159error:
160 debug_info_source_destroy(debug_info_src);
161 return NULL;
162}
163
164static
165void 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
183static
184struct 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
207end:
208 return proc_dbg_info_src;
209
210error:
211 proc_debug_info_sources_destroy(proc_dbg_info_src);
212 return NULL;
213}
214
215static
216struct 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;
243end:
244 g_free(key);
245 return proc_dbg_info_src;
246}
247
248static
249struct 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
300end:
301 free(key);
302 return debug_info_src;
303}
304
305BT_HIDDEN
306struct 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
320end:
321 return dbg_info_src;
322}
323
324BT_HIDDEN
9d325e17 325struct debug_info *debug_info_create(struct debug_info_component *comp)
4f45f9bb
JD
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
9d325e17 342 debug_info->comp = comp;
4f45f9bb
JD
343 ret = debug_info_init(debug_info);
344 if (ret) {
345 goto error;
346 }
347
348end:
349 return debug_info;
350error:
351 g_free(debug_info);
352 return NULL;
353}
354
355BT_HIDDEN
356void 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);
367end:
368 return;
369}
370
371static
372void 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) {
4f45f9bb
JD
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__);
4f45f9bb
JD
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__);
4f45f9bb
JD
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
429end:
430 return;
431}
432
433static
434void 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) {
4f45f9bb
JD
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
497end:
498 return;
499}
500
501static
502void 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 ret = -1;
520 goto end;
521 }
522
523 ret = get_payload_unsigned_int_field_value(err,
524 event, "_memsz", &memsz);
525 if (ret) {
526 fprintf(err, "[error] %s in %s:%d\n", __func__,
527 __FILE__, __LINE__);
528 ret = -1;
529 goto end;
530 }
531
532 ret = get_payload_string_field_value(err,
533 event, "_path", &path);
534 if (ret) {
535 fprintf(err, "[error] %s in %s:%d\n", __func__,
536 __FILE__, __LINE__);
537 ret = -1;
538 goto end;
539 }
540
541 if (has_pic_field) {
542 uint64_t tmp;
543
544 ret = get_payload_unsigned_int_field_value(err,
545 event, "_is_pic", &tmp);
546 if (ret) {
547 fprintf(err, "[error] %s in %s:%d\n", __func__,
548 __FILE__, __LINE__);
549 ret = -1;
550 goto end;
551 }
552 is_pic = (tmp == 1);
553 } else {
554 /*
555 * dlopen has no is_pic field, because the shared
556 * object is always PIC.
557 */
558 is_pic = true;
559 }
560
561 ret = get_stream_event_context_int_field_value(err, event, "_vpid",
562 &vpid);
563 if (ret) {
4f45f9bb
JD
564 goto end;
565 }
566
567 if (!path) {
568 goto end;
569 }
570
571 if (memsz == 0) {
572 /* Ignore VDSO. */
573 goto end;
574 }
575
576 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
577 debug_info->vpid_to_proc_dbg_info_src, vpid);
578 if (!proc_dbg_info_src) {
579 goto end;
580 }
581
582 key = g_new0(uint64_t, 1);
583 if (!key) {
584 goto end;
585 }
586
587 *((uint64_t *) key) = baddr;
588
589 bin = g_hash_table_lookup(proc_dbg_info_src->baddr_to_bin_info,
590 key);
591 if (bin) {
592 goto end;
593 }
594
9d325e17
PP
595 bin = bin_info_create(path, baddr, memsz, is_pic,
596 debug_info->comp->arg_debug_dir,
597 debug_info->comp->arg_target_prefix);
4f45f9bb
JD
598 if (!bin) {
599 goto end;
600 }
601
602 g_hash_table_insert(proc_dbg_info_src->baddr_to_bin_info,
603 key, bin);
604 /* Ownership passed to ht. */
605 key = NULL;
606
607end:
608 g_free(key);
609 return;
610}
611
612static inline
613void handle_statedump_bin_info_event(FILE *err, struct debug_info *debug_info,
614 struct bt_ctf_event *event)
615{
616 handle_bin_info_event(err, debug_info, event, true);
617}
618
619static inline
620void handle_lib_load_event(FILE *err, struct debug_info *debug_info,
621 struct bt_ctf_event *event)
622{
623 handle_bin_info_event(err, debug_info, event, false);
624}
625
626static inline
627void handle_lib_unload_event(FILE *err, struct debug_info *debug_info,
628 struct bt_ctf_event *event)
629{
630 struct proc_debug_info_sources *proc_dbg_info_src;
631 uint64_t baddr;
632 int64_t vpid;
633 gpointer key_ptr = NULL;
634 int ret;
635
636 ret = get_payload_unsigned_int_field_value(err,
637 event, "_baddr", &baddr);
638 if (ret) {
639 fprintf(err, "[error] %s in %s:%d\n", __func__,
640 __FILE__, __LINE__);
641 ret = -1;
642 goto end;
643 }
644
645 ret = get_stream_event_context_int_field_value(err, event, "_vpid",
646 &vpid);
647 if (ret) {
4f45f9bb
JD
648 goto end;
649 }
650
651 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
652 debug_info->vpid_to_proc_dbg_info_src, vpid);
653 if (!proc_dbg_info_src) {
654 goto end;
655 }
656
657 key_ptr = (gpointer) &baddr;
658 (void) g_hash_table_remove(proc_dbg_info_src->baddr_to_bin_info,
659 key_ptr);
660end:
661 return;
662}
663
664static
665void handle_statedump_start(FILE *err, struct debug_info *debug_info,
666 struct bt_ctf_event *event)
667{
668 struct proc_debug_info_sources *proc_dbg_info_src;
669 int64_t vpid;
670 int ret;
671
672 ret = get_stream_event_context_int_field_value(err, event,
673 "_vpid", &vpid);
674 if (ret) {
4f45f9bb
JD
675 goto end;
676 }
677
678 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
679 debug_info->vpid_to_proc_dbg_info_src, vpid);
680 if (!proc_dbg_info_src) {
681 goto end;
682 }
683
684 g_hash_table_remove_all(proc_dbg_info_src->baddr_to_bin_info);
685 g_hash_table_remove_all(proc_dbg_info_src->ip_to_debug_info_src);
686
687end:
688 return;
689}
690
691BT_HIDDEN
692void debug_info_handle_event(FILE *err, struct bt_ctf_event *event,
693 struct debug_info *debug_info)
694{
695 struct bt_ctf_event_class *event_class;
696 const char *event_name;
697 GQuark q_event_name;
698
699 if (!debug_info || !event) {
700 goto end;
701 }
702 event_class = bt_ctf_event_get_class(event);
703 if (!event_class) {
704 goto end;
705 }
706 event_name = bt_ctf_event_class_get_name(event_class);
707 if (!event_name) {
708 goto end_put_class;
709 }
710 q_event_name = g_quark_try_string(event_name);
711
712 if (q_event_name == debug_info->q_statedump_bin_info) {
713 /* State dump */
714 handle_statedump_bin_info_event(err, debug_info, event);
715 } else if (q_event_name == debug_info->q_dl_open ||
716 q_event_name == debug_info->q_lib_load) {
717 /*
718 * dl_open and lib_load events are both checked for since
719 * only dl_open was produced as of lttng-ust 2.8.
720 *
721 * lib_load, which is produced from lttng-ust 2.9+, is a lot
722 * more reliable since it will be emitted when other functions
723 * of the dlopen family are called (e.g. dlmopen) and when
724 * library are transitively loaded.
725 */
726 handle_lib_load_event(err, debug_info, event);
727 } else if (q_event_name == debug_info->q_statedump_start) {
728 /* Start state dump */
729 handle_statedump_start(err, debug_info, event);
730 } else if (q_event_name == debug_info->q_statedump_debug_link) {
731 /* Debug link info */
732 handle_statedump_debug_link_event(err, debug_info, event);
733 } else if (q_event_name == debug_info->q_statedump_build_id) {
734 /* Build ID info */
735 handle_statedump_build_id_event(err, debug_info, event);
736 } else if (q_event_name == debug_info-> q_lib_unload) {
737 handle_lib_unload_event(err, debug_info, event);
738 }
739
740end_put_class:
741 bt_put(event_class);
742end:
743 return;
744}
This page took 0.051964 seconds and 4 git commands to generate.