Table of Contents
Use GDB to debug PHP code and solve the problem of infinite loop of PHP code. gdbphp
Home Backend Development PHP Tutorial Use GDB to debug PHP code and solve the problem of infinite loop in PHP code, gdbphp_PHP tutorial

Use GDB to debug PHP code and solve the problem of infinite loop in PHP code, gdbphp_PHP tutorial

Jul 13, 2016 am 10:05 AM
gdb infinite loop debug php code

Use GDB to debug PHP code and solve the problem of infinite loop of PHP code. gdbphp

Recently, when helping colleagues solve Swoole Server problems, I found that 1 worker process has been in R state. , and the CPU consumption is very high. The preliminary conclusion is that an infinite loop occurs in the PHP code.

The following code shows how to solve the PHP infinite loop problem.

Copy code The code is as follows:
#dead_loop.php
$array = array();
for($i = 0; $i < 10000; $i++)
{
$array[] = $i;
}
include __DIR__."/include.php";
#include.php
while(1)
{
usleep(10);
$keys = array_flip($array);
$index = array_search(rand(1500, 9999), $array);
$str = str_repeat('A', $index);
$strb = test($index, $str);
}
function test($index, $str)
{
Return str_replace('A', 'B', $str);
}

Get the process ID and status through ps aux as follows, use gdb -p process ptrace tracking, and get the call stack through the bt command

Copy code The code is as follows:
htf 3834 2.6 0.2 166676 22060 pts/12 R+ 10:50 0:12 php dead_loop.php
gdb -p 3834
(gdb) bt
#0 0x00000000008cc03f in zend_mm_check_ptr (heap=0x1eaa2c0, ptr=0x2584910, silent=1, __zend_filename=0xee3d40 "/home/htf/workspace/php-5.4.27/Zend/zend_variables.c",
__zend_lineno=182, __zend_orig_filename=0xee1888 "/home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c", __zend_orig_lineno=437)
at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:1485
#1 0x000000008cd643 in _zend_mm_free_int (heap = 0x1eaa2c0, P = 0x2584910, __ZEND_FILENAME = 0xee3d40 "/HOMESPACE/php-5.4.27/zend_v ariables.c ", __zend_lineno = 182,
__zend_orig_filename=0xee1888 "/home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c", __zend_orig_lineno=437) at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:2064
#2 0x00000000008cebf7 in _efree (ptr=0x2584910, __zend_filename=0xee3d40 "/home/htf/workspace/php-5.4.27/Zend/zend_variables.c", __zend_lineno=182,
__zend_orig_filename=0xee1888 "/home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c", __zend_orig_lineno=437) at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:2436
#3 0x00000000008eda0a in _zval_ptr_dtor (zval_ptr=0x25849a0, __zend_filename=0xee3d40 "/home/htf/workspace/php-5.4.27/Zend/zend_variables.c", __zend_lineno=182)
at /home/htf/workspace/php-5.4.27/Zend/zend_execute_API.c:437
#4 0x00000000008fe687 in _zval_ptr_dtor_wrapper (zval_ptr=0x25849a0) at /home/htf/workspace/php-5.4.27/Zend/zend_variables.c:182
#5 0x000000000091259f in zend_hash_destroy (ht=0x7f7263f6e380) at /home/htf/workspace/php-5.4.27/Zend/zend_hash.c:560
#6 0x00000000008fe2c5 in _zval_dtor_func (zvalue=0x7f726426fe50, __zend_filename=0xeea290 "/home/htf/workspace/php-5.4.27/Zend/zend_execute.c", __zend_lineno=901)
at /home/htf/workspace/php-5.4.27/Zend/zend_variables.c:45
#7 0x0000000000936656 in _zval_dtor (zvalue=0x7f726426fe50, __zend_filename=0xeea290 "/home/htf/workspace/php-5.4.27/Zend/zend_execute.c", __zend_lineno=901)
at /home/htf/workspace/php-5.4.27/Zend/zend_variables.h:35
#8 0x0000000000939747 in zend_assign_to_variable (variable_ptr_ptr=0x7f7263f8e738, value=0x7f726426f6a8) at /home/htf/workspace/php-5.4.27/Zend/zend_execute.c:901
#9 0x0000000000997ee5 in ZEND_ASSIGN_SPEC_CV_VAR_HANDLER (execute_data=0x7f726d04b2a8) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:33168
#10 0x000000000093b5fd in execute (op_array=0x21d58b0) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:410
#11 0x0000000000901692 in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /home/htf/workspace/php-5.4.27/Zend/zend.c:1315
#12 0x000000000087926a in php_execute_script (primary_file=0x7ffffe0038d0) at /home/htf/workspace/php-5.4.27/main/main.c:2502
#13 0x00000000009a32e3 in do_cli (argc=2, argv=0x7ffffe004d18) at /home/htf/workspace/php-5.4.27/sapi/cli/php_cli.c:989
#14 0x00000000009a4491 in main (argc=2, argv=0x7ffffe004d18) at /home/htf/workspace/php-5.4.27/sapi/cli/php_cli.c:1365

After executing gdb, the endless loop process will change to T status, indicating that it is Trace. This is exclusive, so you can no longer use strace/gdb or other ptrace tools to debug this process. Additionally this process will interrupt execution. After gdb enters c, the program continues to run downward. Then press ctrl+c again to interrupt the program. View the call stack of the process through the bt command.

复制代码 代码如下:
(gdb) bt
#0 _zend_mm_alloc_int (heap=0x1eaa2c0, size=72, __zend_filename=0xe43410 "/home/htf/workspace/php-5.4.27/ext/standard/array.c", __zend_lineno=2719,
__zend_orig_filename=0xee5a38 "/home/htf/workspace/php-5.4.27/Zend/zend_hash.c", __zend_orig_lineno=412) at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:1895
#1 0x00000000008ceb86 in _emalloc (size=72, __zend_filename=0xe43410 "/home/htf/workspace/php-5.4.27/ext/standard/array.c", __zend_lineno=2719,
__zend_orig_filename=0xee5a38 "/home/htf/workspace/php-5.4.27/Zend/zend_hash.c", __zend_orig_lineno=412) at /home/htf/workspace/php-5.4.27/Zend/zend_alloc.c:2425
#2 0x0000000000911d85 in _zend_hash_index_update_or_next_insert (ht=0x2257a10, h=3972, pData=0x7ffffe0012b0, nDataSize=8, pDest=0x0, flag=1,
__zend_filename=0xe43410 "/home/htf/workspace/php-5.4.27/ext/standard/array.c", __zend_lineno=2719) at /home/htf/workspace/php-5.4.27/Zend/zend_hash.c:412
#3 0x00000000007767e1 in zif_array_flip (ht=1, return_value=0x7f726424ea68, return_value_ptr=0x0, this_ptr=0x0, return_value_used=1)
at /home/htf/workspace/php-5.4.27/ext/standard/array.c:2719
#4 0x000000000093c03e in zend_do_fcall_common_helper_SPEC (execute_data=0x7f726d04b2a8) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:643
#5 0x00000000009400e6 in ZEND_DO_FCALL_SPEC_CONST_HANDLER (execute_data=0x7f726d04b2a8) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:2233
#6 0x000000000093b5fd in execute (op_array=0x21d58b0) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:410

两次的BT信息不一样,这是因为程序在不同的位置中断。看到execute (oparray=0x21d58b0) 这一行,这里就是PHP执行oparray的入口了。gdb下输入f 6,(通过调用栈编号可得)。

复制代码 代码如下:
(gdb) f 6
#6 0x000000000093b5fd in execute (op_array=0x21d58b0) at /home/htf/workspace/php-5.4.27/Zend/zend_vm_execute.h:410
410 if ((ret = OPLINE->handler(execute_data TSRMLS_CC)) > 0) {
(gdb) p *op_array
$2 = {type = 2 '

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Common configuration techniques for debugging Linux kernel using GDB Common configuration techniques for debugging Linux kernel using GDB Jul 05, 2023 pm 01:54 PM

Common configuration techniques for using GDB to debug the Linux kernel Introduction: In Linux development, using GDB to debug the kernel is a very important skill. GDB is a powerful debugging tool that can help developers quickly locate and solve bugs in the kernel. This article will introduce some common GDB configuration techniques and how to use GDB to debug the Linux kernel. 1. Configure the GDB environment First, we need to configure the GDB environment on the Linux system. Please make sure your system has the GDB tool installed

Common configuration methods for using GDB to debug embedded ARM programs under Linux Common configuration methods for using GDB to debug embedded ARM programs under Linux Jul 05, 2023 am 08:10 AM

Common configuration methods for using GDB to debug embedded ARM programs under Linux. As a special computer system, embedded systems are usually integrated in electronic devices and used to control and manage hardware resources. In order to debug and analyze the operation of embedded systems, we need to use specialized tools. Among them, GDB is a commonly used open source debugger that can run on embedded systems and communicate with programs. This article will introduce common configuration methods for using GDB to debug embedded ARM programs under Linux and give code examples.

How to debug Golang functions using GDB? How to debug Golang functions using GDB? Apr 18, 2024 am 08:00 AM

The steps to use GDB to debug Go functions are as follows: Install GDB and Go debugging package. Compile the program with debug information enabled. Use GDB to launch the program. Set breakpoints. Use the run command to start the program. Use debug commands to debug.

How to solve the infinite loop problem in C++ development How to solve the infinite loop problem in C++ development Aug 22, 2023 am 08:53 AM

How to solve the infinite loop problem in C++ development. In C++ development, the infinite loop is a very common but very difficult problem. When a program falls into an infinite loop, it will cause the program to fail to execute normally, and may even cause the system to crash. Therefore, solving infinite loop problems is one of the essential skills in C++ development. This article will introduce some common methods to solve the infinite loop problem. Checking Loop Conditions One of the most common causes of endless loops is incorrect loop conditions. When the loop condition is always true, the loop will continue to execute, resulting in an infinite loop.

Common configuration methods for using GDB to debug multi-threaded programs under Linux Common configuration methods for using GDB to debug multi-threaded programs under Linux Jul 04, 2023 pm 02:49 PM

Common configuration methods for using GDB to debug multi-threaded programs under Linux Introduction: In multi-threaded programming, debugging is an essential task. GDB is a powerful debugger that can help us locate and solve errors in multi-threaded programs. This article will introduce common configuration methods for using GDB to debug multi-threaded programs under Linux, and provide code examples, hoping to help readers better understand and use GDB. 1. Install GDB First, we need to install GDB in the Linux system. Enter in the terminal

Common debugging tools and techniques for Linux systems Common debugging tools and techniques for Linux systems Feb 23, 2024 pm 02:40 PM

In Linux systems, debugging is a crucial part of the program development and maintenance process. In order to assist developers in debugging more effectively, Linux provides a variety of powerful debugging tools and technologies. This article will briefly introduce some commonly used Linux debugging tools and techniques to help developers debug better. 1. Debugging Tools 1.gdb In Linux systems, gdb is widely used as one of the debugging tools. It helps developers track the cause of program crashes and provides a series of commands to check program status, modify variables, and execute code. operate. To start debugging, you can use the following command. $gdb./program where `./program` represents the executable file to be debugged. Once gdb starts,

How to automatically fix the infinite loop in win10 How to automatically fix the infinite loop in win10 Dec 27, 2023 pm 04:04 PM

Win10's automatic repair loop is a common problem for many users, and it has caused a lot of trouble for most users. So is there a button that can directly let him exit? Let’s take a look at the detailed tutorial below. Win10 automatically repairs the endless loop. Press f to jump out: it mainly depends on the brand of the computer. Different brands have different buttons. The commonly used ones are F9 and F11. Lenovo's is usually F12. When the Lenovo LOGO screen appears when the computer is turned on, press F2 to enter the Lenovo computer. To save the system, select Lenovo One-Key Recovery at this time, or directly press the "R" key on the keyboard. At this time, another screen will appear. Press the left and right keys to select "Continue". At this time, you will enter "Lenovo One-key Recovery". There are three options when entering, namely system recovery, system backup and backup uninstall, according to

How to solve: Java algorithm error: infinite loop How to solve: Java algorithm error: infinite loop Aug 25, 2023 pm 10:12 PM

How to solve: Java algorithm error: infinite loop Introduction: In the process of writing Java programs, we often encounter various errors and exceptions. Among them, infinite loop is a common problem, which will cause the program to fall into an infinite loop state, causing the program to fail to execute normally. In this article, we will discuss how to solve the infinite loop problem in Java algorithms and provide some sample code. 1. The definition and cause of an infinite loop: An infinite loop refers to a loop structure in a program that has no conditions for normal termination, causing the program to execute in an infinite loop within this loop.

See all articles