Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm

Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm

Đã đăng vào thg 8 1, 2017 2:55 SA 2 phút đọc 2 phút đọc

Làm dev thì ai cũng quen với việc debug. Chỉ cần var_dump() là ta có thể biết biến đó đang chứa dữ liệu gì, chạy đến đây có sai không... Nhưng sẽ thật kinh khủng khi phải lần mò var_dump() rồi chạy lại code hết chỗ này đến chỗ kia. Lúc này IDE mới chứng tỏ sự khác biệt với những editor. Hãy quên đi var_dump(),

sudo apt-get install php-xdebug
2,
sudo apt-get install php-xdebug
3, với IDE bạn chỉ cần click chọn chỗ cần debug, sau đó request. Pum! toàn bộ nội dung cần debug ở từng phần, từng function sẽ hiển thị cho bạn. Bài viết này sử dụng PHPStorm để debug.

Install Xdebug

macOS

brew install php71-xdebug

ubuntu

sudo apt-get install php-xdebug

Sau đó restart server

Config php.ini

Thêm vào file php.ini

[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"

Path

sudo apt-get install php-xdebug
4 tùy theo nơi đặt xdebug của bạn.

Config PHPStorm

Chọn Run > Edit Configurations... Click button

sudo apt-get install php-xdebug
5 để thêm config mới. Ở đây lấy ví dụ là PHP Web Application Click button
sudo apt-get install php-xdebug
6 ở phần Server để cấu hình server Click button
sudo apt-get install php-xdebug
5 ở dialog Servers. Thêm thông tin về server. Click OK Khi đó ta đã config xong phần server để debug. Click OK Nếu server bạn chạy https, cần chỉnh sửa phần Start URL Giờ đến phần thú vị nhất, thực hiện debug. Chọn Run > Debug [name_server] Một tab trình duyệt web sẽ hiển thị thông báo bắt đầu session debug. Không cần quan tâm, bạn cứ tắt tab đó đi. Click button debug listening, để nó chuyển sang trạng thái lắng nghe (màu xanh lá) Bây giờ nó sẽ lắng nghe và break ở bất kỳ chỗ phần code nào ta chọn làm break point (click khoảng trống bên cạnh line number) Thực hiện request để bắt debug. Sử dụng hàng button điều hướng để xem kỹ từng bước chạy của php. Xem thêm: https://www.jetbrains.com/help/phpstorm/debug-tool-window.html#steptoolbar Khi không cần debug thì click button debug listening lần nữa để off debug (màu đỏ)
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Click button
sudo apt-get install php-xdebug
6 ở phần Server để cấu hình server
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Click button
sudo apt-get install php-xdebug
5 ở dialog Servers. Thêm thông tin về server. Click OK
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Khi đó ta đã config xong phần server để debug. Click OK
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Nếu server bạn chạy https, cần chỉnh sửa phần Start URL
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Giờ đến phần thú vị nhất, thực hiện debug. Chọn Run > Debug [name_server]
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Một tab trình duyệt web sẽ hiển thị thông báo bắt đầu session debug. Không cần quan tâm, bạn cứ tắt tab đó đi. Click button debug listening, để nó chuyển sang trạng thái lắng nghe (màu xanh lá)
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Bây giờ nó sẽ lắng nghe và break ở bất kỳ chỗ phần code nào ta chọn làm break point (click khoảng trống bên cạnh line number)
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Thực hiện request để bắt debug.
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Sử dụng hàng button điều hướng để xem kỹ từng bước chạy của php. Xem thêm: https://www.jetbrains.com/help/phpstorm/debug-tool-window.html#steptoolbar
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Khi không cần debug thì click button debug listening lần nữa để off debug (màu đỏ)
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm

Debug với Postman

Với postman ta hoàn toàn có thể debug giống như trên web, chỉ cần thêm vào url bạn muốn test tham số

sudo apt-get install php-xdebug
8
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm

Bonus: Debug với queue của Laravel

Ở trên chúng ta đã biết cách debug với web, giờ là phần nói về debug với queue của Laravel. Chọn Run > Edit Configurations... Click button

sudo apt-get install php-xdebug
5 để thêm config mới. Chọn PHP Remote Debug Chọn server, đặt tên ide key. Click OK Chọn Run > Debug [name_server] Chạy câu lệnh
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
0 trong terminal với
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
1 là ide key đã đặt ở trên. Tiếp theo thì bật debug listening, click break point, chạy
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
2.
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Chọn server, đặt tên ide key. Click OK
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Chọn Run > Debug [name_server]
Hướng dẫn xdebug_session_start phpstorm - xdebug_session_start phpstorm
Chạy câu lệnh
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
0 trong terminal với
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
1 là ide key đã đặt ở trên. Tiếp theo thì bật debug listening, click break point, chạy
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
2.

All rights reserved

Khi code, việc gặp bug là điều không thể tránh khỏi, bug không ngán một ngôn ngữ nào. Khi project nhỏ (tự code từ đầu) hay đang code để giải bài tập thì việc debug có thể khá dễ dàng. Nhưng khi ta code một project khổng lồ với những luồng đi rất chi là loằng ngoằng thì việc debug code không hề dễ dàng. Hay trường hợp phổ biến là code trên các framework nhiều phần gặp lỗi mà ta không biết lỗi đó là của mình hay của người và để xác định được tại sao thì ta cần debug.

Trong bài, ta sẽ tìm hiểu cách debug code PHP, cài đặt PhpStorm, cài đặt công cụ debug.

Cài đặt PhpStorm

Note: Môi trường demo của mình trên linux, nếu là Windows thì hãy ánh xạ các bước sao cho đúng. 

Để tải PhpStorm vào link: https://www.jetbrains.com/phpstorm/

Phần cài đặt tham khảo link: https://vinasupport.com/phpstorm-ide-huong-dan-download-va-cai-dat/

Hoặc có thể xem video để dễ dàng hơn: https://www.youtube.com/watch?v=jT9h8qyhzcY

Cài đặt php-cli và php-cgi

Khạy PhpStorm để debug, PhpStorm sẽ tự tạo ra một server riêng và code sẽ được thực thi trên server này. Để PhpStorm server chạy được ta cần 2 thư viện là

[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
3 và
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
4

Cài đặt trên linux

sudo apt install php-dev php-cli php-cgi -y

Cài CLI cho PhpStorm, sau khi cài

[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
3 và
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
4 ta bật PhpStorm và kiểm tra xem đã nhận
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
3 chưa. Bằng cách truy cập: File -> Setting (ctr + alt + s) -> PHP & FrameWork

Nếu tại phần CLI Interpreter chưa thấy hiện gì, ta cần cài đặt nó thủ công bằng cách chọn vào ... sẽ hiển thị lên 1 cửa sổ. Chọn tiếp vào dấu + và chọn vào đường dẫn file thực thi của

[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
3, nếu không thấy đường dẫn file thực thi chọn Other Local... để chọn thủ công.CLI Interpreter chưa thấy hiện gì, ta cần cài đặt nó thủ công bằng cách chọn vào ... sẽ hiển thị lên 1 cửa sổ. Chọn tiếp vào dấu + và chọn vào đường dẫn file thực thi của
[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
3, nếu không thấy đường dẫn file thực thi chọn Other Local... để chọn thủ công.

Cài đặt đường dẫn xong nhấn Apply tiếp đó là OK. Sau đó ta quay lại cửa sổ trước và chọn CLI Interpreter và ta mới thêm.Apply tiếp đó là OK. Sau đó ta quay lại cửa sổ trước và chọn CLI Interpreter và ta mới thêm.

Cài đặt trình debug

Để thực hiện debug PhpStorm yêu cầu trình debuger, có 2 trình debugger được hỗ trợ: xdebug và zend debuger. Nhưng theo khuyến cáo của jetbrains thì ta nên dùng xdebug.

Cài đặt xdebug cho linux

sudo pecl install xdebug

Bật chế độ debug cho PHP ta sửa file php.ini, thêm vào đoạn dòng chữ sauphp.ini, thêm vào đoạn dòng chữ sau

[xdebug]
zend_extension="/usr/lib/php/20170718/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.remote_connect_back=on

Nếu không biết đường dẫn file

[Xdebug]
zend_extension_ts=/usr/local/opt/php71-xdebug/xdebug.so
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
9 ta cần tìm nó

find / -name xdebug.so 2>/dev/null

Cuối cùng kiểm tra xem trình debug đã được bật chưa?

php -v

Nếu kết quả hiển thị có thông tin về debugger đã bật thì thành công, nếu không là không thành công.

Debug code PHP

Debug file PHP

Để thực hiện chức năng này ta cần đặt một breakpoint tại vị trí muốn debug. Sau đó nhấn vào icon debug hoặc nhấn tổ hợp phím Shift+F9Shift+F9

Sau khi chương trình thực thi đến phần ta đặt breakpoint nó sẽ lập tức dừng lại cho ta debug. Toàn bộ thông tin về hàm và biến được hiển thị lên.

Debug remote

Đây là phần đáng chú ý trong khi debug code PHP. Nó rất tiện lợi và dễ dàng trong việc debug khi server nhận được toàn bộ thông tin của người dùng nhập trên trình duyệt. Thông tin đó bao gồm cả POST và GET request. Để thực hiện được tính năng này ta cần thực hiện như sau:POSTGET request. Để thực hiện được tính năng này ta cần thực hiện như sau:

  1. Cài extension debug client cho trình duyệt

2. Bật lắng nghe debug server bằng cách chọn

  1. Đặt breakpoint
  2. Truy cập vào server PhpStorm

Bằng cách chọn vào biểu tượng trình duyệt muốn mở. Trang web chạy trên server PhpStorm sẽ hiện lên trên trình duyệt. Khi muốn debug tính năng nào của trong web chỉ cần click vào con bọ trên trình duyệt và thực hiện request.

Đây là một tính năng rất hay của PhpStorm trong việc debug gỡ rối chương trình. Ngoài ra PhpStorm còn nhiều tính năng thú vị nữa.

All rights reserved