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
36 #include <sys/ptrace.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.
84 /* Name of the executable */
87 /* Local functions. */
88 static int get_process_info (int dfd
, long int pid
);
92 main (int argc
, char *argv
[])
94 /* Parse and process arguments. */
96 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
98 if (remaining
!= argc
- 1)
101 gettext ("Exactly one parameter with process ID required.\n"));
102 argp_help (&argp
, stderr
, ARGP_HELP_SEE
, program_invocation_short_name
);
106 assert (sizeof (pid_t
) == sizeof (int)
107 || sizeof (pid_t
) == sizeof (long int));
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'"),
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
);
121 error (EXIT_FAILURE
, errno
, gettext ("cannot open %s"), buf
);
123 size_t exesize
= 1024;
127 exe
= alloca (exesize
);
129 while ((nexe
= readlinkat (dfd
, "exe", exe
, exesize
)) == exesize
)
130 extend_alloca (exe
, exesize
, 2 * exesize
);
132 exe
= (char *) "<program name undetermined>";
136 /* Stop all threads since otherwise the list of loaded modules might
137 change while we are reading it. */
141 struct thread_list
*next
;
142 } *thread_list
= NULL
;
144 int taskfd
= openat (dfd
, "task", O_RDONLY
| O_DIRECTORY
| O_CLOEXEC
);
146 error (EXIT_FAILURE
, errno
, gettext ("cannot open %s/task"), buf
);
147 DIR *dir
= fdopendir (taskfd
);
149 error (EXIT_FAILURE
, errno
, gettext ("cannot prepare reading %s/task"),
153 while ((d
= readdir64 (dir
)) != NULL
)
155 if (! isdigit (d
->d_name
[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'"),
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
)
173 error (EXIT_FAILURE
, errno
, gettext ("cannot attach to process %lu"),
177 struct thread_list
*newp
= alloca (sizeof (*newp
));
179 newp
->next
= thread_list
;
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
);
201 /* Handle program arguments. */
203 parse_opt (int key
, char *arg
, struct argp_state
*state
)
208 return ARGP_ERR_UNKNOWN
;
214 /* Print the version information. */
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\
224 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
235 get_process_info (int dfd
, long int pid
)
237 memfd
= openat (dfd
, "mem", O_RDONLY
);
241 int fd
= openat (dfd
, "exe", O_RDONLY
);
245 error (0, errno
, gettext ("cannot get information about process %lu"),
250 char e_ident
[EI_NIDENT
];
251 if (read (fd
, e_ident
, EI_NIDENT
) != EI_NIDENT
)
256 if (memcmp (e_ident
, ELFMAG
, SELFMAG
) != 0)
258 error (0, 0, gettext ("process %lu is no ELF program"), pid
);
262 fd
= openat (dfd
, "auxv", O_RDONLY
);
266 size_t auxv_size
= 0;
271 auxv
= xrealloc (auxv
, auxv_size
);
273 ssize_t n
= pread (fd
, auxv
, auxv_size
, 0);
286 if (e_ident
[EI_CLASS
] == ELFCLASS32
)
287 retval
= find_maps32 (pid
, auxv
, auxv_size
);
289 retval
= find_maps64 (pid
, auxv
, auxv_size
);