Fix compilation due to __nan defines
[glibc.git] / elf / pldd.c
blobe97d96ae02bb23bd4a522cec8d21c6c65119c9f3
1 /* List dynamic shared objects linked into given process.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gmail.com>, 2011.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <alloca.h>
22 #include <argp.h>
23 #include <assert.h>
24 #include <dirent.h>
25 #include <elf.h>
26 #include <errno.h>
27 #include <error.h>
28 #include <fcntl.h>
29 #include <libintl.h>
30 #include <link.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/ptrace.h>
37 #include <sys/stat.h>
39 #include <ldsodefs.h>
40 #include <version.h>
42 /* Global variables. */
43 extern char *program_invocation_short_name;
44 #define PACKAGE _libc_intl_domainname
46 /* External functions. */
47 extern void *xmalloc (size_t n);
48 extern void *xrealloc (void *p, size_t n);
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;
54 /* Bug report address. */
55 const char *argp_program_bug_address = N_("\
56 For bug reporting instructions, please see:\n\
57 <http://www.gnu.org/software/libc/bugs.html>.\n");
59 /* Definitions of arguments for argp functions. */
60 static const struct argp_option options[] =
62 { NULL, 0, NULL, 0, NULL }
65 /* Short description of program. */
66 static const char doc[] = N_("\
67 List dynamic shared objects loaded into process.");
69 /* Strings for arguments in help texts. */
70 static const char args_doc[] = N_("PID");
72 /* Prototype for option handler. */
73 static error_t parse_opt (int key, char *arg, struct argp_state *state);
75 /* Data structure to communicate with argp functions. */
76 static struct argp argp =
78 options, parse_opt, args_doc, doc, NULL, NULL, NULL
81 // File descriptor of /proc/*/mem file.
82 static int memfd;
84 /* Name of the executable */
85 static char *exe;
87 /* Local functions. */
88 static int get_process_info (int dfd, long int pid);
91 int
92 main (int argc, char *argv[])
94 /* Parse and process arguments. */
95 int remaining;
96 argp_parse (&argp, argc, argv, 0, &remaining, NULL);
98 if (remaining != argc - 1)
100 fprintf (stderr,
101 gettext ("Exactly one parameter with process ID required.\n"));
102 argp_help (&argp, stderr, ARGP_HELP_SEE, program_invocation_short_name);
103 return 1;
106 assert (sizeof (pid_t) == sizeof (int)
107 || sizeof (pid_t) == sizeof (long int));
108 char *endp;
109 errno = 0;
110 long int pid = strtol (argv[remaining], &endp, 10);
111 if (pid < 0 || (pid == ULONG_MAX && errno == ERANGE) || *endp != '\0'
112 || (sizeof (pid_t) < sizeof (pid) && pid > INT_MAX))
113 error (EXIT_FAILURE, 0, gettext ("invalid process ID '%s'"),
114 argv[remaining]);
116 /* Determine the program name. */
117 char buf[7 + 3 * sizeof (pid)];
118 snprintf (buf, sizeof (buf), "/proc/%lu", pid);
119 int dfd = open (buf, O_RDONLY | O_DIRECTORY);
120 if (dfd == -1)
121 error (EXIT_FAILURE, errno, gettext ("cannot open %s"), buf);
123 size_t exesize = 1024;
124 #ifdef PATH_MAX
125 exesize = PATH_MAX;
126 #endif
127 exe = alloca (exesize);
128 ssize_t nexe;
129 while ((nexe = readlinkat (dfd, "exe", exe, exesize)) == exesize)
130 extend_alloca (exe, exesize, 2 * exesize);
131 if (nexe == -1)
132 exe = (char *) "<program name undetermined>";
133 else
134 exe[nexe] = '\0';
136 /* Stop all threads since otherwise the list of loaded modules might
137 change while we are reading it. */
138 struct thread_list
140 pid_t tid;
141 struct thread_list *next;
142 } *thread_list = NULL;
144 int taskfd = openat (dfd, "task", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
145 if (taskfd == 1)
146 error (EXIT_FAILURE, errno, gettext ("cannot open %s/task"), buf);
147 DIR *dir = fdopendir (taskfd);
148 if (dir == NULL)
149 error (EXIT_FAILURE, errno, gettext ("cannot prepare reading %s/task"),
150 buf);
152 struct dirent64 *d;
153 while ((d = readdir64 (dir)) != NULL)
155 if (! isdigit (d->d_name[0]))
156 continue;
158 errno = 0;
159 long int tid = strtol (d->d_name, &endp, 10);
160 if (tid < 0 || (tid == ULONG_MAX && errno == ERANGE) || *endp != '\0'
161 || (sizeof (pid_t) < sizeof (pid) && tid > INT_MAX))
162 error (EXIT_FAILURE, 0, gettext ("invalid thread ID '%s'"),
163 d->d_name);
165 if (ptrace (PTRACE_ATTACH, tid, NULL, NULL) != 0)
167 /* There might be a race between reading the directory and
168 threads terminating. Ignore errors attaching to unknown
169 threads unless this is the main thread. */
170 if (errno == ESRCH && tid != pid)
171 continue;
173 error (EXIT_FAILURE, errno, gettext ("cannot attach to process %lu"),
174 tid);
177 struct thread_list *newp = alloca (sizeof (*newp));
178 newp->tid = tid;
179 newp->next = thread_list;
180 thread_list = newp;
183 closedir (dir);
185 int status = get_process_info (dfd, pid);
187 assert (thread_list != NULL);
190 ptrace (PTRACE_DETACH, thread_list->tid, NULL, NULL);
191 thread_list = thread_list->next;
193 while (thread_list != NULL);
195 close (dfd);
197 return status;
201 /* Handle program arguments. */
202 static error_t
203 parse_opt (int key, char *arg, struct argp_state *state)
205 switch (key)
207 default:
208 return ARGP_ERR_UNKNOWN;
210 return 0;
214 /* Print the version information. */
215 static void
216 print_version (FILE *stream, struct argp_state *state)
218 fprintf (stream, "pldd (GNU %s) %s\n", PACKAGE, VERSION);
219 fprintf (stream, gettext ("\
220 Copyright (C) %s Free Software Foundation, Inc.\n\
221 This is free software; see the source for copying conditions. There is NO\n\
222 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
223 "), "2011");
224 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper");
228 #define CLASS 32
229 #include "pldd-xx.c"
230 #define CLASS 64
231 #include "pldd-xx.c"
234 static int
235 get_process_info (int dfd, long int pid)
237 memfd = openat (dfd, "mem", O_RDONLY);
238 if (memfd == -1)
239 goto no_info;
241 int fd = openat (dfd, "exe", O_RDONLY);
242 if (fd == -1)
244 no_info:
245 error (0, errno, gettext ("cannot get information about process %lu"),
246 pid);
247 return EXIT_FAILURE;
250 char e_ident[EI_NIDENT];
251 if (read (fd, e_ident, EI_NIDENT) != EI_NIDENT)
252 goto no_info;
254 close (fd);
256 if (memcmp (e_ident, ELFMAG, SELFMAG) != 0)
258 error (0, 0, gettext ("process %lu is no ELF program"), pid);
259 return EXIT_FAILURE;
262 fd = openat (dfd, "auxv", O_RDONLY);
263 if (fd == -1)
264 goto no_info;
266 size_t auxv_size = 0;
267 void *auxv = NULL;
268 while (1)
270 auxv_size += 512;
271 auxv = xrealloc (auxv, auxv_size);
273 ssize_t n = pread (fd, auxv, auxv_size, 0);
274 if (n < 0)
275 goto no_info;
276 if (n < auxv_size)
278 auxv_size = n;
279 break;
283 close (fd);
285 int retval;
286 if (e_ident[EI_CLASS] == ELFCLASS32)
287 retval = find_maps32 (pid, auxv, auxv_size);
288 else
289 retval = find_maps64 (pid, auxv, auxv_size);
291 free (auxv);
292 close (memfd);
294 return retval;