xdebug及phpstorm、vim+dbgpy遠程調試配置,xdebug profile+webgrind

xdebug簡介:

Xdebug是一個開放源代碼的PHP程序調試器(即一個Debug工具),可以用來跟蹤,調試和分析PHP程序的運行狀況。它以php擴展的形式存在,在centos下可以通過yum install php70-php-pecl-xdebug安裝(針對php70版本,其他類似。也可以以其他形式安裝,是pecl庫擴展)。
xdebug功能:

    basic functions:Xdebug's basic functions include the display of stack traces on error conditions, maximum nesting level protection and time tracking.(基礎功能:在error conditions時顯示stack traces、maximum nesting level protection、 time tracking)
    Variable Display Features:Xdebug replaces PHP's var_dump() function for displaying variables. Xdebug's version includes different colors for different types and places limits on the amount of array elements/object properties, maximum depth and string lengths. There are a few other functions dealing with variable display as well.
    Stack Traces: When Xdebug is activated it will show a stack trace whenever PHP decides to show a notice, warning, error etc. The information that stack traces display, and the way how they are presented, can be configured to suit your needs.
    Function Traces: Xdebug allows you to log all function calls, including parameters and return values to a file in different formats.
    Code Coverage Analysis: Code coverage tells you which lines of script (or set of scripts) have been executed during a request. With this information you can for example find out how good your unit tests are.
    Profiling PHP Scripts:Xdebug's built-in profiler allows you to find bottlenecks in your script and visualize those with an external tool such as KCacheGrind or WinCacheGrind.
    Remote Debugging:Xdebug provides an interface for debugger clients that interact with running PHP scripts. This section explains how to set-up PHP and Xdebug to allow this, and introduces a few clients.

xdebug配置:

在安裝了xdebug擴展後,對xdebug功能的使用是在php.ini中xdebug對應的配置參數中進行控制的。通過yum安裝的xdebug安裝時會生成一個xdebug.ini,並存放在php.d目錄中(不同方式安裝放置的位置可能不同,不過最終都可以通過phpinfo()查看)。

在xdebug.ini中有關於各個配置項的詳細說明。另外也可以參考官方文檔:https://xdebug.org/docs/
我的配置:

  1. remote debugging遠程調試配置:

    xdebug.remote_enable = 1
    xdebug.remote_port = 9002
    xdebug.remote_host = localhost
    xdebug.idekey = phpstorm

  2. profiling php scripts配置:

    xdebug.profiler_enable = 0
    xdebug.profiler_enable_trigger = 1
    xdebug.profiler_output_name = cachegrind.out.%t.%p
    xdebug.profiler_output_dir = “/dir/to/output/file”

a
xdebug部分參考文獻:

  1. 官方文檔:https://xdebug.org/docs/

xdebug遠程調試功能配置
遠程調試通信建立方式:
With a static IP/single developer

With remote debugging, Xdebug embedded in PHP acts like the client, and the IDE as the server. The following animation shows how the communication channel is set-up:

The IP of the server is 10.0.1.2 with HTTP on port 80
The IDE is on IP 10.0.1.42, so xdebug.remote_host is set to 10.0.1.42
The IDE listens on port 9000, so xdebug.remote_port is set to 9000
The HTTP request is started on the machine running the IDE
Xdebug connects to 10.0.1.42:9000
Debugging runs, HTTP Response provided

With an unknown IP/multiple developers

If xdebug.remote_connect_back is used, the set-up is slightly different:

The IP of the server is 10.0.1.2 with HTTP on port 80
The IDE is on an unknown IP, so xdebug.remote_connect_back is set to 1
The IDE listens on port 9000, so xdebug.remote_port is set to 9000
The HTTP request is made, Xdebug detects the IP addres from the HTTP headers
Xdebug connects to the detected IP (10.0.1.42) on port 9000
Debugging runs, HTTP Response provided

xdebug部分配置

參考前面的提到的配置,以及上面針對不同通信建立方式配置不同的配置項目。
客戶端配置

  1. vim + dbgp配置

在vim下使用xdebug進行調試需要安裝vim DGBP插件http://www.vim.org/scripts/script.php?script_id=1929

並且通過

let g:debuggerPort = 9002

設置客戶端端口

之後按下F5即可進入debug監聽模式,等待請求的到來。
2. phpstorm配置

在Preference->Languages&Frameworks->PHP->Debug中配置xdebug的debug port

在Preference->Languages&Frameworks->PHP->Servers中設置xdebug server參數:

之後在Run/Debug Configurations中添加 PHP Web Application配置即可:

瀏覽器發送請求

輸入對應url,並加上XDEBUG_SESSION_START=14982&XDEBUG_SESSION=phpstorm參數,觸發調試。url例如:http://localhost/index.php?XDEBUG_SESSION_START=1&XDEBUG_SESSION=phpstorm
跨網段調試

(本部分針對phpstorm跨網段調試。因爲vim+DBGp可以永遠的處於同一個網段)

當你的web server和你的客戶端不在同一個網段,你的客戶端可以訪問web server,但是web server由於NAT、防火牆等原因不能訪問客戶端所在主機時,不能進行遠程調試。

不過,如果客戶端所在主機可以和web server建立ssh連接,就可以利用ssh隧道的遠端端口轉發+xdebug With a static IP/single developer 調試實現跨網段調試。
實現原理:

由上面的With a static IP/single developer示例圖可知,當xdebug server收到帶有觸發調試參數的請求後,會根據配置文件制定的remote_host、remote_port嘗試與客戶端建立DBGp連接。當跨網段時,xdebug server並不能訪問到客戶端。

不過通過ssh的遠端端口轉發功能,我們可以建立一個隧道,並將remote_host、remote_port設置爲xdebug server的本地地址和本地端口,然後將該端口利用ssh遠端端口轉發功能映射至客戶端的調試端口,這樣xdebug server就可以通過訪問本地端口實現與客戶端建立DBGp連接的需求。
具體操作:

xdebug配置項設置:

remote_host = localhost
remote_port = 9002

建立ssh遠端端口轉發:

在客戶端命令行執行命令:ssh -R 9002:localhost:9002 server_user@server_ip (該命令是指與server_ip主機建立ssh連接,並將在server_ip主機上接收到的目的地址是localhost的9002端口(指後一個9002)的數據包轉發至客戶端的9002端口(指前面的9002))

phpstorm配置:

phpstorm在配置好debug port和server後即可,只需要點擊“Start Listening for PHP Debug Connections”按鈕等待DBGp連接請求的到來即可。
遠程調試參考文獻:

  1. xdebug 遠程調試官方文檔 https://xdebug.org/docs/remote
  2. PhpStorm XDebug 遠程調試
  3. Remote debugging in PhpStorm via SSH tunnel phpstorm官方文檔
  4. 三種不同類型的ssh隧道 ssh隧道介紹
    xdebug profiling php scripts

具體配置項參考前文及官方文檔https://xdebug.org/docs/profiler

開啓該功能後xdebug就會對php腳本進行性能分析,並在指定目錄下按指定的文件名生成profiler_output文件。這就是xdebug profile的結果(The profiler in Xdebug 2 outputs profiling information in the form of a cachegrind compatible file. )。之後需要使用可視化工具來分析和查看結果。

常用的工具有 KCacheGrind (linux)、 WinCacheGrind.(windows)、WebGrind(web版,跨平臺)等(還有其他工具,在xdebug 官網都有提及)。

我使用的是WebGrind
xdebug profile參考文獻:

  1. xdebug profile官方文檔 https://xdebug.org/docs/profiler
  2. WebGrind https://github.com/jokkedk/webgrind
  3. webgrind安裝使用詳細說明
  4. 用xdebug分析PHP以及結果分析程序webgrind的使用 (包含show call graph相關資料)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章