1 /* List dynamic shared objects linked into given process.
2 Copyright (C) 2011-2015 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, see
18 <http://www.gnu.org/licenses/>. */
35 #include <sys/ptrace.h>
38 #include <scratch_buffer.h>
43 /* Global variables. */
44 extern char *program_invocation_short_name
;
45 #define PACKAGE _libc_intl_domainname
47 /* External functions. */
48 #include <programs/xmalloc.h>
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 /* Function to print some extra text in the help message. */
55 static char *more_help (int key
, const char *text
, void *input
);
57 /* Definitions of arguments for argp functions. */
58 static const struct argp_option options
[] =
60 { NULL
, 0, NULL
, 0, NULL
}
63 /* Short description of program. */
64 static const char doc
[] = N_("\
65 List dynamic shared objects loaded into process.");
67 /* Strings for arguments in help texts. */
68 static const char args_doc
[] = N_("PID");
70 /* Prototype for option handler. */
71 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
73 /* Data structure to communicate with argp functions. */
74 static struct argp argp
=
76 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
, NULL
79 // File descriptor of /proc/*/mem file.
82 /* Name of the executable */
85 /* Local functions. */
86 static int get_process_info (int dfd
, long int pid
);
87 static void wait_for_ptrace_stop (long int pid
);
91 main (int argc
, char *argv
[])
93 /* Parse and process arguments. */
95 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
97 if (remaining
!= argc
- 1)
100 gettext ("Exactly one parameter with process ID required.\n"));
101 argp_help (&argp
, stderr
, ARGP_HELP_SEE
, program_invocation_short_name
);
105 assert (sizeof (pid_t
) == sizeof (int)
106 || sizeof (pid_t
) == sizeof (long int));
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'"),
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
);
120 error (EXIT_FAILURE
, errno
, gettext ("cannot open %s"), buf
);
122 struct scratch_buffer exebuf
;
123 scratch_buffer_init (&exebuf
);
125 while ((nexe
= readlinkat (dfd
, "exe",
126 exebuf
.data
, exebuf
.length
)) == exebuf
.length
)
128 if (!scratch_buffer_grow (&exebuf
))
135 exe
= (char *) "<program name undetermined>";
142 /* Stop all threads since otherwise the list of loaded modules might
143 change while we are reading it. */
147 struct thread_list
*next
;
148 } *thread_list
= NULL
;
150 int taskfd
= openat (dfd
, "task", O_RDONLY
| O_DIRECTORY
| O_CLOEXEC
);
152 error (EXIT_FAILURE
, errno
, gettext ("cannot open %s/task"), buf
);
153 DIR *dir
= fdopendir (taskfd
);
155 error (EXIT_FAILURE
, errno
, gettext ("cannot prepare reading %s/task"),
159 while ((d
= readdir64 (dir
)) != NULL
)
161 if (! isdigit (d
->d_name
[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'"),
171 if (ptrace (PTRACE_ATTACH
, tid
, NULL
, NULL
) != 0)
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
)
179 error (EXIT_FAILURE
, errno
, gettext ("cannot attach to process %lu"),
183 wait_for_ptrace_stop (tid
);
185 struct thread_list
*newp
= alloca (sizeof (*newp
));
187 newp
->next
= thread_list
;
193 int status
= get_process_info (dfd
, pid
);
195 assert (thread_list
!= NULL
);
198 ptrace (PTRACE_DETACH
, thread_list
->tid
, NULL
, NULL
);
199 thread_list
= thread_list
->next
;
201 while (thread_list
!= NULL
);
209 /* Wait for PID to enter ptrace-stop state after being attached. */
211 wait_for_ptrace_stop (long int pid
)
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
))
219 /* The STOP signal should not be delivered to the tracee. */
220 if (WSTOPSIG (status
) == SIGSTOP
)
222 if (ptrace (PTRACE_CONT
, pid
, NULL
,
223 (void *) (uintptr_t) WSTOPSIG (status
)))
224 /* The only possible error is that the process died. */
230 /* Handle program arguments. */
232 parse_opt (int key
, char *arg
, struct argp_state
*state
)
237 return ARGP_ERR_UNKNOWN
;
243 /* Print bug-reporting information in the help message. */
245 more_help (int key
, const char *text
, void *input
)
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)
260 return (char *) text
;
263 /* Print the version information. */
265 print_version (FILE *stream
, struct argp_state
*state
)
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\
273 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
284 get_process_info (int dfd
, long int pid
)
286 memfd
= openat (dfd
, "mem", O_RDONLY
);
290 int fd
= openat (dfd
, "exe", O_RDONLY
);
294 error (0, errno
, gettext ("cannot get information about process %lu"),
299 char e_ident
[EI_NIDENT
];
300 if (read (fd
, e_ident
, EI_NIDENT
) != EI_NIDENT
)
305 if (memcmp (e_ident
, ELFMAG
, SELFMAG
) != 0)
307 error (0, 0, gettext ("process %lu is no ELF program"), pid
);
311 fd
= openat (dfd
, "auxv", O_RDONLY
);
315 size_t auxv_size
= 0;
320 auxv
= xrealloc (auxv
, auxv_size
);
322 ssize_t n
= pread (fd
, auxv
, auxv_size
, 0);
335 if (e_ident
[EI_CLASS
] == ELFCLASS32
)
336 retval
= find_maps32 (pid
, auxv
, auxv_size
);
338 retval
= find_maps64 (pid
, auxv
, auxv_size
);