Blame tests/dwfl-proc-attach.c

Packit 032894
/* Test dwfl_linux_proc_attach works without any modules.
Packit 032894
   Copyright (C) 2015 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 <inttypes.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <errno.h>
Packit 032894
#include <unistd.h>
Packit 032894
#ifdef __linux__
Packit 032894
#include <sys/types.h>
Packit 032894
#include <sys/stat.h>
Packit 032894
#include <sys/user.h>
Packit 032894
#include <fcntl.h>
Packit 032894
#include <string.h>
Packit 032894
#include ELFUTILS_HEADER(dwfl)
Packit 032894
#include <pthread.h>
Packit 032894
#endif
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 ("dwfl_linux_proc_attach unsupported.\n");
Packit 032894
  return 77;
Packit 032894
}
Packit 032894
#else /* __linux__ */
Packit 032894
Packit 032894
static pthread_t thread1;
Packit 032894
static pthread_t thread2;
Packit 032894
Packit 032894
static void *
Packit 032894
sleeper (void* d __attribute__ ((unused)))
Packit 032894
{
Packit 032894
  sleep (60);
Packit 032894
  return NULL;
Packit 032894
}
Packit 032894
Packit 032894
static char *debuginfo_path = NULL;
Packit 032894
Packit 032894
static const Dwfl_Callbacks proc_callbacks =
Packit 032894
  {
Packit 032894
    .find_elf = dwfl_linux_proc_find_elf,
Packit 032894
    .find_debuginfo = dwfl_standard_find_debuginfo,
Packit 032894
    .debuginfo_path = &debuginfo_path,
Packit 032894
  };
Packit 032894
Packit 032894
static int
Packit 032894
thread_callback (Dwfl_Thread *thread, void *thread_arg)
Packit 032894
{
Packit 032894
  int *threads = (int *) thread_arg;
Packit 032894
  pid_t tid = dwfl_thread_tid (thread);
Packit 032894
  printf ("thread tid: %d\n", tid);
Packit 032894
  (*threads)++;
Packit 032894
Packit 032894
  return DWARF_CB_OK;
Packit 032894
}
Packit 032894
Packit 032894
int
Packit 032894
main (int argc __attribute__ ((unused)),
Packit 032894
      char **argv __attribute__ ((unused)))
Packit 032894
{
Packit 032894
  /* Create two extra threads to iterate through.  */
Packit 032894
  int err;
Packit 032894
  if ((err = pthread_create (&thread1, NULL, sleeper, NULL)) != 0)
Packit 032894
    error (-1, err, "Couldn't create thread1");
Packit 032894
  if ((err = pthread_create (&thread2, NULL, sleeper, NULL)) != 0)
Packit 032894
    error (-1, err, "Couldn't create thread2");
Packit 032894
Packit 032894
  Dwfl *dwfl = dwfl_begin (&proc_callbacks);
Packit 032894
  if (dwfl == NULL)
Packit 032894
    error (-1, 0, "dwfl_begin: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  pid_t pid = getpid ();
Packit 032894
  /* This used to fail, since we don't have any modules yet.  */
Packit 032894
  if (dwfl_linux_proc_attach (dwfl, pid, false) < 0)
Packit 032894
    error (-1, 0, "dwfl_linux_proc_attach pid %d: %s", pid,
Packit 032894
	   dwfl_errmsg (-1));
Packit 032894
Packit 032894
  /* Did we see all 3 threads?  */
Packit 032894
  int threads = 0;
Packit 032894
  if (dwfl_getthreads (dwfl, thread_callback, &threads) != DWARF_CB_OK)
Packit 032894
    error (-1, 0, "dwfl_getthreads failed: %s", dwfl_errmsg (-1));
Packit 032894
Packit 032894
  return (threads == 3) ? 0 : -1;
Packit 032894
}
Packit 032894
Packit 032894
#endif /* __linux__ */