Immer mehr Skripte für administrative Aufgaben auf Linux-Betriebssystemen werden von Python geschrieben. Dieser Beitrag zielt darauf ab, ein Tool zum Verfolgen der Ausführung von Python-Anweisungen vorzustellen. Python ist eine dynamische objektorientierte Programmiersprache, mit der verschiedene Arten von Software entwickelt werden können. Es bietet eine starke Unterstützung für die Integration mit anderen Sprachen und Tools und wird mit umfangreichen Standardbibliotheken geliefert. In Linux-Distributionen wird Python häufig zum Schreiben von Verwaltungswerkzeugen wie Druckerkonfigurationspaketen usw. verwendet.
Das Verfolgen der Ausführung von Python-Anweisungen und das zeilenweise Aufzeichnen aller ausgeführten Codes ist sehr nützlich, um die Ursache eines Problems effizient zu lokalisieren.
Glücklicherweise enthält das Python-Paket ein Tool trace.py , die verwendet werden können, um diese Anforderung zu erfüllen. Die trace.py befindet sich im Verzeichnis /user/lib/python2.x, wobei python2.x die Python-Version ist (z. B. python2.3 und python2.4 usw.)
# rpm -ql python |grep trace.py /usr/lib/python2.3/trace.py /usr/lib/python2.3/trace.pyc /usr/lib/python2.3/trace.pyo
Um beispielsweise /usr/sbin/printconf-backend zu verfolgen, lautet der Befehl wie folgt:
# /usr/lib/python2.x/trace.py --trace /usr/sbin/printconf-backend
Es zeigt alle Debug-Informationen und den Quellcode des Python-Skripts auf der Konsole an. Wir können die gesamte Ausgabe wie folgt aufzeichnen.
# script /tmp/printerconf.log # /usr/lib/python2.x/trace.py --trace /usr/sbin/printconf-backend # exit #
Überprüfen Sie dann die /tmp/printerconf.log Datei.
Hinweis :Standardmäßig hat die trace.py keine Ausführungserlaubnis. Daher ist es erforderlich, die Ausführungserlaubnis zu erteilen, bevor die obigen Anweisungen ausgeführt werden.Weitere Optionen von Trace.py
Mit der Option –help werden detaillierte Nutzungsinformationen für trace.py angezeigt. Zum Beispiel:
$ /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).