github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/objdump/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   * objdump 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  usage(void)
    16  {
    17  	fprint(2, "usage: objdump binary start stop\n");
    18  	fprint(2, "Disassembles binary from PC start up to stop.\n");
    19  	exits("usage");
    20  }
    21  
    22  void
    23  main(int argc, char **argv)
    24  {
    25  	int fd, n;
    26  	uvlong pc, start, stop;
    27  	Fhdr fhdr;
    28  	Biobuf bout;
    29  	char buf[1024];
    30  	Map *text;
    31  
    32  	ARGBEGIN{
    33  	default:
    34  		usage();
    35  	}ARGEND
    36  
    37  	if(argc != 3)
    38  		usage();
    39  	start = strtoull(argv[1], 0, 16);
    40  	stop = strtoull(argv[2], 0, 16);
    41  
    42  	fd = open(argv[0], OREAD);
    43  	if(fd < 0)
    44  		sysfatal("open %s: %r", argv[0]);
    45  	if(crackhdr(fd, &fhdr) <= 0)
    46  		sysfatal("crackhdr: %r");
    47  	machbytype(fhdr.type);
    48  	if(syminit(fd, &fhdr) <= 0)
    49  		sysfatal("syminit: %r");
    50  	text = loadmap(nil, fd, &fhdr);
    51  	if(text == nil)
    52  		sysfatal("loadmap: %r");
    53  
    54  	Binit(&bout, 1, OWRITE);
    55  	for(pc=start; pc<stop; ) {
    56  		if(fileline(buf, sizeof buf, pc))
    57  			Bprint(&bout, "%s\n", buf);
    58  		buf[0] = '\0';
    59  		machdata->das(text, pc, 0, buf, sizeof buf);
    60  		Bprint(&bout, " %llx: %s\n", pc, buf);
    61  		n = machdata->instsize(text, pc);
    62  		if(n <= 0)
    63  			break;
    64  		pc += n;
    65  	}
    66  	Bflush(&bout);
    67  	exits(0);
    68  }