summaryrefslogtreecommitdiff
path: root/contrib/scripts/ast_coredumper
blob: e5fc44eb3ef0e6deb316252ed12345115a662e33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env bash
# Turn on extended globbing
shopt -s extglob
# Bail on any error
set -e

prog=$(basename $0)

print_help() {
cat <<EOF
NAME
	$prog - Dump and/or format asterisk coredump files

SYNOPSIS
	$prog [ --help ] [ --running | --RUNNING ] [ --latest ]
		[ --tarball-coredumps ] [ --delete-coredumps-after ]
		[ --tarball-results ] [ --delete-results-after ]
		[ --tarball-uniqueid="<uniqueid>" ]
		[ --no-default-search ] [ --append-coredumps ]
		[ <coredump> | <pattern> ... ]

DESCRIPTION

	Extracts backtraces and lock tables from Asterisk coredump files.
	For each coredump found, 4 new result files are created:
	- <coredump>.brief.txt: The output of "thread apply all bt".

	- <coredump>.thread1.txt: The output of "thread apply 1 bt full".

	- <coredump>.full.txt: The output of "thread apply all bt full".

	- <coredump>.locks.txt: If asterisk was compiled with
		"DEBUG_THREADS", this file will contain a dump of the locks
		table similar to doing a "core show locks" from the asterisk
		CLI.

	Optional features:
	- The running asterisk process can be suspended and dumped.
	- The coredumps can be merged into a tarball.
	- The coredumps can be deleted after processing.
	- The results files can be merged into a tarball.
	- The results files can be deleted after processing.

	Options:

	--help
		Print this help.

	--running
		Create a coredump from the running asterisk instance and
		process it along with any other coredumps found (if any).
		WARNING: This WILL interrupt call processing.  You will be
		asked to confirm.

	--RUNNING
		Same as --running but without the confirmation prompt.
		DANGEROUS!!

	--latest
		Process only the latest coredump from those specified (based
		on last-modified time).  If a dump of the running process was
		requested, it is always included in addition to the latest
		from the existing coredumps.

	--tarball-coredumps
		Creates a gzipped tarball of coredumps processed, their
		results txt files and copies of /etc/os-release,
		/usr/sbin/asterisk, /usr/lib(64)/libasterisk* and
		/usr/lib(64)/asterisk as those files are needed to properly
		examine the coredump.  The file will be named
		/tmp/asterisk.<timestamp>.coredumps.tar.gz or
		/tmp/asterisk-<uniqueid>.coredumps.tar.gz if
		--tarball-uniqueid was specified.
		WARNING:  This file could 1gb in size!

	--delete-coredumps-after
		Deletes all processed coredumps regardless of whether
		a tarball was created.

	--tarball-results
		Creates a gzipped tarball of all result files produced.
		The tarball name will be:
		/tmp/asterisk.<timestamp>.results.tar.gz

	--delete-results-after
		Deletes all processed results regardless of whether
		a tarball was created.  It probably doesn't make sense
		to use this option unless you have also specified
		--tarball-results.

	--tarball-uniqueid="<uniqueid>"
		Normally DATEFORMAT is used to make the tarballs unique
		but you can use your own unique id in the tarball names
		such as the Jira issue id.

	--no-default-search
		Ignore COREDUMPS from the config files and process only
		coredumps listed on the command line (if any) and/or
		the running asterisk instance (if requested).

	--append-coredumps
		Append any coredumps specified on the command line to the
		config file specified ones instead of overriding them.

	<coredump> | <pattern>
		A list of coredumps or coredump search patterns.  Unless
		--append-coredumps was specified, these entries will override
		those specified in the config files.

		Any resulting file that isn't actually a coredump is silently
		ignored.  If your patterns contains spaces be sure to only
		quote the portion of the pattern that DOESN'T contain wildcard
		expressions.  If you quote the whole pattern, it won't be
		expanded.

		If --no-default-search is specified and no files are specified
		on the command line, then the only the running asterisk process
		will be dumped (if requested).  Otherwise if no files are
		specified on the command line the value of COREDUMPS from
		ast_debug_tools.conf will be used.  Failing that, the following
		patterns will be used:
		/tmp/core[-._]asterisk!(*.txt)
		/tmp/core[-._]\$(hostname)!(*.txt)

NOTES
	You must be root to use $prog.

	The script relies on not only bash, but also recent GNU date and
	gdb with python support.  *BSD operating systems may require
	installation of the 'coreutils' and 'devel/gdb' packagess and minor
	tweaking of the ast_debug_tools.conf file.

	Any files output will have ':' characters changed to '-'.  This is
	to facilitate uploading those files to Jira which doesn't like the
	colons.

FILES
	/etc/asterisk/ast_debug_tools.conf
	~/ast_debug_tools.conf
	./ast_debug_tools.conf

	#
	# This file is used by the Asterisk debug tools.
	# Unlike other Asterisk config files, this one is
	# "sourced" by bash and must adhere to bash semantics.
	#

	# A list of coredumps and/or coredump search patterns.
	# Bash extended globs are enabled and any resulting files
	# that aren't actually coredumps are silently ignored
	# so you can be liberal with the globs.
	#
	# If your patterns contains spaces be sure to only quote
	# the portion of the pattern that DOESN'T contain wildcard
	# expressions.  If you quote the whole pattern, it won't
	# be expanded and the glob characters will be treated as
	# literals.
	#
	# The exclusion of files ending ".txt" is just for
	# demonstration purposes as non-coredumps will be ignored
	# anyway.
	COREDUMPS=(/tmp/core[-._]asterisk!(*.txt) /tmp/core[-._]\$(hostname)!(*.txt))

	# Date command for the "running" coredump and tarballs.
	# DATEFORMAT will be executed to get the timestamp.
	# Don't put quotes around the format string or they'll be
	# treated as literal characters.  Also be aware of colons
	# in the output as you can't upload files with colons in
	# the name to Jira.
	#
	# Unix timestamp
	#DATEFORMAT='date +%s.%N'
	#
	# *BSD/MacOS doesn't support %N but after installing GNU
	# coreutils...
	#DATEFORMAT='gdate +%s.%N'
	#
	# Readable GMT
	#DATEFORMAT='date -u +%FT%H-%M-%S%z'
	#
	# Readable Local time
	DATEFORMAT='date +%FT%H-%M-%S%z'

EOF
	exit 1
}

if [ $EUID -ne 0 ] ; then
	echo "You must be root to use $prog."
	exit 1
fi

running=false
RUNNING=false
latest=false
tarball_coredumps=false
delete_coredumps_after=false
tarball_results=false
delete_results_after=false
append_coredumps=false

declare -a COREDUMPS
declare -a ARGS_COREDUMPS

# Read config files from least important to most important
[ -f /etc/asterisk/ast_debug_tools.conf ] && source /etc/asterisk/ast_debug_tools.conf
[ -f ~/ast_debug_tools.conf ] && source ~/ast_debug_tools.conf
[ -f ./ast_debug_tools.conf ] && source ./ast_debug_tools.conf

# For *BSD, the preferred gdb may be in /usr/local/bin so we
# need to search for one that supports python.
for g in $(which -a gdb) ; do
	result=$($g --batch --ex "python print('hello')" 2>/dev/null || : )
	if [[ "$result" =~ ^hello$ ]] ; then
		GDB=$g
		break
	fi
done

if [ -z "$GDB" ] ; then
	echo "No suitable gdb was found in $PATH"
	exit 1
fi

if [ ${#COREDUMPS[@]} -eq 0 ] ; then
	COREDUMPS+=(/tmp/core[-._]asterisk!(*.txt) /tmp/core[-._]$(hostname)!(*.txt))
fi

DATEFORMAT=${DATEFORMAT:-'date +%FT%H-%M-%S%z'}

# Use "$@" (with the quotes) so spaces in patterns or
# file names are preserved.
# Later on when we have to iterate over COREDUMPS, we always
# use the indexes rather than trying to expand the values of COREDUMPS
# just in case.

for a in "$@" ; do
	case "$a" in
	--running)
		running=true
		;;
	--RUNNING)
		RUNNING=true
		;;
	--no-default-search)
		# Clean out COREDUMPS from config files
		COREDUMPS=()
		;;
	--latest)
		latest=true
		;;
	--tarball-coredumps)
		tarball_coredumps=true
		;;
	--delete-coredumps-after)
		delete_coredumps_after=true
		;;
	--tarball-results)
		tarball_results=true
		;;
	--delete-results-after)
		delete_results_after=true
		;;
	--append-coredumps)
		append_coredumps=true
		;;
	--tarball-uniqueid=*)
		tarball_uniqueid=${a#*=}
		;;
	--help|-*)
		print_help
		;;
	*)
		ARGS_COREDUMPS+=("$a")
		# If any files are specified on the command line, ignore those
		# specified in the config files unless append-coredumps was specified.
		if ! $append_coredumps ; then
			COREDUMPS=()
		fi
	esac
done

# append coredumps/patterns specified as command line arguments to COREDUMPS.
for i in ${!ARGS_COREDUMPS[@]} ; do
	COREDUMPS+=("${ARGS_COREDUMPS[$i]}")
done

# At this point, all glob entries that match files should be expanded.
# Any entries that don't exist are probably globs that didn't match anything
# and need to be pruned.  Any non coredumps are also pruned.

for i in ${!COREDUMPS[@]} ; do
	if [ ! -f "${COREDUMPS[$i]}" ] ; then
		unset COREDUMPS[$i]
		continue
	fi
	# Some versions of 'file' don't allow only the first n bytes of the
	# file to be processed so we use dd to grab just the first 32 bytes.
	mimetype=$(dd if="${COREDUMPS[$i]}" bs=32 count=1 2>/dev/null | file -bi -)
	if [[ ! "$mimetype" =~ coredump ]] ; then
		unset COREDUMPS[$i]
		continue
	fi
done

# Sort and weed out any dups
IFS=$'\x0a'
readarray -t COREDUMPS < <(echo -n "${COREDUMPS[*]}" | sort -u )
unset IFS

# If --latest, get the last modified timestamp of each file,
# sort them, then return the latest.
if [ ${#COREDUMPS[@]} -gt 0 ] && $latest ; then
	lf=$(find "${COREDUMPS[@]}" -printf '%T@ %p\n' | sort -n | tail -1)
	COREDUMPS=("${lf#* }")
fi

# Timestamp to use for output files
df=${tarball_uniqueid:-$(${DATEFORMAT})}

if $running || $RUNNING ; then
	# We need to go through some gyrations to find the pid of the running
	# MAIN asterisk process and not someone or something running asterisk -r.
	# The pid file may NOT be in /var/run/asterisk so we need to find any
	# running asterisk process and see if -C was specified on the command
	# line.  The chances of more than 1 asterisk instance running with
	# different -C options is so unlikely that we're going to ignore it.
	#
	# 'ps axo command' should work on Linux (back to CentOS6) and FreeBSD.
	# If asterisk was started with -C, get the asterisk.conf file.
	# If it wasn't, assume /etc/asterisk/asterisk.conf
	astetcconf=`ps axo command | sed -n -r -e "s/.*asterisk\s+.*-C\s+([^ ]+).*/\1/gp" | tail -1`
	[ x$astetcconf = x ] && astetcconf=/etc/asterisk/asterisk.conf
	# Now parse out astrundir and cat asterisk.pid
	astrundir=$(sed -n -r -e "s/astrundir\s+[=>]+\s+(.*)/\1/gp" $astetcconf)
	pid=$(cat $astrundir/asterisk.pid 2>/dev/null || : )
	if [ x$pid = x ] ; then
		echo "Asterisk is not running"
	else
		if $RUNNING ; then
			answer=Y
		else
			read -p "WARNING:  Taking a core dump of the running asterisk instance will suspend call processing while the dump is saved.  Do you wish to continue? (y/N) " answer
		fi
		if [[ "$answer" =~ ^[Yy] ]] ; then
			cf="/tmp/core-asterisk-running-$df"
			echo "Dumping running asterisk process to $cf"
			${GDB} -p $pid -q --batch --ex "gcore $cf" >/dev/null 2>&1
			COREDUMPS+=("$cf")
		else
			echo "Skipping dump of running process"
		fi
	fi
fi

if [ "${#COREDUMPS[@]}" -eq 0 ] ; then
	echo "No coredumps found"
	print_help
fi

# Extract the gdb scripts from the end of this script
# and save them to /tmp/.gdbinit

ss=`egrep -n "^#@@@SCRIPTSTART@@@" $0 |cut -f1 -d:`
tail -n +${ss} $0 >/tmp/.ast_coredumper.gdbinit

# Now iterate over the coredumps and dump the debugging info
for i in ${!COREDUMPS[@]} ; do
	cf=${COREDUMPS[$i]}
	echo "Processing $cf"
	${GDB} -n --batch -q --ex "source /tmp/.ast_coredumper.gdbinit" $(which asterisk) "$cf" 2>/dev/null | (
		of=/dev/null
		while IFS= read line ; do
			if [[ "$line" =~ !@!@!@!\ ([^\ ]+)\ !@!@!@! ]] ; then
				of=${cf}-${BASH_REMATCH[1]}
				of=${of//:/-}
				rm -f "$of"
				echo "Creating $of"
			fi
			echo -e $"$line" >> "$of"
		done
	)
done

if $tarball_coredumps ; then
	tf=/tmp/asterisk-$df.coredumps.tar.gz
	echo "Creating $tf"
	dest=/tmp/asterisk-$df
	rm -rf $dest 2>/dev/null || :
	libdir=usr/lib
	[ -d /usr/lib64 ] && libdir+=64
	mkdir -p $dest/tmp $dest/$libdir/asterisk $dest/etc $dest/usr/sbin
	for i in ${!COREDUMPS[@]} ; do
		ln -s "${COREDUMPS[@]}" $dest/"${COREDUMPS[@]}"
		cp "${COREDUMPS[@]}"*.txt $dest/tmp/
	done
	cp /etc/os-release $dest/etc/
	cp -a /$libdir/libasterisk* $dest/$libdir/
	cp -a /$libdir/asterisk/* $dest/$libdir/asterisk/
	cp -a /usr/sbin/asterisk $dest/usr/sbin
	rm -rf $tf
	tar -chzf $tf --transform="s/^[.]/$df/" -C $dest .
	rm -rf $dest
	echo "Created $tf"
fi

if $delete_coredumps_after ; then
	for i in ${!COREDUMPS[@]} ; do
		rm -rf "${COREDUMPS[$i]}"
	done
fi

if $tarball_results ; then
	tf=/tmp/asterisk-$df-results.tar
	echo "Creating $tf.gz"
	for i in ${!COREDUMPS[@]} ; do
		tar -uvf $tf "${COREDUMPS[$i]//:/-}"-{brief,full,thread1,locks}.txt 2>/dev/null
	done
	gzip $tf
fi

if $delete_results_after ; then
	for i in ${!COREDUMPS[@]} ; do
		rm -rf "${COREDUMPS[$i]//:/-}"-{brief,full,thread1,locks}.txt
	done
fi

exit

# Be careful editng the inline scripts.
# They're space-indented.

# We need the python bit because lock_infos isn't
# a valid symbol in asterisk unless DEBUG_THREADS was
# used during the compile.  Also, interrupt and continue
# are only valid for a running program.

#@@@SCRIPTSTART@@@
python
class DumpAsteriskCommand(gdb.Command):

    def __init__(self):
        super(DumpAsteriskCommand, self).__init__ ("dump-asterisk",
            gdb.COMMAND_OBSCURE, gdb.COMPLETE_COMMAND)

    def invoke(self, arg, from_tty):
        try:
            gdb.execute("interrupt", from_tty)
        except:
            pass
        print("!@!@!@! thread1.txt !@!@!@!\n")
        try:
            gdb.execute("p $_siginfo", from_tty)
            gdb.execute("info signal $_siginfo.si_signo")
            gdb.execute("thread apply 1 bt full", from_tty)
        except:
            pass
        print("!@!@!@! brief.txt !@!@!@!\n")
        try:
            gdb.execute("p $_siginfo", from_tty)
            gdb.execute("info signal $_siginfo.si_signo")
            gdb.execute("thread apply all bt", from_tty)
        except:
            pass
        print("!@!@!@! full.txt !@!@!@!\n")
        try:
            gdb.execute("p $_siginfo", from_tty)
            gdb.execute("info signal $_siginfo.si_signo")
            gdb.execute("thread apply all bt full", from_tty)
        except:
            pass
        print("!@!@!@! locks.txt !@!@!@!\n")
        try:
            gdb.execute("p $_siginfo", from_tty)
            gdb.execute("info signal $_siginfo.si_signo")
            gdb.execute("show_locks", from_tty)
        except:
            pass
        try:
            gdb.execute("continue", from_tty)
        except:
            pass

DumpAsteriskCommand ()
end

define show_locks
   set $n = lock_infos.first

   if $argc == 0
      printf "                                                                                                                    where_held count-|\n"
      printf "                                                                                                                         suspended-| |\n"
      printf "                                                                                                        type- |     times locked-| | |\n"
      printf "thread         status   file                   line function                             lock name            | lock addr        | | |\n"
   else
      printf "thread,status,file,line,function,lock_name,lock_type,lock_addr,times_locked,suspended,where_held_count,where_held_file,where_held_line,where_held_function,there_held_thread\n"
   end

   while $n
      if $n->num_locks > 0
      set $i = 0
      while $i < $n->num_locks
         if $n->locks[$i]->suspended == 0
            if ((ast_mutex_t *)$n->locks[$i]->lock_addr)->tracking
               if $n->locks[$i]->type > 0
                  set $track = ((ast_rwlock_t *)$n->locks[$i]->lock_addr)->track
               else
                  set $track = ((ast_mutex_t *)$n->locks[$i]->lock_addr)->track
               end
            end
            set $reentrancy = $track->reentrancy
            set $pending = $n->locks[$i]->pending
            if $argc > 0
               printf "%p,%d,%s,%d,%s,%s,%d,%p,%d,%d,%d",\
                  $n->thread_id, $n->locks[$i]->pending, $n->locks[$i]->file, $n->locks[$i]->line_num, $n->locks[$i]->func,\
                  $n->locks[$i]->lock_name, $n->locks[$i]->type, $n->locks[$i]->lock_addr, $n->locks[$i]->times_locked,\
                  $n->locks[$i]->suspended, $track->reentrancy
               if $reentrancy
                  if $pending
                     printf ",%s,%d,%s,%p", $track->file[0], $track->lineno[0], $track->func[0], $track->thread[0]
                  end
               end
            else
               if $n->locks[$i]->pending < 0
                  printf "%p failed   %-20s %6d %-36s %-20s %d %14p %3d %d %d",\
                     $n->thread_id,\
                     $n->locks[$i]->file, $n->locks[$i]->line_num, $n->locks[$i]->func,\
                     $n->locks[$i]->lock_name, $n->locks[$i]->type, $n->locks[$i]->lock_addr, $n->locks[$i]->times_locked,\
                     $n->locks[$i]->suspended, $track->reentrancy
               end
               if $n->locks[$i]->pending == 0
                  printf "%p holding  %-20s %6d %-36s %-20s %d %14p %3d %d %d",\
                     $n->thread_id,\
                     $n->locks[$i]->file, $n->locks[$i]->line_num, $n->locks[$i]->func,\
                     $n->locks[$i]->lock_name, $n->locks[$i]->type, $n->locks[$i]->lock_addr, $n->locks[$i]->times_locked,\
                     $n->locks[$i]->suspended, $track->reentrancy
               end
               if $n->locks[$i]->pending > 0
                  printf "%p waiting  %-20s %6d %-36s %-20s %d %14p %3d %d %d",\
                     $n->thread_id,\
                     $n->locks[$i]->file, $n->locks[$i]->line_num, $n->locks[$i]->func,\
                     $n->locks[$i]->lock_name, $n->locks[$i]->type, $n->locks[$i]->lock_addr, $n->locks[$i]->times_locked,\
                     $n->locks[$i]->suspended, $track->reentrancy
               end
               if $reentrancy
                  if $pending
                     printf "\n               held at: %-20s %6d %-36s by 0x%08lx", $track->file[0], $track->lineno[0], $track->func[0], $track->thread_id[0]
                  end
               end
            end
            printf "\n"
         end
         set $i = $i + 1
      end
    end
    set $n = $n->entry->next
  end
end

dump-asterisk