MySQL源代码阅读调试

Wesley13
• 阅读 1143

最近需要阅读下MySQL源代码,所以写这系列博客记录下。
搭调试环境真是比较蛋疼,公司基本Java开发,这里回到C++。。。
用了两天晚上,尝试了VS2013,Eclipse CDT,CodeBlock还有GDB。
GDB比较好搭建,将带Boost库的源代码扔到虚拟机(CentOS 7.0,安装配置桌面web服务器开发机配置,什么软件都装齐了)上,GDB命令搞得飞起。但是,GDB还是比较反人类的。整天对着一成不变的文字也没啥意思,而且找定义还得不断地find ./ -name “_*_“,比较烦。果断转到IDE上调试。

IDE初步调试结果:

我的台式机配置:伪四核,8G内存,windows 7,64位,百兆网卡
笔记本配置:伪八核,16G内存,windows 8,64位,百兆网卡
Eclipse CDT(台式机调试):调试超过16MB包返回时挂掉,build超级慢,慢的不要不要的。。。
CodeBlock(台式机调试):编译直接挂掉。。。
VS2013(台式机和笔记本都调试):性能一般,可以忍。

于是乎,最后选择VS2013

环境准备:

一切以官网说明为王道,在王道基础上扩展。
官网说明地址
需要:

  1. Git:用来拉代码,配置好环境变量指向安装目录的bin
  2. Cmake:我用的3.4.0,版本不要太低,否则无法生成VS2013的project。配置好环境变量指向安装目录的bin
  3. VS2013:VS2015的编译器比较特殊,配置麻烦,对于CMAKE不友好,还是用2013吧。
  4. Bison:语义解析器。我用的2.4.1。配置好环境变量指向安装目录的bin
  5. Perl: 5.16.2, 不知道为啥用Perl,应该是社区的人搞了一些自动检查和封装的脚本。配置好环境变量指向安装目录的bin
  6. MySQL源代码5.7.12,windows版,应该大家对与源代码的实时性要求没那么高,MySQL源代码原来由Bazzar管理,目前是github管理,有兴趣的可以去看看(https://github.com/mysql/mysql-server)。
  7. Boost 1.59: MySQL依赖Boost库,5.7.12对应Boost1.59(一定要这个版本),去官网下载,解压到一个路径中没有中文和空格的文件夹

构建项目

600+MB的代码:
MySQL源代码阅读调试
首先,拉好代码,同时准备另一个文件夹用于build(同样的,路径不能有中文名和空格)
MySQL源代码阅读调试

使用Cmake-gui:
MySQL源代码阅读调试

  1. 指定源代码路径,还有build路径。
  2. 点击Configure,选择项目为VS2013 64位(我的电脑是64位)
    MySQL源代码阅读调试
  3. 第一次会报错,提示找不到Boost,这时在参数with_BOOST中填写你刚才放Boost的路径。同时,勾选WITH_DEBUG(开启调试模式,程序会有DEBUG断点和信息),指定MySQL_DATADIR还有MySQL_KEYRINGDIR(也可以在之后的Build目录中的my.ini中指定)。配置好后,继续点击Configure
    MySQL源代码阅读调试
  4. 还是可能会报错,提示系统TEST_BIG_ENDIAN出错。没关系,将TEST_BIG_ENDIAN这个Test注释掉即可(具体位置忘了,看报错提示吧,是个configure.cmake文件)。
    MySQL源代码阅读调试
  5. 这下应该没问题了。Configure完成后点击Generate。生成VS2013Project。
  6. 双击ALLBUILD那个project文件。进入VS2013,进行编译。
  7. 在编译之前,最好将sql/sql_locale的编码改成带bom的UTF8, 防止不可解字符编译报错。
    MySQL源代码阅读调试
    这样,就可以编译了。编译时间比较长,我的本本跑了10分钟。编译完成。

安装运行调试

MySQL要想调试先要安装好,首先,看你的build目录的bin下面,是否所有的EXE都有了(主要是mysqld.exe)。
在Build根目录下填写my.ini,这里放个最简版:

# Here follows entries for some specific programs
# The MySQL server
[mysqld]
port  = 3306
socket  = MySQL
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M

#这里是我们修改的内容
basedir=E:/mysql-build     
datadir=E:/mysql-build/data    

#Server=L:/mysql-noinstall-5.1.62-win32/bin/mysqld-nt.exe

# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
# 
#skip-networking
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1
# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = C:\\mysql\\data\\
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = C:\\mysql\\data\\
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 256M
#innodb_additional_mem_pool_size = 20M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 64M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

之后,命令行输入:

mysqld --initialize

这个命令初始化你指定的数据目录下的数据(就是一些预设表),并且在目录下的.err文件中告诉你初始密码。
嗯,然后就可以启动啦

mysqld

这个是个阻塞进程,不要退出。

然后在VS中选择调试->附加到进程,选择你的mysqld进程(在data目录的pid文件可以看到PID),就可以开始调试啦。

启动mysqld可能会报错:

Assertion failed: 0, file D:\CProject\mysql-server\sql\mysqld.cc, line 4283
R6010
- abort() has been called
04:23:42 UTC - mysqld got exception 0x80000003 ;
This could be because you hit a bug. It is also possible that this binary
or one of the libraries it was linked against is corrupt, improperly built,
or misconfigured. This error can also be caused by malfunctioning hardware.
Attempting to collect some information that could help diagnose the problem.
As this is a crash and something is definitely wrong, the information
collection process might fail.

key_buffer_size=0
read_buffer_size=131072
max_used_connections=0
max_threads=151
thread_count=0
connection_count=0
It is possible that mysqld could use up to
key_buffer_size + (read_buffer_size + sort_buffer_size)*max_threads = 59879 K  b
ytes of memory
Hope that's ok; if not, decrease some variables in the equation.

Thread pointer: 0x0
Attempting backtrace. You can use the following information to find out
where mysqld died. If you see no messages after this, something went
terribly wrong...
13ff45385    mysqld.exe!my_sigabrt_handler()[my_thr_init.c:449]
14073f4df    mysqld.exe!raise()[winsig.c:594]
14073c3f0    mysqld.exe!abort()[abort.c:82]
140724d58    mysqld.exe!_wassert()[assert.c:156]
13f1c5177    mysqld.exe!test_lc_time_sz()[mysqld.cc:4283]
13f1c5511    mysqld.exe!win_main()[mysqld.cc:4536]
13f1c6067    mysqld.exe!mysql_service()[mysqld.cc:5035]
13f1c693f    mysqld.exe!mysqld_main()[mysqld.cc:5233]
13f1b803f    mysqld.exe!main()[main.cc:26]
14071f19c    mysqld.exe!__tmainCRTStartup()[crt0.c:255]
14071f2de    mysqld.exe!mainCRTStartup()[crt0.c:165]
76c45a4d    kernel32.dll!BaseThreadInitThunk()
76d7b831    ntdll.dll!RtlUserThreadStart()
The manual page at http://dev.mysql.com/doc/mysql/en/crashing.html contains
information that should help you find out what is causing the crash.

其实是一个DEBUG的断言错误,将sql\mysqld.cc第4283行的DBUG_ASSERT(0);改成DBUG_ASSERT(1);就行了

调试断点设置举例:
比如说查询语句断点,设在:
sql/sql_select.cc的
bool handle_query(THD *thd, LEX *lex, Query_result *result,
ulonglong added_options, ulonglong removed_options)这个函数;

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
KVM调整cpu和内存
一.修改kvm虚拟机的配置1、virsheditcentos7找到“memory”和“vcpu”标签,将<namecentos7</name<uuid2220a6d1a36a4fbb8523e078b3dfe795</uuid
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这