github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/cmd/addr2line/main.c (about) 1 // Copyright 2012 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 /* 6 * addr2line simulation - only enough to make pprof work on Macs 7 */ 8 9 #include <u.h> 10 #include <libc.h> 11 #include <bio.h> 12 #include <mach.h> 13 14 void 15 printusage(int fd) 16 { 17 fprint(fd, "usage: addr2line binary\n"); 18 fprint(fd, "reads addresses from standard input and writes two lines for each:\n"); 19 fprint(fd, "\tfunction name\n"); 20 fprint(fd, "\tfile:line\n"); 21 } 22 23 void 24 usage(void) 25 { 26 printusage(2); 27 exits("usage"); 28 } 29 30 void 31 main(int argc, char **argv) 32 { 33 int fd; 34 char *p, *q; 35 uvlong pc; 36 Symbol s; 37 Fhdr fhdr; 38 Biobuf bin, bout; 39 char file[1024]; 40 41 if(argc > 1 && strcmp(argv[1], "--help") == 0) { 42 printusage(1); 43 exits(0); 44 } 45 46 ARGBEGIN{ 47 default: 48 usage(); 49 }ARGEND 50 51 if(argc != 1) 52 usage(); 53 54 fd = open(argv[0], OREAD); 55 if(fd < 0) 56 sysfatal("open %s: %r", argv[0]); 57 if(crackhdr(fd, &fhdr) <= 0) 58 sysfatal("crackhdr: %r"); 59 machbytype(fhdr.type); 60 if(syminit(fd, &fhdr) <= 0) 61 sysfatal("syminit: %r"); 62 63 Binit(&bin, 0, OREAD); 64 Binit(&bout, 1, OWRITE); 65 for(;;) { 66 p = Brdline(&bin, '\n'); 67 if(p == nil) 68 break; 69 p[Blinelen(&bin)-1] = '\0'; 70 q = strchr(p, ':'); 71 if(q != nil) { 72 // reverse: translate file:line to pc 73 *q++ = '\0'; 74 pc = file2pc(p, atoi(q)); 75 if(pc == ~(uvlong)0) 76 Bprint(&bout, "!%r\n"); 77 else 78 Bprint(&bout, "0x%llux\n", pc); 79 continue; 80 } 81 pc = strtoull(p, 0, 16); 82 if(!findsym(pc, CTEXT, &s)) 83 s.name = "??"; 84 if(!fileline(file, sizeof file, pc)) 85 strcpy(file, "??:0"); 86 Bprint(&bout, "%s\n%s\n", s.name, file); 87 } 88 Bflush(&bout); 89 exits(0); 90 }