aboutsummaryrefslogtreecommitdiffstats
path: root/print_backtrace.c
blob: d8a53e81fbb2c418c6762e1ce38e4268a6f85beb (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "internal.h"

#include <elfutils/libdwfl.h>


void
liberror_print_backtrace(struct liberror_error *error, FILE *fp, const char *indent)
{
	struct liberror_backtrace *backtrace;
	int saved_errno = errno, full_paths, lineno;
	char *debuginfo_path = NULL, *env;
	Dwarf_Addr ip;
	Dwfl_Callbacks callbacks;
	Dwfl *dwfl = NULL;
	Dwfl_Line *line = NULL;
	Dwfl_Module *module = NULL;
	const char *filename = NULL, *p;
	const char *funcname = NULL;
	size_t i;

	if (!error)
		return;

	backtrace = error->backtrace;
	if (!backtrace)
		return;

	memset(&callbacks, 0, sizeof(callbacks));

	callbacks.find_elf       = dwfl_linux_proc_find_elf;
	callbacks.find_debuginfo = dwfl_standard_find_debuginfo;
	callbacks.debuginfo_path = &debuginfo_path;

	env = getenv("LIBERROR_FULL_PATHS");
	full_paths = env && (*env == 'Y' || *env == 'y' || *env == '1');

	dwfl = dwfl_begin(&callbacks);

	if (dwfl) {
		if (dwfl_linux_proc_report(dwfl, getpid()) ||
		    dwfl_report_end(dwfl, NULL, NULL)) {
			dwfl_end(dwfl);
			dwfl = NULL;
		}
	}

	for (i = 0; i < backtrace->n; i++) {
		ip = (Dwarf_Addr)backtrace->rips[i];

		if (dwfl) {
			module   = dwfl_addrmodule(dwfl, ip);
			funcname = module ? dwfl_module_addrname(module, ip) : NULL;
			line     = dwfl_getsrc(dwfl, ip);
			if (line) {
				filename = dwfl_lineinfo(line, &(Dwarf_Addr){0}, &lineno, NULL, NULL, NULL);
				if (!full_paths && (p = strrchr(filename, '/')))
					filename = &p[1];
			}
		}

		fprintf(fp, "%s%s 0x%016"PRIxPTR": %s",
		        indent, !i ? "at" : "by",
			(uintptr_t)ip,
		        funcname ? funcname : "???");
		if (line)
			fprintf(fp, " (%s:%i)\n", filename, lineno);
		else
			fprintf(fp, "\n");
	}

	if (dwfl)
		dwfl_end(dwfl);
	errno = saved_errno;
}