Blame tests/vdsosyms.c

Packit 032894
/* Test program for getting symbol table from vdso module.
Packit 032894
   Copyright (C) 2014 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of the GNU General Public License as published by
Packit 032894
   the Free Software Foundation; either version 3 of the License, or
Packit 032894
   (at your option) any later version.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 032894
   GNU General Public License for more details.
Packit 032894
Packit 032894
   You should have received a copy of the GNU General Public License
Packit 032894
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#include <config.h>
Packit 032894
#include <assert.h>
Packit 032894
#include <errno.h>
Packit 032894
#include <inttypes.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <string.h>
Packit 032894
#include <sys/types.h>
Packit 032894
#include <unistd.h>
Packit 032894
#include ELFUTILS_HEADER(dwfl)
Packit 032894
#include "system.h"
Packit 032894
Packit 032894
#ifndef __linux__
Packit 032894
int
Packit 032894
main (int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
Packit 032894
{
Packit 032894
  printf ("Getting the vdso is unsupported.\n");
Packit 032894
  return 77;
Packit 032894
}
Packit 032894
#else /* __linux__ */
Packit 032894
static int vdso_syms = 0;
Packit 032894
Packit 032894
static int
Packit 032894
module_callback (Dwfl_Module *mod, void **userdata __attribute__((unused)),
Packit 032894
		 const char *name, Dwarf_Addr start __attribute__((unused)),
Packit 032894
		 void *arg __attribute__((unused)))
Packit 032894
{
Packit 032894
  /* We can only recognize the vdso by inspecting the "magic name".  */
Packit 032894
  printf ("module name: %s\n", name);
Packit 032894
  if (strncmp ("[vdso: ", name, 7) == 0)
Packit 032894
    {
Packit 032894
      vdso_syms = dwfl_module_getsymtab (mod);
Packit 032894
      printf ("vdso syms: %d\n", vdso_syms);
Packit 032894
      if (vdso_syms < 0)
Packit 032894
	error (2, 0, "dwfl_module_getsymtab: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
      for (int i = 0; i < vdso_syms; i++)
Packit 032894
	{
Packit 032894
	  GElf_Sym sym;
Packit 032894
	  GElf_Addr addr;
Packit 032894
	  const char *sname = dwfl_module_getsym_info (mod, i, &sym, &addr,
Packit 032894
						       NULL, NULL, NULL);
Packit 032894
	  assert (sname != NULL);
Packit 032894
	  printf ("%d: '%s' %" PRIx64 " (%" PRIx64 ")\n",
Packit 032894
		  i, sname, sym.st_value, addr);
Packit 032894
	}
Packit 032894
    }
Packit 032894
Packit 032894
  return DWARF_CB_OK;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc __attribute__ ((unused)), char **argv __attribute__ ((unused)))
Packit 032894
{
Packit 032894
  static char *debuginfo_path;
Packit 032894
  static const Dwfl_Callbacks proc_callbacks =
Packit 032894
    {
Packit 032894
      .find_debuginfo = dwfl_standard_find_debuginfo,
Packit 032894
      .debuginfo_path = &debuginfo_path,
Packit 032894
Packit 032894
      .find_elf = dwfl_linux_proc_find_elf,
Packit 032894
    };
Packit 032894
  Dwfl *dwfl = dwfl_begin (&proc_callbacks);
Packit 032894
  if (dwfl == NULL)
Packit 032894
    error (2, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  /* Take ourself as "arbitrary" process to inspect.  This should work
Packit 032894
     even with "restricted ptrace".  */
Packit 032894
  pid_t pid = getpid();
Packit 032894
Packit 032894
  int result = dwfl_linux_proc_report (dwfl, pid);
Packit 032894
  if (result < 0)
Packit 032894
    error (2, 0, "dwfl_linux_proc_report: %s", dwfl_errmsg (-1));
Packit 032894
  else if (result > 0)
Packit 032894
    error (2, result, "dwfl_linux_proc_report");
Packit 032894
Packit 032894
  /* Also explicitly attach for older kernels (cannot read vdso otherwise).  */
Packit 032894
  result = dwfl_linux_proc_attach (dwfl, pid, false);
Packit 032894
  if (result < 0)
Packit 032894
    error (2, 0, "dwfl_linux_proc_attach: %s", dwfl_errmsg (-1));
Packit 032894
  else if (result > 0)
Packit 032894
    error (2, result, "dwfl_linux_proc_attach");
Packit 032894
Packit 032894
  if (dwfl_report_end (dwfl, NULL, NULL) != 0)
Packit 032894
    error (2, 0, "dwfl_report_end: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  if (dwfl_getmodules (dwfl, module_callback, NULL, 0) != 0)
Packit 032894
    error (1, 0, "dwfl_getmodules: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  /* No symbols is ok, then we haven't seen the vdso at all on this arch.  */
Packit 032894
  return vdso_syms >= 0 ? 0 : -1;
Packit 032894
}
Packit 032894
Packit 032894
#endif /* ! __linux__ */