Linux OS の管理タスク用のスクリプトは、Python で作成されることが増えています。この投稿は、Python ステートメントの実行をトレースするツールを紹介することを目的としています。 Python は、さまざまなタイプのソフトウェアの開発に使用できる動的オブジェクト指向プログラミング言語です。他の言語やツールとの統合を強力にサポートし、広範な標準ライブラリが付属しています。 Linux ディストリビューションでは、プリンター構成パッケージなどの管理ツールを作成するために Python が広く使用されています。
Python ステートメントの実行を追跡し、実行中のすべてのコードを 1 行ずつ記録することは、問題の原因を効率的に特定するのに非常に役立ちます。
幸いなことに、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 には実行権限がありません。そのため、上記の手順を実行する前に実行権限を付与する必要があります。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).