1 /* List dynamic shared objects linked into given process.
2 Copyright (C) 2011-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #define _FILE_OFFSET_BITS 64
29 #include <sys/ptrace.h>
31 #include <scratch_buffer.h>
36 /* Global variables. */
37 extern char *program_invocation_short_name
;
38 #define PACKAGE _libc_intl_domainname
40 /* External functions. */
41 #include <programs/xmalloc.h>
43 /* Name and version of program. */
44 static void print_version (FILE *stream
, struct argp_state
*state
);
45 void (*argp_program_version_hook
) (FILE *, struct argp_state
*) = print_version
;
47 /* Function to print some extra text in the help message. */
48 static char *more_help (int key
, const char *text
, void *input
);
50 /* Definitions of arguments for argp functions. */
51 static const struct argp_option options
[] =
53 { NULL
, 0, NULL
, 0, NULL
}
56 /* Short description of program. */
57 static const char doc
[] = N_("\
58 List dynamic shared objects loaded into process.");
60 /* Strings for arguments in help texts. */
61 static const char args_doc
[] = N_("PID");
63 /* Prototype for option handler. */
64 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
66 /* Data structure to communicate with argp functions. */
67 static struct argp argp
=
69 options
, parse_opt
, args_doc
, doc
, NULL
, more_help
, NULL
73 /* Local functions. */
74 static int get_process_info (const char *exe
, int dfd
, long int pid
);
75 static void wait_for_ptrace_stop (long int pid
);
79 main (int argc
, char *argv
[])
81 /* Parse and process arguments. */
83 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
85 if (remaining
!= argc
- 1)
88 gettext ("Exactly one parameter with process ID required.\n"));
89 argp_help (&argp
, stderr
, ARGP_HELP_SEE
, program_invocation_short_name
);
93 _Static_assert (sizeof (pid_t
) == sizeof (int)
94 || sizeof (pid_t
) == sizeof (long int),
95 "sizeof (pid_t) != sizeof (int) or sizeof (long int)");
99 long int pid
= strtol (argv
[remaining
], &endp
, 10);
100 if (pid
< 0 || (pid
== ULONG_MAX
&& errno
== ERANGE
) || *endp
!= '\0'
101 || (sizeof (pid_t
) < sizeof (pid
) && pid
> INT_MAX
))
102 error (EXIT_FAILURE
, 0, gettext ("invalid process ID '%s'"),
105 /* Determine the program name. */
106 char buf
[7 + 3 * sizeof (pid
)];
107 snprintf (buf
, sizeof (buf
), "/proc/%lu", pid
);
108 int dfd
= open (buf
, O_RDONLY
| O_DIRECTORY
);
110 error (EXIT_FAILURE
, errno
, gettext ("cannot open %s"), buf
);
112 /* Name of the executable */
113 struct scratch_buffer exe
;
114 scratch_buffer_init (&exe
);
116 while ((nexe
= readlinkat (dfd
, "exe",
117 exe
.data
, exe
.length
)) == exe
.length
)
119 if (!scratch_buffer_grow (&exe
))
126 /* Default stack allocation is at least 1024. */
127 snprintf (exe
.data
, exe
.length
, "<program name undetermined>");
129 ((char*)exe
.data
)[nexe
] = '\0';
131 /* Stop all threads since otherwise the list of loaded modules might
132 change while we are reading it. */
136 struct thread_list
*next
;
137 } *thread_list
= NULL
;
139 int taskfd
= openat (dfd
, "task", O_RDONLY
| O_DIRECTORY
| O_CLOEXEC
);
141 error (EXIT_FAILURE
, errno
, gettext ("cannot open %s/task"), buf
);
142 DIR *dir
= fdopendir (taskfd
);
144 error (EXIT_FAILURE
, errno
, gettext ("cannot prepare reading %s/task"),
148 while ((d
= readdir (dir
)) != NULL
)
150 if (! isdigit (d
->d_name
[0]))
154 long int tid
= strtol (d
->d_name
, &endp
, 10);
155 if (tid
< 0 || (tid
== ULONG_MAX
&& errno
== ERANGE
) || *endp
!= '\0'
156 || (sizeof (pid_t
) < sizeof (pid
) && tid
> INT_MAX
))
157 error (EXIT_FAILURE
, 0, gettext ("invalid thread ID '%s'"),
160 if (ptrace (PTRACE_ATTACH
, tid
, NULL
, NULL
) != 0)
162 /* There might be a race between reading the directory and
163 threads terminating. Ignore errors attaching to unknown
164 threads unless this is the main thread. */
165 if (errno
== ESRCH
&& tid
!= pid
)
168 error (EXIT_FAILURE
, errno
, gettext ("cannot attach to process %lu"),
172 wait_for_ptrace_stop (tid
);
174 struct thread_list
*newp
= xmalloc (sizeof (*newp
));
176 newp
->next
= thread_list
;
182 if (thread_list
== NULL
)
183 error (EXIT_FAILURE
, 0, gettext ("no valid %s/task entries"), buf
);
185 int status
= get_process_info (exe
.data
, dfd
, pid
);
189 ptrace (PTRACE_DETACH
, thread_list
->tid
, NULL
, NULL
);
190 struct thread_list
*prev
= thread_list
;
191 thread_list
= thread_list
->next
;
194 while (thread_list
!= NULL
);
197 scratch_buffer_free (&exe
);
203 /* Wait for PID to enter ptrace-stop state after being attached. */
205 wait_for_ptrace_stop (long int pid
)
209 /* While waiting for SIGSTOP being delivered to the tracee we have to
210 reinject any other pending signal. Ignore all other errors. */
211 while (waitpid (pid
, &status
, __WALL
) == pid
&& WIFSTOPPED (status
))
213 /* The STOP signal should not be delivered to the tracee. */
214 if (WSTOPSIG (status
) == SIGSTOP
)
216 if (ptrace (PTRACE_CONT
, pid
, NULL
,
217 (void *) (uintptr_t) WSTOPSIG (status
)))
218 /* The only possible error is that the process died. */
224 /* Handle program arguments. */
226 parse_opt (int key
, char *arg
, struct argp_state
*state
)
231 return ARGP_ERR_UNKNOWN
;
237 /* Print bug-reporting information in the help message. */
239 more_help (int key
, const char *text
, void *input
)
244 case ARGP_KEY_HELP_EXTRA
:
245 /* We print some extra information. */
246 if (asprintf (&tp
, gettext ("\
247 For bug reporting instructions, please see:\n\
248 %s.\n"), REPORT_BUGS_TO
) < 0)
254 return (char *) text
;
257 /* Print the version information. */
259 print_version (FILE *stream
, struct argp_state
*state
)
261 fprintf (stream
, "pldd %s%s\n", PKGVERSION
, VERSION
);
262 fprintf (stream
, gettext ("\
263 Copyright (C) %s Free Software Foundation, Inc.\n\
264 This is free software; see the source for copying conditions. There is NO\n\
265 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
267 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
278 get_process_info (const char *exe
, int dfd
, long int pid
)
280 /* File descriptor of /proc/<pid>/mem file. */
281 int memfd
= openat (dfd
, "mem", O_RDONLY
);
285 int fd
= openat (dfd
, "exe", O_RDONLY
);
289 error (0, errno
, gettext ("cannot get information about process %lu"),
294 char e_ident
[EI_NIDENT
];
295 if (read (fd
, e_ident
, EI_NIDENT
) != EI_NIDENT
)
300 if (memcmp (e_ident
, ELFMAG
, SELFMAG
) != 0)
302 error (0, 0, gettext ("process %lu is no ELF program"), pid
);
306 fd
= openat (dfd
, "auxv", O_RDONLY
);
310 size_t auxv_size
= 0;
315 auxv
= xrealloc (auxv
, auxv_size
);
317 ssize_t n
= pread (fd
, auxv
, auxv_size
, 0);
330 if (e_ident
[EI_CLASS
] == ELFCLASS32
)
331 retval
= find_maps32 (exe
, memfd
, pid
, auxv
, auxv_size
);
333 retval
= find_maps64 (exe
, memfd
, pid
, auxv
, auxv_size
);