summaryrefslogtreecommitdiffstats
path: root/meta/recipes-devtools/dwarfsrcfiles/files/dwarfsrcfiles.c
blob: af7af524ebe232217b01a63e77417366a39782de (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// dwarfsrcfiles.c - Get source files associated with the dwarf in a elf file.
// gcc -Wall -g -O2 -lelf -ldw -o dwarfsrcfiles dwarfsrcfiles.c 
//
// Copyright (C) 2011, Mark Wielaard <mjw@redhat.com>
//
// This file is free software.  You can redistribute it and/or modify
// it under the terms of the GNU General Public License (GPL); either
// version 2, or (at your option) any later version.

#include <argp.h>
#include <stdio.h>

#include <dwarf.h>
#include <elfutils/libdw.h>
#include <elfutils/libdwfl.h>

static int
process_cu (Dwarf_Die *cu_die)
{
  Dwarf_Attribute attr;
  const char *name;
  const char *dir = NULL;
  
  Dwarf_Files *files;
  size_t n;
  int i;
  
  if (dwarf_tag (cu_die) != DW_TAG_compile_unit)
    {
      fprintf (stderr, "DIE isn't a compile unit");
      return -1;
    }
  
  if (dwarf_attr (cu_die, DW_AT_name, &attr) == NULL)
    {
      fprintf(stderr, "CU doesn't have a DW_AT_name");
      return -1;
    }
  
  name = dwarf_formstring (&attr);
  if (name == NULL)
    {
      fprintf(stderr, "Couldn't get DW_AT_name as string, %s",
	     dwarf_errmsg (-1));
      return -1;
    }
  
  if (dwarf_attr (cu_die, DW_AT_comp_dir, &attr) != NULL)
    {
      dir = dwarf_formstring (&attr);
      if (dir == NULL)
	{
	  fprintf(stderr, "Couldn't get DW_AT_comp_die as string, %s",
		 dwarf_errmsg (-1));
	  return -1;
	}
    }
  
  if (dir == NULL)
    printf ("%s\n", name);
  else
    printf ("%s/%s\n", dir, name);
  
  if (dwarf_getsrcfiles (cu_die, &files, &n) != 0)
    {
      fprintf(stderr, "Couldn't get CU file table, %s",
	     dwarf_errmsg (-1));
      return -1;
    }
  
  for (i = 1; i < n; i++)
    {
      const char *file = dwarf_filesrc (files, i, NULL, NULL);
      if (dir != NULL && file[0] != '/')
	printf ("\t%s/%s\n", dir, file);
      else
	printf ("\t%s\n", file);
    }
  
  return 0;
}

int
main (int argc, char **argv)
{
  char* args[3];
  int res = 0;
  Dwfl *dwfl;
  Dwarf_Addr bias;
  
  if (argc != 2)
    fprintf(stderr, "Usage %s <file>", argv[0]);
  
  // Pretend "dwarfsrcfiles -e <file>" was given, so we can use standard
  // dwfl argp parser to open the file for us and get our Dwfl. Useful
  // in case argument is an ET_REL file (like kernel modules). libdwfl
  // will fix up relocations for us.
  args[0] = argv[0];
  args[1] = "-e";
  args[2] = argv[1];
  
  argp_parse (dwfl_standard_argp (), 3, args, 0, NULL, &dwfl);
  
  Dwarf_Die *cu = NULL;
  while ((cu = dwfl_nextcu (dwfl, cu, &bias)) != NULL)
    res |= process_cu (cu);
  
  dwfl_end (dwfl);

  return res;
}