summaryrefslogtreecommitdiffstats
path: root/src/backtrace-lunw.c
blob: eb29467161d83ff805d49d0d3fbcef14ae66744c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <libunwind.h>

#include "config.h"
#include "backtrace.h"

#define LT_MAXMSG 300

int bt_dump(struct lt_config_shared *cfg, unsigned long flags)
{
	unw_cursor_t cursor;
	unw_context_t uc;
	int ret = 0;

	unw_getcontext(&uc);
	if (unw_init_local(&cursor, &uc) < 0) {
		bt_display(cfg, "unw_init_local failed");
		return -1;
	}

	bt_display(cfg, "backtrace:");

	do {
		char text[LT_MAXMSG];
		struct bt_map *map;
		bt_word_t rel_ip = 0, ip = 0, sp;
		bt_word_t sym_offset = 0;
		bt_word_t sym_size= 0;
		char *map_name = NULL;
		char *sym_name = NULL;
		struct bt_symbol *symbol = NULL;
		int size = sizeof(bt_word_t) * 2;
		int symbol_found = 0;

		unw_get_reg(&cursor, UNW_REG_IP, &ip);
		unw_get_reg(&cursor, UNW_REG_SP, &sp);

		if (bt_find_map(cfg, &map, ip)) {
			rel_ip = ip - map->base;
			map_name = map->name;

			if (bt_find_symbol(cfg, &symbol, map, rel_ip)) {
				sym_name = symbol->name;
				sym_offset = rel_ip - symbol->start;
				sym_size = symbol->end - symbol->start;
				symbol_found = 1;
			}
		}

		if (symbol_found)
			snprintf(text, LT_MAXMSG,
				"\t<0x%0*zx> %s+0x%zx/0x%zx [%s]",
				size, rel_ip,
				sym_name,
				sym_offset,
				sym_size,
				map_name);
		else
			snprintf(text, LT_MAXMSG,
				"\t<0x%0*zx> [%s]",
				size, rel_ip,
				map_name);

		bt_display(cfg, text);

		ret = unw_step(&cursor);
		if (ret < 0) {
			unw_get_reg(&cursor, UNW_REG_IP, &ip);
			snprintf(text, LT_MAXMSG,
				"unw_step failed for ip=%zx\n",
				ip);
			bt_display(cfg, text);
		}
	} while (ret > 0);

	return ret;
}