a2b94e2f34b6472d1449f3a032c910046aae4865
[babeltrace.git] / lib / debuginfo.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 <babeltrace/types.h>
31 #include <babeltrace/ctf-ir/metadata.h>
32 #include <babeltrace/debuginfo.h>
33 #include <babeltrace/so-info.h>
34 #include <babeltrace/babeltrace-internal.h>
35 #include <babeltrace/utils.h>
36
37 struct proc_debug_info_sources {
38 /*
39 * Hash table: base address (pointer to uint64_t) to so info; owned by
40 * proc_debug_info_sources.
41 */
42 GHashTable *baddr_to_so_info;
43
44 /*
45 * Hash table: IP (pointer to uint64_t) to (struct debug_info_source *);
46 * owned by proc_debug_info_sources.
47 */
48 GHashTable *ip_to_debug_info_src;
49 };
50
51 struct debug_info {
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_soinfo;
58 GQuark q_statedump_debug_link;
59 GQuark q_statedump_build_id;
60 GQuark q_statedump_start;
61 GQuark q_dl_open;
62 };
63
64 static
65 int debug_info_init(struct debug_info *info)
66 {
67 info->q_statedump_soinfo = g_quark_from_string(
68 "lttng_ust_statedump:soinfo");
69 info->q_statedump_debug_link = g_quark_from_string(
70 "lttng_ust_statedump:debug_link)");
71 info->q_statedump_build_id = g_quark_from_string(
72 "lttng_ust_statedump:build_id");
73 info->q_statedump_start = g_quark_from_string(
74 "lttng_ust_statedump:start");
75 info->q_dl_open = g_quark_from_string("lttng_ust_dl:dlopen");
76
77 return so_info_init();
78 }
79
80 static
81 void debug_info_source_destroy(struct debug_info_source *debug_info_src)
82 {
83 if (!debug_info_src) {
84 return;
85 }
86
87 free(debug_info_src->func);
88 free(debug_info_src->src_path);
89 free(debug_info_src->bin_path);
90 g_free(debug_info_src);
91 }
92
93 static
94 struct debug_info_source *debug_info_source_create_from_so(struct so_info *so,
95 uint64_t ip)
96 {
97 int ret;
98 struct debug_info_source *debug_info_src = NULL;
99 struct source_location *src_loc = NULL;
100
101 debug_info_src = g_new0(struct debug_info_source, 1);
102
103 if (!debug_info_src) {
104 goto end;
105 }
106
107 /* Lookup function name */
108 ret = so_info_lookup_function_name(so, ip, &debug_info_src->func);
109 if (ret) {
110 goto error;
111 }
112
113 /* Can't retrieve src_loc from ELF only, skip it */
114 if (!so->is_elf_only) {
115 /* Lookup source location */
116 ret = so_info_lookup_source_location(so, ip, &src_loc);
117 if (ret) {
118 goto error;
119 }
120 }
121
122 if (src_loc) {
123 debug_info_src->line_no = src_loc->line_no;
124
125 if (src_loc->filename) {
126 debug_info_src->src_path = strdup(src_loc->filename);
127 if (!debug_info_src->src_path) {
128 goto error;
129 }
130
131 debug_info_src->short_src_path = get_filename_from_path(
132 src_loc->filename);
133
134 }
135
136 source_location_destroy(src_loc);
137 }
138
139 if (so->elf_path) {
140 debug_info_src->bin_path = strdup(so->elf_path);
141 if (!debug_info_src->bin_path) {
142 goto error;
143 }
144
145 debug_info_src->short_bin_path = get_filename_from_path(
146 debug_info_src->bin_path);
147 }
148
149 end:
150 return debug_info_src;
151
152 error:
153 debug_info_source_destroy(debug_info_src);
154 return NULL;
155 }
156
157 static
158 void proc_debug_info_sources_destroy(
159 struct proc_debug_info_sources *proc_dbg_info_src)
160 {
161 if (!proc_dbg_info_src) {
162 return;
163 }
164
165 if (proc_dbg_info_src->baddr_to_so_info) {
166 g_hash_table_destroy(proc_dbg_info_src->baddr_to_so_info);
167 }
168
169 if (proc_dbg_info_src->ip_to_debug_info_src) {
170 g_hash_table_destroy(proc_dbg_info_src->ip_to_debug_info_src);
171 }
172
173 g_free(proc_dbg_info_src);
174 }
175
176 static
177 struct proc_debug_info_sources *proc_debug_info_sources_create(void)
178 {
179 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
180
181 proc_dbg_info_src = g_new0(struct proc_debug_info_sources, 1);
182 if (!proc_dbg_info_src) {
183 goto end;
184 }
185
186 proc_dbg_info_src->baddr_to_so_info = g_hash_table_new_full(
187 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
188 (GDestroyNotify) so_info_destroy);
189 if (!proc_dbg_info_src->baddr_to_so_info) {
190 goto error;
191 }
192
193 proc_dbg_info_src->ip_to_debug_info_src = g_hash_table_new_full(
194 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
195 (GDestroyNotify) debug_info_source_destroy);
196 if (!proc_dbg_info_src->ip_to_debug_info_src) {
197 goto error;
198 }
199
200 end:
201 return proc_dbg_info_src;
202
203 error:
204 proc_debug_info_sources_destroy(proc_dbg_info_src);
205 return NULL;
206 }
207
208 static
209 struct proc_debug_info_sources *proc_debug_info_sources_ht_get_entry(
210 GHashTable *ht, int64_t vpid)
211 {
212 gpointer key = g_new0(int64_t, 1);
213 struct proc_debug_info_sources *proc_dbg_info_src = NULL;
214
215 if (!key) {
216 goto end;
217 }
218
219 *((int64_t *) key) = vpid;
220
221 /* Exists? Return it */
222 proc_dbg_info_src = g_hash_table_lookup(ht, key);
223 if (proc_dbg_info_src) {
224 goto end;
225 }
226
227 /* Otherwise, create and return it */
228 proc_dbg_info_src = proc_debug_info_sources_create();
229 if (!proc_dbg_info_src) {
230 goto end;
231 }
232
233 g_hash_table_insert(ht, key, proc_dbg_info_src);
234 /* Ownership passed to ht */
235 key = NULL;
236 end:
237 g_free(key);
238 return proc_dbg_info_src;
239 }
240
241 static
242 struct debug_info_source *proc_debug_info_sources_get_entry(
243 struct proc_debug_info_sources *proc_dbg_info_src, uint64_t ip)
244 {
245 struct debug_info_source *debug_info_src = NULL;
246 gpointer key = g_new0(uint64_t, 1);
247 GHashTableIter iter;
248 gpointer baddr, value;
249
250 if (!key) {
251 goto end;
252 }
253
254 *((uint64_t *) key) = ip;
255
256 /* Look in IP to debug infos hash table first. */
257 debug_info_src = g_hash_table_lookup(
258 proc_dbg_info_src->ip_to_debug_info_src,
259 key);
260 if (debug_info_src) {
261 goto end;
262 }
263
264 /* Check in all so_infos. */
265 g_hash_table_iter_init(&iter, proc_dbg_info_src->baddr_to_so_info);
266
267 while (g_hash_table_iter_next(&iter, &baddr, &value))
268 {
269 struct so_info *so = value;
270
271 if (!so_info_has_address(value, ip)) {
272 continue;
273 }
274
275 /*
276 * Found; add it to cache.
277 *
278 * FIXME: this should be bounded in size (and implement
279 * a caching policy), and entries should be prunned when
280 * libraries are unmapped.
281 */
282 debug_info_src = debug_info_source_create_from_so(so, ip);
283 if (debug_info_src) {
284 g_hash_table_insert(
285 proc_dbg_info_src->ip_to_debug_info_src,
286 key, debug_info_src);
287 /* Ownership passed to ht. */
288 key = NULL;
289 }
290 break;
291 }
292
293 end:
294 free(key);
295 return debug_info_src;
296 }
297
298 BT_HIDDEN
299 struct debug_info_source *debug_info_query(struct debug_info *debug_info,
300 int64_t vpid, uint64_t ip)
301 {
302 struct debug_info_source *dbg_info_src = NULL;
303 struct proc_debug_info_sources *proc_dbg_info_src;
304
305 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
306 debug_info->vpid_to_proc_dbg_info_src, vpid);
307 if (!proc_dbg_info_src) {
308 goto end;
309 }
310
311 dbg_info_src = proc_debug_info_sources_get_entry(
312 proc_dbg_info_src, ip);
313 if (!dbg_info_src) {
314 goto end;
315 }
316
317 end:
318 return dbg_info_src;
319 }
320
321 BT_HIDDEN
322 struct debug_info *debug_info_create(void)
323 {
324 int ret;
325 struct debug_info *debug_info;
326
327 debug_info = g_new0(struct debug_info, 1);
328 if (!debug_info) {
329 goto end;
330 }
331
332 debug_info->vpid_to_proc_dbg_info_src = g_hash_table_new_full(
333 g_int64_hash, g_int64_equal, (GDestroyNotify) g_free,
334 (GDestroyNotify) proc_debug_info_sources_destroy);
335 if (!debug_info->vpid_to_proc_dbg_info_src) {
336 goto error;
337 }
338
339 ret = debug_info_init(debug_info);
340 if (ret) {
341 goto error;
342 }
343
344 end:
345 return debug_info;
346 error:
347 g_free(debug_info);
348 return NULL;
349 }
350
351 BT_HIDDEN
352 void debug_info_destroy(struct debug_info *debug_info)
353 {
354 if (!debug_info) {
355 goto end;
356 }
357
358 if (debug_info->vpid_to_proc_dbg_info_src) {
359 g_hash_table_destroy(debug_info->vpid_to_proc_dbg_info_src);
360 }
361
362 g_free(debug_info);
363 end:
364 return;
365 }
366
367 static
368 void handle_statedump_build_id_event(struct debug_info *debug_info,
369 struct ctf_event_definition *event_def)
370 {
371 struct proc_debug_info_sources *proc_dbg_info_src;
372 struct bt_definition *event_fields_def = NULL;
373 struct bt_definition *sec_def = NULL;
374 struct bt_definition *baddr_def = NULL;
375 struct bt_definition *vpid_def = NULL;
376 struct bt_definition *build_id_def = NULL;
377 struct definition_sequence *build_id_seq;
378 struct so_info *so = NULL;
379 int i;
380 int64_t vpid;
381 uint64_t baddr;
382 uint8_t *build_id = NULL;
383 uint64_t build_id_len;
384
385 event_fields_def = (struct bt_definition *) event_def->event_fields;
386 sec_def = (struct bt_definition *)
387 event_def->stream->stream_event_context;
388
389 if (!event_fields_def || !sec_def) {
390 goto end;
391 }
392
393 baddr_def = bt_lookup_definition(event_fields_def, "_baddr");
394 if (!baddr_def) {
395 goto end;
396 }
397
398 vpid_def = bt_lookup_definition(sec_def, "_vpid");
399 if (!vpid_def) {
400 goto end;
401 }
402
403 build_id_def = bt_lookup_definition(event_fields_def, "_build_id");
404 if (!build_id_def) {
405 goto end;
406 }
407
408 if (baddr_def->declaration->id != CTF_TYPE_INTEGER) {
409 goto end;
410 }
411
412 if (vpid_def->declaration->id != CTF_TYPE_INTEGER) {
413 goto end;
414 }
415
416 if (build_id_def->declaration->id != CTF_TYPE_SEQUENCE) {
417 goto end;
418 }
419
420 baddr = bt_get_unsigned_int(baddr_def);
421 vpid = bt_get_signed_int(vpid_def);
422 build_id_seq = container_of(build_id_def,
423 struct definition_sequence, p);
424 build_id_len = build_id_seq->length->value._unsigned;
425
426 build_id = g_malloc(build_id_len);
427 if (!build_id) {
428 goto end;
429 }
430
431 for (i = 0; i < build_id_len; ++i) {
432 struct bt_definition **field;
433
434 field = (struct bt_definition **) &g_ptr_array_index(
435 build_id_seq->elems, i);
436 build_id[i] = bt_get_unsigned_int(*field);
437 }
438
439 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
440 debug_info->vpid_to_proc_dbg_info_src, vpid);
441 if (!proc_dbg_info_src) {
442 goto end;
443 }
444
445 so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info,
446 (gpointer) &baddr);
447 if (!so) {
448 /*
449 * The build_id event comes after the so has been
450 * created. If it isn't found, just ignore this event.
451 */
452 goto end;
453 }
454
455 so_info_set_build_id(so, build_id, build_id_len);
456
457 end:
458 free(build_id);
459 return;
460 }
461
462 static
463 void handle_statedump_debug_link_event(struct debug_info *debug_info,
464 struct ctf_event_definition *event_def)
465 {
466 struct proc_debug_info_sources *proc_dbg_info_src;
467 struct bt_definition *event_fields_def = NULL;
468 struct bt_definition *sec_def = NULL;
469 struct bt_definition *baddr_def = NULL;
470 struct bt_definition *vpid_def = NULL;
471 struct bt_definition *filename_def = NULL;
472 struct bt_definition *crc32_def = NULL;
473 struct so_info *so = NULL;
474 int64_t vpid;
475 uint64_t baddr;
476 char *filename = NULL;
477 uint32_t crc32;
478
479 event_fields_def = (struct bt_definition *) event_def->event_fields;
480 sec_def = (struct bt_definition *)
481 event_def->stream->stream_event_context;
482
483 if (!event_fields_def || !sec_def) {
484 goto end;
485 }
486
487 baddr_def = bt_lookup_definition(event_fields_def, "_baddr");
488 if (!baddr_def) {
489 goto end;
490 }
491
492 vpid_def = bt_lookup_definition(sec_def, "_vpid");
493 if (!vpid_def) {
494 goto end;
495 }
496
497 filename_def = bt_lookup_definition(event_fields_def, "_filename");
498 if (!filename_def) {
499 goto end;
500 }
501
502 crc32_def = bt_lookup_definition(event_fields_def, "_crc32");
503 if (!crc32_def) {
504 goto end;
505 }
506
507 if (baddr_def->declaration->id != CTF_TYPE_INTEGER) {
508 goto end;
509 }
510
511 if (vpid_def->declaration->id != CTF_TYPE_INTEGER) {
512 goto end;
513 }
514
515 if (filename_def->declaration->id != CTF_TYPE_STRING) {
516 goto end;
517 }
518
519 if (crc32_def->declaration->id != CTF_TYPE_INTEGER) {
520 goto end;
521 }
522
523 baddr = bt_get_unsigned_int(baddr_def);
524 vpid = bt_get_signed_int(vpid_def);
525
526 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
527 debug_info->vpid_to_proc_dbg_info_src, vpid);
528 if (!proc_dbg_info_src) {
529 goto end;
530 }
531
532 so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info,
533 (gpointer) &baddr);
534 if (!so) {
535 /*
536 * The debug_link event comes after the so has been
537 * created. If it isn't found, just ignore this event.
538 */
539 goto end;
540 }
541
542 filename = bt_get_string(filename_def);
543 crc32 = bt_get_unsigned_int(crc32_def);
544
545 so_info_set_debug_link(so, filename, crc32);
546
547 end:
548 return;
549 }
550
551 static
552 void handle_statedump_soinfo_event(struct debug_info *debug_info,
553 struct ctf_event_definition *event_def)
554 {
555 struct bt_definition *baddr_def = NULL;
556 struct bt_definition *memsz_def = NULL;
557 struct bt_definition *sopath_def = NULL;
558 struct bt_definition *vpid_def = NULL;
559 struct bt_definition *event_fields_def = NULL;
560 struct bt_definition *sec_def = NULL;
561 struct proc_debug_info_sources *proc_dbg_info_src;
562 struct so_info *so;
563 uint64_t baddr, memsz;
564 int64_t vpid;
565 const char *sopath;
566 gpointer key = NULL;
567
568 event_fields_def = (struct bt_definition *) event_def->event_fields;
569 sec_def = (struct bt_definition *)
570 event_def->stream->stream_event_context;
571
572 if (!event_fields_def || !sec_def) {
573 goto end;
574 }
575
576 baddr_def = bt_lookup_definition(event_fields_def, "_baddr");
577 if (!baddr_def) {
578 goto end;
579 }
580
581 memsz_def = bt_lookup_definition(event_fields_def, "_memsz");
582 if (!memsz_def) {
583 goto end;
584 }
585
586 sopath_def = bt_lookup_definition(event_fields_def, "_sopath");
587 if (!sopath_def) {
588 goto end;
589 }
590
591 vpid_def = bt_lookup_definition(sec_def, "_vpid");
592 if (!vpid_def) {
593 goto end;
594 }
595
596 if (baddr_def->declaration->id != CTF_TYPE_INTEGER) {
597 goto end;
598 }
599
600 if (memsz_def->declaration->id != CTF_TYPE_INTEGER) {
601 goto end;
602 }
603
604 if (sopath_def->declaration->id != CTF_TYPE_STRING) {
605 goto end;
606 }
607
608 if (vpid_def->declaration->id != CTF_TYPE_INTEGER) {
609 goto end;
610 }
611
612 baddr = bt_get_unsigned_int(baddr_def);
613 memsz = bt_get_unsigned_int(memsz_def);
614 sopath = bt_get_string(sopath_def);
615 vpid = bt_get_signed_int(vpid_def);
616
617 if (!sopath) {
618 goto end;
619 }
620
621 if (memsz == 0) {
622 /* Ignore VDSO. */
623 goto end;
624 }
625
626 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
627 debug_info->vpid_to_proc_dbg_info_src, vpid);
628 if (!proc_dbg_info_src) {
629 goto end;
630 }
631
632 key = g_new0(uint64_t, 1);
633 if (!key) {
634 goto end;
635 }
636
637 *((uint64_t *) key) = baddr;
638
639 so = g_hash_table_lookup(proc_dbg_info_src->baddr_to_so_info,
640 key);
641 if (so) {
642 goto end;
643 }
644
645 so = so_info_create(sopath, baddr, memsz);
646 if (!so) {
647 goto end;
648 }
649
650 g_hash_table_insert(proc_dbg_info_src->baddr_to_so_info,
651 key, so);
652 /* Ownership passed to ht. */
653 key = NULL;
654
655 end:
656 g_free(key);
657 return;
658 }
659
660 static
661 void handle_statedump_start(struct debug_info *debug_info,
662 struct ctf_event_definition *event_def)
663 {
664 struct bt_definition *vpid_def = NULL;
665 struct bt_definition *sec_def = NULL;
666 struct proc_debug_info_sources *proc_dbg_info_src;
667 int64_t vpid;
668
669 sec_def = (struct bt_definition *)
670 event_def->stream->stream_event_context;
671 if (!sec_def) {
672 goto end;
673 }
674
675 vpid_def = bt_lookup_definition(sec_def, "_vpid");
676 if (!vpid_def) {
677 goto end;
678 }
679
680 vpid = bt_get_signed_int(vpid_def);
681
682 proc_dbg_info_src = proc_debug_info_sources_ht_get_entry(
683 debug_info->vpid_to_proc_dbg_info_src, vpid);
684 if (!proc_dbg_info_src) {
685 goto end;
686 }
687
688 g_hash_table_remove_all(proc_dbg_info_src->baddr_to_so_info);
689 g_hash_table_remove_all(proc_dbg_info_src->ip_to_debug_info_src);
690
691 end:
692 return;
693 }
694
695 static
696 void register_event_debug_infos(struct debug_info *debug_info,
697 struct ctf_event_definition *event)
698 {
699 struct bt_definition *ip_def, *vpid_def;
700 int64_t vpid;
701 uint64_t ip;
702 struct bt_definition *sec_def;
703
704 /* Get stream event context definition. */
705 sec_def = (struct bt_definition *) event->stream->stream_event_context;
706 if (!sec_def) {
707 goto end;
708 }
709
710 /* Get "ip" and "vpid" definitions. */
711 vpid_def = bt_lookup_definition((struct bt_definition *) sec_def,
712 "_vpid");
713 ip_def = bt_lookup_definition((struct bt_definition *) sec_def, "_ip");
714
715 if (!vpid_def || !ip_def) {
716 goto end;
717 }
718
719 vpid = bt_get_signed_int(vpid_def);
720 ip = bt_get_unsigned_int(ip_def);
721
722 /* Get debug info for this context. */
723 ((struct definition_integer *) ip_def)->debug_info_src =
724 debug_info_query(debug_info, vpid, ip);
725
726 end:
727 return;
728 }
729
730 BT_HIDDEN
731 void debug_info_handle_event(struct debug_info *debug_info,
732 struct ctf_event_definition *event)
733 {
734 struct ctf_event_declaration *event_class;
735 struct ctf_stream_declaration *stream_class;
736
737 if (!debug_info || !event) {
738 goto end;
739 }
740
741 stream_class = event->stream->stream_class;
742 event_class = g_ptr_array_index(stream_class->events_by_id,
743 event->stream->event_id);
744
745 if (event_class->name == debug_info->q_statedump_soinfo ||
746 event_class->name == debug_info->q_dl_open) {
747 /* State dump/dlopen() */
748 handle_statedump_soinfo_event(debug_info, event);
749 } else if (event_class->name == debug_info->q_statedump_start) {
750 /* Start state dump */
751 handle_statedump_start(debug_info, event);
752 } else if (event_class->name == debug_info->q_statedump_debug_link) {
753 /* Debug link info */
754 handle_statedump_debug_link_event(debug_info, event);
755 } else if (event_class->name == debug_info->q_statedump_build_id) {
756 /* Build ID info */
757 handle_statedump_build_id_event(debug_info, event);
758 } else {
759 /* Other events: register debug infos */
760 register_event_debug_infos(debug_info, event);
761 }
762
763 end:
764 return;
765 }
This page took 0.042083 seconds and 3 git commands to generate.