Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 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 <inttypes.h>
     22 #include <assert.h>
     23 #include ELFUTILS_HEADER(dw)
     24 #include ELFUTILS_HEADER(dwfl)
     25 #include <dwarf.h>
     26 #include <argp.h>
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <string.h>
     30 #include "system.h"
     31 
     32 int
     33 main (int argc, char *argv[])
     34 {
     35   int cnt;
     36 
     37   Dwfl *dwfl = NULL;
     38   (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &cnt, &dwfl);
     39   assert (dwfl != NULL);
     40 
     41   Dwarf_Die *cu = NULL;
     42   Dwarf_Addr bias;
     43   do
     44     {
     45       cu = dwfl_nextcu (dwfl, cu, &bias);
     46       if (cu != NULL)
     47 	{
     48 	  Dwfl_Module *mod = dwfl_cumodule (cu);
     49 	  const char *modname = (dwfl_module_info (mod, NULL, NULL, NULL,
     50 						   NULL, NULL, NULL, NULL)
     51 				 ?: "<unknown>");
     52 	  const char *cuname = (dwarf_diename (cu) ?: "<unknown>");
     53 
     54 	  printf ("mod: %s CU: [%" PRIx64 "] %s\n", modname,
     55 		  dwarf_dieoffset (cu), cuname);
     56 
     57 	  size_t lines;
     58 	  if (dwfl_getsrclines (cu, &lines) != 0)
     59 	    continue; // No lines...
     60 
     61 	  for (size_t i = 0; i < lines; i++)
     62 	    {
     63 	      Dwfl_Line *line = dwfl_onesrcline (cu, i);
     64 
     65 	      Dwarf_Addr addr;
     66 	      int lineno;
     67 	      int colno;
     68 	      Dwarf_Word mtime;
     69 	      Dwarf_Word length;
     70 	      const char *src = dwfl_lineinfo (line, &addr, &lineno, &colno,
     71 					       &mtime, &length);
     72 
     73 	      Dwarf_Addr dw_bias;
     74 	      Dwarf_Line *dw_line = dwfl_dwarf_line (line, &dw_bias);
     75 	      assert (bias == dw_bias);
     76 
     77 	      Dwarf_Addr dw_addr;
     78 	      if (dwarf_lineaddr (dw_line, &dw_addr) != 0)
     79 		error (EXIT_FAILURE, 0, "dwarf_lineaddr: %s",
     80 		       dwarf_errmsg (-1));
     81 	      assert (addr == dw_addr + dw_bias);
     82 
     83 	      unsigned int dw_op_index;
     84 	      if (dwarf_lineop_index (dw_line, &dw_op_index) != 0)
     85 		error (EXIT_FAILURE, 0, "dwarf_lineop_index: %s",
     86 		       dwarf_errmsg (-1));
     87 
     88 	      int dw_lineno;
     89 	      if (dwarf_lineno (dw_line, &dw_lineno) != 0)
     90 		error (EXIT_FAILURE, 0, "dwarf_lineno: %s",
     91 		       dwarf_errmsg (-1));
     92 	      assert (lineno == dw_lineno);
     93 
     94 	      int dw_colno;
     95 	      if (dwarf_linecol (dw_line, &dw_colno) != 0)
     96 		error (EXIT_FAILURE, 0, "dwarf_lineno: %s",
     97 		       dwarf_errmsg (-1));
     98 	      assert (colno == dw_colno);
     99 
    100 	      bool begin;
    101 	      if (dwarf_linebeginstatement (dw_line, &begin) != 0)
    102 		error (EXIT_FAILURE, 0, "dwarf_linebeginstatement: %s",
    103 		       dwarf_errmsg (-1));
    104 
    105 	      bool end;
    106 	      if (dwarf_lineendsequence (dw_line, &end) != 0)
    107 		error (EXIT_FAILURE, 0, "dwarf_lineendsequence: %s",
    108 		       dwarf_errmsg (-1));
    109 
    110 	      bool pend;
    111 	      if (dwarf_lineprologueend (dw_line, &pend) != 0)
    112 		error (EXIT_FAILURE, 0, "dwarf_lineprologueend: %s",
    113 		       dwarf_errmsg (-1));
    114 
    115 	      bool ebegin;
    116 	      if (dwarf_lineepiloguebegin (dw_line, &ebegin) != 0)
    117 		error (EXIT_FAILURE, 0, "dwarf_lineepiloguebegin: %s",
    118 		       dwarf_errmsg (-1));
    119 
    120 	      bool block;
    121 	      if (dwarf_lineblock (dw_line, &block) != 0)
    122 		error (EXIT_FAILURE, 0, "dwarf_lineblock: %s",
    123 		       dwarf_errmsg (-1));
    124 
    125 	      unsigned int isa;
    126 	      if (dwarf_lineisa (dw_line, &isa) != 0)
    127 		error (EXIT_FAILURE, 0, "dwarf_lineisa: %s",
    128 		       dwarf_errmsg (-1));
    129 
    130 	      unsigned int disc;
    131 	      if (dwarf_linediscriminator (dw_line, &disc) != 0)
    132 		error (EXIT_FAILURE, 0, "dwarf_linediscriminator: %s",
    133 		       dwarf_errmsg (-1));
    134 
    135 	      const char *dw_src;
    136 	      Dwarf_Word dw_mtime;
    137 	      Dwarf_Word dw_length;
    138 	      dw_src = dwarf_linesrc (dw_line, &dw_mtime, &dw_length);
    139 	      assert (strcmp (src, dw_src) == 0);
    140 	      assert (mtime == dw_mtime);
    141 	      assert (length == dw_length);
    142 
    143 	      printf ("%zd %#" PRIx64 " %s:%d:%d\n"
    144 		      " time: %#" PRIX64 ", len: %" PRIu64
    145 		      ", idx: %d, b: %d, e: %d"
    146 		      ", pe: %d, eb: %d, block: %d"
    147 		      ", isa: %d, disc: %d\n",
    148 		      i, addr, src, lineno, colno, mtime, length,
    149 		      dw_op_index, begin, end, pend, ebegin, block, isa, disc);
    150 
    151 	      Dwarf_Die *linecu = dwfl_linecu (line);
    152 	      assert (cu == linecu);
    153 
    154 	      Dwfl_Module *linemod = dwfl_linemodule (line);
    155 	      assert (mod == linemod);
    156 	    }
    157 	}
    158     }
    159   while (cu != NULL);
    160 
    161   dwfl_end (dwfl);
    162 
    163   return 0;
    164 }
    165