1/* List dynamic shared objects linked into given process.
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
19
20#define _FILE_OFFSET_BITS 64
21
22#include <argp.h>
23#include <dirent.h>
24#include <error.h>
25#include <fcntl.h>
26#include <libintl.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <sys/ptrace.h>
31#include <sys/wait.h>
32#include <scratch_buffer.h>
33
34#include <ldsodefs.h>
35#include <version.h>
36
37/* Global variables. */
38extern char *program_invocation_short_name;
39#define PACKAGE _libc_intl_domainname
40
41/* External functions. */
42#include <programs/xmalloc.h>
43
44/* Name and version of program. */
45static void print_version (FILE *stream, struct argp_state *state);
46void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
47
48/* Function to print some extra text in the help message. */
49static char *more_help (int key, const char *text, void *input);
50
51/* Definitions of arguments for argp functions. */
52static const struct argp_option options[] =
53{
54 { NULL, 0, NULL, 0, NULL }
55};
56
57/* Short description of program. */
58static const char doc[] = N_("\
59List dynamic shared objects loaded into process.");
60
61/* Strings for arguments in help texts. */
62static const char args_doc[] = N_("PID");
63
64/* Prototype for option handler. */
65static error_t parse_opt (int key, char *arg, struct argp_state *state);
66
67/* Data structure to communicate with argp functions. */
68static struct argp argp =
69{
70 options, parse_opt, args_doc, doc, NULL, more_help, NULL
71};
72
73
74/* Local functions. */
75static int get_process_info (const char *exe, int dfd, long int pid);
76static void wait_for_ptrace_stop (long int pid);
77
78
79int
80main (int argc, char *argv[])
81{
82 /* Parse and process arguments. */
83 int remaining;
84 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
85
86 if (remaining != argc - 1)
87 {
88 fprintf (stderr,
89 gettext ("Exactly one parameter with process ID required.\n"));
90 argp_help (&argp, stderr, ARGP_HELP_SEE, program_invocation_short_name);
91 return 1;
92 }
93
94 _Static_assert (sizeof (pid_t) == sizeof (int)
95 || sizeof (pid_t) == sizeof (long int),
96 "sizeof (pid_t) != sizeof (int) or sizeof (long int)");
97
98 char *endp;
99 errno = 0;
100 long int pid = strtol (argv[remaining], &endp, 10);
101 if (pid < 0 || (pid == ULONG_MAX && errno == ERANGE) || *endp != '\0'
102 || (sizeof (pid_t) < sizeof (pid) && pid > INT_MAX))
103 error (EXIT_FAILURE, 0, gettext ("invalid process ID '%s'"),
104 argv[remaining]);
105
106 /* Determine the program name. */
107 char buf[7 + 3 * sizeof (pid)];
108 snprintf (buf, sizeof (buf), "/proc/%lu", pid);
109 int dfd = open (buf, O_RDONLY | O_DIRECTORY);
110 if (dfd == -1)
111 error (EXIT_FAILURE, errno, gettext ("cannot open %s"), buf);
112
113 /* Name of the executable */
114 struct scratch_buffer exe;
115 scratch_buffer_init (&exe);
116 ssize_t nexe;
117 while ((nexe = readlinkat (dfd, "exe",
118 exe.data, exe.length)) == exe.length)
119 {
120 if (!scratch_buffer_grow (&exe))
121 {
122 nexe = -1;
123 break;
124 }
125 }
126 if (nexe == -1)
127 /* Default stack allocation is at least 1024. */
128 snprintf (exe.data, exe.length, "<program name undetermined>");
129 else
130 ((char*)exe.data)[nexe] = '\0';
131
132 /* Stop all threads since otherwise the list of loaded modules might
133 change while we are reading it. */
134 struct thread_list
135 {
136 pid_t tid;
137 struct thread_list *next;
138 } *thread_list = NULL;
139
140 int taskfd = openat (dfd, "task", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
141 if (taskfd == 1)
142 error (EXIT_FAILURE, errno, gettext ("cannot open %s/task"), buf);
143 DIR *dir = fdopendir (taskfd);
144 if (dir == NULL)
145 error (EXIT_FAILURE, errno, gettext ("cannot prepare reading %s/task"),
146 buf);
147
148 struct dirent *d;
149 while ((d = readdir (dir)) != NULL)
150 {
151 if (! isdigit (d->d_name[0]))
152 continue;
153
154 errno = 0;
155 long int tid = strtol (d->d_name, &endp, 10);
156 if (tid < 0 || (tid == ULONG_MAX && errno == ERANGE) || *endp != '\0'
157 || (sizeof (pid_t) < sizeof (pid) && tid > INT_MAX))
158 error (EXIT_FAILURE, 0, gettext ("invalid thread ID '%s'"),
159 d->d_name);
160
161 if (ptrace (PTRACE_ATTACH, tid, NULL, NULL) != 0)
162 {
163 /* There might be a race between reading the directory and
164 threads terminating. Ignore errors attaching to unknown
165 threads unless this is the main thread. */
166 if (errno == ESRCH && tid != pid)
167 continue;
168
169 error (EXIT_FAILURE, errno, gettext ("cannot attach to process %lu"),
170 tid);
171 }
172
173 wait_for_ptrace_stop (tid);
174
175 struct thread_list *newp = xmalloc (sizeof (*newp));
176 newp->tid = tid;
177 newp->next = thread_list;
178 thread_list = newp;
179 }
180
181 closedir (dir);
182
183 if (thread_list == NULL)
184 error (EXIT_FAILURE, 0, gettext ("no valid %s/task entries"), buf);
185
186 int status = get_process_info (exe.data, dfd, pid);
187
188 do
189 {
190 ptrace (PTRACE_DETACH, thread_list->tid, NULL, NULL);
191 struct thread_list *prev = thread_list;
192 thread_list = thread_list->next;
193 free (prev);
194 }
195 while (thread_list != NULL);
196
197 close (dfd);
198 scratch_buffer_free (&exe);
199
200 return status;
201}
202
203
204/* Wait for PID to enter ptrace-stop state after being attached. */
205static void
206wait_for_ptrace_stop (long int pid)
207{
208 int status;
209
210 /* While waiting for SIGSTOP being delivered to the tracee we have to
211 reinject any other pending signal. Ignore all other errors. */
212 while (waitpid (pid, &status, __WALL) == pid && WIFSTOPPED (status))
213 {
214 /* The STOP signal should not be delivered to the tracee. */
215 if (WSTOPSIG (status) == SIGSTOP)
216 return;
217 if (ptrace (PTRACE_CONT, pid, NULL,
218 (void *) (uintptr_t) WSTOPSIG (status)))
219 /* The only possible error is that the process died. */
220 return;
221 }
222}
223
224
225/* Handle program arguments. */
226static error_t
227parse_opt (int key, char *arg, struct argp_state *state)
228{
229 switch (key)
230 {
231 default:
232 return ARGP_ERR_UNKNOWN;
233 }
234 return 0;
235}
236
237
238/* Print bug-reporting information in the help message. */
239static char *
240more_help (int key, const char *text, void *input)
241{
242 char *tp = NULL;
243 switch (key)
244 {
245 case ARGP_KEY_HELP_EXTRA:
246 /* We print some extra information. */
247 if (asprintf (&tp, gettext ("\
248For bug reporting instructions, please see:\n\
249%s.\n"), REPORT_BUGS_TO) < 0)
250 return NULL;
251 return tp;
252 default:
253 break;
254 }
255 return (char *) text;
256}
257
258/* Print the version information. */
259static void
260print_version (FILE *stream, struct argp_state *state)
261{
262 fprintf (stream, "pldd %s%s\n", PKGVERSION, VERSION);
263 fprintf (stream, gettext ("\
264Copyright (C) %s Free Software Foundation, Inc.\n\
265This is free software; see the source for copying conditions. There is NO\n\
266warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
267"), "2020");
268 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
269}
270
271
272#define CLASS 32
273#include "pldd-xx.c"
274#define CLASS 64
275#include "pldd-xx.c"
276
277
278static int
279get_process_info (const char *exe, int dfd, long int pid)
280{
281 /* File descriptor of /proc/<pid>/mem file. */
282 int memfd = openat (dfd, "mem", O_RDONLY);
283 if (memfd == -1)
284 goto no_info;
285
286 int fd = openat (dfd, "exe", O_RDONLY);
287 if (fd == -1)
288 {
289 no_info:
290 error (0, errno, gettext ("cannot get information about process %lu"),
291 pid);
292 return EXIT_FAILURE;
293 }
294
295 char e_ident[EI_NIDENT];
296 if (read (fd, e_ident, EI_NIDENT) != EI_NIDENT)
297 goto no_info;
298
299 close (fd);
300
301 if (memcmp (e_ident, ELFMAG, SELFMAG) != 0)
302 {
303 error (0, 0, gettext ("process %lu is no ELF program"), pid);
304 return EXIT_FAILURE;
305 }
306
307 fd = openat (dfd, "auxv", O_RDONLY);
308 if (fd == -1)
309 goto no_info;
310
311 size_t auxv_size = 0;
312 void *auxv = NULL;
313 while (1)
314 {
315 auxv_size += 512;
316 auxv = xrealloc (auxv, auxv_size);
317
318 ssize_t n = pread (fd, auxv, auxv_size, 0);
319 if (n < 0)
320 goto no_info;
321 if (n < auxv_size)
322 {
323 auxv_size = n;
324 break;
325 }
326 }
327
328 close (fd);
329
330 int retval;
331 if (e_ident[EI_CLASS] == ELFCLASS32)
332 retval = find_maps32 (exe, memfd, pid, auxv, auxv_size);
333 else
334 retval = find_maps64 (exe, memfd, pid, auxv, auxv_size);
335
336 free (auxv);
337 close (memfd);
338
339 return retval;
340}
341