xref: /third_party/elfutils/tests/allfcts.c (revision da0c48c4)
1/* Copyright (C) 2005, 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 <err.h>
22#include <fcntl.h>
23#include ELFUTILS_HEADER(dw)
24#include ELFUTILS_HEADER(dwelf)
25#include <stdio.h>
26#include <unistd.h>
27
28
29static int
30cb (Dwarf_Die *func, void *arg __attribute__ ((unused)))
31{
32  const char *file = dwarf_decl_file (func);
33  int line = -1;
34  dwarf_decl_line (func, &line);
35  const char *fct = dwarf_diename (func);
36
37  printf ("%s:%d:%s\n", file, line, fct);
38
39  return DWARF_CB_ABORT;
40}
41
42static Dwarf *
43setup_alt (Dwarf *main)
44{
45  const char *alt_name;
46  const void *build_id;
47  ssize_t ret = dwelf_dwarf_gnu_debugaltlink (main, &alt_name, &build_id);
48  if (ret == 0)
49    return NULL;
50  if (ret == -1)
51    errx (1, "dwelf_dwarf_gnu_debugaltlink: %s", dwarf_errmsg (-1));
52  int fd = open (alt_name, O_RDONLY);
53  if (fd < 0)
54    {
55      printf ("Warning: no alt file found.\n");
56      return NULL;
57    }
58  Dwarf *dbg_alt = dwarf_begin (fd, DWARF_C_READ);
59  if (dbg_alt == NULL)
60    errx (1, "dwarf_begin (%s): %s", alt_name, dwarf_errmsg (-1));
61  if (elf_cntl (dwarf_getelf (dbg_alt), ELF_C_FDREAD) != 0)
62    errx (1, "elf_cntl (%s, ELF_C_FDREAD): %s", alt_name, elf_errmsg (-1));
63  close (fd);
64  dwarf_setalt (main, dbg_alt);
65  return dbg_alt;
66}
67
68int
69main (int argc, char *argv[])
70{
71  for (int i = 1; i < argc; ++i)
72    {
73      int fd = open (argv[i], O_RDONLY);
74      if (fd < 0)
75	err (1, "open (%s)", argv[i]);
76
77      Dwarf *dbg = dwarf_begin (fd, DWARF_C_READ);
78      if (dbg != NULL)
79	{
80	  Dwarf_Off off = 0;
81	  size_t cuhl;
82	  Dwarf_Off noff;
83	  Dwarf *dbg_alt = setup_alt (dbg);
84
85	  while (dwarf_nextcu (dbg, off, &noff, &cuhl, NULL, NULL, NULL) == 0)
86	    {
87	      Dwarf_Die die_mem;
88	      Dwarf_Die *die = dwarf_offdie (dbg, off + cuhl, &die_mem);
89
90	      /* Explicitly stop in the callback and then resume each time.  */
91	      ptrdiff_t doff = 0;
92	      do
93		{
94		  doff = dwarf_getfuncs (die, cb, NULL, doff);
95		  if (dwarf_errno () != 0)
96		    errx (1, "dwarf_getfuncs (%s): %s",
97			  argv[i], dwarf_errmsg (-1));
98		}
99	      while (doff != 0);
100
101	      off = noff;
102	    }
103
104	  dwarf_end (dbg_alt);
105	  dwarf_end (dbg);
106	}
107      else
108	errx (1, "dwarf_begin (%s): %s", argv[i], dwarf_errmsg (-1));
109
110      close (fd);
111    }
112}
113