summaryrefslogtreecommitdiff
path: root/elfutils/tests/line2addr.c
blob: d2017fbabe7e21d39f22caedb5e41db2b44b5f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <fcntl.h>
#include <inttypes.h>
#include <libdw.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>


int
main (int argc, char *argv[])
{
  for (int cnt = 1; cnt < argc; ++cnt)
    {
      char *fname;
      char *file;
      int line;

      switch (sscanf (argv[cnt], "%a[^:]:%a[^:]:%d",
			 &fname, &file, &line))
	{
	default:
	case 0:
	case 1:
	  printf ("ignored %s\n", argv[cnt]);
	  continue;
	case 2:
	  line = 0;
	  break;
	case 3:
	  break;
	}

      int fd = open (fname, O_RDONLY);
      if (fd == -1)
	continue;

      Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
      if (dbg != NULL)
	{
	  Dwarf_Line **lines = NULL;
	  size_t nlines = 0;

	  if (dwarf_getsrc_file (dbg, file, line, 0, &lines, &nlines) == 0)
	    {
	      for (size_t inner = 0; inner < nlines; ++inner)
		{
		  Dwarf_Addr addr;
		  if (dwarf_lineaddr (lines[inner], &addr) == 0)
		    printf ("%s -> %#" PRIxMAX "\n",
			    argv[cnt], (uintmax_t) addr);
		}

	      free (lines);
	    }

	  dwarf_end (dbg);
	}

      close (fd);
      free (fname);
      free (file);
    }

  return 0;
}