1/* Test program for dwarf_entry_breakpoints.
2   Copyright (C) 2005 Red Hat, Inc.
3   This file is part of Red Hat elfutils.
4
5   Red Hat elfutils is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by the
7   Free Software Foundation; version 2 of the License.
8
9   Red Hat 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 GNU
12   General Public License for more details.
13
14   You should have received a copy of the GNU General Public License along
15   with Red Hat elfutils; if not, write to the Free Software Foundation,
16   Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17
18   Red Hat elfutils is an included package of the Open Invention Network.
19   An included package of the Open Invention Network is a package for which
20   Open Invention Network licensees cross-license their patents.  No patent
21   license is granted, either expressly or impliedly, by designation as an
22   included package.  Should you wish to participate in the Open Invention
23   Network licensing program, please visit www.openinventionnetwork.com
24   <http://www.openinventionnetwork.com>.  */
25
26#include <config.h>
27#include <assert.h>
28#include <inttypes.h>
29#include ELFUTILS_HEADER(dwfl)
30#include <dwarf.h>
31#include <argp.h>
32#include <stdio.h>
33#include <stdio_ext.h>
34#include <locale.h>
35#include <stdlib.h>
36#include <error.h>
37#include <string.h>
38#include <fnmatch.h>
39
40
41struct args
42{
43  Dwfl *dwfl;
44  Dwarf_Die *cu;
45  Dwarf_Addr dwbias;
46  char **argv;
47};
48
49static int
50handle_function (Dwarf_Die *func, void *arg)
51{
52  struct args *a = arg;
53
54  const char *name = dwarf_diename (func);
55  char **argv = a->argv;
56  if (argv[0] != NULL)
57    {
58      bool match;
59      do
60	match = fnmatch (*argv, name, 0) == 0;
61      while (!match && *++argv);
62      if (!match)
63	return 0;
64    }
65
66  if (dwarf_func_inline (func))
67    return 0;
68
69  Dwarf_Addr entrypc;
70  if (dwarf_entrypc (func, &entrypc) != 0)
71    error (EXIT_FAILURE, 0, "dwarf_entrypc: %s: %s",
72	   dwarf_diename (func), dwarf_errmsg (-1));
73  entrypc += a->dwbias;
74
75  printf ("%-16s %#.16" PRIx64, dwarf_diename (func), entrypc);
76
77  Dwarf_Addr *bkpts = NULL;
78  int result = dwarf_entry_breakpoints (func, &bkpts);
79  if (result <= 0)
80    printf ("\t%s\n", dwarf_errmsg (-1));
81  else
82    {
83      for (int i = 0; i < result; ++i)
84	printf (" %#.16" PRIx64 "%s", bkpts[i] + a->dwbias,
85		i == result - 1 ? "\n" : "");
86      free (bkpts);
87    }
88
89  return 0;
90}
91
92
93int
94main (int argc, char *argv[])
95{
96  int remaining;
97
98  /* Set locale.  */
99  (void) setlocale (LC_ALL, "");
100
101  struct args a = { .dwfl = NULL, .cu = NULL };
102
103  (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
104		     &a.dwfl);
105  assert (a.dwfl != NULL);
106  a.argv = &argv[remaining];
107
108  int result = 0;
109
110  while ((a.cu = dwfl_nextcu (a.dwfl, a.cu, &a.dwbias)) != NULL)
111    dwarf_getfuncs (a.cu, &handle_function, &a, 0);
112
113  dwfl_end (a.dwfl);
114
115  return result;
116}
117