背景

在 https://mengkang.net/1328.html 实验中,我们通过optimizer_trace发现group by会使用intermediate_tmp_table,而且里面的的row_length是20,抱着"打破砂锅问到底"的求学精神,所以想通过 gdb 调试源码的方式看这个row_length为什么是20.

通过row_length关键字,我定位到了mysql 5.7 源码里面的sql/sql_tmp_table.cc文件

实际操作

查找 mysql pid

[root@localhost ~]# ps -ef|grep mysql
root      3739     1  0 09:36 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/localhost.localdomain.pid
mysql     3894  3739  0 09:36 ?        00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/lib/mysql/localhost.localdomain.pid --socket=/var/lib/mysql/mysql.sock
root      3956  3940  0 09:48 pts/1    00:00:00 mysql -uroot -px xxxx
root      4002  3985  0 10:11 pts/2    00:00:00 grep --color=auto mysql

启动 gdb

[root@localhost ~]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.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/>.

attach pid

(gdb) attach 3894

设置断点

(gdb) b trace_tmp_table
Breakpoint 1 at 0x15e1eeb: file /root/newdb/mysql-server/sql/sql_tmp_table.cc, line 2300.

或者

(gdb) b /root/newdb/mysql-server/sql/sql_tmp_table.cc:2306
Breakpoint 1 at 0x15e1f8e: file /root/newdb/mysql-server/sql/sql_tmp_table.cc, line 2306.

客户端连接

[root@localhost ~]# mysql -uroot -p123456 test
mysql: [Warning] Using a password on the command line interface can be insecure.

阻塞中

打印 backtrace

(gdb) bt
#0  0x00007effcf4ec20d in poll () from /lib64/libc.so.6
#1  0x0000000001667759 in Mysqld_socket_listener::listen_for_connection_event (this=0x42e6360)at /root/newdb/mysql-server/sql/conn_handler/socket_connection.cc:852
#2  0x0000000000eb14fc in Connection_acceptor<Mysqld_socket_listener>::connection_event_loop (this=0x42ea0a0)at /root/newdb/mysql-server/sql/conn_handler/connection_acceptor.h:66
#3  0x0000000000ea8f7a in mysqld_main (argc=12, argv=0x41a60e8) at /root/newdb/mysql-server/sql/mysqld.cc:5149
#4  0x0000000000ea00ed in main (argc=8, argv=0x7ffece17c7e8) at /root/newdb/mysql-server/sql/main.cc:25

因为主进程在 poll 一直在等待客户端连接请求。

执行 continue

(gdb) c
Continuing.

此时,mysql 客户端则进入 mysql 命令行界面

[root@localhost ~]# mysql -uroot -p123456 test
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -AWelcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-debug Source distributionCopyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

客户端执行查询

SET optimizer_trace='enabled=on';
select aid,sum(pv) as num from article_rank force index(idx_day_aid_pv) where day>20181223 group by aid order by num desc LIMIT 10;

查看断点处信息

[Switching to Thread 0x7f7f20145700 (LWP 4392)]Breakpoint 1, trace_tmp_table (trace=0x7eff94003088, table=0x7eff94937200) at /root/newdb/mysql-server/sql/sql_tmp_table.cc:2306
warning: Source file is more recent than executable.
2306      trace_tmp.add("row_length",table->s->reclength).
(gdb) p table->s->reclength
$1 = 20
(gdb) p table->s->fields
$2 = 2
(gdb) p (*(table->field+0))->field_name
$3 = 0x7eff94010b0c "aid"
(gdb) p (*(table->field+1))->field_name
$4 = 0x7eff94007518 "num"
(gdb) p (*(table->field+0))->row_pack_length()
$5 = 4
(gdb) p (*(table->field+1))->row_pack_length()
$6 = 15
(gdb) p (*(table->field+0))->type()
$7 = MYSQL_TYPE_LONG
(gdb) p (*(table->field+1))->type()
$8 = MYSQL_TYPE_NEWDECIMAL
(gdb)

总结

原来和 group by没关系,只因为我 sql 种使用了sum行数,使得num字段类型是MYSQL_TYPE_NEWDECIMAL

The SUM() and AVG() functions return a DECIMAL value for exact-value arguments (integer or DECIMAL), and a DOUBLE value for approximate-value arguments (FLOAT or DOUBLE). (Before MySQL 5.0.3, SUM() and AVG() return DOUBLE for all numeric arguments.)

但是通过我们上面打印信息可以看到两个字段的长度加起来是19,而reclength是20。通过其他实验也发现table->s->reclength的长度就是table->field数组里面所有字段的字段长度和再加1。

附录

完整的 gdb 调试信息如下

[root@localhost ~]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-114.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 3894
Attaching to process 3894
Reading symbols from /usr/local/mysql/bin/mysqld...done.
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[New LWP 4392]
[New LWP 4368]
[New LWP 4367]
[New LWP 4366]
[New LWP 4365]
[New LWP 4364]
[New LWP 4363]
[New LWP 4362]
[New LWP 4361]
[New LWP 4360]
[New LWP 4359]
[New LWP 4358]
[New LWP 4357]
[New LWP 4356]
[New LWP 4355]
[New LWP 4353]
[New LWP 4352]
[New LWP 4351]
[New LWP 4350]
[New LWP 4349]
[New LWP 4348]
[New LWP 4347]
[New LWP 4346]
[New LWP 4345]
[New LWP 4344]
[New LWP 4343]
[New LWP 4342]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libcrypt.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libcrypt.so.1
Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /lib64/librt.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/librt.so.1
Reading symbols from /lib64/libstdc++.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libstdc++.so.6
Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/libgcc_s.so.1...(no debugging symbols found)...done.
Loaded symbols for /lib64/libgcc_s.so.1
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /lib64/libfreebl3.so...Reading symbols from /lib64/libfreebl3.so...(no debugging symbols found)...done.
(no debugging symbols found)...done.
Loaded symbols for /lib64/libfreebl3.so
Reading symbols from /lib64/libnss_files.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libnss_files.so.2
0x00007f7f2c32620d in poll () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-260.el7.x86_64 libgcc-4.8.5-36.el7.x86_64 libstdc++-4.8.5-36.el7.x86_64 nss-softokn-freebl-3.36.0-5.el7_5.x86_64
(gdb) b /root/newdb/mysql-server/sql/sql_tmp_table.cc:2306
Breakpoint 1 at 0x15e1f8e: file /root/newdb/mysql-server/sql/sql_tmp_table.cc, line 2306.
(gdb) bt
#0  0x00007f7f2c32620d in poll () from /lib64/libc.so.6
#1  0x0000000001667759 in Mysqld_socket_listener::listen_for_connection_event (this=0x3841360)at /root/newdb/mysql-server/sql/conn_handler/socket_connection.cc:852
#2  0x0000000000eb14fc in Connection_acceptor<Mysqld_socket_listener>::connection_event_loop (this=0x38450a0)at /root/newdb/mysql-server/sql/conn_handler/connection_acceptor.h:66
#3  0x0000000000ea8f7a in mysqld_main (argc=12, argv=0x37010e8) at /root/newdb/mysql-server/sql/mysqld.cc:5149
#4  0x0000000000ea00ed in main (argc=8, argv=0x7ffdaf5bc2e8) at /root/newdb/mysql-server/sql/main.cc:25
(gdb) c
Continuing.
[Switching to Thread 0x7f7f20145700 (LWP 4392)]Breakpoint 1, trace_tmp_table (trace=0x7f7ef8016b18, table=0x7f7ef8937380) at /root/newdb/mysql-server/sql/sql_tmp_table.cc:2306
warning: Source file is more recent than executable.
2306      trace_tmp.add("row_length",table->s->reclength).
(gdb) p table->s->reclength
$1 = 20
(gdb) p table->s->fields
$2 = 2
(gdb) p (*(table->field+0))->field_name
$3 = 0x7eff94010b0c "aid"
(gdb) p (*(table->field+1))->field_name
$4 = 0x7eff94007518 "num"
(gdb) p (*(table->field+0))->row_pack_length()
$5 = 4
(gdb) p (*(table->field+1))->row_pack_length()
$6 = 15
(gdb) p (*(table->field+0))->type()
$7 = MYSQL_TYPE_LONG
(gdb) p (*(table->field+1))->type()
$8 = MYSQL_TYPE_NEWDECIMAL
(gdb)

GDB 调试 Mysql 实战(二)GDB 调试打印相关推荐

  1. GDB 调试 Mysql 实战(一)源码编译安装

    下载源码 git clone https://github.com/mysql/mysql-server.git cd mysql-server git checkout 5.7 编译安装 安装依赖 ...

  2. VS2019调试查看变量_机器人调试(六十六)

    一.给VS2012MFC添加DOS窗口 在相关的.cpp中添加: #include #include 添加控制台初始化函数: 需要调用AllocConsole()函数 //控制台初始化void Ini ...

  3. Linux调试之(二)gdb+vmlinux

    文章目录 Linux调试之(二)gdb+vmlinux [1]定位arm-eabi-gdb 和 vmlinux [2]gdb加载内核符号表 1.查看内核符号表 2.执行arm-eabi-gdb vml ...

  4. Linux调试——gdb调试器的简单使用调试coredump文件

    文章目录 一.背景 二.gdb的指令与使用 1.gdb的基本指令. 2.gdb指令的简单使用 1.进入gdb模式 2.实例说明 三.调试coredump文件 前提:本质上是在调试程序崩溃之后的内存镜像 ...

  5. gdb的简单使用和gdb+gdbserver方式进行ARM程序调试

    gdb的简单使用 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但如果你是在 UNIX平台下做软件,你会发现GDB ...

  6. python gdb coredump_Linux段错误及GDB Coredump调试方法

    最近在Linux环境下做C语言项目,由于是在一个原有项目基础之上进行二次开发,而且项目工程庞大复杂,出现了不少问题,其中遇到最多.花费时间最长的问题就是著名的"段错误"(Segme ...

  7. linux嵌入式gdb调试指南,建立嵌入式gdb调试环境

    一.下载gdb-6.4.tar.gz源代码 http://ftp.gnu.org/gnu/gdb/ 二.编译 GDB #tar zxvf gdb-6.4.tar.gz 2.1.编译GDB Server ...

  8. GDB常用调试命令以及多进程多线程调试

    今天把所有的GDB常用的调试命令都过了一遍,尤其是多线程调试.所以先做个总结,以免忘记.以后如果有新的有用的命令会继续追加. 本文地址:(LYanger的博客:http://blog.csdn.net ...

  9. Linux上程序调试的基石(2)--GDB

    3. GDB的实现  GDB是GNU发布的一个强大的程序调试工具,用以调试C/C++程序.可以使程序员在程序运行的时候观察程序在内存/寄存器中的使用情况.它的实现也是基于ptrace系统调用来完成的. ...

最新文章

  1. SQL 语句之insert语句插入数据:若表中有重复的主键或数据继续插入解决方案
  2. 鸿蒙系统8xmax,华为8月9日发布基于安卓10.0全新系统:自主鸿蒙会同台亮相
  3. sql索引从入门到精通(十亿行数据测试报告)
  4. [driver]linux内核动态加载模块
  5. 中兴中心管理服务器fxh3120,中兴多媒体业务中心ZXMS80
  6. TP5:缩短访问路径和路由的使用——2
  7. base64_encode() 和 base64_decode() 8bit图片通过网络传输
  8. Extjs嵌入html
  9. 值得看的hadoop书籍
  10. groovy+mysql数据库_groovy - groovy连接数据库
  11. Python实现数据技术|爬虫便可获取免费百度文库付费文档
  12. echarts 叠加柱状图柱顶显示百分比
  13. 安全圈年终大趴,FIT 2019首日盛况全程回顾
  14. linux各文件夹作用
  15. Android 性能专项之 Memory Monitor 工具-memery
  16. WebVirtMgr + KVM 环境中的 Linux 虚拟机部署
  17. 迪士尼电影越来越受欢迎
  18. Improving Twitter Sentiment Classification Using Topic-Enriched Multi-Prototype Word Embeddings
  19. Java实现牛牛的水杯
  20. 人员离职it检查_公司软件开发人员离职信_检讨书

热门文章

  1. day16-筛选器以及Tab菜单示例
  2. (转)如何修改maven的默认jdk版本
  3. 贸易保护主义不能解决德国光伏企业的问题
  4. jquery源码分析(七)——事件模块 event(二)
  5. 蓝懿教育九月二十七日记录
  6. [转]Android敏捷开发指南
  7. 创客集结号_你知道单机片和Arduino之间的区别吗
  8. Redis和Memcache的区别是什么
  9. iOS LLDB调试命令(Low Lever Debug)
  10. camera摄像原理之三:色温和自动白平衡【转】