mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / mysql-test / lib / My / CoreDump.pm
blob0954602f1d42350fc68b2326421892f0a9d3c795
1 # -*- cperl -*-
2 # Copyright (c) 2008 MySQL AB, 2008, 2009 Sun Microsystems, Inc.
3 # Use is subject to license terms.
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; version 2 of the License.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 package My::CoreDump;
20 use strict;
21 use Carp;
22 use My::Platform;
24 use File::Temp qw/ tempfile tempdir /;
26 my $hint_mysqld; # Last resort guess for executable path
28 # If path in core file is 79 chars we assume it's been truncated
29 # Looks like we can still find the full path using 'strings'
30 # If that doesn't work, use the hint (mysqld path) as last resort.
32 sub _verify_binpath {
33 my ($binary, $core_name)= @_;
34 my $binpath;
36 if (length $binary != 79) {
37 $binpath= $binary;
38 print "Core generated by '$binpath'\n";
39 } else {
40 # Last occurrence of path ending in /mysql*, cut from first /
41 if (`strings '$core_name' | grep "/mysql[^/. ]*\$" | tail -1` =~ /(\/.*)/) {
42 $binpath= $1;
43 print "Guessing that core was generated by '$binpath'\n";
44 } else {
45 return unless $hint_mysqld;
46 $binpath= $hint_mysqld;
47 print "Wild guess that core was generated by '$binpath'\n";
50 return $binpath;
53 sub _gdb {
54 my ($core_name)= @_;
56 print "\nTrying 'gdb' to get a backtrace\n";
58 return unless -f $core_name;
60 # Find out name of binary that generated core
61 `gdb -c '$core_name' --batch 2>&1` =~
62 /Core was generated by `([^\s\'\`]+)/;
63 my $binary= $1 or return;
65 $binary= _verify_binpath ($binary, $core_name) or return;
67 # Create tempfile containing gdb commands
68 my ($tmp, $tmp_name) = tempfile();
69 print $tmp
70 "bt\n",
71 "thread apply all bt\n",
72 "quit\n";
73 close $tmp or die "Error closing $tmp_name: $!";
75 # Run gdb
76 my $gdb_output=
77 `gdb '$binary' -c '$core_name' -x '$tmp_name' --batch 2>&1`;
79 unlink $tmp_name or die "Error removing $tmp_name: $!";
81 return if $? >> 8;
82 return unless $gdb_output;
84 print <<EOF, $gdb_output, "\n";
85 Output from gdb follows. The first stack trace is from the failing thread.
86 The following stack traces are from all threads (so the failing one is
87 duplicated).
88 --------------------------
89 EOF
90 return 1;
94 sub _dbx {
95 my ($core_name)= @_;
97 print "\nTrying 'dbx' to get a backtrace\n";
99 return unless -f $core_name;
101 # Find out name of binary that generated core
102 `echo | dbx - '$core_name' 2>&1` =~
103 /Corefile specified executable: "([^"]+)"/;
104 my $binary= $1 or return;
106 $binary= _verify_binpath ($binary, $core_name) or return;
108 # Find all threads
109 my @thr_ids = `echo threads | dbx '$binary' '$core_name' 2>&1` =~ /t@\d+/g;
111 # Create tempfile containing dbx commands
112 my ($tmp, $tmp_name) = tempfile();
113 foreach my $thread (@thr_ids) {
114 print $tmp "where $thread\n";
116 print $tmp "exit\n";
117 close $tmp or die "Error closing $tmp_name: $!";
119 # Run dbx
120 my $dbx_output=
121 `cat '$tmp_name' | dbx '$binary' '$core_name' 2>&1`;
123 unlink $tmp_name or die "Error removing $tmp_name: $!";
125 return if $? >> 8;
126 return unless $dbx_output;
128 print <<EOF, $dbx_output, "\n";
129 Output from dbx follows. Stack trace is printed for all threads in order,
130 above this you should see info about which thread was the failing one.
131 ----------------------------
133 return 1;
137 # Check that Debugging tools for Windows are installed
138 sub cdb_check {
139 `cdb -? 2>&1`;
140 if ($? >> 8)
142 print "Cannot find cdb. Please Install Debugging tools for Windows\n";
143 print "from http://www.microsoft.com/whdc/devtools/debugging/";
144 if($ENV{'ProgramW6432'})
146 print "install64bit.mspx (native x64 version)\n";
148 else
150 print "installx86.mspx\n";
156 sub _cdb {
157 my ($core_name)= @_;
158 print "\nTrying 'cdb' to get a backtrace\n";
159 return unless -f $core_name;
161 # Try to set environment for debugging tools for Windows
162 if ($ENV{'PATH'} !~ /Debugging Tools/)
164 if ($ENV{'ProgramW6432'})
166 # On x64 computer
167 $ENV{'PATH'}.= ";".$ENV{'ProgramW6432'}."\\Debugging Tools For Windows (x64)";
169 else
171 # On x86 computer. Newest versions of Debugging tools are installed in the
172 # directory with (x86) suffix, older versions did not have this suffix.
173 $ENV{'PATH'}.= ";".$ENV{'ProgramFiles'}."\\Debugging Tools For Windows (x86)";
174 $ENV{'PATH'}.= ";".$ENV{'ProgramFiles'}."\\Debugging Tools For Windows";
179 # Read module list, find out the name of executable and
180 # build symbol path (required by cdb if executable was built on
181 # different machine)
182 my $tmp_name= $core_name.".cdb_lmv";
183 `cdb -z $core_name -c \"lmv;q\" > $tmp_name 2>&1`;
184 if ($? >> 8)
186 unlink($tmp_name);
187 # check if cdb is installed and complain if not
188 cdb_check();
189 return;
192 open(temp,"< $tmp_name");
193 my %dirhash=();
194 while(<temp>)
196 if($_ =~ /Image path\: (.*)/)
198 if (rindex($1,'\\') != -1)
200 my $dir= substr($1, 0, rindex($1,'\\'));
201 $dirhash{$dir}++;
205 close(temp);
206 unlink($tmp_name);
208 my $image_path= join(";", (keys %dirhash),".");
210 # For better callstacks, setup _NT_SYMBOL_PATH to include
211 # OS symbols. Note : Dowloading symbols for the first time
212 # can take some minutes
213 if (!$ENV{'_NT_SYMBOL_PATH'})
215 my $windir= $ENV{'windir'};
216 my $symbol_cache= substr($windir ,0, index($windir,'\\'))."\\cdb_symbols";
218 print "OS debug symbols will be downloaded and stored in $symbol_cache.\n";
219 print "You can control the location of symbol cache with _NT_SYMBOL_PATH\n";
220 print "environment variable. Please refer to Microsoft KB article\n";
221 print "http://support.microsoft.com/kb/311503 for details about _NT_SYMBOL_PATH\n";
222 print "-------------------------------------------------------------------------\n";
224 $ENV{'_NT_SYMBOL_PATH'}.=
225 "srv*".$symbol_cache."*http://msdl.microsoft.com/download/symbols";
228 my $symbol_path= $image_path.";".$ENV{'_NT_SYMBOL_PATH'};
231 # Run cdb. Use "analyze" extension to print crashing thread stacktrace
232 # and "uniqstack" to print other threads
234 my $cdb_cmd = "!sym prompts off; !analyze -v; .ecxr; !for_each_frame dv /t;!uniqstack -p;q";
235 my $cdb_output=
236 `cdb -c "$cdb_cmd" -z $core_name -i "$image_path" -y "$symbol_path" -t 0 -lines 2>&1`;
237 return if $? >> 8;
238 return unless $cdb_output;
240 # Remove comments (lines starting with *), stack pointer and frame
241 # pointer adresses and offsets to function to make output better readable
242 $cdb_output=~ s/^\*.*\n//gm;
243 $cdb_output=~ s/^([\:0-9a-fA-F\`]+ )+//gm;
244 $cdb_output=~ s/^ChildEBP RetAddr//gm;
245 $cdb_output=~ s/^Child\-SP RetAddr Call Site//gm;
246 $cdb_output=~ s/\+0x([0-9a-fA-F]+)//gm;
248 print <<EOF, $cdb_output, "\n";
249 Output from cdb follows. Faulting thread is printed twice,with and without function parameters
250 Search for STACK_TEXT to see the stack trace of
251 the faulting thread. Callstacks of other threads are printed after it.
253 return 1;
257 sub show {
258 my ($class, $core_name, $exe_mysqld)= @_;
259 $hint_mysqld= $exe_mysqld;
261 # On Windows, rely on cdb to be there...
262 if (IS_WINDOWS)
264 _cdb($core_name);
265 return;
268 # We try dbx first; gdb itself may coredump if run on a Sun Studio
269 # compiled binary on Solaris.
271 my @debuggers =
273 \&_dbx,
274 \&_gdb,
275 # TODO...
278 # Try debuggers until one succeeds
280 foreach my $debugger (@debuggers){
281 if ($debugger->($core_name)){
282 return;
285 return;