Mention IFUNC enhancements to testsuite in NEWS.
[platform/upstream/glibc.git] / elf / pldd.c
1 /* List dynamic shared objects linked into given process.
2    Copyright (C) 2011, 2012 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
38 #include <ldsodefs.h>
39 #include <version.h>
40
41 /* Global variables.  */
42 extern char *program_invocation_short_name;
43 #define PACKAGE _libc_intl_domainname
44
45 /* External functions.  */
46 extern void *xmalloc (size_t n)
47   __attribute_malloc__ __attribute_alloc_size (1);
48 extern void *xrealloc (void *o, size_t n)
49   __attribute_malloc__ __attribute_alloc_size (2);
50
51 /* Name and version of program.  */
52 static void print_version (FILE *stream, struct argp_state *state);
53 void (*argp_program_version_hook) (FILE *, struct argp_state *) = print_version;
54
55 /* Function to print some extra text in the help message.  */
56 static char *more_help (int key, const char *text, void *input);
57
58 /* Definitions of arguments for argp functions.  */
59 static const struct argp_option options[] =
60 {
61   { NULL, 0, NULL, 0, NULL }
62 };
63
64 /* Short description of program.  */
65 static const char doc[] = N_("\
66 List dynamic shared objects loaded into process.");
67
68 /* Strings for arguments in help texts.  */
69 static const char args_doc[] = N_("PID");
70
71 /* Prototype for option handler.  */
72 static error_t parse_opt (int key, char *arg, struct argp_state *state);
73
74 /* Data structure to communicate with argp functions.  */
75 static struct argp argp =
76 {
77   options, parse_opt, args_doc, doc, NULL, more_help, NULL
78 };
79
80 // File descriptor of /proc/*/mem file.
81 static int memfd;
82
83 /* Name of the executable  */
84 static char *exe;
85
86 /* Local functions.  */
87 static int get_process_info (int dfd, 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   size_t exesize = 1024;
123 #ifdef PATH_MAX
124   exesize = PATH_MAX;
125 #endif
126   exe = alloca (exesize);
127   ssize_t nexe;
128   while ((nexe = readlinkat (dfd, "exe", exe, exesize)) == exesize)
129     extend_alloca (exe, exesize, 2 * exesize);
130   if (nexe == -1)
131     exe = (char *) "<program name undetermined>";
132   else
133     exe[nexe] = '\0';
134
135   /* Stop all threads since otherwise the list of loaded modules might
136      change while we are reading it.  */
137   struct thread_list
138   {
139     pid_t tid;
140     struct thread_list *next;
141   } *thread_list = NULL;
142
143   int taskfd = openat (dfd, "task", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
144   if (taskfd == 1)
145     error (EXIT_FAILURE, errno, gettext ("cannot open %s/task"), buf);
146   DIR *dir = fdopendir (taskfd);
147   if (dir == NULL)
148     error (EXIT_FAILURE, errno, gettext ("cannot prepare reading %s/task"),
149            buf);
150
151   struct dirent64 *d;
152   while ((d = readdir64 (dir)) != NULL)
153     {
154       if (! isdigit (d->d_name[0]))
155         continue;
156
157       errno = 0;
158       long int tid = strtol (d->d_name, &endp, 10);
159       if (tid < 0 || (tid == ULONG_MAX && errno == ERANGE) || *endp != '\0'
160           || (sizeof (pid_t) < sizeof (pid) && tid > INT_MAX))
161         error (EXIT_FAILURE, 0, gettext ("invalid thread ID '%s'"),
162                d->d_name);
163
164       if (ptrace (PTRACE_ATTACH, tid, NULL, NULL) != 0)
165         {
166           /* There might be a race between reading the directory and
167              threads terminating.  Ignore errors attaching to unknown
168              threads unless this is the main thread.  */
169           if (errno == ESRCH && tid != pid)
170             continue;
171
172           error (EXIT_FAILURE, errno, gettext ("cannot attach to process %lu"),
173                  tid);
174         }
175
176       struct thread_list *newp = alloca (sizeof (*newp));
177       newp->tid = tid;
178       newp->next = thread_list;
179       thread_list = newp;
180     }
181
182   closedir (dir);
183
184   int status = get_process_info (dfd, pid);
185
186   assert (thread_list != NULL);
187   do
188     {
189       ptrace (PTRACE_DETACH, thread_list->tid, NULL, NULL);
190       thread_list = thread_list->next;
191     }
192   while (thread_list != NULL);
193
194   close (dfd);
195
196   return status;
197 }
198
199
200 /* Handle program arguments.  */
201 static error_t
202 parse_opt (int key, char *arg, struct argp_state *state)
203 {
204   switch (key)
205     {
206     default:
207       return ARGP_ERR_UNKNOWN;
208     }
209   return 0;
210 }
211
212
213 /* Print bug-reporting information in the help message.  */
214 static char *
215 more_help (int key, const char *text, void *input)
216 {
217   char *tp = NULL;
218   switch (key)
219     {
220     case ARGP_KEY_HELP_EXTRA:
221       /* We print some extra information.  */
222       if (asprintf (&tp, gettext ("\
223 For bug reporting instructions, please see:\n\
224 %s.\n"), REPORT_BUGS_TO) < 0)
225         return NULL;
226       return tp;
227     default:
228       break;
229     }
230   return (char *) text;
231 }
232
233 /* Print the version information.  */
234 static void
235 print_version (FILE *stream, struct argp_state *state)
236 {
237   fprintf (stream, "pldd %s%s\n", PKGVERSION, VERSION);
238   fprintf (stream, gettext ("\
239 Copyright (C) %s Free Software Foundation, Inc.\n\
240 This is free software; see the source for copying conditions.  There is NO\n\
241 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
242 "), "2012");
243   fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
244 }
245
246
247 #define CLASS 32
248 #include "pldd-xx.c"
249 #define CLASS 64
250 #include "pldd-xx.c"
251
252
253 static int
254 get_process_info (int dfd, long int pid)
255 {
256   memfd = openat (dfd, "mem", O_RDONLY);
257   if (memfd == -1)
258     goto no_info;
259
260   int fd = openat (dfd, "exe", O_RDONLY);
261   if (fd == -1)
262     {
263     no_info:
264       error (0, errno, gettext ("cannot get information about process %lu"),
265              pid);
266       return EXIT_FAILURE;
267     }
268
269   char e_ident[EI_NIDENT];
270   if (read (fd, e_ident, EI_NIDENT) != EI_NIDENT)
271     goto no_info;
272
273   close (fd);
274
275   if (memcmp (e_ident, ELFMAG, SELFMAG) != 0)
276     {
277       error (0, 0, gettext ("process %lu is no ELF program"), pid);
278       return EXIT_FAILURE;
279     }
280
281   fd = openat (dfd, "auxv", O_RDONLY);
282   if (fd == -1)
283     goto no_info;
284
285   size_t auxv_size = 0;
286   void *auxv = NULL;
287   while (1)
288     {
289       auxv_size += 512;
290       auxv = xrealloc (auxv, auxv_size);
291
292       ssize_t n = pread (fd, auxv, auxv_size, 0);
293       if (n < 0)
294         goto no_info;
295       if (n < auxv_size)
296         {
297           auxv_size = n;
298           break;
299         }
300     }
301
302   close (fd);
303
304   int retval;
305   if (e_ident[EI_CLASS] == ELFCLASS32)
306     retval = find_maps32 (pid, auxv, auxv_size);
307   else
308     retval = find_maps64 (pid, auxv, auxv_size);
309
310   free (auxv);
311   close (memfd);
312
313   return retval;
314 }