Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 2002, 2004 Red Hat, Inc.
      2    This file is part of elfutils.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      4 
      5    This file is free software; you can redistribute it and/or modify
      6    it under the terms of the GNU General Public License as published by
      7    the Free Software Foundation; either version 3 of the License, or
      8    (at your option) any later version.
      9 
     10    elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13    GNU General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License
     16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     17 
     18 #ifdef HAVE_CONFIG_H
     19 # include <config.h>
     20 #endif
     21 
     22 #include <fcntl.h>
     23 #include <inttypes.h>
     24 #include <libelf.h>
     25 #include ELFUTILS_HEADER(dw)
     26 #include <stdio.h>
     27 #include <unistd.h>
     28 
     29 
     30 int
     31 main (int argc, char *argv[])
     32 {
     33   int result = 0;
     34   int cnt;
     35 
     36   for (cnt = 1; cnt < argc; ++cnt)
     37     {
     38       int fd = open (argv[cnt], O_RDONLY);
     39 
     40       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
     41       if  (dbg == NULL)
     42 	{
     43 	  printf ("%s not usable: %s\n", argv[cnt], dwarf_errmsg (-1));
     44 	  close  (fd);
     45 	  continue;
     46 	}
     47 
     48       Dwarf_Off cuoff = 0;
     49       Dwarf_Off old_cuoff = 0;
     50       size_t hsize;
     51       Dwarf_Off ao;
     52       uint8_t asz;
     53       uint8_t osz;
     54       while (dwarf_nextcu (dbg, cuoff, &cuoff, &hsize, &ao, &asz, &osz) == 0)
     55 	{
     56 	  printf ("cuhl = %zu, o = %llu, asz = %hhu, osz = %hhu, ncu = %llu\n",
     57 		  hsize, (unsigned long long int) ao,
     58 		  asz, osz, (unsigned long long int) cuoff);
     59 
     60 	  /* Get the DIE for the CU.  */
     61 	  Dwarf_Die die;
     62  	  if (dwarf_offdie (dbg, old_cuoff + hsize, &die) == NULL)
     63 	    {
     64 	      /* Something went wrong.  */
     65 	      printf ("%s: cannot get CU die\n", argv[cnt]);
     66 	      result = 1;
     67 	      break;
     68 	    }
     69 	  old_cuoff = cuoff;
     70 
     71 	  Dwarf_Lines *lb;
     72 	  size_t nlb;
     73 	  if (dwarf_getsrclines (&die, &lb, &nlb) != 0)
     74 	    {
     75 	      printf ("%s: cannot get lines\n", argv[cnt]);
     76 	      result = 1;
     77 	      break;
     78 	    }
     79 
     80 	  printf (" %zu lines\n", nlb);
     81 
     82 	  for (size_t i = 0; i < nlb; ++i)
     83 	    {
     84 	      Dwarf_Line *l = dwarf_onesrcline (lb, i);
     85 	      if (l == NULL)
     86 		{
     87 		  printf ("%s: cannot get individual line\n", argv[cnt]);
     88 		  result = 1;
     89 		  break;
     90 		}
     91 
     92 	      Dwarf_Addr addr;
     93 	      if (dwarf_lineaddr (l, &addr) != 0)
     94 		addr = 0;
     95 	      const char *file = dwarf_linesrc (l, NULL, NULL);
     96 	      int line;
     97 	      if (dwarf_lineno (l, &line) != 0)
     98 		line = 0;
     99 
    100 	      printf ("%" PRIx64 ": %s:%d:", (uint64_t) addr,
    101 		      file ?: "???", line);
    102 
    103 	      int column;
    104 	      if (dwarf_linecol (l, &column) != 0)
    105 		column = 0;
    106 	      if (column >= 0)
    107 		printf ("%d:", column);
    108 
    109 	      bool is_stmt;
    110 	      if (dwarf_linebeginstatement (l, &is_stmt) != 0)
    111 		is_stmt = false;
    112 	      bool end_sequence;
    113 	      if (dwarf_lineendsequence (l, &end_sequence) != 0)
    114 		end_sequence = false;
    115 	      bool basic_block;
    116 	      if (dwarf_lineblock (l, &basic_block) != 0)
    117 		basic_block = false;
    118 	      bool prologue_end;
    119 	      if (dwarf_lineprologueend (l, &prologue_end) != 0)
    120 		prologue_end = false;
    121 	      bool epilogue_begin;
    122 	      if (dwarf_lineepiloguebegin (l, &epilogue_begin) != 0)
    123 		epilogue_begin = false;
    124 
    125 	      printf (" is_stmt:%s, end_seq:%s, bb:%s, prologue:%s, epilogue:%s\n",
    126 		      is_stmt ? "yes" : "no", end_sequence ? "yes" : "no",
    127 		      basic_block ? "yes" : "no", prologue_end  ? "yes" : "no",
    128 		      epilogue_begin ? "yes" : "no");
    129 	    }
    130 	}
    131 
    132       dwarf_end (dbg);
    133       close (fd);
    134     }
    135 
    136   return result;
    137 }
    138