blob: f637311741bb9eaf688c680c1d675b08a1ca1333 [file] [log] [blame]
Mark Wielaard1b734df2013-09-20 09:50:42 -04001/* Copyright (C) 2005, 2013 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02002 This file is part of elfutils.
Roland McGrathd7f8d0c2005-11-17 02:32:03 +00003
Mark Wielaardde2ed972012-06-05 17:15:16 +02004 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.
Roland McGrathd7f8d0c2005-11-17 02:32:03 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000010 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000013
Mark Wielaardde2ed972012-06-05 17:15:16 +020014 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/>. */
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000016
17#ifdef HAVE_CONFIG_H
18# include <config.h>
19#endif
20
Florian Weimer22481652014-04-15 16:58:39 +020021#include <err.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000022#include <fcntl.h>
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000023#include ELFUTILS_HEADER(dw)
Florian Weimer22481652014-04-15 16:58:39 +020024#include ELFUTILS_HEADER(dwelf)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025#include <stdio.h>
26#include <unistd.h>
27
28
29static int
Roland McGrath6724c902005-10-28 07:07:19 +000030cb (Dwarf_Die *func, void *arg __attribute__ ((unused)))
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000031{
Roland McGrath6724c902005-10-28 07:07:19 +000032 const char *file = dwarf_decl_file (func);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000033 int line = -1;
Roland McGrath6724c902005-10-28 07:07:19 +000034 dwarf_decl_line (func, &line);
35 const char *fct = dwarf_diename (func);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000036
37 printf ("%s:%d:%s\n", file, line, fct);
38
Mark Wielaard1b734df2013-09-20 09:50:42 -040039 return DWARF_CB_ABORT;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000040}
41
Florian Weimer22481652014-04-15 16:58:39 +020042static 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)
Mark Wielaard240a0682018-01-19 23:59:21 +010054 {
55 printf ("Warning: no alt file found.\n");
56 return NULL;
57 }
Florian Weimer22481652014-04-15 16:58:39 +020058 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}
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000067
68int
69main (int argc, char *argv[])
70{
71 for (int i = 1; i < argc; ++i)
72 {
73 int fd = open (argv[i], O_RDONLY);
Florian Weimer22481652014-04-15 16:58:39 +020074 if (fd < 0)
75 err (1, "open (%s)", argv[i]);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000076
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;
Florian Weimer22481652014-04-15 16:58:39 +020083 Dwarf *dbg_alt = setup_alt (dbg);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000084
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
Mark Wielaard1b734df2013-09-20 09:50:42 -040090 /* 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);
Florian Weimer22481652014-04-15 16:58:39 +020095 if (dwarf_errno () != 0)
96 errx (1, "dwarf_getfuncs (%s): %s",
97 argv[i], dwarf_errmsg (-1));
Mark Wielaard1b734df2013-09-20 09:50:42 -040098 }
Florian Weimer22481652014-04-15 16:58:39 +020099 while (doff != 0);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000100
101 off = noff;
102 }
103
Florian Weimer22481652014-04-15 16:58:39 +0200104 dwarf_end (dbg_alt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000105 dwarf_end (dbg);
106 }
Florian Weimer22481652014-04-15 16:58:39 +0200107 else
108 errx (1, "dwarf_begin (%s): %s", argv[i], dwarf_errmsg (-1));
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000109
110 close (fd);
111 }
112}