MySQL 8.0 GDB源码调试

想要调试源码,需要使用编译过的MySQL版本,在编译的时候加上-DWITH_DEBUG=1参数,这样才可以在调试的时候在对应的函数上面加上断点。

在官网下载MySQL 8.0.42版本的社区源码包。

wget https://downloads.mysql.com/archives/get/p/23/file/mysql-8.0.42.tar.gz

tar xfz mysql-8.0.42.tar.gz

编译还需要boost包,可以手动下载并解压到相应的目录下。

wget https://archives.boost.io/release/1.77.0/source/boost_1_77_0.tar.bz2

bzip2 -d boost_1_77_0.tar.bz2

tar xf boost_1_77_0.tar

安装cmake需要的一些包。

yum install devtoolset-11-gcc devtoolset-11-gcc-c++ devtoolset-11-binutils

创建目录

mkdir /data/mysql_software_80_42
mkdir /data/mysql_data_3315
mkdir -p /data/mysql_software_80_boost

cd mysql-8.0.42
mkdir build
cd build/

开始编译

cmake .. -DWITH_DEBUG=1 -DWITH_BOOST=/data/mysql_software_80_boost/boost_1_77_0 \
 -DCMAKE_INSTALL_PREFIX=/data/mysql_software_80_42 \
 -DMYSQL_DATADIR=/data/mysql_data_3315 \
 -DDEFAULT_CHARSET=utf8 \
 -DDEFAULT_COLLATION=utf8_general_ci \
 -DEXTRA_CHARSETS=all \
 -DMYSQL_UNIX_ADDR=/data/mysql_data_3315/mysql.sock \
 -DMYSQL_TCP_PORT=3315 \
 -DSYSCONFDIR=/etc \
 -DENABLED_LOCAL_INFILE=ON \
 -DCOMPILATION_COMMENT='MySQL 8.0.42 test environment'

make的时候,可以并发进行,建议留足少量核数,配置低的主机可能会打满内存。

make -j3
make install

查看mysqld版本

显示debug表面可以支持调试模式

[root@node1 mysql_data_3315]# /data/mysql_software_80_42/bin/mysqld --verbose --version
/data/mysql_software_80_42/bin/mysqld  Ver 8.0.42-debug for Linux on x86_64 (MySQL 8.0.42 test environment)

初始化数据库实例,启动服务并修改密码

/data/mysql_software_80_42/bin/mysqld --defaults-file=/etc/my_80_3315.cnf --initialize --user=mysql

/data/mysql_software_80_42/bin/mysqld_safe --defaults-file=/etc/my_80_3315.cnf --user=mysql &

查询到实例的进程id

(base) [root@node1 mysql_data_3315]# ps -ef|grep mysqld
root      6020  1906  0 16:54 pts/0    00:00:00 grep --color=auto mysqld
root     16245  1906  0 11:38 pts/0    00:00:00 /bin/sh /data/mysql_software_80_42/bin/mysqld_safe --defaults-file=/etc/my_80_3315.cnf --user=mysql
mysql    17058 16245  0 11:38 pts/0    00:00:47 /data/mysql_software_80_42/bin/mysqld --defaults-file=/etc/my_80_3315.cnf --basedir=/data/mysql_software_80_42 --datadir=/data/mysql_data_3315 --plugin-dir=/data/mysql_software_80_42/lib/plugin --user=mysql --log-error=/data/mysql_data_3315/error.log --open-files-limit=65535 --pid-file=/data/mysql_data_3315/mysqld.pid --socket=/data/mysql_data_3315/mysql.sock --port=3315

再开启一个终端创建,在测试表上开启一个mysql会话。

准备好创建一个索引的语句,等待gdb开启后输入。

之后启动gdb并关联到这个进程

[root@node1 mysql_data_3315]# gdb -p 17058
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-120.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/>.
Attaching to process 17058
Reading symbols from /data/mysql_software_80_42/bin/mysqld...
..
(gdb) 

在索引树的创建函数btr_create上打断点

(gdb) break btr_create
Breakpoint 1 at 0x4f671cd: file /data/mysql-8.0.42/storage/innobase/btr/btr0btr.cc, line 840.
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000004f671cd in btr_create(unsigned long, unsigned int, unsigned long, dict_index_t*, mtr_t*) 
                                                   at /data/mysql-8.0.42/storage/innobase/btr/btr0btr.cc:840

在另外的终端,为测试表增加一个字段

root [test]> alter table test add phone_number varchar(30), algorithm = copy;

进入断点,查看函数的调用路径

(gdb) c
Continuing.
[Switching to Thread 0x7f4ea8268700 (LWP 8998)]

Breakpoint 1, btr_create (type=259, space=7, index_id=18446744073709551615, index=0x7f4e8819c528, mtr=0x7f4ea825f990)
    at /data/mysql-8.0.42/storage/innobase/btr/btr0btr.cc:840
840	  ut_ad(index_id != BTR_FREED_INDEX_ID);
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000004f671cd in btr_create(unsigned long, unsigned int, unsigned long, dict_index_t*, mtr_t*) 
                                                   at /data/mysql-8.0.42/storage/innobase/btr/btr0btr.cc:840
	breakpoint already hit 1 time
(gdb) backtrace
#0  btr_create (type=259, space=7, index_id=18446744073709551615, index=0x7f4e8819c528, mtr=0x7f4ea825f990)
    at /data/mysql-8.0.42/storage/innobase/btr/btr0btr.cc:840
#1  0x0000000004f730b9 in btr_sdi_create (space_id=7, mtr=0x7f4ea825f990, table=0x7f4e8819a4c8)
    at /data/mysql-8.0.42/storage/innobase/btr/btr0btr.cc:4685
#2  0x0000000004f731c6 in btr_sdi_create_index (space_id=7, dict_locked=false) at /data/mysql-8.0.42/storage/innobase/btr/btr0btr.cc:4715
#3  0x0000000005075993 in dict_build_tablespace_for_table (table=0x7f4e881986e8, create_info=0x7f4ea82650f0, trx=0x7f4ec028bff8)
    at /data/mysql-8.0.42/storage/innobase/dict/dict0crea.cc:301
#4  0x0000000005074ebc in dict_build_table_def (table=0x7f4e881986e8, create_info=0x7f4ea82650f0, trx=0x7f4ec028bff8)
    at /data/mysql-8.0.42/storage/innobase/dict/dict0crea.cc:86
#5  0x0000000004dc9743 in row_create_table_for_mysql (table=@0x7f4ea8260ca0: 0x7f4e881986e8, compression=0x0, 
    create_info=0x7f4ea82650f0, trx=0x7f4ec028bff8, heap=0x7f4e881991a0) at /data/mysql-8.0.42/storage/innobase/row/row0mysql.cc:2763
#6  0x0000000004b9c55c in create_table_info_t::create_table_def (this=0x7f4ea8260f90, dd_table=0x7f4e8801eef0, old_part_table=0x0)
    at /data/mysql-8.0.42/storage/innobase/handler/ha_innodb.cc:11837
#7  0x0000000004b72e4d in create_table_info_t::create_table (this=0x7f4ea8260f90, dd_table=0x7f4e8801eef0, old_part_table=0x0)
    at /data/mysql-8.0.42/storage/innobase/handler/ha_innodb.cc:13806
#8  0x0000000004b9e692 in innobase_basic_ddl::create_impl<dd::Table> (thd=0x7f4e88006990, name=0x7f4ea82641d4 "./test/#sql-42a2_e", 
    form=0x7f4ea8261c00, create_info=0x7f4ea82650f0, dd_tab=0x7f4e8801eef0, file_per_table=true, evictable=true, skip_strict=false, 
    old_flags=0, old_flags2=0, old_part_table=0x0) at /data/mysql-8.0.42/storage/innobase/handler/ha_innodb.cc:14157
#9  0x0000000004b75550 in ha_innobase::create (this=0x7f4e881964b0, name=0x7f4ea82641d4 "./test/#sql-42a2_e", form=0x7f4ea8261c00, 
    create_info=0x7f4ea82650f0, table_def=0x7f4e8801eef0) at /data/mysql-8.0.42/storage/innobase/handler/ha_innodb.cc:15122
#10 0x000000000387d06c in handler::ha_create (this=0x7f4e881964b0, name=0x7f4ea82641d4 "./test/#sql-42a2_e", form=0x7f4ea8261c00, 
    info=0x7f4ea82650f0, table_def=0x7f4e8801eef0) at /data/mysql-8.0.42/sql/handler.cc:5051
#11 0x000000000387d6dc in ha_create_table (thd=0x7f4e88006990, path=0x7f4ea82641d4 "./test/#sql-42a2_e", db=0x7f4e8801bc68 "test", 
    table_name=0x7f4ea82635f0 "#sql-42a2_e", create_info=0x7f4ea82650f0, update_create_info=false, is_temp_table=true, 
    table_def=0x7f4e8801eef0) at /data/mysql-8.0.42/sql/handler.cc:5219
#12 0x0000000003593829 in mysql_alter_table (thd=0x7f4e88006990, new_db=0x7f4e8801bc68 "test", new_name=0x0, create_info=0x7f4ea82650f0, 
    table_list=0x7f4e8801b618, alter_info=0x7f4ea8264f80) at /data/mysql-8.0.42/sql/sql_table.cc:17681
#13 0x0000000003bc184b in Sql_cmd_alter_table::execute (this=0x7f4e8801c190, thd=0x7f4e88006990)
    at /data/mysql-8.0.42/sql/sql_alter.cc:350
#14 0x00000000034ac6c3 in mysql_execute_command (thd=0x7f4e88006990, first_level=true) at /data/mysql-8.0.42/sql/sql_parse.cc:4724
#15 0x00000000034aea80 in dispatch_sql_command (thd=0x7f4e88006990, parser_state=0x7f4ea8266890)
    at /data/mysql-8.0.42/sql/sql_parse.cc:5385
#16 0x00000000034a46a4 in dispatch_command (thd=0x7f4e88006990, com_data=0x7f4ea8267980, command=COM_QUERY)
    at /data/mysql-8.0.42/sql/sql_parse.cc:2055
#17 0x00000000034a25ef in do_command (thd=0x7f4e88006990) at /data/mysql-8.0.42/sql/sql_parse.cc:1440
#18 0x00000000036c3379 in handle_connection (arg=0xd0a7ad0) at /data/mysql-8.0.42/sql/conn_handler/connection_handler_per_thread.cc:303
#19 0x00000000056dddbe in pfs_spawn_thread (arg=0xd0af160) at /data/mysql-8.0.42/storage/perfschema/pfs.cc:3050
#20 0x00007f4ed0f2aea5 in start_thread () from /lib64/libpthread.so.0
---Type <return> to continue, or q <return> to quit---
#21 0x00007f4ecf6ba9fd in clone () from /lib64/libc.so.6
(gdb) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值