• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2002, 2004 Red Hat, Inc.
2    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
3 
4    This program is Open Source software; you can redistribute it and/or
5    modify it under the terms of the Open Software License version 1.0 as
6    published by the Open Source Initiative.
7 
8    You should have received a copy of the Open Software License along
9    with this program; if not, you may obtain a copy of the Open Software
10    License version 1.0 from http://www.opensource.org/licenses/osl.php or
11    by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12    3001 King Ranch Road, Ukiah, CA 95482.   */
13 
14 #include <fcntl.h>
15 #include <inttypes.h>
16 #include <libelf.h>
17 #include <libdw.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 
21 
22 int
main(int argc,char * argv[])23 main (int argc, char *argv[])
24 {
25   int result = 0;
26   int cnt;
27 
28   for (cnt = 1; cnt < argc; ++cnt)
29     {
30       int fd = open (argv[cnt], O_RDONLY);
31 
32       Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
33       if  (dbg == NULL)
34 	{
35 	  printf ("%s not usable: %s\n", argv[cnt], dwarf_errmsg (-1));
36 	  close  (fd);
37 	  continue;
38 	}
39 
40       Dwarf_Off cuoff = 0;
41       Dwarf_Off old_cuoff = 0;
42       size_t hsize;
43       Dwarf_Off ao;
44       uint8_t asz;
45       uint8_t osz;
46       while (dwarf_nextcu (dbg, cuoff, &cuoff, &hsize, &ao, &asz, &osz) == 0)
47 	{
48 	  printf ("cuhl = %zu, o = %llu, asz = %hhu, osz = %hhu, ncu = %llu\n",
49 		  hsize, (unsigned long long int) ao,
50 		  asz, osz, (unsigned long long int) cuoff);
51 
52 	  /* Get the DIE for the CU.  */
53 	  Dwarf_Die die;
54  	  if (dwarf_offdie (dbg, old_cuoff + hsize, &die) == NULL)
55 	    {
56 	      /* Something went wrong.  */
57 	      printf ("%s: cannot get CU die\n", argv[cnt]);
58 	      result = 1;
59 	      break;
60 	    }
61 	  old_cuoff = cuoff;
62 
63 	  Dwarf_Lines *lb;
64 	  size_t nlb;
65 	  if (dwarf_getsrclines (&die, &lb, &nlb) != 0)
66 	    {
67 	      printf ("%s: cannot get lines\n", argv[cnt]);
68 	      result = 1;
69 	      break;
70 	    }
71 
72 	  printf (" %zu lines\n", nlb);
73 
74 	  for (size_t i = 0; i < nlb; ++i)
75 	    {
76 	      Dwarf_Line *l = dwarf_onesrcline (lb, i);
77 	      if (l == NULL)
78 		{
79 		  printf ("%s: cannot get individual line\n", argv[cnt]);
80 		  result = 1;
81 		  break;
82 		}
83 
84 	      Dwarf_Addr addr;
85 	      if (dwarf_lineaddr (l, &addr) != 0)
86 		addr = 0;
87 	      const char *file = dwarf_linesrc (l, NULL, NULL);
88 	      int line;
89 	      if (dwarf_lineno (l, &line) != 0)
90 		line = 0;
91 
92 	      printf ("%" PRIx64 ": %s:%d:", (uint64_t) addr,
93 		      file ?: "???", line);
94 
95 	      int column;
96 	      if (dwarf_linecol (l, &column) != 0)
97 		column = 0;
98 	      if (column >= 0)
99 		printf ("%d:", column);
100 
101 	      bool is_stmt;
102 	      if (dwarf_linebeginstatement (l, &is_stmt) != 0)
103 		is_stmt = false;
104 	      bool end_sequence;
105 	      if (dwarf_lineendsequence (l, &end_sequence) != 0)
106 		end_sequence = false;
107 	      bool basic_block;
108 	      if (dwarf_lineblock (l, &basic_block) != 0)
109 		basic_block = false;
110 	      bool prologue_end;
111 	      if (dwarf_lineprologueend (l, &prologue_end) != 0)
112 		prologue_end = false;
113 	      bool epilogue_begin;
114 	      if (dwarf_lineepiloguebegin (l, &epilogue_begin) != 0)
115 		epilogue_begin = false;
116 
117 	      printf (" is_stmt:%s, end_seq:%s, bb:%s, prologue:%s, epilogue:%s\n",
118 		      is_stmt ? "yes" : "no", end_sequence ? "yes" : "no",
119 		      basic_block ? "yes" : "no", prologue_end  ? "yes" : "no",
120 		      epilogue_begin ? "yes" : "no");
121 	    }
122 	}
123 
124       dwarf_end (dbg);
125       close (fd);
126     }
127 
128   return result;
129 }
130