summaryrefslogtreecommitdiff
path: root/tests/vdsosyms.c
blob: b876c10bde1217a758326d2b7e17a2ae41adea2d (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
/* Test program for getting symbol table from vdso module.
   Copyright (C) 2014 Red Hat, Inc.
   This file is part of elfutils.

   This file is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   elfutils is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include <config.h>
#include <assert.h>
#include <errno.h>
#include <error.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include ELFUTILS_HEADER(dwfl)

#ifndef __linux__
int
main (int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
{
  printf ("Getting the vdso is unsupported.\n");
  return 77;
}
#else /* __linux__ */
static int vdso_syms = 0;

static int
module_callback (Dwfl_Module *mod, void **userdata __attribute__((unused)),
		 const char *name, Dwarf_Addr start __attribute__((unused)),
		 void *arg __attribute__((unused)))
{
  /* We can only recognize the vdso by inspecting the "magic name".  */
  printf ("module name: %s\n", name);
  if (strncmp ("[vdso: ", name, 7) == 0)
    {
      vdso_syms = dwfl_module_getsymtab (mod);
      printf ("vdso syms: %d\n", vdso_syms);
      if (vdso_syms < 0)
	error (2, 0, "dwfl_module_getsymtab: %s", dwfl_errmsg (-1));

      for (int i = 0; i < vdso_syms; i++)
	{
	  GElf_Sym sym;
	  GElf_Addr addr;
	  const char *sname = dwfl_module_getsym_info (mod, i, &sym, &addr,
						       NULL, NULL, NULL);
	  assert (sname != NULL);
	  printf ("%d: '%s' %" PRIx64 " (%" PRIx64 ")\n",
		  i, sname, sym.st_value, addr);
	}
    }

  return DWARF_CB_OK;
}

int
main (int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
{
  static char *debuginfo_path;
  static const Dwfl_Callbacks proc_callbacks =
    {
      .find_debuginfo = dwfl_standard_find_debuginfo,
      .debuginfo_path = &debuginfo_path,

      .find_elf = dwfl_linux_proc_find_elf,
    };
  Dwfl *dwfl = dwfl_begin (&proc_callbacks);
  if (dwfl == NULL)
    error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));

  /* Take ourself as "arbitrary" process to inspect.  This should work
     even with "restricted ptrace".  */
  pid_t pid = getpid();

  int result = dwfl_linux_proc_report (dwfl, pid);
  if (result < 0)
    error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
  else if (result > 0)
    error (2, result, "dwfl_linux_proc_report");

  /* Also explicitly attach for older kernels (cannot read vdso otherwise).  */
  result = dwfl_linux_proc_attach (dwfl, pid, false);
  if (result < 0)
    error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
  else if (result > 0)
    error (2, result, "dwfl_linux_proc_attach");

  if (dwfl_report_end (dwfl, NULL, NULL) != 0)
    error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));

  if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
    error (1, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));

  /* No symbols is ok, then we haven't seen the vdso at all on this arch.  */
  return vdso_syms >= 0 ? 0 : -1;
}

#endif /* ! __linux__ */