3 # Copyright (C) 2003-2015 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 # Script to generate a core file of a running program.
20 # It starts up gdb, attaches to the given PID and invokes the gcore command.
25 echo "usage: @GCORE_TRANSFORM_NAME@ [-o filename] pid"
29 # Need to check for -o option, but set default basename to "core".
36 # Not enough arguments.
37 echo "usage: @GCORE_TRANSFORM_NAME@ [-o filename] pid"
42 # Shift over to start of pid list
46 # Attempt to fetch the absolute path to the gcore script that was
48 binary_path=`dirname "$0"`
50 if test "x$binary_path" = x. ; then
51 # We got "." back as a path. This means the user executed
52 # the gcore script locally (i.e. ./gcore) or called the
53 # script via a shell interpreter (i.e. sh gcore).
54 binary_basename=`basename "$0"`
56 # If the gcore script was called like "sh gcore" and the script
57 # lives in the current directory, "which" will not give us "gcore".
58 # So first we check if the script is in the current directory
59 # before using the output of "which".
60 if test -f "$binary_basename" ; then
61 # We have a local gcore script in ".". This covers the case of
62 # doing "./gcore" or "sh gcore".
65 # The gcore script was not found in ".", which means the script
66 # was called from somewhere else in $PATH by "sh gcore".
67 # Extract the correct path now.
68 binary_path_from_env=`which "$0"`
69 binary_path=`dirname "$binary_path_from_env"`
73 # Check if the GDB binary is in the expected path. If not, just
74 # quit with a message.
75 if [ ! -f "$binary_path"/@GDB_TRANSFORM_NAME@ ]; then
76 echo "gcore: GDB binary (${binary_path}/@GDB_TRANSFORM_NAME@) not found"
80 # Initialise return code.
86 # `</dev/null' to avoid touching interactive terminal if it is
87 # available but not accessible as GDB would get stopped on SIGTTIN.
88 $binary_path/@GDB_TRANSFORM_NAME@ </dev/null --nx --batch \
89 -ex "set pagination off" -ex "set height 0" -ex "set width 0" \
90 -ex "attach $pid" -ex "gcore $name.$pid" -ex detach -ex quit
92 if [ -r $name.$pid ] ; then
95 echo "@GCORE_TRANSFORM_NAME@: failed to create $name.$pid"