| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
shopt -s extglob |
| 5 |
shopt -s nullglob |
| 6 |
|
| 7 |
set -e |
| 8 |
|
| 9 |
prog=$(basename "$0") |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
source <(sed -n "/^#@@@FUNCSSTART@@@/,/^#@@@FUNCSEND@@@/ p" "$0" | sed '1d;$d') |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
: "${DATEOPTS=-u +%FT%H-%M-%SZ}" |
| 24 |
: "${DELETE_COREDUMPS_AFTER:=false}" |
| 25 |
: "${DELETE_RESULTS_AFTER:=false}" |
| 26 |
: "${DRY_RUN:=false}" |
| 27 |
: "${GDB:=$(which gdb)}" |
| 28 |
: "${HELP:=false}" |
| 29 |
: "${LATEST:=false}" |
| 30 |
: "${OUTPUTDIR:=/tmp}" |
| 31 |
: "${PROMPT:=true}" |
| 32 |
: "${RUNNING:=false}" |
| 33 |
: "${RENAME:=true}" |
| 34 |
: "${TARBALL_CONFIG:=false}" |
| 35 |
: "${TARBALL_COREDUMPS:=false}" |
| 36 |
: "${TARBALL_RESULTS:=false}" |
| 37 |
: "${MODDIR:=}" |
| 38 |
: "${LIBDIR:=}" |
| 39 |
: "${ETCDIR:=}" |
| 40 |
|
| 41 |
COMMANDLINE_COREDUMPS=false |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
safe_source_config ./ast_debug_tools.conf |
| 46 |
safe_source_config ~/ast_debug_tools.conf |
| 47 |
safe_source_config /etc/asterisk/ast_debug_tools.conf |
| 48 |
|
| 49 |
if [ -n "${DATEFORMAT}" ] ; then |
| 50 |
err <<-EOF |
| 51 |
FYI... The DATEFORMAT variable in your ast_debug_tools.conf file has been |
| 52 |
replaced with DATEOPTS which has a different format. See the latest |
| 53 |
ast_debug_tools.conf sample file for more information. |
| 54 |
EOF |
| 55 |
fi |
| 56 |
|
| 57 |
for a in "$@" ; do |
| 58 |
if [[ $a == "--RUNNING" ]] ; then |
| 59 |
RUNNING=true |
| 60 |
PROMPT=false |
| 61 |
elif [[ $a =~ --no-([^=]+)$ ]] ; then |
| 62 |
var=${BASH_REMATCH[1]//-/_} |
| 63 |
eval "${var^^}"="false" |
| 64 |
elif [[ $a =~ --([^=]+)$ ]] ; then |
| 65 |
var=${BASH_REMATCH[1]//-/_} |
| 66 |
eval "${var^^}"="true" |
| 67 |
elif [[ $a =~ --([^=]+)=(.+)$ ]] ; then |
| 68 |
var=${BASH_REMATCH[1]//-/_} |
| 69 |
eval "${var^^}"="${BASH_REMATCH[2]}" |
| 70 |
else |
| 71 |
if ! $COMMANDLINE_COREDUMPS ; then |
| 72 |
COMMANDLINE_COREDUMPS=true |
| 73 |
COREDUMPS=() |
| 74 |
fi |
| 75 |
COREDUMPS+=( "$a" ) |
| 76 |
fi |
| 77 |
done |
| 78 |
|
| 79 |
if $HELP ; then |
| 80 |
print_help |
| 81 |
exit 0 |
| 82 |
fi |
| 83 |
|
| 84 |
|
| 85 |
check_gdb |
| 86 |
|
| 87 |
if [ $EUID -ne 0 ] ; then |
| 88 |
die -13 "You must be root to use $prog." |
| 89 |
fi |
| 90 |
|
| 91 |
if [ -z "${OUTPUTDIR}" ] || [ ! -d "${OUTPUTDIR}" ] ; then |
| 92 |
die -20 "OUTPUTDIR ${OUTPUTDIR} doesn't exists or is not a directory" |
| 93 |
fi |
| 94 |
|
| 95 |
if $RUNNING ; then |
| 96 |
MAIN_PID=$(find_pid) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
COREDUMPS=( ) |
| 101 |
|
| 102 |
msg "Found a single asterisk instance running as process $MAIN_PID" |
| 103 |
|
| 104 |
if $PROMPT ; then |
| 105 |
read -r -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 |
| 106 |
else |
| 107 |
answer=Y |
| 108 |
fi |
| 109 |
|
| 110 |
if [[ "$answer" =~ ^[Yy] ]] ; then |
| 111 |
|
| 112 |
df=$(date ${DATEOPTS}) |
| 113 |
cf="${OUTPUTDIR}/core-asterisk-running-$df" |
| 114 |
echo "$(S_COR "${DRY_RUN}" 'Simulating dumping' 'Dumping') running asterisk process to $cf" |
| 115 |
if ${DRY_RUN} ; then |
| 116 |
echo "Would run: ${GDB} -p $MAIN_PID -q --batch --ex gcore $cf" |
| 117 |
else |
| 118 |
install -m 0600 /dev/null "$cf" |
| 119 |
${GDB} -p "$MAIN_PID" -q --batch --ex "gcore $cf" >/dev/null 2>&1 |
| 120 |
fi |
| 121 |
echo "$(S_COR "${DRY_RUN}" 'Simulated dump' 'Dump') is complete." |
| 122 |
|
| 123 |
COREDUMPS=( "$cf" ) |
| 124 |
|
| 125 |
exe=$(extract_binary_name "${cf}") |
| 126 |
if [ -z "${exe}" ] ; then |
| 127 |
die -125 "Coredump produced has no executable!" |
| 128 |
fi |
| 129 |
|
| 130 |
module_dir=$(extract_string_symbol "${exe}" "${cf}" ast_config_AST_MODULE_DIR) |
| 131 |
if [ ! -d "$module_dir" ] ; then |
| 132 |
die -125 "Couldn't get module directory from coredump!" |
| 133 |
fi |
| 134 |
else |
| 135 |
die -125 "Aborting dump of running process" |
| 136 |
fi |
| 137 |
|
| 138 |
$DRY_RUN && exit 0 |
| 139 |
else |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if [ ${#COREDUMPS[@]} -eq 0 ] ; then |
| 144 |
|
| 145 |
|
| 146 |
mapfile -t COREDUMPS < <(readlink -f /tmp/core!(*.txt) | sort -u) |
| 147 |
fi |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
for i in "${!COREDUMPS[@]}" ; do |
| 154 |
if [ ! -f "${COREDUMPS[$i]}" ] ; then |
| 155 |
unset "COREDUMPS[$i]" |
| 156 |
continue |
| 157 |
fi |
| 158 |
cf="${COREDUMPS[$i]}" |
| 159 |
|
| 160 |
msg "Examining ${cf}" |
| 161 |
|
| 162 |
dump_note_strings "${cf}" | grep -q -E "app_dial|pbx_config" || { |
| 163 |
err " Doesn't appear to be an asterisk coredump" |
| 164 |
unset "COREDUMPS[$i]" |
| 165 |
continue |
| 166 |
} |
| 167 |
msg " Does appear to be an asterisk coredump" |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
exe=$(extract_binary_name "${cf}") |
| 175 |
if [ -z "${exe}" ] ; then |
| 176 |
err " Can't extract executable. Skipping." |
| 177 |
unset "COREDUMPS[$i]" |
| 178 |
continue |
| 179 |
fi |
| 180 |
msg " Coredump indicates executable '${exe}'" |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if [ -n "${ASTERISK_BIN}" ] ; then |
| 188 |
msg " but --asterisk-bin was specified so using '${ASTERISK_BIN}'" |
| 189 |
exe="${ASTERISK_BIN}" |
| 190 |
fi |
| 191 |
|
| 192 |
msg " Searching for asterisk module directory" |
| 193 |
|
| 194 |
module_dir=$(extract_string_symbol "${exe}" "${cf}" \ |
| 195 |
ast_config_AST_MODULE_DIR) |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
if [ ! -d "$module_dir" ] ; then |
| 200 |
err <<-EOF |
| 201 |
Can't extract asterisk module directory. |
| 202 |
Either the executable '${exe}' has no symbols |
| 203 |
or it's changed since the coredump was generated. |
| 204 |
Either way we can't use it. If you still have the |
| 205 |
binary that created this coredump, or can recreate |
| 206 |
the binary from the exact same code base and exact same |
| 207 |
options that were used to to create the binary that generated |
| 208 |
this coredump, specify its location with the |
| 209 |
--asterisk-bin option. |
| 210 |
EOF |
| 211 |
unset "COREDUMPS[$i]" |
| 212 |
continue |
| 213 |
fi |
| 214 |
msg " Found asterisk module directory '${module_dir}'" |
| 215 |
if [ -n "${MODDIR}" ] ; then |
| 216 |
msg " but --moddir was specified so using '${MODDIR}'" |
| 217 |
fi |
| 218 |
|
| 219 |
done |
| 220 |
|
| 221 |
if [ ${#COREDUMPS[@]} -eq 0 ] ; then |
| 222 |
die -2 "No valid coredumps found" |
| 223 |
fi |
| 224 |
|
| 225 |
|
| 226 |
mapfile -t COREDUMPS < <(readlink -e "${COREDUMPS[@]}" | sort -u) |
| 227 |
|
| 228 |
if [ ${#COREDUMPS[@]} -eq 0 ] ; then |
| 229 |
die -2 "No coredumps found" |
| 230 |
fi |
| 231 |
|
| 232 |
if $LATEST ; then |
| 233 |
COREDUMPS=( "${COREDUMPS[0]}" ) |
| 234 |
fi |
| 235 |
fi |
| 236 |
|
| 237 |
if [ ${#COREDUMPS[@]} -eq 0 ] ; then |
| 238 |
die -2 "No coredumps found" |
| 239 |
fi |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
gdbinit=${OUTPUTDIR}/.ast_coredumper.gdbinit |
| 246 |
trap 'rm $gdbinit' EXIT |
| 247 |
install -m 600 /dev/stdin "$gdbinit" < <(sed '1,/^#@@@SCRIPTSTART@@@/ d' "$0") |
| 248 |
|
| 249 |
|
| 250 |
for i in "${!COREDUMPS[@]}" ; do |
| 251 |
cf=$(realpath -e "${COREDUMPS[$i]}" || : ) |
| 252 |
if [ -z "$cf" ] ; then |
| 253 |
continue |
| 254 |
fi |
| 255 |
echo "Processing $cf" |
| 256 |
astbin="${ASTERISK_BIN}" |
| 257 |
[ -z "${astbin}" ] && astbin=$(extract_binary_name "${cf}") |
| 258 |
moddir="${MODDIR}" |
| 259 |
[ -z "${moddir}" ] && moddir=$(extract_string_symbol "${exe}" "${cf}" ast_config_AST_MODULE_DIR) |
| 260 |
etcdir="${ETCDIR}" |
| 261 |
[ -z "${etcdir}" ] && etcdir=$(extract_string_symbol "${exe}" "${cf}" ast_config_AST_CONFIG_DIR) |
| 262 |
libdir="${LIBDIR}" |
| 263 |
[ -z "${libdir}" ] && { |
| 264 |
libfile=$(dump_note_strings "${cf}" | grep -m 1 -E "libasteriskssl|libasteriskpj") |
| 265 |
libdir=$(dirname "${libfile}") |
| 266 |
} |
| 267 |
|
| 268 |
[ "$(dirname "${astbin}")" == "." ] && astbin="$(which "${astbin}")" || : |
| 269 |
astbin=$(realpath -e "${astbin}") |
| 270 |
msg " ASTBIN: $astbin" |
| 271 |
msg " MODDIR: $moddir" |
| 272 |
msg " ETCDIR: $etcdir" |
| 273 |
msg " LIBDIR: $libdir" |
| 274 |
|
| 275 |
astbin_base=$(basename "${astbin}") |
| 276 |
if ! $RUNNING && ! [[ "$cf" =~ "running" ]] && $RENAME ; then |
| 277 |
|
| 278 |
df=$(date -r "$cf" ${DATEOPTS}) |
| 279 |
cfdir=$(dirname "$cf") |
| 280 |
newcf="${cfdir}/core-${astbin_base}-${df}" |
| 281 |
if [ "${newcf}" != "${cf}" ] ; then |
| 282 |
msg " Renaming $cf to $cfdir/core-${astbin_base}-${df}" |
| 283 |
rm "${cfdir}/core-${astbin_base}-${df}" >/dev/null 2>&1 || : |
| 284 |
ln -s "$cf" "${cfdir}/core-${astbin_base}-${df}" |
| 285 |
cf="${cfdir}/core-${astbin_base}-${df}" |
| 286 |
fi |
| 287 |
fi |
| 288 |
|
| 289 |
cfname=$(basename "${cf}") |
| 290 |
|
| 291 |
|
| 292 |
${GDB} -n --batch -q --iex "source $gdbinit" -ex "dump-asterisk" "${astbin}" "$cf" 2>/dev/null | ( |
| 293 |
of=/dev/null |
| 294 |
while IFS= read -r line ; do |
| 295 |
if [[ "$line" =~ !@!@!@!\ ([^\ ]+)\ !@!@!@! ]] ; then |
| 296 |
of=${OUTPUTDIR}/${cfname}-${BASH_REMATCH[1]} |
| 297 |
of=${of//:/-} |
| 298 |
rm -f "$of" |
| 299 |
install -m 0600 /dev/null "$of" |
| 300 |
msg " Creating $of" |
| 301 |
fi |
| 302 |
echo -e $"$line" >> "$of" |
| 303 |
done |
| 304 |
) |
| 305 |
|
| 306 |
if $TARBALL_COREDUMPS ; then |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
cfname="${cfname//:/-}" |
| 311 |
tf="${OUTPUTDIR}/${cfname}.tar.gz" |
| 312 |
echo " Creating ${tf}" |
| 313 |
|
| 314 |
dest="${OUTPUTDIR}/${cfname}.output" |
| 315 |
rm -rf "${dest}" 2>/dev/null || : |
| 316 |
|
| 317 |
astbindir=$(dirname "${astbin}") |
| 318 |
install -m 0700 -d "${dest}" |
| 319 |
mkdir -p "${dest}/tmp" "${dest}/${moddir}" "${dest}/etc" \ |
| 320 |
"${dest}/${etcdir}" "${dest}/${libdir}" "${dest}/${astbindir}" |
| 321 |
|
| 322 |
ln -s "${cf}" "${dest}/tmp/${cfname}" |
| 323 |
msg " Copying results files" |
| 324 |
cp "${OUTPUTDIR}/${cfname}"*.txt "${dest}/tmp/" |
| 325 |
[ -f /etc/os-release ] && { |
| 326 |
msg " Copying /etc/os-release" |
| 327 |
cp /etc/os-release "${dest}/etc/" |
| 328 |
} |
| 329 |
|
| 330 |
$TARBALL_CONFIG && { |
| 331 |
msg " Copying $etcdir" |
| 332 |
cp -a "${etcdir}"/* "${dest}/${etcdir}/" |
| 333 |
} |
| 334 |
|
| 335 |
msg " Copying ${libdir}/libasterisk*" |
| 336 |
cp -a "${libdir}/libasterisk"* "${dest}/${libdir}/" |
| 337 |
msg " Copying ${moddir}" |
| 338 |
cp -a "${moddir}"/* "${dest}/${moddir}/" |
| 339 |
msg " Copying ${astbin}" |
| 340 |
cp -a "${astbin}" "${dest}/${astbin}" |
| 341 |
rm -rf "${tf}" |
| 342 |
msg " Creating ${tf}" |
| 343 |
install -m 0600 /dev/null "$tf" |
| 344 |
tar -chzf "${tf}" --transform="s/^[.]/${cfname}.output/" -C "${dest}" . |
| 345 |
sleep 3 |
| 346 |
rm -rf "${dest}" |
| 347 |
msg " Created $tf" |
| 348 |
elif $TARBALL_RESULTS ; then |
| 349 |
cfname="${cfname//:/-}" |
| 350 |
tf="${OUTPUTDIR}/${cfname}.tar.gz" |
| 351 |
msg " Creating ${tf}" |
| 352 |
|
| 353 |
dest="${OUTPUTDIR}/${cfname}.output" |
| 354 |
rm -rf "${dest}" 2>/dev/null || : |
| 355 |
mkdir -p "${dest}" |
| 356 |
cp "${OUTPUTDIR}/${cfname}"*.txt "${dest}/" |
| 357 |
install -m 0600 /dev/null "$tf" |
| 358 |
tar -chzf "${tf}" --transform="s/^[.]/${cfname}/" -C "${dest}" . |
| 359 |
rm -rf "${dest}" |
| 360 |
echo "Created $tf" |
| 361 |
fi |
| 362 |
|
| 363 |
if $DELETE_COREDUMPS_AFTER ; then |
| 364 |
rm -rf "${cf}" |
| 365 |
fi |
| 366 |
|
| 367 |
if $DELETE_RESULTS_AFTER ; then |
| 368 |
to_delete="$cf" |
| 369 |
if [ -n "$OUTPUTDIR" ] ; then |
| 370 |
to_delete="$OUTPUTDIR/$cfname" |
| 371 |
fi |
| 372 |
rm -rf "${to_delete//:/-}"-{brief,full,thread1,locks,info}.txt |
| 373 |
fi |
| 374 |
done |
| 375 |
|
| 376 |
exit |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
err() { |
| 382 |
if [ -z "$1" ] ; then |
| 383 |
cat >&2 |
| 384 |
else |
| 385 |
echo "$1" >&2 |
| 386 |
fi |
| 387 |
return 0 |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
msg() { |
| 392 |
if [ -z "$1" ] ; then |
| 393 |
cat |
| 394 |
else |
| 395 |
echo "$1" |
| 396 |
fi |
| 397 |
return 0 |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
die() { |
| 402 |
if [[ $1 =~ ^-([0-9]+) ]] ; then |
| 403 |
RC=${BASH_REMATCH[1]} |
| 404 |
shift |
| 405 |
fi |
| 406 |
err "$1" |
| 407 |
exit "${RC:-1}" |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
S_COR() { |
| 412 |
if $1 ; then |
| 413 |
echo -n "$2" |
| 414 |
else |
| 415 |
echo -n "$3" |
| 416 |
fi |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
check_gdb() { |
| 421 |
if [ -z "${GDB}" -o ! -x "${GDB}" ] ; then |
| 422 |
die -2 <<-EOF |
| 423 |
${GDB} seems to not be installed. |
| 424 |
Please install gdb or use the '--gdb' option to |
| 425 |
point to a valid executable. |
| 426 |
EOF |
| 427 |
fi |
| 428 |
|
| 429 |
result=$($GDB --batch --ex "python print('hello')" 2>/dev/null || : ) |
| 430 |
if [[ ! "$result" =~ ^hello$ ]] ; then |
| 431 |
die -2 <<-EOF |
| 432 |
$GDB does not support python. |
| 433 |
Use the '--gdb' option to point to one that does. |
| 434 |
EOF |
| 435 |
fi |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
safe_source_config() { |
| 442 |
local config_file="$1" |
| 443 |
|
| 444 |
|
| 445 |
[ -f "$config_file" ] || return 0 |
| 446 |
|
| 447 |
|
| 448 |
config_file=$(readlink -f "$config_file") |
| 449 |
|
| 450 |
|
| 451 |
local file_stat |
| 452 |
file_stat=$(stat -c "%u %a" "$config_file" 2>/dev/null) || return 0 |
| 453 |
local owner_uid=${file_stat%% *} |
| 454 |
local perms=${file_stat##* } |
| 455 |
|
| 456 |
|
| 457 |
if [ "$owner_uid" -ne 0 ]; then |
| 458 |
err "Config file $config_file is not owned by root. Skipping for security." >&2 |
| 459 |
return 1 |
| 460 |
fi |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
local group_perms=$((perms / 10 % 10)) |
| 465 |
local other_perms=$((perms % 10)) |
| 466 |
|
| 467 |
if [ $((group_perms & 2)) -ne 0 ] || [ $((other_perms & 2)) -ne 0 ]; then |
| 468 |
err "Config file $config_file is writable by group or others. Skipping for security." >&2 |
| 469 |
return 1 |
| 470 |
fi |
| 471 |
|
| 472 |
source "$config_file" |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
find_pid() { |
| 477 |
if [ -n "$PID" ] ; then |
| 478 |
|
| 479 |
[[ $PID =~ ^[0-9]+$ ]] || die -22 $"Pid $PID is invalid." |
| 480 |
|
| 481 |
cmd=$(ps -p "$PID" -o comm=) || die -22 "Pid $PID is not a valid process." |
| 482 |
|
| 483 |
|
| 484 |
grep -q -E "app_dial|pbx_config" "/proc/$PID/maps" || \ |
| 485 |
die -22 "Pid $PID '$cmd' not 'asterisk'." |
| 486 |
echo "$PID" |
| 487 |
return 0 |
| 488 |
fi |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
pids=$( pgrep -d ',' -x "asterisk") |
| 494 |
if [ -z "${pids}" ] ; then |
| 495 |
die -3 <<-EOF |
| 496 |
No running asterisk instances detected. |
| 497 |
If you know the pid of the process you want to dump, |
| 498 |
supply it on the command line with --pid=<pid>. |
| 499 |
EOF |
| 500 |
fi |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
declare -a candidates |
| 505 |
while read -r LINE ; do |
| 506 |
[[ $LINE =~ ([0-9]+)[\ ]+([^\ ]+)[\ ]+(.*) ]] || continue |
| 507 |
pid=${BASH_REMATCH[1]} |
| 508 |
prog=${BASH_REMATCH[2]} |
| 509 |
args=${BASH_REMATCH[3]} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
[[ "$prog" == "rasterisk" ]] && continue; |
| 515 |
candidates[$pid]="${prog}^${args}" |
| 516 |
done < <(ps -o pid= -o command= -p "$pids") |
| 517 |
|
| 518 |
if [ ${#candidates[@]} -eq 0 ] ; then |
| 519 |
die -3 <<-EOF |
| 520 |
No running asterisk instances detected. |
| 521 |
If you know the pid of the process you want to dump, |
| 522 |
supply it on the command line with --pid=<pid>. |
| 523 |
EOF |
| 524 |
fi |
| 525 |
|
| 526 |
if [ ${#candidates[@]} -gt 1 ] ; then |
| 527 |
die -22 <<-EOF |
| 528 |
Detected more than one asterisk process running. |
| 529 |
$(printf "%8s %s\n" "PID" "COMMAND") |
| 530 |
$(for p in "${!candidates[@]}" ; do printf "%8s %s\n" $p "${candidates[$p]//^/ }" ; done ) |
| 531 |
If you know the pid of the process you want to dump, |
| 532 |
supply it on the command line with --pid=<pid>. |
| 533 |
EOF |
| 534 |
fi |
| 535 |
|
| 536 |
echo "${!candidates[@]}" |
| 537 |
return 0 |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
extract_binary_name() { |
| 543 |
${GDB} -c "$1" -q --batch -ex "info proc exe" 2>/dev/null \ |
| 544 |
| sed -n -r -e "s/exe\s*=\s*'([^ ]+).*'/\1/gp" |
| 545 |
return 0 |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
extract_string_symbol() { |
| 551 |
${GDB} "$1" "$2" -q --batch \ |
| 552 |
-ex "p (char *)$3" 2>/dev/null \ |
| 553 |
| sed -n -r -e 's/[$]1\s*=\s*[0-9a-fx]+\s+<[^>]+>\s+"([^"]+)"/\1/gp' |
| 554 |
return 0 |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
dump_note_strings() { |
| 567 |
note0=$(objdump -h "$1" | grep note0) |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
[[ "${note0}" =~ ^[\ \t]*[0-9]+[\ \t]+note0[\ \t]+([0-9a-f]+)[\ \t]+[0-9a-f]+[\ \t]+[0-9a-f]+[\ \t]+([0-9a-f]+) ]] || { |
| 575 |
return 1 |
| 576 |
} |
| 577 |
count=$((0x${BASH_REMATCH[1]})) |
| 578 |
skip=$((0x${BASH_REMATCH[2]})) |
| 579 |
|
| 580 |
dd if="$1" bs=1 count="$count" skip="$skip" 2>/dev/null | strings -n "${2:-8}" |
| 581 |
return 0 |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
print_help() { |
| 586 |
cat <<EOF |
| 587 |
NAME |
| 588 |
$prog - Dump and/or format asterisk coredump files |
| 589 |
|
| 590 |
SYNOPSIS |
| 591 |
$prog [ --help ] [ --running | --RUNNING ] [ --pid=<pid> ] |
| 592 |
[ --latest ] [ --outputdir=<path> ] |
| 593 |
[ --asterisk-bin=<path to asterisk binary that created the coredump> ] |
| 594 |
[ --moddir=<path to asterisk modules directory that created the coredump> ] |
| 595 |
[ --libdir=<path to directory containing libasterisk* libraries> ] |
| 596 |
[ --gdb=<path to gdb> ] [ --rename ] [ --dateformat=<date options> ] |
| 597 |
[ --tarball-coredumps ] [ --delete-coredumps-after ] |
| 598 |
[ --tarball-results ] [ --delete-results-after ] |
| 599 |
[ --tarball-config ] |
| 600 |
[ --etcdir=<path to directory containing asterisk config files> ] |
| 601 |
[ <coredump> | <pattern> ... ] |
| 602 |
|
| 603 |
DESCRIPTION |
| 604 |
|
| 605 |
Extracts backtraces and lock tables from Asterisk coredump files. |
| 606 |
For each coredump found, 5 new result files are created: |
| 607 |
- <coredump>-brief.txt: The output of "thread apply all bt". |
| 608 |
|
| 609 |
- <coredump>-full.txt: The output of "thread apply all bt full". |
| 610 |
|
| 611 |
- <coredump>-info.txt: State info like taskprocessors, channels, etc |
| 612 |
|
| 613 |
- <coredump>-locks.txt: If asterisk was compiled with |
| 614 |
"DEBUG_THREADS", this file will contain a dump of the locks |
| 615 |
table similar to doing a "core show locks" from the asterisk |
| 616 |
CLI. |
| 617 |
|
| 618 |
- <coredump>-thread1.txt: The output of "thread apply 1 bt full". |
| 619 |
|
| 620 |
Options: |
| 621 |
|
| 622 |
--help |
| 623 |
Print this help. |
| 624 |
|
| 625 |
--running |
| 626 |
Create a coredump from the running asterisk instance and |
| 627 |
process it. |
| 628 |
WARNING: This WILL interrupt call processing. You will be |
| 629 |
asked to confirm. |
| 630 |
|
| 631 |
--RUNNING |
| 632 |
Same as --running but without the confirmation prompt. |
| 633 |
DANGEROUS!! |
| 634 |
|
| 635 |
--pid=<asterisk main process pid> |
| 636 |
If you are trying to get a dump of the running asterisk |
| 637 |
instance, specifying its pid on the command line will |
| 638 |
bypass the complex logic used to figure it out. |
| 639 |
|
| 640 |
--latest |
| 641 |
Process only the latest coredump from those specified (based |
| 642 |
on last-modified time). Only needed when --running was not |
| 643 |
specified and there is more that one coredump matched. |
| 644 |
|
| 645 |
--outputdir=<output directory> |
| 646 |
The directory into which output products will be saved. |
| 647 |
Default: same directory as coredump |
| 648 |
|
| 649 |
--asterisk-bin=<path to asterisk binary that created the coredump> |
| 650 |
You should only need to use this if the asterisk binary on |
| 651 |
the system has changed since the coredump was generated. |
| 652 |
In this case, the symbols won't be valid and the coredump |
| 653 |
will be useless. If you can recreate the binary with |
| 654 |
the exact same source code and compile options, or you have |
| 655 |
a saved version, you can use this option to use that binary |
| 656 |
instead. |
| 657 |
Default: executable path extracted from coredump |
| 658 |
|
| 659 |
--moddir=<path to asterisk modules directory> |
| 660 |
You should only need to use this for the same reason you'd |
| 661 |
need to use --asterisk-bin. |
| 662 |
Default: "astmoddir" directory extracted from coredump |
| 663 |
|
| 664 |
--libdir=<path to directory containing libasterisk* libraries> |
| 665 |
You should only need to use this for the same reason you'd |
| 666 |
need to use --asterisk-bin. |
| 667 |
Default: libdir extracted from coredump |
| 668 |
|
| 669 |
--gdb=<path_to_gdb> |
| 670 |
gdb must have python support built-in. Most do. |
| 671 |
Default: /usr/bin/gdb |
| 672 |
|
| 673 |
--dateformat=<date options> |
| 674 |
Passed to the 'date' utility to construct dates. |
| 675 |
The default is '-u +%FT%H-%M-%SZ' which results |
| 676 |
in a UTC timestamp. |
| 677 |
|
| 678 |
--rename |
| 679 |
Causes the coredump to be renamed using DATEOPTS |
| 680 |
and the output files to be named accordingly. |
| 681 |
This is the default. To disable renaming, specify |
| 682 |
--no-rename |
| 683 |
|
| 684 |
--tarball-coredumps |
| 685 |
Creates a gzipped tarball of each coredump processed, their |
| 686 |
results txt files, a copy of /etc/os-release, the |
| 687 |
asterisk binary, and all modules. |
| 688 |
The file will be named like the coredump with '.tar.gz' |
| 689 |
appended. |
| 690 |
WARNING: This file could be quite large! |
| 691 |
Mutually exclusive with --tarball-results |
| 692 |
|
| 693 |
--tarball-config |
| 694 |
Adds the contents of /etc/asterisk to the tarball created |
| 695 |
with --tarball-coredumps. |
| 696 |
WARNING: This may include confidential information like |
| 697 |
secrets or keys. |
| 698 |
|
| 699 |
--etcdir=<path to directory asterisk config files> |
| 700 |
If you use --tarball-config and the config files that |
| 701 |
match this coredump are in a location other than that which |
| 702 |
was specified in "astetcdir" in asterisk.conf, you can use |
| 703 |
this option to point to their current location. |
| 704 |
Default: "astetcdir" extracted from coredump. |
| 705 |
|
| 706 |
--delete-coredumps-after |
| 707 |
Deletes all processed coredumps regardless of whether |
| 708 |
a tarball was created. |
| 709 |
|
| 710 |
--tarball-results |
| 711 |
Creates a gzipped tarball of all result files produced. |
| 712 |
The tarball name will be: |
| 713 |
$OUTPUTDIR/asterisk.<timestamp>.results.tar.gz |
| 714 |
Mutually exclusive with --tarball-coredumps |
| 715 |
|
| 716 |
--delete-results-after |
| 717 |
Deletes all processed results regardless of whether |
| 718 |
a tarball was created. It probably does not make sense |
| 719 |
to use this option unless you have also specified |
| 720 |
--tarball-results. |
| 721 |
|
| 722 |
<coredump> | <pattern> |
| 723 |
A list of coredumps or coredump search patterns. These |
| 724 |
will override the default of "/tmp/core!(*.txt)" |
| 725 |
|
| 726 |
The "!(*.txt)" tells bash to ignore any files that match |
| 727 |
the base pattern and end in ".txt". It$'s not strictly |
| 728 |
needed as non asterisk coredumps are always ignored. |
| 729 |
|
| 730 |
NOTES |
| 731 |
You must be root to use this program. |
| 732 |
|
| 733 |
All options except "running", "RUNNING" and "pid" can be |
| 734 |
specified in the ast_debug_tools.conf file. |
| 735 |
Option names must be translated to upper case and their '-' |
| 736 |
characters replaced by '_'. Boolean options must be set to |
| 737 |
'true' or 'false' (lower case, without the quotes). |
| 738 |
Examples: |
| 739 |
TARBALL_RESULTS=true |
| 740 |
RENAME=false |
| 741 |
|
| 742 |
The script relies on not only bash, but also recent GNU date and |
| 743 |
gdb with python support. *BSD operating systems may require |
| 744 |
installation of the 'coreutils' and 'devel/gdb' packages and minor |
| 745 |
tweaking of the ast_debug_tools.conf file. |
| 746 |
|
| 747 |
Any files output will have ':' characters changed to '-'. This is |
| 748 |
to facilitate uploading those files to Jira which doesn't like the |
| 749 |
colons. |
| 750 |
|
| 751 |
FILES |
| 752 |
/etc/asterisk/ast_debug_tools.conf |
| 753 |
~/ast_debug_tools.conf |
| 754 |
./ast_debug_tools.conf |
| 755 |
|
| 756 |
See the configs/samples/ast_debug_tools.conf file in the asterisk |
| 757 |
source tree for more info. |
| 758 |
|
| 759 |
EOF |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
python |
| 774 |
|
| 775 |
import datetime |
| 776 |
|
| 777 |
|
| 778 |
def timeval_to_datetime(value): |
| 779 |
"""Convert a timeval struct to a python datetime object |
| 780 |
|
| 781 |
Args: |
| 782 |
value: A gdb Value representing a C timeval |
| 783 |
|
| 784 |
Return: |
| 785 |
A python datetime object |
| 786 |
""" |
| 787 |
|
| 788 |
sec = int(value['tv_sec']) |
| 789 |
usec = int(value['tv_usec']) |
| 790 |
|
| 791 |
return datetime.datetime.fromtimestamp(sec + usec / float(1000000)) |
| 792 |
|
| 793 |
|
| 794 |
def s_strip(value): |
| 795 |
"""Convert the given value to a string, and strip any leading/trailing |
| 796 |
spaces and/or quotes. |
| 797 |
|
| 798 |
Args: |
| 799 |
name: The gdb Value to convert and strip |
| 800 |
|
| 801 |
Return: |
| 802 |
The stripped value as a string |
| 803 |
""" |
| 804 |
|
| 805 |
if value == None: |
| 806 |
return "None" |
| 807 |
|
| 808 |
try: |
| 809 |
if 'char *' not in str(value.type) and 'char [' not in str(value.type): |
| 810 |
# Use the string method for everything but string pointers (only |
| 811 |
|
| 812 |
return value.string().strip('" ') or "<None>" |
| 813 |
except: |
| 814 |
pass |
| 815 |
|
| 816 |
try: |
| 817 |
if value.type.code in [ gdb.TYPE_CODE_ARRAY, gdb.TYPE_CODE_PTR ]: |
| 818 |
return value.string() |
| 819 |
except: |
| 820 |
pass |
| 821 |
|
| 822 |
return str(value).strip('" ') or "<None>" |
| 823 |
|
| 824 |
|
| 825 |
def get(name): |
| 826 |
"""Retrieve a named variable's value as a string using GDB. |
| 827 |
|
| 828 |
Args: |
| 829 |
name: The name of the variable to look up |
| 830 |
|
| 831 |
Return: |
| 832 |
The variable's value as a string |
| 833 |
""" |
| 834 |
|
| 835 |
return s_strip(gdb.parse_and_eval(name)) |
| 836 |
|
| 837 |
|
| 838 |
def get_container_hash_objects(name, type, on_object=None): |
| 839 |
"""Retrieve a list of objects from an ao2_container_hash. |
| 840 |
|
| 841 |
Expected on_object signature: |
| 842 |
|
| 843 |
res, stop = on_object(GDB Value) |
| 844 |
|
| 845 |
The given callback, on_object, is called for each object found in the |
| 846 |
container. The callback is passed a dereferenced GDB Value object and |
| 847 |
expects an object to be returned, which is then appended to a list of |
| 848 |
objects to be returned by this function. Iteration can be stopped by |
| 849 |
returning "True" for the second return value. |
| 850 |
|
| 851 |
If on_object is not specified then the dereferenced GDB value is instead |
| 852 |
added directly to the returned list. |
| 853 |
|
| 854 |
Args: |
| 855 |
name: The name of the ao2_container |
| 856 |
type: The type of objects stored in the container |
| 857 |
on_object: Optional function called on each object found |
| 858 |
|
| 859 |
Return: |
| 860 |
A list of container objects |
| 861 |
""" |
| 862 |
|
| 863 |
objs = [] |
| 864 |
|
| 865 |
try: |
| 866 |
|
| 867 |
container = gdb.parse_and_eval(name).cast( |
| 868 |
gdb.lookup_type('struct ao2_container_hash').pointer()) |
| 869 |
|
| 870 |
|
| 871 |
for n in range(container['n_buckets']): |
| 872 |
node = container['buckets'][n]['list']['last'] |
| 873 |
while node: |
| 874 |
|
| 875 |
obj = node.dereference()['common']['obj'].cast( |
| 876 |
gdb.lookup_type(type).pointer()).dereference() |
| 877 |
|
| 878 |
res, stop = on_object(obj) if on_object else (obj, False) |
| 879 |
|
| 880 |
if res: |
| 881 |
objs.append(res) |
| 882 |
|
| 883 |
if stop: |
| 884 |
return objs |
| 885 |
|
| 886 |
node = node.dereference()['links']['last'] |
| 887 |
except Exception as e: |
| 888 |
print("{0} - {1}".format(name, e)) |
| 889 |
pass |
| 890 |
|
| 891 |
return objs |
| 892 |
|
| 893 |
|
| 894 |
def get_container_rbtree_objects(name, type, on_object=None): |
| 895 |
"""Retrieve a list of objects from an ao2_container_rbtree. |
| 896 |
|
| 897 |
Expected on_object signature: |
| 898 |
|
| 899 |
res, stop = on_object(GDB Value) |
| 900 |
|
| 901 |
The given callback, on_object, is called for each object found in the |
| 902 |
container. The callback is passed a dereferenced GDB Value object and |
| 903 |
expects an object to be returned, which is then appended to a list of |
| 904 |
objects to be returned by this function. Iteration can be stopped by |
| 905 |
returning "True" for the second return value. |
| 906 |
|
| 907 |
If on_object is not specified then the dereferenced GDB value is instead |
| 908 |
added directly to the returned list. |
| 909 |
|
| 910 |
Args: |
| 911 |
name: The name of the ao2_container |
| 912 |
type: The type of objects stored in the container |
| 913 |
on_object: Optional function called on each object found |
| 914 |
|
| 915 |
Return: |
| 916 |
A list of container objects |
| 917 |
""" |
| 918 |
|
| 919 |
objs = [] |
| 920 |
|
| 921 |
def handle_node(node): |
| 922 |
|
| 923 |
if not node: |
| 924 |
return True |
| 925 |
|
| 926 |
# Each node holds the needed object |
| 927 |
obj = node.dereference()['common']['obj'].cast( |
| 928 |
gdb.lookup_type(type).pointer()).dereference() |
| 929 |
|
| 930 |
res, stop = on_object(obj) if on_object else (obj, False) |
| 931 |
|
| 932 |
if res: |
| 933 |
objs.append(res) |
| 934 |
|
| 935 |
return not stop and (handle_node(node['left']) and |
| 936 |
handle_node(node['right'])) |
| 937 |
|
| 938 |
try: |
| 939 |
container = gdb.parse_and_eval(name).cast( |
| 940 |
gdb.lookup_type('struct ao2_container_rbtree').pointer()) |
| 941 |
|
| 942 |
handle_node(container['root']) |
| 943 |
except Exception as e: |
| 944 |
print("{0} - {1}".format(name, e)) |
| 945 |
pass |
| 946 |
|
| 947 |
return objs |
| 948 |
|
| 949 |
def get_container_count(name): |
| 950 |
return int(gdb.parse_and_eval(name).dereference()['elements']) |
| 951 |
|
| 952 |
|
| 953 |
def get_container_map_objects(name, type, on_object=None): |
| 954 |
"""Retrieve a list of objects from a C++ map. |
| 955 |
|
| 956 |
Expected on_object signature: |
| 957 |
|
| 958 |
res, stop = on_object(GDB Value) |
| 959 |
|
| 960 |
The given callback, on_object, is called for each object found in the |
| 961 |
map. The callback is passed a dereferenced GDB Value object and |
| 962 |
expects an object to be returned, which is then appended to a list of |
| 963 |
objects to be returned by this function. Iteration can be stopped by |
| 964 |
returning "True" for the second return value. |
| 965 |
|
| 966 |
If on_object is not specified then the dereferenced GDB value is instead |
| 967 |
added directly to the returned list. |
| 968 |
|
| 969 |
Args: |
| 970 |
name: The name of the map |
| 971 |
type: The type of objects stored in the container |
| 972 |
on_object: Optional function called on each object found |
| 973 |
|
| 974 |
Return: |
| 975 |
A list of map objects |
| 976 |
""" |
| 977 |
objs = [] |
| 978 |
map = gdb.parse_and_eval(name) |
| 979 |
node = map['_M_t']['_M_impl']['_M_header']['_M_left'] |
| 980 |
tree_size = map['_M_t']['_M_impl']['_M_node_count'] |
| 981 |
for i in range(0, tree_size): |
| 982 |
obj_node = (node + 2) |
| 983 |
obj_p = obj_node.cast(gdb.lookup_type(type).pointer().pointer()).dereference() |
| 984 |
obj = obj_p.dereference() |
| 985 |
res, stop = on_object(obj) if on_object else (obj, False) |
| 986 |
if res: |
| 987 |
objs.append(res) |
| 988 |
if stop: |
| 989 |
return objs |
| 990 |
|
| 991 |
if node['_M_right'] != 0: |
| 992 |
node = node['_M_right'] |
| 993 |
while node['_M_left'] != 0: |
| 994 |
node = node['_M_left'] |
| 995 |
else: |
| 996 |
tmp_node = node['_M_parent'] |
| 997 |
while node == tmp_node['_M_right']: |
| 998 |
node = tmp_node |
| 999 |
tmp_node = tmp_node['_M_parent'] |
| 1000 |
if node['_M_right'] != tmp_node: |
| 1001 |
node = tmp_node |
| 1002 |
return objs |
| 1003 |
|
| 1004 |
def get_map_count(name): |
| 1005 |
map = gdb.parse_and_eval(name) |
| 1006 |
return map['_M_t']['_M_impl']['_M_node_count'] |
| 1007 |
|
| 1008 |
def build_info(): |
| 1009 |
|
| 1010 |
try: |
| 1011 |
return ("Asterisk {0} built by {1} @ {2} on a {3} running {4} on {5}" |
| 1012 |
.format(get("asterisk_version"), |
| 1013 |
get("ast_build_user"), |
| 1014 |
get("ast_build_hostname"), |
| 1015 |
get("ast_build_machine"), |
| 1016 |
get("ast_build_os"), |
| 1017 |
get("ast_build_date"))) |
| 1018 |
except: |
| 1019 |
return "Unable to retrieve build info" |
| 1020 |
|
| 1021 |
|
| 1022 |
def build_opts(): |
| 1023 |
|
| 1024 |
try: |
| 1025 |
return get("asterisk_build_opts") |
| 1026 |
except: |
| 1027 |
return "Unable to retrieve build options" |
| 1028 |
|
| 1029 |
|
| 1030 |
def uptime(): |
| 1031 |
|
| 1032 |
try: |
| 1033 |
started = timeval_to_datetime(gdb.parse_and_eval("ast_startuptime")) |
| 1034 |
loaded = timeval_to_datetime(gdb.parse_and_eval("ast_lastreloadtime")) |
| 1035 |
|
| 1036 |
return ("System started: {0}\n" |
| 1037 |
"Last reload: {1}".format(started, loaded)) |
| 1038 |
except: |
| 1039 |
return "Unable to retrieve uptime" |
| 1040 |
|
| 1041 |
|
| 1042 |
class TaskProcessor(object): |
| 1043 |
|
| 1044 |
template = ("{name:70} {processed:>10} {in_queue:>10} {max_depth:>10} " |
| 1045 |
"{low_water:>10} {high_water:>10}") |
| 1046 |
|
| 1047 |
header = {'name': 'Processor', 'processed': 'Processed', |
| 1048 |
'in_queue': 'In Queue', 'max_depth': 'Max Depth', |
| 1049 |
'low_water': 'Low water', 'high_water': 'High water'} |
| 1050 |
|
| 1051 |
@staticmethod |
| 1052 |
def objects(): |
| 1053 |
|
| 1054 |
try: |
| 1055 |
objs = get_container_hash_objects('tps_singletons', |
| 1056 |
'struct ast_taskprocessor', TaskProcessor.from_value) |
| 1057 |
|
| 1058 |
objs.sort(key=lambda x: x.name.lower()) |
| 1059 |
except Exception as e: |
| 1060 |
return [] |
| 1061 |
|
| 1062 |
return objs |
| 1063 |
|
| 1064 |
@staticmethod |
| 1065 |
def from_value(value): |
| 1066 |
|
| 1067 |
return TaskProcessor( |
| 1068 |
value['name'], |
| 1069 |
value['stats']['_tasks_processed_count'], |
| 1070 |
value['tps_queue_size'], |
| 1071 |
value['stats']['max_qsize'], |
| 1072 |
value['tps_queue_low'], |
| 1073 |
value['tps_queue_high']), False |
| 1074 |
|
| 1075 |
def __init__(self, name, processed, in_queue, max_depth, |
| 1076 |
low_water, high_water): |
| 1077 |
|
| 1078 |
self.name = str(name).strip('"') |
| 1079 |
self.processed = int(processed) |
| 1080 |
self.in_queue = int(in_queue) |
| 1081 |
self.max_depth = int(max_depth) |
| 1082 |
self.low_water = int(low_water) |
| 1083 |
self.high_water = int(high_water) |
| 1084 |
|
| 1085 |
|
| 1086 |
class Channel(object): |
| 1087 |
|
| 1088 |
template = ("{name:42} {context:>20} {exten:>20} {priority:>10} {state:>25} " |
| 1089 |
"{app:>20} {data:>30} {caller_id:>15} {created:>30} " |
| 1090 |
"{account_code:>15} {peer_account:>15} {bridge_id:>38}") |
| 1091 |
|
| 1092 |
header = {'name': 'Channel', 'context': 'Context', 'exten': 'Extension', |
| 1093 |
'priority': 'Priority', 'state': "State", 'app': 'Application', |
| 1094 |
'data': 'Data', 'caller_id': 'CallerID', 'created': 'Created', |
| 1095 |
'account_code': 'Accountcode', 'peer_account': 'PeerAccount', |
| 1096 |
'bridge_id': 'BridgeID'} |
| 1097 |
|
| 1098 |
container = 'current_channel_storage_instance->handle->handle' |
| 1099 |
map = '(((struct mni_channelstorage_driver_pvt *)current_channel_storage_instance->handle)->by_name)' |
| 1100 |
|
| 1101 |
@staticmethod |
| 1102 |
def objects(): |
| 1103 |
|
| 1104 |
try: |
| 1105 |
driver = gdb.parse_and_eval("current_channel_storage_driver") |
| 1106 |
driver_name = driver['driver_name'].string() |
| 1107 |
if driver_name == "cpp_map_name_id": |
| 1108 |
objs = get_container_map_objects(Channel.map, |
| 1109 |
'struct ast_channel', Channel.from_value) |
| 1110 |
else: |
| 1111 |
objs = get_container_hash_objects(Channel.container, |
| 1112 |
'struct ast_channel', Channel.from_value) |
| 1113 |
objs.sort(key=lambda x: x.name.lower()) |
| 1114 |
except: |
| 1115 |
return [] |
| 1116 |
|
| 1117 |
return objs |
| 1118 |
|
| 1119 |
@staticmethod |
| 1120 |
def from_value(value): |
| 1121 |
|
| 1122 |
bridge_id = None |
| 1123 |
if value['bridge']: |
| 1124 |
bridge_id = value['bridge']['uniqueid'] |
| 1125 |
|
| 1126 |
return Channel( |
| 1127 |
value['name'], |
| 1128 |
value['context'], |
| 1129 |
value['exten'], |
| 1130 |
value['priority'], |
| 1131 |
value['state'], |
| 1132 |
value['appl'], |
| 1133 |
value['data'], |
| 1134 |
value['caller']['id']['number']['str'], |
| 1135 |
timeval_to_datetime(value['creationtime']), |
| 1136 |
value['accountcode'], |
| 1137 |
value['peeraccount'], |
| 1138 |
bridge_id), False |
| 1139 |
|
| 1140 |
@staticmethod |
| 1141 |
def summary(): |
| 1142 |
driver = gdb.parse_and_eval("current_channel_storage_driver") |
| 1143 |
driver_name = driver['driver_name'].string() |
| 1144 |
if driver_name == "cpp_map_name_id": |
| 1145 |
count = get_map_count(Channel.map) |
| 1146 |
else: |
| 1147 |
count = get_container_count(Channel.container) |
| 1148 |
|
| 1149 |
try: |
| 1150 |
return ("Channel Driver Name: {0}\n" |
| 1151 |
"{1} active channels\n" |
| 1152 |
"{2} active calls\n" |
| 1153 |
"{3} calls processed".format(driver_name, |
| 1154 |
count, |
| 1155 |
get("countcalls"), |
| 1156 |
get("totalcalls"))) |
| 1157 |
except: |
| 1158 |
return "Unable to retrieve channel summary" |
| 1159 |
|
| 1160 |
def __init__(self, name, context=None, exten=None, priority=None, |
| 1161 |
state=None, app=None, data=None, caller_id=None, |
| 1162 |
created=None, account_code=None, peer_account=None, |
| 1163 |
bridge_id=None): |
| 1164 |
|
| 1165 |
self.name = s_strip(name) |
| 1166 |
self.context = s_strip(context) |
| 1167 |
self.exten = s_strip(exten) |
| 1168 |
self.priority = int(priority) |
| 1169 |
self.state = s_strip(state) |
| 1170 |
self.app = s_strip(app) |
| 1171 |
self.data = s_strip(data) |
| 1172 |
self.caller_id = s_strip(caller_id) |
| 1173 |
self.created = s_strip(created) |
| 1174 |
self.account_code = s_strip(account_code) |
| 1175 |
self.peer_account = s_strip(peer_account) |
| 1176 |
self.bridge_id = s_strip(bridge_id) |
| 1177 |
|
| 1178 |
|
| 1179 |
class Bridge(object): |
| 1180 |
|
| 1181 |
template = ("{uniqueid:38} {num_channels:>15} {subclass:>10} {tech:>20} " |
| 1182 |
"{created:>30}") |
| 1183 |
|
| 1184 |
header = {'uniqueid': 'Bridge-ID', 'num_channels': 'Chans', |
| 1185 |
'subclass': 'Type', 'tech': 'Technology', 'created': 'Created'} |
| 1186 |
|
| 1187 |
@staticmethod |
| 1188 |
def objects(): |
| 1189 |
|
| 1190 |
try: |
| 1191 |
objs = get_container_rbtree_objects('bridges', |
| 1192 |
'struct ast_bridge', Bridge.from_value) |
| 1193 |
|
| 1194 |
objs.sort(key=lambda x: x.uniqueid.lower()) |
| 1195 |
except: |
| 1196 |
return [] |
| 1197 |
|
| 1198 |
return objs |
| 1199 |
|
| 1200 |
@staticmethod |
| 1201 |
def from_value(value): |
| 1202 |
|
| 1203 |
return Bridge( |
| 1204 |
value['uniqueid'], |
| 1205 |
value['num_channels'], |
| 1206 |
timeval_to_datetime(value['creationtime']), |
| 1207 |
value['v_table']['name'], |
| 1208 |
value['technology']['name']), False |
| 1209 |
|
| 1210 |
|
| 1211 |
def __init__(self, uniqueid, num_channels=None, created=None, subclass=None, |
| 1212 |
tech=None): |
| 1213 |
|
| 1214 |
self.uniqueid = s_strip(uniqueid) |
| 1215 |
self.num_channels = int(num_channels) |
| 1216 |
self.created = s_strip(created) |
| 1217 |
self.subclass = s_strip(subclass) |
| 1218 |
self.tech = s_strip(tech) |
| 1219 |
|
| 1220 |
|
| 1221 |
class DumpAsteriskCommand(gdb.Command): |
| 1222 |
|
| 1223 |
def __init__(self): |
| 1224 |
super(DumpAsteriskCommand, self).__init__ ("dump-asterisk", |
| 1225 |
gdb.COMMAND_OBSCURE, gdb.COMPLETE_COMMAND) |
| 1226 |
|
| 1227 |
def print_table(self, type): |
| 1228 |
|
| 1229 |
plural = "{0}s".format(type.__name__) |
| 1230 |
|
| 1231 |
objs = type.objects() |
| 1232 |
|
| 1233 |
if not len(objs): |
| 1234 |
print("{0} not found\n".format(plural)) |
| 1235 |
return |
| 1236 |
|
| 1237 |
print("{0} ({1}):\n".format(plural, len(objs))) |
| 1238 |
|
| 1239 |
print(type.template.format(**type.header)) |
| 1240 |
|
| 1241 |
for obj in objs: |
| 1242 |
print(type.template.format(**vars(obj))) |
| 1243 |
|
| 1244 |
print("\n") |
| 1245 |
|
| 1246 |
def invoke(self, arg, from_tty): |
| 1247 |
try: |
| 1248 |
gdb.execute("interrupt", from_tty) |
| 1249 |
except: |
| 1250 |
pass |
| 1251 |
print("!@!@!@! thread1.txt !@!@!@!\n") |
| 1252 |
try: |
| 1253 |
gdb.execute("p $_siginfo", from_tty) |
| 1254 |
gdb.execute("info signal $_siginfo.si_signo") |
| 1255 |
except: |
| 1256 |
pass |
| 1257 |
try: |
| 1258 |
gdb.execute("thread apply 1 bt full", from_tty) |
| 1259 |
except: |
| 1260 |
pass |
| 1261 |
print("!@!@!@! brief.txt !@!@!@!\n") |
| 1262 |
try: |
| 1263 |
gdb.execute("p $_siginfo", from_tty) |
| 1264 |
gdb.execute("info signal $_siginfo.si_signo") |
| 1265 |
except: |
| 1266 |
pass |
| 1267 |
try: |
| 1268 |
gdb.execute("thread apply all bt", from_tty) |
| 1269 |
except: |
| 1270 |
pass |
| 1271 |
print("!@!@!@! full.txt !@!@!@!\n") |
| 1272 |
try: |
| 1273 |
gdb.execute("p $_siginfo", from_tty) |
| 1274 |
gdb.execute("info signal $_siginfo.si_signo") |
| 1275 |
except: |
| 1276 |
pass |
| 1277 |
try: |
| 1278 |
gdb.execute("thread apply all bt full", from_tty) |
| 1279 |
except: |
| 1280 |
pass |
| 1281 |
print("!@!@!@! locks.txt !@!@!@!\n") |
| 1282 |
try: |
| 1283 |
gdb.execute("p $_siginfo", from_tty) |
| 1284 |
gdb.execute("info signal $_siginfo.si_signo") |
| 1285 |
except: |
| 1286 |
pass |
| 1287 |
try: |
| 1288 |
gdb.execute("show_locks", from_tty) |
| 1289 |
except: |
| 1290 |
pass |
| 1291 |
|
| 1292 |
print("!@!@!@! info.txt !@!@!@!\n") |
| 1293 |
|
| 1294 |
gdb.execute('set print addr off') |
| 1295 |
|
| 1296 |
try: |
| 1297 |
print("{0}\n".format(build_info())) |
| 1298 |
print("{0}\n".format(uptime())) |
| 1299 |
print("Build options = {0}\n".format(build_opts())) |
| 1300 |
|
| 1301 |
self.print_table(TaskProcessor) |
| 1302 |
self.print_table(Bridge) |
| 1303 |
self.print_table(Channel) |
| 1304 |
|
| 1305 |
print(Channel.summary()) |
| 1306 |
except: |
| 1307 |
pass |
| 1308 |
finally: |
| 1309 |
gdb.execute('set print addr on') |
| 1310 |
|
| 1311 |
try: |
| 1312 |
gdb.execute("continue", from_tty) |
| 1313 |
except: |
| 1314 |
pass |
| 1315 |
|
| 1316 |
DumpAsteriskCommand () |
| 1317 |
end |
| 1318 |
|
| 1319 |
define show_locks |
| 1320 |
set $n = lock_infos.first |
| 1321 |
|
| 1322 |
if $argc == 0 |
| 1323 |
printf "%s\n", " where_held count-|" |
| 1324 |
printf "%s\n", " suspended-| |" |
| 1325 |
printf "%s\n", " type-| times locked-| | |" |
| 1326 |
printf "%-14s %-36s %6s %-42s %-8s %-36s %3s %-14s %3s %3s %3s\n",\ |
| 1327 |
"thread","file","line","function","status","lock name","|","lock addr","|","|","|" |
| 1328 |
printf "%s\n", "----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------" |
| 1329 |
|
| 1330 |
else |
| 1331 |
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" |
| 1332 |
end |
| 1333 |
|
| 1334 |
while $n |
| 1335 |
if $n->num_locks > 0 |
| 1336 |
set $i = 0 |
| 1337 |
while $i < $n->num_locks |
| 1338 |
if $n->locks[$i]->suspended != 47 |
| 1339 |
if $n->locks[$i]->pending > 0 |
| 1340 |
set $status = "waiting" |
| 1341 |
end |
| 1342 |
if $n->locks[$i]->pending < 0 |
| 1343 |
set $status = "failed" |
| 1344 |
end |
| 1345 |
if $n->locks[$i]->pending == 0 |
| 1346 |
set $status = "holding" |
| 1347 |
end |
| 1348 |
|
| 1349 |
if $n->locks[$i]->type == 0 |
| 1350 |
set $ltype = "M" |
| 1351 |
end |
| 1352 |
if $n->locks[$i]->type == 1 |
| 1353 |
set $ltype = "RD" |
| 1354 |
end |
| 1355 |
if $n->locks[$i]->type == 2 |
| 1356 |
set $ltype = "WR" |
| 1357 |
end |
| 1358 |
|
| 1359 |
if ((ast_mutex_t *)$n->locks[$i]->lock_addr)->track |
| 1360 |
if $n->locks[$i]->type > 0 |
| 1361 |
set $track = ((ast_rwlock_t *)$n->locks[$i]->lock_addr)->track |
| 1362 |
else |
| 1363 |
set $track = ((ast_mutex_t *)$n->locks[$i]->lock_addr)->track |
| 1364 |
end |
| 1365 |
end |
| 1366 |
if $track |
| 1367 |
set $reentrancy = $track->reentrancy |
| 1368 |
else |
| 1369 |
set $reentrancy = 0 |
| 1370 |
end |
| 1371 |
set $pending = $n->locks[$i]->pending |
| 1372 |
if $argc > 0 |
| 1373 |
printf "%p,%d,%s,%d,%s,%s,%d,%p,%d,%d,%d",\ |
| 1374 |
$n->thread_id, $n->locks[$i]->pending, $n->locks[$i]->file, $n->locks[$i]->line_num, $n->locks[$i]->func,\ |
| 1375 |
$n->locks[$i]->lock_name, $n->locks[$i]->type, $n->locks[$i]->lock_addr, $n->locks[$i]->times_locked,\ |
| 1376 |
$n->locks[$i]->suspended, $reentrancy |
| 1377 |
if $reentrancy && $pending |
| 1378 |
printf ",%s,%d,%s,%p", $track->file[0], $track->lineno[0], $track->func[0], $track->thread[0] |
| 1379 |
end |
| 1380 |
else |
| 1381 |
printf "%14p %-36s %6d %-42s %-8s %-36s %3s %-14p %3d %3d %3d", \ |
| 1382 |
$n->thread_id, $n->locks[$i]->file, $n->locks[$i]->line_num, $n->locks[$i]->func, $status, \ |
| 1383 |
$n->locks[$i]->lock_name, $ltype, $n->locks[$i]->lock_addr, $n->locks[$i]->times_locked,\ |
| 1384 |
$n->locks[$i]->suspended, $reentrancy |
| 1385 |
if $reentrancy && $pending |
| 1386 |
printf "\n held at: %s:%d %s() by 0x%08lx", \ |
| 1387 |
$track->file[0], $track->lineno[0], $track->func[0], $track->thread_id[0] |
| 1388 |
end |
| 1389 |
end |
| 1390 |
printf "\n" |
| 1391 |
end |
| 1392 |
set $i = $i + 1 |
| 1393 |
end |
| 1394 |
end |
| 1395 |
set $n = $n->entry->next |
| 1396 |
end |
| 1397 |
end |