8d19d11710afc650a68d3d2c901db31971c5cf86
[platform/upstream/elfutils.git] / tests / funcretval.c
1 /* Test program for dwfl_module_return_value_location.
2    Copyright (C) 2005 Red Hat, Inc.
3    This file is part of elfutils.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19 #include <assert.h>
20 #include <inttypes.h>
21 #include ELFUTILS_HEADER(dwfl)
22 #include <dwarf.h>
23 #include <argp.h>
24 #include <stdio.h>
25 #include <stdio_ext.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <error.h>
29 #include <string.h>
30 #include <fnmatch.h>
31
32
33 struct args
34 {
35   Dwfl *dwfl;
36   Dwarf_Die *cu;
37   Dwarf_Addr dwbias;
38   char **argv;
39 };
40
41 static int
42 handle_function (Dwarf_Die *funcdie, void *arg)
43 {
44   struct args *a = arg;
45
46   const char *name = dwarf_diename (funcdie);
47   char **argv = a->argv;
48   if (argv[0] != NULL)
49     {
50       bool match;
51       do
52         match = fnmatch (*argv, name, 0) == 0;
53       while (!match && *++argv);
54       if (!match)
55         return 0;
56     }
57
58   printf ("(%s) %s: ", dwfl_module_info (dwfl_cumodule (a->cu), NULL,
59                                          NULL, NULL,
60                                          NULL, NULL,
61                                          NULL, NULL), name);
62
63   const Dwarf_Op *locops;
64   int nlocops = dwfl_module_return_value_location (dwfl_cumodule (a->cu),
65                                                    funcdie, &locops);
66   if (nlocops < 0)
67     error (EXIT_FAILURE, 0, "dwfl_module_return_value_location: %s",
68            dwfl_errmsg (-1));
69   else if (nlocops == 0)
70     puts ("returns no value");
71   else
72     {
73       printf ("return value location:");
74       for (int i = 0; i < nlocops; ++i)
75         printf (" {%#x, %#" PRIx64 "}", locops[i].atom, locops[i].number);
76       puts ("");
77     }
78
79   return 0;
80 }
81
82
83 int
84 main (int argc, char *argv[])
85 {
86   int remaining;
87
88   /* Set locale.  */
89   (void) setlocale (LC_ALL, "");
90
91   struct args a = { .dwfl = NULL, .cu = NULL };
92
93   (void) argp_parse (dwfl_standard_argp (), argc, argv, 0, &remaining,
94                      &a.dwfl);
95   assert (a.dwfl != NULL);
96   a.argv = &argv[remaining];
97
98   int result = 0;
99
100   while ((a.cu = dwfl_nextcu (a.dwfl, a.cu, &a.dwbias)) != NULL)
101     dwarf_getfuncs (a.cu, &handle_function, &a, 0);
102
103   dwfl_end (a.dwfl);
104
105   return result;
106 }