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.
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>
41 /* Global variables. */
42 extern char *program_invocation_short_name
;
43 #define PACKAGE _libc_intl_domainname
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);
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
;
55 /* Bug report address. */
56 const char *argp_program_bug_address
= N_("\
57 For bug reporting instructions, please see:\n\
58 <http://www.gnu.org/software/libc/bugs.html>.\n");
60 /* Definitions of arguments for argp functions. */
61 static const struct argp_option options
[] =
63 { NULL
, 0, NULL
, 0, NULL
}
66 /* Short description of program. */
67 static const char doc
[] = N_("\
68 List dynamic shared objects loaded into process.");
70 /* Strings for arguments in help texts. */
71 static const char args_doc
[] = N_("PID");
73 /* Prototype for option handler. */
74 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
76 /* Data structure to communicate with argp functions. */
77 static struct argp argp
=
79 options
, parse_opt
, args_doc
, doc
, NULL
, NULL
, NULL
82 // File descriptor of /proc/*/mem file.
85 /* Name of the executable */
88 /* Local functions. */
89 static int get_process_info (int dfd
, long int pid
);
93 main (int argc
, char *argv
[])
95 /* Parse and process arguments. */
97 argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
);
99 if (remaining
!= argc
- 1)
102 gettext ("Exactly one parameter with process ID required.\n"));
103 argp_help (&argp
, stderr
, ARGP_HELP_SEE
, program_invocation_short_name
);
107 assert (sizeof (pid_t
) == sizeof (int)
108 || sizeof (pid_t
) == sizeof (long int));
111 long int pid
= strtol (argv
[remaining
], &endp
, 10);
112 if (pid
< 0 || (pid
== ULONG_MAX
&& errno
== ERANGE
) || *endp
!= '\0'
113 || (sizeof (pid_t
) < sizeof (pid
) && pid
> INT_MAX
))
114 error (EXIT_FAILURE
, 0, gettext ("invalid process ID '%s'"),
117 /* Determine the program name. */
118 char buf
[7 + 3 * sizeof (pid
)];
119 snprintf (buf
, sizeof (buf
), "/proc/%lu", pid
);
120 int dfd
= open (buf
, O_RDONLY
| O_DIRECTORY
);
122 error (EXIT_FAILURE
, errno
, gettext ("cannot open %s"), buf
);
124 size_t exesize
= 1024;
128 exe
= alloca (exesize
);
130 while ((nexe
= readlinkat (dfd
, "exe", exe
, exesize
)) == exesize
)
131 extend_alloca (exe
, exesize
, 2 * exesize
);
133 exe
= (char *) "<program name undetermined>";
137 /* Stop all threads since otherwise the list of loaded modules might
138 change while we are reading it. */
142 struct thread_list
*next
;
143 } *thread_list
= NULL
;
145 int taskfd
= openat (dfd
, "task", O_RDONLY
| O_DIRECTORY
| O_CLOEXEC
);
147 error (EXIT_FAILURE
, errno
, gettext ("cannot open %s/task"), buf
);
148 DIR *dir
= fdopendir (taskfd
);
150 error (EXIT_FAILURE
, errno
, gettext ("cannot prepare reading %s/task"),
154 while ((d
= readdir64 (dir
)) != NULL
)
156 if (! isdigit (d
->d_name
[0]))
160 long int tid
= strtol (d
->d_name
, &endp
, 10);
161 if (tid
< 0 || (tid
== ULONG_MAX
&& errno
== ERANGE
) || *endp
!= '\0'
162 || (sizeof (pid_t
) < sizeof (pid
) && tid
> INT_MAX
))
163 error (EXIT_FAILURE
, 0, gettext ("invalid thread ID '%s'"),
166 if (ptrace (PTRACE_ATTACH
, tid
, NULL
, NULL
) != 0)
168 /* There might be a race between reading the directory and
169 threads terminating. Ignore errors attaching to unknown
170 threads unless this is the main thread. */
171 if (errno
== ESRCH
&& tid
!= pid
)
174 error (EXIT_FAILURE
, errno
, gettext ("cannot attach to process %lu"),
178 struct thread_list
*newp
= alloca (sizeof (*newp
));
180 newp
->next
= thread_list
;
186 int status
= get_process_info (dfd
, pid
);
188 assert (thread_list
!= NULL
);
191 ptrace (PTRACE_DETACH
, thread_list
->tid
, NULL
, NULL
);
192 thread_list
= thread_list
->next
;
194 while (thread_list
!= NULL
);
202 /* Handle program arguments. */
204 parse_opt (int key
, char *arg
, struct argp_state
*state
)
209 return ARGP_ERR_UNKNOWN
;
215 /* Print the version information. */
217 print_version (FILE *stream
, struct argp_state
*state
)
219 fprintf (stream
, "pldd (GNU %s) %s\n", PACKAGE
, VERSION
);
220 fprintf (stream
, gettext ("\
221 Copyright (C) %s Free Software Foundation, Inc.\n\
222 This is free software; see the source for copying conditions. There is NO\n\
223 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
225 fprintf (stream
, gettext ("Written by %s.\n"), "Ulrich Drepper");
236 get_process_info (int dfd
, long int pid
)
238 memfd
= openat (dfd
, "mem", O_RDONLY
);
242 int fd
= openat (dfd
, "exe", O_RDONLY
);
246 error (0, errno
, gettext ("cannot get information about process %lu"),
251 char e_ident
[EI_NIDENT
];
252 if (read (fd
, e_ident
, EI_NIDENT
) != EI_NIDENT
)
257 if (memcmp (e_ident
, ELFMAG
, SELFMAG
) != 0)
259 error (0, 0, gettext ("process %lu is no ELF program"), pid
);
263 fd
= openat (dfd
, "auxv", O_RDONLY
);
267 size_t auxv_size
= 0;
272 auxv
= xrealloc (auxv
, auxv_size
);
274 ssize_t n
= pread (fd
, auxv
, auxv_size
, 0);
287 if (e_ident
[EI_CLASS
] == ELFCLASS32
)
288 retval
= find_maps32 (pid
, auxv
, auxv_size
);
290 retval
= find_maps64 (pid
, auxv
, auxv_size
);