Atividade Python Trabalho4
Atividade Python Trabalho4
processes on a computer system are doing. They both use domain-specific languages
allowing a user to write scripts which:
As of Python 3.6, CPython can be built with embedded “markers”, also known as
“probes”, that can be observed by a DTrace or SystemTap script, making it easier to
monitor what the CPython processes on a system are doing.
$ python3.6 -q &
$ sudo dtrace -l -P python$! # or: dtrace -l -m python3.6
$ readelf -n ./python
python$target:::function-entry
/copyinstr(arg1) == "start"/
{
self->trace = 1;
}
python$target:::function-entry
/self->trace/
{
printf("%d\t%*s:", timestamp, 15, probename);
printf("%*s", self->indent, "");
printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
self->indent++;
}
python$target:::function-return
/self->trace/
{
self->indent--;
printf("%d\t%*s:", timestamp, 15, probename);
printf("%*s", self->indent, "");
printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
}
python$target:::function-return
/copyinstr(arg1) == "start"/
{
self->trace = 0;
}
It can be invoked like this:
156641360502280 function-entry:call_stack.py:start:23
156641360518804 function-entry: call_stack.py:function_1:1
156641360532797 function-entry: call_stack.py:function_3:9
156641360546807 function-return: call_stack.py:function_3:10
156641360563367 function-return: call_stack.py:function_1:2
156641360578365 function-entry: call_stack.py:function_2:5
156641360591757 function-entry: call_stack.py:function_1:1
156641360605556 function-entry: call_stack.py:function_3:9
156641360617482 function-return: call_stack.py:function_3:10
156641360629814 function-return: call_stack.py:function_1:2
156641360642285 function-return: call_stack.py:function_2:6
156641360656770 function-entry: call_stack.py:function_3:9
156641360669707 function-return: call_stack.py:function_3:10
156641360687853 function-entry: call_stack.py:function_4:13
156641360700719 function-return: call_stack.py:function_4:14
156641360719640 function-entry: call_stack.py:function_5:18
156641360732567 function-return: call_stack.py:function_5:21
156641360747370 function-return:call_stack.py:start:28
Static SystemTap markers
The low-level way to use the SystemTap integration is to use the static markers
directly. This requires you to explicitly state the binary file containing them.
For example, this SystemTap script can be used to show the call/return hierarchy of
a Python script:
probe process("python").mark("function__entry") {
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
probe process("python").mark("function__return") {
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
$ stap \
show-call-hierarchy.stp \
-c "./python test.py"
The output looks like this:
name of executable
PID of process
and the remainder indicates the call/return hierarchy as the script executes.
For a –enable-shared build of CPython, the markers are contained within the
libpython shared library, and the probe’s dotted path needs to reflect this. For
example, this line from the above example:
probe process("python").mark("function__entry") {
should instead read:
probe process("python").library("libpython3.6dm.so.1.0").mark("function__entry") {
(assuming a debug build of CPython 3.6)
The filename, function name, and line number are provided back to the tracing
script as positional arguments, which must be accessed using $arg1, $arg2, $arg3:
gc__start(int generation)
Fires when the Python interpreter starts a garbage collection cycle. arg0 is the
generation to scan, like gc.collect().
gc__done(long collected)
Fires when the Python interpreter finishes a garbage collection cycle. arg0 is the
number of collected objects.
import__find__load__start(str modulename)
Fires before importlib attempts to find and load the module. arg0 is the module
name.