找出php中可能有问题的代码行

2024-09-08 14:18
文章标签 代码 问题 php 可能 找出

本文主要是介绍找出php中可能有问题的代码行,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

当你发现一个平时占用cpu比较少的进程突然间占用cpu接近100%时,你如何找到导致cpu飙升的原因?我的思路是,首先找到进程正在执行的代码行,从而确定可能有问题的代码段。然后,再仔细分析有问题的代码段,从而找出原因。

如果你的程序使用的是c、c++编写,那么你可以很容易的找到正在执行的代码行。但是,程序是php编写的,如何找到可能有问题的代码行呢?这个问题就是本文要解决的问题。

背景知识:

大家都知道php是一个解释性语言。用户编写的php代码会生成opcode,由解释器引擎去解释执行。在解释执行过程中,有一个全局变量包含了执行过程中用到的各种数据。它就是executor_globals。在源码的Zend/zend_globals.h 文件中可以找到他的类型定义。

struct _zend_executor_globals {zval **return_value_ptr_ptr;zval uninitialized_zval;zval *uninitialized_zval_ptr;zval error_zval;zval *error_zval_ptr;zend_ptr_stack arg_types_stack;/* symbol table cache */HashTable *symtable_cache[SYMTABLE_CACHE_SIZE];HashTable **symtable_cache_limit;HashTable **symtable_cache_ptr;zend_op **opline_ptr;HashTable *active_symbol_table;HashTable symbol_table;     /* main symbol table */HashTable included_files;   /* files already included */JMP_BUF *bailout;int error_reporting;int orig_error_reporting;int exit_status;zend_op_array *active_op_array;HashTable *function_table;  /* function symbol table */HashTable *class_table;     /* class table */HashTable *zend_constants;  /* constants table */zend_class_entry *scope;zend_class_entry *called_scope; /* Scope of the calling class */zval *This;long precision;int ticks_count;zend_bool in_execution;HashTable *in_autoload;zend_function *autoload_func;zend_bool full_tables_cleanup;/* for extended information support */zend_bool no_extensions;#ifdef ZEND_WIN32zend_bool timed_out;OSVERSIONINFOEX windows_version_info;
#endifHashTable regular_list;HashTable persistent_list;zend_vm_stack argument_stack;int user_error_handler_error_reporting;zval *user_error_handler;zval *user_exception_handler;zend_stack user_error_handlers_error_reporting;zend_ptr_stack user_error_handlers;zend_ptr_stack user_exception_handlers;zend_error_handling_t  error_handling;zend_class_entry      *exception_class;/* timeout support */int timeout_seconds;int lambda_count;HashTable *ini_directives;HashTable *modified_ini_directives;zend_objects_store objects_store;zval *exception, *prev_exception;zend_op *opline_before_exception;zend_op exception_op[3];struct _zend_execute_data *current_execute_data;struct _zend_module_entry *current_module;zend_property_info std_property_info;zend_bool active; void *saved_fpu_cw;void *reserved[ZEND_MAX_RESERVED_RESOURCES];
};

这里我们只说两个对我们比较重要的变量,active_op_arraycurrent_execute_data
active_op_array变量中保存了引擎正在执行的op_array(想了解什么是op_array请点击查看)。在Zend/zend_compile.h中有关于op_array的数据类型的定义。

struct _zend_op_array {/* Common elements */zend_uchar type;char *function_name;        zend_class_entry *scope;zend_uint fn_flags;union _zend_function *prototype;zend_uint num_args;zend_uint required_num_args;zend_arg_info *arg_info;zend_bool pass_rest_by_reference;unsigned char return_reference;/* END of common elements */zend_bool done_pass_two;zend_uint *refcount;zend_op *opcodes;zend_uint last, size;zend_compiled_variable *vars;int last_var, size_var;zend_uint T;zend_brk_cont_element *brk_cont_array;int last_brk_cont;int current_brk_cont;zend_try_catch_element *try_catch_array;int last_try_catch;/* static variables support */HashTable *static_variables;zend_op *start_op;int backpatch_count;zend_uint this_var;char *filename;zend_uint line_start;zend_uint line_end;char *doc_comment;zend_uint doc_comment_len;zend_uint early_binding; /* the linked list of delayed declarations */void *reserved[ZEND_MAX_RESERVED_RESOURCES];
};

看完定义,就不用我多说了把。定义中,filenamefunction_name分别保存了正在执行的文件名和方法名。
current_execute_data保存了正在执行的op_arrayexecute_dataexecute_data保存了每个op_array执行过程中的一些数据。其定义在,Zend/zend_compile.h

struct _zend_execute_data {  struct _zend_op *opline;  zend_function_state function_state;  zend_function *fbc; /* Function Being Called */ zend_class_entry *called_scope;  zend_op_array *op_array;  zval *object;  union _temp_variable *Ts;  zval ***CVs;  HashTable *symbol_table;  struct _zend_execute_data *prev_execute_data;  zval *old_error_reporting;  zend_bool nested;  zval **original_return_value;  zend_class_entry *current_scope;  zend_class_entry *current_called_scope;  zval *current_this;  zval *current_object;  struct _zend_op *call_opline;  
};  

定义中的opline就是正在执行的opcode。opcode的结构定义如下:

struct _zend_op {opcode_handler_t handler;znode result;znode op1;znode op2;ulong extended_value;uint lineno;zend_uchar opcode;
};

其中lineno就是opcode所对应的行号。

示例说明:

看完上面的数据结构定义,你是否已经知道如何找php正在执行的文件名,方法名和行号呢?如果还有疑问的话,那就接着看下面的例子。创建一个文件test.php,代码如下:

<?php
function test(){while(true){sleep(1);}
}
test1();
?>

cli方式执行php脚本,加入执行的进程号为14973。我们使用gdb命令来调试进程。

$sudo gdb -p 14973
(gdb) print (char *)executor_globals.active_op_array->filename
$1 = 0x9853a34 "/home/server/test/php/test.php"
(gdb) print (char *)executor_globals.active_op_array->function_name
$2 = 0x9854db8 "test"
(gdb) print executor_globals->current_execute_data->opline->lineno
$3 = 4

很显然,他正在执行第四行的sleep方法。

如果上面的方法你感觉麻烦,那你可以使用.gdbinit文件。这个文件在php源码的根目录下。使用方法如下:

$sudo gdb -p 14973
(gdb) source /home/server/.gdbinit 
(gdb) zbacktrace
[0xa453f34] sleep(1) /home/server/test/php/test.php:4 
[0xa453ed0] test() /home/server/test/php/test.php:7 
(gdb) 

题外话:

从php5.6开始,php中集成了一个phpdbg的工具。可以像gdb调试c语言程序一样,调试php程序。感兴趣的话,可以打开下面的连接看看。
https://wiki.php.net/rfc/phpdbg
http://phpdbg.com/docs

当cpu飙升时,找出php中可能有问题的代码行

这篇关于找出php中可能有问题的代码行的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1148337

相关文章

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.

mybatis和mybatis-plus设置值为null不起作用问题及解决

《mybatis和mybatis-plus设置值为null不起作用问题及解决》Mybatis-Plus的FieldStrategy主要用于控制新增、更新和查询时对空值的处理策略,通过配置不同的策略类型... 目录MyBATis-plusFieldStrategy作用FieldStrategy类型每种策略的作

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

Python Jupyter Notebook导包报错问题及解决

《PythonJupyterNotebook导包报错问题及解决》在conda环境中安装包后,JupyterNotebook导入时出现ImportError,可能是由于包版本不对应或版本太高,解决方... 目录问题解决方法重新安装Jupyter NoteBook 更改Kernel总结问题在conda上安装了

pip install jupyterlab失败的原因问题及探索

《pipinstalljupyterlab失败的原因问题及探索》在学习Yolo模型时,尝试安装JupyterLab但遇到错误,错误提示缺少Rust和Cargo编译环境,因为pywinpty包需要它... 目录背景问题解决方案总结背景最近在学习Yolo模型,然后其中要下载jupyter(有点LSVmu像一个

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

如何解决Pycharm编辑内容时有光标的问题

《如何解决Pycharm编辑内容时有光标的问题》文章介绍了如何在PyCharm中配置VimEmulator插件,包括检查插件是否已安装、下载插件以及安装IdeaVim插件的步骤... 目录Pycharm编辑内容时有光标1.如果Vim Emulator前面有对勾2.www.chinasem.cn如果tools工

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

最长公共子序列问题的深度分析与Java实现方式

《最长公共子序列问题的深度分析与Java实现方式》本文详细介绍了最长公共子序列(LCS)问题,包括其概念、暴力解法、动态规划解法,并提供了Java代码实现,暴力解法虽然简单,但在大数据处理中效率较低,... 目录最长公共子序列问题概述问题理解与示例分析暴力解法思路与示例代码动态规划解法DP 表的构建与意义动