HPUX上可用的GDB版本有一个名为"packcore“的命令,该命令创建一个包含核心转储、可执行文件和所有库的tarball。当我尝试在不同的机器上调试核心转储时,我发现这非常有用。
在GDB的标准版本中,有没有我在Linux机器上可能找到的类似命令?
我正在寻找一个简单的命令,当生产机器上出现问题时,不一定是开发人员的人可以运行。
发布于 2013-04-24 00:42:56
下面是一个执行必要步骤的脚本(仅在RHEL5上测试,但也可以在其他地方运行):
#!/bin/sh
#
# Take a core dump and create a tarball of all of the binaries and libraries
# that are needed to debug it.
#
include_core=1
keep_workdir=0
usage()
{
argv0="$1"
retval="$2"
errmsg="$3"
if [ ! -z "$errmsg" ] ; then
echo "ERROR: $errmsg" 1>&2
fi
cat <<EOF
Usage: $argv0 [-k] [-x] <corefile>
Parse a core dump and create a tarball with all binaries and libraries
needed to be able to debug the core dump.
Creates <corefile>.tgz
-k - Keep temporary working directory
-x - Exclude the core dump from the generated tarball
EOF
exit $retval
}
while [ $# -gt 0 ] ; do
case "$1" in
-k)
keep_workdir=1
;;
-x)
include_core=0
;;
-h|--help)
usage "$0" 0
;;
-*)
usage "$0" 1 "Unknown command line arguments: $*"
;;
*)
break
;;
esac
shift
done
COREFILE="$1"
if [ ! -e "$COREFILE" ] ; then
usage "$0" 1 "core dump '$COREFILE' doesn't exist."
fi
case "$(file "$COREFILE")" in
*"core file"*)
break
;;
*)
usage "$0" 1 "per the 'file' command, core dump '$COREFILE' is not a core dump."
;;
esac
cmdname=$(file "$COREFILE" | sed -e"s/.*from '\(.*\)'/\1/")
echo "Command name from core file: $cmdname"
fullpath=$(which "$cmdname")
if [ ! -x "$fullpath" ] ; then
usage "$0" 1 "unable to find command '$cmdname'"
fi
echo "Full path to executable: $fullpath"
mkdir "${COREFILE}.pack"
gdb --eval-command="quit" "${fullpath}" ${COREFILE} 2>&1 | \
grep "Reading symbols" | \
sed -e's/Reading symbols from //' -e's/\.\.\..*//' | \
tar --files-from=- -cf - | (cd "${COREFILE}.pack" && tar xf -)
if [ $include_core -eq 1 ] ; then
cp "${COREFILE}" "${COREFILE}.pack"
fi
tar czf "${COREFILE}.pack.tgz" "${COREFILE}.pack"
if [ $keep_workdir -eq 0 ] ; then
rm -r "${COREFILE}.pack"
fi
echo "Done, created ${COREFILE}.path.tgz"发布于 2011-09-27 01:36:00
核心文件包括从中生成它的命令。理想情况下,这将包括适当可执行文件的完整路径。例如:
$ file core.29529
core.29529: ELF 64-bit LSB core file x86-64, version 1 (SYSV), SVR4-style, from '/bin/sleep 60'在ELF二进制文件上运行ldd将显示它所依赖的库:
$ ldd /bin/sleep
linux-vdso.so.1 => (0x00007fff1d3ff000)
libc.so.6 => /lib64/libc.so.6 (0x0000003d3ce00000)
/lib64/ld-linux-x86-64.so.2 (0x0000003d3ca00000)现在我知道了分析核心转储所需的可执行文件和库。
这里棘手的部分是从core文件中提取可执行路径。似乎没有一个好的工具来直接阅读这篇文章。数据编码为/usr/include/sys/procfs.h结构(来自readelf ),您可以使用prpsinfo查找数据的位置大小
$ readelf -n core.29529
Notes at offset 0x00000468 with length 0x00000558:
Owner Data size Description
CORE 0x00000150 NT_PRSTATUS (prstatus structure)
CORE 0x00000088 NT_PRPSINFO (prpsinfo structure)
CORE 0x00000130 NT_AUXV (auxiliary vector)
CORE 0x00000200 NT_FPREGSET (floating point registers)从理论上讲,...so one可以编写一个代码片段来从这个结构中提取命令行,并以一种使整个过程更容易自动化的方式将其打印出来。当然,您可以只解析file的输出
$ file core.29529 | sed "s/.*from '\([^']*\)'/\1/"
/bin/sleep 60这就是所有的部分。这里有一个将所有这些放在一起的起点:
#!/bin/sh
core=$1
exe=$(file $core | sed "s/.*from '\([^']*\)'/\1/" | awk '{print $1}')
libs=$(
ldd $exe |
awk '
/=> \// {print $3}
! /=>/ {print $1}
'
)
cat <<EOF | tar -cah -T- -f $1-all.tar.xz
$libs
$exe
EOF对于我的示例,如果我将此脚本命名为packcore,并在sleep命令的核心文件上运行它,则会得到以下结果:
$ packcore core.29529
tar: Removing leading `/' from member names
$ tar -c -f core.29529-all.tar.xz
core.29529
lib64/libc.so.6
lib64/ld-linux-x86-64.so.2
bin/sleep就目前而言,这个脚本非常脆弱;我仅根据这个示例输出就对ldd的输出做了很多假设。
发布于 2019-08-03 03:34:51
我已经为此编写了shell script。它使用了上面答案中的想法,并添加了一些用法信息和其他命令。将来,我可能会添加命令,以便使用gdb在docker容器中进行快速调试。
https://stackoverflow.com/questions/7557283
复制相似问题