调试协程
优质
小牛编辑
136浏览
2023-12-01
使用Swoole
协程时,可以使用下面的方法进行调试
GDB调试
进入 gdb
gdb php test.php
gdbinit
(gdb) source /path/to/swoole-src/gdbinit
设置断点
例如 co::sleep
函数
(gdb) b zim_swoole_coroutine_util_sleep
打印当前进程的所有协程和状态
(gdb) co_list
coroutine 1 SW_CORO_YIELD
coroutine 2 SW_CORO_RUNNING
打印当前运行时协程的调用栈
(gdb) co_bt
coroutine cid:[2]
[0x7ffff148a100] Swoole\Coroutine->sleep(0.500000) [internal function]
[0x7ffff148a0a0] {closure}() /home/shiguangqi/php/swoole-src/examples/coroutine/exception/test.php:7
[0x7ffff141e0c0] go(object[0x7ffff141e110]) [internal function]
[0x7ffff141e030] (main) /home/shiguangqi/php/swoole-src/examples/coroutine/exception/test.php:10
打印指定协程id的调用栈
(gdb) co_bt 1
[0x7ffff1487100] Swoole\Coroutine->sleep(0.500000) [internal function]
[0x7ffff14870a0] {closure}() /home/shiguangqi/php/swoole-src/examples/coroutine/exception/test.php:3
[0x7ffff141e0c0] go(object[0x7ffff141e110]) [internal function]
[0x7ffff141e030] (main) /home/shiguangqi/php/swoole-src/examples/coroutine/exception/test.php:10
打印全局协程的状态
(gdb) co_status
stack_size: 2097152
call_stack_size: 1
active: 1
coro_num: 2
max_coro_num: 3000
peak_coro_num: 2
PHP代码调试
遍历当前进程内的所有协程,并打印调用栈。
function Coroutine::listCoroutines() : Iterator
需要
4.1.0
或更高版本
- 返回迭代器,可使用
foreach
遍历,或使用iterator_to_array
转为数组
$coros = Coroutine::listCoroutines();
foreach($coros as $cid)
{
var_dump(Coroutine::getBackTrace($cid));
}
可以参考Swoole微课程中的视频教程