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