本文给大家介绍关于如何使用gdb调试php(gdb 是c语言的代码调试工具,可以用来调试php、python、mysql等),希望对需要的朋友有所帮助!4.打断点,这里在
简介
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
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 focusbreak
- 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 查看当前调用栈
print [value]
- p value 打印变量信息
help
- help layout 查看命令如何使用
调试php代码
1. 新增一个php文件
<?php
echo date('Y-m-d', strtotime("last day of +2month", strtotime('2020-05-31')));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://gnu.org/licenses/gpl.html> 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://www.gnu.org/software/gdb/bugs/>. (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.
4.打断点,这里在timelib_update_ts和do_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视频教程》
相关阅读 更多
-
抖音网页版官方入口 抖音网页版在线观看官网 时间:2026-01-08 -
GitLab、Git、Github、Gitee四者的区别 时间:2026-01-08 -
顺磁性和逆磁性的区别 顺磁性和逆磁性的判断方法 时间:2026-01-08 -
顺磁性是什么意思 顺磁性材料有哪些 时间:2026-01-08 -
usim卡是什么意思 usim卡和sim卡的区别 时间:2026-01-08 -
Web前端四大渲染模式SSR、CSR、ISR、SSG的定义和区别 时间:2026-01-08
今日更新
-
腾讯视频会议app下载手机版-腾讯会议平台免费下载安装
阅读:18
-
哔咔漫画安卓版下载安装-哔咔漫画最新版本下载入口
阅读:18
-
刘富贵的梗是什么梗揭秘网络热梗背后的搞笑故事和流行原因
阅读:18
-
qq朋友网登录入口-qq朋友网网页版一键登录
阅读:18
-
刘梗宏女孩是什么梗?揭秘网络爆火健身主播的魔性口号与粉丝狂欢现象,看完秒懂!
阅读:18
-
啵乐漫画app最新版本下载-啵乐漫画官方安装包免费下载
阅读:18
-
Freeok免费追剧app下载安装-Freeok免费追剧软件官方版本
阅读:18
-
哔哩哔哩漫画网页版官方入口-哔哩哔哩漫画官网一键直达
阅读:18
-
七夕漫画官方下载入口安卓最新版-七夕漫画官方下载入口免费安装包
阅读:18
-
夸克网页版入口-夸克浏览器2026最新网页版登录
阅读:18










