Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 2005, 2013 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 <err.h>
     22 #include <fcntl.h>
     23 #include ELFUTILS_HEADER(dw)
     24 #include ELFUTILS_HEADER(dwelf)
     25 #include <stdio.h>
     26 #include <unistd.h>
     27 
     28 
     29 static int
     30 cb (Dwarf_Die *func, void *arg __attribute__ ((unused)))
     31 {
     32   const char *file = dwarf_decl_file (func);
     33   int line = -1;
     34   dwarf_decl_line (func, &line);
     35   const char *fct = dwarf_diename (func);
     36 
     37   printf ("%s:%d:%s\n", file, line, fct);
     38 
     39   return DWARF_CB_ABORT;
     40 }
     41 
     42 static Dwarf *
     43 setup_alt (Dwarf *main)
     44 {
     45   const char *alt_name;
     46   const void *build_id;
     47   ssize_t ret = dwelf_dwarf_gnu_debugaltlink (main, &alt_name, &build_id);
     48   if (ret == 0)
     49     return NULL;
     50   if (ret == -1)
     51     errx (1, "dwelf_dwarf_gnu_debugaltlink: %s", dwarf_errmsg (-1));
     52   int fd = open (alt_name, O_RDONLY);
     53   if (fd < 0)
     54     err (1, "open (%s)", alt_name);
     55   Dwarf *dbg_alt = dwarf_begin (fd, DWARF_C_READ);
     56   if (dbg_alt == NULL)
     57     errx (1, "dwarf_begin (%s): %s", alt_name, dwarf_errmsg (-1));
     58   if (elf_cntl (dwarf_getelf (dbg_alt), ELF_C_FDREAD) != 0)
     59     errx (1, "elf_cntl (%s, ELF_C_FDREAD): %s", alt_name, elf_errmsg (-1));
     60   close (fd);
     61   dwarf_setalt (main, dbg_alt);
     62   return dbg_alt;
     63 }
     64 
     65 int
     66 main (int argc, char *argv[])
     67 {
     68   for (int i = 1; i < argc; ++i)
     69     {
     70       int fd = open (argv[i], O_RDONLY);
     71       if (fd < 0)
     72 	err (1, "open (%s)", argv[i]);
     73 
     74       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
     75       if (dbg != NULL)
     76 	{
     77 	  Dwarf_Off off = 0;
     78 	  size_t cuhl;
     79 	  Dwarf_Off noff;
     80 	  Dwarf *dbg_alt = setup_alt (dbg);
     81 
     82 	  while (dwarf_nextcu (dbg, off, &noff, &cuhl, NULL, NULL, NULL) == 0)
     83 	    {
     84 	      Dwarf_Die die_mem;
     85 	      Dwarf_Die *die = dwarf_offdie (dbg, off + cuhl, &die_mem);
     86 
     87 	      /* Explicitly stop in the callback and then resume each time.  */
     88 	      ptrdiff_t doff = 0;
     89 	      do
     90 		{
     91 		  doff = dwarf_getfuncs (die, cb, NULL, doff);
     92 		  if (dwarf_errno () != 0)
     93 		    errx (1, "dwarf_getfuncs (%s): %s",
     94 			  argv[i], dwarf_errmsg (-1));
     95 		}
     96 	      while (doff != 0);
     97 
     98 	      off = noff;
     99 	    }
    100 
    101 	  dwarf_end (dbg_alt);
    102 	  dwarf_end (dbg);
    103 	}
    104       else
    105 	errx (1, "dwarf_begin (%s): %s", argv[i], dwarf_errmsg (-1));
    106 
    107       close (fd);
    108     }
    109 }
    110