MySQL5.7源码在MacOS+Clion上的单步调试

上一次写了篇文章MySQL5.7源码在MacOS上的编译运行,是在commandLine上编译及运行,而这次我要在Clion上编译并且完成单步调试,然后就可以逐步拆解MySQL源码了

参考文章:MySQL 源码阅读 —— macOS CLion 编译调试 MySQL 5.7

步骤一:源码下载

首先,需要下载源码

打开 https://dev.mysql.com/downloads/mysql/,点击Looking for the latest GA version,按图中所选,点击Download
在这里插入图片描述
下载后解压缩包,tar -xzvf mysql-5.7.40.tar.gz,得到一个文件夹:mysql-5.7.40,里面就是源码

步骤二:下载boost包

打开 https://www.boost.org/,点击More Downloads…,点击Version History,找到1.59.0,点击Download
在这里插入图片描述
下载完后可以得到一个压缩包,然后解压,tar -zxvf boost_1_59_0.tar.gz,得到一个文件夹:boost_1_59_0

步骤三:源码导入Clion

当然,先得下载软件Clion,通过注册码验证,我买的是正版,这里就不分享具体过程了

点击File - Open,选择mysql-5.7.40文件夹,Open
导入后项目结构如下
在这里插入图片描述

步骤四:配置CMake

点击CLion - Preferences - Build, Execution, Deployment - CMake
在这里插入图片描述
CMAKE参数

-DWITH_DEBUG=1
-DWITH_BOOST=/Users/peteryuanpan/mysql/boost_1_59_0
-DCMAKE_INSTALL_PREFIX=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out
-DMYSQL_DATADIR=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data
-DSYSCONFDIR=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/etc
-DMYSQL_TCP_PORT=3307
-DMYSQL_UNIX_ADDR=mysql-debug.sock

点击Tools - CMake - Reset Cache and Reload Project
在这里插入图片描述
运行结果

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DWITH_DEBUG=1 -DWITH_BOOST=/Users/peteryuanpan/mysql/boost_1_59_0 -DCMAKE_INSTALL_PREFIX=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out -DMYSQL_DATADIR=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data -DSYSCONFDIR=/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/etc -DMYSQL_TCP_PORT=3307 -DMYSQL_UNIX_ADDR=mysql-debug.sock -DCMAKE_DEPENDS_USE_COMPILER=FALSE -G "CodeBlocks - Unix Makefiles" /Users/peteryuanpan/mysql/mysql-5.7.40
CMake Deprecation Warning at CMakeLists.txt:30 (CMAKE_MINIMUM_REQUIRED):
  Compatibility with CMake < 2.8.12 will be removed from a future version of
  CMake.

......

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug

[Finished]

步骤五:构建mysqld

找到 mysqld,点击构建按钮
会编译打包出mysqld相关的动态链接库
在这里插入图片描述
运行结果(这一步需要些时间)

====================[ Build | mysqld | Debug ]==================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug --target mysqld -- -j 3
Scanning dependencies of target zlib
Scanning dependencies of target strings
[  0%] Generating include/probes_mysql_dtrace.h, include/probes_mysql_nodtrace.h
[  0%] Building C object extra/zlib/zlib-1.2.12/CMakeFiles/zlib.dir/adler32.c.o
[  0%] Built target gen_dtrace_header
[  0%] Building C object extra/zlib/zlib-1.2.12/CMakeFiles/zlib.dir/compress.c.o

......

[ 50%] Building CXX object storage/innobase/CMakeFiles/innobase.dir/fut/fut0fut.cc.o
[ 50%] Building CXX object storage/perfschema/CMakeFiles/perfschema.dir/pfs_events_waits.cc.o
[ 50%] Building CXX object sql/CMakeFiles/binlog.dir/rpl_filter.cc.o
[ 50%] Building CXX object storage/innobase/CMakeFiles/innobase.dir/fut/fut0lst.cc.o
[ 50%] Building CXX object storage/perfschema/CMakeFiles/perfschema.dir/pfs_global.cc.o

......

[ 85%] Building CXX object sql/CMakeFiles/sql.dir/procedure.cc.o
[ 85%] Building CXX object sql/CMakeFiles/sql.dir/protocol_classic.cc.o
[ 85%] Building CXX object sql/CMakeFiles/sql.dir/records.cc.o
[ 85%] Building CXX object sql/CMakeFiles/sql.dir/rpl_group_replication.cc.o
[ 85%] Building CXX object sql/CMakeFiles/sql.dir/rpl_handler.cc.o

.......

Scanning dependencies of target mysqld
[100%] Building CXX object sql/CMakeFiles/mysqld.dir/main.cc.o
[100%] Linking CXX executable mysqld
[100%] Built target mysqld

Build finished

步骤六:初始化运行mysqld

点击Edit Configurations
在这里插入图片描述
找到mysqld,在Program arguments处输入–initialize-insecure,点击OK
在这里插入图片描述
然后运行 mysqld
在这里插入图片描述
运行结果(有错)

/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/sql/mysqld --initialize-insecure
mysqld: Can't create directory '/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data/' (Errcode: 2 - No such file or directory)
2022-10-26T09:42:00.421445Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-26T09:42:00.421934Z 0 [ERROR] Can't find error-message file '/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2022-10-26T09:42:00.423085Z 0 [ERROR] Aborting

有错误,这是因为文件夹cmake-build-debug/build_out/share没创建
在上面配置CMake过程中,指定了许多文件夹,需要将它们都创建一下

Command Line

mkdir -p cmake-build-debug/build_out cmake-build-debug/build_out/data cmake-build-debug/build_out/etc

然后再运行 mysqld

运行结果

/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/sql/mysqld --initialize-insecure
2022-10-26T09:44:18.416856Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-26T09:44:18.417402Z 0 [ERROR] Can't find error-message file '/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2022-10-26T09:44:18.418613Z 0 [Warning] Setting lower_case_table_names=2 because file system for /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data/ is case insensitive
2022-10-26T09:44:18.563112Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-10-26T09:44:18.650628Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-10-26T09:44:18.719189Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c2f88054-5512-11ed-8613-adb99ad55f5a.
2022-10-26T09:44:18.751552Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-10-26T09:44:18.910258Z 0 [Warning] 
2022-10-26T09:44:18.910274Z 0 [Warning] 
2022-10-26T09:44:18.910903Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-26T09:44:18.967925Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

Process finished with exit code 0

完成初始化,用户名是root,无密码

在Program arguments处删除–initialize-insecure,否则会报错!

然后以Debug方式运行mysqld
在这里插入图片描述
运行结果

/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/sql/mysqld
2022-10-26T09:46:22.168628Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-10-26T09:46:22.169287Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2022-10-26T09:46:22.169357Z 0 [Note] /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/sql/mysqld (mysqld 5.7.40-debug) starting as process 54634 ...
2022-10-26T09:46:22.169426Z 0 [ERROR] Can't find error-message file '/Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2022-10-26T09:46:22.172069Z 0 [Warning] Setting lower_case_table_names=2 because file system for /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data/ is case insensitive
2022-10-26T09:46:22.174448Z 0 [Note] InnoDB: !!!!!!!! UNIV_DEBUG switched on !!!!!!!!!
2022-10-26T09:46:22.174474Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2022-10-26T09:46:22.174479Z 0 [Note] InnoDB: Uses event mutexes
2022-10-26T09:46:22.174485Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
2022-10-26T09:46:22.174491Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.12
2022-10-26T09:46:22.175028Z 0 [Note] InnoDB: Number of pools: 1
2022-10-26T09:46:22.175232Z 0 [Note] InnoDB: Using CPU crc32 instructions
2022-10-26T09:46:22.176898Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2022-10-26T09:46:22.227774Z 0 [Note] InnoDB: Completed initialization of buffer pool
2022-10-26T09:46:22.244214Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2022-10-26T09:46:22.278665Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2022-10-26T09:46:22.278955Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2022-10-26T09:46:22.290362Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2022-10-26T09:46:22.296979Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2022-10-26T09:46:22.297003Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2022-10-26T09:46:22.298246Z 0 [Note] InnoDB: Waiting for purge to start
2022-10-26T09:46:22.352425Z 0 [Note] InnoDB: 5.7.40 started; log sequence number 2755482
2022-10-26T09:46:22.352751Z 0 [Note] InnoDB: Loading buffer pool(s) from /Users/peteryuanpan/mysql/mysql-5.7.40/cmake-build-debug/build_out/data/ib_buffer_pool
2022-10-26T09:46:22.353131Z 0 [Note] Plugin 'FEDERATED' is disabled.
2022-10-26T09:46:22.362113Z 0 [Note] InnoDB: Buffer pool(s) load completed at 221026 17:46:22
2022-10-26T09:46:22.377191Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2022-10-26T09:46:22.377222Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2022-10-26T09:46:22.377240Z 0 [Warning] 
2022-10-26T09:46:22.377245Z 0 [Warning] 
2022-10-26T09:46:22.377946Z 0 [Warning] CA certificate ca.pem is self signed.
2022-10-26T09:46:22.378030Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2022-10-26T09:46:22.378307Z 0 [Note] Server hostname (bind-address): '*'; port: 3307
2022-10-26T09:46:22.378684Z 0 [Note] IPv6 is available.
2022-10-26T09:46:22.378728Z 0 [Note]   - '::' resolves to '::';
2022-10-26T09:46:22.378836Z 0 [Note] Server socket created on IP: '::'.
2022-10-26T09:46:22.419539Z 0 [Note] Event Scheduler: Loaded 0 events
2022-10-26T09:46:22.419884Z 0 [Note] 

运行成功,进程正在监听外部请求

步骤七:单步调试

找到文件sql_executor.cc,在do_select方法内部打一个断点
在这里插入图片描述
使用命令行,连接mysql
输入命令:show databases;

➜  mysql -uroot -P3307 -h127.0.0.1
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.40-debug Source distribution

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;

Clion这边应该有反应,断点进入
在这里插入图片描述
接下来就可以Debug + 单步调试了

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值