MySQL源代码阅读调试 - 1. 环境搭建

最近需要阅读下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源代码阅读调试 - 1. 环境搭建_第1张图片
首先,拉好代码,同时准备另一个文件夹用于build(同样的,路径不能有中文名和空格)
MySQL源代码阅读调试 - 1. 环境搭建_第2张图片

使用Cmake-gui:
MySQL源代码阅读调试 - 1. 环境搭建_第3张图片

  1. 指定源代码路径,还有build路径。
  2. 点击Configure,选择项目为VS2013 64位(我的电脑是64位)
    MySQL源代码阅读调试 - 1. 环境搭建_第4张图片
  3. 第一次会报错,提示找不到Boost,这时在参数with_BOOST中填写你刚才放Boost的路径。同时,勾选WITH_DEBUG(开启调试模式,程序会有DEBUG断点和信息),指定MySQL_DATADIR还有MySQL_KEYRINGDIR(也可以在之后的Build目录中的my.ini中指定)。配置好后,继续点击Configure
    MySQL源代码阅读调试 - 1. 环境搭建_第5张图片
  4. 还是可能会报错,提示系统TEST_BIG_ENDIAN出错。没关系,将TEST_BIG_ENDIAN这个Test注释掉即可(具体位置忘了,看报错提示吧,是个configure.cmake文件)。
    MySQL源代码阅读调试 - 1. 环境搭建_第6张图片
  5. 这下应该没问题了。Configure完成后点击Generate。生成VS2013Project。
  6. 双击ALLBUILD那个project文件。进入VS2013,进行编译。
  7. 在编译之前,最好将sql/sql_locale的编码改成带bom的UTF8, 防止不可解字符编译报错。
    MySQL源代码阅读调试 - 1. 环境搭建_第7张图片
    这样,就可以编译了。编译时间比较长,我的本本跑了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=, MASTER_PORT=,
#    MASTER_USER=, MASTER_PASSWORD= ;
#
#    where you replace , ,  by quoted strings and
#     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     =   
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user     =   
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password =   
#
# The port the master is listening on.
# optional - defaults to 3306
#master-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)这个函数;

你可能感兴趣的:(MySQL相关)