summaryrefslogtreecommitdiff
path: root/src/cmd/addr2line/main.c
blob: 6b2fe5dfe19dece5a43d16e51e1bbb9a819e382e (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
// 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
usage(void)
{
	fprint(2, "usage: addr2line binary\n");
	fprint(2, "reads addresses from standard input and writes two lines for each:\n");
	fprint(2, "\tfunction name\n");
	fprint(2, "\tfile:line\n");
	exits("usage");
}

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

	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);
}