1#! /bin/bash
2#
3# Copyright (C) 2010 Carlos Lopez <clopez@igalia.com>, Igalia S.L.
4#
5# This library is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Library General Public
7# License as published by the Free Software Foundation; either
8# version 2 of the License, or (at your option) any later version.
9#
10# This library 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 GNU
13# Library General Public License for more details.
14#
15# You should have received a copy of the GNU Library General Public License
16# along with this library; see the file COPYING.LIB.  If not, write to
17# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18# Boston, MA 02110-1301, USA.
19
20set -e
21
22[ "${coredir}" ] || {
23    echo "Env var '\${coredir}' not defined!" >&2
24    exit 111
25}
26[ "${programpath}" ] || {
27    echo "Env var '\${programpath}' not defined" >&2
28    exit 111
29}
30[ "${arch}" ] || {
31    echo "Env var '\${arch}' not defined" >&2
32    exit 111
33}
34[ "${mailto}" ] || {
35    echo "Env var '\${mailto}' not defined" >&2
36    exit 111
37}
38
39
40inotifywait -q -m --format '%f' --exclude '.trace.html$' -e close_write "${coredir}" | \
41while read -r coredump
42do
43	if grep -qE '^core-when_[[:digit:]]{10\,12}-_-who_[[:print:]]+-_-why_' <<< "${coredump}"
44	then
45		# Get revision number from Subversion sources
46		rev=$(cd "${crashmon_src_path}" && svn info | sed -e '/^Revision:/s/Revision: //p' -e d)
47
48		# Get the who from the coredump name
49		programfile=$(echo "${coredump}"  \
50			| awk -F'-_-who_' '{ print $2 }'\
51			| awk -F'-_-why_' '{ print $1 }')
52
53		# Sometimes programfile gets cut when it is a long name:
54		# Search using wildcards
55		fullprogrampath=$(find "${programpath}" -executable -name "${programfile}"\* | head -n1)
56
57		( printf "<html><head><title>StackTrace for ${programfile} from svn"
58			printf " rev ${rev}</title></head>\n<body>Core dump file: "
59			printf "<a href=\"cores/${coredump}\">${coredump}</a><br/>\n"
60			printf "<pre>Executable crashed: ${fullprogrampath}</pre>\n"
61			printf "<br/><hr><b>Stack Trace:</b><hr><br/>\n<pre>"
62
63			gdb -ex "thread apply all bt" --batch "${fullprogrampath}" "${coredump}" 2>&1 \
64				| sed -e 's/\&/\&amp/g;s/</\&lt;/g;s/>/\&gt;/g;s/\"/\&quot;/g' -e "s/'/\&\#039;/g"
65
66			printf "</pre></body></html>\n"
67		) > "/var/www/svn_${rev}.${coredump}.trace.html"
68
69		# Make sure the web server can read it
70		chmod 644 "${coredump}"
71	fi
72done
73
74