|
Packit |
6c4009 |
/* List dynamic shared objects linked into given process.
|
|
Packit |
6c4009 |
Copyright (C) 2011-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit Service |
6f196e |
#define _FILE_OFFSET_BITS 64
|
|
Packit Service |
6f196e |
|
|
Packit |
6c4009 |
#include <argp.h>
|
|
Packit |
6c4009 |
#include <dirent.h>
|
|
Packit |
6c4009 |
#include <error.h>
|
|
Packit |
6c4009 |
#include <fcntl.h>
|
|
Packit |
6c4009 |
#include <libintl.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <sys/ptrace.h>
|
|
Packit |
6c4009 |
#include <sys/wait.h>
|
|
Packit |
6c4009 |
#include <scratch_buffer.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <ldsodefs.h>
|
|
Packit |
6c4009 |
#include <version.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Global variables. */
|
|
Packit |
6c4009 |
extern char *program_invocation_short_name;
|
|
Packit |
6c4009 |
#define PACKAGE _libc_intl_domainname
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* External functions. */
|
|
Packit |
6c4009 |
#include <programs/xmalloc.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Name and version of program. */
|
|
Packit |
6c4009 |
static void print_version (FILE *stream, struct argp_state *state);
|
|
Packit |
6c4009 |
void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Function to print some extra text in the help message. */
|
|
Packit |
6c4009 |
static char *more_help (int key, const char *text, void *input);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Definitions of arguments for argp functions. */
|
|
Packit |
6c4009 |
static const struct argp_option options[] =
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
{ NULL, 0, NULL, 0, NULL }
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Short description of program. */
|
|
Packit |
6c4009 |
static const char doc[] = N_("\
|
|
Packit |
6c4009 |
List dynamic shared objects loaded into process.");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Strings for arguments in help texts. */
|
|
Packit |
6c4009 |
static const char args_doc[] = N_("PID");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Prototype for option handler. */
|
|
Packit |
6c4009 |
static error_t parse_opt (int key, char *arg, struct argp_state *state);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Data structure to communicate with argp functions. */
|
|
Packit |
6c4009 |
static struct argp argp =
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
options, parse_opt, args_doc, doc, NULL, more_help, NULL
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Local functions. */
|
|
Packit Service |
6f196e |
static int get_process_info (const char *exe, int dfd, long int pid);
|
|
Packit |
6c4009 |
static void wait_for_ptrace_stop (long int pid);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
main (int argc, char *argv[])
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* Parse and process arguments. */
|
|
Packit |
6c4009 |
int remaining;
|
|
Packit |
6c4009 |
argp_parse (&argp, argc, argv, 0, &remaining, NULL);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (remaining != argc - 1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
fprintf (stderr,
|
|
Packit |
6c4009 |
gettext ("Exactly one parameter with process ID required.\n"));
|
|
Packit |
6c4009 |
argp_help (&argp, stderr, ARGP_HELP_SEE, program_invocation_short_name);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit Service |
6f196e |
_Static_assert (sizeof (pid_t) == sizeof (int)
|
|
Packit Service |
6f196e |
|| sizeof (pid_t) == sizeof (long int),
|
|
Packit Service |
6f196e |
"sizeof (pid_t) != sizeof (int) or sizeof (long int)");
|
|
Packit Service |
6f196e |
|
|
Packit |
6c4009 |
char *endp;
|
|
Packit |
6c4009 |
errno = 0;
|
|
Packit |
6c4009 |
long int pid = strtol (argv[remaining], &endp, 10);
|
|
Packit |
6c4009 |
if (pid < 0 || (pid == ULONG_MAX && errno == ERANGE) || *endp != '\0'
|
|
Packit |
6c4009 |
|| (sizeof (pid_t) < sizeof (pid) && pid > INT_MAX))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0, gettext ("invalid process ID '%s'"),
|
|
Packit |
6c4009 |
argv[remaining]);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Determine the program name. */
|
|
Packit |
6c4009 |
char buf[7 + 3 * sizeof (pid)];
|
|
Packit |
6c4009 |
snprintf (buf, sizeof (buf), "/proc/%lu", pid);
|
|
Packit |
6c4009 |
int dfd = open (buf, O_RDONLY | O_DIRECTORY);
|
|
Packit |
6c4009 |
if (dfd == -1)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, gettext ("cannot open %s"), buf);
|
|
Packit |
6c4009 |
|
|
Packit Service |
6f196e |
/* Name of the executable */
|
|
Packit Service |
6f196e |
struct scratch_buffer exe;
|
|
Packit Service |
6f196e |
scratch_buffer_init (&exe;;
|
|
Packit |
6c4009 |
ssize_t nexe;
|
|
Packit |
6c4009 |
while ((nexe = readlinkat (dfd, "exe",
|
|
Packit Service |
6f196e |
exe.data, exe.length)) == exe.length)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
6f196e |
if (!scratch_buffer_grow (&exe))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
nexe = -1;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (nexe == -1)
|
|
Packit Service |
6f196e |
/* Default stack allocation is at least 1024. */
|
|
Packit Service |
6f196e |
snprintf (exe.data, exe.length, "<program name undetermined>");
|
|
Packit |
6c4009 |
else
|
|
Packit Service |
6f196e |
((char*)exe.data)[nexe] = '\0';
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Stop all threads since otherwise the list of loaded modules might
|
|
Packit |
6c4009 |
change while we are reading it. */
|
|
Packit |
6c4009 |
struct thread_list
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
pid_t tid;
|
|
Packit |
6c4009 |
struct thread_list *next;
|
|
Packit |
6c4009 |
} *thread_list = NULL;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int taskfd = openat (dfd, "task", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
|
Packit |
6c4009 |
if (taskfd == 1)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, gettext ("cannot open %s/task"), buf);
|
|
Packit |
6c4009 |
DIR *dir = fdopendir (taskfd);
|
|
Packit |
6c4009 |
if (dir == NULL)
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, gettext ("cannot prepare reading %s/task"),
|
|
Packit |
6c4009 |
buf);
|
|
Packit |
6c4009 |
|
|
Packit Service |
6f196e |
struct dirent *d;
|
|
Packit Service |
6f196e |
while ((d = readdir (dir)) != NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (! isdigit (d->d_name[0]))
|
|
Packit |
6c4009 |
continue;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
errno = 0;
|
|
Packit |
6c4009 |
long int tid = strtol (d->d_name, &endp, 10);
|
|
Packit |
6c4009 |
if (tid < 0 || (tid == ULONG_MAX && errno == ERANGE) || *endp != '\0'
|
|
Packit |
6c4009 |
|| (sizeof (pid_t) < sizeof (pid) && tid > INT_MAX))
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, 0, gettext ("invalid thread ID '%s'"),
|
|
Packit |
6c4009 |
d->d_name);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (ptrace (PTRACE_ATTACH, tid, NULL, NULL) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* There might be a race between reading the directory and
|
|
Packit |
6c4009 |
threads terminating. Ignore errors attaching to unknown
|
|
Packit |
6c4009 |
threads unless this is the main thread. */
|
|
Packit |
6c4009 |
if (errno == ESRCH && tid != pid)
|
|
Packit |
6c4009 |
continue;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
error (EXIT_FAILURE, errno, gettext ("cannot attach to process %lu"),
|
|
Packit |
6c4009 |
tid);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
wait_for_ptrace_stop (tid);
|
|
Packit |
6c4009 |
|
|
Packit Service |
6f196e |
struct thread_list *newp = xmalloc (sizeof (*newp));
|
|
Packit |
6c4009 |
newp->tid = tid;
|
|
Packit |
6c4009 |
newp->next = thread_list;
|
|
Packit |
6c4009 |
thread_list = newp;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
closedir (dir);
|
|
Packit |
6c4009 |
|
|
Packit Service |
6f196e |
if (thread_list == NULL)
|
|
Packit Service |
6f196e |
error (EXIT_FAILURE, 0, gettext ("no valid %s/task entries"), buf);
|
|
Packit Service |
6f196e |
|
|
Packit Service |
6f196e |
int status = get_process_info (exe.data, dfd, pid);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
do
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
ptrace (PTRACE_DETACH, thread_list->tid, NULL, NULL);
|
|
Packit Service |
6f196e |
struct thread_list *prev = thread_list;
|
|
Packit |
6c4009 |
thread_list = thread_list->next;
|
|
Packit Service |
6f196e |
free (prev);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
while (thread_list != NULL);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
close (dfd);
|
|
Packit Service |
6f196e |
scratch_buffer_free (&exe;;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return status;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Wait for PID to enter ptrace-stop state after being attached. */
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
wait_for_ptrace_stop (long int pid)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int status;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* While waiting for SIGSTOP being delivered to the tracee we have to
|
|
Packit |
6c4009 |
reinject any other pending signal. Ignore all other errors. */
|
|
Packit |
6c4009 |
while (waitpid (pid, &status, __WALL) == pid && WIFSTOPPED (status))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* The STOP signal should not be delivered to the tracee. */
|
|
Packit |
6c4009 |
if (WSTOPSIG (status) == SIGSTOP)
|
|
Packit |
6c4009 |
return;
|
|
Packit |
6c4009 |
if (ptrace (PTRACE_CONT, pid, NULL,
|
|
Packit |
6c4009 |
(void *) (uintptr_t) WSTOPSIG (status)))
|
|
Packit |
6c4009 |
/* The only possible error is that the process died. */
|
|
Packit |
6c4009 |
return;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Handle program arguments. */
|
|
Packit |
6c4009 |
static error_t
|
|
Packit |
6c4009 |
parse_opt (int key, char *arg, struct argp_state *state)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
switch (key)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
return ARGP_ERR_UNKNOWN;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Print bug-reporting information in the help message. */
|
|
Packit |
6c4009 |
static char *
|
|
Packit |
6c4009 |
more_help (int key, const char *text, void *input)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *tp = NULL;
|
|
Packit |
6c4009 |
switch (key)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
case ARGP_KEY_HELP_EXTRA:
|
|
Packit |
6c4009 |
/* We print some extra information. */
|
|
Packit |
6c4009 |
if (asprintf (&tp, gettext ("\
|
|
Packit |
6c4009 |
For bug reporting instructions, please see:\n\
|
|
Packit |
6c4009 |
%s.\n"), REPORT_BUGS_TO) < 0)
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
return tp;
|
|
Packit |
6c4009 |
default:
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
return (char *) text;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Print the version information. */
|
|
Packit |
6c4009 |
static void
|
|
Packit |
6c4009 |
print_version (FILE *stream, struct argp_state *state)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
fprintf (stream, "pldd %s%s\n", PKGVERSION, VERSION);
|
|
Packit |
6c4009 |
fprintf (stream, gettext ("\
|
|
Packit |
6c4009 |
Copyright (C) %s Free Software Foundation, Inc.\n\
|
|
Packit |
6c4009 |
This is free software; see the source for copying conditions. There is NO\n\
|
|
Packit |
6c4009 |
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
|
|
Packit |
6c4009 |
"), "2018");
|
|
Packit |
6c4009 |
fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define CLASS 32
|
|
Packit |
6c4009 |
#include "pldd-xx.c"
|
|
Packit |
6c4009 |
#define CLASS 64
|
|
Packit |
6c4009 |
#include "pldd-xx.c"
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit Service |
6f196e |
get_process_info (const char *exe, int dfd, long int pid)
|
|
Packit |
6c4009 |
{
|
|
Packit Service |
6f196e |
/* File descriptor of /proc/<pid>/mem file. */
|
|
Packit Service |
6f196e |
int memfd = openat (dfd, "mem", O_RDONLY);
|
|
Packit |
6c4009 |
if (memfd == -1)
|
|
Packit |
6c4009 |
goto no_info;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int fd = openat (dfd, "exe", O_RDONLY);
|
|
Packit |
6c4009 |
if (fd == -1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
no_info:
|
|
Packit |
6c4009 |
error (0, errno, gettext ("cannot get information about process %lu"),
|
|
Packit |
6c4009 |
pid);
|
|
Packit |
6c4009 |
return EXIT_FAILURE;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
char e_ident[EI_NIDENT];
|
|
Packit |
6c4009 |
if (read (fd, e_ident, EI_NIDENT) != EI_NIDENT)
|
|
Packit |
6c4009 |
goto no_info;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
close (fd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (memcmp (e_ident, ELFMAG, SELFMAG) != 0)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
error (0, 0, gettext ("process %lu is no ELF program"), pid);
|
|
Packit |
6c4009 |
return EXIT_FAILURE;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
fd = openat (dfd, "auxv", O_RDONLY);
|
|
Packit |
6c4009 |
if (fd == -1)
|
|
Packit |
6c4009 |
goto no_info;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
size_t auxv_size = 0;
|
|
Packit |
6c4009 |
void *auxv = NULL;
|
|
Packit |
6c4009 |
while (1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
auxv_size += 512;
|
|
Packit |
6c4009 |
auxv = xrealloc (auxv, auxv_size);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ssize_t n = pread (fd, auxv, auxv_size, 0);
|
|
Packit |
6c4009 |
if (n < 0)
|
|
Packit |
6c4009 |
goto no_info;
|
|
Packit |
6c4009 |
if (n < auxv_size)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
auxv_size = n;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
close (fd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int retval;
|
|
Packit |
6c4009 |
if (e_ident[EI_CLASS] == ELFCLASS32)
|
|
Packit Service |
6f196e |
retval = find_maps32 (exe, memfd, pid, auxv, auxv_size);
|
|
Packit |
6c4009 |
else
|
|
Packit Service |
6f196e |
retval = find_maps64 (exe, memfd, pid, auxv, auxv_size);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
free (auxv);
|
|
Packit |
6c4009 |
close (memfd);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return retval;
|
|
Packit |
6c4009 |
}
|