越来越多的 Linux 操作系统管理任务脚本是由 Python 编写的。
这篇文章旨在介绍一个跟踪 Python 语句执行的工具。
Python 是一种动态的面向对象编程语言,可用于开发各种类型的软件。
它为与其他语言和工具的集成提供了强大的支持,并带有广泛的标准库。
在 Linux 发行版中,Python 被广泛用于编写管理工具,例如打印机配置包等。
跟踪 Python 语句执行并逐行记录所有运行代码对于高效定位问题原因非常有用。
幸运的是,python 包附带了一个工具 trace.py ,可以用来满足这些要求。
trace.py 位于 /user/lib/python2.x 目录中,其中 python2.x 是 python 版本(例如 python2.3 和 python2.4 等)
# rpm -ql python |grep trace.py /usr/lib/python2.3/trace.py /usr/lib/python2.3/trace.pyc /usr/lib/python2.3/trace.pyo
例如跟踪/usr/sbin/printconf-backend,命令如下:
# /usr/lib/python2.x/trace.py --trace /usr/sbin/printconf-backend
它将在控制台上显示所有调试信息和 python 脚本源代码。
我们可以按如下方式记录所有输出。
# script /tmp/printerconf.log # /usr/lib/python2.x/trace.py --trace /usr/sbin/printconf-backend # exit #
然后检查 /tmp/printerconf.log 文件。
注意:默认情况下,trace.py 没有执行权限。
所以在执行上述指令之前需要授予执行权限。
欢迎来到之路教程(on itroad-com)
Trace.py 的更多选项
使用选项 -help 显示 trace.py 的详细使用信息。
例如:
$ /usr/lib/python2.3/trace.py --help
Usage: /usr/lib/python2.3/trace.py [OPTIONS] [ARGS]
Meta-options:
--help Display this help then exit.
--version Output version information then exit.
Otherwise, exactly one of the following three options must be given:
-t, --trace Print each line to sys.stdout before it is executed.
-c, --count Count the number of times each line is executed
and write the counts to .cover for each
module executed, in the module's directory.
See also `--coverdir', `--file', `--no-report' below.
-l, --listfuncs Keep track of which functions are executed at least
once and write the results to sys.stdout after the
program exits.
-r, --report Generate a report from a counts file; do not execute
any code. `--file' must specify the results file to
read, which must have been created in a previous run
with `--count --file=FILE'.
Modifiers:
-f, --file= File to accumulate counts over several runs.
-R, --no-report Do not generate the coverage report files.
Useful if you want to accumulate over several runs.
-C, --coverdir= Directory where the report files. The coverage
report for . is written to file
//.cover.
-m, --missing Annotate executable lines that were not executed
with '>>>>>> '.
-s, --summary Write a brief summary on stdout for each file.
(Can only be used with --count or --report.)
Filters, may be repeated multiple times:
--ignore-module= Ignore the given module and its submodules
(if it is a package).
--ignore-dir= Ignore files in the given directory (multiple
directories can be joined by os.pathsep).
日期:2020-09-17 00:10:52 来源:oir作者:oir
