forked from HariSekhon/DevOps-Bash-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpldd.sh
executable file
·83 lines (65 loc) · 1.99 KB
/
pldd.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env bash
# vim:ts=4:sts=4:sw=4:et
# args: $$
#
# Author: Hari Sekhon
# Date: 2024-09-16 02:32:28 +0200 (Mon, 16 Sep 2024)
#
# https///github.com/HariSekhon/DevOps-Bash-tools
#
# License: see accompanying Hari Sekhon LICENSE file
#
# If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish
#
# https://www.linkedin.com/in/HariSekhon
#
set -euo pipefail
[ -n "${DEBUG:-}" ] && set -x
srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# shellcheck disable=SC1090,SC1091
. "$srcdir/lib/utils.sh"
# shellcheck disable=SC2034,SC2154
usage_description="
On Linux parses the /proc/<pid>/maps to list all dyanmic so libraries that a program is using
The runtime equivalent of the classic Linux ldd command
Because sometimes the system pldd command gives results like this:
pldd: cannot attach to process 32781: Operation not permitted
"
# used by usage() in lib/utils.sh
# shellcheck disable=SC2034
usage_args="<pid>"
help_usage "$@"
num_args 1 "$@"
pid="$1"
if ! is_linux; then
usage "ERROR: this script only runs on Linux"
fi
max_pid="$(cat /proc/sys/kernel/pid_max)"
if ! is_int "$max_pid"; then
die "ERROR: failed to determine max pid of the system, got: $max_pid"
fi
if ! is_int "$pid" ||
! [ "$pid" -ge 1 ] ||
! [ "$pid" -le "$max_pid" ]; then
die "Error: PID '$pid' is not in the valid range of 1 to $max_pid"
fi
maps_file="/proc/$pid/maps"
if [ ! -f "$maps_file" ]; then
echo "Could not find /proc/$pid/maps"
exit 1
fi
while IFS= read -r line; do
# last field contains the file path
lib_path="$(awk '{print $NF}' <<< "$line")"
# if the path contains in '.so' its a shared library
if [[ "$lib_path" =~ \.so$ ]] ||
[[ "$lib_path" =~ \.so\. ]]; then
realpath=$(readlink -f "$lib_path" 2>/dev/null)
if [ -n "$realpath" ]; then
echo "$realpath"
else
echo "$lib_path"
fi
fi
done < "$maps_file" |
sort -u