Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 2014 Red Hat, Inc.
      2    This file is part of elfutils.
      3 
      4    This file is free software; you can redistribute it and/or modify
      5    it under the terms of the GNU General Public License as published by
      6    the Free Software Foundation; either version 3 of the License, or
      7    (at your option) any later version.
      8 
      9    elfutils is distributed in the hope that it will be useful, but
     10    WITHOUT ANY WARRANTY; without even the implied warranty of
     11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12    GNU General Public License for more details.
     13 
     14    You should have received a copy of the GNU General Public License
     15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     16 
     17 #ifdef HAVE_CONFIG_H
     18 # include <config.h>
     19 #endif
     20 
     21 #include <errno.h>
     22 #include <error.h>
     23 #include <fcntl.h>
     24 #include <inttypes.h>
     25 #include <libelf.h>
     26 #include ELFUTILS_HEADER(dw)
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <unistd.h>
     30 
     31 
     32 int
     33 main (int argc, char *argv[])
     34 {
     35   /* file addr+ */
     36   int fd = open (argv[1], O_RDONLY);
     37   Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
     38   if  (dbg == NULL)
     39     error (-1, 0, "dwarf_begin (%s): %s\n", argv[1], dwarf_errmsg (-1));
     40 
     41   for (int i = 2; i < argc; i++)
     42     {
     43       Dwarf_Addr addr;
     44       char *endptr;
     45       Dwarf_Die cudie;
     46       Dwarf_Line *line;
     47 
     48       errno = 0;
     49       addr = strtoull (argv[i], &endptr, 16);
     50       if (errno != 0)
     51 	error (-1, errno, "Cannot parrse '%s'", argv[1]);
     52 
     53       if (dwarf_addrdie (dbg, addr, &cudie) == NULL)
     54 	error (-1, 0, "dwarf_addrdie (%s): %s", argv[i], dwarf_errmsg (-1));
     55 
     56       line = dwarf_getsrc_die (&cudie, addr);
     57       if (line == NULL)
     58 	error (-1, 0, "dwarf_getsrc_die (%s): %s", argv[i], dwarf_errmsg (-1));
     59 
     60       const char *f = dwarf_linesrc (line, NULL, NULL);
     61       int l;
     62       if (dwarf_lineno (line, &l) != 0)
     63 	l = 0;
     64 
     65       printf ("%s:%d\n", f ?: "???", l);
     66     }
     67 
     68   dwarf_end (dbg);
     69   close (fd);
     70 
     71   return 0;
     72 }
     73