phpstorm+xdebug遠端斷點除錯

技術小阿哥發表於2017-11-27

CentOS(Linux)下安裝Xdebug

Xdebug是一個開放原始碼的PHP程式偵錯程式(即一個Debug工具),可以用來跟蹤,除錯和分析PHP程式的執行狀況,本文主要記錄一下在centos(linux)下xdebug的安裝和配置方法。

首先讓php錯誤顯示,只需要修改php.ini當中的2條指令,把 displayerrors和htmlerrors都設定為On,如下所示:

html_errors = On

display_errors = On

當然如果你要需要檢視更多資訊,比如說列印呼叫棧,哪就需要安裝xdebug,這個對於比較複雜的程式碼系統特別有幫助。

xdebug是php的一個module,需要編譯安裝,我用lnmp安裝的php,php被預設安裝到/usr/local/php,然後做一個硬連結到/usr/bin。

(1)下載xdebug

xdebug 下載地址頁:https://xdebug.org/download.php

wget https://xdebug.org/files/xdebug-2.4.0rc4.tgz

注意:xdebug的版本需與您的php版本相對應,由於我的php是5.6,所以這裡下載是:xdebug2.4

(2)編譯xdebug

然後開始編譯

tar xzf xdebug-2.4.0rc4.tgz

cd xdebug-2.4.0RC4/

phpize

注意這裡如果你的phpize沒有加入到環境變數的話,需要帶該檔案的全路徑

./configure –enable-xdebug –with-php-config=/usr/local/php/bin/php-config

這裡的php-config引數要改為你實際的php-config地址額,不知道的話自己搜吧。

make

make test

make install

(4)配置php.ini

編譯完成之後可以去php的安裝目錄通過find搜尋一下是否生成了xdebug.so檔案。如果生成成功,就可以直接配置php.ini檔案了。具體配置程式碼如下:

[Xdebug]  

zend_extension=”/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so”  

xdebug.profiler_enable=on   

xdebug.trace_output_dir=”/usr/local/php5/xdebug/”  

xdebug.profiler_output_dir=”/usr/local/php5/xdebug/”  

xdebug.remote_enable=on             

xdebug.remote_handler=dbgp            

;xdebug.remote_host=localhost  

xdebug.remote_port=9999 

重啟php-fpm,可以通過phpinfo()檢查你的xdebug是否安裝成功,也可以隨便寫段錯誤的php程式碼,重新整理瀏覽器,就能看到錯誤提示。


xdebug配置

1.在php.ini配置檔案中新增如下內容
zend_extension="/wherever/you/put/it/xdebug.so"

2.如果是php5.3之前版本執行緒安全,新增如下內容代替上面1的內容
zend_extension_ts="/wherever/you/put/it/xdebug.so"

3.自從php5.3之後zend_extension_ts, zend_extension_debug不再使用

4.xdebug的一些其他配置

;顯示錯誤資訊
xdebug.default_enable = 1;函式除錯
xdebug.auto_trace=on
xdebug.trace_output_dir
xdebug.trace_output_name
;Type: string, Default value: trace.%c
xdebug.collect_params = 1|3|4 (引數長度,引數值,引數=值)
xdebug.show_mem_delta=1 顯示記憶體
xdebug.collect_return=1 顯示返回值
xdebug.trace_options =1 追加日誌
xdebug.collect_params=1xdebug.collect_vars = 1;開啟效能分析
xdebug.profiler_enable=1;效能分析日誌儲存目錄
xdebug.profiler_output_dir = /data/logs/xdebug/
;效能分析日誌檔名稱
xdebug.profiler_output_name = cachegrind.out.log;預設是如下格式,t時間,p程式id
;xdebug.profiler_output_name = cachegrind.out.%t.%p

;程式碼覆蓋率
xdebug.coverage_enable = 1;以下是遠端除錯配置
xdebug.remote_host= 127.0.0.1xdebug.remote_connect_back = 1xdebug.remote_port = 9000xdebug.remote_log="/data/logs/xdebug/xdebug.log"

4.重啟php使配置生效


效能分析日誌工具

1.linux平臺

工具KCacheGrind (Linux, KDE) https://kcachegrind.github.io/

2.win平臺檢視工具WinCacheGrind

相關網址

http://ceefour.github.io/wincachegrind/https://sourceforge.net/projects/wincachegrind/https://github.com/ceefour/wincachegrind/releases/tag/1.1

列名稱含義

Self – Shows the execution time of the code in the current block

Cum. – Shows the total exuction time of functions that the current function (Self) calls

Avg vs. Total: Average is average time per call, Total is the total time spend in all calls.

3.Web方式檢視webgrind

https://github.com/jokkedk/webgrind


PHPSTORM 配置:

1.file->setings->php|Debug右側。xdebug的那一塊。 設定Debug port:9900(這裡設定 的是,xdebug 吐出的debug資訊,通過本機的什麼埠傳輸。)

2.file->setings->php|Servers  右側。  host: 你的web伺服器的域名或ip ,埠,  下面的 use path mapping  意的是,你的專案的目錄,對應伺服器上的,什麼目錄?   這裡一定要設定哦! 不然,會發生找不到檔案而出錯,導至除錯終止。


3.Run->Edit Configurations-> 增加一個 PHP WEB APPlication 的除錯點。  右側: server 選擇你上面建立的server.  starturl 設定你的入口檔案。


至此,配置完畢!

這樣的請求,可以註冊一個除錯客戶端哦!

http://www.aihuxi.com/****.php?XDEBUG_SESSION_START=19192

點選,小蟲子圖示,即可,開始除錯! 

本文轉自 2012hjtwyf 51CTO部落格,原文連結:http://blog.51cto.com/hujiangtao/1920665,如需轉載請自行聯絡原作者


相關文章