2018-03-02 06:23:31 +08:00
|
|
|
#!/usr/bin/env bash
|
2003-04-18 04:33:09 +08:00
|
|
|
|
2024-01-12 23:30:44 +08:00
|
|
|
# Copyright (C) 2003-2024 Free Software Foundation, Inc.
|
2003-04-18 04:33:09 +08:00
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2007-08-24 02:19:51 +08:00
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
2003-04-18 04:33:09 +08:00
|
|
|
# (at your option) any later version.
|
2007-08-24 02:19:51 +08:00
|
|
|
#
|
2003-04-18 04:33:09 +08:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2007-08-24 02:19:51 +08:00
|
|
|
#
|
2003-04-18 04:33:09 +08:00
|
|
|
# You should have received a copy of the GNU General Public License
|
2007-08-24 02:19:51 +08:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2003-04-18 04:33:09 +08:00
|
|
|
|
|
|
|
#
|
|
|
|
# Script to generate a core file of a running program.
|
|
|
|
# It starts up gdb, attaches to the given PID and invokes the gcore command.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Need to check for -o option, but set default basename to "core".
|
2018-07-27 12:52:23 +08:00
|
|
|
prefix=core
|
2003-04-18 04:33:09 +08:00
|
|
|
|
2017-12-04 16:17:14 +08:00
|
|
|
# When the -a option is present, this may hold additional commands
|
|
|
|
# to ensure gdb dumps all mappings (OS dependent).
|
|
|
|
dump_all_cmds=()
|
|
|
|
|
2024-02-02 18:13:47 +08:00
|
|
|
data_directory_opt=()
|
|
|
|
|
|
|
|
while getopts :ao:d: opt; do
|
2018-03-02 06:23:31 +08:00
|
|
|
case "$opt" in
|
2017-12-04 16:17:14 +08:00
|
|
|
a)
|
2018-03-02 06:23:31 +08:00
|
|
|
case "$OSTYPE" in
|
2017-12-04 16:17:14 +08:00
|
|
|
linux*)
|
|
|
|
dump_all_cmds=("-ex" "set use-coredump-filter off")
|
|
|
|
dump_all_cmds+=("-ex" "set dump-excluded-mappings on")
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
o)
|
2018-07-27 12:52:23 +08:00
|
|
|
prefix=$OPTARG
|
2017-12-04 16:17:14 +08:00
|
|
|
;;
|
2024-02-02 18:13:47 +08:00
|
|
|
d)
|
|
|
|
data_directory_opt=("--data-directory" "$OPTARG")
|
|
|
|
;;
|
2017-12-04 16:17:14 +08:00
|
|
|
*)
|
2024-02-02 18:13:47 +08:00
|
|
|
echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] [-d data-directory] pid1 [pid2...pidN]"
|
2017-12-04 16:17:14 +08:00
|
|
|
exit 2
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
|
|
|
if [ "$#" -eq "0" ]
|
2003-04-18 04:33:09 +08:00
|
|
|
then
|
2024-02-02 18:13:47 +08:00
|
|
|
echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] [-d data-directory] pid1 [pid2...pidN]"
|
2017-12-04 16:17:14 +08:00
|
|
|
exit 2
|
2003-04-18 04:33:09 +08:00
|
|
|
fi
|
|
|
|
|
2013-10-16 23:08:11 +08:00
|
|
|
# Attempt to fetch the absolute path to the gcore script that was
|
|
|
|
# called.
|
|
|
|
binary_path=`dirname "$0"`
|
|
|
|
|
|
|
|
if test "x$binary_path" = x. ; then
|
|
|
|
# We got "." back as a path. This means the user executed
|
|
|
|
# the gcore script locally (i.e. ./gcore) or called the
|
|
|
|
# script via a shell interpreter (i.e. sh gcore).
|
|
|
|
binary_basename=`basename "$0"`
|
|
|
|
|
|
|
|
# If the gcore script was called like "sh gcore" and the script
|
|
|
|
# lives in the current directory, "which" will not give us "gcore".
|
|
|
|
# So first we check if the script is in the current directory
|
|
|
|
# before using the output of "which".
|
|
|
|
if test -f "$binary_basename" ; then
|
|
|
|
# We have a local gcore script in ".". This covers the case of
|
|
|
|
# doing "./gcore" or "sh gcore".
|
|
|
|
binary_path="."
|
|
|
|
else
|
|
|
|
# The gcore script was not found in ".", which means the script
|
|
|
|
# was called from somewhere else in $PATH by "sh gcore".
|
|
|
|
# Extract the correct path now.
|
|
|
|
binary_path_from_env=`which "$0"`
|
|
|
|
binary_path=`dirname "$binary_path_from_env"`
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Check if the GDB binary is in the expected path. If not, just
|
|
|
|
# quit with a message.
|
2018-03-02 06:23:31 +08:00
|
|
|
if [ ! -f "$binary_path/@GDB_TRANSFORM_NAME@" ]; then
|
2013-10-16 23:08:11 +08:00
|
|
|
echo "gcore: GDB binary (${binary_path}/@GDB_TRANSFORM_NAME@) not found"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2003-04-18 04:33:09 +08:00
|
|
|
# Initialise return code.
|
|
|
|
rc=0
|
|
|
|
|
|
|
|
# Loop through pids
|
2018-03-02 06:23:31 +08:00
|
|
|
for pid in "$@"
|
2003-04-18 04:33:09 +08:00
|
|
|
do
|
2010-07-24 05:10:55 +08:00
|
|
|
# `</dev/null' to avoid touching interactive terminal if it is
|
|
|
|
# available but not accessible as GDB would get stopped on SIGTTIN.
|
2019-02-23 23:05:19 +08:00
|
|
|
"$binary_path/@GDB_TRANSFORM_NAME@" </dev/null \
|
2024-02-02 18:13:47 +08:00
|
|
|
"${data_directory_opt[@]}" \
|
2022-03-03 09:00:59 +08:00
|
|
|
--nx --batch --readnever -iex 'set debuginfod enabled off' \
|
2010-07-24 05:10:55 +08:00
|
|
|
-ex "set pagination off" -ex "set height 0" -ex "set width 0" \
|
2017-12-04 16:17:14 +08:00
|
|
|
"${dump_all_cmds[@]}" \
|
2018-07-27 12:52:23 +08:00
|
|
|
-ex "attach $pid" -ex "gcore $prefix.$pid" -ex detach -ex quit
|
2005-10-03 07:01:01 +08:00
|
|
|
|
2018-07-27 12:52:23 +08:00
|
|
|
if [ -r "$prefix.$pid" ] ; then
|
2003-04-18 04:33:09 +08:00
|
|
|
rc=0
|
|
|
|
else
|
2018-07-27 12:52:23 +08:00
|
|
|
echo "@GCORE_TRANSFORM_NAME@: failed to create $prefix.$pid"
|
2003-04-18 04:33:09 +08:00
|
|
|
rc=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
done
|
|
|
|
|
|
|
|
exit $rc
|