| 1 |
#!/bin/sh |
| 2 |
|
| 3 |
# |
| 4 |
# SPDX-FileCopyrightText: 2014-2021 Milian Wolff <[email protected]> |
| 5 |
# |
| 6 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 7 |
# |
| 8 |
|
| 9 |
usage() { |
| 10 |
echo "Usage: $0 [--debug|-d] [--use-inject] [--record-only] DEBUGGEE [ARGUMENT]..." |
| 11 |
echo "or: $0 [--debug|-d] -p PID" |
| 12 |
echo "or: $0 -a FILE" |
| 13 |
echo |
| 14 |
echo "A heap memory usage profiler. It uses LD_PRELOAD to track all" |
| 15 |
echo "calls to the core memory allocation functions and logs these" |
| 16 |
echo "occurrences. Additionally, backtraces are obtained and logged." |
| 17 |
echo "Combined this can give interesting answers to questions such as:" |
| 18 |
echo |
| 19 |
echo " * How much heap memory is my application using?" |
| 20 |
echo " * Where is heap memory being allocated, and how often?" |
| 21 |
echo " * How much space are heap individual allocations requesting?" |
| 22 |
echo |
| 23 |
echo "To evaluate the generated heaptrack data, use heaptrack_print or heaptrack_gui." |
| 24 |
echo |
| 25 |
echo "Mandatory arguments to heaptrack:" |
| 26 |
echo " DEBUGGEE The name or path to the application that should" |
| 27 |
echo " be run with heaptrack analyzation enabled." |
| 28 |
echo |
| 29 |
echo "Alternatively, to attach to a running process:" |
| 30 |
echo " -p, --pid PID The process ID of a running process into which" |
| 31 |
echo " heaptrack will be injected. This only works with" |
| 32 |
echo " applications that already link against libdl." |
| 33 |
echo " WARNING: Runtime-attaching heaptrack is UNSTABLE and can lead to CRASHES" |
| 34 |
echo " in your application, especially after you detach heaptrack again." |
| 35 |
echo " You are hereby warned, use it at your own risk!" |
| 36 |
echo |
| 37 |
echo "Optional arguments to heaptrack:" |
| 38 |
echo " -r, --raw Only record raw data, do not interpret it." |
| 39 |
echo " -d, --debug Run the debuggee in GDB and heaptrack." |
| 40 |
echo " --use-inject Use the same heaptrack_inject symbol interception mechanism instead of relying on" |
| 41 |
echo " the dynamic linker and LD_PRELOAD. This is an experimental flag for now." |
| 42 |
echo " --asan Enables running heaptrack on binaries built with gcc's address sanitizer enabled." |
| 43 |
echo " Implies --use-inject." |
| 44 |
echo " --record-only Only record and interpret the data, do not attempt to analyze it." |
| 45 |
echo " ARGUMENT Any number of arguments that will be passed verbatim" |
| 46 |
echo " to the debuggee." |
| 47 |
echo " -h, --help Show this help message and exit." |
| 48 |
echo " -v, --version Displays version information." |
| 49 |
echo " -q, --quiet Only print error messages." |
| 50 |
echo " -o, --output Specifies the data-file for the captured data." |
| 51 |
echo " %h in the file name string is replaced with the hostname of the system." |
| 52 |
echo " %p in the file name string is replaced with the pid of the application being profiled." |
| 53 |
echo " Parent directories will be created if output files are under non-existing directories." |
| 54 |
echo " e.g.," |
| 55 |
echo " ./%h/%p/outdat will be translated into ./<hostname>/<pid>/outdat." |
| 56 |
echo " The directory ./<hostname>/<pid> will be created if it doesn't exist." |
| 57 |
echo |
| 58 |
echo "Alternatively, to interpret a raw recorded heaptrack data file:" |
| 59 |
echo " -i, --interpret FILE Convert a raw heaptrack data file with heaptrack_interpret." |
| 60 |
echo " Any options passed after --analyze will be passed along." |
| 61 |
echo |
| 62 |
echo "Alternatively, to analyze a recorded heaptrack data file:" |
| 63 |
echo " -a, --analyze FILE Open the heaptrack data file in heaptrack_gui, if available," |
| 64 |
echo " or fallback to heaptrack_print otherwise." |
| 65 |
echo " Any options passed after --analyze will be passed along." |
| 66 |
echo |
| 67 |
exit 0 |
| 68 |
} |
| 69 |
|
| 70 |
debug= |
| 71 |
pid= |
| 72 |
client= |
| 73 |
use_inject_lib= |
| 74 |
write_raw_data= |
| 75 |
record_only= |
| 76 |
asan= |
| 77 |
asan_ld_preload= |
| 78 |
quiet= |
| 79 |
output= |
| 80 |
|
| 81 |
# path to current heaptrack.sh executable |
| 82 |
SCRIPT_PATH=$(readlink -f "$0") |
| 83 |
SCRIPT_DIR=$(dirname "$SCRIPT_PATH") |
| 84 |
EXE_PATH=$(readlink -f "$SCRIPT_DIR") |
| 85 |
|
| 86 |
# find preload library and interpreter executable using relative paths |
| 87 |
LIB_REL_PATH="@LIB_REL_PATH@" |
| 88 |
LIBEXEC_REL_PATH="@LIBEXEC_REL_PATH@" |
| 89 |
|
| 90 |
INTERPRETER="$EXE_PATH/$LIBEXEC_REL_PATH/heaptrack_interpret" |
| 91 |
if [ -z "$write_raw_data" ] && [ ! -f "$INTERPRETER" ]; then |
| 92 |
echo "Could not find heaptrack interpreter executable: $INTERPRETER" |
| 93 |
exit 1 |
| 94 |
fi |
| 95 |
INTERPRETER=$(readlink -f "$INTERPRETER") |
| 96 |
|
| 97 |
GZ_COMPRESSOR="gzip -c" |
| 98 |
GZ_UNCOMPRESSOR="gzip -dc" |
| 99 |
|
| 100 |
ZSTD_COMPRESSOR="zstd -c" |
| 101 |
ZSTD_UNCOMPRESSOR="zstd -dc" |
| 102 |
|
| 103 |
COMPRESSOR="$GZ_COMPRESSOR" |
| 104 |
UNCOMPRESSOR="$GZ_UNCOMPRESSOR" |
| 105 |
output_suffix="gz" |
| 106 |
if [ "@ZSTD_FOUND@" = "TRUE" ] && [ ! -z "$(command -v zstd 2> /dev/null)" ]; then |
| 107 |
output_suffix="zst" |
| 108 |
COMPRESSOR="$ZSTD_COMPRESSOR" |
| 109 |
UNCOMPRESSOR="$ZSTD_UNCOMPRESSOR" |
| 110 |
fi |
| 111 |
|
| 112 |
interpretRawHeaptrackDataFile() { |
| 113 |
input="$1" |
| 114 |
shift 1 |
| 115 |
|
| 116 |
if [ ! -f "$input" ]; then |
| 117 |
echo "raw file "$input" does not exist" |
| 118 |
exit 1 |
| 119 |
fi |
| 120 |
|
| 121 |
output=$(echo $input | sed -e 's/.zst$//' -e 's/.gz$//' -e 's/.raw$//') |
| 122 |
output="$output.$output_suffix" |
| 123 |
echo "writing interpreted data to $output..." |
| 124 |
|
| 125 |
case "$input" in |
| 126 |
*.gz) |
| 127 |
$GZ_UNCOMPRESSOR < "$input" | "$INTERPRETER" "$@" | $COMPRESSOR > "$output" |
| 128 |
;; |
| 129 |
*.zst) |
| 130 |
$ZSTD_UNCOMPRESSOR < "$input" | "$INTERPRETER" "$@" | $COMPRESSOR > "$output" |
| 131 |
;; |
| 132 |
*) |
| 133 |
"$INTERPRETER" "$@" | $COMPRESSOR > "$output" |
| 134 |
;; |
| 135 |
esac |
| 136 |
|
| 137 |
echo |
| 138 |
echo "Interpretation finished, you can now analyze the data:" |
| 139 |
echo |
| 140 |
echo " heaptrack --analyze \"$output\"" |
| 141 |
} |
| 142 |
|
| 143 |
openHeaptrackDataFiles() { |
| 144 |
if [ -x "$EXE_PATH/heaptrack_gui" ]; then |
| 145 |
"$EXE_PATH/heaptrack_gui" "$@" |
| 146 |
else |
| 147 |
"$EXE_PATH/heaptrack_print" "$@" |
| 148 |
fi |
| 149 |
} |
| 150 |
|
| 151 |
ORIG_CMDLINE=$@ |
| 152 |
|
| 153 |
while true; do |
| 154 |
case "$1" in |
| 155 |
"-d" | "--debug") |
| 156 |
if [ -z "$(command -v gdb 2> /dev/null)" ]; then |
| 157 |
echo "GDB is not installed, cannot debug heaptrack." |
| 158 |
exit 1 |
| 159 |
fi |
| 160 |
debug=1 |
| 161 |
shift 1 |
| 162 |
;; |
| 163 |
"--use-inject") |
| 164 |
use_inject_lib=1 |
| 165 |
shift 1 |
| 166 |
;; |
| 167 |
"-r" | "--raw") |
| 168 |
write_raw_data=1 |
| 169 |
shift 1 |
| 170 |
;; |
| 171 |
"--asan") |
| 172 |
asan=1 |
| 173 |
use_inject_lib=1 |
| 174 |
shift 1 |
| 175 |
;; |
| 176 |
"--record-only") |
| 177 |
record_only=1 |
| 178 |
shift 1 |
| 179 |
;; |
| 180 |
"-h" | "--help") |
| 181 |
usage |
| 182 |
exit 0 |
| 183 |
;; |
| 184 |
"-o" | "--output" | "--output-file") |
| 185 |
if [ -z "$2" ]; then |
| 186 |
echo "Missing output argument." |
| 187 |
exit 1 |
| 188 |
fi |
| 189 |
output=$(echo $2 | sed "s/%h/$(hostname)/g" | sed "s/%p/$$/g") |
| 190 |
if [ -d "$output" ]; then |
| 191 |
echo "Please specify a file-name or a full path-name for output." |
| 192 |
exit 1 |
| 193 |
fi |
| 194 |
if [ ! -d $(dirname $output) ]; then |
| 195 |
mkdir -p $(dirname $output) |
| 196 |
fi |
| 197 |
output=$(readlink -f $output) |
| 198 |
shift 2 |
| 199 |
;; |
| 200 |
"-p" | "--pid") |
| 201 |
if [ -z "$(command -v gdb 2> /dev/null)" ]; then |
| 202 |
echo "GDB is not installed, cannot attach to running process." |
| 203 |
exit 1 |
| 204 |
fi |
| 205 |
if [ -f "/proc/sys/kernel/yama/ptrace_scope" ] && [ "$(cat "/proc/sys/kernel/yama/ptrace_scope")" -gt "0" ]; then |
| 206 |
echo "Cannot runtime-attach, you need to set /proc/sys/kernel/yama/ptrace_scope to 0" |
| 207 |
exit 1 |
| 208 |
fi |
| 209 |
pid=$2 |
| 210 |
if [ -z "$pid" ]; then |
| 211 |
echo "Missing PID argument." |
| 212 |
exit 1 |
| 213 |
fi |
| 214 |
case $(uname) in |
| 215 |
Linux*) |
| 216 |
client=$(cat "/proc/$pid/comm") |
| 217 |
;; |
| 218 |
FreeBSD*) |
| 219 |
client=$(awk '{print $1}' < "/proc/$pid/cmdline") |
| 220 |
;; |
| 221 |
esac |
| 222 |
if [ -z "$client" ]; then |
| 223 |
echo "Cannot attach to unknown process with PID $pid." |
| 224 |
exit 1 |
| 225 |
fi |
| 226 |
shift 2 |
| 227 |
echo $@ |
| 228 |
if [ ! -z "$@" ]; then |
| 229 |
echo "You cannot specify a debuggee and a pid at the same time." |
| 230 |
exit 1 |
| 231 |
fi |
| 232 |
break |
| 233 |
;; |
| 234 |
"-q" | "--quiet") |
| 235 |
quiet=1 |
| 236 |
shift |
| 237 |
;; |
| 238 |
"-v" | "--version") |
| 239 |
echo "heaptrack @HEAPTRACK_VERSION_MAJOR@.@HEAPTRACK_VERSION_MINOR@.@HEAPTRACK_VERSION_PATCH@" |
| 240 |
exit 0 |
| 241 |
;; |
| 242 |
"-i" | "--interpret") |
| 243 |
shift 1 |
| 244 |
interpretRawHeaptrackDataFile "$@" |
| 245 |
exit |
| 246 |
;; |
| 247 |
"-a" | "--analyze") |
| 248 |
shift 1 |
| 249 |
openHeaptrackDataFiles "$@" |
| 250 |
exit |
| 251 |
;; |
| 252 |
*) |
| 253 |
if [ "$1" = "--" ]; then |
| 254 |
shift 1 |
| 255 |
fi |
| 256 |
if [ ! -x "$(command -v "$1" 2> /dev/null)" ]; then |
| 257 |
if [ -z "$1" ] && [ -x "$EXE_PATH/heaptrack_gui" ]; then |
| 258 |
"$EXE_PATH/heaptrack_gui" |
| 259 |
exit |
| 260 |
fi |
| 261 |
if [ -f "$1" ] && echo "$1" | grep -q "heaptrack."; then |
| 262 |
openHeaptrackDataFiles "$ORIG_CMDLINE" |
| 263 |
exit |
| 264 |
fi |
| 265 |
|
| 266 |
if [ ! -e "$1" ]; then |
| 267 |
echo "Error: Debuggee \"$1\" was not found." |
| 268 |
else |
| 269 |
echo "Error: Debuggee \"$1\" is not an executable." |
| 270 |
fi |
| 271 |
|
| 272 |
echo |
| 273 |
echo "Usage: $0 [--debug|-d] [--help|-h] DEBUGGEE [ARGS...]" |
| 274 |
exit 1 |
| 275 |
fi |
| 276 |
client="$1" |
| 277 |
shift 1 |
| 278 |
break |
| 279 |
;; |
| 280 |
esac |
| 281 |
done |
| 282 |
|
| 283 |
# put output into current pwd |
| 284 |
if [ -z "$output" ]; then |
| 285 |
output=$(pwd)/heaptrack.$(basename "$client").$$ |
| 286 |
fi |
| 287 |
|
| 288 |
ENVCHECKER="$EXE_PATH/$LIBEXEC_REL_PATH/heaptrack_env" |
| 289 |
if [ ! -f "$ENVCHECKER" ]; then |
| 290 |
echo "Could not find heaptrack_env: $ENVCHECKER" |
| 291 |
exit 1 |
| 292 |
fi |
| 293 |
ENVCHECKER=$(readlink -f "$ENVCHECKER") |
| 294 |
|
| 295 |
if [ -z "$use_inject_lib" ]; then |
| 296 |
LIBHEAPTRACK_PRELOAD="$EXE_PATH/$LIB_REL_PATH/libheaptrack_preload.so" |
| 297 |
else |
| 298 |
LIBHEAPTRACK_PRELOAD="$EXE_PATH/$LIB_REL_PATH/libheaptrack_inject.so" |
| 299 |
fi |
| 300 |
if [ ! -f "$LIBHEAPTRACK_PRELOAD" ]; then |
| 301 |
echo "Could not find heaptrack preload library $LIBHEAPTRACK_PRELOAD" |
| 302 |
exit 1 |
| 303 |
fi |
| 304 |
LIBHEAPTRACK_PRELOAD=$(readlink -f "$LIBHEAPTRACK_PRELOAD") |
| 305 |
|
| 306 |
LIBHEAPTRACK_INJECT="$EXE_PATH/$LIB_REL_PATH/libheaptrack_inject.so" |
| 307 |
if [ ! -f "$LIBHEAPTRACK_INJECT" ]; then |
| 308 |
echo "Could not find heaptrack inject library $LIBHEAPTRACK_INJECT" |
| 309 |
exit 1 |
| 310 |
fi |
| 311 |
LIBHEAPTRACK_INJECT=$(readlink -f "$LIBHEAPTRACK_INJECT") |
| 312 |
|
| 313 |
if [ -n "$asan" ]; then |
| 314 |
# We need to check the actual path to the binary |
| 315 |
bin_path=$(readlink -f /proc/$pid/exe) |
| 316 |
asan_ld_preload=$(ldd $bin_path | grep libasan | sed -e 's/.*=> //;s/ (.*//') |
| 317 |
if [ -z "$asan_ld_preload" ]; then |
| 318 |
echo "Unable to detect libasan when running ldd on the executable $client" |
| 319 |
exit 1 |
| 320 |
fi |
| 321 |
echo "Found ASAN library: $asan_ld_preload" |
| 322 |
asan_ld_preload="$asan_ld_preload:" |
| 323 |
fi |
| 324 |
|
| 325 |
# setup named pipe to read data from |
| 326 |
pipe=/tmp/heaptrack_fifo$$ |
| 327 |
mkfifo $pipe |
| 328 |
|
| 329 |
# if root is profiling a process for non root |
| 330 |
# give profiled process write access to the pipe |
| 331 |
if [ ! -z "$pid" ]; then |
| 332 |
case $(uname) in |
| 333 |
Linux*) |
| 334 |
pid_user=$(stat -c %u "/proc/$pid") |
| 335 |
;; |
| 336 |
FreeBSD*) |
| 337 |
pid_user=$(stat -f %Su "/proc/$pid") |
| 338 |
;; |
| 339 |
esac |
| 340 |
if [ -z "$pid_user" ]; then |
| 341 |
exit 1 |
| 342 |
fi |
| 343 |
chown "$pid_user" "$pipe" || exit 1 |
| 344 |
fi |
| 345 |
|
| 346 |
output_suffix="gz" |
| 347 |
COMPRESSOR="gzip -c" |
| 348 |
UNCOMPRESSOR="gzip -dc" |
| 349 |
|
| 350 |
if [ "@ZSTD_FOUND@" = "TRUE" ] && [ ! -z "$(command -v zstd 2> /dev/null)" ]; then |
| 351 |
output_suffix="zst" |
| 352 |
COMPRESSOR="zstd -c" |
| 353 |
UNCOMPRESSOR="zstd -dc" |
| 354 |
fi |
| 355 |
|
| 356 |
output_no_suffix="$output" |
| 357 |
output_non_raw="$output.$output_suffix" |
| 358 |
|
| 359 |
if [ ! -z "$write_raw_data" ]; then |
| 360 |
output_suffix="raw.$output_suffix" |
| 361 |
fi |
| 362 |
|
| 363 |
# interpret the data and compress the output on the fly |
| 364 |
output="$output.$output_suffix" |
| 365 |
if [ -z "$write_raw_data" ]; then |
| 366 |
"$INTERPRETER" < $pipe | $COMPRESSOR > "$output" & |
| 367 |
else |
| 368 |
$COMPRESSOR < $pipe > "$output" & |
| 369 |
fi |
| 370 |
debuggee=$! |
| 371 |
|
| 372 |
cleanup() { |
| 373 |
if [ ! -z "$pid" ] && [ -d "/proc/$pid" ]; then |
| 374 |
echo "removing heaptrack injection via GDB, this might take some time..." |
| 375 |
gdb --batch-silent -n -iex="set auto-solib-add off" \ |
| 376 |
-iex="set language c" -p $pid \ |
| 377 |
--eval-command="sharedlibrary libheaptrack_inject" \ |
| 378 |
--eval-command="call (void) heaptrack_stop()" \ |
| 379 |
--eval-command="detach" |
| 380 |
# NOTE: we do not call dlclose here, as that has the tendency to trigger |
| 381 |
# crashes in the debuggee. So instead, we keep heaptrack loaded. |
| 382 |
fi |
| 383 |
rm -f "$pipe" |
| 384 |
case $(uname) in |
| 385 |
FreeBSD*) |
| 386 |
rm -f "$pipe.lock" |
| 387 |
;; |
| 388 |
esac |
| 389 |
kill "$debuggee" 2> /dev/null |
| 390 |
|
| 391 |
if [ -z ${quiet} ]; then |
| 392 |
echo "Heaptrack finished! Now run the following to investigate the data:" |
| 393 |
echo |
| 394 |
|
| 395 |
if [ ! -z "$write_raw_data" ]; then |
| 396 |
echo "First, interpret the raw data (possibly specifying the sysroot etc):" |
| 397 |
echo |
| 398 |
echo " heaptrack --interpret \"$output\"" |
| 399 |
echo |
| 400 |
echo "Then, you can analyze it:" |
| 401 |
echo |
| 402 |
fi |
| 403 |
|
| 404 |
echo " heaptrack --analyze \"$output\"" |
| 405 |
fi |
| 406 |
|
| 407 |
if [ -z "$record_only" ] && [ -z "$write_raw_data" ] && [ -x "$EXE_PATH/heaptrack_gui" ]; then |
| 408 |
if [ -z ${quiet} ]; then |
| 409 |
echo "" |
| 410 |
echo "heaptrack_gui detected, automatically opening the file..." |
| 411 |
fi |
| 412 |
"$EXE_PATH/heaptrack_gui" "$output" |
| 413 |
fi |
| 414 |
} |
| 415 |
trap cleanup EXIT |
| 416 |
|
| 417 |
if [ -z ${quiet} ]; then |
| 418 |
echo "heaptrack output will be written to \"$output\"" |
| 419 |
fi |
| 420 |
|
| 421 |
if [ -z "$debug" ] && [ -z "$pid" ]; then |
| 422 |
if [ -z ${quiet} ]; then |
| 423 |
echo "starting application, this might take some time..." |
| 424 |
fi |
| 425 |
LD_PRELOAD="$asan_ld_preload$LIBHEAPTRACK_PRELOAD${LD_PRELOAD:+:$LD_PRELOAD}" DUMP_HEAPTRACK_OUTPUT="$pipe" "$client" "$@" |
| 426 |
EXIT_CODE=$? |
| 427 |
else |
| 428 |
if [ -z "$pid" ]; then |
| 429 |
if [ -z ${quiet} ]; then |
| 430 |
echo "starting application in GDB, this might take some time..." |
| 431 |
fi |
| 432 |
gdb --quiet --eval-command="set environment LD_PRELOAD=$LIBHEAPTRACK_PRELOAD" \ |
| 433 |
--eval-command="set environment DUMP_HEAPTRACK_OUTPUT=$pipe" \ |
| 434 |
--eval-command="set startup-with-shell off" \ |
| 435 |
--eval-command="run" --args "$client" "$@" |
| 436 |
EXIT_CODE=$? |
| 437 |
else |
| 438 |
if [ -z ${quiet} ]; then |
| 439 |
echo "injecting heaptrack into application via GDB, this might take some time..." |
| 440 |
fi |
| 441 |
dlopen=$($ENVCHECKER dlopen "$LIBHEAPTRACK_INJECT") |
| 442 |
if [ -z "$debug" ]; then |
| 443 |
unset DEBUGINFOD_URLS |
| 444 |
gdb --batch-silent -n -iex="set auto-solib-add off" \ |
| 445 |
-iex="set language c" -p $pid \ |
| 446 |
--eval-command="sharedlibrary libc.so" \ |
| 447 |
--eval-command="call (void) $dlopen" \ |
| 448 |
--eval-command="sharedlibrary libheaptrack_inject" \ |
| 449 |
--eval-command="call (void) heaptrack_inject(\"$pipe\")" \ |
| 450 |
--eval-command="detach" |
| 451 |
else |
| 452 |
echo $dlopen |
| 453 |
gdb --quiet -iex="set language c" -p $pid \ |
| 454 |
--eval-command="sharedlibrary libc.so" \ |
| 455 |
--eval-command="print (void*) $dlopen" \ |
| 456 |
--eval-command="sharedlibrary libheaptrack_inject" \ |
| 457 |
--eval-command="call (void) heaptrack_inject(\"$pipe\")" |
| 458 |
fi |
| 459 |
EXIT_CODE=$? |
| 460 |
if [ -z ${quiet} ]; then |
| 461 |
echo "injection finished" |
| 462 |
fi |
| 463 |
fi |
| 464 |
fi |
| 465 |
|
| 466 |
wait $debuggee |
| 467 |
exit $EXIT_CODE |
| 468 |
|
| 469 |
# kate: hl Bash |