Commit | Line | Data |
---|---|---|
e1e6f073 | 1 | #!/usr/bin/env bash |
627bf7c1 | 2 | |
42a4f53d | 3 | # Copyright (C) 2003-2019 Free Software Foundation, Inc. |
627bf7c1 EZ |
4 | |
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 | |
50efebf8 | 7 | # the Free Software Foundation; either version 3 of the License, or |
627bf7c1 | 8 | # (at your option) any later version. |
50efebf8 | 9 | # |
627bf7c1 EZ |
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. | |
50efebf8 | 14 | # |
627bf7c1 | 15 | # You should have received a copy of the GNU General Public License |
50efebf8 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
627bf7c1 | 17 | |
627bf7c1 | 18 | # |
627bf7c1 EZ |
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. | |
21 | # | |
22 | ||
627bf7c1 | 23 | # Need to check for -o option, but set default basename to "core". |
129eb0f1 | 24 | prefix=core |
627bf7c1 | 25 | |
cd93789b SL |
26 | # When the -a option is present, this may hold additional commands |
27 | # to ensure gdb dumps all mappings (OS dependent). | |
28 | dump_all_cmds=() | |
29 | ||
30 | while getopts :ao: opt; do | |
e1e6f073 | 31 | case "$opt" in |
cd93789b | 32 | a) |
e1e6f073 | 33 | case "$OSTYPE" in |
cd93789b SL |
34 | linux*) |
35 | dump_all_cmds=("-ex" "set use-coredump-filter off") | |
36 | dump_all_cmds+=("-ex" "set dump-excluded-mappings on") | |
37 | ;; | |
38 | esac | |
39 | ;; | |
40 | o) | |
129eb0f1 | 41 | prefix=$OPTARG |
cd93789b SL |
42 | ;; |
43 | *) | |
129eb0f1 | 44 | echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] pid1 [pid2...pidN]" |
cd93789b SL |
45 | exit 2 |
46 | ;; | |
47 | esac | |
48 | done | |
49 | ||
50 | shift $((OPTIND-1)) | |
51 | ||
52 | if [ "$#" -eq "0" ] | |
627bf7c1 | 53 | then |
129eb0f1 | 54 | echo "usage: @GCORE_TRANSFORM_NAME@ [-a] [-o prefix] pid1 [pid2...pidN]" |
cd93789b | 55 | exit 2 |
627bf7c1 EZ |
56 | fi |
57 | ||
87326c87 LM |
58 | # Attempt to fetch the absolute path to the gcore script that was |
59 | # called. | |
60 | binary_path=`dirname "$0"` | |
61 | ||
62 | if test "x$binary_path" = x. ; then | |
63 | # We got "." back as a path. This means the user executed | |
64 | # the gcore script locally (i.e. ./gcore) or called the | |
65 | # script via a shell interpreter (i.e. sh gcore). | |
66 | binary_basename=`basename "$0"` | |
67 | ||
68 | # If the gcore script was called like "sh gcore" and the script | |
69 | # lives in the current directory, "which" will not give us "gcore". | |
70 | # So first we check if the script is in the current directory | |
71 | # before using the output of "which". | |
72 | if test -f "$binary_basename" ; then | |
73 | # We have a local gcore script in ".". This covers the case of | |
74 | # doing "./gcore" or "sh gcore". | |
75 | binary_path="." | |
76 | else | |
77 | # The gcore script was not found in ".", which means the script | |
78 | # was called from somewhere else in $PATH by "sh gcore". | |
79 | # Extract the correct path now. | |
80 | binary_path_from_env=`which "$0"` | |
81 | binary_path=`dirname "$binary_path_from_env"` | |
82 | fi | |
83 | fi | |
84 | ||
85 | # Check if the GDB binary is in the expected path. If not, just | |
86 | # quit with a message. | |
e1e6f073 | 87 | if [ ! -f "$binary_path/@GDB_TRANSFORM_NAME@" ]; then |
87326c87 LM |
88 | echo "gcore: GDB binary (${binary_path}/@GDB_TRANSFORM_NAME@) not found" |
89 | exit 1 | |
90 | fi | |
91 | ||
627bf7c1 EZ |
92 | # Initialise return code. |
93 | rc=0 | |
94 | ||
95 | # Loop through pids | |
e1e6f073 | 96 | for pid in "$@" |
627bf7c1 | 97 | do |
3a674486 JK |
98 | # `</dev/null' to avoid touching interactive terminal if it is |
99 | # available but not accessible as GDB would get stopped on SIGTTIN. | |
8a6a8513 SDJ |
100 | "$binary_path/@GDB_TRANSFORM_NAME@" </dev/null \ |
101 | --nx --batch --readnever \ | |
3a674486 | 102 | -ex "set pagination off" -ex "set height 0" -ex "set width 0" \ |
cd93789b | 103 | "${dump_all_cmds[@]}" \ |
129eb0f1 | 104 | -ex "attach $pid" -ex "gcore $prefix.$pid" -ex detach -ex quit |
dfb893af | 105 | |
129eb0f1 | 106 | if [ -r "$prefix.$pid" ] ; then |
627bf7c1 EZ |
107 | rc=0 |
108 | else | |
129eb0f1 | 109 | echo "@GCORE_TRANSFORM_NAME@: failed to create $prefix.$pid" |
627bf7c1 EZ |
110 | rc=1 |
111 | break | |
112 | fi | |
113 | ||
114 | ||
115 | done | |
116 | ||
117 | exit $rc |