日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:使用 DTrace 和 SystemTap 檢測CPython

使用 DTrace 和 SystemTap 檢測Cpython

作者

David Malcolm

作者

?ukasz Langa

DTrace和SystemTap是監(jiān)控工具,它們都提供了一種檢查計算機系統(tǒng)上的進程的方法。 它們都使用特定領域的語言,允許用戶編寫腳本,其中:

  • 進程監(jiān)視的過濾器

  • 從感興趣的進程中收集數(shù)據(jù)

  • 生成有關數(shù)據(jù)的報告

從Python 3.6開始,CPython可以使用嵌入式“標記”構建,也稱為“探測器”,可以通過DTrace或SystemTap腳本觀察,從而更容易監(jiān)視系統(tǒng)上的CPython進程正在做什么。

CPython 實現(xiàn)細節(jié): DTrace標記是CPython解釋器的實現(xiàn)細節(jié)。 不保證CPython版本之間的探針兼容性。 更改CPython版本時,DTrace腳本可能會停止工作或無法正常工作而不會發(fā)出警告。

啟用靜態(tài)標記

macOS內(nèi)置了對DTrace的支持。 在Linux上,為了使用SystemTap的嵌入式標記構建CPython,必須安裝SystemTap開發(fā)工具。

在Linux機器上,這可以通過:

 
 
 
 
  1. $ yum install systemtap-sdt-devel

或者:

 
 
 
 
  1. $ sudo apt-get install systemtap-sdt-dev

之后 CPython 必須 配置 —with-dtrace 選項:

 
 
 
 
  1. checking for --with-dtrace... yes

在macOS上,您可以通過在后臺運行Python進程列出可用的DTrace探測器,并列出Python程序提供的所有探測器:

 
 
 
 
  1. $ Python3.6 -q &
  2. $ sudo dtrace -l -P python$! # or: dtrace -l -m python3.6
  3. ID PROVIDER MODULE FUNCTION NAME
  4. 29564 python18035 python3.6 _PyEval_EvalFrameDefault function-entry
  5. 29565 python18035 python3.6 dtrace_function_entry function-entry
  6. 29566 python18035 python3.6 _PyEval_EvalFrameDefault function-return
  7. 29567 python18035 python3.6 dtrace_function_return function-return
  8. 29568 python18035 python3.6 collect gc-done
  9. 29569 python18035 python3.6 collect gc-start
  10. 29570 python18035 python3.6 _PyEval_EvalFrameDefault line
  11. 29571 python18035 python3.6 maybe_dtrace_line line

在Linux上,您可以通過查看是否包含“.note.stapsdt”部分來驗證構建的二進制文件中是否存在SystemTap靜態(tài)標記。

 
 
 
 
  1. $ readelf -S ./python | grep .note.stapsdt
  2. [30] .note.stapsdt NOTE 0000000000000000 00308d78

如果你將 Python 編譯為共享庫(使用 --enable-shared 配置選項),那么你需要改為在共享庫內(nèi)部查看。 例如:

 
 
 
 
  1. $ readelf -S libpython3.3dm.so.1.0 | grep .note.stapsdt
  2. [29] .note.stapsdt NOTE 0000000000000000 00365b68

足夠現(xiàn)代的readelf命令可以打印元數(shù)據(jù):

 
 
 
 
  1. $ readelf -n ./python
  2. Displaying notes found at file offset 0x00000254 with length 0x00000020:
  3. Owner Data size Description
  4. GNU 0x00000010 NT_GNU_ABI_TAG (ABI version tag)
  5. OS: Linux, ABI: 2.6.32
  6. Displaying notes found at file offset 0x00000274 with length 0x00000024:
  7. Owner Data size Description
  8. GNU 0x00000014 NT_GNU_BUILD_ID (unique build ID bitstring)
  9. Build ID: df924a2b08a7e89f6e11251d4602022977af2670
  10. Displaying notes found at file offset 0x002d6c30 with length 0x00000144:
  11. Owner Data size Description
  12. stapsdt 0x00000031 NT_STAPSDT (SystemTap probe descriptors)
  13. Provider: python
  14. Name: gc__start
  15. Location: 0x00000000004371c3, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf6
  16. Arguments: -4@%ebx
  17. stapsdt 0x00000030 NT_STAPSDT (SystemTap probe descriptors)
  18. Provider: python
  19. Name: gc__done
  20. Location: 0x00000000004374e1, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bf8
  21. Arguments: -8@%rax
  22. stapsdt 0x00000045 NT_STAPSDT (SystemTap probe descriptors)
  23. Provider: python
  24. Name: function__entry
  25. Location: 0x000000000053db6c, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6be8
  26. Arguments: 8@%rbp 8@%r12 -4@%eax
  27. stapsdt 0x00000046 NT_STAPSDT (SystemTap probe descriptors)
  28. Provider: python
  29. Name: function__return
  30. Location: 0x000000000053dba8, Base: 0x0000000000630ce2, Semaphore: 0x00000000008d6bea
  31. Arguments: 8@%rbp 8@%r12 -4@%eax

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.

靜態(tài)DTrace探針

下面的 DTrace 腳本示例可以用來顯示一個 Python 腳本的調(diào)用/返回層次結構,只在調(diào)用名為 “start” 的函數(shù)內(nèi)進行跟蹤。換句話說,導入時的函數(shù)調(diào)用不會被列出。

 
 
 
 
  1. self int indent;
  2. python$target:::function-entry
  3. /copyinstr(arg1) == "start"/
  4. {
  5. self->trace = 1;
  6. }
  7. python$target:::function-entry
  8. /self->trace/
  9. {
  10. printf("%d\t%*s:", timestamp, 15, probename);
  11. printf("%*s", self->indent, "");
  12. printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
  13. self->indent++;
  14. }
  15. python$target:::function-return
  16. /self->trace/
  17. {
  18. self->indent--;
  19. printf("%d\t%*s:", timestamp, 15, probename);
  20. printf("%*s", self->indent, "");
  21. printf("%s:%s:%d\n", basename(copyinstr(arg0)), copyinstr(arg1), arg2);
  22. }
  23. python$target:::function-return
  24. /copyinstr(arg1) == "start"/
  25. {
  26. self->trace = 0;
  27. }

它可以這樣調(diào)用:

 
 
 
 
  1. $ sudo dtrace -q -s call_stack.d -c "python3.6 script.py"

輸出結果會像這樣:

 
 
 
 
  1. 156641360502280 function-entry:call_stack.py:start:23
  2. 156641360518804 function-entry: call_stack.py:function_1:1
  3. 156641360532797 function-entry: call_stack.py:function_3:9
  4. 156641360546807 function-return: call_stack.py:function_3:10
  5. 156641360563367 function-return: call_stack.py:function_1:2
  6. 156641360578365 function-entry: call_stack.py:function_2:5
  7. 156641360591757 function-entry: call_stack.py:function_1:1
  8. 156641360605556 function-entry: call_stack.py:function_3:9
  9. 156641360617482 function-return: call_stack.py:function_3:10
  10. 156641360629814 function-return: call_stack.py:function_1:2
  11. 156641360642285 function-return: call_stack.py:function_2:6
  12. 156641360656770 function-entry: call_stack.py:function_3:9
  13. 156641360669707 function-return: call_stack.py:function_3:10
  14. 156641360687853 function-entry: call_stack.py:function_4:13
  15. 156641360700719 function-return: call_stack.py:function_4:14
  16. 156641360719640 function-entry: call_stack.py:function_5:18
  17. 156641360732567 function-return: call_stack.py:function_5:21
  18. 156641360747370 function-return:call_stack.py:start:28

靜態(tài)SystemTap標記

使用 SystemTap 集成的底層方法是直接使用靜態(tài)標記。 這需要你顯式地說明包含它們的二進制文件。

例如,這個SystemTap腳本可以用來顯示Python腳本的調(diào)用/返回層次結構:

 
 
 
 
  1. probe process("python").mark("function__entry") {
  2. filename = user_string($arg1);
  3. funcname = user_string($arg2);
  4. lineno = $arg3;
  5. printf("%s => %s in %s:%d\\n",
  6. thread_indent(1), funcname, filename, lineno);
  7. }
  8. probe process("python").mark("function__return") {
  9. filename = user_string($arg1);
  10. funcname = user_string($arg2);
  11. lineno = $arg3;
  12. printf("%s <= %s in %s:%d\\n",
  13. thread_indent(-1), funcname, filename, lineno);
  14. }

它可以這樣調(diào)用:

 
 
 
 
  1. $ stap \
  2. show-call-hierarchy.stp \
  3. -c "./python test.py"

輸出結果會像這樣:

 
 
 
 
  1. 11408 python(8274): => __contains__ in Lib/_abcoll.py:362
  2. 11414 python(8274): => __getitem__ in Lib/os.py:425
  3. 11418 python(8274): => encode in Lib/os.py:490
  4. 11424 python(8274): <= encode in Lib/os.py:493
  5. 11428 python(8274): <= __getitem__ in Lib/os.py:426
  6. 11433 python(8274): <= __contains__ in Lib/_abcoll.py:366

其中的列是:

  • 腳本開始后經(jīng)過的微秒數(shù)

  • 可執(zhí)行文件的名字

  • 進程的PID

其余部分則表示腳本執(zhí)行時的調(diào)用/返回層次結構。

對于 CPython 的 --enable-shared 編譯版,這些標記包含在 libpython 共享庫內(nèi)部,并且 probe 的加點路徑需要反映這個。 例如,上述示例的這一行:

 
 
 
 
  1. probe process("python").mark("function__entry") {

應改為:

 
 
 
 
  1. probe process("python").library("libpython3.6dm.so.1.0").mark("function__entry") {

(假定為 CPython 3.6 的 調(diào)試編譯版)

可用的靜態(tài)標記

function__entry(str filename, str funcname, int lineno)

這個標記表示一個Python函數(shù)的執(zhí)行已經(jīng)開始。它只對純 Python (字節(jié)碼)函數(shù)觸發(fā)。

文件名、函數(shù)名和行號作為位置參數(shù)提供給跟蹤腳本,必須使用 $arg1, $arg2, $arg3 訪問:

  • $arg1 : (const char *) 文件名,使用 user_string($arg1) 訪問

  • $arg2 : (const char *) 函數(shù)名,使用 user_string($arg2) 訪問

  • $arg3 : int 行號

function__return(str filename, str funcname, int lineno)

這個標記與 function__entry() 相反,表示Python函數(shù)的執(zhí)行已經(jīng)結束 (通過 return 或者異常)。 它只對純Python (字節(jié)碼) 函數(shù)觸發(fā)。

參數(shù)和 function__entry() 相同

line(str filename, str funcname, int lineno)

這個標記表示一個 Python 行即將被執(zhí)行。它相當于用 Python 分析器逐行追蹤。它不會在C函數(shù)中觸發(fā)。

參數(shù)和 function__entry() 相同

gc__start(int generation)

當Python解釋器啟動一個垃圾回收循環(huán)時被觸發(fā)。 arg0 是要掃描的生成器,如 gc.collect()。

gc__done(long collected)

當Python解釋器完成一個垃圾回收循環(huán)時被觸發(fā)。arg0 是收集到的對象的數(shù)量。

import__find__load__start(str modulename)

在 importlib 試圖查找并加載模塊之前被觸發(fā)。arg0 是模塊名稱。

3.7 新版功能.

import__find__load__done(str modulename, int found)

在 importlib 的 find_and_load 函數(shù)被調(diào)用后被觸發(fā) 。arg0 是模塊名稱, arg1 表示模塊是否成功加載。

3.7 新版功能.

audit(str event, void *tuple)

當 sys.audit() 或 PySys_Audit() 被調(diào)用時啟動。 arg0 是事件名稱的 C 字符串,arg1 是一個指向元組對象的 PyObject 指針。

3.8 新版功能.

SystemTap Tapsets

使用SystemTap集成的更高層次的方法是使用 “tapset” 。SystemTap 的等效庫,它隱藏了靜態(tài)標記的一些底層細節(jié)。

這里是一個基于 CPython 的非共享構建的 tapset 文件。

 
 
 
 
  1. /*
  2. Provide a higher-level wrapping around the function__entry and
  3. function__return markers:
  4. \*/
  5. probe python.function.entry = process("python").mark("function__entry")
  6. {
  7. filename = user_string($arg1);
  8. funcname = user_string($arg2);
  9. lineno = $arg3;
  10. frameptr = $arg4
  11. }
  12. probe python.function.return = process("python").mark("function__return")
  13. {
  14. filename = user_string($arg1);
  15. funcname = user_string($arg2);
  16. lineno = $arg3;
  17. frameptr = $arg4
  18. }

如果這個文件安裝在 SystemTap 的 tapset 目錄下(例如``/usr/share/systemtap/tapset`` ),那么這些額外的探測點就會變得可用。

python.function.entry(str filename, str funcname, int lineno, frameptr)

這個探針點表示一個Python函數(shù)的執(zhí)行已經(jīng)開始。它只對純Python (字節(jié)碼)函數(shù)觸發(fā)。

python.function.return(str filename, str funcname, int lineno, frameptr)

這個探針點是 python.function.return 的反義操作,表示一個 Python 函數(shù)的執(zhí)行已經(jīng)結束(或是通過 return,或是通過異常)。 它只會針對純 Python(字節(jié)碼)函數(shù)觸發(fā)。

例子

這個SystemTap腳本使用上面的tapset來更清晰地實現(xiàn)上面給出的跟蹤Python函數(shù)調(diào)用層次結構的例子,而不需要直接命名靜態(tài)標記。

 
 
 
 
  1. probe python.function.entry
  2. {
  3. printf("%s => %s in %s:%d\n",
  4. thread_indent(1), funcname, filename, lineno);
  5. }
  6. probe python.function.return
  7. {
  8. printf("%s <= %s in %s:%d\n",
  9. thread_indent(-1), funcname, filename, lineno);
  10. }

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:

 
 
 
 
  1. global fn_calls;
  2. probe python.function.entry
  3. {
  4. fn_calls[pid(), filename, funcname, lineno] += 1;
  5. }
  6. probe timer.ms(1000) {
  7. printf("\033[2J\033[1;1H") /* clear screen \*/
  8. printf("%6s %80s %6s %30s %6s\n",
  9. "PID", "FILENAME", "LINE", "FUNCTION", "CALLS")
  10. foreach ([pid, filename, funcname, lineno] in fn_calls- limit 20) {
  11. printf("%6d %80s %6d %30s %6d\n",
  12. pid, filename, lineno, funcname,
  13. fn_calls[pid, filename, funcname, lineno]);
  14. }
  15. delete fn_calls;
  16. }

網(wǎng)站題目:創(chuàng)新互聯(lián)Python教程:使用 DTrace 和 SystemTap 檢測CPython
網(wǎng)頁地址:http://www.dlmjj.cn/article/djgdhco.html