PHP获取函数所有调用栈(debug_backtrace)

更新时间:2023-07-14 16:27:42 阅读: 评论:0

PHP获取函数所有调⽤栈(debug_backtrace)
应⽤场景
有的时候调试框架业务代码,尤其是框架代码,⾮常⿇烦,你从⼊⼝⽂件开始调试,可能要跑⼏⼗个⽂件才能到达你最后书写逻辑的控制器,流程类似
A->B->C->D->E->F->G->H->I->J->K (K代表最终执⾏的控制器⽅法)
那我就快速知道是哪个地⽅调⽤了K⽅法,K的上级调⽤栈有哪些,就可以使⽤debug_backtrace,以Laravel框架为例,打
开debug_backtrace(2)以后,可以看到上级调⽤栈有43个,具体是从从哪⼀⾏的哪个⽅法调⽤类进来的⼀⽬了然,也⽅便调试
函数原型
debug_backtrace ( int $options=DEBUG_BACKTRACE_PROVIDE_OBJECT, int $limit=0):array
// debug_backtrace() 产⽣⼀条 PHP 的回溯跟踪(backtrace)。
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)等同于 `debug_backtrace(2)`
这个函数有俩参数
截⾄ 5.3.6,这个参数是以下选项的位掩码:
options
DEBUG_BACKTRACE_PROVIDE_OBJECT 是否填充 “object” 的索引。
上海巨良DEBUG_BACKTRACE_IGNORE_ARGS 是否忽略 “args” 的索引,包括所有的 function/method 的参数,能够节省内存开销。在 5.3.6 之前,仅仅能使⽤的值是 true 或者 fal,分别等于是否设置 DEBUG_BACKTRACE_PROVIDE_OBJECT 选项。
limit
截⾄ 5.4.0,这个参数能够⽤于限制返回堆栈帧的数量。 默认为 (limit=0) ,返回所有的堆栈帧。
⽰例
下⾯以Laravel框架为例,我在IndexController中打印了⼀下这个⽅法,由此可以找到在IndexControlle
电商风云r之前的所有调⽤链路,也便于快速排查问题
Array
(
[0]=>Array
(
[function]=> index
[class]=> App\Http\Controllers\IndexController
[type]=>->
篮球招式)
[1]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Controller.php
[line]=>54
[function]=> call_ur_func_array
)
[2]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher.php
[line]=>45
[function]=> callAction
[class]=> Illuminate\Routing\Controller
[type]=>->
)
[3]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Route.php
[line]=>239
[function]=> dispatch
[function]=> dispatch
[class]=> Illuminate\Routing\ControllerDispatcher
[type]=>->
)
[4]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Route.php
[line]=>196
[function]=> runController
[class]=> Illuminate\Routing\Route
[type]=>->
)
[5]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php
[line]=>685
[function]=> run
[class]=> Illuminate\Routing\Route
[type]=>->
)
[6]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
藕粉好处[line]=>128
[function]=> Illuminate\Routing\{closure}
[class]=> Illuminate\Routing\Router
[type]=>->
)
激烈的反义词
[7]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php [line]=>41
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[8]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167
[function]=> handle
[class]=> Illuminate\Routing\Middleware\SubstituteBindings
[type]=>->
)
[9]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken.php [line]=>77
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[10]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167
[function]=> handle
[class]=> Illuminate\Foundation\Http\Middleware\VerifyCsrfToken
[type]=>->
[type]=>->
画画用英语怎么说)
[11]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\View\Middleware\ShareErrorsFromSession.php
[line]=>49
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[12]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167
[function]=> handle
[class]=> Illuminate\View\Middleware\ShareErrorsFromSession
[type]=>->
)
[13]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php
[line]=>116
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[14]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Session\Middleware\StartSession.php
[line]=>62
[function]=> handleStatefulRequest
[class]=> Illuminate\Session\Middleware\StartSession
[type]=>->
)
[15]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167
[function]=> handle
[class]=> Illuminate\Session\Middleware\StartSession
[type]=>->
)
[16]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\AddQueuedCookiesToRespon.php [line]=>37
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[17]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167
[function]=> handle
[class]=> Illuminate\Cookie\Middleware\AddQueuedCookiesToRespon
[type]=>->
)
[18]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Cookie\Middleware\EncryptCookies.php [line]=>67
[function]=> Illuminate\Pipeline\{closure}
学生综合素质测评自我总结
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[19]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167
[function]=> handle
[class]=> Illuminate\Cookie\Middleware\EncryptCookies
[type]=>->
)
[20]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>103
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[21]=>Array
罗马杆窗帘(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php
[line]=>687
[function]=> then
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[22]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php
[line]=>662
[function]=> runRouteWithinStack
[class]=> Illuminate\Routing\Router
[type]=>->
)
[23]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php
[line]=>628
[function]=> runRoute
[class]=> Illuminate\Routing\Router
[type]=>->
)
[24]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php
[line]=>617
[function]=> dispatchToRoute
[class]=> Illuminate\Routing\Router
[type]=>->
)
[25]=>Array
(
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php
[line]=>165
[function]=> dispatch
[class]=> Illuminate\Routing\Router
[type]=>->
)
[26]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>128
[function]=> Illuminate\Foundation\Http\{closure}
[class]=> Illuminate\Foundation\Http\Kernel
[type]=>->
)
[27]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php [line]=>21
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[28]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167
[function]=> handle
[class]=> Illuminate\Foundation\Http\Middleware\TransformsRequest
[type]=>->
)
[29]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php [line]=>21
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[30]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167
[function]=> handle
[class]=> Illuminate\Foundation\Http\Middleware\TransformsRequest
[type]=>->
)
[31]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php [line]=>27
[function]=> Illuminate\Pipeline\{closure}
[class]=> Illuminate\Pipeline\Pipeline
[type]=>->
)
[32]=>Array
(
[file]=> C:\phpstudy\WWW\laravel\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php
[line]=>167

本文发布于:2023-07-14 16:27:42,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1096427.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:参数   能够   是否   便于   调试   返回
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图