-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation.html
More file actions
590 lines (546 loc) · 31.6 KB
/
instrumentation.html
File metadata and controls
590 lines (546 loc) · 31.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_TW">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Instrumenting CPython with DTrace and SystemTap — Python 3.7.0 說明文件</title>
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/translations.js"></script>
<script type="text/javascript" src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.7.0 說明文件 中搜尋"
href="../_static/opensearch.xml"/>
<link rel="author" title="關於這些文件" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜尋" href="../search.html" />
<link rel="copyright" title="Copyright" href="../copyright.html" />
<link rel="next" title="Python 常見問題" href="../faq/index.html" />
<link rel="prev" title="Argument Clinic How-To" href="clinic.html" />
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
<link rel="canonical" href="https://docs.python.org/3/howto/instrumentation.html" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/switchers.js"></script>
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="../faq/index.html" title="Python 常見問題"
accesskey="N">下一頁</a> |</li>
<li class="right" >
<a href="clinic.html" title="Argument Clinic How-To"
accesskey="P">上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" accesskey="U">Python HOWTOs</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="instrumenting-cpython-with-dtrace-and-systemtap">
<span id="instrumentation"></span><h1>Instrumenting CPython with DTrace and SystemTap<a class="headerlink" href="#instrumenting-cpython-with-dtrace-and-systemtap" title="本標題的永久連結">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">author:</th><td class="field-body">David Malcolm</td>
</tr>
<tr class="field-even field"><th class="field-name">author:</th><td class="field-body">Łukasz Langa</td>
</tr>
</tbody>
</table>
<p>DTrace and SystemTap are monitoring tools, each providing a way to inspect
what the processes on a computer system are doing. They both use
domain-specific languages allowing a user to write scripts which:</p>
<blockquote>
<div><ul class="simple">
<li>filter which processes are to be observed</li>
<li>gather data from the processes of interest</li>
<li>generate reports on the data</li>
</ul>
</div></blockquote>
<p>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.</p>
<div class="impl-detail compound">
<p><strong>CPython implementation detail:</strong> DTrace markers are implementation details of the CPython interpreter.
No guarantees are made about probe compatibility between versions of
CPython. DTrace scripts can stop working or work incorrectly without
warning when changing CPython versions.</p>
</div>
<div class="section" id="enabling-the-static-markers">
<h2>Enabling the static markers<a class="headerlink" href="#enabling-the-static-markers" title="本標題的永久連結">¶</a></h2>
<p>macOS comes with built-in support for DTrace. On Linux, in order to
build CPython with the embedded markers for SystemTap, the SystemTap
development tools must be installed.</p>
<p>On a Linux machine, this can be done via:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> yum install systemtap-sdt-devel
</pre></div>
</div>
<p>or:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> sudo apt-get install systemtap-sdt-dev
</pre></div>
</div>
<p>CPython must then be configured <code class="docutils literal notranslate"><span class="pre">--with-dtrace</span></code>:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>checking for --with-dtrace... yes
</pre></div>
</div>
<p>On macOS, you can list available DTrace probes by running a Python
process in the background and listing all probes made available by the
Python provider:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> python3.6 -q <span class="p">&</span>
<span class="gp">$</span> sudo dtrace -l -P python<span class="nv">$!</span> <span class="c1"># or: dtrace -l -m python3.6</span>
<span class="go"> ID PROVIDER MODULE FUNCTION NAME</span>
<span class="go">29564 python18035 python3.6 _PyEval_EvalFrameDefault function-entry</span>
<span class="go">29565 python18035 python3.6 dtrace_function_entry function-entry</span>
<span class="go">29566 python18035 python3.6 _PyEval_EvalFrameDefault function-return</span>
<span class="go">29567 python18035 python3.6 dtrace_function_return function-return</span>
<span class="go">29568 python18035 python3.6 collect gc-done</span>
<span class="go">29569 python18035 python3.6 collect gc-start</span>
<span class="go">29570 python18035 python3.6 _PyEval_EvalFrameDefault line</span>
<span class="go">29571 python18035 python3.6 maybe_dtrace_line line</span>
</pre></div>
</div>
<p>On Linux, you can verify if the SystemTap static markers are present in
the built binary by seeing if it contains a 「.note.stapsdt」 section.</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> readelf -S ./python <span class="p">|</span> grep .note.stapsdt
<span class="go">[30] .note.stapsdt NOTE 0000000000000000 00308d78</span>
</pre></div>
</div>
<p>If you’ve built Python as a shared library (with –enable-shared), you
need to look instead within the shared library. For example:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> readelf -S libpython3.3dm.so.1.0 <span class="p">|</span> grep .note.stapsdt
<span class="go">[29] .note.stapsdt NOTE 0000000000000000 00365b68</span>
</pre></div>
</div>
<p>Sufficiently modern readelf can print the metadata:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> readelf -n ./python
<span class="go">Displaying notes found at file offset 0x00000254 with length 0x00000020:</span>
<span class="go"> Owner Data size Description</span>
<span class="go"> GNU 0x00000010 NT_GNU_ABI_TAG (ABI version tag)</span>
<span class="go"> OS: Linux, ABI: 2.6.32</span>
<span class="go">Displaying notes found at file offset 0x00000274 with length 0x00000024:</span>
<span class="go"> Owner Data size Description</span>
<span class="go"> GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)</span>
<span class="go"> Build ID: df924a2b08a7e89f6e11251d4602022977af2670</span>
<span class="go">Displaying notes found at file offset 0x002d6c30 with length 0x00000144:</span>
<span class="go"> Owner Data size Description</span>
<span class="go"> stapsdt 0x00000031 NT_STAPSDT (SystemTap probe descriptors)</span>
<span class="go"> Provider: python</span>
<span class="go"> Name: gc__start</span>
<span class="go"> Location: 0x00000000004371c3, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf6</span>
<span class="go"> Arguments: -4@%ebx</span>
<span class="go"> stapsdt 0x00000030 NT_STAPSDT (SystemTap probe descriptors)</span>
<span class="go"> Provider: python</span>
<span class="go"> Name: gc__done</span>
<span class="go"> Location: 0x00000000004374e1, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf8</span>
<span class="go"> Arguments: -8@%rax</span>
<span class="go"> stapsdt 0x00000045 NT_STAPSDT (SystemTap probe descriptors)</span>
<span class="go"> Provider: python</span>
<span class="go"> Name: function__entry</span>
<span class="go"> Location: 0x000000000053db6c, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6be8</span>
<span class="go"> Arguments: 8@%rbp 8@%r12 -4@%eax</span>
<span class="go"> stapsdt 0x00000046 NT_STAPSDT (SystemTap probe descriptors)</span>
<span class="go"> Provider: python</span>
<span class="go"> Name: function__return</span>
<span class="go"> Location: 0x000000000053dba8, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bea</span>
<span class="go"> Arguments: 8@%rbp 8@%r12 -4@%eax</span>
</pre></div>
</div>
<p>The above metadata contains information for SystemTap describing how it
can patch strategically-placed machine code instructions to enable the
tracing hooks used by a SystemTap script.</p>
</div>
<div class="section" id="static-dtrace-probes">
<h2>Static DTrace probes<a class="headerlink" href="#static-dtrace-probes" title="本標題的永久連結">¶</a></h2>
<p>The following example DTrace script can be used to show the call/return
hierarchy of a Python script, only tracing within the invocation of
a function called 「start」. In other words, import-time function
invocations are not going to be listed:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>self int indent;
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;
}
</pre></div>
</div>
<p>It can be invoked like this:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> sudo dtrace -q -s call_stack.d -c <span class="s2">"python3.6 script.py"</span>
</pre></div>
</div>
<p>The output looks like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>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
</pre></div>
</div>
</div>
<div class="section" id="static-systemtap-markers">
<h2>Static SystemTap markers<a class="headerlink" href="#static-systemtap-markers" title="本標題的永久連結">¶</a></h2>
<p>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.</p>
<p>For example, this SystemTap script can be used to show the call/return
hierarchy of a Python script:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>probe process("python").mark("function__entry") {
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
printf("%s => %s in %s:%d\\n",
thread_indent(1), funcname, filename, lineno);
}
probe process("python").mark("function__return") {
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
printf("%s <= %s in %s:%d\\n",
thread_indent(-1), funcname, filename, lineno);
}
</pre></div>
</div>
<p>It can be invoked like this:</p>
<div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$</span> stap <span class="se">\</span>
show-call-hierarchy.stp <span class="se">\</span>
-c <span class="s2">"./python test.py"</span>
</pre></div>
</div>
<p>The output looks like this:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>11408 python(8274): => __contains__ in Lib/_abcoll.py:362
11414 python(8274): => __getitem__ in Lib/os.py:425
11418 python(8274): => encode in Lib/os.py:490
11424 python(8274): <= encode in Lib/os.py:493
11428 python(8274): <= __getitem__ in Lib/os.py:426
11433 python(8274): <= __contains__ in Lib/_abcoll.py:366
</pre></div>
</div>
<p>where the columns are:</p>
<blockquote>
<div><ul class="simple">
<li>time in microseconds since start of script</li>
<li>name of executable</li>
<li>PID of process</li>
</ul>
</div></blockquote>
<p>and the remainder indicates the call/return hierarchy as the script executes.</p>
<p>For a <cite>–enable-shared</cite> 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:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>probe process("python").mark("function__entry") {
</pre></div>
</div>
<p>should instead read:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>probe process("python").library("libpython3.6dm.so.1.0").mark("function__entry") {
</pre></div>
</div>
<p>(assuming a debug build of CPython 3.6)</p>
</div>
<div class="section" id="available-static-markers">
<h2>Available static markers<a class="headerlink" href="#available-static-markers" title="本標題的永久連結">¶</a></h2>
<dl class="function">
<dt id="c.function__entry">
<code class="descname">function__entry</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em><span class="sig-paren">)</span><a class="headerlink" href="#c.function__entry" title="本定義的永久連結">¶</a></dt>
<dd><p>This marker indicates that execution of a Python function has begun.
It is only triggered for pure-Python (bytecode) functions.</p>
<p>The filename, function name, and line number are provided back to the
tracing script as positional arguments, which must be accessed using
<code class="docutils literal notranslate"><span class="pre">$arg1</span></code>, <code class="docutils literal notranslate"><span class="pre">$arg2</span></code>, <code class="docutils literal notranslate"><span class="pre">$arg3</span></code>:</p>
<blockquote>
<div><ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">$arg1</span></code> : <code class="docutils literal notranslate"><span class="pre">(const</span> <span class="pre">char</span> <span class="pre">*)</span></code> filename, accessible using <code class="docutils literal notranslate"><span class="pre">user_string($arg1)</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">$arg2</span></code> : <code class="docutils literal notranslate"><span class="pre">(const</span> <span class="pre">char</span> <span class="pre">*)</span></code> function name, accessible using
<code class="docutils literal notranslate"><span class="pre">user_string($arg2)</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">$arg3</span></code> : <code class="docutils literal notranslate"><span class="pre">int</span></code> line number</li>
</ul>
</div></blockquote>
</dd></dl>
<dl class="function">
<dt id="c.function__return">
<code class="descname">function__return</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em><span class="sig-paren">)</span><a class="headerlink" href="#c.function__return" title="本定義的永久連結">¶</a></dt>
<dd><p>This marker is the converse of <a class="reference internal" href="#c.function__entry" title="function__entry"><code class="xref c c-func docutils literal notranslate"><span class="pre">function__entry()</span></code></a>, and indicates that
execution of a Python function has ended (either via <code class="docutils literal notranslate"><span class="pre">return</span></code>, or via an
exception). It is only triggered for pure-Python (bytecode) functions.</p>
<p>The arguments are the same as for <a class="reference internal" href="#c.function__entry" title="function__entry"><code class="xref c c-func docutils literal notranslate"><span class="pre">function__entry()</span></code></a></p>
</dd></dl>
<dl class="function">
<dt id="c.line">
<code class="descname">line</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em><span class="sig-paren">)</span><a class="headerlink" href="#c.line" title="本定義的永久連結">¶</a></dt>
<dd><p>This marker indicates a Python line is about to be executed. It is
the equivalent of line-by-line tracing with a Python profiler. It is
not triggered within C functions.</p>
<p>The arguments are the same as for <a class="reference internal" href="#c.function__entry" title="function__entry"><code class="xref c c-func docutils literal notranslate"><span class="pre">function__entry()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="c.gc__start">
<code class="descname">gc__start</code><span class="sig-paren">(</span>int<em> generation</em><span class="sig-paren">)</span><a class="headerlink" href="#c.gc__start" title="本定義的永久連結">¶</a></dt>
<dd><p>Fires when the Python interpreter starts a garbage collection cycle.
<code class="docutils literal notranslate"><span class="pre">arg0</span></code> is the generation to scan, like <a class="reference internal" href="../library/gc.html#gc.collect" title="gc.collect"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.collect()</span></code></a>.</p>
</dd></dl>
<dl class="function">
<dt id="c.gc__done">
<code class="descname">gc__done</code><span class="sig-paren">(</span>long<em> collected</em><span class="sig-paren">)</span><a class="headerlink" href="#c.gc__done" title="本定義的永久連結">¶</a></dt>
<dd><p>Fires when the Python interpreter finishes a garbage collection
cycle. <code class="docutils literal notranslate"><span class="pre">arg0</span></code> is the number of collected objects.</p>
</dd></dl>
<dl class="function">
<dt id="c.import__find__load__start">
<code class="descname">import__find__load__start</code><span class="sig-paren">(</span>str<em> modulename</em><span class="sig-paren">)</span><a class="headerlink" href="#c.import__find__load__start" title="本定義的永久連結">¶</a></dt>
<dd><p>Fires before <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a> attempts to find and load the module.
<code class="docutils literal notranslate"><span class="pre">arg0</span></code> is the module name.</p>
<div class="versionadded">
<p><span class="versionmodified">3.7 版新加入.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="c.import__find__load__done">
<code class="descname">import__find__load__done</code><span class="sig-paren">(</span>str<em> modulename</em>, int<em> found</em><span class="sig-paren">)</span><a class="headerlink" href="#c.import__find__load__done" title="本定義的永久連結">¶</a></dt>
<dd><p>Fires after <a class="reference internal" href="../library/importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a>’s find_and_load function is called.
<code class="docutils literal notranslate"><span class="pre">arg0</span></code> is the module name, <code class="docutils literal notranslate"><span class="pre">arg1</span></code> indicates if module was
successfully loaded.</p>
<div class="versionadded">
<p><span class="versionmodified">3.7 版新加入.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="systemtap-tapsets">
<h2>SystemTap Tapsets<a class="headerlink" href="#systemtap-tapsets" title="本標題的永久連結">¶</a></h2>
<p>The higher-level way to use the SystemTap integration is to use a 「tapset」:
SystemTap’s equivalent of a library, which hides some of the lower-level
details of the static markers.</p>
<p>Here is a tapset file, based on a non-shared build of CPython:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>/*
Provide a higher-level wrapping around the function__entry and
function__return markers:
\*/
probe python.function.entry = process("python").mark("function__entry")
{
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
frameptr = $arg4
}
probe python.function.return = process("python").mark("function__return")
{
filename = user_string($arg1);
funcname = user_string($arg2);
lineno = $arg3;
frameptr = $arg4
}
</pre></div>
</div>
<p>If this file is installed in SystemTap’s tapset directory (e.g.
<code class="docutils literal notranslate"><span class="pre">/usr/share/systemtap/tapset</span></code>), then these additional probepoints become
available:</p>
<dl class="function">
<dt id="c.python.function.entry">
<code class="descname">python.function.entry</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em>, frameptr<span class="sig-paren">)</span><a class="headerlink" href="#c.python.function.entry" title="本定義的永久連結">¶</a></dt>
<dd><p>This probe point indicates that execution of a Python function has begun.
It is only triggered for pure-python (bytecode) functions.</p>
</dd></dl>
<dl class="function">
<dt id="c.python.function.return">
<code class="descname">python.function.return</code><span class="sig-paren">(</span>str<em> filename</em>, str<em> funcname</em>, int<em> lineno</em>, frameptr<span class="sig-paren">)</span><a class="headerlink" href="#c.python.function.return" title="本定義的永久連結">¶</a></dt>
<dd><p>This probe point is the converse of <a class="reference internal" href="#c.python.function.return" title="python.function.return"><code class="xref c c-func docutils literal notranslate"><span class="pre">python.function.return()</span></code></a>, and
indicates that execution of a Python function has ended (either via
<code class="docutils literal notranslate"><span class="pre">return</span></code>, or via an exception). It is only triggered for pure-python
(bytecode) functions.</p>
</dd></dl>
</div>
<div class="section" id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="本標題的永久連結">¶</a></h2>
<p>This SystemTap script uses the tapset above to more cleanly implement the
example given above of tracing the Python function-call hierarchy, without
needing to directly name the static markers:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>probe python.function.entry
{
printf("%s => %s in %s:%d\n",
thread_indent(1), funcname, filename, lineno);
}
probe python.function.return
{
printf("%s <= %s in %s:%d\n",
thread_indent(-1), funcname, filename, lineno);
}
</pre></div>
</div>
<p>The following script uses the tapset above to provide a top-like view of all
running CPython code, showing the top 20 most frequently-entered bytecode
frames, each second, across the whole system:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>global fn_calls;
probe python.function.entry
{
fn_calls[pid(), filename, funcname, lineno] += 1;
}
probe timer.ms(1000) {
printf("\033[2J\033[1;1H") /* clear screen \*/
printf("%6s %80s %6s %30s %6s\n",
"PID", "FILENAME", "LINE", "FUNCTION", "CALLS")
foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) {
printf("%6d %80s %6d %30s %6d\n",
pid, filename, lineno, funcname,
fn_calls[pid, filename, funcname, lineno]);
}
delete fn_calls;
}
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目錄</a></h3>
<ul>
<li><a class="reference internal" href="#">Instrumenting CPython with DTrace and SystemTap</a><ul>
<li><a class="reference internal" href="#enabling-the-static-markers">Enabling the static markers</a></li>
<li><a class="reference internal" href="#static-dtrace-probes">Static DTrace probes</a></li>
<li><a class="reference internal" href="#static-systemtap-markers">Static SystemTap markers</a></li>
<li><a class="reference internal" href="#available-static-markers">Available static markers</a></li>
<li><a class="reference internal" href="#systemtap-tapsets">SystemTap Tapsets</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
</ul>
<h4>上個主題</h4>
<p class="topless"><a href="clinic.html"
title="上一章">Argument Clinic How-To</a></p>
<h4>下個主題</h4>
<p class="topless"><a href="../faq/index.html"
title="下一章">Python 常見問題</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">Report a Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.7/Doc/howto/instrumentation.rst"
rel="nofollow">Show Source
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>瀏覽</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模組索引"
>模組</a> |</li>
<li class="right" >
<a href="../faq/index.html" title="Python 常見問題"
>下一頁</a> |</li>
<li class="right" >
<a href="clinic.html" title="Argument Clinic How-To"
>上一頁</a> |</li>
<li><img src="../_static/py.png" alt=""
style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> »</li>
<li>
<span class="language_switcher_placeholder">zh_TW</span>
<span class="version_switcher_placeholder">3.7.0</span>
<a href="../index.html">Documentation </a> »
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python HOWTOs</a> »</li>
<li class="right">
<div class="inline-search" style="display: none" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="Quick search" type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('.inline-search').show(0);</script>
|
</li>
</ul>
</div>
<div class="footer">
© <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
Last updated on 8月 22, 2018.
<a href="../bugs.html">Found a bug</a>?
<br />
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.7.
</div>
</body>
</html>