목차
简介
调试主要有4种形式
常用调试命令
attach pid
layout
break
continue [num]
next [num]
step [num]
backtrace
print [value]
help
调试php代码
1. 新增一个php文件
2.查看php-fpm work进程PID
3.追踪PID
4.打断点,这里在timelib_update_tsdo_years方法打了一个断点,这里需要你看下php源码,看你需要在哪里调试代码
5.请求测试文件
6.查看调试信息
总结
백엔드 개발 PHP 튜토리얼 gdb를 사용하여 PHP를 디버깅하는 방법을 가르쳐주세요!

gdb를 사용하여 PHP를 디버깅하는 방법을 가르쳐주세요!

Jan 26, 2022 pm 04:32 PM
gdb php

本文给大家介绍关于如何使用gdb调试php(gdb 是c语言的代码调试工具,可以用来调试php、python、mysql等),希望对需要的朋友有所帮助!

简介

gdb 是c语言的代码调试工具
可以用来调试php、python、mysql等

调试主要有4种形式

gdb:启动之后用attach pid 追踪程序
gdb [options] [executable-file [core-file or process-id]]
gdb [options] --args executable-file [inferior-arguments ...]
gdb [options] [--python|-P] script-file [script-arguments ...]
로그인 후 복사

常用调试命令

attach pid

例:如果向跟踪调试mysql代码
1.先找到mysql进行ID:10111
gdb를 사용하여 PHP를 디버깅하는 방법을 가르쳐주세요!

2.再attach 10111追踪mysql

layout

显示源码/汇编指令

Layout names are:
   src      : Displays source and command windows. 显示源码
   asm      : Displays disassembly    and command windows. 显示汇编指令
   split : Displays source, disassembly    and command windows. 显示源码和汇编指令
   regs     : Displays register window. If    existing layout
              is source/command or    assembly/command, the 
              register window is displayed. If the
              source/assembly/command (split) is displayed, 
              the register    window is displayed with 
              the window that has current logical focus
로그인 후 복사

break

  • b 增加断点
  • info b 显示断点信息
  • delete num 删除指定断点

continue [num]

  • c num 执行到num个断点,num可以不填默认=1

next [num]

  • n num 执行到下num行,num可以不填默认=1,不进入函数内部

step [num]

  • s num 执行到下num行,num可以不填默认=1,不进入函数内部

backtrace

  • bt 查看当前调用栈
  • p value 打印变量信息

help

  • help layout 查看命令如何使用

调试php代码

1. 新增一个php文件

<?php echo date(&#39;Y-m-d&#39;, strtotime("last day of +2month", strtotime(&#39;2020-05-31&#39;)));
로그인 후 복사

2.查看php-fpm work进程PID

我这里通过修改php-fpm配置只启动一个work进程方便追踪

pm = static
pm.max_children = 1
로그인 후 복사
[root@test ~]# ps aux|grep php-fpm
www        1127  0.0  0.1 279352  2816 ?        S    5月12   0:00 php-fpm: pool www
root      12224  0.0  0.0 112736   976 pts/0    S+   17:37   0:00 grep --color=auto php-fpm
로그인 후 복사

3.追踪PID

[root@test ~]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-115.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http:>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http:></http:>.
(gdb) attach 1127
Attaching to process 1127
Reading symbols from /usr/local/php/sbin/php-fpm...done.
Reading symbols from /usr/lib64/libcrypt.so.1...Reading symbols 
from /usr/lib/debug/usr/lib64/libcrypt-2.17.so.debug...done.
done.</http:>
로그인 후 복사

4.打断点,这里在timelib_update_tsdo_years方法打了一个断点,这里需要你看下php源码,看你需要在哪里调试代码

(gdb) b timelib_update_ts
Breakpoint 1 at 0x48ba90: file /opt/lnmp1.6/src/php-7.3.11/ext/date/lib/tm2unixtime.c, line 499.
로그인 후 복사
(gdb) b do_years
`Breakpoint 3 at 0x48bb95: file /opt/lnmp1.6/src/php-7.3.11/ext/date/lib/tm2unixtime.c, line 381.`
로그인 후 복사

5.请求测试文件

请求之后发现没有立刻看到返回结果,被阻塞在了这里,说明执行到了断点的地方

[root@test ~]# curl localhost/3.php
로그인 후 복사

6.查看调试信息

通过p *time可以看到变量time里面的内容

(gdb) c
Continuing.

Breakpoint 1, timelib_update_ts (time=time@entry=0x7fc63ec02000, tzi=tzi@entry=0x7fc63ec75000) 
at /opt/lnmp1.6/src/php-7.3.11/ext/date/lib/tm2unixtime.c:499
499    {
(gdb) c
Continuing.

Breakpoint 3, timelib_update_ts (time=time@entry=0x7fc63ec02000, tzi=tzi@entry=0x7fc63ec75000) 
at /opt/lnmp1.6/src/php-7.3.11/ext/date/lib/tm2unixtime.c:505
505        res += do_years(time->y);
(gdb) s
do_years (year=2020) at /opt/lnmp1.6/src/php-7.3.11/ext/date/lib/tm2unixtime.c:381
381        eras = (year - 1970) / 40000;
(gdb) s
timelib_update_ts (time=time@entry=0x7fc63ec02000, tzi=tzi@entry=0x7fc63ec75000) 
at /opt/lnmp1.6/src/php-7.3.11/ext/date/lib/tm2unixtime.c:504
504        do_adjust_special(time);
(gdb) p *time
$1 = {y = 2020, m = 7, d = 31, h = 0, i = 0, s = 0, us = 0, z = 28800, tz_abbr = 0x7fc63ec71018 "CST", 
tz_info = 0x7fc63ec75000, dst = 0, relative = {y = 0, m = 2, 
    d = 0, h = 0, i = 0, s = 0, us = 0, weekday = 0, weekday_behavior = 0, first_last_day_of = 2, invert = 0, 
    days = -99999, special = {type = 0, amount = 0}, 
    have_weekday_relative = 0, have_special_relative = 0}, sse = 0, have_time = 0, have_date = 0, have_zone = 0, 
    have_relative = 1, have_weeknr_day = 0, 
  sse_uptodate = 0, tim_uptodate = 0, is_localtime = 1, zone_type = 3}
(gdb)
로그인 후 복사

下面是do_years方法的代码

static timelib_sll do_years(timelib_sll year)
{
    timelib_sll i;
    timelib_sll res = 0;
    timelib_sll eras;

    eras = (year - 1970) / 40000;
    if (eras != 0) {
        year = year - (eras * 40000);
        res += (SECS_PER_ERA * eras * 100);
    }
    
    if (year >= 1970) {
        for (i = year - 1; i >= 1970; i--) {
            //判断是否是闰年,闰年366天,平年365天
            if (timelib_is_leap(i)) {
                res += (DAYS_PER_LYEAR * SECS_PER_DAY);
            } else {
                res += (DAYS_PER_YEAR * SECS_PER_DAY);
            }
        }
    } else {
        for (i = 1969; i >= year; i--) {
            if (timelib_is_leap(i)) {
                res -= (DAYS_PER_LYEAR * SECS_PER_DAY);
            } else {
                res -= (DAYS_PER_YEAR * SECS_PER_DAY);
            }
        }
    }
    return res;
}
로그인 후 복사

总结

通过gdb追踪很方便我们debug代码信息,查看底层代码跳用栈,对学习源码有很大的帮助
这里也总结下php strtotime方法的实现逻辑
如果当前年>=1970,则循环判断[1970-(当前年-1)]中每一年是否是闰年,是闰年则86400366,不是则86400355  (86400是一天的秒数),月天时分秒计算逻辑不再累述,最后还会加上/减去时区,上海是东八区会减去8小时。
东八区(UTC/GMT+08:00)是比世界协调时间(UTC)/格林尼治时间(GMT)快8小时的时区,
附php代码实现年转化成时间戳
<?php const YEARLEEP = 366;//闰年366天
const YEAR     = 365;//平年365天

//判断是否是闰年
function is_leap($year)
{
    return ($year % 4 == 0) && ($year % 100 != 0 || $year % 400 == 0);
}

//将年转换成时间戳
function getStime($year)
{
    $res = 0;
    for ($i = $year - 1; $i >= 1970; $i--) {
        if (is_leap($i)) {
            $res += YEARLEEP * 86400;
        } else {
            $res += YEAR * 86400;
        }
    }
    //上海是东八区要减8小时
    $res -= 8 * 3600;
    return $res;
}

echo getStime('2020');
로그인 후 복사

推荐学习:《PHP视频教程

위 내용은 gdb를 사용하여 PHP를 디버깅하는 방법을 가르쳐주세요!의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

이번 장에서는 CakePHP의 환경 변수, 일반 구성, 데이터베이스 구성, 이메일 구성에 대해 알아봅니다.

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

PHP 8.4는 상당한 양의 기능 중단 및 제거를 통해 몇 가지 새로운 기능, 보안 개선 및 성능 개선을 제공합니다. 이 가이드에서는 Ubuntu, Debian 또는 해당 파생 제품에서 PHP 8.4를 설치하거나 PHP 8.4로 업그레이드하는 방법을 설명합니다.

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

cakephp4에서 날짜와 시간을 다루기 위해 사용 가능한 FrozenTime 클래스를 활용하겠습니다.

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

파일 업로드 작업을 위해 양식 도우미를 사용할 것입니다. 다음은 파일 업로드의 예입니다.

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

이번 장에서는 라우팅과 관련된 다음과 같은 주제를 학습하겠습니다.

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP는 PHP용 오픈 소스 프레임워크입니다. 이는 애플리케이션을 훨씬 쉽게 개발, 배포 및 유지 관리할 수 있도록 하기 위한 것입니다. CakePHP는 강력하고 이해하기 쉬운 MVC와 유사한 아키텍처를 기반으로 합니다. 모델, 뷰 및 컨트롤러 gu

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

VS Code라고도 알려진 Visual Studio Code는 모든 주요 운영 체제에서 사용할 수 있는 무료 소스 코드 편집기 또는 통합 개발 환경(IDE)입니다. 다양한 프로그래밍 언어에 대한 대규모 확장 모음을 통해 VS Code는

CakePHP 유효성 검사기 만들기 CakePHP 유효성 검사기 만들기 Sep 10, 2024 pm 05:26 PM

컨트롤러에 다음 두 줄을 추가하면 유효성 검사기를 만들 수 있습니다.

See all articles