Commit | Line | Data |
---|---|---|
627bf7c1 EZ |
1 | #!/bin/sh |
2 | ||
4c38e0a4 JB |
3 | # Copyright (C) 2003, 2005, 2007, 2008, 2009, 2010 |
4 | # Free Software Foundation, Inc. | |
627bf7c1 EZ |
5 | |
6 | # This program is free software; you can redistribute it and/or modify | |
7 | # it under the terms of the GNU General Public License as published by | |
50efebf8 | 8 | # the Free Software Foundation; either version 3 of the License, or |
627bf7c1 | 9 | # (at your option) any later version. |
50efebf8 | 10 | # |
627bf7c1 EZ |
11 | # This program is distributed in the hope that it will be useful, |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. | |
50efebf8 | 15 | # |
627bf7c1 | 16 | # You should have received a copy of the GNU General Public License |
50efebf8 | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
627bf7c1 | 18 | |
627bf7c1 EZ |
19 | # |
20 | # gcore.sh | |
21 | # Script to generate a core file of a running program. | |
22 | # It starts up gdb, attaches to the given PID and invokes the gcore command. | |
23 | # | |
24 | ||
25 | if [ "$#" -eq "0" ] | |
26 | then | |
27 | echo "usage: gcore [-o filename] pid" | |
28 | exit 2 | |
29 | fi | |
30 | ||
31 | # Need to check for -o option, but set default basename to "core". | |
32 | name=core | |
33 | ||
34 | if [ "$1" = "-o" ] | |
35 | then | |
36 | if [ "$#" -lt "3" ] | |
37 | then | |
38 | # Not enough arguments. | |
39 | echo "usage: gcore [-o filename] pid" | |
40 | exit 2 | |
41 | fi | |
42 | name=$2 | |
43 | ||
44 | # Shift over to start of pid list | |
45 | shift; shift | |
46 | fi | |
47 | ||
dfb893af DJ |
48 | # Create a temporary file. Use mktemp if available, but cope if it is not. |
49 | tmpfile=`mktemp ${name}.XXXXXX 2>/dev/null` || { | |
50 | tmpfile=${name}.$$ | |
51 | if test -e $tmpfile; then | |
52 | echo "Could not create temporary file $tmpfile" | |
53 | exit 1 | |
54 | fi | |
55 | touch $tmpfile | |
56 | } | |
57 | trap "rm -f $tmpfile" EXIT | |
58 | ||
627bf7c1 EZ |
59 | # Initialise return code. |
60 | rc=0 | |
61 | ||
62 | # Loop through pids | |
63 | for pid in $* | |
64 | do | |
65 | # Write gdb script for pid $pid. | |
dfb893af DJ |
66 | cat >>$tmpfile <<EOF |
67 | attach $pid | |
68 | gcore $name.$pid | |
69 | detach | |
70 | quit | |
627bf7c1 EZ |
71 | EOF |
72 | ||
dfb893af DJ |
73 | gdb -x $tmpfile -batch |
74 | ||
627bf7c1 EZ |
75 | if [ -r $name.$pid ] ; then |
76 | rc=0 | |
77 | else | |
78 | echo gcore: failed to create $name.$pid | |
79 | rc=1 | |
80 | break | |
81 | fi | |
82 | ||
83 | ||
84 | done | |
85 | ||
86 | exit $rc |