summaryrefslogtreecommitdiff
path: root/src/cmd/addr2line/main.c
blob: 9faadc27bb33041ffbc30507ade9baa57427c38e (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
77
78
79
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/*
 * addr2line simulation - only enough to make pprof work on Macs
 */

#include <u.h>
#include <libc.h>
#include <bio.h>
#include <mach.h>

void
printusage(int fd)
{
	fprint(fd, "usage: addr2line binary\n");
	fprint(fd, "reads addresses from standard input and writes two lines for each:\n");
	fprint(fd, "\tfunction name\n");
	fprint(fd, "\tfile:line\n");
}

void
usage(void)
{
	printusage(2);
	exits("usage");
}

void
main(int argc, char **argv)
{
	int fd;
	char *p;
	uvlong pc;
	Symbol s;
	Fhdr fhdr;
	Biobuf bin, bout;
	char file[1024];

	if(argc > 1 && strcmp(argv[1], "--help") == 0) {
		printusage(1);
		exits(0);
	}

	ARGBEGIN{
	default:
		usage();
	}ARGEND

	if(argc != 1)
		usage();

	fd = open(argv[0], OREAD);
	if(fd < 0)
		sysfatal("open %s: %r", argv[0]);
	if(crackhdr(fd, &fhdr) <= 0)
		sysfatal("crackhdr: %r");
	machbytype(fhdr.type);
	if(syminit(fd, &fhdr) <= 0)
		sysfatal("syminit: %r");

	Binit(&bin, 0, OREAD);
	Binit(&bout, 1, OWRITE);
	for(;;) {
		p = Brdline(&bin, '\n');
		if(p == nil)
			break;
		p[Blinelen(&bin)-1] = '\0';
		pc = strtoull(p, 0, 16);
		if(!findsym(pc, CTEXT, &s))
			s.name = "??";
		if(!fileline(file, sizeof file, pc))
			strcpy(file, "??:0");
		Bprint(&bout, "%s\n%s\n", s.name, file);
	}
	Bflush(&bout);
	exits(0);
}