MySQL源代码阅读调试

Wesley13
• 阅读 718

最近需要阅读下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
1年前
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
小森森 小森森
5个月前
校园表白墙微信小程序V1.0 SayLove -基于微信云开发-一键快速搭建,开箱即用
后续会继续更新,敬请期待2.0全新版本欢迎添加左边的微信一起探讨!项目地址:(https://www.aliyun.com/activity/daily/bestoffer?userCodesskuuw5n)\2.Bug修复更新日历2.情侣脸功能大家不要使用了,现在阿里云的接口已经要收费了(土豪请随意),\\和注意
初识DevOps
基本概念和延伸的思考DevOps,是Development(开发)和Operations(运维)组成的复合词,一般译为“开发运维一体化”。看到这个概念,首先会产生几个问题:开发是什么,哪些环节是开发?运维是什么,哪些环节是运维?开发人员写好代码在本地调试,环境出问题了自己来调整,这是开发工作还是运维工作?系统故障后,运维人员发现是配置文件内容出错了就改成了正
SPDK QOS机制解析
本文关键词:intelspdkbdevqos序:intelspdk软件在存储领域应用广泛。因其可以高效管理linux系统的nvmessd盘,又支持vhostuser协议可以对接qemu虚拟机,在云计算领域通常被用来做本地盘云主机的存储管理软件。如此优秀的一款软件,有必要仔细分析其内部的实现机制,本篇文章主要介绍spdkqos机制。spdk
天翼云高可用虚拟IP(HAVIP)实践
(一)产品概述天翼云高可用虚拟IP(HighAvailabilityVirtualIPAddress,简称HAVIP)是一种可用独立创建和删除的私有网络IP地址资源。通过在VIPCIDR中申请一个私有网络IP地址,然后与高可用软件(如高可用软件Keepalived)配合使用,可用在VPC中搭建高可用的主备集群服务,提高VPC中服务的可用性。限制和说明
一个关于SDWAN单臂部署方案验证的实验
假设有这样一张网络,其中RTA和PCA表示某公司的A分支,通过中国电信CT路由器接入互联网ISP;RTB和PCB表示某公司的B分支,通过中国联通CU路由器接入互联网ISP。DNS(8.8.8.8)表示某互联网应用。为实现A分支私网192.168.2.0/24和B分支私网192.168.3.0/24的互通,现计划使用某厂商的SDWAN方案进打通两个内网,像下图
高性能API网关Kong介绍
本文关键词:高性能、API网关、Kong、微服务1.Introduction是随着微服务(Microservice)概念兴起的一种架构模式。原本一个庞大的单体应用(Allinone)业务系统被拆分成许多微服务(Microservice)系统进行独立的维护和部署,服务拆分带来的变化是API的规模成倍增长,API的管理难度也在日益增加,使用API网关发布和管
SPDK对接Ceph性能优化
关键词:SPDK、NVMeOF、Ceph、CPU负载均衡SPDK是intel公司主导开发的一套存储高性能开发套件,提供了一组工具和库,用于编写高性能、可扩展和用户态存储应用。它通过使用一些关键技术实现了高性能:1.将所有必需的驱动程序移到用户空间,以避免系统调用并且支持零拷贝访问2.IO的完成通过轮询硬件而不是依赖中断,以降低时延3.使用消息传递,以避免IO
3A网络 3A网络
5个月前
理解 virt、res、shr 之间的关系(linux 系统篇)
理解virt、res、shr之间的关系(linux系统篇)前言想必在linux上写过程序的同学都有分析进程占用多少内存的经历,或者被问到这样的问题——你的程序在运行时占用了多少内存(物理内存)?通常我们可以通过t
3A网络 3A网络
5个月前
开发一个不需要重写成 Hive QL 的大数据 SQL 引擎
开发一个不需要重写成HiveQL的大数据SQL引擎学习大数据技术的核心原理,掌握一些高效的思考和思维方式,构建自己的技术知识体系。明白了原理,有时甚至不需要学习,顺着原理就可以推导出各种实现细节。各种知识表象看杂乱无章,若只是学习