MySQL easy start series — the second station uses visual studio to debug MySQL at source level

Time:2020-9-22

1: Background

1. Tell a story

The last article talked about the architecture of MySQL. Many students said that it was not enjoyable. After all, they still listen to me to tell stories. Then this article talks about how to use visual studio to debug MySQL at source level. After all, in front of the source code, there is no privacy. In front of the saints, they are all ants.

2: Tools Collection

MySQL is written by C + +. If you want to compile it on windows, you need to download several necessary tools.

  • mysql-5.7.12.zip
  • cmake-3.17.3-win64-x64.msi
  • boost_1_59_0.tar.gz
  • bison-2.4.1-setup.exe
  • windows 10 x64

Here’s a simple statement: it can be usedcmakeGenerate source code*.slnOpenable solutions, for example, can be generated through itMySQL.slnboostIt is a very powerful basic library in C + +,bisonA popular parser program, used to provide parsing for MySQL, and finally download the correct version of MySQL5.7.12

3、 Detailed installation

I will write more detailed, after all, I also spent the whole afternoon, poor (┬)

1. Cmake-3.17.3-win64-x64.msi and bison-2.4.1- setup.exe

Cmake and bison are more convenient to install. One click installation is OK. However, there is a big pit here. Please pay attention to the installationBisonDo not use the default path, because the default path has a space, it will cause you later vs compilation stuck, and do not show what reason, Keqi!!! So I changed to custom:C:\2\GnuWin32

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

Last but not leastcmakeandbisonThe bin file is in the environment variable.

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

2. mysql-5.7.12.zip

I use it hereC:\2As the root folder, all the widgets are here, as shown in the following figure:

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

Nextmysql-5.7.12.zipUnzip it, then enter the unzipped folder, create a new boost folder, andboost_1_59_0.tar.gzPut it in it, and then create a new onebreleaseThe folder can be used to hold the final generatedMySql.sln。😄😄😄。

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level
MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

3. Cmake compilation

When you’re ready, you can start cmake compilation.


PS C:\mysql-5.7.12\brelease> cmake ..  -DDOWNLOAD_BOOST=1 -DWITH_BOOST="C:\mysql-5.7.12\boost\boost_1_59_0.tar.gz"
-- Building for: Visual Studio 16 2019
CMake Deprecation Warning at CMakeLists.txt:26 (CMAKE_POLICY):
  The OLD behavior for policy CMP0018 will be removed from a future version
  of CMake.
-- Cannot find wix 3, installer project will not be generated
-- COMPILE_DEFINITIONS: _WIN32_WINNT=0x0601;WIN32_LEAN_AND_MEAN;NOGDI;NOMINMAX;HAVE_CONFIG_H
-- CMAKE_C_FLAGS: /DWIN32 /D_WINDOWS /W3 /MP /wd4800 /wd4805 /wd4996
-- CMAKE_CXX_FLAGS: /DWIN32 /D_WINDOWS /W3 /GR /EHsc /MP /wd4800 /wd4805 /wd4996 /we4099
-- CMAKE_C_FLAGS_DEBUG: /MTd /Z7 /Ob1 /Od /RTC1 /EHsc -DENABLED_DEBUG_SYNC -DSAFE_MUTEX
-- CMAKE_CXX_FLAGS_DEBUG: /MTd /Z7 /Ob1 /Od /RTC1 /EHsc -DENABLED_DEBUG_SYNC -DSAFE_MUTEX
-- CMAKE_C_FLAGS_RELWITHDEBINFO: /MT /Z7 /O2 /Ob1 /DNDEBUG /EHsc -DDBUG_OFF
-- CMAKE_CXX_FLAGS_RELWITHDEBINFO: /MT /Z7 /O2 /Ob1 /DNDEBUG /EHsc -DDBUG_OFF
-- Configuring done
-- Generating done
-- Build files have been written to: C:/2/mysql-5.7.12/brelease

When you see the last sentenceBuild files have been written to: C:/2/mysql-5.7.12/breleaseCongratulations,MySQL.slnIt’s generated.

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

4. Open MySQL.sln Compile project

My computer is installed withvisual studio 2019Open it nextMySql.SlnOverall compilation, need to wait more than ten minutes, see the following output even if the installation is successful.

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

3: Start MySQL and debug insert

1. Initialization of MySQL

There are two things to do here. The first thing is to open the debugging mode of MySQL, and the second thing is to attach--initializeStart parameters.

<1> MySQL debugging mode on

modifyC:\2\mysql-5.7.12\sql\mysqld.ccMediumtest_lc_time_szMethod DBUG_ASSERT(0);Change to DBUG_ASSERT(1);As shown in the figure below:

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

<2> The command of vs adds the start parameter

As we all know in the last article, the mysqld project is the startup project of MySQL, and the main function is also included in it. Before F5 debugging, the initialization parameter is added--console --initializeAs shown in the following figure:

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

2. Continue to enter and exit the pit

After starting, there are 103 reports of error, which is very annoying… The error message should be a coding problem, as shown in the following figure:

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

It is also very simple to modifyC:\2\mysql-5.7.12\sqlsql_locale.ccSave it in [UTF-8 + BOM] format, and thenmysqldProject rebuildCtrl+F5Run directly, and finally, thank God, you can see from the output that it’s done… It’s not easy.

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

As can be seen from the above figure, the default password is:zJDE>IC5o+yaFirst record the password, then remove — initialize in commandline arguments, and then restart the console.

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

As you can see, port 3306 is turned on, and then use thezJDE>IC5o+yaJust connect. Here I use Navicat.

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

After connecting, you will be prompted to change the default password123456It’s all done~~~

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

3. Keep tracking write_ Row

We tracked down the last onewrite_rowAt that time, I said it was a virtual method called by the underlying specific storage engine. The code is as follows:

int handler::ha_write_row(uchar *buf)
{
    MYSQL_TABLE_IO_WAIT(m_psi, PSI_TABLE_WRITE_ROW, MAX_KEY, 0,{ error= write_row(buf); })
}

//This is a virtual method
virtual int write_row(uchar *buf __attribute__((unused)))
{
    return HA_ERR_WRONG_COMMAND;
}

In the end, this time I’ll debug it myself to show you the evidence first… For convenience, I generate a create table SQL.

drop database if exists `datamip`;
create database `datamip`;
drop table if exists `datamip`.`customer`;
create table `datamip`.`customer` (
 `customerID` int NOT NULL AUTO_ Increment comment 'Auto increment primary key',
 `Customername ` varchar (50) comment 'user name',
 `Email 'varchar (50) comment' email address',
 `Desc ` varchar (50) comment 'description',
 primary key (`customerID`)
) ENGINE=InnoDB charset=utf8 collate=utf8_bin;

Next, we look carefully, in the source codeint handler::ha_write_row(uchar *buf)Method to the next breakpoint and F5 to debug the application.

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

Next, you can execute the insert operation, which will call the breakpoint.


insert into  `datamip`.`customer`(customerName,email,`desc`) values('mary','[email protected]','vip');

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

As you can see, the breakpoint is hit, and then step into debugging. Finally, you will see that the code will enter theC:\2\mysql-5.7.12\storage\innobase\handler\ha_innodb.ccMediumint ha_innobase::write_rowMethods, as shown in the following figure:

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

Then look for a few local variables and call stacks…

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

MySQL easy start series -- the second station uses visual studio to debug MySQL at source level

4: Summary

This is the guide that I spent the whole afternoon summarizing, hoping to help you save time, or that sentence, in front of the source code, do not talk about privacy, if you can still debug, everything is a mole ant!

Recommended Today

How to import application service in Choerodon platform

Through the previous article “the application life cycle management of the Choerodon pig toothed fish practice”, we have a basic understanding of the characteristics of application services and microservice architecture in the Choerodon platform. On this basis, this paper will introduce the function of importing application services in the Choerodon platform. Background of importing application […]