背景
在 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
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:
.
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 -uro